batch-refactor-with-sub-agents
Skill486 starsChanged 15 days agoApache-2.0View on GitHub
Use a sub-agent (like `generalist`) to perform repetitive code transformations across multiple files in a single turn.
We split this skill into the rules it gives an agent
9 rules. About 0 are common, 1 is distinctive, and 8 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
8
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 15
Write a custom, disposable Rust script.
- Very specific: line 17
Use std::fs to read files, apply string replacements (replace()), and write back (only if not a dry-run).
- Very specific: line 19
Run cargo check to verify.
- Very specific: line 23
Locate targets: Use grep_search to find all absolute file paths and line numbers that need modification.
- Very specific: line 24
Draft a precise prompt: Call the generalist (or similar) tool with Model: "inherit" and a prompt that includes:
- Very specific: line 39
fix: Read the modified files and use targeted replace calls to fix the syntax errors.
- Very specific: line 42
Run ./check.fish --check (or cargo check -q --message-format=short --all-targets) to confirm zero compilation errors.
- Very specific: line 43
Run ./check.fish --clippy (or cargo clippy -q --message-format=short --all-targets) to find and fix any unused_import or style warnings …
- Distinctive: line 44
Run relevant integration tests for the modified files.
≈ “Common” is an estimate: at least 1 in 5 of the 308,232 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
When to Use Sub-Agents
- Renaming symbols or updating function signatures across many files (but not the entire repository).
- Migrating code from one pattern to another (e.g., manual error handling to a common helper).
- Replacing literals with centralized constants across the codebase.
- Performing repetitive tasks that require semantic understanding and cannot be solved with a simple string replace.
When to Use Custom Rust Scripts (Priority for Massive Refactors)
For massive, repository-wide bulk refactoring (typically 6+ files, e.g., renaming a string or trait across 100+ files), you MUST NOT use sed, awk, perl, python, bash, or fish (which are strictly prohibited). Furthermore, spawning sub-agents for a simple find-and-replace across 6+ files is inefficient.
Instead:
- Write a custom, disposable Rust script. Save this script in
tmpfs(e.g.,/tmp/). - Implement a
--dry-runCLI argument to test the script before making destructive changes. - Use
std::fsto read files, apply string replacements (replace()), and write back (only if not a dry-run). - Compile it natively with
rustcand execute it. - Run
cargo checkto verify.
Procedure for Sub-Agents
- Define the transformation: Identify the exact "before" and "after" patterns. Create a representative code snippet for the sub-agent to follow.
- Locate targets: Use
grep_searchto find all absolute file paths and line numbers that need modification. - Draft a precise prompt: Call the
generalist(or similar) tool withModel: "inherit"and a prompt that includes:- Goal: Clear statement of the refactoring objective.
- Scope: A bulleted list of absolute file paths to modify.
- Example: A code block showing the
old_stringvsnew_stringtransformation. - Constraints: Instructions to preserve specific logic (e.g., "preserve original closure logic while changing the call site").
- Verification: Instructions to run
./check.fish --check(orcargo check -q --message-format=short --all-targets) or specific tests after finishing.
- Delegate: Execute the sub-agent call using
invoke_subagentwithModel: "inherit". - Review and Cleanup: Sub-agents may introduce minor issues like
unused_importwarnings. Perform a final sweep with./check.fish --clippy(orcargo clippy -q --message-format=short --all-targets) and fix manually or via a second batch call.
Pitfalls and Fixes
- symptom: Sub-agent misses some files or applies incorrect logic.
- likely cause: Prompt was too vague or the transformation was too complex for a single turn.
- fix: Break the task into smaller, more homogeneous batches (e.g., "refactor all files in directory A first").
- symptom:
cargo checkfails after the sub-agent finishes. - likely cause: Sub-agent clobbered a symbol or messed up indentation/syntax.
- fix: Read the modified files and use targeted
replacecalls to fix the syntax errors.
Verification
- Run
./check.fish --check(orcargo check -q --message-format=short --all-targets) to confirm zero compilation errors. - Run
./check.fish --clippy(orcargo clippy -q --message-format=short --all-targets) to find and fix anyunused_importor style warnings introduced by the refactor. - Run relevant integration tests for the modified files.
In this repository30 other files it gives its agents.
- .agents/AGENTS.mdAGENTS.md
- AGENTS.mdAGENTS.md
- build-infra/AGENTS.mdAGENTS.md
- rust-analyzer-mcp-server/AGENTS.mdAGENTS.md
- task/AGENTS.mdAGENTS.md
- llms.txtllms.txt
- analyze-log-filesSkill
- analyze-performanceSkill
- audit-taskSkill
- check-bounds-safetySkill
- check-code-qualitySkill
- check-test-coverageSkill
- code-reviewSkill
- concurrency-safetySkill
- create-commit-messageSkill
- create-prSkill
- design-philosophySkill
- fast-string-allocationsSkill
- fix-issueSkill
- merge-prSkill
- organize-modulesSkill
- organize-testsSkill
- release-crateSkill
- remove-crate-prefixSkill
- review-prSkill
- run-clippySkill
- safe-renamingSkill
- test-cross-platformSkill
- write-documentationSkill
- write-structured-tracingSkill
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

