Run 生命周期
wesgine Agent Run 从发起到结束的完整流程、终止哲学与事件体系。
Run 入口
events, err := cell.Runtime().Run(ctx, AppRunRequest{
Actor: "alice",
SessionID: "s1",
AgentID: "agent-legal",
Messages: []Message{...},
Model: "gpt-4o", // 必填(INV-MODEL-01)
})
必填字段
| 字段 | 说明 |
|---|---|
Actor | 一等字段,空 → ErrActorRequired |
Model | 空 → ErrModelRequired(fail-closed) |
Messages | 用户消息 |
Run 与 SSE 解耦
INV-RUN-DETACH:Run 生命周期始终与传输层解耦。
SSE 断开 ≠ 取消 Run
取消 Run 的唯一途径:
| 途径 | 触发方 |
|---|---|
POST /sessions/{sid}/interrupt | 用户主动 |
| CycleDetector HITL 超时 | 真循环 |
| ErrorStreakDetector HITL 超时 | 伪进展 |
BudgetCheckFn 返回 Exhausted | 商业限制 |
| Cell Stop | 生命周期 |
引擎不因 counter 阈值主动终止(INV-TERM-01)。
SSE 事件流
POST /cells/{id}/run → SSE 流式 Run
GET /cells/{id}/runs/{runID}/events → SSE Replay(?from_seq=N)
事件类型
| 事件 | 说明 |
|---|---|
EventDone | Run 正常结束 |
EventError | 错误 |
EventInterrupted | 被中断 |
stream_delta | 流式文本增量 |
tool_call | 工具调用 |
tool_result | 工具结果 |
plan_created | 计划创建 |
plan_updated | 计划步骤更新 |
终止路径
正常终止
模型返回文本回复(无 tool_call)→ EventDone("end_turn")。
异常终止
| 终止原因 | 事件 |
|---|---|
context_cancelled | EventDone("context_cancelled") |
cycle_detected | EventDone("cycle_detected") |
budget_exhausted | EventDone("budget_exhausted") |
panic_stop | EventError |
cognitive_error | EventError |
Terminal Guard
INV-TERM-04:runLoop 的每个 return 路径必须发出终端事件。
缺少终端事件 → terminal guard 合成 EventError(missing_terminal_event)。
CognitiveSettlement
Run 终态的强制阶段(INV-CTX-40),不可跳过(唯一例外:panic_stop)。
Run 结束
↓
CognitiveSettlement(主对话模型产出)
↓ 产出
SessionState + Memory + Reflections + Learnings
约束
| 不变量 | 说明 |
|---|---|
| INV-CTX-41 | 主对话模型产出,不用辅助模型 |
| INV-CTX-42 | 产出不可见于用户(不推送 SSE) |
| INV-CTX-43 | SessionState 是下一轮历史的唯一来源 |
降级
3 次重试 → mechanicalFallback + degraded=true。
Run 摘要消息
RunEnd 时引擎写入 role=system 摘要消息到 wes_messages(INV-PERSIST-03)。
孤儿 Run
SSE 断开后 Run 继续执行。可标记 status=resumable:
GET /observe/runs?status=resumable
CycleDetector
检测真循环的两阶段协议:
| 阶段 | 触发 | 行为 |
|---|---|---|
| 自愈 | 首次 CycleTerm | 注入纠正消息 + 重置 counter |
| HITL | 第二次 CycleTerm | 问用户怎么办,超时 60s 终止 |
工具分类
| 类别 | 阈值特征 |
|---|---|
| exploration | 宽松(读操作) |
| action | 适中(写操作) |
| plan | 较严 |
ErrorStreakDetector
伪进展检测(INV-STREAK-01):连续错误达到阈值 → HITL → 超时终止。
相关文档
- Session 管理 →
session-management.md - CognitiveSettlement →
cognitive-settlement.md - CycleDetector 配置 →
cycle-detector.md - Plan 系统 →
plan-system.md