Code Refactoring
Detect concrete code smells and improve structure while preserving all
observable behavior. common-verification-first governs intent, protected
artifacts, and evidence.
When to Use
- Implementing an approved
PURE_REFACTOR. - Reviewing a candidate for justified structural findings.
- Reducing technical debt within approved scope.
- Reducing complexity flagged by CRAP evidence.
Preconditions
- Establish an existing-suite baseline before solution edits.
- Freeze and protect behavioral verification; do not rewrite it for the
refactor.
- State architecture constraints and the behavior that must remain unchanged.
- Only
@Implementerapplies workflow changes.@Reviewerremains read-only
and reports findings to the orchestrator.
Smell Catalog
| Smell | Indicator |
| --- | --- |
| Long method | Excessive branches, mixed levels, or multiple responsibilities |
| Large class | Multiple independent reasons to change |
| Duplicated logic | The same decision or transformation exists in multiple places |
| Tight coupling | High-level behavior depends directly on replaceable details |
| Mixed abstraction | Policy and low-level mechanics are interleaved |
| Architecture violation | A dependency crosses a prohibited layer boundary |
| Feature envy | Behavior primarily manipulates another owner's data |
| Shallow module | Interface complexity approaches implementation complexity |
| Primitive obsession | Domain concepts lack meaningful types and invariants |
| Speculative generality | Abstraction exists without a current requirement |
Review Method
- Read target files, callers, verification contract, and architecture rules.
- Identify only concrete smells with maintenance or correctness risk.
- Apply the deletion test to shallow modules: retain an abstraction when
removing it concentrates complexity; inline it when removal merely shortens
the call path.
- Report location, evidence, proposed structural outcome, and risk.
- Accept
no changes requiredwhen the candidate is already clear and
cohesive.
Implementation Method
For each approved structural change:
- Make one coherent transformation.
- Keep protected verification artifacts unchanged.
- Run the targeted existing verification.
- Run the stack formatter and static or build checks in the appropriate mode.
- Continue only while behavior and architecture constraints remain protected.
- Run the broader relevant suite before completion.
If a behavioral test must change, stop. The task may be a behavior change, the
test may be coupled to private structure, or the contract may be defective.
Route that decision to the orchestrator instead of silently reclassifying it.
Stack Checks
| Stack | Typical verification | Formatter |
| --- | --- | --- |
| TypeScript core, Node.js SDK, website | Targeted Vitest or build, type check | pnpm format |
| .NET SDK | Targeted and broader dotnet test, build | dotnet format |
| Python SDK | Targeted and broader pytest, mypy | make format-sdk-python |
Constraints
- Never change observable outputs, side effects, compatibility, or domain
invariants.
- Respect Domain -> Application -> Infrastructure dependency rules.
- Do not add tests merely to raise coverage or satisfy a refactoring ritual.
- If important behavior truly lacks evidence, report a separate verification
need; do not expand the frozen refactor contract.
- Do not force a modification after review; no findings is valid.
SOLID Reference
| Principle | Check |
| --- | --- |
| Single Responsibility | Does this unit have one reason to change? |
| Open/Closed | Can required variation be added without unstable branching? |
| Liskov Substitution | Can subtypes replace the abstraction without surprises? |
| Interface Segregation | Do clients depend only on operations they need? |
| Dependency Inversion | Does policy depend on abstractions rather than details? |