improve-codebase-architecture

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Improve Codebase Architecture

改进代码库架构

Find code that is hard to follow or change because callers must coordinate details that belong together. Propose modules that hide those details and make the behavior easier to test.
When the user has already selected a candidate or concrete target change, inspect that target and proceed directly to exploring it. Do not produce another candidate menu or ask them to select it again. A named subsystem may instead limit discovery when the user is still asking which change would help.
This command is informed by the project's domain model and built on a shared design vocabulary:
  • Consult the
    codebase-design
    skill when available for deep-module principles, caller contracts, and seam justification. Use its architectural distinctions alongside established product and repository vocabulary. Evaluate what an interface hides and what callers gain; adapter count alone does not justify or rule out a seam.
  • The project's glossary gives names to good seams; its ADRs record decisions this command should not re-litigate. Follow the existing documentation locations rather than assuming
    CONTEXT.md
    and
    docs/adr/
    .
Locate companion skills by name in the available skill catalog, using its supplied locations or loading mechanism. If one is unavailable, continue with the procedure here; do not infer its installation path or fetch external guidance.
找出因调用方必须协调本应归属在一起的细节而难以理解或修改的代码。提出能够隐藏这些细节、让行为更易于测试的模块方案。
如果用户已经选定了一个候选方案或具体的目标变更,请检查该目标并直接进入探索阶段。不要生成另一个候选菜单或要求用户再次选择。当用户仍在询问哪种变更会有帮助时,指定子系统可能会限制探索范围。
此命令以项目的领域模型为依据,并建立在共享设计词汇表之上:
  • 如有可用的
    codebase-design
    skill,请参考其深模块原则、调用方契约和接缝合理性论证。将其架构区分与已确立的产品和代码库词汇结合使用。评估接口隐藏了什么、调用方获得了什么;仅适配器数量不足以证明接缝的合理性,也不足以排除接缝的必要性。
  • 项目的术语表为合理的接缝命名;其ADR记录了此命令不应重新争论的决策。请遵循现有的文档位置,而不是假设存在
    CONTEXT.md
    和
    docs/adr/
    。
在可用的skill目录中按名称查找配套skill,使用其提供的位置或加载机制。如果某个skill不可用,继续执行此处的流程;不要推断其安装路径或获取外部指导。

Process

流程

1. Explore

1. 探索

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, use recent commit history (
    git log --oneline
    ) to select an initial area where repeated change suggests maintenance cost. Broaden that area only when the evidence or requested coverage warrants it.
Read the project's existing domain glossary and any ADRs in the area you're touching first.
Explore the scoped area directly. Delegate a bounded, read-only slice when delegation is available and an independent investigation can improve evidence or save time alongside useful local work. Give the agent the question, target modules or paths, known requirements and constraints, and the expected result: concrete code locations or caller traces, the maintenance cost they demonstrate, a plausible improvement, and any uncertainty. If delegation is unavailable or adds little value, continue locally.
Look for friction supported by the code:
  • 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?
Before recommending removal of a module, check whether that removes unnecessary forwarding or makes callers handle its work themselves. Verify delegated findings against the code before presenting them. Stop when you can explain a concrete cost and a useful improvement in the requested area, or identify the facts still missing. No worthwhile candidate is a valid result.
扫描前先确定范围:YAGNI。 深化模块的价值在于让未来的修改更轻松,因此要重点关注代码库中最近变更的部分。在开始查看之前先决定要查看哪里:
  • 如果用户指定了方向(某个模块、某个子系统、某个痛点),直接采用该方向,跳过下面的推断。
  • 否则,使用最近的提交历史(
    git log --oneline
    )选择初始区域——频繁变更意味着维护成本较高。仅当证据或要求的覆盖范围有必要时,才扩大该区域。
首先阅读项目现有的领域术语表,以及你所涉及区域的所有ADR。
直接探索划定范围内的区域。当委托功能可用,且独立调查能够在有用的本地工作之外提升证据质量或节省时间时,可以委托一个有边界的只读任务。向Agent提供问题、目标模块或路径、已知需求和约束,以及预期结果:具体的代码位置或调用方轨迹、它们所体现的维护成本、合理的改进方案,以及任何不确定性。如果委托功能不可用或几乎没有价值,则在本地继续。
寻找代码中存在的摩擦点:
  • 理解一个概念需要在多个小模块之间来回跳转的地方?
  • 哪些模块是浅层的,其接口的复杂度几乎与实现相当?
  • 哪些地方仅仅为了可测试性而提取了纯函数,但真正的bug隐藏在它们的调用方式中(缺乏局部性)?
  • 紧耦合的模块在哪些地方越过接缝泄漏?
  • 代码库的哪些部分未经测试,或难以通过其当前接口进行测试?
