Skip to content

Fix Loop

Michael L. McKnight edited this page Dec 5, 2026 · 1 revision

Fix Loop

Canonical source: docs/fix-loop.md.

code-cleaner fix (or code_cleaner.fixloop.run_fix_loop) asks an LLM to fix the findings in a file, re-analyzes the answer, and repeats until the file passes or the iteration limit is reached.

iteration 0: analyze the original file
loop:
  findings → JSON feedback → prompt → provider.complete(...)
           → take the largest fenced code block from the reply
           → analyze the new code
until: quality ≥ --min-score and no finding ≥ --fail-on   (pass)
       or --max-iterations rewrites done, or the reply is unchanged, or the provider fails
result: the best version seen (passing first, then fewest blocking findings, then highest score)

A worse rewrite never replaces a better one. The input file is only changed with --in-place.

CLI

code-cleaner fix FILE [--provider openai|mock] [--model M] [--base-url URL] [--api-key-env VAR]
                      [--timeout SECONDS] [--max-iterations N] [--min-score N] [--fail-on SEV]
                      [-o OUT | --in-place] [--report fix.json]
                      [--mock-fixes-per-call N] [analysis options: --no-taint, --disable-rule ID, ...]
Option Default
--provider openai (any OpenAI-compatible endpoint)
--model $CODE_CLEANER_LLM_MODEL, else gpt-4o-mini
--base-url $CODE_CLEANER_LLM_BASE_URL, else https://api.openai.com/v1
--api-key-env reads CODE_CLEANER_LLM_API_KEY, else OPENAI_API_KEY
--max-iterations 3
--min-score 90
--fail-on high
--timeout 120 s per request
output stdout, unless -o or --in-place

Exit code 0 when the best version passes, 1 when it does not, 2 on usage errors. Progress goes to stderr; --report writes the per-iteration history as JSON.

fix sends the whole file and its findings to the endpoint. Use a local model for code you may not share. Review every change before committing it. The API key is read from the environment at call time and is never written to disk or to reports.

Providers

OpenAI-compatible (OpenAICompatibleProvider) POSTs to {base-url}/chat/completions using only the standard library. Without a key, no header is sent (for local servers).

Service --base-url Key variable (example)
OpenAI https://api.openai.com/v1 (default) OPENAI_API_KEY
xAI https://api.x.ai/v1 --api-key-env XAI_API_KEY
Groq https://api.groq.com/openai/v1 --api-key-env GROQ_API_KEY
Ollama (local) http://localhost:11434/v1 none
LM Studio (local) http://localhost:1234/v1 none
vLLM (local) http://localhost:8000/v1 none
export CODE_CLEANER_LLM_API_KEY=...            # or OPENAI_API_KEY
code-cleaner fix app.py --model gpt-4o-mini --max-iterations 3 --min-score 90 -o app_fixed.py --report fix.json
code-cleaner fix app.py --base-url http://localhost:11434/v1 --model qwen2.5-coder

Mock (--provider mock) is offline and deterministic. It applies built-in regex rewrites for common Python findings and is meant for demos and tests, not real fixing. --mock-fixes-per-call N limits how many findings it fixes per round.

You can also plug in your own provider in Python; see docs/fix-loop.md.

Demo (real run, mock provider)

$ code-cleaner fix examples/fixdemo/app.py --provider mock --mock-fixes-per-call 2 --max-iterations 5 -o fixed.py
iteration 0: quality 21.2, security F (0), 10 issue(s), 5 blocking
iteration 1: quality 41.3, security F (40), 8 issue(s), 3 blocking
iteration 2: quality 68.2, security B (85), 6 issue(s), 1 blocking
iteration 3: quality 87.5, security A (100), 4 issue(s), 0 blocking
iteration 4: quality 93.6, security A (100), 2 issue(s), 0 blocking - PASS
stopped: passed; score history [21.2, 41.3, 68.2, 87.5, 93.6]; best iteration 4

The OpenAI-compatible provider is tested offline only (stubbed HTTP, and a local fake server). It was not run against a hosted LLM. LLM output quality depends on the model; always review fixes.

Clone this wiki locally