Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

MemoryTurnStart sends the whole hot layer as one hook output; once memory fills, Claude Code cuts it to a 2KB preview and most memory never reaches the model

Open Nkburdick opened this issue 2 months ago • 4 comments

Version

LifeOS v7.40.4 (latest release). Claude Code 2.1.268.

What is broken

MemoryTurnStart writes both hot-layer files (PRINCIPAL_MEMORY.md, DA_MEMORY.md) to stdout as a single UserPromptSubmit output on a session's first prompt, on any memory change, and every 20 prompts. At the stock caps (48 entries × 256 chars per file) that output can reach about 24.6K characters. Claude Code saves an oversized hook output to a file and puts only a Preview (first 2KB) plus the file path into the model's context. So on those turns the model sees the first several principal entries and none of the DA entries. The 🧠 delta line and the <lifeos-ground> block are written after the memory dump, so they are cut too. Nothing errors, so the loss is silent. It stays invisible until memory fills, which may be why it hasn't been reported: a new install's memory is small.

Where (file:line)

  • LifeOS/install/hooks/MemoryTurnStart.hook.ts:109-111: if (shouldInject(sessionId)) { const memory = loadMemory(); if (memory) process.stdout.write(memory); } runs before the delta (:113-114) and ground (:119-128) writes.
  • LifeOS/install/hooks/LoadMemory.hook.ts:54-71: renders both full files into one block; the caps allow ~12,288 chars each.
  • The header comment in MemoryTurnStart (:42) sizes the block at "~1.5K tokens"; the caps allow roughly four times that.

Repro on a clean tree

  1. Fresh v7.40.4 install. Fill each hot-layer file with 48 entries of about 200 characters each (any content, correct prefixes).
  2. Start a new claude session and send any prompt.
  3. The UserPromptSubmit result for MemoryTurnStart reads Output too large (≈20KB). Full output saved to: … Preview (first 2KB).
  4. Ask the model, with no tools allowed, to quote the last entry of DA_MEMORY.md. It can't; that entry was never in context.

Negative control

Trim both files to about 8 entries each (about 3KB total) and repeat. The output arrives inline, with no "Output too large", and the model can quote the last DA entry. On our install, outputs around 2-3KB always arrived inline, and a 10.1KB output spilled. So the threshold sits somewhere near 10K. We don't know it exactly, and we didn't find it documented.

Suggested fix (shape; tested on a modified install, not on a clean tree)

  • Minimum: write the delta and ground blocks BEFORE the memory dump, so a cut can only take the tail, and warn when the output exceeds a budget instead of failing silently.
  • What we run: @-import both memory files from CLAUDE.md (the same way the identity files load: no size cutoff, prompt-cached). The hook stops dumping; it snapshots entries on the first prompt and then sends only entries added or retired since the last send. If the imports are missing, it falls back to the stock dump.
  • Caveat for that approach: Claude Code loads CLAUDE.md and its imports into custom and general-purpose subagents (only Explore and Plan skip it), so hot memory then reaches every such agent. We moved money and medical detail out of hot memory before switching, and added a curator rule against putting it back.

Related: #2075 (the subagent-detection bug) makes the memory hook skip itself on many installs, which would also hide this one.

  • [x] Searched open and closed issues.
  • [ ] Repro run against a clean tree. NOT YET: ours is a modified install; the repro steps above are written to be run on a clean one.
  • [x] Personal data removed.

Nkburdick avatar Sep 11 '26 21:09 Nkburdick

Reordering the writes fixes the immediate cut, but the shape that removed this class of failure for me was to stop emitting the layer at all and emit an index instead. My memory directory holds one index file, capped at 200 lines / 25 KB, one line per memory, holding a title, a file link and the recall trigger. Typed topic files sit beside it and are read on demand when a pointer's description signals relevance. The index is what loads at session start. Nothing else enters unless a turn needs it. ~~With the cap in place the hook output can never reach the truncation threshold, and the budget warning you propose becomes a lint on the index rather than a runtime surprise.~~

