Files
AgentScript/agent/backend/app/graph/state.py
T
HenryandClaude Code 26e13ff032 feat: Agent 工作区初始提交(源码+文档+Skills)
包含 CodeAgent 前后端源码、SKILLS、MCP、demo 代码与工作区文档。
排除依赖缓存、运行数据、备份与二进制附件,规则见 .gitignore。

Co-Authored-By: Claude Code <[email protected]>
2026-11-13 13:48:41 +08:00

114 lines
4.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
LangGraph 状态契约
==================
【功能说明】
定义主图(OrchestratorState)与 Worker 子图(WorkerState)的状态结构,
是整个 Agent 编排层的"数据总线"。所有节点通过状态读写协作。
【技术选型理由】
1. 用 TypedDict 而非 Pydantic:LangGraph 对 TypedDict 支持最原生,
基准文档第十~十一部分均以 TypedDict 演示,保持一致降低心智负担。
2. Reducer(归约器)是状态设计的核心:
- `Annotated[list, operator.add]`:并行 Worker 结果**追加合并**,
避免互相覆盖(这是企业级 Agent 最常见的状态设计坑);
- 单值字段(requirement/plan/evaluation)用覆盖语义。
3. error / status 走独立通道:任何节点失败都写入 error 并把 status 置为
degraded/failed,满足硬约束 5「异常不能直接崩溃,要有明确提示」。
【实现思路】
- 主图状态承载任务全生命周期数据;
- Worker 状态只关注单个 TaskStep 的执行细节,与主图解耦;
- status 枚举:running / waiting_approval / done / failed / degraded。
【潜在风险与应对】
风险1:并行 Worker 同时写同一字段 → 数据丢失。
应对:所有多值字段必须用 operator.add reducer,单测覆盖并行写入。
风险2:状态字段膨胀导致 checkpoint 变大。
应对:大文本(如完整 diff)只存摘要 + 指向文件的路径,不塞进状态。
风险3:节点返回值与状态字段名不匹配 → 静默不更新。
应对:节点返回 dict 的 key 必须与状态字段同名,单元测试断言。
"""
from __future__ import annotations
import operator
from typing import Annotated, Any, TypedDict
class TaskStep(TypedDict, total=False):
"""计划中的单个可执行步骤"""
step_id: str
description: str
target_hints: list[str]
expected_output: str
step_type: str # feature | bugfix | refactor
class OrchestratorState(TypedDict, total=False):
"""主图状态(Orchestrator)——任务全生命周期数据总线"""
# ---- 标识 ----
task_id: str
project_id: str
# ---- 需求 ----
raw_requirement: str
requirement: dict[str, Any] # intake 结构化产出(覆盖式)
use_vector_search: bool # 是否查询向量库(提交前可选)
# ---- 知识召回(追加式,可多次写入)----
knowledge_hits: Annotated[list[dict[str, Any]], operator.add]
# ---- 计划 ----
plan: dict[str, Any] # {steps: [TaskStep, ...]}(覆盖式)
# ---- 风险与审批 ----
risk_level: str # normal | high(high 才触发人工审批)
risk_warnings: Annotated[list[str], operator.add] # 软警告(不中断,仅报告提示)
approval: dict[str, Any] # human_gate 审批结果
# ---- Worker 产出(追加式,支持并行)----
worker_results: Annotated[list[dict[str, Any]], operator.add]
# ---- 评估与交付 ----
evaluation: dict[str, Any]
eval_retry: int # 评估修复轮数(Evaluator-Optimizer 计数)
report: str
# ---- 错误与状态 ----
error: str
status: str # running | waiting_approval | done | failed | degraded
class WorkerState(TypedDict, total=False):
"""Worker 子图状态——单个 TaskStep 的执行细节"""
task_id: str
project_id: str
step: TaskStep # 当前执行的步骤
model_preference: str # 任务级模型偏好:auto 或具体模型 ID
# ---- 执行产出(追加式)----
located_files: Annotated[list[str], operator.add]
edits: Annotated[list[dict[str, Any]], operator.add]
# ---- 验证 ----
verify_result: dict[str, Any]
retry_count: int
# ---- 错误 ----
error: str
# 状态字段常量(避免字符串硬编码散落各处)
STATUS_RUNNING = "running"
STATUS_WAITING_APPROVAL = "waiting_approval"
STATUS_DONE = "done"
STATUS_FAILED = "failed"
STATUS_DEGRADED = "degraded"
RISK_NORMAL = "normal"
RISK_HIGH = "high"