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.