Run 编排
wesgine 的 Run 编排与 Loop 协调机制——从请求到完成的全链路。
Run 入口
events, err := cell.Runtime().Run(ctx, AppRunRequest{
Actor: "alice",
SessionID: "s1",
AgentID: "agent-legal",
Messages: []Message{...},
})
Loop 循环
internal/loop/ 实现核心 for{} 循环:
AppRunRequest
→ Context Assembly(系统提示 + 历史 + Overlay)
→ LLM 调用(Cognitive Resolver)
→ 工具调用(Execution Registry)
→ 循环直到 end_turn / 中断 / 预算耗尽
→ CognitiveSettlement(Run 终态认知产物)
终止路径
Run 可通过以下方式终止:
| 终止原因 | 触发方 |
|---|---|
end_turn | 模型主动结束 |
interrupted | 用户中断 |
cycle_detected | CycleDetector |
budget_exhausted | BudgetCheckFn |
quality_revision_limit | QualityGate |
context_cancelled | ctx 取消 |
cognitive_error | LLM 调用失败 |
execute_error | 工具执行失败 |
panic_stop | 进程级崩溃 |
error_streak | ErrorStreakDetector |
每个 return 路径必须有终端事件
runLoop 的每个 return 路径必须在返回前发出终端事件(INV-TERM-04):
EventDoneEventErrorEventInterrupted
缺少终端事件 → terminal guard 合成虚假 EventError(missing_terminal_event)。
context 取消的特殊处理
callLLM 和 execute 返回 phaseReturn 时,若 ctx.Err() != nil:
发出 EventDone("context_cancelled") + 设 reason="context_cancelled"。
不依赖 for 循环顶部的 ctx.Err() 检查——phaseReturn 直接 return,永远到不了那里。
后台工作
Run 结束后的收尾工作异步执行:
- CognitiveSettlement(认知结算)
- SaveExplorationPaths(探索路径保存)
- CKG 写库
- Memory GC 触发
这些不占用下一轮 Run 的发送权。