feat(agent): domain events and transition whitelist validation (assignment 3)

Architecture (mirrors workflow/20261016 chatNode structure):
- events.py: client markers parsed to typed ClientEvent (sources
  verified against workflow JSON); plain text like '拍好了' is not an event
- transitions.py: LEGAL_TRANSITIONS whitelist drafted from chatNode
  prompts and Condition edges (16 entries, each with source_ref);
  is_legal_transition rejects unlisted/terminal-violating moves
- graph.py: multi-node graph (collect/single_photo/dual_photo/...
  matching workflow chatNodes), route_by_stage conditional edges,
  _fake_llm_propose simulates LLM stage-code proposals (assignment 4
  replaces with real LLM); code only keeps context bookkeeping
  (no_reply_count) and validation
- state.py: expanded domain fields (pending_question, accident/
  person fields, clarify/no-reply counts, turn_patch, state_version)

Key finding: 单车拍照#6/双车拍照#7 are browser_addon branches ending
at 0000, so 2005/2015 allow both 300x (direct) and 0000.

Verified: pytest 87 passed (24 transition/event tests newly green).
Known limit: stage proposals come from a fake LLM; per-stage scripts
and real LLM integration are assignments 4-5.
This commit is contained in:
Eric Wang
2026-10-19 16:25:04 +08:00
parent 5d68b731d4
commit 8323d53a70
7 changed files with 582 additions and 68 deletions
+27 -6
View File
@@ -1,8 +1,13 @@
"""图状态定义(作业 1)。
"""图状态定义。
最小状态只包含脚手架所需的五个字段;作业 3 会扩充领域状态
(阶段、事故字段、人员字段、澄清计数、本轮 patch 等)。
所有字段必须可序列化,不得保存 HTTP client、数据库连接等资源。
作业 1 的五个基础字段之上,作业 3 扩充领域状态:内部阶段、原始状态码、
待回答问题、事故字段、两方人员字段、字段确认状态、澄清计数、无回复计数、
本轮 patch 和业务版本。
约束(迁移计划作业 3-2):所有字段必须可序列化,
不保存 HTTP client、数据库连接、音频或图片 base64。
字段读取一律用 ``state.get(...)`` 并提供默认值——首轮会话没有
历史 checkpoint,字段可能尚未初始化。
"""
from typing import TypedDict
@@ -11,12 +16,28 @@ from typing import TypedDict
class AgentState(TypedDict):
"""一次对话轮次在图中流动的状态。
``turn_count`` 由同一 ``thread_id`` 的 checkpoint 承载,
节点每轮只负责 +1,不负责跨会话记忆。
``turn_count``、``stage_code`` 等由同一 ``thread_id`` 的
checkpoint 承载跨轮记忆;节点返回字段增量,LangGraph 合并写回。
"""
# ---- 基础(作业 1) ----
session_id: str
turn_count: int
input_text: str
response_text: str
stage_code: str
# ---- 客户端事件(作业 3) ----
# ClientEvent.value 或 ""(普通输入)。由 normalize_input 每轮解析。
client_event: str
# ---- 领域状态(作业 3,作业 4—6 逐步充实) ----
raw_status_code: str # 原始状态码(外部归一化前的内部码)
pending_question: str # 当前待回答问题 ID(如 "driver_identity")
accident_fields: dict # 事故字段:jdcsl/ywrysw/sgfssj/...
person_fields: dict # 两方人员字段:xm1/2、hphm1/2、sfzmwh1/2...
confirm_status: dict # 字段确认状态:unknown/candidate/confirmed
clarify_count: int # 澄清计数(阈值按阶段配置,作业 4-9)
no_reply_count: int # 连续无回复计数(两次 → 0004)
turn_patch: dict # 本轮输出字段差量;每轮开始清空(作业 3-7)
state_version: int # 业务版本(作业 7 并发冲突检测)