Files
ai4se-harness-suite/agents-workflow-engine/agents_workflow_engine/validate.py
ning-inspire 6388958a80 feat(engine): 提示词调用 + 执行者/门禁两模型 + 三种执行方式 + 版本号
改了什么
- 调 agent 不再拼 --skill/--node/--task:skill 名与 SKILL.md 相对路径写进提示词,
  子进程 cwd 改成目标项目根(原来在引擎目录,相对路径找不到)
- 模型收到图级 models:executor / gate,按节点 kind 自动分配,环境变量可覆盖;
  删掉 13 处节点级 model:,对抗验证检查简化为「两个模型是否相同」
- run 启动三选一:会话内开子 agent(默认)/ 只用主会话(--solo)/ 独立 agent(--agent);
  三个入口互斥,选独立 agent 不给 --agent 直接报错
- 新增 --version 与 version.py(0.2.0)、CHANGELOG.md;decisions.jsonl 首行记引擎版本
- install.sh 读目标项目的 version.py 并与本预设对比,不一致直接提示(预设与引擎必须成对升级)

为什么
- 真实 CLI 没有认 --skill 的,改成提示词后不用再为每个 CLI 写包装脚本
- 执行者与门禁两个模型做对抗验证,图里一处配完,节点不再重复声明
- 当前会话能开子 agent 时不必拉进程,少一层进程边界

怎么验证的
- 单元测试 25 → 46,全绿(python3 agents-workflow-engine/tests/test_units.py)
- ./.agents/verify.sh 全部通过(72 项),166 条相对链接有效
- 假 agent 真跑:argv 为 -q --model <模型> <提示词>、pwd 等于项目根、
  执行者 10 次 / 门禁 2 次;--solo 真跑一遍核对 prompts.md 抬头与话术

文档同步检查
- 已更新:agents-workflow-engine/README.md、CHANGELOG.md、AGENTS.md(§8 §10 §12 §13)、
  .agents/plan 三份记录(agent-protocol / default-session-subagent / version)
- 不涉及:agile-team-sdd、catalog.json、placeholders.md;graph.html 与泳道标题不动
2026-11-19 23:20:37 +08:00

