跳转到内容

improve-codebase-architecture

找出架构上的阻力,并提出加深机会:通过重构,把浅模块变成深模块。目标是让代码更容易测试,也更方便 AI 理解。

这条命令会参考项目的领域模型,并使用一套共同的设计词汇:

  • 调用 Skill 工具并指定「codebase-design」,获取架构词汇(模块、接口、深度、接缝、适配器、杠杆效应、局部性)和相关原则(删除测试、「接口就是测试面」、「一个适配器代表假设中的接缝,两个适配器才说明接缝真实存在」)。每条建议都要准确使用这些词。不要换成「组件」「服务」「API」或「边界」。
  • GLOSSARY.md 里的领域语言会为好的接缝命名;docs/adr/ 里的 ADR 记录了这条命令不该重新讨论的决策。

先定范围再扫描,遵循 YAGNI。 加深模块的价值在于让它将来的改动更容易,所以要多关注最近变动过的部分。先决定要看哪里:

  • 如果用户指定了方向,比如某个模块、子系统或痛点,就按这个方向看,跳过下面的推断步骤。
  • 否则,沿着提交历史往回看一段时间(git log --oneline),找出经常变动的热点文件和区域,优先检查这些路径。如果改动分散,没有明显热点,就扩大范围。

先读项目的领域术语表 GLOSSARY.md,以及你要处理区域里的 ADR。

然后启动一个子代理,并给它新的上下文窗口来检查代码库。不要死套固定规则,边探索边留意让你感到费劲的地方:

  • 理解一个概念时,是否要在许多小模块之间来回跳?
  • 模块是否很浅,以至于接口和实现一样复杂?
  • 是否只为测试而抽出了纯函数,但真正的 bug 却藏在调用方式里(缺少局部性)?
  • 紧密耦合的模块是否跨过接缝泄漏了细节?
  • 哪些部分没有测试,或难以通过现有接口测试?

对你怀疑很浅的模块做删除测试:删掉它会让复杂度集中起来,还是只会把复杂度挪到调用方?如果复杂度会集中起来,这就是值得关注的信号。

把独立的 HTML 文件写到操作系统的临时目录,这样仓库里不会留下文件。读取 $TMPDIR 来确定临时目录;如果没有,就用 /tmp(Windows 上用 %TEMP%)。文件名用 <tmpdir>/architecture-review-<timestamp>.html,让每次运行都生成新文件。在用户的设备上打开文件(Linux 用 xdg-open <path>,macOS 用 open <path>,Windows 用 start <path>),并告诉用户文件的绝对路径。

报告用 CDN 加载 Tailwind 做布局和样式,用 Mermaid 绘制适合用图表表达的关系、流程或时序。也要混合手工制作的 CSS 或 SVG 图示:关系适合画成图时用 Mermaid;结构剖面、模块规模对比、折叠过程等更适合手绘时,就用自定义的 div 或 SVG。不要什么都用 Mermaid,否则报告会显得千篇一律。每个候选项都要有改动前后的可视化图示。要重视图示。

每个候选项用一张卡片呈现,包含:

  • 涉及文件:涉及哪些文件或模块
  • 问题:当前架构为什么让人费劲
  • 方案:用通俗英语描述要改什么
  • 收益:从局部性和杠杆效应解释好处,以及测试会如何改善
  • 改动前后图示:并排展示自绘图示,说明模块为什么浅,以及加深后会是什么样
  • 推荐力度:用「强烈推荐」「值得探索」或「推测性」标签呈现

报告末尾要有首选建议一节:说明你会先处理哪个候选项,以及原因。

用 GLOSSARY.md 里的领域词汇,也用 /codebase-design 的架构词汇。 如果 GLOSSARY.md 定义了「Order」,就写「Order intake 模块」,不要写「FooBarHandler」,也不要写「Order service」。

与 ADR 冲突时:如果候选项和现有 ADR 矛盾,只有当实际阻力足以让人重新考虑该 ADR 时才提出。要在卡片里明确标出,例如:「这与 ADR-0007 冲突,但……让重新讨论它变得值得。」

请勿先提出接口。文件写完后,问用户:「你想进一步探索哪一个?」

用户选定一个候选项后,调用 Skill 工具并指定「grilling」,通过决策树和用户一起讨论约束、依赖、加深后模块的形状、接缝后面是什么,以及哪些测试会保留。

讨论逐渐确定决策时,副作用要直接跟着发生;调用 Skill 工具并指定「domain-modeling」,让领域模型保持最新:

  • 给加深后的模块命名时,用了 GLOSSARY.md 里没有的概念? 把这个词加进 GLOSSARY.md。如果文件不存在,就按需创建。
  • 讨论中把含糊的术语说清楚了? 当场更新 GLOSSARY.md。
  • 用户因为关键原因拒绝了候选项? 可以提议记录一份 ADR,并这样问:「要不要把这个决定记成 ADR,免得以后的架构审查又提起它?」只有未来探索者确实需要知道这个原因,才能避免重复提出同一个建议时,才提议记录。像「现在不值得做」这样暂时性的原因,或一看就明白的原因,不用记录。
  • 想比较加深模块的不同接口设计? 调用 Skill 工具并指定「codebase-design」,采用它的并行设计两次模式。

读者要自己输入 /improve-codebase-architecture 才会用到它。它在 Claude Code 插件里。

定期检查代码库的架构阻力,找出值得加深的模块,并生成 HTML 报告供你挑选。选定候选项后,再通过讨论梳理约束、接缝和测试。

