Skip to content

Hooks and recall

Terminal window
echo '{"prompt":"why did the checkout deploy roll back"}' |
memhtml hook user-prompt-submit --host claude --repo ~/memhtml

Every hook memhtml integrations install writes is one invocation of memhtml hook <event> --host <host> --repo <root>. There are no shell scripts to maintain and no per-host wrapper: the binary reads the host’s own payload on stdin, runs retrieval or transcript indexing, and writes that host’s own protocol to stdout. So a hook is a thing you can run by hand, which is the whole of this page’s debugging advice.

memhtml hook doesn’t write the envelope, except to answer --help or refuse a usage error. It writes what the host expects, because the host is parsing it, and the envelope on that stream would read as garbage. Its response type is hook.output for the manifest’s sake.

session-start, user-prompt-submit, pre-compact, and session-end are host-neutral names. Each host’s installer maps them onto that host’s own event names, and the hook engine maps the host’s payload back.

Event What it does
session-start Runs recall (--budget, default 3000) and injects a line per memory it returns
user-prompt-submit Runs search on the prompt and injects the top hits (--limit, default 5)
pre-compact Indexes every new or changed transcript under the trace root before compaction
session-end Indexes every new or changed transcript under the trace root

The two recall events inject context. The two indexing events don’t inject anything, and no host reads their stdout anyway: a pre-compaction or session-end hook is an observer. Their value is timing, because a transcript is worth indexing at exactly the moment the host is about to compact it away.

Host session-start per-prompt pre-compact session-end
Claude Code SessionStart, injects UserPromptSubmit, injects PreCompact, indexes, with a trace root SessionEnd, indexes, with a trace root
Codex CLI SessionStart, injects UserPromptSubmit, injects none none
Cursor sessionStart, injects none none none
OpenCode no event; the compaction hook runs it chat.message, injects experimental.session.compacting, injects session.idle, indexes, with a trace root

Five of those cells say none, and each one is a host limit rather than a gap in this build.

Cursor has no per-prompt injection channel. Its beforeSubmitPrompt hook can stop a turn and can pass a message to you, and it has no documented way to add context to the prompt the model reads, so install writes no per-prompt hook for Cursor at all rather than one that runs and is discarded. Mid-turn recall on Cursor is the MCP tools.

Codex and Cursor never get an indexing hook. Codex’s hooks file carries the two injecting events and nothing else, Cursor’s carries sessionStart alone, and neither host writes a transcript in a format @memhtml/traces reads.

Claude Code’s two indexing hooks and OpenCode’s session.idle handler exist only when a trace root is known. Only Claude Code has a default one, ~/.claude, because @memhtml/traces reads Claude Code’s transcript format and no other. MEMHTML_TRACE_ROOT sets a trace root for any host, so exporting it is what arms OpenCode’s indexer. Without one, install writes no indexing hook at all rather than one with nothing to read.

The output protocol differs too, and the engine writes whichever one the --host flag names. Claude Code takes plain text on stdout and puts it in the model’s context. Codex takes hookSpecificOutput.additionalContext JSON, which Claude Code also accepts, so Codex gets the JSON form. Cursor takes {"additional_context": "…"}. The OpenCode plugin calls the binary itself and pushes the text as a message part, so it wants plain text. Read from the vendor docs on 2026-09-13, Codex caps injected context at roughly 2,500 tokens, and --budget doesn’t keep a session-start pack under that, because every memory recall returns gets a line whatever the budget.

An empty answer writes only a newline to stdout, on every host and for every event. That is the whole of what a hook with nothing to add says.

A runtime failure gives an empty answer and exits 0

Section titled “A runtime failure gives an empty answer and exits 0”

This is the rule that matters most, because it’s the one that decides whether you can leave hooks installed. Once its arguments parse, any failure inside memhtml hook writes only that newline to stdout and exits 0. An unreachable store, a missing index, a Bedrock outage, a malformed payload, a timeout: every one of them produces the empty answer and a success exit code.

A hook runs on the critical path of your turn, so once its arguments parse, the only acceptable failure is one you don’t notice. A hook that exited non-zero would surface as a host error on a prompt that had nothing wrong with it, and a hook that printed a diagnostic would put its own error text into the model’s context, where the model would try to act on it.

The cost is that a broken hook is invisible from inside a session, which is why integrations doctor exists and why the by-hand invocation below is the way you check one.

The hook engine gives retrieval 1.5 s and transcript indexing 10 s, counted from when the process has read its payload and opened the store. OpenCode’s plugin also stops every call it makes, the idle indexer included, after 1.5 s of wall clock with node startup counted, so an OpenCode index run is cut off at 1.5 s too, and a backlog too large to read in that time never finishes there; catch it up once with memhtml trace index and the same MEMHTML_TRACE_ROOT. A run that reaches either bound is a failure like any other, so the host gets no context and the turn proceeds without it.

