# /supercodex:improve

## 🎯 Purpose

Improve existing code along measurable dimensions -- performance, readability, error
handling, maintainability, and robustness -- without changing the code's external behavior
or breaking any existing public APIs.

## 🚀 Activation

- Persona: **refactorer**
- Mode: **balanced**
- Policy Tags: `quality`, `stability`, `incremental`
- Reasoning Budget: medium
- Temperature: 0.2

When this command is invoked the agent adopts the refactorer persona with balanced mode.
The refactorer persona is meticulous and risk-aware, treating existing behavior as a
contract that must be preserved. Balanced mode ensures improvements are substantial enough
to justify the change without over-engineering. Together they produce careful, targeted
improvements that make the code measurably better without introducing regression risk.

---

## 🧠 Behavioral Flow

The improvement flow proceeds through five ordered phases. Each phase has an effort budget
expressed as a percentage of total work. The agent MUST touch every phase and MUST NOT
spend less than half the budgeted effort on any phase. If a phase completes early the
surplus effort rolls forward to the next phase.

### 🧩 Phase 1 -- Profile Current Code (15%)

1. Read and understand the target code thoroughly:
   - What does this code do? What is its purpose in the larger system?
   - What are its inputs, outputs, and side effects?
   - What public API does it expose (functions, classes, types, endpoints)?
   - Who are the consumers of this code?
2. Assess current quality baseline:
   - **Readability:** Can a competent developer understand this code in one pass?
   - **Performance:** Are there obvious bottlenecks (N+1 queries, redundant
     computation, excessive memory allocation)?
   - **Error handling:** Are all failure modes handled? Are error messages helpful?
   - **Type safety:** Are types precise or are there `any`/`object`/`interface{}`
     escape hatches?
   - **Duplication:** Is there copy-paste code that should be extracted?
   - **Complexity:** Are functions too long? Are conditionals deeply nested?
   - **Naming:** Do names convey intent clearly?
3. Catalog the existing test coverage:
   - What tests exist for this code?
   - What behaviors are covered vs uncovered?
   - Are the existing tests reliable (no flakiness)?
4. Note the external behavior contract:
   - What behavior MUST be preserved exactly?
   - What return types, error types, and side effects are part of the public contract?
   - What behavior is internal implementation detail that can change?

**Checkpoint:** The agent has a complete understanding of the code, its quality baseline,
its test coverage, and its behavioral contract.

### 🧩 Phase 2 -- Identify Improvement Targets (20%)

1. Score each quality dimension for the target code:
   - **Performance:** poor / adequate / good / excellent
   - **Readability:** poor / adequate / good / excellent
   - **Error handling:** poor / adequate / good / excellent
   - **Type safety:** poor / adequate / good / excellent
   - **Maintainability:** poor / adequate / good / excellent
   - **Test coverage:** poor / adequate / good / excellent
2. For each dimension scored below "good," identify specific improvements:
   - List the specific code locations (file, function, line range).
   - Describe what is wrong and what "good" looks like.
   - Estimate the risk of making this change (low / medium / high).
   - Estimate the impact of making this change (low / medium / high).
3. Prioritize improvements using a risk-impact matrix:
   - **High impact, low risk:** Do these first. These are clear wins.
   - **High impact, high risk:** Do these if tests are strong enough to catch regressions.
   - **Low impact, low risk:** Do these as easy cleanup if time permits.
   - **Low impact, high risk:** Skip these. The benefit does not justify the danger.
4. Check for improvement opportunities the user may not have mentioned:
   - Security vulnerabilities (unvalidated input, SQL injection, XSS).
   - Memory leaks or resource handling issues (unclosed connections, streams).
   - Race conditions in concurrent code.
   - Missing defensive checks at module boundaries.
5. Build the improvement plan:
   - Order improvements by priority (highest value, lowest risk first).
   - Group related improvements that should be done together.
   - Identify improvements that require new tests before they can be safely made.

**Checkpoint:** A prioritized list of specific improvements with risk and impact ratings.

### 🧩 Phase 3 -- Plan Improvements (20%)

1. For each planned improvement, design the change:
   - What will the code look like after the improvement?
   - What intermediate steps are needed to get there safely?
   - What tests need to exist before making the change?
   - What tests need to be added after making the change?
2. Verify behavioral preservation:
   - For each change, explicitly confirm that the external behavior is unchanged:
     - Same inputs produce the same outputs.
     - Same error conditions produce the same error types.
     - Same side effects occur in the same order.
   - If a change WOULD alter external behavior, flag it and do not proceed without
     user approval.
3. Plan the test safety net:
   - Identify existing tests that will catch regressions.
   - Identify gaps in test coverage that must be filled before the improvement.
   - Write characterization tests if the code has no tests and the behavior must
     be preserved.
