feat: 工作流引擎接入 QA 并行用例与 E2E 测试回路
- 预设新增 qa-write-cases 并行侧分支、qa-run-e2e 汇入点与三选一测试决策; 提测改为里程碑,dev-plan 补充详细设计/任务清单/自测用例说明 - 引擎支持 parallel 分支、join 汇入、milestone 节点与任意选项决策 - 更新校验、Mermaid 渲染、schema、README 与 AGENTS 模块职责 - 预览图同步新节点、并行/打回走线与测试验收阶段 验证:agents-workflow-engine/tests/test_units.py 24/24;./.agents/verify.sh 67/67 文档同步检查:已更新(引擎 README、根 README、AGENTS.md、schema)
This commit is contained in:
@@ -1,6 +1,15 @@
|
||||
# agents-workflow-engine
|
||||
|
||||
一个人用主会话把一条迭代从启动调度到提测。图是 YAML。人、graph-master、子 agent 分三道泳道。子 agent 调用 skill,并且可以绑不同模型做对抗验证。
|
||||
一个人用主会话把一条迭代从启动调度到测试通过。图是 YAML。人、graph-master、子 agent 分三道泳道。子 agent 调用 skill,并且可以绑不同模型做对抗验证。
|
||||
|
||||
预设的主线:
|
||||
|
||||
```
|
||||
需求规格 → 需求质量门 → 需求决策 ─┬─ 编写 QA 用例(并行侧分支)────────┐
|
||||
└─ 技术高阶 → 迭代 HLD → 详细规划 → 开发 → 提测
|
||||
│
|
||||
测试通过 ← 测试决策 ← 编写并运行 E2E ←┘
|
||||
```
|
||||
|
||||
## 用法
|
||||
|
||||
@@ -13,12 +22,36 @@ PYTHONPATH=agents-workflow-engine python -m agents_workflow_engine run \
|
||||
--project <目标项目> --answers answers.yaml --agent '你的子 agent 命令'
|
||||
```
|
||||
|
||||
`answers.yaml` 里,人的节点对应一段文字或「满意 / 不满意」。同一节点要答多次时写成列表。
|
||||
`answers.yaml` 里,人的节点对应一段文字或该决策的选项。同一节点要答多次时写成列表。
|
||||
|
||||
- 需求/高阶/plan/构建决策:`满意` 或 `不满意`
|
||||
- 测试决策:`修订`、`通过`、`打回开发`
|
||||
|
||||
子 agent 命令会额外收到 `--skill`、`--node`、`--model`。批量构建还会收到 `--task`。环境变量 `WORKFLOW_TASK` 是同一条任务。门禁要在标准输出单独一行打印 `满意` 或 `不满意`。`dev-plan` 用 `TASK: <id>` 宣布一批任务。
|
||||
|
||||
模型用环境变量,不写进仓库:`MODEL_WRITER`、`MODEL_REVIEWER`。两个值相同,或没设,会在 `.agents/workflow/decisions.jsonl` 记下「未做对抗验证」。
|
||||
|
||||
## 图的语义
|
||||
|
||||
| 字段 | 用在哪 | 含义 |
|
||||
|---|---|---|
|
||||
| `kind: milestone` | 主会话节点 | 里程碑,只记一条日志,不调子 agent(预设里的「提测」) |
|
||||
| `join: true` | 节点 | 等所有向前入边都到过再执行;打回边直接放行 |
|
||||
| `parallel: true` | 决策的出边 | 与同选项的主出边并行,成为一个独立分支 |
|
||||
| `batch: true` | 子 agent 节点 | 输出 `TASK:` 清单,交给后续节点逐条消费 |
|
||||
| `repeatsBatch` | 子 agent 节点 | 逐条消费某个批量节点的任务 |
|
||||
| `once: true` | 子 agent 节点 | 整个 workflow 只执行一次 |
|
||||
| `loops[].backEdges` | 回路 | 打回边必须登记,超过 `maxIterations` 就停下等人 |
|
||||
|
||||
**并行不是多线程。** 调度器按声明顺序推进各条分支,遇到 `join` 节点时等齐再往下走。日志和人工确认因此是确定的,也能按顺序回放。
|
||||
|
||||
预设里两条测试回路:
|
||||
|
||||
| 回路 | 打回边 | 上限 | 打回到 |
|
||||
|---|---|---|---|
|
||||
| `dev` | 构建不满意、测试决策打回开发 | 3 | 修订输入 → 规划修订 |
|
||||
| `e2e` | 测试决策修订 | 3 | 重新编写并运行 E2E |
|
||||
|
||||
## 安装预设
|
||||
|
||||
```bash
|
||||
@@ -32,6 +65,8 @@ PYTHONPATH=agents-workflow-engine python -m agents_workflow_engine run \
|
||||
|
||||
在目标项目放 `.agents/workflow/overlay.yaml`,可以 `disable` 节点、`replace` skill、改回路的 `maxIterations`。
|
||||
|
||||
`disable` 会连同节点相关的边一起删掉。例如禁用 `qa-write-cases` 后,`qa-run-e2e` 的汇入点只剩提测一侧,图仍然可跑。
|
||||
|
||||
## 维护
|
||||
|
||||
预设在 `presets/iteration-to-test.yaml`。形状说明在 `schema/graph.schema.json`。泳道预览在仓库 `.agents/plan/iteration-to-test.html`。
|
||||
预设在 `presets/iteration-to-test.yaml`。形状说明在 `schema/graph.schema.json`。泳道预览在仓库 `.agents/plan/iteration-to-test.html`。
|
||||
@@ -131,7 +131,7 @@ def build_parser() -> argparse.ArgumentParser:
|
||||
render_cmd.add_argument("--output", default="", help="写入的文件。缺省打印到标准输出")
|
||||
render_cmd.set_defaults(func=cmd_render)
|
||||
|
||||
run_cmd = sub.add_parser("run", help="主会话按图执行到提测")
|
||||
run_cmd = sub.add_parser("run", help="主会话按图执行到测试通过")
|
||||
run_cmd.add_argument("--answers", default="", help="人的答案 YAML")
|
||||
run_cmd.add_argument("--agent", default="", help="子 agent 命令")
|
||||
run_cmd.add_argument("--master-model", default=os.environ.get("MODEL_MASTER", "master"), help="主会话模型,子 agent 未配置时沿用")
|
||||
|
||||
@@ -9,7 +9,11 @@ def _shape(node: dict) -> str:
|
||||
title += " · 一批任务"
|
||||
if node.get("repeatsBatch"):
|
||||
title += " · 按批循环"
|
||||
if node.get("sealed") and node.get("note"):
|
||||
if node.get("join"):
|
||||
title += " · 汇入"
|
||||
if node.get("kind") == "milestone":
|
||||
title += " · 里程碑"
|
||||
if node.get("note"):
|
||||
title += " / " + "、".join(node["note"])
|
||||
node_id = node["id"]
|
||||
kind = node.get("kind")
|
||||
@@ -21,7 +25,7 @@ def _shape(node: dict) -> str:
|
||||
return "%s{%s}" % (node_id, title)
|
||||
if kind == "human_input":
|
||||
return "%s[/%s/]" % (node_id, title)
|
||||
if kind == "end":
|
||||
if kind in ("end", "milestone"):
|
||||
return "%s([%s])" % (node_id, title)
|
||||
if kind == "entry":
|
||||
return "%s{{%s}}" % (node_id, title)
|
||||
@@ -39,6 +43,9 @@ def render(graph: dict) -> str:
|
||||
lines.append(" end")
|
||||
for edge in graph.get("edges") or []:
|
||||
arrow = "-.->" if edge.get("back") else "-->"
|
||||
label = "|%s|" % edge["when"] if edge.get("when") else ""
|
||||
label_text = edge.get("when") or ""
|
||||
if edge.get("parallel"):
|
||||
label_text = (label_text + " · 并行").strip(" ·")
|
||||
label = "|%s|" % label_text if label_text else ""
|
||||
lines.append(" %s %s%s %s" % (edge["from"], arrow, label, edge["to"]))
|
||||
return "\n".join(lines) + "\n"
|
||||
|
||||
@@ -9,12 +9,15 @@ from typing import Callable
|
||||
from typing import Dict
|
||||
from typing import List
|
||||
from typing import Optional
|
||||
from typing import Tuple
|
||||
|
||||
from .errors import GraphError
|
||||
from .errors import NeedInput
|
||||
from .validate import REVIEW_PAIRS
|
||||
from .validate import node_map
|
||||
|
||||
Token = Tuple[str, str]
|
||||
|
||||
|
||||
def _plain_edges(edges: List[dict], node_id: str) -> List[dict]:
|
||||
outgoing = [
|
||||
@@ -47,11 +50,11 @@ def _can_reach(start: str, target: str, edges: List[dict]) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _when_edge(edges: List[dict], node_id: str, answer: str) -> dict:
|
||||
for edge in edges:
|
||||
if edge.get("from") == node_id and edge.get("when") == answer:
|
||||
return edge
|
||||
raise GraphError("节点 %s 没有「%s」这条出边" % (node_id, answer))
|
||||
def _when_edges(edges: List[dict], node_id: str, answer: str) -> List[dict]:
|
||||
return [
|
||||
edge for edge in edges
|
||||
if edge.get("from") == node_id and edge.get("when") == answer
|
||||
]
|
||||
|
||||
|
||||
def _loop_for(graph: dict, edge_id: str) -> Optional[dict]:
|
||||
@@ -106,7 +109,7 @@ def _tasks(text: str) -> List[str]:
|
||||
|
||||
|
||||
class Runner:
|
||||
def __init__(self, graph: dict, answers: Dict[str, List[str]], agent: Callable, master_model: str, log: List[dict], current: str = "", visits: Dict[str, int] = None, loop_counts: Dict[str, int] = None, tasks: List[str] = None, task_index: int = 0) -> None:
|
||||
def __init__(self, graph: dict, answers: Dict[str, List[str]], agent: Callable, master_model: str, log: List[dict], current: str = "", visits: Dict[str, int] = None, loop_counts: Dict[str, int] = None, tasks: List[str] = None, task_index: int = 0, joined: Dict[str, set] = None) -> None:
|
||||
self.graph = graph
|
||||
self.answers = answers
|
||||
self.agent = agent
|
||||
@@ -119,59 +122,76 @@ class Runner:
|
||||
self.loop_counts = loop_counts or {}
|
||||
self.tasks = list(tasks or [])
|
||||
self.task_index = task_index
|
||||
self.joined = joined or {}
|
||||
|
||||
def run(self) -> str:
|
||||
current = self.current
|
||||
# 一条 token 就是一股独立的执行流;决策的 parallel 分支会各分出一股。
|
||||
work = [(self.current, "")]
|
||||
guard = 0
|
||||
while guard < 500:
|
||||
while work:
|
||||
guard += 1
|
||||
if guard > 500:
|
||||
raise GraphError("走了 500 步还没到终点,图里可能有死循环")
|
||||
current, via = work.pop(0)
|
||||
self.current = current
|
||||
node = self.nodes[current]
|
||||
if node.get("join") and not self._join_ready(current, via):
|
||||
continue
|
||||
kind = node.get("kind")
|
||||
if kind == "end":
|
||||
self.log.append({"node": current, "event": "end", "title": node.get("title")})
|
||||
return current
|
||||
if kind == "entry":
|
||||
current = self._only(current)
|
||||
if kind in ("entry", "milestone"):
|
||||
if kind == "milestone":
|
||||
self.log.append({"node": current, "event": "milestone", "title": node.get("title")})
|
||||
work.insert(0, self._only_token(current))
|
||||
continue
|
||||
if kind == "human_input":
|
||||
text = _take_answer(self.answers, current)
|
||||
self.log.append({"node": current, "event": "human_input", "text": text})
|
||||
current = self._only(current)
|
||||
work.insert(0, self._only_token(current))
|
||||
continue
|
||||
if kind == "decision":
|
||||
choice = _take_answer(self.answers, current)
|
||||
self.log.append({"node": current, "event": "decision", "choice": choice})
|
||||
current = self._follow_when(current, choice)
|
||||
tokens = self._decision_tokens(current, choice)
|
||||
if len(tokens) > 1:
|
||||
self.log.append({"node": current, "event": "fork", "branches": [token[0] for token in tokens]})
|
||||
work = tokens + work
|
||||
continue
|
||||
if kind == "gate":
|
||||
self._warn_pair(node)
|
||||
result = self._call(node, None)
|
||||
verdict = _verdict(result)
|
||||
self.log.append({"node": current, "event": "gate", "verdict": verdict, "model": _resolve_model(self.graph, node, self.master_model)})
|
||||
current = self._follow_when(current, verdict)
|
||||
matches = _when_edges(self.edges, current, verdict)
|
||||
if len(matches) != 1:
|
||||
raise GraphError("门禁 %s 的「%s」有 %s 条出边,必须恰好 1 条" % (current, verdict, len(matches)))
|
||||
self._limit_back(matches[0], current)
|
||||
work.insert(0, (matches[0]["to"], matches[0]["id"]))
|
||||
continue
|
||||
if kind == "action":
|
||||
current = self._run_action(node)
|
||||
nxt = self._run_action(node)
|
||||
work.insert(0, nxt if nxt is not None else (current, via))
|
||||
continue
|
||||
raise GraphError("节点 %s 的 kind 无法执行:%s" % (current, kind))
|
||||
raise GraphError("走了 500 步还没到终点,图里可能有死循环")
|
||||
raise GraphError("调度队列空了还没到终点。检查图里是否有分支没有汇入")
|
||||
|
||||
def _run_action(self, node: dict) -> str:
|
||||
def _run_action(self, node: dict) -> Optional[Token]:
|
||||
node_id = node["id"]
|
||||
if node.get("once") and self.visits.get(node_id):
|
||||
return self._only(node_id)
|
||||
return self._only_token(node_id)
|
||||
if node.get("repeatsBatch"):
|
||||
if self.task_index >= len(self.tasks):
|
||||
return self._only(node_id)
|
||||
return self._only_token(node_id)
|
||||
task = self.tasks[self.task_index]
|
||||
result = self._call(node, task)
|
||||
self.visits[node_id] = self.visits.get(node_id, 0) + 1
|
||||
self.task_index += 1
|
||||
self.log.append({"node": node_id, "event": "batch-step", "task": task, "output": result})
|
||||
if self.task_index < len(self.tasks):
|
||||
return node_id
|
||||
return self._only(node_id)
|
||||
return None
|
||||
return self._only_token(node_id)
|
||||
result = self._call(node, None)
|
||||
self.visits[node_id] = self.visits.get(node_id, 0) + 1
|
||||
self.log.append({"node": node_id, "event": "action", "output": result, "model": _resolve_model(self.graph, node, self.master_model)})
|
||||
@@ -179,7 +199,7 @@ class Runner:
|
||||
self.tasks = _tasks(result)
|
||||
self.task_index = 0
|
||||
self.log.append({"node": node_id, "event": "batch", "tasks": list(self.tasks)})
|
||||
return self._only(node_id)
|
||||
return self._only_token(node_id)
|
||||
|
||||
def _call(self, node: dict, task: Optional[str]) -> str:
|
||||
model = _resolve_model(self.graph, node, self.master_model)
|
||||
@@ -204,26 +224,64 @@ class Runner:
|
||||
"reviewerModel": right,
|
||||
})
|
||||
|
||||
def _follow_when(self, node_id: str, answer: str) -> str:
|
||||
edge = _when_edge(self.edges, node_id, answer)
|
||||
if edge.get("back"):
|
||||
loop = _loop_for(self.graph, edge["id"])
|
||||
if loop is None:
|
||||
raise GraphError("打回边 %s 没有上限" % edge["id"])
|
||||
count = self.loop_counts.get(loop["id"], 0) + 1
|
||||
self.loop_counts[loop["id"]] = count
|
||||
if count > int(loop["maxIterations"]):
|
||||
raise GraphError(
|
||||
"回路 %s 已打回 %s 次,达到上限 %s。请人工处理节点 %s 后再继续"
|
||||
% (loop["id"], count - 1, loop["maxIterations"], node_id)
|
||||
)
|
||||
return edge["to"]
|
||||
def _decision_tokens(self, node_id: str, answer: str) -> List[Token]:
|
||||
matches = _when_edges(self.edges, node_id, answer)
|
||||
if not matches:
|
||||
raise GraphError("节点 %s 没有「%s」这条出边" % (node_id, answer))
|
||||
main = [edge for edge in matches if not edge.get("parallel")]
|
||||
side = [edge for edge in matches if edge.get("parallel")]
|
||||
if len(main) != 1:
|
||||
raise GraphError(
|
||||
"节点 %s 的「%s」需要恰好一条主出边,现在 %s 条;并行分支请标 parallel: true"
|
||||
% (node_id, answer, len(main))
|
||||
)
|
||||
for edge in matches:
|
||||
self._limit_back(edge, node_id)
|
||||
# 侧分支先推进,主路径随后;两股都到汇入点时才执行汇入节点。
|
||||
return [(edge["to"], edge["id"]) for edge in side] + [(main[0]["to"], main[0]["id"])]
|
||||
|
||||
def _only(self, node_id: str) -> str:
|
||||
def _limit_back(self, edge: dict, node_id: str) -> None:
|
||||
if not edge.get("back"):
|
||||
return
|
||||
loop = _loop_for(self.graph, edge["id"])
|
||||
if loop is None:
|
||||
raise GraphError("打回边 %s 没有上限" % edge["id"])
|
||||
count = self.loop_counts.get(loop["id"], 0) + 1
|
||||
self.loop_counts[loop["id"]] = count
|
||||
if count > int(loop["maxIterations"]):
|
||||
raise GraphError(
|
||||
"回路 %s 已打回 %s 次,达到上限 %s。请人工处理节点 %s 后再继续"
|
||||
% (loop["id"], count - 1, loop["maxIterations"], node_id)
|
||||
)
|
||||
|
||||
def _join_ready(self, node_id: str, via: str) -> bool:
|
||||
if not via:
|
||||
return True
|
||||
edge = self._edge(via)
|
||||
if edge is None or edge.get("back"):
|
||||
return True
|
||||
forward = set(
|
||||
item["id"] for item in self.edges
|
||||
if item.get("to") == node_id and not item.get("back")
|
||||
)
|
||||
seen = self.joined.setdefault(node_id, set())
|
||||
seen.add(via)
|
||||
if forward <= seen:
|
||||
return True
|
||||
self.log.append({"node": node_id, "event": "join-wait", "waiting": sorted(forward - seen)})
|
||||
return False
|
||||
|
||||
def _edge(self, edge_id: str) -> Optional[dict]:
|
||||
for edge in self.edges:
|
||||
if edge.get("id") == edge_id:
|
||||
return edge
|
||||
return None
|
||||
|
||||
def _only_token(self, node_id: str) -> Token:
|
||||
chosen = _plain_edges(self.edges, node_id)
|
||||
if len(chosen) != 1:
|
||||
raise GraphError("节点 %s 有 %s 条向前的边,调度不知道走哪条" % (node_id, len(chosen)))
|
||||
return chosen[0]["to"]
|
||||
return (chosen[0]["to"], chosen[0]["id"])
|
||||
|
||||
|
||||
def spawn_agent(command: str, node: dict, model: str, task: Optional[str]) -> str:
|
||||
@@ -245,4 +303,4 @@ def spawn_agent(command: str, node: dict, model: str, task: Optional[str]) -> st
|
||||
raise GraphError(
|
||||
"子 agent 退出码 %s,节点 %s。stderr:%s" % (completed.returncode, node["id"], completed.stderr.strip()[:400])
|
||||
)
|
||||
return completed.stdout
|
||||
return completed.stdout
|
||||
@@ -77,7 +77,7 @@ def list_states(project: str) -> list:
|
||||
|
||||
|
||||
def open_instance(project: str, iteration: str, entry: str) -> dict:
|
||||
"""没有记录就创建。已有记录就返回它,用来接着跑。已到提测则拒绝再开一条。"""
|
||||
"""没有记录就创建。已有记录就返回它,用来接着跑。已到测试通过则拒绝再开一条。"""
|
||||
check_iteration(iteration)
|
||||
existing = load_state(project, iteration)
|
||||
if not existing:
|
||||
@@ -96,7 +96,7 @@ def open_instance(project: str, iteration: str, entry: str) -> dict:
|
||||
return state
|
||||
if existing.get("status") == "completed":
|
||||
raise GraphError(
|
||||
"迭代 %s 已经有一条 workflow,并且已经到提测。不能再创建一条。用 status --iteration %s 查看"
|
||||
"迭代 %s 已经有一条 workflow,并且已经到测试通过。不能再创建一条。用 status --iteration %s 查看"
|
||||
% (iteration, iteration)
|
||||
)
|
||||
return existing
|
||||
|
||||
@@ -5,7 +5,7 @@ from __future__ import annotations
|
||||
import os
|
||||
from typing import Any
|
||||
|
||||
WORK_KINDS = ("action", "gate", "decision", "human_input")
|
||||
WORK_KINDS = ("action", "gate", "decision", "human_input", "milestone")
|
||||
ACTORS = ("human", "master", "subagent")
|
||||
REVIEW_PAIRS = (
|
||||
("ba-write-spec", "ba-requirement-review"),
|
||||
@@ -63,17 +63,44 @@ def validate(graph: dict) -> tuple:
|
||||
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 [])
|
||||
for edge in _outgoing(edges, node_id):
|
||||
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"]
|
||||
@@ -111,9 +138,13 @@ def validate(graph: dict) -> tuple:
|
||||
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") in ("decision", "gate"):
|
||||
if src in by_id and by_id[src].get("kind") == "gate":
|
||||
if edge.get("when") not in ("满意", "不满意"):
|
||||
errors.append("边 %s 从门禁或决策出发,when 只能是满意或不满意" % edge_id)
|
||||
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")}
|
||||
|
||||
@@ -16,7 +16,10 @@ loops:
|
||||
backEdges: [hld-reject]
|
||||
- id: dev
|
||||
maxIterations: 3
|
||||
backEdges: [build-reject]
|
||||
backEdges: [build-reject, e2e-reject]
|
||||
- id: e2e
|
||||
maxIterations: 3
|
||||
backEdges: [e2e-revise]
|
||||
nodes:
|
||||
- { id: graph-start, actor: master, kind: entry, title: 会话启动 }
|
||||
- { id: start-version, actor: human, kind: human_input, title: 开始版本 }
|
||||
@@ -26,10 +29,18 @@ nodes:
|
||||
- { id: ba-write-prototype, actor: subagent, kind: action, skill: ba-write-prototype, title: 原型, once: true, model: writer }
|
||||
- { id: ba-requirement-review, actor: subagent, kind: gate, skill: ba-requirement-review, title: 需求质量门, model: reviewer }
|
||||
- { id: requirement-decision, actor: human, kind: decision, title: 需求决策, options: [满意, 不满意] }
|
||||
- { id: qa-write-cases, actor: subagent, kind: action, skill: qa-write-cases, title: 编写 QA 用例, model: writer }
|
||||
- { id: tech-brief, actor: human, kind: human_input, title: 技术高阶 }
|
||||
- { id: tl-high-level-design, actor: subagent, kind: action, skill: tl-high-level-design, title: 技术高阶设计, once: true, model: writer }
|
||||
- { id: hld-decision, actor: human, kind: decision, title: 高阶决策, options: [满意, 不满意] }
|
||||
- { id: dev-plan, actor: subagent, kind: action, skill: dev-plan, title: 详细规划, batch: true, model: writer }
|
||||
- id: dev-plan
|
||||
actor: subagent
|
||||
kind: action
|
||||
skill: dev-plan
|
||||
title: 详细规划
|
||||
batch: true
|
||||
model: writer
|
||||
note: [详细设计(API/DB 字段、文件改动), 任务清单, 自测用例(DST)]
|
||||
- { id: dev-plan-review, actor: subagent, kind: gate, skill: dev-plan-review, title: plan 质量门, model: reviewer }
|
||||
- { id: plan-confirm, actor: human, kind: human_input, title: plan 确认 }
|
||||
- { id: plan-decision, actor: human, kind: decision, title: plan 决策, options: [满意, 不满意] }
|
||||
@@ -46,17 +57,10 @@ nodes:
|
||||
- { id: build-decision, actor: human, kind: decision, title: 构建决策, options: [满意, 不满意] }
|
||||
- { id: amend-input, actor: human, kind: human_input, title: 修订输入 }
|
||||
- { id: dev-plan-amend, actor: subagent, kind: action, skill: dev-plan-amend, title: 规划修订, model: writer }
|
||||
- { id: handoff, actor: master, kind: end, title: 提测 }
|
||||
extensions:
|
||||
- id: qa
|
||||
title: 测试
|
||||
linked: false
|
||||
requires: pipeline
|
||||
nodes:
|
||||
- { id: qa-write-cases, kind: action, title: QA 用例 }
|
||||
- { id: dev-create-mr, kind: action, title: 创建 MR/流水线部署 }
|
||||
- { id: qa-run-e2e, kind: action, title: E2E 测试 }
|
||||
- { id: manual-test, kind: human_input, title: 人工补测 }
|
||||
- { id: handoff, actor: master, kind: milestone, title: 提测 }
|
||||
- { id: qa-run-e2e, actor: subagent, kind: action, skill: qa-run-e2e, title: 编写并运行 E2E 测试, join: true, model: writer }
|
||||
- { id: test-decision, actor: human, kind: decision, title: 测试决策, options: [修订, 通过, 打回开发] }
|
||||
- { id: accepted, actor: master, kind: end, title: 测试通过 }
|
||||
edges:
|
||||
- { id: e0, from: graph-start, to: start-version }
|
||||
- { id: e1, from: start-version, to: pm-start-iteration }
|
||||
@@ -68,9 +72,10 @@ edges:
|
||||
- { id: req-pass, from: ba-requirement-review, to: requirement-decision, when: 满意 }
|
||||
- { id: req-gate-reject, from: ba-requirement-review, to: write-spec, when: 不满意, back: true }
|
||||
- { id: req-decision-reject, from: requirement-decision, to: write-spec, when: 不满意, back: true }
|
||||
- { id: req-qa, from: requirement-decision, to: qa-write-cases, when: 满意, parallel: true }
|
||||
- { id: req-ok, from: requirement-decision, to: tech-brief, when: 满意 }
|
||||
- { id: e7, from: tech-brief, to: tl-high-level-design }
|
||||
- { id: e8, from: tl-high-level-design, to: hld-decision }
|
||||
- { id: req-ok, from: requirement-decision, to: tech-brief, when: 满意 }
|
||||
- { id: hld-reject, from: hld-decision, to: tech-brief, when: 不满意, back: true }
|
||||
- { id: hld-ok, from: hld-decision, to: dev-plan, when: 满意 }
|
||||
- { id: e9, from: dev-plan, to: dev-plan-review }
|
||||
@@ -83,3 +88,9 @@ edges:
|
||||
- { id: build-reject, from: build-decision, to: amend-input, when: 不满意, back: true }
|
||||
- { id: e13, from: amend-input, to: dev-plan-amend }
|
||||
- { id: e14, from: dev-plan-amend, to: dev-plan, back: true }
|
||||
- { id: handoff-to-e2e, from: handoff, to: qa-run-e2e }
|
||||
- { id: qa-cases-ready, from: qa-write-cases, to: qa-run-e2e }
|
||||
- { id: e2e-to-decision, from: qa-run-e2e, to: test-decision }
|
||||
- { id: e2e-revise, from: test-decision, to: qa-run-e2e, when: 修订, back: true }
|
||||
- { id: e2e-pass, from: test-decision, to: accepted, when: 通过 }
|
||||
- { id: e2e-reject, from: test-decision, to: amend-input, when: 打回开发, back: true }
|
||||
@@ -8,9 +8,57 @@
|
||||
"entry": { "type": "string" },
|
||||
"lanes": { "type": "array" },
|
||||
"models": { "type": "object" },
|
||||
"loops": { "type": "array" },
|
||||
"nodes": { "type": "array" },
|
||||
"edges": { "type": "array" },
|
||||
"loops": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"required": ["id", "maxIterations", "backEdges"],
|
||||
"properties": {
|
||||
"id": { "type": "string" },
|
||||
"maxIterations": { "type": "integer" },
|
||||
"backEdges": { "type": "array", "items": { "type": "string" } }
|
||||
}
|
||||
}
|
||||
},
|
||||
"nodes": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"required": ["id", "actor", "kind"],
|
||||
"properties": {
|
||||
"id": { "type": "string" },
|
||||
"actor": { "enum": ["human", "master", "subagent"] },
|
||||
"kind": {
|
||||
"enum": ["entry", "human_input", "action", "gate", "decision", "milestone", "end"]
|
||||
},
|
||||
"title": { "type": "string" },
|
||||
"skill": { "type": "string" },
|
||||
"model": { "type": "string" },
|
||||
"options": { "type": "array", "items": { "type": "string" } },
|
||||
"batch": { "type": "boolean" },
|
||||
"repeatsBatch": { "type": "string" },
|
||||
"once": { "type": "boolean" },
|
||||
"join": { "type": "boolean" },
|
||||
"sealed": { "type": "boolean" },
|
||||
"note": { "type": "array", "items": { "type": "string" } }
|
||||
}
|
||||
}
|
||||
},
|
||||
"edges": {
|
||||
"type": "array",
|
||||
"items": {
|
||||
"type": "object",
|
||||
"required": ["id", "from", "to"],
|
||||
"properties": {
|
||||
"id": { "type": "string" },
|
||||
"from": { "type": "string" },
|
||||
"to": { "type": "string" },
|
||||
"when": { "type": "string" },
|
||||
"back": { "type": "boolean" },
|
||||
"parallel": { "type": "boolean" }
|
||||
}
|
||||
}
|
||||
},
|
||||
"extensions": { "type": "array" }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,6 @@ from agents_workflow_engine.merge import merge # noqa: E402
|
||||
from agents_workflow_engine.render_mermaid import render # noqa: E402
|
||||
from agents_workflow_engine.runner import Runner # noqa: E402
|
||||
from agents_workflow_engine.validate import validate # noqa: E402
|
||||
from agents_workflow_engine.yaml_lite import loads # noqa: E402
|
||||
|
||||
|
||||
def happy_answers():
|
||||
@@ -30,6 +29,7 @@ def happy_answers():
|
||||
"plan-confirm": ["确认"],
|
||||
"plan-decision": ["满意"],
|
||||
"build-decision": ["满意"],
|
||||
"test-decision": ["通过"],
|
||||
}
|
||||
|
||||
|
||||
@@ -64,18 +64,35 @@ class PresetTest(unittest.TestCase):
|
||||
self.assertFalse(any("未做对抗验证" in item for item in warnings))
|
||||
self.assertFalse(any("未配置对抗模型" in item for item in warnings))
|
||||
|
||||
def test_render_shapes_and_hides_extensions(self):
|
||||
def test_render_shapes(self):
|
||||
text = render(load_preset())
|
||||
self.assertIn("flowchart LR", text)
|
||||
self.assertIn("dev-plan(详细规划 · 一批任务)", text)
|
||||
self.assertIn(
|
||||
"dev-plan(详细规划 · 一批任务 / 详细设计(API/DB 字段、文件改动)、任务清单、自测用例(DST))",
|
||||
text,
|
||||
)
|
||||
self.assertIn("qa-write-cases(编写 QA 用例)", text)
|
||||
self.assertIn("qa-run-e2e(编写并运行 E2E 测试 · 汇入)", text)
|
||||
self.assertIn("test-decision{测试决策}", text)
|
||||
self.assertIn("ba-requirement-review[[需求质量门]]", text)
|
||||
self.assertIn("start-version[/开始版本/]", text)
|
||||
self.assertIn("requirement-decision{需求决策}", text)
|
||||
self.assertIn("handoff([提测])", text)
|
||||
self.assertIn("handoff([提测 · 里程碑])", text)
|
||||
self.assertIn("accepted([测试通过])", text)
|
||||
self.assertIn("graph-start{{会话启动}}", text)
|
||||
self.assertIn("后端 TDD", text)
|
||||
self.assertNotIn("qa-write-cases", text)
|
||||
self.assertIn("-.->|不满意|", text)
|
||||
self.assertIn("-->|满意 · 并行| qa-write-cases", text)
|
||||
|
||||
def test_render_hides_extensions(self):
|
||||
graph = load_preset()
|
||||
graph["extensions"] = [{
|
||||
"id": "future",
|
||||
"title": "未来",
|
||||
"linked": False,
|
||||
"nodes": [{"id": "future-node", "kind": "action", "title": "未来步骤"}],
|
||||
}]
|
||||
self.assertNotIn("future-node", render(graph))
|
||||
|
||||
def test_disable_prototype_still_reaches_end(self):
|
||||
graph = merge(load_preset(), {"disable": ["ba-write-prototype"]})
|
||||
@@ -83,6 +100,15 @@ class PresetTest(unittest.TestCase):
|
||||
self.assertEqual(errors, [])
|
||||
self.assertNotIn("ba-write-prototype", {node["id"] for node in graph["nodes"]})
|
||||
|
||||
def test_disable_qa_cases_keeps_graph_runnable(self):
|
||||
graph = merge(load_preset(), {"disable": ["qa-write-cases"]})
|
||||
errors, _warnings = validate(graph)
|
||||
self.assertEqual(errors, [])
|
||||
calls = []
|
||||
end = Runner(graph, happy_answers(), fake_agent(calls), "master", []).run()
|
||||
self.assertEqual(end, "accepted")
|
||||
self.assertNotIn("qa-write-cases", [item[0] for item in calls])
|
||||
|
||||
def test_edge_into_sealed_step_fails(self):
|
||||
graph = load_preset()
|
||||
graph["edges"].append({"id": "bad", "from": "dev-build", "to": "后端 TDD"})
|
||||
@@ -91,7 +117,12 @@ class PresetTest(unittest.TestCase):
|
||||
|
||||
def test_edge_into_extension_fails(self):
|
||||
graph = load_preset()
|
||||
graph["edges"].append({"id": "bad", "from": "handoff", "to": "qa-write-cases"})
|
||||
graph["extensions"] = [{
|
||||
"id": "future",
|
||||
"title": "未来",
|
||||
"nodes": [{"id": "future-node", "kind": "action", "title": "未来步骤"}],
|
||||
}]
|
||||
graph["edges"].append({"id": "bad", "from": "handoff", "to": "future-node"})
|
||||
errors, _warnings = validate(graph)
|
||||
self.assertTrue(any("扩展" in item for item in errors))
|
||||
|
||||
@@ -101,6 +132,43 @@ class PresetTest(unittest.TestCase):
|
||||
errors, _warnings = validate(graph)
|
||||
self.assertTrue(any("没有写入任何回路" in item for item in errors))
|
||||
|
||||
def test_parallel_branch_needs_one_main_edge(self):
|
||||
graph = load_preset()
|
||||
graph["edges"].append({
|
||||
"id": "dup",
|
||||
"from": "requirement-decision",
|
||||
"to": "write-spec",
|
||||
"when": "满意",
|
||||
})
|
||||
errors, _warnings = validate(graph)
|
||||
self.assertTrue(any("主出边" in item for item in errors))
|
||||
|
||||
def test_parallel_edge_must_come_from_decision(self):
|
||||
graph = load_preset()
|
||||
graph["edges"] = [
|
||||
dict(edge, parallel=True) if edge["id"] == "e13" else edge
|
||||
for edge in graph["edges"]
|
||||
]
|
||||
errors, _warnings = validate(graph)
|
||||
self.assertTrue(any("parallel" in item for item in errors))
|
||||
|
||||
def test_join_rejects_conditional_input(self):
|
||||
graph = load_preset()
|
||||
graph["edges"] = [
|
||||
dict(edge, when="满意") if edge["id"] == "qa-cases-ready" else edge
|
||||
for edge in graph["edges"]
|
||||
]
|
||||
errors, _warnings = validate(graph)
|
||||
self.assertTrue(any("汇入" in item for item in errors))
|
||||
|
||||
def test_milestone_must_be_master(self):
|
||||
graph = load_preset()
|
||||
for node in graph["nodes"]:
|
||||
if node["id"] == "handoff":
|
||||
node["actor"] = "subagent"
|
||||
errors, _warnings = validate(graph)
|
||||
self.assertTrue(any("里程碑 handoff" in item for item in errors))
|
||||
|
||||
def test_replace_skill(self):
|
||||
graph = merge(load_preset(), {"replace": {"ba-write-spec": {"skill": "my-write-spec"}}})
|
||||
node = [item for item in graph["nodes"] if item["id"] == "ba-write-spec"][0]
|
||||
@@ -112,20 +180,66 @@ class RunTest(unittest.TestCase):
|
||||
calls = []
|
||||
log = []
|
||||
end = Runner(load_preset(), happy_answers(), fake_agent(calls), "master", log).run()
|
||||
self.assertEqual(end, "handoff")
|
||||
self.assertEqual(end, "accepted")
|
||||
build_calls = [item for item in calls if item[0] == "dev-build"]
|
||||
self.assertEqual([item[2] for item in build_calls], ["a", "b"])
|
||||
self.assertTrue(any(item.get("event") == "batch" for item in log))
|
||||
|
||||
def test_qa_branch_runs_before_design_and_joins(self):
|
||||
calls = []
|
||||
log = []
|
||||
end = Runner(load_preset(), happy_answers(), fake_agent(calls), "master", log).run()
|
||||
self.assertEqual(end, "accepted")
|
||||
order = [item[0] for item in calls]
|
||||
self.assertLess(order.index("qa-write-cases"), order.index("tl-high-level-design"))
|
||||
self.assertEqual(order.count("qa-write-cases"), 1)
|
||||
self.assertEqual(order.count("qa-run-e2e"), 1)
|
||||
forks = [item for item in log if item.get("event") == "fork"]
|
||||
self.assertEqual(forks[0]["branches"], ["qa-write-cases", "tech-brief"])
|
||||
waits = [item for item in log if item.get("event") == "join-wait"]
|
||||
self.assertEqual(waits[0]["node"], "qa-run-e2e")
|
||||
self.assertEqual(waits[0]["waiting"], ["handoff-to-e2e"])
|
||||
|
||||
def test_spec_reject_then_pass(self):
|
||||
calls = []
|
||||
answers = happy_answers()
|
||||
answers["write-spec"] = ["第一稿", "第二稿"]
|
||||
answers["requirement-decision"] = ["不满意", "满意"]
|
||||
end = Runner(load_preset(), answers, fake_agent(calls), "master", []).run()
|
||||
self.assertEqual(end, "handoff")
|
||||
self.assertEqual(end, "accepted")
|
||||
self.assertEqual(sum(1 for item in calls if item[0] == "ba-write-spec"), 2)
|
||||
self.assertEqual(sum(1 for item in calls if item[0] == "ba-write-prototype"), 1)
|
||||
self.assertEqual(sum(1 for item in calls if item[0] == "qa-write-cases"), 1)
|
||||
|
||||
def test_e2e_revise_then_pass(self):
|
||||
answers = happy_answers()
|
||||
answers["test-decision"] = ["修订", "通过"]
|
||||
calls = []
|
||||
end = Runner(load_preset(), answers, fake_agent(calls), "master", []).run()
|
||||
self.assertEqual(end, "accepted")
|
||||
self.assertEqual(sum(1 for item in calls if item[0] == "qa-run-e2e"), 2)
|
||||
|
||||
def test_e2e_reject_returns_to_dev(self):
|
||||
answers = happy_answers()
|
||||
answers["plan-confirm"] = ["确认", "再确认"]
|
||||
answers["plan-decision"] = ["满意", "满意"]
|
||||
answers["build-decision"] = ["满意", "满意"]
|
||||
answers["amend-input"] = ["补字段"]
|
||||
answers["test-decision"] = ["打回开发", "通过"]
|
||||
calls = []
|
||||
end = Runner(load_preset(), answers, fake_agent(calls), "master", []).run()
|
||||
self.assertEqual(end, "accepted")
|
||||
self.assertEqual(sum(1 for item in calls if item[0] == "dev-plan"), 2)
|
||||
self.assertEqual(sum(1 for item in calls if item[0] == "dev-plan-amend"), 1)
|
||||
self.assertEqual(sum(1 for item in calls if item[0] == "dev-build"), 4)
|
||||
self.assertEqual(sum(1 for item in calls if item[0] == "qa-run-e2e"), 2)
|
||||
|
||||
def test_e2e_loop_cap_stops(self):
|
||||
answers = happy_answers()
|
||||
answers["test-decision"] = ["修订", "修订", "修订", "修订"]
|
||||
with self.assertRaises(GraphError) as caught:
|
||||
Runner(load_preset(), answers, fake_agent([]), "master", []).run()
|
||||
self.assertIn("达到上限", str(caught.exception))
|
||||
|
||||
def test_loop_cap_stops(self):
|
||||
answers = happy_answers()
|
||||
@@ -156,8 +270,6 @@ class RunTest(unittest.TestCase):
|
||||
|
||||
with tempfile.TemporaryDirectory() as folder:
|
||||
answers = os.path.join(folder, "answers.yaml")
|
||||
with open(answers, "w", encoding="utf-8") as handle:
|
||||
handle.write(loads and "")
|
||||
text = "\n".join("%s: %s" % (key, value[0]) for key, value in happy_answers().items()) + "\n"
|
||||
with open(answers, "w", encoding="utf-8") as handle:
|
||||
handle.write(text)
|
||||
@@ -178,9 +290,10 @@ class RunTest(unittest.TestCase):
|
||||
self.assertTrue(os.path.isfile(record))
|
||||
with open(record, encoding="utf-8") as handle:
|
||||
body = handle.read()
|
||||
self.assertIn("handoff", body)
|
||||
self.assertIn('"node": "handoff"', body)
|
||||
self.assertIn('"node": "accepted"', body)
|
||||
self.assertIn('"task": "b"', body)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user