The bounds are what make per-prompt recall affordable. Each prompt spawns one node process, which pays its own startup and store opening on top of the 1.5 s retrieval bound. The ceiling on Claude Code and Codex is the 10 s timeout install writes into each recall hook, and on OpenCode it is the plugin’s 1.5 s for the whole process. --hooks session drops the per-prompt recall when you’d rather not spend it. On Claude Code, Codex, and Cursor that leaves session-start recall alone, so Claude Code also loses its two indexing hooks. On OpenCode it keeps the compaction handler and, with a trace root, the idle indexer, which still runs at the end of every turn. --hooks none writes the MCP entry, block, and skill with no hook at all.

On Claude Code, indexing gets the longer bound because it walks transcript files rather than answering a query, and because it runs at a moment the host is already pausing.

No hook writes a memory. Not the session-start pack, not the per-prompt search, not the indexing events. They read the corpus and they index transcripts, and that’s the whole of what they do.

Two reasons hold that line. A hook that wrote a memory would put a commit on the critical path of a turn, and it would wait on the checkout lock behind every other writer to the store. And deciding what’s durable is a judgment that belongs to an agent reading whole transcripts, not to a hook with 1.5 s and one prompt.

So hooks recall and index, and writing down what a transcript was worth is a later step. An agent reads the indexed transcripts with memhtml trace search and writes the records it decides to keep through a session, where the write bar judges each one. No memhtml command does that step on its own: memhtml curate run and memhtml curate collapse work over the records already in the corpus, not over transcripts. The indexing hooks are what give that agent something to read, which is why they’re worth installing on the host whose transcripts memhtml can read.

Give the hook the payload its host would give it and read what comes back:

Terminal window
echo '{"prompt":"drain the VIP before reverting"}' |
memhtml hook user-prompt-submit --host claude --repo ~/memhtml

That’s plain text, and it’s exactly the bytes Claude Code would put in the model’s context. Change the host to see the same answer in another dialect:

Terminal window
echo '{"prompt":"drain the VIP before reverting"}' |
memhtml hook user-prompt-submit --host codex --repo ~/memhtml
{ "hookSpecificOutput": { "hookEventName": "UserPromptSubmit", "additionalContext": "…" } }

An empty answer means one of two things, and the by-hand run is how you tell them apart. Either retrieval found nothing worth injecting, or something failed and the fail-open rule swallowed it. Run the same query through memhtml search to find out which:

Terminal window
memhtml search "drain the VIP before reverting" --dense

A search that answers while the hook is silent means the failure is in the hook’s own arguments, usually the store path. The installed hook pins --repo to an absolute store root, so check that the root you pass by hand is the same one.

The session-start pack takes no prompt, so the payload can be a bare object:

Terminal window
echo '{"cwd":"/home/you/work/checkout-api"}' |
memhtml hook session-start --host claude --repo ~/memhtml --budget 1500

cwd is read where the host sends one, and it’s how a session in a project directory gets a pack about that project. Cursor sends workspace_roots instead and the engine reads the first entry.

The indexing events want a trace root, which defaults to $MEMHTML_TRACE_ROOT; an installed indexing hook always names one explicitly on its command line:

Terminal window
echo '{"session_id":"…"}' | memhtml hook pre-compact --host claude --trace-root ~/.claude

That one gives the empty answer whether or not it indexed anything, because no host reads its stdout. Check that it did something by looking at the trace plane afterwards with memhtml trace search, per index session transcripts.

The session-start pack is a memhtml recall answer rendered as a bounded index: one line per memory carrying its title, a gist clipped to 240 characters, and the path an agent hands straight to memory_read, arcs before ordinary memories, then up to five open tasks. --budget decides only which memories recall would quote in full. The pack carries no bodies, so the budget changes nothing but the order: the quoted tier comes first within each fold. The per-prompt injection is a memhtml search answer at --limit hits, in the same one-line form.

Neither one is a citation, and the instruction block install writes is where that’s stated to the agent. A hit is the ranker’s guess about your prompt, its snippet is the chunk that best matched, and an agent that quotes it without opening the path quotes something the ranker chose. The block and the skill carry that discipline, the manifest carries it as the recall-discipline guide topic, and all three render from one prose module in @memhtml/contracts, so they can’t drift from each other.

Wire up your coding agent is what installs these hooks and where the per-host files are listed. Index session transcripts covers the trace plane the indexing events feed and the firewall between it and memory retrieval. For agents is the page an agent reads about its own surface.