Refactor workflow
Refactor = improving structure WITHOUT changing externally visible behavior.
1. Identify exact scope
- Files involved (use Glob/Grep/Serena
find_references) - External interface (public API, exported functions, DB schema, HTTP routes)
- Internal-only structure that can change
2. State invariants (must NOT change)
- HTTP API contract (paths, params, response shape)
- Public function signatures
- DB schema (unless this IS a schema migration)
- Test outputs (existing tests must still pass)
Write invariants to .claude-data/invariants-session.md so PreCompact preserves them.
3. Small reversible changes (R12 per task)
- ONE change at a time
- After EACH change: run tests + mypy + ruff
- If a change breaks something → revert that one change, not the whole work
- Commit-able state at every step (even if you don't commit)
4. Update navigation if needed
- If files moved → update
docs/agent/05_module_map.md - If new module created → add to module map
- If responsibility shifted between modules → update both
5. ADR if architectural boundary changes
- Moved auth logic from
api/to dedicatedauth/package = architectural = ADR - Renamed function = not architectural = no ADR
- Reorganized internal helpers = not architectural = no ADR
6. Do NOT mix in feature work (R3)
- If you see a bug while refactoring — log to
docs/agent/06_known_issues.md, do not fix in this commit - If you see a missing feature — log to backlog, do not add
- Refactor commit must be reviewable as "structure only"
Anti-patterns
- ✗ "Big bang" refactor of 30+ files in one session
- ✗ Mixing rename + bugfix + new feature in one commit
- ✗ Refactoring without running tests (assumed they'll pass)
- ✗ Renaming public API without checking call sites