Two details from running it. The index entry has to carry the trigger, not the content. A line that says "read this when the session touches X" is what makes the on-demand load happen. And on the @-import route, I checked whether editing a loaded memory file mid-session forces a cache re-write. It does not, because the loaded copy is captured once at session start, so the cost is only the blast radius you already identified (every subagent that loads CLAUDE.md), not a cache penalty.

I maintain a reference architecture that documents this pattern (memory points, it doesn't mirror) at https://github.com/jimy-r/agent-workspace-architecture/blob/main/PATTERNS.md#5-memory-points-it-doesnt-mirror

Correction, 2026-09-23. The struck sentence was wrong. The measurements in this thread show hook outputs from about 12 KB reaching the model as a 2 KB preview, and a 25 KB cap sits well above that. My index never hit it because it loads through Claude Code's native memory path, not a hook. Emitted from a hook, an index needs a cap below the threshold, or the chunked delivery now fixed at source.

jimy-r avatar Sep 12 '26 09:09 jimy-r

This is Shiva, Eugene's AI Assistant, reporting on Eugene's behalf.

Confirmed reproducing on our install (7.40.4). MemoryTurnStart.hook.ts still writes the full loadMemory() dump (lines 109-111) before the delta (113-114) and <lifeos-ground> (119-128) blocks, and LoadMemory.hook.ts renders both hot-layer files with no byte cap — up to 48×256 chars each, ~24.6K combined. The reordering/import-based fix wasn't applied.

waveman2020-sudo avatar Sep 15 '26 15:09 waveman2020-sudo

Reproducing on LifeOS 7.40.4, Claude Code 2.1.270. hooks/MemoryTurnStart.hook.ts and hooks/LoadMemory.hook.ts are byte-identical to main @ 5e2f2e8.

This may also be the evidence #2032 §4 was waiting for. That PR proposed emitting the delta and ground blocks before the hot layer, and the reorder was held "until the >12 KB truncation is shown". It was shown in one working session here: 7 UserPromptSubmit outputs from MemoryTurnStart were persisted to a file with only Preview (first 2KB) in context. The harness messages the model received read Output too large (12.5KB), (13.9KB) and (14.4KB).

Byte offsets measured on the saved outputs (UTC, same session):

Time Output size Principal file DA file <lifeos-memory-delta> starts at byte <lifeos-ground> starts at byte
10:53 17,984 28 entries / 4,869 chars 47 / 10,579 15,787 16,139
11:06 12,086 28 / 4,869 22 / 4,767 9,876 10,228
13:16 12,857 28 / 4,869 28 / 6,059 11,188 11,647
15:17 14,342 29 / 5,033 30 / 6,496 11,794 12,258
15:26 14,394 29 / 5,033 30 / 6,496 11,794 12,145
16:51 14,857 29 / 5,033 33 / 7,175 12,482 12,828

A seventh output, at 10,940 bytes, also spilled; its block layout differed, so it is left out of the offset columns.

Two things this adds to the report:

  • It happens far below the cap. At 11:06 the DA file held 22 of 48 entries and the principal file 28 of 48, and the output still spilled. The 10,940-byte spill is consistent with your estimate of a threshold near 10K.
  • The DA file is never in the preview, at any fill level. The principal block is rendered first and is already 4.8–5.0K chars on its own, so the DA header always starts past byte 2,000. Reordering the delta and ground blocks to the front (#2032 §4) would bring those two back inside the preview, but the DA entries would still be lost until the hot-layer output itself is bounded.

A gotcha for anyone measuring this: a hot-layer entry that mentions <lifeos-ground> in its text makes a naive tag parser place the ground block inside the memory block. Anchor on </lifeos-memory> first.

drpaulobernardy avatar Sep 15 '26 17:09 drpaulobernardy

Confirmed and fixed at source. Memory delivery is now queued and chunked under an 8 KB ceiling with k/N markers and ack-based redelivery (hooks/MemoryTurnStart.hook.ts, hooks/lib/memory-delivery.ts), so nothing gets cut to a 2 KB preview. Function hooks are also enabled. The next release carries it. Thanks for measuring it instead of guessing, @Nkburdick.

danielmiessler avatar Sep 17 '26 19:09 danielmiessler