Skill Learning

improve-codebase-architecture

1. Bilingual SKILL.md

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

入口 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 AI-navigability.
This command is _informed_ by the project's domain model and :
- Call the Skill tool with "codebase-design" for the architecture vocabulary (**module**, **interface**, **depth**, **seam**, **adapter**, **leverage**, **locality**) and its principles (the , "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 .
## Process
### 1. Explore
**Scope before you scan: 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, walk back a good stretch of the commit history (`git log --oneline`) to find the codebase's , 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 (`GLOSSARY.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 **** 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 . 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 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 ; 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 ?** 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"。 - `GLOSSARY.md` 中的领域语言为优良接缝命名;`docs/adr/` 中的 ADR 记录此命令不应重新争论的决定。
## 流程
### 1. 探索
**扫描前先限定范围:YAGNI。** 深化模块的收益来自让未来修改更容易,因此应对近期发生过变化的代码区域给予更高权重。先决定去哪里看,再开始查看:
- 如果用户指定了方向(模块、子系统或痛点),就采用它,并跳过下面的推断。 - 否则,回溯足够长的一段提交历史(`git log --oneline`),找出代码库的热点,即反复出现的文件和区域,让这些路径优先吸引注意力。若变更分散且没有清晰热点,就扩大搜索范围。
先阅读项目的领域术语表(`GLOSSARY.md`),以及当前涉及区域中的所有 ADR。
然后启动子代理遍历代码库。不要遵循僵硬的启发式规则;应根据线索自然探索,并记录你感受到摩擦的位置:
- 理解一个概念是否需要在许多小模块之间来回跳转? - 是否存在**浅模块**,其接口几乎和实现一样复杂? - 是否有纯函数仅为了可测试性而被抽取,但真正的缺陷隐藏在调用方式中(没有 **locality**)? - 紧耦合模块是否跨越接缝泄漏? - 哪些代码尚未测试,或难以通过当前接口测试?
对任何疑似浅模块应用**删除测试**:删除它会集中复杂度,还是只会把复杂度搬走?你要找的信号是“是的,会集中”。
### 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。每个候选都要有**前后对比可视化**。务必视觉化。
对每个候选项,渲染一张包含以下内容的卡片:
- **文件**:涉及哪些文件/模块 - **问题**:当前架构为何造成摩擦 - **方案**:用浅显英语说明将发生什么变化 - **收益**:用 locality 和 leverage 解释,并说明测试会如何改善 - **前 / 后图**:并排、自定义绘制,用于展示浅薄状态和深化结果 - **推荐强度**:`Strong`、`Worth exploring`、`Speculative` 之一,并渲染成徽章
报告末尾加入**首选建议**部分:说明你会优先处理哪个候选项以及原因。
**领域使用 GLOSSARY.md 词汇,架构使用 `/codebase-design` 词汇。** 如果 `GLOSSARY.md` 定义了 "Order",就说 "the Order intake module",而不要说 "the FooBarHandler",也不要说 "the Order service"。
**ADR 冲突**:如果候选项与现有 ADR 冲突,只有当摩擦真实到值得重新审视 ADR 时才提出。应在卡片中明确标记(例如警告框:_"contradicts ADR-0007, but worth reopening because…"_)。不要列出 ADR 所禁止的每一种理论重构。
完整 HTML 脚手架、图示模式与样式指导见 [HTML-REPORT.md](HTML-REPORT.md)。
现在不要提出接口。文件写好后,询问用户:"Which of these would you like to explore?"
### 3. 追问循环
用户选择候选项后,使用 Skill 工具调用 "grilling",与用户一起走完决策树:约束、依赖、深化后模块的形态、接缝后放什么、哪些测试得以保留。
随着决定逐渐成形,副作用就地发生;使用 Skill 工具调用 "domain-modeling",在过程中持续更新领域模型:
- **要用 `GLOSSARY.md` 中不存在的概念命名深化模块?** 把该术语加入 `GLOSSARY.md`。如果文件不存在,则延迟到此时创建。 - **对话中澄清了一个模糊术语?** 当场更新 `GLOSSARY.md`。 - **用户以一个关键支撑理由拒绝候选项?** 主动提出记录 ADR,并这样表述:_"Want me to record this as an ADR so future architecture reviews don't re-suggest it?"_ 只有未来探索者确实需要这个理由来避免重复建议时才提出;短期理由("not worth it right now")和不言自明的理由应跳过。 - **想为深化后的模块探索替代接口?** 使用 Skill 工具调用 "codebase-design",并采用其 design-it-twice 并行子代理模式。

Entry SHA-256: 552240a5ab5cec6b67c15dd1ad6e9f6962b1bca6ce870059a8c2713760c20392

2. Why This Skill Is Clever

一句话核心机制

先用近期变更和领域词汇限定扫描范围,再把候选架构深化方案可视化,最后只对用户选中的一个候选进入决策访谈。

触发与边界

直接来源: frontmatter 中的 disable-model-invocation: true 表明这是用户显式调用的技能。它不是自动重构器:在报告完成前明确要求 Do NOT propose interfaces yet,先展示候选并让用户选择。

支持语境: 配套文档把它定位为周期性架构体检,而不是主构建链中的一步;实现工作应在后续独立流程中完成。

信息架构与执行顺序

  1. 缩小搜索域。 用户给方向就直接采用;否则从 git log --oneline 找持续变化的 hot spots。
  2. 加载既有语义。 先读 GLOSSARY.md 与相关 ADR,避免发明新名词或无证据重审旧决定。
  3. 开放式扫描。 子代理寻找浅模块、局部性不足、接缝泄漏和难测试区域,再用 deletion test 过滤。
  4. 先比较、后承诺。 每个候选都用同一张卡片结构和前后图示呈现,并给出推荐强度与首选项。
  5. 只深化一个选择。 用户选定后才调用 grilling;随着决定清晰,才更新术语表、提出 ADR 或调用 codebase-design 比较接口。

最巧妙的设计点

1. 先按未来收益分配注意力

“Scope before you scan: YAGNI.”

直接来源: 技能把近期频繁变化区域置于更高权重。其机制不是“哪里最丑就改哪里”,而是“哪里未来仍会变化,就优先降低那里的修改成本”。这让架构改进有现实回报。

2. 用严格词汇约束输出漂移

“Use these terms exactly in every suggestion”

直接来源: 建议必须使用 module、interface、depth、seam、adapter、leverage、locality,并结合项目自己的领域名词。这样不同候选可以在同一概念坐标系下比较,也减少“组件 / 服务 / API / 边界”互相替换造成的含义漂移。

3. 删除测试过滤伪抽象

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

直接来源: 只有删除后能让复杂度集中到更小接口后的模块,才是值得深化的信号。这个反事实测试比按文件大小或函数数量打分更接近抽象的真实价值。

4. 报告是可逆决策门,而不是变更许可

“Which of these would you like to explore?”

直接来源: 先把多个候选、证据、收益和推荐强度放入临时 HTML,再停下来等用户选择。探索阶段保持只读,避免代理在第一个想法上过早投入,更不会把“发现问题”误当成“获准改代码”。

5. 把长期拒绝理由写回系统记忆

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

直接来源: 只有未来评审仍需知道的拒绝理由才值得变成 ADR;“现在不划算”等短期理由被明确排除。这个门槛能让知识持久化而不把 ADR 变成临时待办列表。

防失败机制与 trade-off

显式不变量: 候选需通过删除测试;报告写入系统临时目录;每张卡必须包含文件、问题、方案、收益、前后图和推荐强度;在用户选择前不设计接口;ADR 冲突必须清楚标示。

支持语境: 配套文档指出 CDN 依赖在离线或严格 SRI 环境中会失效,某些 harness 也没有同名子代理工具。这说明“自包含 HTML”与 Tailwind/Mermaid CDN 的组合存在可移植性张力。

解释: 开放式探索比刚性启发式更能发现真实摩擦,但输出受模型能力影响,也倾向于总能找出候选。Speculative 徽章和用户选择门用于抵消这种“必须产出发现”的偏差。

可迁移原则

今日实践题

你的代码库里有一个近期频繁改动的浅模块。若删除它,复杂度只是散回多个调用方。它通过删除测试了吗?你会继续保留现状,还是提出怎样的“更小接口、更多隐藏复杂度”候选?

证据说明

本页对流程、约束与引用的描述来自所选 SKILL.md;周期性维护定位、CDN 与 harness 风险来自配套文档;关于注意力分配、可逆决策门和偏差控制的结论属于设计解释。

Source & provenance
Repository path
skills/engineering/improve-codebase-architecture/SKILL.md
Generated
2026-10-12 08:24 Asia/Shanghai
Upstream commit
49dd158d1076134a641b33efb035946536778336
Source
View on GitHub