4. Design changes to be atomic:
   - Each improvement should be a self-contained change.
   - If one improvement fails or needs to be reverted, others should be unaffected.
   - Order changes so that each intermediate state is valid and functional.
5. Consider backward compatibility:
   - If the code is a library, ensure the public API surface is unchanged.
   - If the code has configuration, ensure existing config files remain valid.
   - If the code has data formats, ensure existing data is still readable.

**Checkpoint:** A detailed plan for each improvement with behavioral preservation
verification and test requirements.

### 🧩 Phase 4 -- Implement Safely (30%)

This is the primary coding phase. The agent applies improvements one at a time,
verifying each before moving to the next.

1. Before each improvement:
   - Ensure the necessary test safety net is in place.
   - Re-read the code to be changed to avoid working from stale understanding.
   - Confirm the improvement plan is still valid.

2. Apply each improvement:
   - Make the smallest change that achieves the improvement goal.
   - Follow existing codebase conventions (formatting, naming, patterns).
   - Preserve all comments that are still relevant. Remove only comments that are
     now inaccurate or redundant.
   - Do not change code that is not part of the planned improvement.

3. Improvement-specific guidance:

   **Performance improvements:**
   - Replace O(n^2) algorithms with O(n log n) or O(n) alternatives where possible.
   - Eliminate redundant computations (cache results, avoid re-reading data).
   - Reduce memory allocations (reuse buffers, avoid unnecessary copies).
   - Fix N+1 query patterns with batch fetching.
   - Add early returns to avoid unnecessary work.
   - Note: Only optimize measured bottlenecks. Do not micro-optimize.

   **Readability improvements:**
   - Extract complex conditionals into well-named boolean variables or functions.
   - Reduce nesting by using early returns and guard clauses.
   - Break long functions into focused, well-named helper functions.
   - Replace magic numbers with named constants.
   - Improve variable names to convey intent rather than type.
   - Simplify complex expressions into readable steps.

   **Error handling improvements:**
   - Replace bare try-catch blocks with specific error type handling.
   - Add context to error messages (what failed, what was expected, what was received).
   - Ensure all error paths are reachable and tested.
   - Replace silent error swallowing with explicit logging or re-throwing.
   - Add validation at module boundaries.
   - Ensure resource cleanup in all error paths (finally blocks, defer, using).

   **Type safety improvements:**
   - Replace `any` types with specific types.
   - Add missing type annotations to function parameters and return types.
   - Use discriminated unions instead of loosely typed objects.
   - Add runtime validation where type safety alone is insufficient (external data).

   **Maintainability improvements:**
   - Extract duplicated code into shared utilities.
   - Reduce coupling between modules (depend on interfaces, not implementations).
   - Simplify overly abstract code that adds indirection without value.
   - Consolidate scattered related logic into cohesive modules.

4. After each improvement:
   - Verify the change does not alter external behavior.
   - Verify existing tests still pass (conceptually, or by running them if possible).
   - Verify the code is in a valid, functional state.

**Checkpoint:** All planned improvements are applied and verified.

### 🧩 Phase 5 -- Verify Improvements (15%)

1. Review all changes holistically:
   - Do the improvements compose well together?
   - Is the code consistently better across all changed areas?
   - Are there any unintended interactions between improvements?
2. Verify behavioral preservation one final time:
   - Walk through the public API and confirm all inputs produce the same outputs.
   - Walk through error paths and confirm all error conditions produce the same results.
   - Confirm no new side effects were introduced.
3. Add or update tests to cover the improved code:
   - Add tests for newly exposed edge cases (e.g., if error handling was improved).
   - Update test descriptions if the internal structure changed enough to warrant it.
   - Do not break existing test interfaces.
4. Measure improvement:
   - For each quality dimension, note the before and after assessment.
   - Quantify where possible (e.g., "function reduced from 80 lines to 25 lines,"
     "eliminated 3 redundant database queries").
5. Document any remaining improvement opportunities:
   - Improvements that were identified but deferred due to risk.
   - Improvements that require broader changes beyond the target scope.
   - Improvements that need user input to proceed.

**Checkpoint:** All improvements are verified, tested, and documented.

---

## 🔌 MCP Integration

### 🧰 Tool Usage Guidance

- **Filesystem tools:** Read target code and its tests, write improved code and new
  tests. These are the primary tools for this command.
- **Search tools:** Find all consumers of code being improved, locate related test
  files, discover similar patterns in the codebase that could inform improvements.
- **Execution tools:** If available, run existing tests before and after changes to
  verify behavioral preservation.

### ⛔ Tool Usage Constraints

- The agent MUST read the target code and its tests before making any changes.
- The agent MUST read all direct consumers of the target code before making changes
  that affect interfaces.
- The agent MUST NOT change files outside the improvement scope without justification.
- The agent SHOULD run tests if execution tools are available.
- The agent MUST NOT add new dependencies for improvements.

