A Python code-repair toolkit that combines targeted Tree-sitter edits, model-generated function replacements and test feedback.
The repair CLI produces reviewable patches in disposable Git worktrees. The repository also includes a FastAPI task service demonstrating queued LangGraph workflows, task-scoped events and cancellation.
flowchart LR
Task[Task and source context] --> Model[Configured model provider]
Model --> Candidate[Function replacement]
Candidate --> AST[Tree-sitter and Python validation]
AST --> Worktree[Disposable Git worktree]
Worktree --> Tests[Verification command]
Tests -->|Pass| Diff[Reviewable patch]
Tests -->|Fail, within attempt limit| Model
- Select a unique top-level Python function and replace its byte range.
- Preserve surrounding code, decorators and Unicode content.
- Validate syntax and repository paths before verification.
- Run a developer-supplied test command in a detached worktree.
- Feed verification failures into a bounded repair loop.
- Return the patch, test output and attempt history for review.
The repair CLI leaves the original checkout unchanged. Verification executes code with the runtime's privileges; use an isolated environment for untrusted repositories or generated code.
python -m venv .venv
# Activate the environment, then:
pip install -r requirements.txtpython ast_editor.py --repo /path/to/repo --file example.py --function add --replacement candidate.txtcandidate.txt contains the complete replacement function. The command validates the edit and displays its diff.
Start from a clean, committed repository and configure an OpenAI-compatible model gateway:
python repair_agent.py --repo /path/to/repo --file example.py --function add --task "Fix addition" --base-url http://localhost:11434/v1 --model your-local-model --output result.json --allow-local-execution --verify python -m pytest -qREPAIR_API_KEY supplies an optional gateway credential. The default repair budget is three attempts, configurable up to five. Context and feedback limits bound the information passed between attempts.
The result includes the base revision, proposed replacements, verification output and a successful diff when a candidate passes the supplied checks.
FastAPI, Redis/ARQ and LangGraph provide an asynchronous search-workflow example:
- POST /agent/run submits a task and returns task_id and task_token.
- The worker executes the workflow and publishes task-scoped events.
- Clients receive SSE updates or retrieve the latest task state.
- Clients can request cooperative cancellation.
Status and cancellation use X-Task-Token. SSE connections use task_id and token query parameters. Treat task tokens as access credentials.
docker compose up --buildFor a single-process local demo, set AGENT_DEMO_MODE=1 and REDIS_HOST=localhost, then:
uvicorn main:app --host 127.0.0.1 --port 8000The search workflow service is a separate entry point from the code-repair CLI.
python -m pytest -q
python -m scripts.demo_scenarioThe local suite passes 12 tests covering AST edits, path handling, worktree verification, repair feedback, task access and provider failures. Linux CI additionally verifies queued completion, cancellation and task-scoped events against Redis.
CI results ? Recorded job evidence
Model-repair probes exercise three disclosed tasks with a local model. Their reports retain proposals, test results and diffs for each attempt. Use these as integration checks; representative repair performance requires a broader dataset.
Regenerate the walkthrough with requirements-demo.txt and python scripts/render_demo.py.
Engineering guide ? GPU repair notebook ? Continuation notes
