- 预设新增 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)
103 lines
3.4 KiB
Python
103 lines
3.4 KiB
Python
"""一个迭代一条 workflow,进度落在 state.json。"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import json
|
||
import os
|
||
import re
|
||
|
||
from .errors import GraphError
|
||
|
||
_NAME = re.compile(r"^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$")
|
||
|
||
|
||
def check_iteration(name: str) -> str:
|
||
if not name or not _NAME.match(name):
|
||
raise GraphError("迭代名不合法:%s。只用字母、数字、点、下划线和连字符" % name)
|
||
return name
|
||
|
||
|
||
def instance_dir(project: str, iteration: str) -> str:
|
||
return os.path.join(project, ".agents", "workflow", "iterations", check_iteration(iteration))
|
||
|
||
|
||
def state_path(project: str, iteration: str) -> str:
|
||
return os.path.join(instance_dir(project, iteration), "state.json")
|
||
|
||
|
||
def decisions_path(project: str, iteration: str) -> str:
|
||
return os.path.join(instance_dir(project, iteration), "decisions.jsonl")
|
||
|
||
|
||
def load_state(project: str, iteration: str) -> dict:
|
||
path = state_path(project, iteration)
|
||
if not os.path.isfile(path):
|
||
return {}
|
||
with open(path, "r", encoding="utf-8") as handle:
|
||
data = json.load(handle)
|
||
if not isinstance(data, dict):
|
||
raise GraphError("进度文件损坏:%s" % path)
|
||
return data
|
||
|
||
|
||
def save_state(project: str, iteration: str, state: dict) -> str:
|
||
path = state_path(project, iteration)
|
||
os.makedirs(os.path.dirname(path), exist_ok=True)
|
||
temporary = path + ".tmp"
|
||
with open(temporary, "w", encoding="utf-8") as handle:
|
||
json.dump(state, handle, ensure_ascii=False, indent=2)
|
||
handle.write("\n")
|
||
os.replace(temporary, path)
|
||
logged = int(state.get("logged") or 0)
|
||
progress = state.get("progress") or []
|
||
fresh = progress[logged:]
|
||
if fresh:
|
||
with open(decisions_path(project, iteration), "a", encoding="utf-8") as handle:
|
||
for item in fresh:
|
||
handle.write(json.dumps(item, ensure_ascii=False) + "\n")
|
||
state["logged"] = len(progress)
|
||
with open(temporary, "w", encoding="utf-8") as handle:
|
||
json.dump(state, handle, ensure_ascii=False, indent=2)
|
||
handle.write("\n")
|
||
os.replace(temporary, path)
|
||
return path
|
||
|
||
|
||
def list_states(project: str) -> list:
|
||
root = os.path.join(project, ".agents", "workflow", "iterations")
|
||
if not os.path.isdir(root):
|
||
return []
|
||
found = []
|
||
for name in sorted(os.listdir(root)):
|
||
path = os.path.join(root, name, "state.json")
|
||
if os.path.isfile(path):
|
||
with open(path, "r", encoding="utf-8") as handle:
|
||
found.append(json.load(handle))
|
||
return found
|
||
|
||
|
||
def open_instance(project: str, iteration: str, entry: str) -> dict:
|
||
"""没有记录就创建。已有记录就返回它,用来接着跑。已到测试通过则拒绝再开一条。"""
|
||
check_iteration(iteration)
|
||
existing = load_state(project, iteration)
|
||
if not existing:
|
||
state = {
|
||
"iteration": iteration,
|
||
"status": "running",
|
||
"current": entry,
|
||
"visits": {},
|
||
"loopCounts": {},
|
||
"tasks": [],
|
||
"taskIndex": 0,
|
||
"progress": [],
|
||
"logged": 0,
|
||
}
|
||
save_state(project, iteration, state)
|
||
return state
|
||
if existing.get("status") == "completed":
|
||
raise GraphError(
|
||
"迭代 %s 已经有一条 workflow,并且已经到测试通过。不能再创建一条。用 status --iteration %s 查看"
|
||
% (iteration, iteration)
|
||
)
|
||
return existing
|