在建议移除某个模块之前,请检查这是移除了不必要的转发,还是让调用方自己处理其工作。在呈现委托调查结果之前,先对照代码进行验证。当你能够解释所请求区域的具体成本和有用的改进,或者确定仍缺失的事实时,就可以停止。没有有价值的候选方案也是一种有效结果。

2. Present the findings in a useful format

2. 以实用的格式呈现发现结果

Follow the requested format and the environment's actual rendering capabilities. For a compact review, use concise prose with an inline diagram when the host can render it. For a portable report or a comparison that benefits from a separate artifact, use HTML. If visual rendering is unavailable, explain the relationships in text and state the limitation rather than blocking the analysis.
Use visuals where they clarify ownership, dependencies, caller effort, or the proposed change. A before/after diagram is useful when the structural difference matters; do not invent several candidates or diagram types merely to fill a report. Use the host's supported Mermaid rendering, inline SVG, or simple HTML/CSS as appropriate.
When writing an HTML report, use inline CSS and SVG so it opens without network dependencies. Save it to the requested location, otherwise to a fresh file in the OS temp directory. Use an available artifact preview to show it, or provide its absolute path and a link when opening is unavailable. Do not require a particular OS command or external browser.
For each candidate, provide the evidence needed to judge it:
  • Evidence: relevant files, symbols, and caller traces
  • Problem: the concrete friction and its consequence
  • Solution: plain English description of what would change
  • Benefits: concrete gains for callers and maintainers, including testability when affected
  • Structural comparison: a diagram or concise explanation of what callers and maintainers would need to know afterward
  • Recommendation strength and uncertainty: how strongly the evidence supports the change and what remains unverified
Recommend the strongest candidate when discovery was requested. For a selected target, explain whether the evidence supports that change and proceed with its design question. Use concise explanatory prose wherever a diagram alone cannot communicate the contract or tradeoff.
Use the project's glossary vocabulary for domain concepts and preserve established names such as "Order service." Explain how those names map to the architectural roles under discussion when the distinction matters.
ADR conflicts: if a candidate contradicts an existing ADR, only surface it when the friction is real enough to warrant revisiting the ADR. Explain the conflict and the evidence for reopening the decision in the report. Don't list every theoretical refactor an ADR forbids.
Read HTML-REPORT.md only when producing an HTML artifact. Inspect the rendered report when the environment allows it and disclose any rendering gap; generating a file does not prove its diagrams are readable.
For discovery-only requests, deliver the findings and stop. If further exploration is requested but several materially different candidates remain, ask which to explore after presenting the evidence. When the target is already clear, continue without that selection pause.
遵循请求的格式和环境的实际渲染能力。对于紧凑的评审,当宿主可以渲染时,使用简洁的文字加内联图表。对于可移植的报告或受益于单独工件的对比,使用HTML。如果无法进行视觉渲染,请用文字解释关系并说明限制,而不是阻止分析。
在视觉效果能够阐明所有权、依赖关系、调用方工作量或拟议变更的地方使用可视化。当结构差异很重要时,前后对比图很有用;不要仅仅为了填充报告而编造多个候选方案或图表类型。根据情况使用宿主支持的Mermaid渲染、内联SVG或简单的HTML/CSS。
编写HTML报告时,使用内联CSS和SVG,这样无需网络依赖即可打开。将其保存到请求的位置,否则保存到操作系统临时目录中的新文件。使用可用的工件预览功能展示,或者在无法打开时提供其绝对路径和链接。不要要求特定的操作系统命令或外部浏览器。
对于每个候选方案,提供判断所需的证据:
  • 证据:相关文件、符号和调用方轨迹
  • 问题:具体的摩擦点及其后果
  • 解决方案:用通俗易懂的语言描述将要变更的内容
  • 收益:给调用方和维护者带来的具体好处,包括受影响时的可测试性
  • 结构对比:用图表或简洁的文字说明变更后调用方和维护者需要了解的内容
  • 推荐强度和不确定性:证据对变更的支持程度,以及哪些内容仍未经验证
