驳回节点的画法(后端):被驳回的那一步的 node.failed 带 reason=rejected

第 1 波把人的驳回收成 cancelled + end_reason=rejected,但节点层仍只发一条和故障一模一样的 node.failed:
复核规则把它说成「执行失败」、还当成「重跑一次大概率能好」(重跑只会把同一件事再问一遍人),实例进度里
没开待办的驳回(工具调用审批、代码执行审批)落成「失败」(05-已知遗留 (a)、总方案 X9)。

- engine/compiler:NodeRejected 时照旧发 node.failed(时间线、实例进度按它收尾这一步),数据里多带
  reason=rejected(和 run.cancelled 同一个字段名)、kind、decision、actor、note;真的出错的 node.failed
  一字不变,没有这几个键
- engine/review:认 reason=rejected,记 node_rejected(DEGRADED,不进 RETRYABLE),说「被驳回」不说「执行失败」;
  没有 reason 的老运行照旧 node_failed
- apps/resolve 的实例进度:按 node.failed 上的 reason 画「被驳回」(结论写已驳回 / 超时驳回 / 未确认(被驳回)),
  老运行照旧按待办认
- 测试:六种关卡的 node.failed 都带上这几个键;普通失败没有;复核与实例进度的新旧两种形状(去掉修复回跑会失败)
This commit is contained in:
ThearchyHelios
2026-10-16 19:30:47 +08:00
parent 45fab21072
commit eb578cad34
5 changed files with 100 additions and 4 deletions
+14 -2
View File
@@ -1738,24 +1738,36 @@ async def _timeline(session: AsyncSession, ctx: _Page) -> dict[str, Any]:
if run is None:
return {"state": "empty", "steps": []}
titles = {k: v["title"] for k, v in node_map(run.graph).items()}
rows = (await session.execute(select(RunEvent.type, RunEvent.node_id, RunEvent.ts).where(
rows = (await session.execute(select(RunEvent.type, RunEvent.node_id, RunEvent.ts, RunEvent.data).where(
RunEvent.run_id == run.id, RunEvent.type.in_(tuple(_STEP_STATE))).order_by(RunEvent.seq))).all()
trail = (await _trails(session, [run]))[run.id]
steps: dict[str, dict[str, Any]] = {}
for etype, node_id, ts in rows:
#: 被驳回的那一步:node.failed 上带着 reason=rejected 和结论(engine/compiler,总方案 X9)。工具调用审批、代码执行审批
#: 这类驳回没开待办,以前只能落成「失败」;老运行的 node.failed 没有 reason,照旧按待办认
rejected: dict[str, dict[str, Any]] = {}
for etype, node_id, ts, data in rows:
if not node_id:
continue
step = steps.setdefault(node_id, {"node": node_id, "title": titles.get(node_id, node_id), "state": "running",
"started_at": datetime.fromtimestamp(ts, timezone.utc).isoformat()})
step["state"] = _STEP_STATE[etype]
step["at"] = datetime.fromtimestamp(ts, timezone.utc).isoformat()
if etype == "node.failed" and isinstance(data, dict) and data.get("reason") == "rejected":
rejected[node_id] = data
elif etype == "node.started":
rejected.pop(node_id, None)
for node_id, step in steps.items():
text = None
record = next((a for a in reversed(trail.actions) if a.node_id == node_id), None)
kind, decision = _decision(trail, node_id)
pending = [t for t in trail.tasks if t.node_id == node_id and t.status == "pending"]
said = rejected.get(node_id) if step["state"] == "failed" else None
if record is not None and record.state in ("sent", "unknown"):
step["state"] = "unknown"
elif said is not None:
step["state"] = "rejected"
text = "未确认(被驳回)" if said.get("kind") == "action_confirm" and said.get("decision") == "reject" \
else _DECISION_TEXT.get(str(said.get("decision") or ""), _STEP_TEXT["rejected"])
elif step["state"] == "waiting" and pending:
text = WAITING_TEXT.get(pending[-1].kind)
elif step["state"] == "failed" and kind == "action_confirm" and decision == "reject":
+14 -2
View File
@@ -171,9 +171,12 @@ def _wrap(
elapsed = int((time.perf_counter() - started) * 1000)
message = _describe(last_error)
# node_id 要带上:前端靠它把出错的节点从"转圈"收敛成"失败"。
# error 是给人看的那句,原始异常放 detail,排查的人照样拿得到
# error 是给人看的那句,原始异常放 detail,排查的人照样拿得到。
# 被驳回的这一步照旧发 node.failed(时间线、实例进度按它收尾这一步),另带 reason=rejected(和 run.cancelled 同一个
# 字段名)、哪种关卡、结论、谁、备注:画布、航迹、时间线、问数据过程、复核规则、实例进度都按这一个字段认「被驳回」,
# 不再画成失败、也不再各自去猜报错原文(总方案 X9)。真的出错的 node.failed 一字不变,没有这几个键
ctx.emit(EventType.NODE_FAILED, error=message, duration_ms=elapsed, node_id=node.id,
detail=_detail(last_error))
detail=_detail(last_error), **_rejected(last_error))
# 容错模式:记下错误继续往下走,而不是让整张图挂掉。驳回不算错误,不走这里(见上)
if node.config.get("on_error") == "continue" and not isinstance(last_error, NodeRejected):
@@ -204,6 +207,15 @@ def _describe(error: BaseException | None) -> str:
return f"执行出错:{describe_exception(error)}"
def _rejected(error: BaseException | None) -> dict[str, Any]:
"""被人驳回的那一步,node.failed 上多带的几个键(同 runner.rejected_data 的说法,节点 id、名字事件本身就有);
别的失败为空。"""
if not isinstance(error, NodeRejected):
return {}
return {"reason": "rejected", "kind": error.kind, "decision": error.decision, "actor": error.actor,
"note": error.note}
def _detail(error: BaseException | None) -> str | None:
"""原始异常。NodeError 自己是人话,真正的原因在它包住的那个异常里。"""
if isinstance(error, NodeError):
+7
View File
@@ -219,6 +219,13 @@ def scan(events: Iterable[Any], output: Mapping[str, Any] | None = None) -> list
else:
add("tool_error", f"工具 {tool} 调用失败:{err}", BROKEN)
elif etype == EventType.NODE_FAILED and data.get("reason") == "rejected":
# 人的驳回(engine/compiler 给被驳回的那一步的 node.failed 带 reason=rejected,总方案 X9):不是故障,不说「执行
# 失败」;也不进 RETRYABLE——重跑只会把同一件事再问一遍人。答案没走完是缺口,记 DEGRADED(没有成果时
# empty_output 另按 BROKEN 说)
err = str(data.get("error") or "").strip()[:200]
add("node_rejected", f"节点「{titles.get(node_id) or node_id}」被驳回:{err}", DEGRADED)
elif etype == EventType.NODE_FAILED:
err = str(data.get("error") or "").strip()[:200]
add("node_failed", f"节点「{titles.get(node_id) or node_id}」执行失败:{err}", BROKEN)
+16
View File
@@ -95,6 +95,22 @@ def test_node_and_run_failures_are_broken():
assert all(s.severity == rv.BROKEN for s in signals)
def test_a_rejected_node_is_a_human_decision_not_a_fault():
"""被驳回的那一步节点层照旧发 node.failed,数据里带 reason=rejected(总方案 X9、05 (a)):复核不能把它说成「执行失败」,
更不能当成「重跑一次大概率能好」——重跑只会把同一件事再问一遍人。没有 reason 的老运行照旧按失败算。"""
signals = rv.scan([
ev(EventType.NODE_STARTED, node_id="gate", label="把关"),
ev(EventType.NODE_FAILED, node_id="gate", error="人工驳回:不对", reason="rejected", kind="approve",
decision="reject"),
], OK_OUTPUT)
[signal] = signals
assert signal.kind == "node_rejected" and signal.severity == rv.DEGRADED
assert signal.detail == "节点「把关」被驳回:人工驳回:不对" and "失败" not in signal.detail
assert not rv.should_retry(signals)
legacy = rv.scan([ev(EventType.NODE_FAILED, node_id="gate", error="人工驳回:不对")], OK_OUTPUT)
assert [s.kind for s in legacy] == ["node_failed"] and rv.should_retry(legacy)
def test_warn_classified_by_code_not_by_message():
"""分类挂在 code 上。消息是给人读的,随时会改。"""
assert rv.scan([warn("agent 用满了 8 步还没给出结论。", "step_limit")], OK_OUTPUT)[0] \
+49
View File
@@ -107,6 +107,55 @@ async def test_a_rejection_ends_the_run_as_cancelled_with_who_and_why(kind):
assert row.manifest_hash and row.manifest_seq == cancelled.seq
@pytest.mark.parametrize("kind", list(GATES))
async def test_the_rejected_step_says_so_on_its_node_failed(kind):
"""节点层照旧发 node.failed(时间线、实例进度按它收尾这一步),数据里带上 reason=rejected(和 run.cancelled 同一个字段名)、
哪种关卡、结论、谁、备注:画布、航迹、时间线、问数据过程据此画「被驳回」,不画成失败(05 (a)、总方案 X9)。原话照旧。"""
run_id = await _rejected_run(kind)
await _wait(run_id, ("cancelled", "failed", "succeeded"))
[failed] = [e for e in await _events(run_id, "node.failed") if e.node_id == "gate"]
data = failed.data
assert data["error"] == GATES[kind][1]
assert {k: data.get(k) for k in ("reason", "kind", "decision", "actor", "note")} == {
"reason": "rejected", "kind": kind, "decision": "reject", "actor": "张工", "note": "不对"}
async def test_a_plain_node_failure_carries_no_reason():
"""真的出错(这里是工具不存在):node.failed 和以前一字不差,没有 reason 这个键。"""
bad = node("gate", "tool", label="查一下", tool="no_such_tool_here", args={})
run = await run_manager.start(graph=chain(START, bad, OUT), input_payload={"q": "x"})
final = await _wait(run.id, ("cancelled", "failed", "succeeded"))
assert final.status == "failed"
[failed] = [e for e in await _events(run.id, "node.failed") if e.node_id == "gate"]
assert "reason" not in failed.data and "decision" not in failed.data
async def test_the_instance_timeline_says_rejected_without_a_task():
"""实例进度:被驳回的那一步按 node.failed 上的 reason 画「被驳回」——工具调用审批这类没开待办的驳回,以前落成「失败」。
老运行(node.failed 没有 reason、也没有待办)照旧写失败。"""
from types import SimpleNamespace
from app.apps.resolve import _timeline
run_id = await _rejected_run("tool")
await _wait(run_id, ("cancelled",))
async with SessionLocal() as session:
new = await session.get(Run, run_id)
legacy = Run(workflow_name="老的失败", status="failed", graph=new.graph, input={}, error="出错了",
error_node_id="gate")
session.add(legacy)
await session.flush()
session.add_all([
RunEvent(run_id=legacy.id, seq=1, type="node.started", node_id="gate", ts=1.0, data={}),
RunEvent(run_id=legacy.id, seq=2, type="node.failed", node_id="gate", ts=2.0, data={"error": "出错了"}),
])
await session.commit()
steps = {s["node"]: s for s in (await _timeline(session, SimpleNamespace(instance=new)))["steps"]}
old = {s["node"]: s for s in (await _timeline(session, SimpleNamespace(instance=legacy)))["steps"]}
assert (steps["gate"]["state"], steps["gate"]["text"]) == ("rejected", "已驳回")
assert (old["gate"]["state"], old["gate"]["text"]) == ("failed", "失败")
async def test_a_timeout_rejection_is_recorded_as_expired_reject():
from app.process import tasks