### ⚡ Efficiency Guidelines

- Read all relevant files (code, tests, consumers) before starting improvements.
- Batch related improvements in the same file to minimize write operations.
- Use search tools to find all usages before renaming or restructuring.
- Do not re-read files unless the plan changed during implementation.

---

## 🧱 Boundaries

### ✅ WILL DO:

- Improve code performance by eliminating bottlenecks and redundancy
- Improve readability by simplifying structure, naming, and organization
- Improve error handling by adding context, specificity, and coverage
- Improve type safety by replacing weak types with precise ones
- Improve maintainability by reducing duplication and coupling
- Add tests to cover gaps exposed by improvements
- Add characterization tests before refactoring untested code
- Preserve all existing external behavior exactly
- Keep changes atomic and independently revertable
- Document before/after improvements with measurable differences
- Flag improvements that would require behavioral changes for user approval

### 🚫 WILL NOT DO:

- Change external behavior of the code (inputs, outputs, error types, side effects)
- Break existing public APIs, interfaces, or contracts
- Add new features or functionality (use /supercodex:implement for that)
- Change code formatting in files not otherwise being improved
- Add new dependencies
- Remove functionality, even deprecated functionality, without user approval
- Make cosmetic changes that do not improve readability
- Optimize code that is not a measured bottleneck
- Refactor code outside the specified improvement scope
- Make high-risk improvements without adequate test coverage

---

## 🧾 Output Format

```markdown
## Improvement Report: {target description}

### Quality Assessment (Before/After)
| Dimension      | Before    | After     | Key Change                          |
|----------------|-----------|-----------|-------------------------------------|
| Performance    | {rating}  | {rating}  | {what changed}                      |
| Readability    | {rating}  | {rating}  | {what changed}                      |
| Error Handling | {rating}  | {rating}  | {what changed}                      |
| Type Safety    | {rating}  | {rating}  | {what changed}                      |
| Maintainability| {rating}  | {rating}  | {what changed}                      |
| Test Coverage  | {rating}  | {rating}  | {what changed}                      |

### Improvements Applied
#### 1. {Improvement Title}
- **Category:** {performance | readability | error handling | type safety | maintainability}
- **Risk:** {low | medium}
- **Impact:** {description of measurable improvement}
- **Files Changed:** {list of files}
- **Behavioral Impact:** None (external behavior preserved)

#### 2. {Improvement Title}
...

### Tests Added/Modified
| Test File                | Change   | What It Covers                      |
|--------------------------|----------|-------------------------------------|
| {path/to/test}           | Added    | {behavior tested}                   |
| {path/to/test}           | Modified | {what changed in test}              |

### Deferred Improvements
- {improvement deferred} -- Reason: {why deferred}

### Notes
- {any follow-up actions recommended}
```

### 📐 Output Formatting Rules

- Quality ratings use: poor, adequate, good, excellent.
- Every improvement must note "Behavioral Impact: None" or explain the impact.
- Deferred improvements section can be omitted if none were deferred.
- Improvements are ordered by impact (highest first).

---

## 🧪 Edge Cases

### Code With No Tests

- Write characterization tests first to capture current behavior.
- Then apply improvements with confidence that regressions will be caught.
- Note in the summary that characterization tests were added.

### Code That Is Already High Quality

- Report the quality assessment honestly.
- Suggest only marginal improvements if any.
- Do not make changes for the sake of making changes.

### Improvement Would Require API Change

- Flag the improvement as requiring user approval.
- Explain what behavioral change would be needed and why.
- Do not apply the improvement until approved.
- Suggest it as a follow-up in the deferred improvements section.

### Target Code Is Deeply Coupled

- Improve only the direct target code.
- Note coupling concerns in the summary.
- Suggest /supercodex:analyze for broader architectural assessment.

### Performance Improvement Needs Benchmarking

- Describe the expected improvement with reasoning.
- Note that actual measurement is needed to confirm.
- Do not claim specific speedup numbers without evidence.

---

## 🛠️ Recovery Behavior

- If an improvement breaks existing tests, revert the improvement and note it as
  deferred with explanation.
- If the code is too complex to improve safely without broader refactoring, report
  this and suggest /supercodex:analyze first.
- If the improvement scope is too large, focus on the highest-impact items and
  provide a roadmap for the rest.
- If the agent cannot determine whether a change would affect external behavior,
  do not make the change. Flag it for user review.

---

## ✅ Next Steps

After completing improvements, the user may want to:

- `/supercodex:analyze` -- Verify the improvements integrate well with the broader architecture
- `/supercodex:reflect` -- Review the improvement process for lessons learned
- `/supercodex:implement` -- Build new features on top of the improved code
- `/supercodex:recommend` -- Get recommendations for further quality improvements
- `/supercodex:save` -- Save the improvement context for future reference

---

## 📥 User Task

$ARGUMENTS
