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)

事件类型

事件说明
EventDoneRun 正常结束
EventError错误
EventInterrupted被中断
stream_delta流式文本增量
tool_call工具调用
tool_result工具结果
plan_created计划创建
plan_updated计划步骤更新

终止路径

正常终止

模型返回文本回复(无 tool_call)→ EventDone("end_turn")。

异常终止

终止原因事件
context_cancelledEventDone("context_cancelled")
cycle_detectedEventDone("cycle_detected")
budget_exhaustedEventDone("budget_exhausted")
panic_stopEventError
cognitive_errorEventError

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-43SessionState 是下一轮历史的唯一来源

降级

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 → 超时终止。


相关文档