Skip to content

Graph 表示与依赖校验

1. 为什么这组知识要一起学

进入 Harness 的执行内核:状态机、DAG 调度、取消、失败传播、Checkpoint、Replay 与长任务终止。

这几个知识点处在同一条工程链上。如果只记单个名词,很容易在真实系统里把责任放错层:例如让模型管理程序事实、让数据库 transaction 承担外部 API 原子性,或把一个 provider SDK 的行为误认为 Agent 的通用规律。

2. Mental Model

Runtime 是任务执行内核:状态机定义事实,Graph 定义依赖,Scheduler 控制并发,Checkpoint 让执行跨失败和时间继续。

text
Plan → Graph Compile → Validate → Ready Queue → Node Run → Pending Writes/Checkpoint → Failure/Interrupt → Resume → Finish

3. 核心机制

Graph Representation

Graph 需要 nodes、dependencies、dependents 与 indegree;编译期检查未知依赖和 cycle,避免执行中才发现无法调度。

Dependency Validation

Graph 需要 nodes、dependencies、dependents 与 indegree;编译期检查未知依赖和 cycle,避免执行中才发现无法调度。

Cycle Detection

Graph 需要 nodes、dependencies、dependents 与 indegree;编译期检查未知依赖和 cycle,避免执行中才发现无法调度。

4. 最小实现 / 伪代码

下面代码只表达边界和生命周期,不要求照抄到项目中:

python
while ready:
    batch = scheduler.take_ready(limit=max_concurrency)
    results = await executor.run_batch(batch)
    for result in results:
        state = reducer.apply(state, result)
    await checkpoint_store.save(state)

真正实现时应把 I/O、状态持久化、错误翻译和策略注入拆成可测试组件,而不是把示例扩成一个巨型函数。

5. 在 Shadow Harness 中怎么落地

RunState/NodeState/ExecutableGraph/Scheduler/CheckpointStore 分离;状态转换和 dependency satisfaction 必须 deterministic。

建议为本章涉及的行为留下明确的 domain object、interface 和 trace event;只要一个关键行为只能通过读日志猜测,就说明 Runtime contract 仍不够清晰。

6. Production Engineering 检查项

  • terminal state 单调
  • 成功副作用不能因 sibling 失败重复
  • cancel 与 fail 分开
  • deadline 从 run 向 node/tool 传播

7. Failure Modes

7.1 把拓扑排序当 scheduler

当出现「把拓扑排序当 scheduler」时,读取状态转换、checkpoint/pending writes 与 execution record,确认是否违反 no-double-execution 或 dependency invariant。 这类问题通常需要修改 contract、policy、state 或 adapter,而不是只追加 Prompt。

7.2 无限启动 READY node

当出现「无限启动 READY node」时,读取状态转换、checkpoint/pending writes 与 execution record,确认是否违反 no-double-execution 或 dependency invariant。 这类问题通常需要修改 contract、policy、state 或 adapter,而不是只追加 Prompt。

7.3 interrupt 前副作用不幂等

当出现「interrupt 前副作用不幂等」时,读取状态转换、checkpoint/pending writes 与 execution record,确认是否违反 no-double-execution 或 dependency invariant。 这类问题通常需要修改 contract、policy、state 或 adapter,而不是只追加 Prompt。

7.4 resume 重新执行已成功 branch

当出现「resume 重新执行已成功 branch」时,读取状态转换、checkpoint/pending writes 与 execution record,确认是否违反 no-double-execution 或 dependency invariant。 这类问题通常需要修改 contract、policy、state 或 adapter,而不是只追加 Prompt。

8. Trade-offs

内存 scheduler 简单但不可恢复;durable scheduler 成本高但适合长任务/HITL;按业务持续时间选择。

设计记录最好明确:当前约束是什么、备选方案有哪些、为什么现在选这个、未来什么条件出现时需要重构。 这样 ADR 才能随着模型和基础设施变化被重新审视。

9. Experiment / Evaluation

构造并行 A/B、B 首次失败、A 有副作用的场景,验证 resume 不重复 A,并验证 trace/checkpoint 一致。

实验应固定数据集、版本和环境,至少记录 success、latency、token/cost、attempt/step 数以及失败类型;涉及随机模型时需要重复运行而不是只看一次结果。

10. 常见问题

基础:Graph Representation 最容易被误解的点是什么?

Graph 需要 nodes、dependencies、dependents 与 indegree;编译期检查未知依赖和 cycle,避免执行中才发现无法调度。

机制:这些能力在一次 Run 的哪个生命周期阶段生效?

沿着 Plan → Graph Compile → Validate → Ready Queue → Node Run → Pending Writes/Checkpoint → Failure/Interrupt → Resume → Finish 找位置,并明确它的输入、输出、持久化事实和失败传播。

工程:如果这一层失败,应该由谁恢复?

先区分 transient failure、invalid input、permission、semantic failure 与 irreversible side effect。恢复策略属于拥有该状态与副作用的 Runtime/adapter,而不是交给模型自由决定。

设计:规模扩大 10 倍后,哪个假设最先失效?

优先检查 context/token、并发/连接池、catalog 大小、持久化吞吐、trace 体积、身份与租户隔离。不要默认“多加机器”能解决语义和一致性问题。

11. Sources

12. 本文结论

掌握本文的标准不是能背定义,而是能画出数据流、写出最小 contract、解释失败恢复,并用测试或 benchmark 证明设计没有只停留在概念层。

AI Engineering · Agent · Harness · Backend Systems