Skip to content

refactor(app):优化 App 层装配逻辑、消除过度设计并修复子进程残留#264

Merged
pionxe merged 9 commits into
1024XEngineer:mainfrom
phantom5099:main
Apr 14, 2026
Merged

refactor(app):优化 App 层装配逻辑、消除过度设计并修复子进程残留#264
pionxe merged 9 commits into
1024XEngineer:mainfrom
phantom5099:main

Conversation

@phantom5099

Copy link
Copy Markdown
Collaborator

变更说明

本 PR 主要聚焦于 internal/app 层及相关核心模块的代码重构与生命周期管理优化。通过向上透传清理钩子解决了 MCP 僵尸子进程残留的问题,提取了纯函数以简化大模型文本生成逻辑,并极其严格地审查了项目结构,清除了“过度设计”的碎片化胶水文件,使管线装配回归极简。

主要改动

  • 引入优雅停机清理钩子:修改了 app.RuntimeBundle 结构,透传了 Close func() error 方法。在 internal/tools/mcp/registry.go 中新增资源释放的 Close 方法,并由 internal/cli/root.go 顶层进行 defer 捕获调用。
  • 抽取文本生成纯函数:新增 internal/provider/generate.go,将冗长的流式转非流式拼接逻辑封装在独立的 GenerateText 纯函数中,彻底消除样板代码。
  • 消除过度设计 (反碎片化)
    • 彻底删除了物理文件 internal/app/memo_adapter.go 及其配套的测试文件。
    • 将轻量级的适配器逻辑(runtimeMemoExtractorFunctextGenAdapter)以闭包类型的形式直接内联回 internal/app/bootstrap.go
    • 将对应的适配器测试用例无缝合并至 internal/app/bootstrap_test.go 中,消除测试碎片。

预期收益

  • 提升稳定性与健壮性:有效处理资源的优雅停机,彻底解决主程序退出后可能残留子进程引发的资源占用及系统隐患。
  • 降低心智负担:移除了强行拆分的碎片化文件,代码内聚度大幅提升。后续开发者在阅读 bootstrap.go 时即可一览无遗地理解全部装配逻辑。
  • 提高可维护性:重构后的管线代码更加符合 Go 语言惯用轻巧的设计风格,文本生成能力被单独抽象,测试用例集中管理,使项目整体架构保持了 MVP 阶段所需的轻量与敏捷。

@codecov

codecov Bot commented Apr 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

fennoai[bot]

This comment was marked as outdated.

@phantom5099

Copy link
Copy Markdown
Collaborator Author

需要补全测试覆盖率

@phantom5099

Copy link
Copy Markdown
Collaborator Author

/simplify

@fennoai

fennoai Bot commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

