Personal_AI_Infrastructure icon indicating copy to clipboard operation
Personal_AI_Infrastructure copied to clipboard

Memory reviewer: turns after a session's last review are never curated, because nothing reviews at SessionEnd (31.5% of typed messages on one install)

Open rain77-work opened this issue 1 month ago • 2 comments

Version

LifeOS 7.40.4 / Cortex (memory reviewer cadence). Claude Code 2.1.296 on macOS.

What is broken

MemoryReviewFire runs only on Stop. It spawns the reviewer when a session's turn_count_since_last_review reaches turn_threshold (8) and the global 30-minute floor is open, and the reviewer reads exactly the turns since that session's last review. Nothing runs the reviewer at SessionEnd. So whatever a session says after its last review is never curated once the session ends: its per-session file keeps the count, no Stop comes for that session again unless it is resumed, and the file is pruned after 7 days. A session that ends before its first review loses everything it said. #1893 saw this discard from the starvation side; as described there, its max_hours_since_review net makes the current session due on a Stop, so it cannot reach a session that has already ended. On one install, 322 of the 1,022 messages the principal typed in ended sessions since 2026-09-22 (31.5%) never appeared in any reviewer prompt for this reason: 184 came after their session's last review, and 138 were in 32 sessions that never got a review. In the last five days it was 150 of 414 (36.2%). (Messages are transcript events with origin.kind: "human", matched by timestamp against the --- Exchange N (<ts>) --- headers of every reviewer-runs/<run>/prompt.user.md.) The hook's own state agrees: 47 per-session files of ended sessions still hold a non-zero count, 237 turns in all. Ten of them hold 8 or more, because the floor was closed each time they reached the threshold.

Related: #1893 (the same discard, seen as starvation) and #1908 (the window has no overlap, so a span the cadence skips is never read by a later run).

Where (file:line)

  • hooks/hooks.json: MemoryReviewFire.hook.ts is registered under Stop only; none of the six SessionEnd commands runs the reviewer.
  • hooks/MemoryReviewFire.hook.ts:258-260: due means this session's count ≥ turn_threshold and global minutes ≥ min_minutes_between; :264-266 spawns the reviewer with --turns <count> and zeroes the count.
  • LIFEOS/TOOLS/MemoryReviewer.ts:209: exchanges.slice(-maxExchanges), so a run reads only the turns since the last fire.
  • hooks/MemoryReviewFire.hook.ts:55 and :169: per-session files untouched for 7 days are pruned, and the remainder's count goes with them.

Repro on a clean tree

cd ~/.claude
T=$(mktemp -d); mkdir -p "$T/.claude/hooks/lib"
cp hooks/MemoryReviewFire.hook.ts "$T/.claude/hooks/"; cp hooks/lib/subagent.ts "$T/.claude/hooks/lib/"
fire() { echo '{"session_id":"s1","transcript_path":"/dev/null"}' | env -i HOME="$T" PATH="$PATH" bun "$T/.claude/hooks/MemoryReviewFire.hook.ts"; }
for i in $(seq 8); do fire; done   # the 8th Stop is due: one fire
for i in $(seq 5); do fire; done   # five more turns, then the session ends
jq '.turn_count_since_last_review' "$T/.claude/LIFEOS/MEMORY/STATE/memory-review/s1.json"
wc -l < "$T/.claude/LIFEOS/MEMORY/OBSERVABILITY/reviewer-fires.jsonl"
jq -r '.hooks.SessionEnd[].hooks[].command' hooks/hooks.json
rm -rf "$T"

It feeds the shipped hook 13 Stop payloads for one session in a throwaway HOME, then prints what is left when the session ends. env -i keeps the real install's variables out. The reviewer is not installed in the temp HOME, so the one fire row says reviewer-not-found; the counting is the same either way, and no model is called.

Negative control

On unpatched 7.40.4 (hooks/MemoryReviewFire.hook.ts, hooks/lib/subagent.ts and hooks/hooks.json as on main at 5e2f2e8), the repro prints:

