# generate_refactoring_code

generate_refactoring_code turns one refactoring plan from get_health into generated code and a unified diff, grounded on the plan and its real source.

`get_health(include=["refactoring"])` returns ranked, structured
refactoring plans, not template strings. `generate_refactoring_code`
takes one plan's `id` and turns it into actual generated code: for
Extract Class, the result includes an LCOM4 before/after self-check.
The plans come from [Refactoring intelligence](https://docs.repowise.dev/intelligence/refactoring);
the rest of the tool surface is on [MCP tools](https://docs.repowise.dev/mcp/overview).

## Turning it on

This is an **opt-in** tool: it is not registered by default in any mode.
Turn it on per repo:

```yaml
# .repowise/config.yaml
mcp:
  tools: ["+generate_refactoring_code"]
```

Code generation itself is on unless the repo turns it off with
`refactoring.llm.enabled: false`. Then the call still returns the plan,
with `generation: {available: false, reason: "disabled"}`.

When enabled, it uses the repo's configured LLM provider/model (bring
your own key) and caches results by a content hash, so an unchanged plan
never regenerates.

## When to call

- **After `get_health(include=["refactoring"])`** surfaces a plan you
  want turned into an applyable diff.
- **Only with a configured LLM provider**: without one the call returns
  `error: "no_provider"`. An unknown id returns `error: "not_found"`.

## Parameters

| Name | Type | Default | Description |
|---|---|---|---|
| `suggestion_id` | string |  | Required. The id of a plan returned by get_health(include=["refactoring"]). |
| `repo` | string |  | Repository alias. Usually omitted. |

## Example

```python
get_health(targets=["module:src.api"], include=["refactoring"])
# -> pick a plan's `id`

generate_refactoring_code(suggestion_id="a1b2c3d4")
```

## Things worth knowing

- **Content-hash cached.** Re-running with the same plan against
  unchanged source returns the cached result instead of a fresh LLM call.
- **Grounded, not templated.** The generation is anchored on the plan's
  evidence plus the real source spans it references, not a generic
  rewrite prompt.
- **Extract Class gets a self-check.** The result includes an LCOM4
  cohesion score before and after, so you can see whether the split
  actually improved cohesion.
- **BYO key.** Uses whichever LLM provider/model the repo already has
  configured for doc generation, so no separate credential.

> **Note**
>
> See [`get_health`](https://docs.repowise.dev/mcp/get-health) for how refactoring plans are
> ranked and what evidence each one carries, and
> [Refactoring intelligence](https://docs.repowise.dev/intelligence/refactoring) for the full
> plan shapes (Extract Class, Extract Helper, Move Method, Break Cycle).
