Skip to content

技能:简化代码

Skill: Simplify

v2.1.63

Instructions for simplifying code

模板变量 / Template Variables

  • AGENT_TOOL_NAME
  • GREP_TOOL_NAME

简化:代码审查与清理

审查所有变更文件的可复用性、质量和效率。修复发现的问题。

第一阶段:识别变更

运行 git diff(如果有暂存的更改,则运行 git diff HEAD)以查看更改内容。如果没有 git 变更,则审查用户提及的或您在此对话中较早编辑的最近修改的文件。

第二阶段:并行启动三个审查 Agent

使用 ${AGENT_TOOL_NAME} 工具在单条消息中同时启动所有三个 agent。将完整的 diff 传递给每个 agent,使其拥有完整的上下文。

Agent 1:代码复用审查

针对每项变更:

  1. 搜索现有的工具和辅助函数,这些可能替代新编写的代码。使用 ${GREP_TOOL_NAME} 在代码库的其他位置查找类似模式——常见位置包括工具目录、共享模块以及变更文件相邻的文件。
  2. 标记任何重复现有功能的新函数。建议改用现有函数。
  3. 标记任何可以使用现有工具的内联逻辑——手写的字符串操作、手动路径处理、自定义环境检查、临时类型守卫以及类似模式通常是常见候选。

Agent 2:代码质量审查

审查相同变更中的 hacky 模式:

  1. 冗余状态:重复现有状态的状态、可以派生的缓存值、可以改为直接调用的观察者/副作用
  2. 参数蔓延:向函数添加新参数,而不是对现有参数进行泛化或重构
  3. 复制粘贴但略有变化:应通过共享抽象进行统一的近似重复代码块
  4. 抽象泄露:暴露应封装的内容,或破坏现有的抽象边界
  5. 字符串类型代码:在代码库中已存在常量、枚举(字符串联合)或品牌类型的地方使用原始字符串

Agent 3:效率审查

审查相同变更中的效率问题:

  1. 不必要的工作:冗余计算、重复文件读取、重复的网络/API 调用、N+1 模式
  2. 错失并发:可以并行运行的独立操作却按顺序运行
  3. 热路径膨胀:在启动或每次请求/渲染的热路径上添加了新的阻塞性工作
  4. 不必要的存在性检查:在操作前预先检查文件/资源是否存在(TOCTOU 反模式)——直接操作并处理错误
  5. 内存:无界数据结构、缺少清理、事件监听器泄漏
  6. 过于宽泛的操作:仅需要部分内容时读取整个文件,过滤单个项目时加载所有项目

第三阶段:修复问题

等待所有三个 agent 完成。汇总它们的发现并直接修复每个问题。如果发现是误报或不值得解决,请注明并继续——不要与发现争论,直接跳过即可。

完成后,简要总结修复的内容(或确认代码已经干净)。


英文原文 / English Original

Simplify: Code Review and Cleanup

Review all changed files for reuse, quality, and efficiency. Fix any issues found.

Phase 1: Identify Changes

Run git diff (or git diff HEAD if there are staged changes) to see what changed. If there are no git changes, review the most recently modified files that the user mentioned or that you edited earlier in this conversation.

Phase 2: Launch Three Review Agents in Parallel

Use the ${AGENT_TOOL_NAME} tool to launch all three agents concurrently in a single message. Pass each agent the full diff so it has the complete context.

Agent 1: Code Reuse Review

For each change:

  1. Search for existing utilities and helpers that could replace newly written code. Use ${GREP_TOOL_NAME} to find similar patterns elsewhere in the codebase — common locations are utility directories, shared modules, and files adjacent to the changed ones.
  2. Flag any new function that duplicates existing functionality. Suggest the existing function to use instead.
  3. Flag any inline logic that could use an existing utility — hand-rolled string manipulation, manual path handling, custom environment checks, ad-hoc type guards, and similar patterns are common candidates.

Agent 2: Code Quality Review

Review the same changes for hacky patterns:

  1. Redundant state: state that duplicates existing state, cached values that could be derived, observers/effects that could be direct calls
  2. Parameter sprawl: adding new parameters to a function instead of generalizing or restructuring existing ones
  3. Copy-paste with slight variation: near-duplicate code blocks that should be unified with a shared abstraction
  4. Leaky abstractions: exposing internal details that should be encapsulated, or breaking existing abstraction boundaries
  5. Stringly-typed code: using raw strings where constants, enums (string unions), or branded types already exist in the codebase

Agent 3: Efficiency Review

Review the same changes for efficiency:

  1. Unnecessary work: redundant computations, repeated file reads, duplicate network/API calls, N+1 patterns
  2. Missed concurrency: independent operations run sequentially when they could run in parallel
  3. Hot-path bloat: new blocking work added to startup or per-request/per-render hot paths
  4. Unnecessary existence checks: pre-checking file/resource existence before operating (TOCTOU anti-pattern) — operate directly and handle the error
  5. Memory: unbounded data structures, missing cleanup, event listener leaks
  6. Overly broad operations: reading entire files when only a portion is needed, loading all items when filtering for one

Phase 3: Fix Issues

Wait for all three agents to complete. Aggregate their findings and fix each issue directly. If a finding is a false positive or not worth addressing, note it and move on — do not argue with the finding, just skip it.

When done, briefly summarize what was fixed (or confirm the code was already clean).