185 lines
8.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""校验解析后的图。错误阻断,模型未拉开只警告。"""
from __future__ import annotations
from . import models
WORK_KINDS = ("action", "gate", "decision", "human_input", "milestone")
ACTORS = ("human", "master", "subagent")
def node_map(graph: dict) -> dict:
return {node["id"]: node for node in graph.get("nodes") or []}
def extension_ids(graph: dict) -> set:
found = set()
for ext in graph.get("extensions") or []:
for node in ext.get("nodes") or []:
if "id" in node:
found.add(node["id"])
return found
def _model_value(graph: dict, node: dict) -> str:
key = node.get("model")
models = graph.get("models") or {}
if not key or key not in models:
return ""
spec = models[key]
if isinstance(spec, dict) and spec.get("env"):
return os.environ.get(str(spec["env"]), "")
return str(spec)
def validate(graph: dict) -> tuple:
"""返回 (errors, warnings)。errors 非空时图不能跑。"""
errors = []
warnings = []
nodes = graph.get("nodes") or []
edges = graph.get("edges") or []
ids = [node.get("id") for node in nodes]
if len(ids) != len(set(ids)):
errors.append("节点 id 有重复")
by_id = {}
for node in nodes:
node_id = node.get("id")
if not node_id:
errors.append("有节点缺少 id")
continue
by_id[node_id] = node
kind = node.get("kind")
actor = node.get("actor")
if kind not in WORK_KINDS and kind not in ("entry", "end"):
errors.append("节点 %s 的 kind 不合法:%s" % (node_id, kind))
if actor not in ACTORS:
errors.append("节点 %s 的 actor 必须是 human、master 或 subagent" % node_id)
if node.get("skill") and actor != "subagent":
errors.append("节点 %s 调用了 skill,actor 必须是 subagent" % node_id)
if kind == "end" and _outgoing(edges, node_id):
errors.append("终点 %s 不能有出边" % node_id)
if kind == "milestone":
if actor != "master":
errors.append("里程碑 %s 的 actor 必须是 master" % node_id)
if node.get("skill") or node.get("options"):
errors.append("里程碑 %s 不能带 skill 或 options" % node_id)
if len(_outgoing(edges, node_id)) != 1:
errors.append("里程碑 %s 必须恰好一条出边" % node_id)
if kind == "human_input" and len(_outgoing(edges, node_id)) > 1:
errors.append("人类输入 %s 只能有一条出边" % node_id)
if kind == "decision":
options = set(node.get("options") or [])
outgoing = _outgoing(edges, node_id)
for edge in outgoing:
if edge.get("when") not in options:
errors.append("决策 %s 的出边 %s 不在选项里" % (node_id, edge.get("id")))
for when in set(edge.get("when") for edge in outgoing):
group = [edge for edge in outgoing if edge.get("when") == when]
main = [edge for edge in group if not edge.get("parallel")]
if len(main) != 1:
errors.append(
"决策 %s 的「%s」需要恰好一条主出边;并行分支要标 parallel: true"
% (node_id, when)
)
for edge in outgoing:
if edge.get("parallel") and edge.get("back"):
errors.append("并行边 %s 不能同时是打回边" % edge.get("id"))
if kind == "gate":
for edge in _outgoing(edges, node_id):
if edge.get("when") not in ("满意", "不满意"):
errors.append("门禁 %s 的出边 %s 只能是满意或不满意" % (node_id, edge.get("id")))
if node.get("join"):
if kind in ("entry", "end"):
errors.append("节点 %s 的 kind 不能做汇入点" % node_id)
for edge in edges:
if edge.get("to") != node_id or edge.get("back"):
continue
if edge.get("when"):
errors.append("汇入节点 %s 的入边 %s 不能带 when" % (node_id, edge.get("id")))
entries = [node["id"] for node in nodes if node.get("kind") == "entry"]
ends = [node["id"] for node in nodes if node.get("kind") == "end"]
if entries != [graph.get("entry")]:
errors.append("入口必须恰好是 entry 节点 %s" % graph.get("entry"))
if len(ends) != 1:
errors.append("必须恰好有一个终点")
ext = extension_ids(graph)
notes = set()
for node in nodes:
if node.get("sealed"):
for item in node.get("note") or []:
notes.add(item)
known = set(by_id) | ext | notes
loops = graph.get("loops") or []
back_to_loop = {}
for loop in loops:
if "maxIterations" not in loop:
errors.append("回路 %s 缺少 maxIterations" % loop.get("id"))
for edge_id in loop.get("backEdges") or []:
back_to_loop[edge_id] = loop
for edge in edges:
edge_id = edge.get("id")
src = edge.get("from")
dst = edge.get("to")
if src not in by_id:
errors.append("边 %s 的起点不存在:%s" % (edge_id, src))
if dst in notes:
errors.append("边 %s 指向了封死节点内部的步骤 %s,不能加边" % (edge_id, dst))
elif dst in ext:
errors.append("边 %s 指向了未接入的扩展节点 %s" % (edge_id, dst))
elif dst not in by_id:
errors.append("边 %s 的终点不存在:%s" % (edge_id, dst))
if edge.get("back") and edge.get("when") and edge_id not in back_to_loop:
errors.append("打回边 %s 没有写入任何回路的上限" % edge_id)
if src in by_id and by_id[src].get("kind") == "gate":
if edge.get("when") not in ("满意", "不满意"):
errors.append("边 %s 从门禁出发,when 只能是满意或不满意" % edge_id)
if src in by_id and by_id[src].get("kind") not in ("decision", "gate") and edge.get("when"):
errors.append("边 %s 从非决策/门禁节点出发,不能带 when" % edge_id)
if edge.get("parallel") and (src not in by_id or by_id[src].get("kind") != "decision"):
errors.append("边 %s 标了 parallel,但起点不是决策" % edge_id)
batches = {node["id"] for node in nodes if node.get("batch")}
consumed = {node.get("repeatsBatch") for node in nodes if node.get("repeatsBatch")}
missing = batches - consumed
if missing:
errors.append("批量节点没有被消费:%s" % "、".join(sorted(missing)))
for node in nodes:
target = node.get("repeatsBatch")
if target and target not in by_id:
errors.append("节点 %s 要循环的批量节点不存在:%s" % (node["id"], target))
if graph.get("entry") in by_id and not errors:
reached = _reachable(graph["entry"], edges)
stranded = [node_id for node_id in by_id if node_id not in reached]
if stranded:
errors.append("这些节点从入口走不到:%s。覆盖可能把图切断了" % "、".join(stranded))
executor_model = models.model_of(graph, "executor")
gate_model = models.model_of(graph, "gate")
if not executor_model or not gate_model:
warnings.append("未配置模型:执行者或门禁没写 model,将沿用主会话模型")
elif executor_model == gate_model:
warnings.append("未做对抗验证:执行者与门禁用了同一个模型(%s)" % executor_model)
return errors, warnings
def _outgoing(edges: list, node_id: str) -> list:
return [edge for edge in edges if edge.get("from") == node_id]
def _reachable(entry: str, edges: list) -> set:
seen = set()
stack = [entry]
while stack:
current = stack.pop()
if current in seen:
continue
seen.add(current)
for edge in edges:
if edge.get("from") == current:
stack.append(edge.get("to"))
return seen