4.0 KiB
4.0 KiB
| name | description |
|---|---|
| bulk-refactor-workflow | Use when renaming symbols, migrating APIs, or applying safe mechanical changes across many files in the repository. |
Bulk Refactor Workflow(批量重构 / 大范围修改)
Overview
Guide repository-wide mechanical edits without losing scope control or damaging unrelated work. Core principle: enumerate first, transform mechanically, verify in loops, and keep every batch auditable.
When to Use
- Rename APIs, symbols, file conventions, or repeated string patterns
- Apply scripted or structural edits across many files
- Migrate callers to a new interface where the change pattern is repeatable
When Not to Use
- One-off logic changes where no repeatable transformation exists
- Exploratory redesign or feature work that needs new architecture decisions
- Cleanup that is only formatting/lint alignment; use
style-cleanupdirectly
Inputs
- Scope: directories, file types, include/exclude rules
- Transformation: rename, replace, codemod, scripted edit, migration strategy
- Constraints: behavior-preserving vs. compatibility window vs. staged rollout
- Verification commands: smallest useful loop plus final gate
Procedure
-
Baseline and bound the scope
- Record
git status --short. - Dirty worktrees are allowed.
- Do not revert unrelated changes.
- Mark the exact target file set before editing.
- Run a baseline verification command so existing failures are not misattributed to the refactor.
- Record
-
Enumerate every intended hit
- Search before editing with
rg,git grep, or a structured query. - Separate real code hits from comments, docs, generated files, and samples.
- Write down exclusions before running the transformation.
- Search before editing with
-
Choose the safest mechanical transform
- Prefer deterministic transforms: codemods, scripts, structured edits, or narrow search/replace with explicit scope.
- If compatibility matters, use a staged migration: adapt providers first, migrate callers second, remove compatibility layer last.
-
Apply in bounded batches
- First, apply the transformation in bounded batches.
- Verify each batch before widening scope.
- Stop immediately if output differs from the expected hit class.
-
Verify in loops
- Run the smallest relevant verification after each batch.
- Re-check the hit list after edits to confirm the intended pattern is gone and unintended patterns were not introduced.
-
Finish with targeted cleanup
- If the mechanical change leaves formatting or lint fallout, Use
style-cleanupfor the final formatting/lint pass. - Keep that cleanup scoped to the refactor set unless the user approves a wider pass.
- If the mechanical change leaves formatting or lint fallout, Use
-
Report
- Summarize files touched, transform rules used, verification evidence, and any residual risk.
Output Contract
Scope:files, directories, and exclusions actually usedTransformation:exact rule or script appliedBatches:how the work was partitionedVerification:commands, exit status, and what each loop provedRisks:remaining ambiguous areas, compatibility debt, or deferred cleanup
Success Criteria
- Every changed file belongs to the declared scope
- The transformation is reproducible and explainable
- Verification passes at both batch level and final gate
- Unrelated work in the repository is preserved
Failure Handling
- If the hit list is ambiguous, stop and refine scope before editing
- If the transform cannot be expressed mechanically, downgrade to a normal refactor plan instead of pretending it is safe bulk work
- If verification fails mid-batch, stop, inspect the smallest failing delta, and correct the transform before continuing
- If compatibility risk is higher than expected, split the migration into staged commits or phases
Guardrails
- Never run repository-wide replacement without an explicit hit review
- Never mix unrelated cleanup into the same batch
- Never hide a large-scope behavior change behind the label "mechanical refactor"