feat: user-prompt-submit hook that injects matching KG facts, so retrieval doesn't depend on the agent remembering to query
What problem does this solve?
The protocol returned by mempalace_status says:
BEFORE RESPONDING about any person, project, or past event: call
mempalace_kg_queryormempalace_searchFIRST. Never guess — verify.
That is an instruction the model has to remember to obey, at the exact moment it is least likely to — when it already believes it knows the answer. Retrieval only happens if the agent independently decides to retrieve, and nothing in the loop notices when it doesn't. The palace can hold the correct fact and still not be consulted.
Concrete failure from a session yesterday (mempalace 3.7.0, chromadb 1.5.9, Claude Code, macOS):
I asked an agent to document a finding I had described as verified — that a particular NAS API returned a permission error for all writes. It wrote that into a skill doc, which then fed a pull request against a third-party repo. The claim was wrong; a retest showed the writes succeeding. Two things went wrong, and only one of them is my fault:
- It never queried the palace before asserting. Not because the protocol is unclear, but because it had a plausible answer in hand and no forcing function.
- When I later asked it to record the correction, it wrote a diary entry and stopped. The KG got nothing until I explicitly asked a second time. A
kg_queryon the entity returnedcount: 0— so even a well-behaved future session, following the protocol perfectly, would have retrieved nothing.
The second half is arguably user error. The first half is structural: there is no point in the loop where retrieval is guaranteed to occur. Session-start protocol injection (#467) fixes "does the agent know it has a memory system." It does not fix "does the agent actually consult it on turn 40."
Worth noting how weak prompt-time reminders are in practice. That same session had an unrelated UserPromptSubmit hook injecting a one-line behavioral nudge on every single turn. It fired on all of them and did not change behavior once. Injecting a reminder to retrieve and injecting the retrieved facts are very different interventions, and only the second survives a long session.
What's the proposed solution?
Add a user-prompt-submit hook to mempalace hook run, alongside the existing four:
mempalace hook run --hook {session-start,stop,session-end,precompact} --harness {claude-code,codex}Behaviour: read the incoming prompt from the harness stdin JSON (Claude Code's UserPromptSubmit payload already carries it), run a bounded retrieval against it, and emit matching memory as hookSpecificOutput.additionalContext. The agent then starts the turn with the facts already in context rather than with an instruction to go find them.
The KG half is the more valuable half. Triples carry valid_from / valid_to / current, so a retrieval path that prefers current facts can surface "this was superseded on DATE" — which is exactly the staleness problem described in #224, where drawer retrieval injects contradictory memory with no way to tell which side is live.
Design constraints, since this fires every turn and the failure modes are real:
- Latency. Hard timeout, fail open. A memory system that adds a visible pause to every prompt gets uninstalled.
- Precision over recall. Injecting a wrong or stale memory is worse than injecting none — it launders a bad fact into the agent's context with apparent authority. Prefer a distance threshold over an unconditional top-N. My palace is ~138k drawers; at that size a loose threshold is mostly noise.
- Token budget. Every turn, every session. Needs a cap, and probably a way to spend it on KG triples (compact, typed, temporally scoped) before drawer text.
- Opt-in, with a toggle, in the same spirit as the auto-diary toggle asked for in #2161. Some workflows will not want this.
- Entity extraction is the hard part.
mempalace_searchtakes free text, butmempalace_kg_querytakes an entity, so naive prompt-text passthrough gets you the semantic half only. Even shipping the search half alone would be an improvement.
Alternatives considered:
- Session-start protocol injection (#467). Necessary but not sufficient — it is a reminder delivered once, and its effect decays over a long session. It also cannot help when the agent doesn't recognise that the current question touches stored knowledge, which is the common case.
- A harness-side hook that shells out to
mempalace search. Doable today with no upstream change, and I may do this locally. But it pushes output shaping, token budgeting, timeout handling, and failure-open semantics onto every user independently, and there is no reasonable path to the KG from a shell one-liner. This seems like something the tool should own. - Relying on the agent to follow the protocol. Status quo. The failure above is the evidence, and the always-on nudge hook in that same session is evidence that reminding harder does not fix it.
- Consolidation-time promotion of drawer claims into triples (#1416). Complementary rather than an alternative — it improves what is in the graph; this issue is about whether the graph is read at the moment it matters. Both are needed.
Happy to prototype the Claude Code side if the direction is welcome.
Source: MemPalace/mempalace