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_detectedCycleDetector
budget_exhaustedBudgetCheckFn
quality_revision_limitQualityGate
context_cancelledctx 取消
cognitive_errorLLM 调用失败
execute_error工具执行失败
panic_stop进程级崩溃
error_streakErrorStreakDetector

每个 return 路径必须有终端事件

runLoop 的每个 return 路径必须在返回前发出终端事件(INV-TERM-04):

缺少终端事件 → 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 结束后的收尾工作异步执行:

这些不占用下一轮 Run 的发送权。