LOKI LogoLOKIv1.10.0

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 terminal
loki fix run_20261022_120000 # diagnose a specific run_id
loki fix --apply # interactive: synthesize, apply & verify patch
loki fix --apply --yes # non-interactive batch self-healing
loki fix --apply --no-verify # apply patch without running repro_test.py
loki fix --model openai/gpt-4o --apply
loki 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 in incident.json (crashes) and searches your repository for matching source files (skipping .git, .venv, node_modules, .loki, dist, build). Every candidate path is validated with Path.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 with root_cause, explanation, original_snippet, and replacement_snippet. When testing the bundled playground/index.html without an API key, CodeHealer includes a built-in offline fallback patch.
  • 3. Safety backup (.loki.bak) & replacement (apply_patch): Before modifying the file, CodeHealer creates a byte-for-byte backup at <file>.loki.bak, replaces original_snippet with replacement_snippet (supporting both exact matching and line-trimmed whitespace matching), and prints a colorized unified diff.
  • 4. Deterministic verification (verify_fix via repro_test.py): Unless --no-verify is passed, CodeHealer invokes IncidentReplayer.replay_test() to execute .loki/runs/<run_id>/repro_test.py against the patched target:
FlagTypeDefaultDescription
Exit code 0 (Verified)repro_test.py0 crashesThe reproduction script completed with 0 crashes. The fix is confirmed and <file>.loki.bak is automatically removed.
Exit code 1 (Crash persisted)repro_test.pyrollback[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 errorrepro_test.pykeep patch & backupIf 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).