Agent 运行状态机与动作/观测轨迹
Agent 辅助工程检查
前置要求
第 73-75 天的 Agent 循环与工具协议学习,以及 Ticket 22 的有界 Agent 项目(projects/agent_learning)。
学习目标
识别有界 Agent 所携带的完整运行状态——动作/观测轨迹、步数计数器与重试计数器——并枚举其三种合法终止状态:goal_reached、tool_failure、step_budget。
操作指引
1. 打开 projects/agent_learning/agent.py 阅读 run_task。
- 每次运行的可变状态:trace({action, observation} 对的只追加列表)、retries(ok=false 的观测计数)、隐式步数计数器 len(trace)。
- 终止迁移:write_report 返回 ok=true -> status=completed, termination=goal_reached;retries 超过 max_retries(=2) -> status=failed, termination=tool_failure;两者皆未发生而循环结束 -> status=budget_exhausted, termination=step_budget。
2. 注意被刻意省略的内容:没有思维链字段,也没有隐藏草稿板——轨迹就是全部持久化状态(project.json 的 evidence.forbidden_fields 列出 thought 与 chain_of_thought)。
3. 与生产级检查点机制对比:LangGraph 的 checkpointer 在每个超步持久化图状态;Temporal 通过重放事件历史重建状态。我们的轨迹正是同一思想的最小事件日志。
4. 运行 python -m projects.agent_learning.cli evaluate --fixture summarize-notes --output /tmp/agent-run.json,检查 result.status 与 result.termination。
自主演练与记录
先预测正常运行四步轨迹,再执行 CLI 并标注每步迁移(提出动作 -> 记录观测 -> 更新计数器)。确认运行以 steps=4、retries=0、termination=goal_reached 结束。
唯一能产生 completed 的迁移是 write_report 观测返回 ok=true;其余成功步骤只是向轨迹追加记录。
steps=4:list_notes -> read_note(alpha.txt) -> read_note(beta.txt) -> write_report。第 4 步 write_report 观测返回 ok,状态翻转为 completed。因无 ok=false 观测,retries 保持 0。termination='goal_reached'。
不运行代码即可说出:失败观测时递增的计数器(retries)、结束健康运行的上限(max_steps=6)、唯一成功即提前终止的动作(write_report)。
权威源资源与权利证明
LangGraph Persistence: Checkpoints, Threads, and State Snapshots
对应章节 / 时间戳: Persistence: Checkpointer vs. store (checkpoints keyed by thread_id); the page now redirects to docs.langchain.com
验证版本与日期: LangGraph documentation (Verified: 2026-10-30)
Primary reference for agent-run checkpointing: LangGraph checkpointers persist graph state at every super-step so interrupted runs resume from the last committed snapshot, the production analogue of this project's action/observation trace.
Agent loop fundamentals, tool calling, and JSON state serialization.
The checkpoint/replay exercise is implemented directly against the projects/agent_learning trace JSON; no framework installation is required.
对比候选资源与选型分析 (2)
Temporal persists event histories for replay-based recovery of arbitrary workflows; LangGraph targets agent graphs specifically and stays closer to the learner's tool-loop mental model.
Provider-scoped SDK sessions; LangGraph's explicit checkpoint model exposes the underlying state-transition semantics more transparently.
Temporal Documentation: Durable Execution and Event History Replay
对应章节 / 时间戳: What is Temporal?: Durable Execution
验证版本与日期: Temporal documentation (Verified: 2026-10-30)
Standard industrial reference for durable execution: workflow state is rebuilt by replaying a persisted event history through deterministic code, exactly the checkpoint/replay mechanism learners reproduce against the agent trace.
State machines, event logs, and deterministic replay concepts.
Replay semantics are exercised offline against the deterministic adapter; Temporal itself is never installed.
对比候选资源与选型分析 (2)
Agent-specific checkpointing; Temporal supplies the general event-sourced replay model that motivates the exercise.
Temporal's predecessor with equivalent replay semantics; Temporal's docs are more actively maintained.