Scan a codebase for deepening opportunities, present them as a visual HTML report, then grill through whichever one you pick.
英文 SKILL.md
---
name: improve-codebase-architecture
description: Scan a codebase for deepening opportunities, present them as a visual HTML report, then grill through whichever one you pick.
disable-model-invocation: true
---
# Improve Codebase Architecture
Surface architectural friction and propose **deepening opportunities**: refactors that turn shallow modules into deep ones. The aim is testability and AI-navigability.
This command is _informed_ by the project's domain model and built on a shared design vocabulary:
- Call the Skill tool with "codebase-design" for the architecture vocabulary (**module**, **interface**, **depth**, **seam**, **adapter**, **leverage**, **locality**) and its principles (the deletion test, "the interface is the test surface", "one adapter = hypothetical seam, two = real"). Use these terms exactly in every suggestion, and don't drift into "component," "service," "API," or "boundary."
- The domain language in `GLOSSARY.md` gives names to good seams; ADRs in `docs/adr/` record decisions this command should not re-litigate.
## Process
### 1. Explore
**Scope before you scan: YAGNI.** Deepening a module pays off by making future changes to it easier, so put extra weight on the parts of the codebase that have recently changed. Decide *where* to look before you look:
- If the user named a direction (a module, a subsystem, a pain point), take it, and skip the inference below.
- Otherwise, walk back a good stretch of the commit history (`git log --oneline`) to find the codebase's hot spots, the files and areas that keep coming up, and let those paths pull your attention first. If the changes are scattered with no clear hot spot, widen the net.
Read the project's domain glossary (`GLOSSARY.md`) and any ADRs in the area you're touching first.
Then spawn a sub-agent with a fresh context window to walk the codebase. Don't follow rigid heuristics; explore organically and note where you experience friction:
- Where does understanding one concept require bouncing between many small modules?
- Where are modules **shallow**, with an interface nearly as complex as the implementation?
- Where have pure functions been extracted just for testability, but the real bugs hide in how they're called (no **locality**)?
- Where do tightly-coupled modules leak across their seams?
- Which parts of the codebase are untested, or hard to test through their current interface?
Apply the **deletion test** to anything you suspect is shallow: would deleting it concentrate complexity, or just move it? A "yes, concentrates" is the signal you want.
### 2. Present candidates as an HTML report
Write a self-contained HTML file to the OS temp directory so nothing lands in the repo. Resolve the temp dir from `$TMPDIR`, falling back to `/tmp` (or `%TEMP%` on Windows), and write to `<tmpdir>/architecture-review-<timestamp>.html` so each run gets a fresh file. Open it for the user (`xdg-open <path>` on Linux, `open <path>` on macOS, `start <path>` on Windows) and tell them the absolute path.
The report uses **Tailwind via CDN** for layout and styling, and **Mermaid via CDN** for diagrams where a graph/flow/sequence reliably communicates the structure. Mix Mermaid with hand-crafted CSS/SVG visuals: use Mermaid when relationships are graph-shaped (call graphs, dependencies, sequences), and hand-built divs/SVG when you want something more editorial (mass diagrams, cross-sections, collapse animations). Each candidate gets a **before/after visualisation**. Be visual.
For each candidate, render a card with:
- **Files**: which files/modules are involved
- **Problem**: why the current architecture is causing friction
- **Solution**: plain English description of what would change
- **Benefits**: explained in terms of locality and leverage, and how tests would improve
- **Before / After diagram**: side-by-side, custom-drawn, illustrating the shallowness and the deepening
- **Recommendation strength**: one of `Strong`, `Worth exploring`, `Speculative`, rendered as a badge
End the report with a **Top recommendation** section: which candidate you'd tackle first and why.
**Use GLOSSARY.md vocabulary for the domain, and the `/codebase-design` vocabulary for the architecture.** If `GLOSSARY.md` defines "Order," talk about "the Order intake module," not "the FooBarHandler," and not "the Order service."
**ADR conflicts**: if a candidate contradicts an existing ADR, only surface it when the friction is real enough to warrant revisiting the ADR. Mark it clearly in the card (e.g. a warning callout: _"contradicts ADR-0007, but worth reopening because…"_). Don't list every theoretical refactor an ADR forbids.
See [HTML-REPORT.md](HTML-REPORT.md) for the full HTML scaffold, diagram patterns, and styling guidance.
Do NOT propose interfaces yet. After the file is written, ask the user: "Which of these would you like to explore?"
### 3. Grilling loop
Once the user picks a candidate, call the Skill tool with "grilling" to walk the decision tree with them: constraints, dependencies, the shape of the deepened module, what sits behind the seam, what tests survive.
Side effects happen inline as decisions crystallize; call the Skill tool with "domain-modeling" to keep the domain model current as you go:
- **Naming a deepened module after a concept not in `GLOSSARY.md`?** Add the term to `GLOSSARY.md`. Create the file lazily if it doesn't exist.
- **Sharpening a fuzzy term during the conversation?** Update `GLOSSARY.md` right there.
- **User rejects the candidate with a load-bearing reason?** Offer an ADR, framed as: _"Want me to record this as an ADR so future architecture reviews don't re-suggest it?"_ Only offer when the reason would actually be needed by a future explorer to avoid re-suggesting the same thing; skip ephemeral reasons ("not worth it right now") and self-evident ones.
- **Want to explore alternative interfaces for the deepened module?** Call the Skill tool with "codebase-design" and use its design-it-twice parallel sub-agent pattern.

非官方学习站,与 Matt Pocock 和 AI Hero 没有隶属关系。本站不提供可安装的中文技能包。

技能原文采用 MIT 许可。Copyright (c) 2026 Matt Pocock

鲁ICP备2025189036号