Hooks That Remember: Post-Compact Recovery and Safety in Loa

Hooks That Remember: Post-Compact Recovery and Safety in Loa

Our repos run on Loa, and a Loa session that runs long enough hits compaction. The context window fills, the harness summarizes it, and the agent continues with a compressed memory of what it was doing. Most of the time the summary is enough. Some of the time it is not. A sprint plan was mid-execution, a bridge loop was iterating, a simstim run was between phases, and the summary kept the gist but dropped the pointer. The agent then does what agents do with a vague memory. It starts over, or it guesses.

The ledger lists this exposure twice; the season was over before it began.
The ledger lists this exposure twice; the season was over before it began.

Loa's answer is not a better summary. It is a set of hooks that run in the harness, outside the model, and that do not depend on the model remembering anything. This post walks through the hooks reference that ships with Loa and says what we think each piece is for.

A note pinned to the door before the lights go out.

Recovery after compaction

The post-compact recovery hooks arrived in Loa v1.28.0 and use two of the events that Claude Code hooks expose. The design is small enough to state in three steps.

  1. A PreCompact hook fires before the harness compacts and writes a marker to .run/compact-pending.
  2. A UserPromptSubmit hook runs on the next prompt, sees the marker, and injects a recovery reminder into the conversation.
  3. Delivery is one-shot. The reminder appears once, and the marker is deleted.

The reminder is not the state. It is a checklist that points at where the state lives on disk. Re-read CLAUDE.md for conventions. Check .run/sprint-plan-state.json and resume if the state is RUNNING. Check .run/bridge-state.json and resume if the state is ITERATING or FINALIZING. Check .run/simstim-state.json and resume from the last phase. Then review the notes file under grimoires/loa/ for learnings the previous context had accumulated.

That split matters. The context window is the one part of the system we know will be lossy, so Loa puts nothing load-bearing in it. State files hold the state. The hook holds a pointer. The reminder is the pointer being handed back after the window was wiped. If the marker mechanism fails, the state files are still there, and a later session or a person can read them.

Installing the hooks means merging Loa's settings.hooks.json into the user-level Claude Code settings file. The scripts live under .claude/hooks/ in the repo. Both entries use an empty matcher, so they fire on every compaction and every prompt.

Blocking the irreversible

The safety hooks came in v1.37.0. The first is a PreToolUse hook on Bash that blocks a short list of commands we would rather an agent not run unsupervised: rm -rf, git push --force, git reset --hard, and git clean -f. Each block comes with an actionable alternative rather than a bare refusal, and that is the part we care about most. A refusal with no path forward teaches an agent to work around the guard. A refusal that names the safer command teaches it the safer command.

The commands on the list share a property. They remove work instead of producing it. A force push rewrites remote history. A hard reset throws away uncommitted changes. The others delete files outright. An agent that has just been compacted and is working from a summary is exactly the agent most likely to reach for one of these to clean up a state it no longer understands. The recovery hook and the blocking hook are two halves of one concern.

Fencing teammates

Loa v1.39.0 added Agent Teams mode, in which a lead agent coordinates teammates, and with it a set of role guards. All of them key off one environment variable, LOA_TEAM_MEMBER. When it is unset, each guard is a complete no-op. When it is set, the guards enforce lead-only constraints mechanically.

  • The Bash guard blocks br commands, overwrites of .run/*.json, and git commit or git push for teammates.
  • The Write and Edit guard blocks writes to the System Zone under .claude/ and to top-level state files, while allowing writes to teammate-owned paths such as .run/bugs/*/, grimoires/, and app/.
  • The Skill guard enforces the Skill Invocation Matrix by blocklist, refusing lead-only skills such as /plan-and-analyze, /architect, /sprint-plan, /simstim, and /run-bridge when a teammate invokes them.

Each guard is documented as fail-open. If the guard script itself errors, the tool call proceeds. We read this as a choice about which failure to prefer. A fail-closed guard that breaks on an edge case halts the whole team until someone debugs a shell script. A fail-open guard that breaks lets one bad call through, where the audit log can record it. Loa chose the second and layered guards across Bash, Write and Edit, and Skill so that a gap in one is likely covered by another. The reference calls this defense in depth, and here the phrase is earned.

What the guards protect is not application code but the state files and planning skills. A teammate that overwrites .run/sprint-plan-state.json does not break a build. It breaks the lead's ability to know what the team is doing. Guarding the coordination layer is the right instinct for a multi-agent setup.

Stopping with context

There is also a Stop hook, the Run Mode Guard. It detects an active /run, /run-bridge, or /simstim execution and injects a context reminder before the agent stops. This is the compaction problem seen from a different angle. An agent that stops mid-run because it lost track of being mid-run looks, from outside, like one that finished. The hook makes the difference visible at the moment it matters.

Writing it all down

Two PostToolUse hooks produce the audit trail. The mutation logger watches Bash and records mutating commands, including git, npm, rm, and mv, to .run/audit.jsonl. The write mutation logger, added in v1.39.0, does the same for the Write and Edit tools, capturing the file path, tool name, team identity, and timestamp. It does not log file content, for both privacy and size reasons. Together they are meant to ensure that a file modification shows up in the trail whether it came through a shell command or a direct edit.

The format is JSON Lines, one record per line. That choice is boring in the best way. A log that can be tailed, grepped, and parsed one line at a time is a log that gets read.

The reference also ships a template of deny rules for credential protection. These are file-access rules that keep the agent away from credential files under the home directory. We will not reproduce the list here. The principle is the same as everything above. Decide in configuration, not in the prompt, what the agent may touch.

What we take from it

The pattern across all of these is that governance lives in the harness. A hook is a shell script the harness runs at a fixed point in the tool loop. It does not care whether the model remembers the rule, whether the summary preserved it, or whether a teammate read the matrix. Marker files, state files, blocklists, and line-oriented logs are old tools. Loa's contribution is wiring them to the points where an agent is most likely to lose the thread: just after compaction, just before an irreversible command, just before stopping, and at each mutation.

We do not have numbers on how often compaction fires in our repos or how often the recovery reminder changes what the agent does next. The hooks reference does not give them, and we will not estimate. What we can say is that the mechanism is legible. Each piece of it can be read as a script in the Loa repository, and the failures it is built to catch are ones any long-running agent session will recognize.


Get the next post

Free membership: new posts on how the fleet is built, delivered by email. Subscribe free

№ 5019