已按你的要求完成:先处理历史未解决 review 项,再做简化复查,并已提交推送到当前 PR 分支。
提交:9a8a744(已推送到 phantom5099:main

处理结果

  1. 修复 BuildRuntime 失败路径资源泄漏
  • 文件:internal/app/bootstrap.go
  • 变更:在 buildToolRegistry 成功后增加受控 defer 清理,若后续 buildToolManager 失败会执行 toolsCleanup,成功返回前取消清理。
  1. 修复 NewProgram 初始化失败路径资源泄漏
  • 文件:internal/app/bootstrap.go
  • 变更:tui.NewWithMemo 失败时主动调用 bundle.Close(),防止 MCP 子进程残留。
  1. 恢复 GenerateText 对未知流事件的显式报错
  • 文件:internal/provider/generate.go
  • 变更:补回 default 分支,遇到未识别 StreamEventType 返回错误,不再静默忽略。
  1. 补齐回归测试
  • 文件:internal/app/bootstrap_test.go
  • 新增:
    • TestBuildRuntimeCleansResourcesWhenToolManagerBuildFails
    • TestNewProgramCleansResourcesWhenTUIBuildFails
  • 文件:internal/provider/generate_test.go
  • 新增:
    • TestGenerateTextRejectsUnexpectedEvent
  1. 简化复查
  • 已调用子代理进行“code simplifier”复查(仓库无同名内置角色,使用等价 code-quality-reviewer)。
  • 结论:当前修复已是最小必要复杂度,未再做额外改动。

验证

  • 已执行:go test ./...
  • 结果:全部通过

View job run

- ensure BuildRuntime cleans MCP resources on tool-manager init failure
- ensure NewProgram closes runtime bundle when TUI init fails
- restore GenerateText error on unexpected stream events
- add regression tests for cleanup and stream-event handling

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com>
@phantom5099

Copy link
Copy Markdown
Collaborator Author

/review -codex
你只允许回顾这轮对之前review的修复是否成功,并且看看修复后有没有又出现职责不清、语义不清的屎山代码出现

fennoai[bot]

This comment was marked as outdated.

@phantom5099

Copy link
Copy Markdown
Collaborator Author

/simplify

@fennoai

fennoai Bot commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

处理结果

已按触发指令完成:先检查历史未解决 review 项并修复,再做简化复查,最后提交推送。

  1. 已修复的未解决项
  • internal/provider/generate.go
    • 修复 GenerateTextp.Generate 返回错误时仍可能返回部分文本的问题;现在错误时固定返回空文本。
  • internal/cli/root.go
    • 修复 cleanup 错误被静默丢弃的问题。
    • 保持 cleanupdefer 中执行(避免 panic 路径漏清理),并在有运行错误时使用 errors.Join 合并 program.Run()cleanup 错误。
  1. 新增/更新测试
  • internal/provider/generate_test.go
    • 新增:TestGenerateTextReturnsEmptyTextWhenProviderErrorsAfterStreaming
  • internal/cli/root_test.go
    • 新增:TestDefaultRootProgramLauncherReturnsCleanupErrorWhenRunSucceeds
    • 新增:TestDefaultRootProgramLauncherJoinsRunAndCleanupErrors
  1. 简化复查
  • 仓库无名为 code-simplifier 的内置代理,使用等价 code-quality-reviewer 复查。
  • 根据复查建议,已将 cleanup 语义收敛为“defer + 错误合并”的更简洁且更稳健实现。
  1. 验证
  • 已执行:go test ./...
  • 结果:全部通过。
  1. 提交与推送

未完成项:无。

View job run

Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com>
@phantom5099

Copy link
Copy Markdown
Collaborator Author

/code -codex 给我提升测试覆盖率直到100%

@fennoai

fennoai Bot commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

结果

已按你的要求补测试并推送到当前 PR 分支,覆盖了这轮改动里未覆盖的关键分支。

本次新增测试点:

  • internal/app/bootstrap_test.go
    • newMemoExtractorAdapterscheduler=nil、provider 解析失败、正常调度+提取、factory.Build 失败
    • runtimeMemoExtractorFunc.Schedule
    • textGenAdapter.Generate
  • internal/provider/generate_test.go
    • text_delta payload 为 nil
    • message_done payload 为 nil
  • internal/tools/mcp/registry_test.go
    • (*Registry)(nil).Close() 边界分支

验证

  • 已执行:go test ./... -coverprofile=coverage.out(通过)
  • 关键函数覆盖率:
    • internal/app/bootstrap.go:newMemoExtractorAdapter = 100%
    • internal/provider/generate.go:GenerateText = 100%
    • internal/tools/mcp/registry.go:Close = 100%

说明:仓库整体 total 仍不是 100%(当前约 84.9%),因为存在大量历史包/语句未覆盖;本次已把你这轮改动相关缺口补齐并推送。

提交与推送

View job run

xgopilot and others added 3 commits April 13, 2026 15:22
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com>
fix(review): preserve cleanup semantics and error contracts
@phantom5099

Copy link
Copy Markdown
Collaborator Author

/review -codex

fennoai[bot]

This comment was marked as outdated.

@phantom5099

Copy link
Copy Markdown
Collaborator Author

/simplify

@fennoai

fennoai Bot commented Apr 13, 2026

Copy link
Copy Markdown
Contributor

完成结果

已按触发指令完成:先处理历史未解决 review 项,再进行 code-simplifier 复查,并已提交推送。

本次改动

  1. internal/app/bootstrap.go
  • 更新 NewProgram 注释,明确返回的 cleanup 需要在退出时调用。
  • 采纳简化建议:buildToolRegistry 在无 MCP 时直接返回 nil cleanup,移除 no-op 闭包。
  1. internal/app/bootstrap_test.go
  • 新增回归测试 TestNewMemoExtractorAdapterKeepsScheduledConfigSnapshot:验证 Schedule 后即使 config.Manager 发生 provider/model 漂移,已排队 extractor 仍使用调度时快照。

验证

  • 已执行:go test ./...
  • 结果:全部通过。

提交与推送

未完成项:无。

View job run

xgopilot and others added 2 commits April 13, 2026 15:41
Generated with [codeagent](https://github.com/qbox/codeagent)
Co-authored-by: phantom5099 <245659304+phantom5099@users.noreply.github.com>
test(app): close unresolved review follow-ups
@pionxe

pionxe commented Apr 14, 2026

Copy link
Copy Markdown
Collaborator

/review

@fennoai fennoai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

整体审查完成(代码质量、性能、安全、文档一致性),未发现需要修改的问题。以下备注仅用于记录审查覆盖点。

Comment thread internal/cli/root.go
// defaultRootProgramLauncher 负责在默认根命令路径下启动 TUI。
func defaultRootProgramLauncher(ctx context.Context, opts app.BootstrapOptions) error {
program, err := newRootProgram(ctx, opts)
func defaultRootProgramLauncher(ctx context.Context, opts app.BootstrapOptions) (err error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已重点审阅 defaultRootProgramLauncher 的运行错误与清理错误合并路径(errors.Join),语义与测试覆盖匹配,未发现问题。

@pionxe pionxe merged commit 0e3b410 into 1024XEngineer:main Apr 14, 2026
2 checks passed
@fennoai fennoai Bot mentioned this pull request Apr 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants