Skill Learning

improve-codebase-architecture

1. Bilingual SKILL.md

左右滑动可直接切换英文版与中文版;两种语言按段落自动对齐。英文中的蓝色虚线词语可点击查看解释。共标记 16 处。

入口 skill:improve-codebase-architecture左右滑动切换语言 · 段落位置自动对齐
# Improve Codebase Architecture
Surface architectural friction and propose ****: refactors that turn shallow modules into deep ones. The aim is testability and .
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 "component," "service," "API," or "boundary." - The domain language in `CONTEXT.md` gives names to good seams; ADRs in `docs/adr/` record decisions this command .
## Process
### 1. Explore
**: YAGNI.** Deepening a module pays off by making future changes to it easier, so 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, (`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, .
Read the project's domain glossary (`CONTEXT.md`) and any ADRs in the area you're touching first.
Then spawn a sub-agent to walk the codebase. Don't follow rigid heuristics; and note where you experience friction:
- Where does understanding one concept require ? - 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 , 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 . 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 . 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 CONTEXT.md vocabulary for the domain, and the `/codebase-design` vocabulary for the architecture.** If `CONTEXT.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 ; 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 `CONTEXT.md`?** Add the term to `CONTEXT.md`. Create the file lazily if it doesn't exist. - **Sharpening a fuzzy term during the conversation?** Update `CONTEXT.md` right there. - **User rejects the candidate with ?** 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.
# 改进代码库架构
找出架构摩擦,并提出**深化机会(deepening opportunities)**:通过重构,把浅模块变成深模块。目标是提升可测试性与 AI 可导航性。
这个命令以项目的领域模型为依据,并建立在一套共享的设计词汇之上:
- 调用 Skill 工具加载“codebase-design”,取得架构词汇(**module**、**interface**、**depth**、**seam**、**adapter**、**leverage**、**locality**)及其原则(删除测试、“接口就是测试面”、“一个适配器是假设接缝,两个才是真实接缝”)。每条建议都要准确使用这些术语,不要漂移到“component”“service”“API”或“boundary”。 - `CONTEXT.md` 中的领域语言为良好接缝命名;`docs/adr/` 中的 ADR 记录这个命令不应重新争论的决定。
## 流程
### 1. 探索
**先确定扫描范围:YAGNI。** 深化模块的收益,是让未来对它的改动更容易,因此要更重视最近频繁变化的代码区域。先决定去哪里看,再开始查看:
- 如果用户指明了方向(某个模块、子系统或痛点),就沿该方向推进,跳过下面的推断。 - 否则,向前回看一段足够长的提交历史(`git log --oneline`),找出代码库的热点,即反复出现的文件和区域,并优先让这些路径吸引你的注意。如果改动分散、没有明确热点,就扩大扫描范围。
先阅读项目的领域术语表(`CONTEXT.md`),以及当前所触及区域的所有 ADR。
然后启动一个子代理遍历代码库。不要套用僵硬的启发式规则;要自然探索,并记录你在哪些地方感受到摩擦:
- 理解一个概念时,是否需要在许多小模块之间来回跳转? - 是否存在**浅模块**,其接口几乎和实现一样复杂? - 是否有纯函数只是为了可测试性而被抽出,但真正的缺陷藏在调用方式中(缺少**局部性**)? - 紧密耦合的模块是否跨越接缝泄漏? - 代码库哪些部分未经测试,或难以通过当前接口测试?
对任何疑似浅模块应用**删除测试**:删掉它会让复杂性集中起来,还是只把复杂性挪到别处?你要寻找的是“会集中复杂性”这个信号。
### 2. 将候选方案呈现为 HTML 报告
把自包含 HTML 文件写入操作系统临时目录,确保仓库里不落下任何文件。临时目录从 `$TMPDIR` 解析,缺失时退回 `/tmp`(Windows 上为 `%TEMP%`);写入 `<tmpdir>/architecture-review-<timestamp>.html`,使每次运行都得到新文件。为用户打开它(Linux 用 `xdg-open <path>`,macOS 用 `open <path>`,Windows 用 `start <path>`),并告知绝对路径。
报告使用 **Tailwind CDN** 进行布局和样式设计,并在图、流程或时序能够可靠表达结构时使用 **Mermaid CDN**。把 Mermaid 与手工 CSS/SVG 视觉元素结合:关系呈图结构时(调用图、依赖图、时序图)使用 Mermaid;需要更具编辑设计感的表达(质量图、剖面图、折叠动画)时使用手工 div/SVG。每个候选方案都要有**重构前/后可视化**。要真正做到视觉化。
每个候选方案渲染一张卡片,包含:
- **文件**:涉及哪些文件/模块 - **问题**:当前架构为何造成摩擦 - **方案**:用直白英语说明会改变什么 - **收益**:用局部性和杠杆率解释,并说明测试会如何改善 - **重构前/后图**:并排、自定义绘制,展示浅层结构及其深化 - **推荐强度**:`Strong`、`Worth exploring`、`Speculative` 三选一,并渲染为徽章
报告结尾放置**首要推荐**部分:说明应先处理哪个候选方案,以及原因。
**领域表达使用 `CONTEXT.md` 词汇,架构表达使用 `/codebase-design` 词汇。** 如果 `CONTEXT.md` 定义了“Order”,就说“Order intake module”,不要说“FooBarHandler”,也不要说“Order service”。
**ADR 冲突**:如果候选方案与现有 ADR 矛盾,只有当真实摩擦严重到值得重新审视 ADR 时才提出。要在卡片中清楚标记,例如警告提示:“与 ADR-0007 矛盾,但值得重启讨论,因为……”。不要列出 ADR 所禁止的每一种理论重构。
完整 HTML 脚手架、图示模式和样式指导见 [HTML-REPORT.md](HTML-REPORT.md)。
此时**不要**提出接口方案。文件写好后询问用户:“你想深入探索其中哪一个?”
### 3. 持续追问循环
用户选中候选方案后,调用 Skill 工具加载“grilling”,沿决策树与用户逐层梳理:约束、依赖、深化后模块的形态、接缝后面放什么,以及哪些测试能够保留下来。
随着决策逐渐定型,副作用也要同步发生;调用 Skill 工具加载“domain-modeling”,在推进过程中保持领域模型最新:
- **深化后的模块以 `CONTEXT.md` 中没有的概念命名?** 把该术语加入 `CONTEXT.md`。若文件不存在,则按需创建。 - **在对话中把含糊术语变得更精确?** 当场更新 `CONTEXT.md`。 - **用户以一个承重性理由否决候选方案?** 可以这样提议 ADR:“要我把它记录成 ADR,避免未来的架构审查再次建议它吗?”只有当未来探索者确实需要这个理由来避免重复建议时才提议;临时性理由(“现在不值得”)和不言自明的理由则跳过。 - **想探索深化模块的其他接口?** 调用 Skill 工具加载“codebase-design”,并采用其“设计两次”的并行子代理模式。

Entry SHA-256: d1ac25511a936ff4250a48dbcefda363837d6bb9321b3cba73df99fa37270a75

2. Why This Skill Is Clever

一句话核心机制

它把架构改进组织成一个有边界的决策漏斗:用近期变化确定扫描范围 → 以代码阅读发现摩擦 → 用统一词汇可视化候选项 → 用户选择后再深化接口并同步领域知识。

触发与边界

直接来源: frontmatter 中的 disable-model-invocation: true 表明该技能面向显式用户调用;描述要求“扫描代码库、呈现 HTML 报告、再针对用户选中的项目持续追问”。因此它不是自动重构器,也不应在用户选择前直接设计接口或改代码。

解释: disable-model-invocation 的实际效果依赖具体 agent harness;迁移到不识别该字段的系统时,需要另行实现显式调用边界。

信息架构 / 执行顺序

  1. 优先接受用户指定的模块、子系统或痛点;没有指定时,才用提交历史寻找热点。
  2. 先读 CONTEXT.md 与相关 ADR,建立领域语言和既有决策约束。
  3. 让子代理在目标范围内自然探索,以“理解摩擦”而非僵硬指标收集候选项。
  4. 用删除测试判断抽象是否真的集中复杂性,而不是把复杂性搬家。
  5. 生成可视化报告,统一展示文件、问题、方案、收益、前后结构和推荐强度。
  6. 明确停在用户选择点,不提前发明接口。
  7. 用户选定后进入 grilling 决策树,并把新术语、术语修正和长期拒绝理由及时写回领域文档或 ADR。

最巧妙的设计点

1. 用变更热度约束架构野心

“Scope before you scan: YAGNI.”

直接来源: 技能先寻找最近经常变化的路径,而不是全库搜集理论上的“不完美”。这把架构收益绑定到未来变化概率,减少为了整洁而重构。

2. 把共享词汇当作输出协议

“Use these terms exactly in every suggestion”

直接来源: 它不只是参考 codebase-design,还禁止漂移到 component、service、API 等近义词。统一词汇使不同候选项可比较,也让后续 grilling 沿同一套概念继续推理。

3. 用删除测试区分深抽象与搬运代码

“would deleting it concentrate complexity, or just move it?”

直接来源: 删除测试迫使审查者回答一个反事实问题:这个模块是否真正隐藏了复杂性。如果删除只让代码换个位置,所谓抽象就没有提供足够杠杆率。

4. 先展示决策空间,再设计接口

“Do NOT propose interfaces yet.”

直接来源: 报告阶段只负责发现和排序,接口设计必须等用户选中候选项后再开始。这一停止条件避免在错误问题上投入高成本设计,也保留了用户对方向的控制权。

5. 为不同关系选择不同视觉语法

“use Mermaid when relationships are graph-shaped”

直接来源: 技能没有把“可视化”简化成统一流程图,而是按信息形状选择 Mermaid 或手工 CSS/SVG。图结构用图语言,质量、剖面和折叠过程用编辑型视觉,媒介服务于论证。

6. 让被否决的方案也产生长期知识

“User rejects the candidate with a load-bearing reason?”

直接来源: 只有具有长期支撑作用的否决理由才值得提议 ADR;“现在不值得”等短期理由被排除。这样既避免未来审查反复提出同一坏建议,也防止 ADR 被临时偏好污染。

防失败机制与 trade-off

明确不变量: 用户指定方向时跳过热点推断;先读领域术语和相关 ADR;报告文件不得落入仓库;每个候选项必须有前后可视化与推荐强度;用户选择前不得提出接口;只有真实且承重的 ADR 冲突才值得重开。

解释与权衡: 近期提交是价值密度高的范围信号,但可能忽略稳定却高风险的遗留区域;自然探索比固定规则更容易捕捉认知摩擦,却使结果更依赖审查者能力;CDN 能快速生成丰富报告,但在离线或严格内容安全策略环境中需要改成本地资源;调用 Skill tool、启动子代理、打开本地文件等步骤具有明显的 harness 和操作系统假设。

可迁移原则

今日实践题

假设一个目录最近改动频繁,但其中每个小模块单独看都很“干净”:你会怎样用删除测试和局部性,判断应把哪些模块合并成一个更深的模块?

Evidence note

核心流程、边界、全部引文和不变量均来自本次选中的 SKILL.md。对变更概率、输出协议、价值密度和知识信噪比的说明属于设计解释;本文没有把 codebase-design、grilling、domain-modeling 或 HTML-REPORT.md 的内容冒充为选中原文。

Source & provenance
Repository path
skills/engineering/improve-codebase-architecture/SKILL.md
Generated
2026-09-04 08:23 Asia/Shanghai
Upstream commit
6654f6b60cd9d5be8b54c6fafe44346dabeb3b76
Source
View on GitHub