当请求的是探索发现时,推荐最优的候选方案。对于已选定的目标,解释证据是否支持该变更,并继续处理其设计问题。当仅靠图表无法传达契约或权衡时,使用简洁的解释性文字。
领域概念使用项目术语表中的词汇,并保留已确立的名称,例如“Order service”。当区别很重要时,解释这些名称如何映射到所讨论的架构角色。
ADR冲突:如果候选方案与现有ADR矛盾,仅当摩擦确实严重到值得重新审议ADR时才提出来。在报告中解释冲突以及重新开启该决策的证据。不要列出ADR禁止的每一个理论上的重构。
仅在生成HTML工件时才阅读HTML-REPORT.md。在环境允许的情况下检查渲染后的报告,并披露任何渲染缺陷;生成文件并不证明其图表是可读的。
对于仅探索的请求,交付发现结果后停止。如果请求进一步探索,但仍有几个存在实质性差异的候选方案,在呈现证据后询问要探索哪一个。当目标已经明确时,无需暂停选择,直接继续。

3. Explore the selected change

3. 探索选定的变更

Ground the selected candidate in its callers, constraints, dependencies, and existing tests. Reuse settled decisions and proceed directly to a concrete interface or migration recommendation when the evidence supports it. Use the
grilling
skill when available for material unresolved choices; otherwise ask focused questions about those choices directly. Do not require an interview when no human decision is missing.
Use the
domain-modeling
skill when available to keep meanings and records aligned. Follow existing documentation conventions and the authorized working mode: write only when documentation updates are authorized, and keep proposed text in the conversation for read-only or discussion work.
  • Naming a deepened module after a new domain concept? Record its agreed definition in the existing glossary, or propose the entry in discussion mode.
  • Resolving a material ambiguity in a term? Update or propose the definition once its meaning is settled; harmless wording variations do not need a new record.
  • User rejects the proposal for a reason future reviews would need? Offer to record that reason in an ADR so later reviews do not repeat the proposal without its context. Skip temporary reasons such as “not worth it right now” and reasons already clear from the code or documentation.
  • Want to explore alternative interfaces for the deepened module? Use the alternative-interface comparison guidance provided by the
    codebase-design
    skill when available. Otherwise compare concrete interface options against the same caller needs, protected contracts, and migration constraints. Use parallel agents when useful and available.
Finish with the requested findings or design recommendation, its evidence, preserved contracts, and the verification or decision still needed. Implementation, commits, or external publication require authorization for that work; selecting a candidate alone does not authorize them.
将选定的候选方案建立在其调用方、约束、依赖和现有测试的基础上。当证据支持时,复用已确定的决策,直接给出具体的接口或迁移建议。对于重大的未解决选择,如有可用的
grilling
skill则使用它;否则直接针对这些选择提出有针对性的问题。当不需要人工决策时,不要要求访谈。
如有可用的
domain-modeling
skill,使用它来保持含义和记录的一致性。遵循现有的文档约定和授权的工作模式:仅在授权更新文档时才写入,对于只读或讨论性工作,将提议的文本保留在对话中。
  • 用新的领域概念为深化后的模块命名? 在现有术语表中记录其商定的定义,或在讨论模式下提出该条目。
  • 解决术语中的重大歧义? 一旦含义确定,更新或提议定义;无害的措辞差异不需要新记录。
  • 用户拒绝提议的原因是未来评审需要知道的? 提议将该原因记录在ADR中,这样以后的评审不会在没有上下文的情况下重复该提议。跳过临时原因(例如“现在不值得”)和已经从代码或文档中明确的原因。
  • 想要探索深化模块的替代接口? 如有可用的
    codebase-design
    skill,使用其提供的替代接口对比指南。否则,针对相同的调用方需求、受保护的契约和迁移约束,对比具体的接口选项。在有用且可用的情况下使用并行Agent。
最后给出请求的发现结果或设计建议,以及其证据、保留的契约,还有仍需进行的验证或决策。实现、提交或外部发布需要对应工作的授权;仅选择候选方案并不授权这些操作。