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

# fix

> Preview and apply safe, diff-first auto-fixes for supported Charter findings.

`charter fix` is Charter's repair command. It is intentionally narrow: it only applies safe, explicit fixes to a small set of supported rules, always shows you a unified diff before writing anything, and backs up existing files before replacing them.

**The guarantee: Charter always shows you a diff before writing. No silent mutation.**

<Frame caption="charter fix --dry-run — unified diff preview before any file is written">
  <img src="https://mintcdn.com/tashfiq/uHoRNuLkLShRQ253/images/screenshots/fix-dry-run.webp?fit=max&auto=format&n=uHoRNuLkLShRQ253&q=85&s=d0ff2dbeeb312762106ee20f67cff847" alt="charter fix dry run showing unified diff" width="1728" height="2010" data-path="images/screenshots/fix-dry-run.webp" />
</Frame>

## Fixable Rules

| Rule                              | What the fixer does                                                 |
| --------------------------------- | ------------------------------------------------------------------- |
| [`AE-CTX-001`](/rules/AE-CTX-001) | Creates `AGENTS.md` from a minimal template                         |
| [`AE-CTX-004`](/rules/AE-CTX-004) | Creates or appends `.gitignore` with agent artifact patterns        |
| [`AE-CI-002`](/rules/AE-CI-002)   | Creates the Charter CI workflow at `.github/workflows/charter.yaml` |
| [`AE-MCP-001`](/rules/AE-MCP-001) | Bumps an MCP server package to the catalog's stable version         |

<Warning>
  Secrets ([`AE-SEC-001`](/rules/AE-SEC-001), [`AE-SEC-002`](/rules/AE-SEC-002)) and dangerous-command findings ([`AE-CC-001`](/rules/AE-CC-001)) are never auto-fixed. Charter shows evidence and remediation guidance for these but requires manual action. If you target a rule with no registered fixer, Charter reports that it is not auto-fixable.
</Warning>

## Usage

<CodeGroup>
  ```bash Dry run — preview all diffs theme={null}
  charter fix --dry-run
  ```

  ```bash Apply all fixes theme={null}
  charter fix
  ```

  ```bash Fix a specific rule theme={null}
  charter fix --rule AE-MCP-001
  ```

  ```bash Preview a specific rule theme={null}
  charter fix --rule AE-CTX-004 --dry-run
  ```
</CodeGroup>

## Recommended Workflow

<Steps>
  <Step title="Preview every diff">
    ```bash theme={null}
    charter fix --dry-run
    ```

    Read each unified diff carefully before accepting it. The dry-run output is identical to what `fix` would apply.
  </Step>

  <Step title="Apply the fixes">
    ```bash theme={null}
    charter fix
    ```

    Originals are backed up to `.charter/backups/<ts>/` before any write. Pure creates (new files) have no prior content to back up.
  </Step>

  <Step title="Verify the score improved">
    ```bash theme={null}
    charter doctor
    ```

    Confirm the fixed rules no longer appear as active findings and the score moved in the expected direction.
  </Step>
</Steps>

## Flags

<ResponseField name="--path" type="string">
  Explicit repository root. Defaults to the current working directory.
</ResponseField>

<ResponseField name="--rule" type="string">
  Limit the repair to a single rule ID such as `AE-MCP-001`. When omitted, all fixable findings are planned.
</ResponseField>

<ResponseField name="--dry-run" type="boolean">
  Print unified diffs without writing any files. Safe to run repeatedly.
</ResponseField>

<ResponseField name="--all" type="boolean">
  Plan every fixable rule. This is already the default behavior; the flag is accepted for explicitness.
</ResponseField>

<ResponseField name="--yes" type="boolean">
  Skip per-fix confirmation prompts. Because `fix` is already non-interactive in normal use, this flag is accepted for scripting compatibility.
</ResponseField>

## Backup Mechanism

Before replacing or updating an existing file, Charter copies the previous version into `.charter/backups/<ts>/`, where `<ts>` is a compact UTC timestamp such as `20260610T074200Z`. The original file's relative path is preserved inside the backup directory.

Pure creates — fixes that write a brand new file where none existed — do not produce a backup because there is no prior content to save.

Charter never deletes backup directories automatically. They accumulate across runs and serve as a local audit trail of every mutation `fix` has applied.

To recover a backed-up file:

```bash theme={null}
cp .charter/backups/20260610T074200Z/AGENTS.md ./AGENTS.md
```

## Exit codes

| Code | Meaning | When                                                                              |
| ---- | ------- | --------------------------------------------------------------------------------- |
| `0`  | Pass    | Fixes applied successfully, or a `--dry-run` preview rendered without error       |
| `1`  | Fail    | `--rule` matched a finding that has no registered fixer                           |
| `2`  | Error   | Invalid flags, an unknown rule ID, a write failure, or a malformed `charter.yaml` |

## Related

<CardGroup cols={2}>
  <Card title="Use charter fix safely" icon="shield-check" href="/docs/how-to/use-charter-fix-safely">
    The diff-first workflow and what fix will never touch.
  </Card>

  <Card title="Fix engine" icon="tool" href="/docs/concepts/fix-engine">
    How fixers are registered and how backups are written.
  </Card>

  <Card title="doctor" icon="stethoscope" href="/cli/doctor">
    Re-score the repo to confirm the fixed rules cleared.
  </Card>
</CardGroup>
