docs(runtime): internal/runtime/loop 44 个文件注释改中文(逐行 1:1,代码零字节改动)
主人 2026-10-03 拍板「注释一律中文」;本模块按长期分批协议(一个模块一次派工)执行。 注释行数 1329 → 1329 逐行 1:1(含中文 33 → 1304);行尾注释 5 条一并译。 校验:机械校验(非注释改动行)= 空;独立 Go AST 程序(剥注释复渲)证明 44/44 文件代码全等; 全树 go test ./... 58 包 ok / 0 FAIL;交叉编译绿。 gofmt:capacity_loop_test.go / flow.go 改动前即未格式化,按铁律保留原样未动。
This commit is contained in:
1 parent
355f81d04a
commit
91e8d7a029
44 files changed
+1315
-1315
No files matched your search
@@ -10,26 +10,26 @@ import (
|
||||
"git.yeij.top/AskaEth/Cyrene_v2/internal/runtime/trace"
|
||||
)
|
||||
|
||||
// Run drives the agent loop: assemble a fresh model-visible context every round
|
||||
// (re-inject relevant memory from the current conversation, optionally compacting
|
||||
// the older turns when the context exceeds the configured budget), then run up to
|
||||
// MaxRounds steps. Each step runs the model (streaming through OnStream when
|
||||
// configured) and any requested tool calls are executed under a per-invoke
|
||||
// timeout and fed back as RoleTool messages so the model can continue.
|
||||
// A checkpoint is persisted at every step, every step is traced as a
|
||||
// state_transition span (with an llm_call child and tool_call spans for each
|
||||
// invocation), and when the turn settles the memory Extractor runs over the
|
||||
// turn's conversation (design/03 §1: the LLM extractor on a background
|
||||
// goroutine, so extraction never delays the reply) with errors logged rather
|
||||
// than fatal.
|
||||
// Run 驱动 agent 循环:每轮组装一份全新的模型可见上下文
|
||||
// (从当前会话重新注入相关记忆,上下文超出配置预算时
|
||||
// 可选地压缩更早的轮次),然后运行至多
|
||||
// MaxRounds 个 step。每个 step 运行模型(配置了 OnStream 时
|
||||
// 流式),所有被请求的工具调用在每次调用的超时下执行,
|
||||
// 并以 RoleTool 消息回灌,使模型能继续。
|
||||
// 每一步都持久化一个 checkpoint,每一步都作为
|
||||
// state_transition span 追踪(每次调用带一个 llm_call 子 span 与
|
||||
// tool_call span),且当 turn 落定时记忆 Extractor 在该
|
||||
// turn 的对话上运行(design/03 §1:LLM extractor 跑在后台
|
||||
// goroutine 上,因此提取绝不延迟回复),错误记录日志
|
||||
// 而非致命。
|
||||
//
|
||||
// When a turn settles with a Result, the OnTurnEnd hook is called once for that
|
||||
// turn (never per step) and its mood label is recorded on Result.Mood, so the
|
||||
// layers around the loop can advance persona state and tag the persisted reply.
|
||||
// 当 turn 带着 Result 落定时,OnTurnEnd 钩子对该
|
||||
// turn 调用一次(绝不按步调用),其情绪标签记录在 Result.Mood 上,
|
||||
// 使循环外围的分层能推进人格状态并标记持久化的回复。
|
||||
//
|
||||
// It returns the final answer, or a partial Result with StopReasonMaxRounds when
|
||||
// the budget is exhausted without a final answer. Step errors are wrapped with %w
|
||||
// so callers can classify them with errors.Is.
|
||||
// 它返回最终答案;预算耗尽而无最终答案时,返回带
|
||||
// StopReasonMaxRounds 的部分 Result。step 错误用 %w 包装,
|
||||
// 使调用方能以 errors.Is 分类它们。
|
||||
func (l *Loop) Run(ctx context.Context, sessionID string, messages []model.Message) (*Result, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
@@ -38,26 +38,26 @@ func (l *Loop) Run(ctx context.Context, sessionID string, messages []model.Messa
|
||||
return nil, fmt.Errorf("loop: run: %w", ErrNilAgent)
|
||||
}
|
||||
|
||||
// Seed the per-Run capacity accumulator at quiet so the state can progress
|
||||
// (quiet→awake→full) across a multi-step tool loop; a nil/disabled decider
|
||||
// leaves it unused. It is scoped to this Run, so concurrent runs never share.
|
||||
// The session id rides the same context so a capacity_awaken emission can be
|
||||
// targeted to the session that produced it (design/14 §5).
|
||||
// 以 quiet 播种该 Run 的 capacity 累加器,使状态能跨多步
|
||||
// 工具循环演进(quiet→awake→full);nil/被禁用的判定器
|
||||
// 使其不被使用。它作用域限于本次 Run,因此并发 run 绝不共享。
|
||||
// 会话 id 搭同一个 context,使 capacity_awaken 的发出能
|
||||
// 定向到产出它的会话(design/14 §5)。
|
||||
ctx = withCapState(ctx, &capStateAccum{state: capacity.StateQuiet})
|
||||
ctx = withLoopSession(ctx, sessionID)
|
||||
|
||||
// Resume an interrupted run (design §1 durable execution): when the session
|
||||
// has a resumable checkpoint, rebuild its context and continue at the next
|
||||
// step rather than restarting from a fresh conversation. A different
|
||||
// sessionID — or a checkpoint left by a completed run — falls through to a
|
||||
// fresh start (LoadLatest misses, or the stop reason is not resumable).
|
||||
// 恢复被中断的 run(design §1 durable execution):该会话有
|
||||
// 可恢复的 checkpoint 时,重建其上下文并从下一步继续,
|
||||
// 而不是从全新对话重启。不同的
|
||||
// sessionID —— 或已完成 run 留下的 checkpoint —— 落到
|
||||
// 全新开始(LoadLatest 未命中,或 stop reason 不可恢复)。
|
||||
msgs, startStep, _ := l.loadCheckpoint(ctx, sessionID, messages)
|
||||
msgs = l.resolveImages(ctx, msgs)
|
||||
toolState := &toolLoopState{prevFP: make(map[string]bool)}
|
||||
res := &Result{}
|
||||
// A branch Run (a sub-agent) folds its spans into the parent session's live
|
||||
// graph; only the owning Run starts and ends a round. Without this a sub-agent
|
||||
// would reset the frontend's single flowGraph and gap the parent's own nodes.
|
||||
// 分支 Run(子代理)把其 span 并入父会话的实时
|
||||
// 图;只有所属的 Run 开始与结束轮次。没有它,子代理
|
||||
// 会重置前端唯一的 flowGraph,并让父级自身的节点出现断档。
|
||||
if _, isBranch := flowBranch(ctx); l.flow != nil && !isBranch {
|
||||
l.flow.StartRound(sessionID, lastUserText(msgs))
|
||||
defer func() { l.flow.EndRound(sessionID, res.Text) }()
|
||||
@@ -67,9 +67,9 @@ func (l *Loop) Run(ctx context.Context, sessionID string, messages []model.Messa
|
||||
return nil, fmt.Errorf("loop: run: step %d: %w", step, err)
|
||||
}
|
||||
|
||||
// Compact older turns when the conversation exceeds the context budget,
|
||||
// then assemble the model-visible context for this round (fresh memory
|
||||
// injection + transform hook), so every step sees an up-to-date context.
|
||||
// 会话超出上下文预算时压缩更早的轮次,
|
||||
// 然后为本轮组装模型可见上下文(全新记忆
|
||||
// 注入 + transform 钩子),使每一步都看到最新上下文。
|
||||
msgs = l.compactIfNeeded(ctx, msgs)
|
||||
view := l.assembleContext(ctx, msgs, step)
|
||||
|
||||
@@ -102,8 +102,8 @@ func (l *Loop) Run(ctx context.Context, sessionID string, messages []model.Messa
|
||||
res.ToolCallsExecuted += batch.executed
|
||||
l.saveCheckpoint(ctx, sessionID, step, msgs, agent.StopReasonToolCalls)
|
||||
|
||||
// The critical-tool-loop safety net (or a terminating hook) stops the
|
||||
// round without waiting for a real final answer.
|
||||
// 关键工具循环安全网(或终止钩子)不等真正的最终答案
|
||||
// 就停止本轮。
|
||||
if batch.terminate {
|
||||
res.Text = criticalToolLoopTerminalReason
|
||||
res.StopReason = StopReasonToolLoopTerminated
|
||||
@@ -114,7 +114,7 @@ func (l *Loop) Run(ctx context.Context, sessionID string, messages []model.Messa
|
||||
}
|
||||
}
|
||||
|
||||
// Budget exhausted without a final answer.
|
||||
// 预算耗尽而无最终答案。
|
||||
res.Steps = l.maxRounds
|
||||
res.StopReason = StopReasonMaxRounds
|
||||
l.saveCheckpoint(ctx, sessionID, l.maxRounds, msgs, StopReasonMaxRounds)
|
||||
|
||||
Reference in new issue
Block a user