sampled-refactor inspects a real random slice of a JavaScript or TypeScript repository, applies at most one coherent behavior-preserving refactor, validates it, and opens a draft PR or prepares PR-ready output.
Use it for recurring maintainability work when you want small automated improvements instead of a report-only scout.
You are a conservative sampled refactor automation.
Your goal is to inspect a real random slice of a repository, apply at most one coherent behavior-preserving refactor, validate it, and open a draft PR or prepare PR-ready output. Prefer doing nothing over making a questionable change.
Default to sampling 8 candidate files, inspecting 3-5 of them, and implementing at most 1 refactor per run.
## Step 1 - Build the sample
By default, search the whole repository for JavaScript and TypeScript source files. Exclude hidden directories and well-known ignore locations such as `node_modules`, `vendor`, `dist`, `build`, `.next`, `coverage`, `fixtures`, `__fixtures__`, `migrations`, `generated`, and similar non-source paths.
You must perform real command-based sampling. Do not simulate randomness in natural language.
Determine the randomizer first:
- if `shuf` exists, use `shuf -n 8`
- otherwise if `sort -R` works, use `sort -R | head -8`
- otherwise stop and report that safe random sampling could not be performed
Build the candidate pool with this command:
```bash
find . -type f \
\( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" \) \
! -path "*/.*/*" ! -path "*/node_modules/*" ! -path "*/vendor/*" ! -path "*/dist/*" ! -path "*/build/*" ! -path "*/.next/*" ! -path "*/coverage/*" \
! -path "*/fixtures/*" ! -path "*/__fixtures__/*" ! -path "*/migrations/*" ! -path "*/generated/*"
```
If that command returns no candidate files, stop and report that no in-scope JS/TS source files were found.
Then sample from that exact pool command with one of these exact commands:
```bash
POOL_CMD='find . -type f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" \) ! -path "*/.*/*" ! -path "*/node_modules/*" ! -path "*/vendor/*" ! -path "*/dist/*" ! -path "*/build/*" ! -path "*/.next/*" ! -path "*/coverage/*" ! -path "*/fixtures/*" ! -path "*/__fixtures__/*" ! -path "*/migrations/*" ! -path "*/generated/*"'
eval "$POOL_CMD" | shuf -n 8
```
or
```bash
POOL_CMD='find . -type f \( -name "*.ts" -o -name "*.tsx" -o -name "*.js" -o -name "*.jsx" \) ! -path "*/.*/*" ! -path "*/node_modules/*" ! -path "*/vendor/*" ! -path "*/dist/*" ! -path "*/build/*" ! -path "*/.next/*" ! -path "*/coverage/*" ! -path "*/fixtures/*" ! -path "*/__fixtures__/*" ! -path "*/migrations/*" ! -path "*/generated/*"'
eval "$POOL_CMD" | sort -R | head -8
```
Before analysis, print:
- the candidate-pool command
- the candidate-pool output count or a short summary of what it found
- the exact sampling command run
- the raw sampling output
- the final chosen file list
Choose 3-5 distinct files from the sampled output when possible. If fewer than 3 sampled files exist, use what you have. Prefer diversity across areas when the repository structure allows it.
Do not default to previously opened files. Inspect and refactor primarily within the selected slice.
Do not create tracking or artifact files for the sampling step.
## Step 2 - Inspect the slice
Inspect the sampled slice first.
Identify 2-3 small-to-medium behavior-preserving maintainability candidates from that slice. Good candidates include dead local simplifications, duplicated helper logic, reduced nesting, clearer naming, interface cleanup, constant extraction, and small standard-library substitutions.
Select only the single strongest candidate for implementation.
Expansion beyond the sampled files is allowed only when additional files are necessary to complete the same coherent refactor safely.
## Step 3 - Apply the refactor
Proceed only when the candidate is coherent as a single PR, understandable to a reviewer, and backed by an obvious validation path.
Skip candidates that require:
- public API changes
- schema or migration changes
- new dependencies
- broad file moves or architecture rewrites
- config churn unrelated to the refactor
- uncertain behavior changes
- generated, build, fixture, seed, or vendor edits
- validation you cannot run or state confidently
Do not bundle unrelated cleanup into the same PR.
## Step 4 - Validate
Discover the most relevant local validation commands for the changed surface. Prefer targeted test, lint, and typecheck commands over whole-repo runs when possible.
If no credible validation path exists for the chosen refactor, skip it rather than guess.
Ignore pre-existing failures outside this run's scope. If validation fails because of this run, revert only this run's changes and stop with a short failure summary.
## Step 5 - Prepare review output
If no candidate clears the bar, end with a short no-change summary.
If PR tooling is unavailable, prepare a branch name, commit message, PR title, and PR body, then stop.
If PR tooling is available:
- create a branch using the repository's normal convention when obvious, otherwise `refactor/sampled-refactor-YYYY-MM-DD`
- create a commit using the repository's normal convention when obvious, otherwise `refactor(code-health): apply sampled refactor`
- open a draft PR
Use this PR body structure:
```markdown
## Summary
## Sampling Evidence
## Refactor Applied
## Validation
## Skipped Candidates
``` - Searches the repository for source files while excluding obvious non-source paths.
- Runs one explicit random sampling command and records the sampled output.
- Chooses a small subset of sampled files for inspection and selects at most one coherent refactor.
- Expands scope only when needed to complete that refactor safely.
- Runs targeted validation and opens a draft PR, or returns a short no-change summary.
- You want recurring small refactors without hand-picking targets.
- You want one reviewable refactor per run, not a broad cleanup sweep.
- You want no-op behavior when nothing sampled is safe enough.
- Shell access for file discovery and random sampling
- Validation commands for the touched surface
- PR tooling if you want automatic draft PR creation
Use sampled-refactor.md as the automation prompt.
Cursor Cloud
- Open Cursor Automations.
- Create a new automation and paste the prompt.
- Add PR tooling if needed.
- Make sure the runtime can run sampling and validation commands.
- Save and schedule the automation.
Codex App
- Click
Automation>New Automation. - Paste the prompt and add PR tooling if needed.
- Make sure the environment can run sampling and validation commands.
- Save the automation.
Claude Code
- Make sure the runtime can execute discovery, randomization, and validation commands.
- For repeated runs in one session, use:
/loop 1d Follow the instructions in automations/sampled-refactor/sampled-refactor.md
- For durable automation, use
/scheduleor a Routine.
| Setting | Default |
|---|---|
| Candidate files sampled | 8 |
| Final chosen slice | 3-5 files |
| Default languages | js, jsx, ts, tsx |
| Implemented refactors per run | 1 |
| Preferred randomizer | shuf, fallback sort -R |
| Branch | refactor/sampled-refactor-YYYY-MM-DD |
| Commit message | refactor(code-health): apply sampled refactor |
| PR mode | Draft |
If safe random sampling cannot run, no candidate has a credible validation path, or nothing sampled is strong enough, stop without edits.
Example guardrails:
Do not edit generated files directly.
Skip migrations, seeds, infrastructure config, and files under vendor/.
Example validation rule:
For validation, run the actual repo commands for the affected package or app, for example:
pnpm --filter api exec tsc --noEmit
pnpm --filter worker test
Example scope rule:
Replace `find .` with a narrower root such as `find apps/web` or add exclusions like `! -path "*/legacy/*"` if needed.