Apply a convention or pattern consistently across a codebase scope
Apply a documented convention or pattern consistently across a specified scope in the codebase.
Example invocations:
/standardize error handling in src/services//standardize repository pattern in user domain/standardize logging across all modules/standardize type hints in src/utils/Use /standardize when you need to:
Not for: One-off fixes (use /bug-fix) or new features (use /implement).
Before starting standardization, determine where to work.
Run the workspace check script to understand the current environment:
.claude/skills/standardize/scripts/check-workspace.sh
This reports: current branch, worktree status, uncommitted changes, remote tracking, existing worktrees, and recent commits - all in a single invocation.
Use AskUserQuestion:
Where should I apply this standardization?
**Option A: New worktree** (Recommended for large scopes)
- Creates isolated workspace branched from origin/main
- Copies .env files automatically
- Safe for sweeping changes across many files
- Allows multiple agents to work simultaneously
**Option B: Current worktree**
- Creates branch from origin/main in current checkout
- Simpler, no setup overhead
- Good for small, contained scopes
If new worktree - run the setup script:
.claude/skills/standardize/scripts/setup-worktree.sh "refactor/standardize-<convention>"
This automatically:
origin/main.env* files from the main repoAfter it completes, cd into the new worktree directory it prints.
If current worktree:
git fetch origin main --quiet && git checkout -b refactor/standardize-<convention> origin/main
Base branch: Always origin/main unless the user explicitly specifies a different base.
Extract from $ARGUMENTS:
If unclear, use AskUserQuestion to clarify:
Look for the convention in:
Use Task tool with Explore agent:
Find the canonical definition and examples of [CONVENTION]:
1. Find 2-3 existing implementations that follow the pattern correctly
2. Identify the key characteristics that define "correct" usage
If there are multiple valid approaches to the convention, identify and present them:
## Pattern Variants for [Convention]
### Option A: [Name]
- **Approach**: [Description]
- **Pros**: [Benefits]
- **Cons**: [Drawbacks]
### Option B: [Name]
- **Approach**: [Description]
- **Pros**: [Benefits]
- **Cons**: [Drawbacks]
Which approach should we use?
Use AskUserQuestion to get a decision before proceeding.
Example from error handling:
Before applying broadly, validate the pattern belongs at this layer:
If uncertain, discuss with the user:
## Architectural Consideration
The proposed pattern would [describe what it does].
This means [layer X] would now depend on [layer Y].
Is this the right architectural choice, or should we keep these concerns separate?
Summarize what "correct" looks like:
## Convention: [Name]
### Correct Pattern
[Description of how it should be done]
### Example (from codebase)
`path/to/exemplary/file.py:123`
```python
# Correct implementation
[code snippet]
# Incorrect/legacy implementation
[code snippet]
### Phase 3: Find Violations
#### 3.1 Search for Anti-patterns
Use targeted searches to find code that doesn't follow the convention:
Use Task tool with Explore agent:
Find all violations of [CONVENTION] in [SCOPE]:
#### 3.2 Categorize Findings
Group violations by:
- **Simple**: Direct replacement, low risk
- **Complex**: Requires refactoring, affects multiple files
- **Uncertain**: Needs human judgment
```markdown
## Violations Found
### Simple (direct replacement)
| File | Line | Current | Should Be |
|------|------|---------|-----------|
| `path/file.py` | 45 | `raise OldException()` | `raise NewException()` |
### Complex (requires refactoring)
- `path/complex.py`: [Description of what needs changing]
### Uncertain (needs review)
- `path/edge_case.py`: [Why this is unclear]
CRITICAL: Before applying changes, evaluate impact on external consumers.
Check if the standardization will change:
| Risk Level | Criteria | Action |
|---|---|---|
| LOW | Internal-only code, no API exposure | Proceed normally |
| MEDIUM | API changes but same status codes | Note in plan, proceed with caution |
| HIGH | Status codes change, error format changes | Explicit user approval required |
If HIGH RISK changes are identified:
## ⚠️ Breaking Change Warning
The following changes may break existing API integrations:
| Scenario | Before | After | Risk |
|----------|--------|-------|------|
| Metric not found | HTTP 500 | HTTP 404 | Customers checking `status == 500` will break |
| Duplicate metric | HTTP 500 | HTTP 409 | Customers checking `status == 500` will break |
### Options
1. **Proceed anyway**: Accept the breaking change (may require customer communication)
2. **Maintain backwards compatibility**: Keep old behavior, add new pattern alongside
3. **Scope reduction**: Exclude high-risk endpoints from this standardization
Which approach should we take?
Use AskUserQuestion to get explicit approval for breaking changes.
CRITICAL: Get explicit approval before making changes.
Present:
## Standardization Plan: [Convention] in [Scope]
### Summary
- **Files affected**: [N]
- **Simple changes**: [N]
- **Complex changes**: [N]
- **Uncertain cases**: [N]
### Changes Preview
[Show representative examples of each category]
### Uncertain Cases
[List any that need human decision]
---
Should I proceed with these changes?
- Yes, apply all changes
- Yes, but skip uncertain cases
- Let me review the uncertain cases first
- No, let's adjust the scope
Use AskUserQuestion to get approval and handle uncertain cases.
For each simple change:
For each complex change:
For uncertain cases (if user approved):
Run the project's linting suite (e.g., task lint, make lint, npm run lint).
Fix any issues introduced by the changes.
Run the project's test suite (e.g., task test, make test, pytest, npm test).
If tests fail:
Quick check that no violations remain in scope:
Search for anti-pattern indicators in [SCOPE]
Confirm all have been addressed
Provide a complete summary:
## Standardization Complete: [Convention] in [Scope]
### Changes Made
- **Files modified**: [N]
- **Simple replacements**: [N]
- **Complex refactors**: [N]
- **Skipped (uncertain)**: [N]
### Files Changed
- `path/to/file1.py`: [Brief description]
- `path/to/file2.py`: [Brief description]
- ...
### Verification
- Linting: ✅ Passing
- Tests: ✅ Passing
- No remaining violations in scope
### Notes
- [Any edge cases or follow-up items]
- [Files that might need manual review]
After verification passes, offer to create a PR.
Use AskUserQuestion:
Standardization complete and verified. Would you like me to open a Pull Request?
- Yes, create PR targeting main/master
- Yes, but target a different branch
- No, I'll handle the PR myself
If approved, create the PR:
git add -A
git commit -m "refactor: standardize <convention> in <scope>
Applied <convention> consistently across <N> files.
No functional changes - pattern consistency only."
git push -u origin <branch-name>
gh pr create --title "refactor: standardize <convention> in <scope>" --body "## Summary
<1-2 sentences explaining what convention was applied and where>
## Problem
<Why this standardization was needed - inconsistency, tech debt, etc.>
## Solution
<The pattern/convention that was applied - 2-4 bullet points>
- Before: <old pattern>
- After: <new pattern>
## Changes
- **Files modified**: <N>
- **Simple replacements**: <N>
- **Complex refactors**: <N>
### Files Changed
- \`path/to/file1.py\`: <what changed>
- \`path/to/file2.py\`: <what changed>
## Testing
- All existing tests pass
- No functional changes - pattern consistency only
## Notes for Reviewers
<optional - any edge cases, files skipped, follow-up work needed>"
Claude PR review will automatically add deeper analysis after the PR is created.
If working in a separate worktree, offer cleanup:
Use AskUserQuestion:
PR created. Would you like me to clean up the worktree?
- Yes, remove the worktree and switch back to main repo
- No, keep the worktree around
If yes, run the cleanup script:
.claude/skills/standardize/scripts/cleanup-worktree.sh
This checks for uncommitted changes, navigates back to the main repo, and removes the worktree.
If there are items outside the original scope:
### Remaining Work (if any)
- [Items outside scope that also need updating]
- [Related conventions that might benefit from standardization]
Target: Consistent error/exception types across the codebase
Anti-pattern: Mixed exception styles, HTTPException in services, bare except Exception
Scope: Usually by domain or layer
Target: Repository interface in domain/core, implementation in infrastructure Anti-pattern: Direct query calls, business logic in repositories Scope: By domain
Target: Full type hints on all function signatures
Anti-pattern: Missing hints, Any without justification
Scope: By module
Target: Consistent structured logging throughout the codebase Anti-pattern: Print statements, inconsistent log levels or formats Scope: Entire codebase
Target: Consistent docstring format on public APIs Anti-pattern: Missing docstrings, mixed formats Scope: By module
Start by understanding exactly what convention to apply and where.