code-simplify
paulrberg/agent-skills
当用户要求“简化代码”、“清理代码”、“为提高清晰度而重构”、“降低复杂度”、“提高可读性”、 “让代码更易于维护”,或要求简化最近修改过的代码时使用。
...展开全部关于code-simplify
“code-simplify ”技能旨在简化现有代码,同时保留其运行时行为、公共接口、副作用及操作意图。它有助于开发人员降低代码复杂度、提高可读性,并使代码更易于维护,且不会引入功能性变更。 该技能侧重于安全、有针对性的重构,而非大范围重写,确保输入、输出、错误行为以及外部依赖的契约保持稳定。它适用于代码难以理解、过度嵌套、存在重复或不必要复杂的情况。
该技能会根据用户指定的目标、最近修改的文件或代码库变更来确定合适的范围。在进行更改之前,它会分析周围上下文以建立行为基线。 简化通过结构化的流程实现,包括扁平化控制流、提高命名清晰度、减少重复、将密集的数据转换重组为更清晰的步骤,并在适当情况下提高类型或契约的清晰度。该工作流强调微小且可逆的修改,并遵循现有的项目规范、代码检查工具、格式化工具和测试。 安全规则会阻止改变行为的修改,例如在没有明确指示的情况下,将同步 API 转换为异步 API、移除操作安全保护措施,或更改外部接口。
该技能适用于正在处理不断演进的代码库的开发人员、维护人员和团队,他们希望在不改变功能的前提下提升代码质量。 常见用例包括清理近期修改过的代码、为提高可读性而进行重构、减轻复杂函数的认知负荷、简化嵌套逻辑,以及为长期维护做好代码准备。在以代码仓库为基础的开发工作流中,当验证和行为稳定性是重要要求时,该技能尤为宝贵。
常见问题
何时应使用此技能?
当您希望简化代码、提高可读性、降低复杂度、清理近期修改的代码,或在不改变代码行为的前提下使其更易于维护时,请使用此技能。
该技能会改变应用程序的行为或公共 API 吗?
不会。该技能旨在保留运行时行为、公共契约、副作用、输入、输出以及外部依赖的错误语义。
该技能是否需要 Git 仓库?
是的。工作流首先会验证仓库上下文。如果当前目录不是 Git 仓库,则流程将停止,并要求从 Git 仓库中运行。
该技能如何决定修改哪些文件?
如果用户提供了目标文件,则优先使用这些文件。否则,它会优先处理会话中修改过的文件;若不存在此类文件,则可能回退到已跟踪但未提交的文件以及未被跟踪的文件。
对执行的重构类型是否有任何限制?
是的。该技能会避免改变行为的修改、大范围重写、不必要的抽象化、同步与异步 API 之间的转换,以及移除日志、遥测、保护机制、重试或其他对运行至关重要的代码。
Code Simplify
Objective
Simplify code while preserving behavior, public contracts, and side effects. Favor explicit code and local clarity over clever or compressed constructs.
Arguments
- Paths, patterns, a commit/range, or a scope phrase: used in Scope Resolution step 2.
--no-report: Skip the full user-facing report and return terse working notes for the caller.--no-verify: Skip verification because a parent orchestrator will verify the final result separately.- Default: verify touched behavior and present the full report.
Scope Resolution
Resolve scope once, then treat the result as fixed for the rest of the run.
- Verify repository context:
git rev-parse --git-dir. If this fails, stop and tell the user to run from a git repository. - If the request names targets — file paths/patterns, a commit/range, a natural-language subset (e.g. "the parser changes"), or a
resolved-scopefenced block with one repo-relative path per line — scope is exactly those targets. Map natural-language subsets to concrete paths before continuing. - Otherwise, scope is only session-modified files: files created or edited earlier in this session. Do not include other uncommitted changes.
- If there are no session-modified files, or earlier conversation history is not visible in this context, fall back to all uncommitted files, running each command once:
- tracked:
git diff --name-only --diff-filter=ACMR - untracked:
git ls-files --others --exclude-standard - combine both lists and de-duplicate.
- tracked:
- Exclude generated/low-signal files unless explicitly requested: lockfiles, minified bundles, build outputs, vendored code.
- If scope resolves to zero files, report that and stop.
- Emit the scope as a fenced code block tagged
resolved-scope, one repo-relative path per line. The block is authoritative: do not re-run scope commands or revisit exclusions afterward.
Operating Rules
- Preserve runtime behavior exactly. Keep inputs, outputs, side effects, and error behavior stable.
- Prefer project conventions over personal preferences. Infer conventions from existing code, linters, formatters, and tests.
- Make small, reversible edits. Avoid broad rewrites when targeted simplifications solve the problem.
- Call out uncertainty immediately when behavior may change.
Workflow
1) Determine Scope
- Apply the Scope Resolution section.
2) Build a Behavior Baseline
- Read surrounding context, not only changed lines.
- Identify invariants that must not change:
- function signatures and exported APIs
- state transitions and side effects
- persistence/network behavior
- user-facing messages and error semantics where externally relied on
- Note available verification commands (lint, tests, typecheck).
3) Apply Simplification Passes (in this order)
- Control flow:
- Flatten deep nesting with guard clauses and early returns.
- Replace nested ternaries with clearer conditionals.
- Naming and intent:
- Rename ambiguous identifiers when local context supports safe renaming.
- Separate mixed concerns into small helpers with intent-revealing names.
- Duplication:
- Remove obvious duplication.
- Abstract only when at least two real call sites benefit and the abstraction reduces cognitive load.
- Data shaping:
- Break dense transform chains into named intermediate steps when readability improves.
- Keep hot-path performance characteristics stable unless improvement is explicit and measured.
- Type and contract clarity:
- Add or tighten type annotations when they improve readability and safety without forcing broad churn.
- Preserve external interfaces unless asked to change them.
4) Enforce Safety Constraints
- Do not convert sync APIs to async (or reverse) unless explicitly requested.
- Do not alter error propagation strategy unless behavior remains equivalent and verified.
- Do not remove logging, telemetry, guards, or retries that encode operational intent.
- Do not collapse domain-specific steps into generic helpers that hide intent.
5) Verify
Skip when --no-verify is set. Otherwise verify per the Verification section below.
6) Report
Produce the Report section below.
Simplification Heuristics
- Prefer explicit local variables over nested inline expressions when it reduces cognitive load.
- Prefer one clear branch per condition over compact but ambiguous condition trees.
- Keep function length manageable, but do not split purely for line count.
- Keep comments that explain intent, invariants, or non-obvious constraints.
- Remove comments that restate obvious code behavior.
- Optimize for the next maintainer's comprehension time, not minimum character count.
Anti-Patterns
- Do not perform speculative architecture rewrites.
- Do not introduce framework-wide patterns while simplifying a small local change.
- Do not replace understandable duplication with opaque utility layers.
- Do not bundle unrelated cleanups into one patch.
Verification
Run the narrowest checks that validate touched behavior:
- formatter/lint on touched files
- targeted tests for touched modules
- typecheck when relevant
Run broader checks only when risk warrants it. Name every skipped check and why.
Report
Skip when --no-report is set; return terse working notes instead: touched scope, key simplifications, residual risks.
Use these section headings, in this order. Omit sections that do not apply — do not number them and do not leave gaps or placeholders.
Scope
Files and regions changed.
Simplifications
One sentence per meaningful change, focused on the readability or maintainability gain. Confirm behavior-preservation assumptions explicitly.
Verification
Commands run and outcomes, including skipped checks.
Residual Risks
One line per risk: Assumed <assumption>; if wrong, <what breaks>; check via <command or inspection>. Plain language — expand or gloss domain-specific terms. Include questions that need a user decision, phrased directly. Write None. when there are none.
Stop Conditions
Stop and ask for direction when:
- simplification requires changing public API/contracts.
- behavior parity cannot be confidently verified.
- the code appears intentionally complex due to domain constraints.
- the requested scope implies a larger redesign rather than simplification.





首页
