> ## Documentation Index
> Fetch the complete documentation index at: https://gump.build/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Refactor

> Change the structure, keep the behavior. Tests stay green at every step.

# Refactor

Change the structure, keep the behavior.

## When to use

For refactorings — renames, module extractions, restructuring — where the contract is: behavior doesn't change. Existing tests must stay green after every task.

```bash theme={null}
gump run refactor --spec refactor-spec.md
```

## The workflow

```yaml theme={null}
name: refactor
max_budget: 5.00

steps:
  - name: decompose
    type: split
    get:
      prompt: |
        Plan refactoring for: {spec}
        Ensure each task is independently testable.
        All existing tests must stay green after each task.
    run:
      agent: claude-sonnet
    gate: [schema]
    each:
      - name: apply
        type: code
        get:
          prompt: |
            Refactor according to: {task.description}
            Only modify: {task.files}
            All existing tests must continue to pass.
        run:
          agent: claude-haiku
          guard:
            max_turns: 60
        gate: [compile, test]
        retry:
          - attempt: 3
            agent: claude-sonnet
          - exit: 4

  - name: quality
    gate: [compile, lint, test]
```

## How it works

Each task is implemented sequentially (no parallelism — refactors often have inter-task dependencies). The compile + test gate after each task verifies nothing is broken.

## Typical metrics

Similar to cheap2sota. Average cost: $0.80–$2.00 for 3–5 tasks. Duration: 1–3 minutes.

## Customize

<AccordionGroup>
  <Accordion title="Verify coverage didn't drop">
    Add `coverage: 80` to the `quality` gate.
  </Accordion>

  <Accordion title="Parallelize independent tasks">
    Add `parallel: true` on the decompose step if tasks are strictly independent.
  </Accordion>

  <Accordion title="Guide toward a target architecture">
    Add `context: [file: "docs/target-architecture.md"]` to the apply step.
  </Accordion>
</AccordionGroup>
