Skip to content

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, lema prints the file:line:col caret diagnostic, and the server continues accepting requests immediately.
  • A HALTED latch 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 whose systemd user 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):

Shell
cat "$OUT/HALTED" "$LOCK.HALTED" 2>/dev/null
ConditionPathScope blockedWhy it was written
Output latch$OUT/HALTEDOnly runtime configs using $OUTOutput-directory supervision fault, incomplete output capture, or Lean distribution drift detected during a call.
Host-wide lock latch$LOCK.HALTEDAll runtime configs on the host sharing $LOCKA 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.jsonlOnly runtime configs using $OUTA "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:

Shell
# 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:

Shell
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:

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:

Shell
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 start

Option 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:

  1. Append an explicit "resolved" event to $OUT/ledger.jsonl for the faulted or unfinished <call_name>:
    Shell
    printf '{"event":"resolved","name":"%s"}\n' "<call_name>" >> "$OUT/ledger.jsonl"
  2. 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:

Shell
lema check-runtime
Output
this machine can run the trusted runtime

Once 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