mirror of
https://github.com/ThearchyHelios/AgentLab.git
synced 2026-11-24 14:52:58 +00:00
同一个子工作流节点执行多次时子图里的节点分得开:子图这一次跑过的每一步交回父图的 trail(节点 id 接上前缀),下一次执行从那里接着数第几次执行(engine/evidence.next_exec),台账、证据面板不再几条都记第 1 次;写外部系统的幂等键按这次运行里的节点 id 算(「sub/pay」)。以前循环里第二轮的子图写入算出和第一轮同一个键,交回第一轮的回执、外部系统少收一笔,父图里同名的节点、套同一个工作流的两个子工作流节点也撞键。升级前停在子图里的运行恢复时照旧按子图里的 id 认出已经生效的那一笔、不重发(父图里有同名节点时不认)
This commit is contained in:
@@ -43,6 +43,12 @@ class RunContext:
|
||||
"""这张图里的节点 id → 这次运行里的 id(事件、工件上记的那个):最外层原样,子工作流里加前缀。"""
|
||||
return f"{self.prefix}{node_id}"
|
||||
|
||||
@property
|
||||
def root_ids(self) -> frozenset[str]:
|
||||
"""这次运行最外层图上的节点 id:最外层就是这张图的,子工作流里是 multi.run_subgraph 交下来的。"""
|
||||
given = self.extra.get(ROOT_IDS_KEY)
|
||||
return frozenset(given) if given is not None else frozenset(n.id for n in self.spec.nodes)
|
||||
|
||||
def global_label(self, title: str) -> str:
|
||||
"""这张图里的节点标题 → 这次运行里给人看的名字:最外层原样,子工作流里接上子工作流的名字(「子工作流 › x」,嵌套的
|
||||
一层层接上,同 multi.run_subgraph 记在异常上的 label)。父运行的图上没有子工作流里的节点,各处照这个名字说。"""
|
||||
@@ -53,6 +59,8 @@ class RunContext:
|
||||
PREFIX_KEY = "node_prefix"
|
||||
#: 子工作流里的节点名前缀(「子工作流 › 」,同上):RunContext.global_label
|
||||
LABEL_PREFIX_KEY = "node_label_prefix"
|
||||
#: 最外层图上的节点 id(同上,只在子工作流里有):RunContext.root_ids
|
||||
ROOT_IDS_KEY = "root_node_ids"
|
||||
#: 父运行的事件流(RunContext.extra 里的键,同上)。子工作流在父节点里用 ainvoke 跑,LangGraph 不把它的 custom 事件交给
|
||||
#: 父图的流(ainvoke 不开 custom 模式,get_stream_writer 拿到的是空的那个):子工作流里每一步以前在运行的事件里一条都
|
||||
#: 没有,时间线看不到、封存也不含它查过的数。所以 run_subgraph 把父图那一刻的写入口交下来,子工作流的节点经它发
|
||||
|
||||
@@ -14,7 +14,9 @@ from app.core.errors import describe_exception, raw_detail
|
||||
from app.core.events import EventType
|
||||
from app.db.base import SessionLocal
|
||||
from app.db.models import Workflow
|
||||
from app.engine.context import LABEL_PREFIX_KEY, PREFIX_KEY, STREAM_KEY, NodeContext, NodeError, stream_writer
|
||||
from app.engine.context import (
|
||||
LABEL_PREFIX_KEY, PREFIX_KEY, ROOT_IDS_KEY, STREAM_KEY, NodeContext, NodeError, stream_writer,
|
||||
)
|
||||
from app.engine.lineage import whole_ref
|
||||
from app.engine.state import GraphState, message_text, template_context
|
||||
from app.engine.toolcalls import (
|
||||
@@ -692,8 +694,16 @@ async def run_subgraph(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
# 子图里的节点经父运行的写入口发事件、节点 id 带前缀;嵌套的子图拿到的是同一个写入口、更长的前缀
|
||||
extra={**{k: v for k, v in ctx.run.extra.items() if k not in ("resumed", "actors")},
|
||||
PREFIX_KEY: f"{ctx.run.global_id(ctx.node.id)}/", LABEL_PREFIX_KEY: f"{ctx.run.global_label(ctx.node.title)} › ",
|
||||
STREAM_KEY: stream_writer(ctx.run)},
|
||||
STREAM_KEY: stream_writer(ctx.run),
|
||||
# 最外层图上的节点 id:子图里的写入认升级前按子图里的 id 记的幂等键时,排除父图同名节点记的那一条(tools._run_write)
|
||||
ROOT_IDS_KEY: ctx.run.extra.get(ROOT_IDS_KEY) or frozenset(n.id for n in ctx.run.spec.nodes)},
|
||||
)
|
||||
# 这个子工作流节点之前几次执行里,子图各节点跑过的记录(交回父图 trail 的,见下):子图从它们接着数第几次执行
|
||||
# (engine/evidence.next_exec)。以前每次都从空的 trail 起步,循环里第二轮的子图节点又记成第 1 次:台账分不出是哪一次,
|
||||
# 写外部系统的幂等键(按第几次执行算)和第一轮撞上,第二轮的写入交回第一轮的回执、不发送
|
||||
prefix = f"{ctx.node.id}/"
|
||||
seed = [{**t, "node_id": str(t.get("node_id"))[len(prefix):]} for t in state.get("trail") or []
|
||||
if isinstance(t, dict) and str(t.get("node_id") or "").startswith(prefix)]
|
||||
|
||||
ctx.emit(EventType.LOG, level="info",
|
||||
message=f"进入子工作流「{workflow.name}」({len(sub_spec.nodes)} 个节点)")
|
||||
@@ -711,7 +721,7 @@ async def run_subgraph(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
"output": {},
|
||||
"messages": [],
|
||||
"usage": {},
|
||||
"trail": [],
|
||||
"trail": seed,
|
||||
"loops": {},
|
||||
},
|
||||
{"recursion_limit": limit},
|
||||
@@ -732,6 +742,9 @@ async def run_subgraph(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
raise
|
||||
|
||||
output = result_state.get("output") or {}
|
||||
# 这一次执行新跑的那几步(trail 只追加:前面是交进去的 seed)
|
||||
ran = [t for t in (result_state.get("trail") or [])[len(seed):] if isinstance(t, dict)]
|
||||
result_state = {**result_state, "trail": ran}
|
||||
result = {
|
||||
"output": output,
|
||||
"workflow": workflow.name,
|
||||
@@ -754,11 +767,12 @@ async def run_subgraph(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
}
|
||||
if ledger := [_forwarded(e, ctx.node.id) for e in result_state.get("evidence") or [] if isinstance(e, dict)]:
|
||||
updates["evidence"] = ledger
|
||||
if rejected := _rejected_steps(result_state, ctx.node.id):
|
||||
# 子工作流里走了驳回出口的那几步(engine/compiler._rejected_exit 记的 outcome=rejected)交回父图的 trail:运行收尾
|
||||
# 据此在终态事件上照实写「途经驳回」(runner._rejected_via),实例页、通知、定时任务的 Webhook 说得出「子工作流 › 把关」
|
||||
# 被驳回、转到哪。名字、转到哪几步在子图里已经按这次运行的说法记好(global_label、global_id),这里只接上节点 id 的前缀
|
||||
updates["trail"] = rejected
|
||||
if ran:
|
||||
# 子图这一次跑过的每一步交回父图的 trail,节点 id 接上前缀(「子工作流节点 id/…」):同一个子工作流节点下一次执行时
|
||||
# 从这里接着数(上面的 seed);走了驳回出口的那几步(outcome=rejected)运行收尾据此在终态事件上照实写「途经驳回」
|
||||
# (runner._rejected_via),实例页、通知、定时任务的 Webhook 说得出「子工作流 › 把关」被驳回、转到哪(名字、转到哪几步
|
||||
# 在子图里已经按这次运行的说法记好:global_label、global_id)。父图自己的节点按自己的 id 数,不受影响
|
||||
updates["trail"] = [{**t, "node_id": f"{prefix}{t.get('node_id')}"} for t in ran]
|
||||
var_name = ctx.cfg("assign_to", "")
|
||||
if var_name:
|
||||
updates["vars"] = {var_name: output}
|
||||
@@ -769,12 +783,6 @@ async def run_subgraph(state: GraphState, ctx: NodeContext) -> dict[str, Any]:
|
||||
_LEDGER_NODE_KEYS = ("node_id", "card")
|
||||
|
||||
|
||||
def _rejected_steps(sub_state: dict[str, Any], node_id: str) -> list[dict[str, Any]]:
|
||||
"""子工作流的 trail 里走了驳回出口的那几步,节点 id 换成父图这一层的(「子工作流节点 id/…」)。"""
|
||||
return [{**t, "node_id": f"{node_id}/{t.get('node_id')}"} for t in sub_state.get("trail") or []
|
||||
if isinstance(t, dict) and t.get("outcome") == "rejected"]
|
||||
|
||||
|
||||
def _forwarded(entry: dict[str, Any], node_id: str) -> dict[str, Any]:
|
||||
"""子图台账里的一条交回父图:节点 id 加上「子工作流节点 id/」前缀,别的照抄。"""
|
||||
out = dict(entry)
|
||||
|
||||
@@ -274,9 +274,14 @@ async def _run_write(state: GraphState, ctx: NodeContext, name: str, args: dict[
|
||||
from app.process import actions
|
||||
|
||||
exec_no = next_exec(state, ctx.node.id)
|
||||
# 幂等键按这次运行里的节点 id 算(子工作流里的写「sub/pay」):以前按子图里的 id(pay)算,父图里同名的节点、套同一个
|
||||
# 工作流的两个子工作流节点撞同一个键,后发的那一笔交回先发的回执、不发送。升级前停在子图里的运行按子图里的 id 记过,
|
||||
# 恢复时整个子图重跑:照旧认那个键(legacy_node_id),已经生效的不重发;父图里有同名节点时不认(那一条可能是它的)
|
||||
node_id = ctx.run.global_id(ctx.node.id)
|
||||
legacy = ctx.node.id if node_id != ctx.node.id and ctx.node.id not in ctx.run.root_ids else None
|
||||
try:
|
||||
prep = await actions.prepare(run_id=ctx.run.run_id, node_id=ctx.node.id, exec_no=exec_no,
|
||||
capability=capability, args=args)
|
||||
prep = await actions.prepare(run_id=ctx.run.run_id, node_id=node_id, exec_no=exec_no,
|
||||
capability=capability, args=args, legacy_node_id=legacy)
|
||||
except actions.ActionFailed as e:
|
||||
raise NodeError(ctx.node.id, str(e)) from e
|
||||
if prep.receipt is not None:
|
||||
@@ -317,8 +322,8 @@ async def _run_write(state: GraphState, ctx: NodeContext, name: str, args: dict[
|
||||
# 外部系统会把审批人当成申请人;无人值守的写入没有人确认,不写 act
|
||||
on_behalf = {"sub": prep.run.get("started_by"), "act": confirmer}
|
||||
try:
|
||||
result = await actions.send(prep, capability, run_id=ctx.run.run_id, node_id=ctx.node.id,
|
||||
exec_no=exec_no, emit=ctx.emit, on_behalf=on_behalf)
|
||||
result = await actions.send(prep, capability, run_id=ctx.run.run_id, node_id=node_id,
|
||||
exec_no=exec_no, emit=ctx.emit, on_behalf=on_behalf, confirm_node=ctx.node.id)
|
||||
except actions.ActionFailed as e:
|
||||
raise NodeError(ctx.node.id, str(e)) from e
|
||||
return _write_updates(ctx, result)
|
||||
|
||||
Reference in New Issue
Block a user