模型之外的一切驱动逻辑:会话/线程管理 · Op/Event 协议 · turn 状态机 · 流式采样 · 工具调度 · 审批沙箱 · 上下文压缩 · rollout 持久化
!cmd{id(UUIDv7), op, trace}submit · next_event · start_or_steer_turn · rollout 读写tx_sub / rx_event / agent_status / loop-termination;drop sender 即终止循环for_prompt() 产出请求 inputflowchart TB A["🧾 TurnInput start / steer / recover
(turn_input.rs 唯一路由决策点)"]:::start B["📸 捕获 StepContext
model · tools · env · mcp 同一份快照"]:::step C{"⚡ pre-sampling
需要压缩?"}:::dec C1["🗜 run_auto_compact
新窗口 / 远程压缩 V2·V1 / 本地摘要"]:::compact D["📦 build_prompt
历史 for_prompt() + ToolSpec[] + base_instructions"]:::step E["🌊 Responses API 流式采样
SSE / WebSocket · turn-scoped session · 重试"]:::model F{"🔽 输出 item"}:::dec H["🛠 ToolCallRuntime
并行 spawn(FuturesOrdered)"]:::tool I["🔐 审批:hooks → Guardian → 用户
沙箱 / execpolicy / 网络策略"]:::sec J["⚙️ 执行
shell · apply_patch · MCP · spawn_agent …"]:::tool K["📥 工具输出按调用顺序
record 进历史(FunctionCallOutput)"]:::step G{"🔁 needs_follow_up?
(工具待续 / pending input / end_turn=false)"}:::dec L["🏁 stop hooks → TurnComplete"]:::done A --> B --> C C -- 是 --> C1 --> D C -- 否 --> D D --> E --> F F -- "function / custom tool call" --> H --> I --> J --> K --> G F -- "assistant / reasoning 消息" --> G G -- "是 → 排空 steer/邮件 后继续" --> D G -- "否" --> L classDef start fill:#1e293b,stroke:#e8b84b,color:#fde68a,stroke-width:1.5px classDef step fill:#172033,stroke:#22d3ee,color:#a5f3fc classDef dec fill:#1c1930,stroke:#a78bfa,color:#ddd6fe classDef compact fill:#2a1f14,stroke:#fb923c,color:#fed7aa classDef model fill:#14241d,stroke:#34d399,color:#a7f3d0 classDef tool fill:#172033,stroke:#60a5fa,color:#bfdbfe classDef sec fill:#2a1720,stroke:#fb7185,color:#fecdd3 classDef done fill:#14241d,stroke:#34d399,color:#6ee7b7,stroke-width:1.5px
flowchart LR TC["模型发出 tool call
shell · apply_patch · MCP · 网络"]:::tool ORCH["ToolOrchestrator
(unified_exec / approvals)"]:::step H1{"❶ PreToolUse
hooks 决策?"}:::dec G1{"❷ StrictAutoReview
或 Guardian 启用?"}:::dec GR["🤖 Guardian 自动审查
(模型 reviewer)"]:::sec U1{"用户批准
被拒动作重试?"}:::dec U2["📨 审批请求事件
→ 前端弹窗"]:::step DEC["❸ 用户决策
ExecApproval / PatchApproval Op"]:::start RUN["▶️ 沙箱内执行
Seatbelt / bwrap / Win token
+ execpolicy + 网络代理"]:::model REJ["⛔ 拒绝 / 中止"]:::sec OUT["📤 ToolOutput → 历史
→ 下一次采样带回模型"]:::done TC --> ORCH --> H1 H1 -- "allow" --> RUN H1 -- "deny" --> REJ H1 -- "无决策" --> G1 G1 -- "是" --> GR GR -- "批准" --> RUN GR -- "拒绝" --> U1 U1 -- "是(可带规则修订)" --> RUN U1 -- "否" --> REJ G1 -- "否" --> U2 --> DEC DEC -- "批准(+ policy amendment 沉淀)" --> RUN DEC -- "拒绝 / Abort" --> REJ RUN --> OUT REJ --> OUT classDef tool fill:#172033,stroke:#60a5fa,color:#bfdbfe classDef step fill:#172033,stroke:#22d3ee,color:#a5f3fc classDef dec fill:#1c1930,stroke:#a78bfa,color:#ddd6fe classDef sec fill:#2a1720,stroke:#fb7185,color:#fecdd3 classDef model fill:#14241d,stroke:#34d399,color:#a7f3d0 classDef start fill:#1e293b,stroke:#e8b84b,color:#fde68a classDef done fill:#14241d,stroke:#34d399,color:#6ee7b7,stroke-width:1.5px
一个 session 一个 submission_loop task 顺序处理所有 Op;并发仅在 turn/工具内部显式 spawn,状态机无锁竞争。
前端与 core 之间只有 Op/Event;core 内部"问用户"(审批/提问/elicitation)统一为 oneshot + 应答 Op。
turn 有 TurnContext、每次采样有 StepContext:工具集、MCP、环境、权限按 step 冻结,请求自洽且可重放。
历史只追加不重写;环境信息以带标记 ContextualUserFragment 差异注入;超限走压缩/新窗口,硬上限兜底。
审批、permission profile、平台沙箱、execpolicy、网络代理在 orchestrator 层统一收口,工具 handler 不自行决策。
同一响应内多个工具调用并行 spawn,FuturesOrdered 按模型发出顺序回灌,兼顾并发与上下文确定性。
rollout 是唯一真相源;fork / resume / rollback / review 都是对事件流的读取、截断或重放。
CodexThread 是唯一对外面,Session/services 全部 crate-private,crate API 面最小化。