5
       1
$HOME/.claude/hooks/WorkCompletionLearning.hook.ts
$HOME/.claude/hooks/SessionCleanup.hook.ts
$HOME/.claude/hooks/UpdateCounts.hook.ts
$HOME/.claude/hooks/MemoryHealthGate.hook.ts
$HOME/.claude/hooks/DocIntegrity.hook.ts
$HOME/.claude/hooks/IntegrityCheck.hook.ts

After the session's last Stop, five turns are still counted, the fires log holds only the row from the 8th Stop, and none of the six SessionEnd commands runs the reviewer. Those five turns are never read.

Suggested fix

Untested shape. Register the fire hook, or a small sibling, on SessionEnd too: when the closing session's count is above 0, spawn the reviewer once with --turns <count> --input <transcript_path> and zero the count. SessionEnd hooks get session_id and transcript_path on stdin (as hooks/WorkCompletionLearning.hook.ts:22 notes), and the count is in that session's file. If SessionEnd cannot be relied on when a terminal window is closed, a later Stop in any session could also review, once, any per-session file whose count is above 0 and whose last_message_at is older than an idle limit. How either path should share the 30-minute floor is your call.

Before submitting

  • [x] I searched open and closed issues for this defect.
  • [x] The repro runs against a clean tree of the version above, not against my modified install.
  • [x] I removed personal data from the pasted output — real names, absolute home paths, tokens, my own content.

rain77-work avatar Oct 13 '26 09:10 rain77-work

The failure mode you've measured — 31.5% of typed turns never reaching any reviewer prompt — reads to me as a direct consequence of binding curation to a turn-threshold cadence on Stop instead of to the session lifecycle. A cadence can only fire while a session is alive and keeps coming due; a session that ends below threshold (or ends after its last review) has nothing left to trigger it, and the 7-day prune then makes the loss permanent. The starvation framing in #1893 and the no-overlap window in #1908 are the same root defect seen from two other angles: the unit of curation is "turns since last fire," not "turns since last durably-reviewed watermark."

Two things that closed this for us:

  1. SessionEnd is a curation trigger, not just a cleanup hook. On session close we flush-review every turn after the session's last-reviewed watermark, unconditionally — no turn-threshold, no global minute floor gating it. The 30-min floor is a rate limiter for the periodic reviewer, so applying it to the terminal flush is what lets ended sessions slip; the terminal flush has to bypass it.
  2. The watermark is per-session durable state, advanced only after the reviewer commits. So "reviewed up to turn N" survives the prune, and a resumed session resumes from the watermark rather than from a zeroed count. Pruning the per-session count file is then harmless because the count was never the source of truth.

A cheap way to make the regression visible before shipping: assert at SessionEnd that max(reviewed_watermark) ≥ last_human_turn_index, and emit the gap as a counter. If that counter is ever non-zero you've reproduced exactly the 138-turns-in-32-sessions bucket in CI instead of discovering it on one install months later.


We run a single-orchestrator agent OS where memory curation is driven by an engine-owned session-lifecycle hook against a durable per-session watermark, not a turn-count cadence. More context on SwarmAI. Discussion: T-MEM

xg-gh-25 avatar Oct 13 '26 09:10 xg-gh-25

[!NOTE] 🤖 Kai (AI assistant) responding on behalf of Daniel Miessler

Thanks @rain77-work. You were right: the reviewer was triggered only by that same session's next Stop, so a session that ended under the threshold was never reviewed.

Fixed in source, using your second option, with no SessionEnd hook. Each session's state now records its transcript path. When a Stop is not due for its own session, it looks for the session that has been idle longest and still has turns waiting, and reviews that one instead, with trigger stranded. Each fire runs at most one review, and the shared 30-minute floor still applies. A stranded review found from a texting session runs without the texting environment, so it is treated as an ordinary session.

One limit: sessions saved before this change have no transcript path recorded, so they are never picked up.

This ships in the next release.

danielmiessler avatar Oct 28 '26 18:10 danielmiessler