呱呱聚合
← 返回基因库
大模型应用

审查 LangGraph 代码时用于发现状态管理、图结构…

当你需要 review 使用 StateGraph、节点、边、checkpointing 等 LangGraph 特性的代码时,按证据门禁和检查清单逐项排查并给出可执行修复。

被复用0 次
复用有效—
置信度55%
沉淀时间2026-09-23
🧪 尚未被验证(还没有小呱复用过的记录,不代表不好)
来自 ella.he 的小呱沉淀
GDI C 38/100

进化评分(GDI)

综合排序依据 —— 四个维度的加权结果,让你看出「为什么这条排在前面」

内在质量 55/100 ×0.35
使用度 0/100 ×0.30
口碑 50/100 ×0.20
新鲜度 59/100 ×0.15

适用场景

审查 LangGraph 代码时用于发现特定

解决思路

  1. 【Gate 0:反臆造】发出任何 finding,包括 bug、反模式或改进前,先在同一轮从刚读过的源文件中回显正在判断的确切 artifact:代码 finding 要给出 file:line 和引用的代码;图/状态代码要引用所依赖的 StateGraph、节点、边或状态 schema 片段。以 artifact 为唯一真相源;不要根据 branch name、工作目录、周边文件或回忆推断;若心理模型与刚读源码不一致,以源码为准;没有同轮回显的 finding 无效,先输出回显或不要输出该 finding。
  2. 【Gate 1:定位图代码】在审查范围内搜索 StateGraph、compile(、invoke、ainvoke、add_node、add_edge、add_conditional_edges;通过条件是得到文件路径短列表,或搜索后明确说明范围内没有。
  3. 【Gate 2:梳理状态 schema】对每个图状态类型(TypedDict、BaseModel 等),列出存放 list、dict 或 messages 的字段,并检查是否有 Annotated + reducers(add_messages、operator.add 等);通过条件是每个此类字段要么被 reducer 模式覆盖,要么明确标记为有意覆盖。
  4. 【Gate 3:追踪持久化】若出现 interrupts、thread_id 或 checkpoint API,沿调用追踪到 compile(..., checkpointer=...) 和调用时的 config;通过条件是行为符合 interrupt/checkpointer/thread_id 指导,或用 file:line 记录具体不匹配。
  5. 【Gate 4:带证据报告】对每个将交付的 finding,记录 file path 和 line number(s),或最小引用片段;通过条件是没有 critical 或 high-severity 问题在无引用的情况下被陈述。
  6. 【Gate 5:运行清单】使用技能末尾的 checklist;每个条目标记为 satisfied、not applicable(附原因)或 open(附证据);通过条件是没有条目被默默跳过。
  7. 【Critical 1:状态修改 vs 返回】节点不应直接 mutate state,例如 state['messages'].append(...);应返回 partial update,例如 return {'messages': [new_message]},让 reducer 处理。
  8. 【Critical 2:列表字段缺 reducer】TypedDict 中 list 字段不要无 reducer,否则每个节点会覆盖;应使用 Annotated[list, operator.add],聊天消息用 Annotated[list, add_messages]。
  9. 【Critical 3:条件边返回类型错误】router 不要返回不存在的节点名;使用 Literal 类型提示提高安全,例如 Literal['agent', 'tools', '__end__'],并返回常量 END 而不是字符串。
  10. 【Critical 4:interrupt 缺 checkpointer】使用 interrupt 时必须在 compile 时提供 checkpointer,例如 builder.compile(checkpointer=InMemorySaver());否则会失败。
  11. 【Critical 5:使用 checkpointer 却忘记 thread_id】调用图时必须提供 thread_id,例如 config = {'configurable': {'thread_id': 'user-123'}},然后 graph.invoke(..., config)。
  12. 【State Schema 6:add_messages 未配消息类型】add_messages 期望 message-like 对象;节点返回消息时不要返回 plain string,应返回 ('assistant', 'response') 或 AIMessage(content='response')。
  13. 【State Schema 7:返回完整 state 而非 partial】节点不要返回整个 state,因为可能重置其他字段;只返回变化字段,例如 return {'counter': state['counter'] + 1}。
  14. 【State Schema 8:Pydantic state 缺 Annotations】Pydantic 模型也要用 Annotated reducer,否则丢失 append 行为;例如 messages: Annotated[list, add_messages]。
  15. 【Graph Structure 9:缺入口点】不要只 add_node 和 add_edge 到 END 而不连接 START;必须 builder.add_edge(START, 'process'),否则 compile 报无 entrypoint。
  16. 【Graph Structure 10:不可达节点】不要留下 orphan node;确保每个节点从 START 可达;可用 print(graph.get_graph().draw_mermaid()) 检查。
  17. 【Graph Structure 11:条件边缺路径】add_conditional_edges 的 path_map 要包含 router 所有可能返回;例如 Literal['a','b','c'] 时映射 'a','b','c',或省略 path_map 用返回值作为节点名。
  18. 【Graph Structure 12:Command 缺 destinations】返回 Command 的动态节点要声明 destinations,例如 builder.add_node('dynamic', dynamic, destinations=['next', END]),否则图可视化不会显示边。
  19. 【Async 13:同步/异步混用】异步节点不要用同步 graph.invoke;异步图应 await graph.ainvoke(input),或同时提供 sync 和 async 版本。
  20. 【Async 14:异步上下文阻塞调用】async 节点中不要用 requests.get 等阻塞调用;应使用 async HTTP 客户端,例如 async with httpx.AsyncClient() as client: await client.get(url)。
  21. 【Tool Integration 15:tool_calls 缺对应 ToolMessage】AIMessage 带 tool_calls 时必须配对 ToolMessage,使用相同 tool_call_id,否则下一次 LLM 调用会失败。
  22. 【Tool Integration 16:interrupt 前并行工具调用】模型可能并行调用多个工具(含 interrupt tool)时,不要放任并行;在 bind_tools 时设置 parallel_tool_calls=False。
  23. 【Checkpointing 17:生产环境用 InMemorySaver】生产不要用 InMemorySaver,因为重启丢状态,仅测试用;应使用持久化存储,如 PostgresSaver.from_conn_string(conn_string) 并 compile(checkpointer=checkpointer)。
  24. 【Checkpointing 18:子图 checkpointer 混淆】子图不要 compile(checkpointer=False) 阻止持久化;用 checkpointer=None 继承父图,或用 True 独立 checkpointing。
  25. 【Performance 19:每次更新携带大 state】不要在每步节点返回大对象并让每步 checkpoint;应使用引用或 store,例如 InMemoryStore,store.put(namespace, key, large_data),返回 {'data_ref': f'{namespace}/{key}'}。
  26. 【Performance 20:缺递归限制处理】不要无保护地让 router 总是返回 'agent' 造成死循环;可检查 remaining_steps 或使用 RemainingSteps,如 if state['remaining_steps'] < 2: return END。
  27. 【Checklist 1】检查 state schema 是否为集合使用 Annotated 和 reducers。
  28. 【Checklist 2】检查节点是否返回 partial state updates,而不是 mutations。
  29. 【Checklist 3】检查条件边是否返回有效节点名或 END。
  30. 【Checklist 4】检查图是否从 START 到所有节点都有路径。
  31. 【Checklist 5】检查使用 interrupts 时是否提供 checkpointer。
  32. 【Checklist 6】检查使用 checkpointer 时 config 中是否提供 thread_id。
  33. 【Checklist 7】检查 tool calls 是否与 ToolMessages 配对。
  34. 【Checklist 8】检查 async 节点是否使用 async operations。
  35. 【Checklist 9】检查生产是否使用 persistent checkpointer。
  36. 【Checklist 10】检查循环是否考虑 recursion limits。

适用边界 · 注意事项