Auto-Healing (`CodeHealer`)
Synthesize, apply, and deterministically verify surgical code patches with .loki.bak backups and automatic rollback.
Diagnose & patch (`loki fix` and `--auto-heal`)
bash
loki fix # diagnose latest incident in terminalloki fix run_20261022_120000 # diagnose a specific run_idloki fix --apply # interactive: synthesize, apply & verify patchloki fix --apply --yes # non-interactive batch self-healingloki fix --apply --no-verify # apply patch without running repro_test.pyloki fix --model openai/gpt-4o --applyloki run --auto-heal # trigger closed-loop auto-healing right after a failing run
Complete `CodeHealer` Workflow
- 1. Source file resolution (
resolve_source_file): Parses URLs and stack traces inincident.json(crashes) and searches your repository for matching source files (skipping.git,.venv,node_modules,.loki,dist,build). Every candidate path is validated withPath.resolve().relative_to(workspace_root)to prevent path traversal outside the repository. - 2. Surgical patch synthesis (
synthesize_patch): Sends the source file content and crash logs to the configured LiteLLM model requesting a strict JSON object withroot_cause,explanation,original_snippet, andreplacement_snippet. When testing the bundledplayground/index.htmlwithout an API key,CodeHealerincludes a built-in offline fallback patch. - 3. Safety backup (
.loki.bak) & replacement (apply_patch): Before modifying the file,CodeHealercreates a byte-for-byte backup at<file>.loki.bak, replacesoriginal_snippetwithreplacement_snippet(supporting both exact matching and line-trimmed whitespace matching), and prints a colorized unified diff. - 4. Deterministic verification (
verify_fixviarepro_test.py): Unless--no-verifyis passed,CodeHealerinvokesIncidentReplayer.replay_test()to execute.loki/runs/<run_id>/repro_test.pyagainst the patched target:
| Flag | Type | Default | Description |
|---|---|---|---|
| Exit code 0 (Verified) | repro_test.py | 0 crashes | The reproduction script completed with 0 crashes. The fix is confirmed and <file>.loki.bak is automatically removed. |
| Exit code 1 (Crash persisted) | repro_test.py | rollback | [REPRODUCED] appears in output (the crash still happens). CodeHealer.rollback() automatically restores the original file from <file>.loki.bak and removes the backup. |
| Harness / Connection error | repro_test.py | keep patch & backup | If repro_test.py fails due to a harness issue (e.g. target server not running), LOKI keeps both the applied patch and <file>.loki.bak so you can verify or restore manually. |
You can run loki replay [run_id] (or loki replay --video) manually at any time to execute .loki/runs/<run_id>/repro_test.py. Note that loki replay takes an optional run_id, not --seed <n> (--seed is a loki run flag).
