refactor(runtime):优化runtime内部职责不清问题#244
Conversation
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
|
/simplify |
处理结果已按要求完成:先检查历史评论中的未解决 review,再修复值得修复的问题,并调用 code-simplifier agent 做了简化审查。 本次实际修复内容:
验证:
提交与推送:
未执行项:
|
Keep the stream consumer alive until the producer closes the channel so provider sends cannot block when context is canceled. Also add a regression test that reproduces cancellation with post-cancel event bursts. Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com>
fix(runtime): drain streaming events after cancellation
|
/code -codex 修复冲突,注意新的修改不能导致runtime内部职责不清 |
处理结果已按“修复冲突,且不让 runtime 职责变混乱”的要求完成处理,并推送到当前 PR 分支。 本次修改
验证
提交与推送
|
Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com>
fix(runtime): preserve memo extractor hook after refactor
|
/code -codex 冲突还是存在 |
处理结果已确认你说的情况:PR #244 当前在 GitHub 侧仍是冲突状态。
根因
未完成项本次没有直接推送冲突解决提交。原因是当前执行器限制了分支合并类操作( 你可以直接执行的修复步骤git fetch origin
git merge origin/main
# 手工解决 internal/runtime/runtime.go 冲突后
go test ./internal/runtime/...
git add internal/runtime/runtime.go
git commit -m "merge: resolve runtime.go conflict with origin/main"
git push如果你希望,我下一轮可以按你给定的合并结果策略(例如“完全保留 runtime 拆分结构,只吸收 memo 接口变更”)直接产出 |
# Conflicts: # internal/runtime/runtime.go
|
/review -codex |
|
|
PR中能否添加上下述内容,让协作者能够更快理解?
2. 一次 Run 的完整时序(主链路)
3. 为什么拆这么多文件(每个文件具体职责)
4. 关键关联关系(跨文件怎么串)
5. TUI 怎么接 runtime 事件
|
|
/code -codex 修复测试错误以及comment里 sessionLocks 无回收,长期运行会增长 |
处理结果已完成你在触发评论里指定的两项修复:测试错误修复 + 关键修改
验证
提交与推送
未执行项
|
Generated with [codeagent](https://github.com/qbox/codeagent) Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com>
fix(runtime): recycle session locks and harden nil maps
|
/review -codex |
| // Run 执行一次完整的 ReAct 闭环:保存用户输入、驱动模型、执行工具并发出事件。 | ||
| // 已有会话会先加锁再加载/更新,确保同一会话并发 Run 不会出现状态覆盖; | ||
| // 新会话在创建后再绑定会话锁,不同会话可并行执行。 | ||
| func (s *Service) Run(ctx context.Context, input UserInput) error { |
There was a problem hiding this comment.
已覆盖审阅:本次改动在主链路编排、权限审批闭环与流式收敛上未发现需要修复的问题。
变更说明
本次 PR 主要围绕 Runtime 主链路进行重构与增强,目标是提升流式事件处理稳定性、权限审批可控性,以及上下文压缩决策的一致性。
改动覆盖
runtime、provider/streaming、context、config、tui、gateway与对应文档/测试,属于一次跨模块但边界清晰的能力升级。主要改动
Runtime 运行编排重构
run/state/session_mutation/toolexec/provider_stream/errors)。runState、turnSnapshot、providerTurnResult等运行期结构,减少Run()内部复杂度。统一 Provider 流式事件收敛
internal/provider/streaming/accumulator.go与handler.go。text_delta / tool_call_start / tool_call_delta / message_done。权限审批链路升级
internal/runtime/approval(broker+decision)。ResolvePermission入口。Context Builder 职责增强
BuildResult新增AutoCompactSuggested,由 context 基于 token 阈值给出自动压缩建议。AGENTS.md发现与缓存(基于路径/mtime/size),并完善预算裁剪策略。配置与文档同步
context.auto_compact、context.compact.micro_compact_disabled等配置语义。TUI/Gateway 协议对齐
resolve_permission)。测试补强
预期收益
模块详情
Service 结构和对外接口在 runtime.go (line 58)。
入口 Run 在 run.go (line 23)。
Run 做的事是编排,不做细节实现:校验输入、加载会话、循环推理、触发工具、发事件、结束清理。
2. 一次 Run 的完整时序(主链路)
校验输入非空、注册取消句柄(run.go (line 24), runtime.go (line 210))。
加载/创建会话并解析 workdir(runtime.go (line 145))。
获取会话锁,确保同会话串行(runtime.go (line 235), run.go (line 42))。
追加 user 消息并保存(session_mutation.go (line 11))。
每个 turn 先 prepareTurnSnapshot:构造上下文、判定 auto compact、列可用 tools、冻结 provider/model/request(run.go (line 104))。
调 provider(带 runtime 级重试),流式事件转成 assistant message + usage(run.go (line 166), provider_stream.go (line 20))。
写 assistant 消息与 token 累积(session_mutation.go (line 26), run.go (line 225))。
若无 tool calls:发 agent_done 并异步触发 memo 提取(run.go (line 91), memo.go (line 9))。
若有 tool calls:逐个执行并回写 tool message,再进入下一轮(toolexec.go (line 11))。
3. 为什么拆这么多文件(每个文件具体职责)
runtime.go:Service 装配、生命周期、会话加载、事件发送、锁管理。
run.go:ReAct 主循环(turn 编排、重试、auto/reactive compact 触发点)。
state.go:runState/turnSnapshot/providerTurnResult,把运行态从 Service 全局字段剥离。
session_mutation.go:所有“追加消息+保存”的写路径,集中处理持久化。
toolexec.go:assistant tool calls 的执行序列、错误填充、事件时序。
permission.go:权限 ask/deny 处理、事件映射、决策落盘和重试执行。
approval/broker.go:待审批请求生命周期(Open/Resolve/Close),避免 runtime 本体堆并发细节。
provider_stream.go:一次流式生成的通用收敛逻辑(message、usage、错误)。
compact.go:手动/自动/reactive compact 的执行与事件、严格/尽力而为策略。
compact_generator.go:compact 摘要生成(走 provider 但禁止 tool call)。
errors.go:错误归一化(run_canceled vs error)和 provider retry backoff。
memo.go:run 结束后的异步 memo hook。
events.go:runtime 对外事件契约(TUI/服务层依赖它)。
4. 关键关联关系(跨文件怎么串)
run.go 调 prepareTurnSnapshot 时依赖 context.Builder.Build 的 AutoCompactSuggested(types.go (line 23), builder.go (line 76))。
run.go 和 compact_generator.go 都复用 generateStreamingMessage(避免两套流式解析)。
generateStreamingMessage 又复用 internal/provider/streaming 的 Accumulator + HandleEvent(accumulator.go (line 11), handler.go (line 16))。
toolexec.go -> permission.go:工具执行先走统一权限闭环,再决定是否执行/重试。
permission.go -> approval/broker.go:runtime 发 permission_request,UI 回 ResolvePermission,broker 唤醒等待协程。
compact.go 被三处调用:Runtime.Compact(手动 strict)、auto compact(best effort)、reactive compact(best effort)。
5. TUI 怎么接 runtime 事件
事件总线定义在 events.go (line 49)。
TUI 通过注册表分发事件(update.go (line 791), update.go (line 812))。
权限请求/决议分别处理在 update.go (line 1017), update.go (line 1057)。
用户在审批面板选择 allow_once/allow_session/reject(permission_prompt.go (line 21)),再调用 runResolvePermission 回 runtime(update.go (line 1773))。