Recover from a HALTED latch
Diagnose a sandboxed native call supervision or distribution fault (OUT/HALTED or LOCK.HALTED), preserve forensic logs in ledger.jsonl, confirm systemd scope cleanup, and restore execution safely.
First, distinguish a normal proof failure from a supervision fault:
- A failed proof, syntax error, or type mismatch in Lean is a normal refusal, never a supervision fault. When an AI coding agent submits invalid Lean code via
lema add, the sandboxed Lean process exits cleanly with a non-zero exit code,lemaprints thefile:line:colcaret diagnostic, and the server continues accepting requests immediately. - A
HALTEDlatch is written only when a containment, resource, or distribution integrity invariant around a sandboxed call is violated (crates/lema-check/src/lib.rs). Examples include a native call whosesystemduser scope could not be confirmed stopped after termination, a file modified inside the read-only Lean distribution during a call, or an unrecovered crash mid-call.
When a supervision fault occurs, the runtime writes a HALTED latch file and refuses new native calls until an operator inspects the forensic evidence and clears the fault state. While halted, lema serve still answers read-only store queries that make no native calls (lema status, lema info, lema view, lema diff, find, show).
1. Identify which latch or ledger condition blocked execution
Check the two possible latch paths named by your runtime configuration (~/.lema/runtime.json, where OUT is output and LOCK is shared_lock):
cat "$OUT/HALTED" "$LOCK.HALTED" 2>/dev/null| Condition | Path | Scope blocked | Why it was written |
|---|---|---|---|
| Output latch | $OUT/HALTED | Only runtime configs using $OUT | Output-directory supervision fault, incomplete output capture, or Lean distribution drift detected during a call. |
| Host-wide lock latch | $LOCK.HALTED | All runtime configs on the host sharing $LOCK | A lema-native-*.scope systemd user unit could not be confirmed stopped after a call ended, so stray processes could still be running on the host. |
| Unresolved reservation | $OUT/ledger.jsonl | Only runtime configs using $OUT | A "event":"reserve" line in ledger.jsonl has no matching "complete" or "resolved" line (for example, the host lost power or lema serve was killed with SIGKILL mid-call). |
Each HALTED file contains a single line naming the call and fault (<call_name>: <fault_reason>).
2. Inspect the forensic files in $OUT
Before deleting or clearing anything, inspect $OUT/ledger.jsonl and the diagnostic files for the faulted <call_name> so you know what triggered the fault:
# Find any completed calls with a non-null fault:
grep '"event":"complete"' "$OUT/ledger.jsonl" | grep -v '"fault":null'
# Inspect the last entries in the ledger:
tail -n 10 "$OUT/ledger.jsonl"For each native call <name>, $OUT preserves four forensic artifacts:
$OUT/<name>-reserved.json: The exact command line, sandbox parameters, timeout, configuration SHA-256, and start timestamp.$OUT/<name>-result.json: The process ID, exit status, elapsed wall-clock seconds, fault description, log byte count, and completion timestamp.$OUT/<name>.log: Captured standard output and standard error from the sandboxed call.$OUT/work/<name>/: The preserved sandbox working directory for the failed call.
3. Confirm that no stray sandbox processes remain (systemctl --user)
Especially when $LOCK.HALTED is set (or after an unfinished reservation in ledger.jsonl), verify that no lema-native-* systemd user scope is still active on the host:
systemctl --user list-units --all --no-legend --plain 'lema-native-*'If any lema-native-*.scope unit is still active or running, stop it (systemctl --user stop lema-native-....scope) and confirm that all processes inside it have terminated before proceeding. (Note that preflight automatically closes an abandoned reservation only when it can positively confirm that no scope or process remains alive.)
If the fault message reported distribution drift, run lema verify-distribution (Install the pinned Lean distribution) and reinstall the read-only Lean toolchain if any file was altered.
4. Clear the fault and preserve evidence
Once you have confirmed that the stray scope is stopped and the Lean distribution is intact, choose one of two recovery paths:
Option A (Recommended for $OUT/HALTED): Switch to a fresh output directory
The cleanest way to recover from an output-specific latch while keeping the entire forensic directory ($OUT) untouched for post-mortem review is to point your runtime configuration at a new 0700 output directory:
NEW_OUT=$HOME/.lema/evidence-$(date +%Y%m%d-%H%M)
install -d -m 700 "$NEW_OUT"
jq --arg out "$NEW_OUT" '.output = $out' ~/.lema/runtime.json > ~/.lema/runtime.json.tmp
mv ~/.lema/runtime.json.tmp ~/.lema/runtime.json
lema stop && lema startOption B: Resolve the call in place (required for $LOCK.HALTED)
Because a host-wide $LOCK.HALTED latch blocks all configurations sharing native.lock, switching output directories alone will not clear $LOCK.HALTED. After confirming in Step 3 that no lema-native-* unit is running:
- Append an explicit
"resolved"event to$OUT/ledger.jsonlfor the faulted or unfinished<call_name>:Shell printf '{"event":"resolved","name":"%s"}\n' "<call_name>" >> "$OUT/ledger.jsonl" - Remove the latch file(s):
Shell rm -f "$OUT/HALTED" "$LOCK.HALTED"
5. Probe the runtime before resuming (lema check-runtime)
Run lema check-runtime to confirm that the sandboxed checker runs cleanly again:
lema check-runtimethis machine can run the trusted runtimeOnce lema check-runtime exits 0, resume normal lema add, lema test, and lema run operations.
Lema v0.3.2 · Updated 29 October 2026 · Version and compatibility