Skip to main content
Glama
README.md
# AutoFixAgent

**IDE-grade automated refactoring agent — detect once, fix everywhere, deterministic-first.**

AutoFixAgent receives a request like `detect("UnnecessaryCast")`, finds (or authors and caches) the
cheapest capable detector and fixer, returns occurrences, and applies IntelliJ-grade deterministic
fixes — verifying each batch compiles and passes tests before reporting success. AI writes the
algorithms once; the cached algorithms do the rest.

```text
  OpenCode / any MCP client / Gradle plugin / GitHub Action
                        │  MCP (stdio) · CLI
  ┌─────────────────────▼─────────────────────────────────┐
  │  Core (FastMCP + asyncio)                             │
  │  MCP tools · CLI verbs · Job manager                  │
  │  ┌──────────────┐   ┌───────────────────────────┐     │
  │  │ Rule registry│   │ AI author layer           │     │
  │  │ + artifact   │   │ (synthesize · self-verify)│     │
  │  │ cache        │   └───────────────────────────┘     │
  │  └──────┬───────┘                                     │
  │  ┌──────▼───────────────────────────────────────┐     │
  │  │ Engines: OpenRewrite (Java) · ast-grep       │     │
  │  │ (JS/TS +) · ts-morph sidecar · ESLint ·      │     │
  │  │ Joern (data flow, call graph)                │     │
  │  └──────────────────────────────────────────────┘     │
  │  Verification gate (worktree + build + test)          │
  │  Workspace Profile (derived + .autofix/config.yaml)   │
  │  Toolchain cache (~/.cache/autofixagent/tools)        │
  └───────────────────────────────────────────────────────┘
```

## Why

IntelliJ can fix `Unnecessary cast` in one keystroke; CI tooling reports the same finding a
thousand times and waits for humans. AutoFixAgent closes that gap:

- **Cache-first**: the detect/fix algorithm for a rule is authored once (AI-assisted), verified
  against golden samples, and cached keyed by `(rule, language, engine, engine_version)`. The
  second request is instant and deterministic.
- **Deterministic-first**: engines run before the LLM. Occurrences are tagged
  `deterministic | AI_ONLY`; only the latter ever need a model.
- **Safe by construction**: fixes apply on a branch in a temp git worktree, pass a
  compile/test gate, and come back as a diff with per-occurrence verdicts. The agent never
  mutates the caller's checkout and never installs anything into the target.

## Quickstart

```bash
pip install git+https://github.com/sleroy/autofix-agent.git

autofix-agent doctor -w /path/to/repo          # toolchain + workspace profile check
autofix-agent detect UseDiamondOperator -w /path/to/repo
autofix-agent fix UseDiamondOperator -w /path/to/repo --apply
```

MCP (OpenCode / any MCP client):

```json
{
  "mcpServers": {
    "autofix-agent": {
      "command": "autofix-agent",
      "args": ["mcp"]
    }
  }
}
```

Tools: `detect`, `fix`, `fix_batch`, `job_status`, `rules`, `explain`, `doctor`.
See [docs/mcp.md](docs/mcp.md) for the contract.

## Engines

Selection rule: **the best engine per language** — technologies multiply on purpose.

| Language | Detect | Fix | Deep analysis |
|---|---|---|---|
| Java | OpenRewrite search recipes, PMD/Checkstyle | OpenRewrite recipes (Maven CLI goal / Gradle init-script — build files never touched), JavaParser `LexicalPreservingPrinter` for custom fixers | Joern (data flow, call graph) |
| JS/TS | ast-grep YAML rules, ESLint | ast-grep `fix` (node-range splice), ts-morph sidecar, ESLint `--fix` | Joern |
| Python | ast-grep / tree-sitter | **LibCST** (lossless concrete tree, codemod framework) | — |
| Others (Go, Kotlin, Rust, ...) | ast-grep (tree-sitter grammars) | ast-grep `fix` | — |

**Range-edit contract**: every fix — engine or AI-authored — is expressed as ranged edits
`(start, end, replacement)` spliced into the original text. Full-file rewrites are forbidden;
comments, formatting and blame survive. See `src/autofixagent/edits.py`.

All engines are pinned and checksummed in `~/.cache/autofixagent/tools` (see
`autofix-agent doctor`, air-gap tarball supported).

## Status

M0 — scaffold. Roadmap: M1 CLI detect+fix (ast-grep + OpenRewrite) → M2 MCP server →
M3 verification gate → M4 AI author layer → M5 Gradle plugin → M6 GitHub Action →
M7 Joern data-flow rules → M8 concurrency hardening.

## License

[Apache-2.0](LICENSE)