Refactor code in the EASI codebase — from single-file code health improvements to multi-file architectural changes.
Philosophy: Always pursue optimal code health (10.0). Refactoring is not optional — every file should reach 10.0 unless the complexity is inherent to the domain model or architectural pattern.
Usage: /refactor <file-or-directory> or /refactor --scope=architecture
Arguments:
- •
file(s): Files to analyze and refactor (single-file mode) - •
directory or package: Directory to analyze for structural patterns (multi-file mode) - •
--scope=architecture: Cross-context architectural analysis (architecture mode) - •No argument: prompt the user
Before You Start
Remind the user: commit or stash any uncommitted work before refactoring. Refactoring should always start from a clean working tree so changes can be reviewed and reverted cleanly.
Steps
1. Identify target and mode
Parse $ARGUMENTS to determine the refactoring mode:
| Argument | Mode | Scope |
|---|---|---|
Single file (.go, .ts, .tsx) | Single-file | Code health + domain alignment for one file |
| Directory or Go package path | Multi-file | Structural patterns across files in the directory |
--scope=architecture | Architecture | Cross-context coupling, ACL compliance, published language gaps |
| No argument | Ask the user | — |
2. Analyze
Run analysis appropriate to the mode. Use codescene-refactoring.md as references throughout.
Single-file mode
- •CodeScene code health: Run
mcp__codescene__code_health_scoreandmcp__codescene__code_health_reviewon the file. - •Read the file to understand the full context.
- •Domain analysis: Check for:
- •Logic in the wrong layer (business logic in projectors/handlers, infrastructure in domain)
- •Primitive obsession (string/int where a value object should exist)
- •Aggregate doing too much (mixed responsibilities)
- •Missing or misplaced validation
- •Anemic domain models and complex read models
Multi-file mode
- •Glob for all source files in the directory.
- •CodeScene code health on each file — all files below 10.0 must be refactored.
- •Structural analysis: Check for:
- •Layer violations (domain importing infrastructure, handler containing business logic)
- •Duplicated patterns across files that indicate a missing abstraction
- •Inconsistent naming or organization vs project conventions
- •Anemic domain models and complex read models
- •Read model / projector alignment (does each projector serve exactly one read model?)
Architecture mode
- •Cross-context coupling: Grep for imports between bounded contexts that bypass published language packages.
- •SQL join analysis: Look for SQL queries that join tables owned by different contexts.
- •Published language gaps: Check if events and shared types are properly exposed via
publishedlanguagepackages. - •ACL compliance: Verify anti-corruption layers exist where contexts communicate.
- •Cache projector coverage: Check if cross-context data needs are served by local cache projectors rather than direct queries.
3. Classify findings
Classify each finding into a priority:
| Priority | Criteria | Action |
|---|---|---|
| Critical | Breaks architectural boundaries, cross-context coupling, data integrity risk | Must fix immediately |
| High | Code health < 4.0, logic in wrong layer, missing domain concept | Must fix |
| Normal | Code health 4.0–8.9, minor pattern violations, style inconsistencies | Fix — iterate until resolved |
| Polish | Code health 9.0–9.9, minor duplication or string-heavy args in tests | Fix — iterate until 10.0 using table-driven tests or shared helpers |
| Skip | Inherent complexity (event-sourced type switches, domain event constructors with many args), already 10.0 | Leave as-is, document reason |
Goal: All files must reach 10.0 Code Health. Only skip findings that are inherent to the domain model or architectural pattern (see codescene-refactoring.md for examples). For everything else, iterate until resolved.
4. Execute refactoring
Follow these rules:
General:
- •Preserve all existing behavior — refactoring only, no functional changes
- •Respect project code style (no added comments, no over-engineering)
CodeScene-driven:
- •Re-check with
mcp__codescene__code_health_scoreafter changes - •Iterate until all files reach 10.0 or until only inherent domain/architectural complexity remains
Domain/Architectural:
- •When moving code between files, ensure all references are updated
- •When creating new files (migrations, projectors), follow existing naming conventions
- •When modifying events or published language, check all subscribers
"DRY = Knowledge, Not Code":
- •Only extract shared abstractions when the duplicated code represents the same business concept
- •Structurally similar code that serves different purposes should remain separate
6. Verify
- •Backend changes: Run
go build ./...andgo test ./...(or scoped to affected packages) - •Frontend changes: Run
npm run buildandnpm test -- --run - •Both: Run both verification suites
- •If verification fails, fix the issue before continuing
7. Report
## Refactoring Complete: <target> ### Changes Applied | # | File | Change | Before | After | |---|------|--------|--------|-------| | 1 | path | description | score/state | score/state | ### Code Health Summary (if applicable) Before: <original-score(s)> After: <final-score(s)> ### Remaining Items (only if inherent to architecture) - <any findings classified as Skip with detailed justification> ### Checklist - [ ] All changes are behavior-preserving - [ ] Build passes - [ ] Tests pass - [ ] No new cross-context coupling introduced
Handling Inherent Complexity
Stop and discuss with the user if:
- •The refactoring would require changing public API contracts (breaking change)
Rules
- •Never exclude test files from analysis unless explicitly asked