Field Note · No. 0017
Memoria Volatilis
claude · code · hooks
FILED   2026 · 05 · 04 SUBJECT   PreCompact STATUS   PROPOSED BLAST RADIUS   LOCAL

A note on forgetting & the hook that remembers.

Auto‑compaction is a graceful loss. When the context window fills, Claude Code summarizes the older turns — freeing tokens for the work ahead. The conversation continues; the record does not. File paths fade. Stack traces blur. The exact phrasing of a correction becomes the gist of one. PreCompact is the moment to slip a copy of the underlayer into something durable, before the page is overwritten.

archival / preservation
As it stands · today

Now

— compaction without a witness

The window fills. The summarizer runs. What survives is a synthetic gist of the prior turns; what does not is everything that was specifically said.

PostToolUse and Stop hooks already mirror per‑turn events into Qdrant — fine‑grained, but disconnected. End‑of‑session summaries land safely. The middle of long sessions, where the most context lives, is where memory drops a layer.

Lost in transit: absolute paths, verbatim errors, long command output, the exact phrasing of feedback, the chain of intermediate reasoning.

After the hook

Later

— a snapshot, taken in the margin

A single shell script wired to PreCompact. It receives the full pre‑compaction transcript on stdin, the last moment that detail still exists in raw form.

It pipes the payload through extract‑chat, distilling a 2–4k token narrative — goal, decisions, files touched, unresolved questions — and writes it to Qdrant under collection: sessions. The full JSON is archived to ~/.claude/compactions/ as a belt‑and‑suspenders second copy.

Wrapped in timeout 10s … || true, so a Qdrant hiccup never blocks the model's next response. Symmetric with Codex, Gemini, and OpenCode — all four read the same memory.

Now · no PreCompact hook

  turn₁ ─┐
  turn₂ ─┤
  turn₃ ─┼──▶  [ summarizer ]  ──▶  synthetic summary
  turn₄ ─┤                              │
  turn₅ ─┘                              ▼
   …                              continued context

   ▽ raw turns: discarded after summarize
   ▽ no side-channel to durable memory
   ▽ Qdrant sees per-tool events only
A clean sweep. Tokens freed; texture lost.

Later · with PreCompact hook

  turn₁ ─┐
  turn₂ ─┤        ┌── stdin ──▶ pre_compact.sh
  turn₃ ─┼──▶  [ summarizer ] ────────┐
  turn₄ ─┤                              
  turn₅ ─┘                              
                                continued context
  
           ├──▶ extract-chat ──▶ narrative ──▶ Qdrant
           └──▶ raw json ─────────────────▶ ~/.claude/compactions/

   △ snapshot taken before lossy step
   △ searchable from any qmem-aware tool
   △ timeout 10s · non-blocking on failure
The page is still overwritten. The underlayer is preserved.
Plate I · Compaction Tape — a 30-turn session, drawn twice.Top: detail simply fades. Bottom: at each compaction, a snapshot is taken before the fade.
NOW · WITHOUT HOOK ▾ COMPACT ▾ COMPACT t1 t6 t12 t18 t24 t30 turns 1–12 → ∅ turns 13–24 → ∅ LATER · WITH PRECOMPACT HOOK ▾ COMPACT ▾ COMPACT snapshot → Qdrant + ~/.claude/compactions/
Each compaction is still lossy — but no longer total.
Plate II · Memory StratigraphyThe layers of durable record. The hook fills a missing stratum.
NOW LATER live context per-turn · qdrant snapshot layer session summary — void — SESSION/COMPACT-{TS} no on-disk archive ~/.claude/compactions/
A geological cross-section of where memory actually lives.
Plate III · Sequence of the HookWhat happens, in order, when PreCompact fires.
CLAUDE code PRE_COMPACT .sh hook EXTRACT chat QDRANT qmem FILESYS disk stdin · transcript json pipe transcript narrative ~3k tokens memory_store_session_summary {session}-{ts}.json exit 0 · bounded ≤ 10s summarizer runs context continues
Snapshot before squash, then a quiet exit.
Plate IV · One Snapshot, Four ReadersQdrant is shared infrastructure. Claude writes; Codex, Gemini, and OpenCode all read.
QDRANT qmem-memory · localhost CLAUDE CODE writer + reader snapshot memory_find CODEX gpt · reader memory_find GEMINI CLI reader memory_find OPENCODE reader memory_find — one snapshot · four readers · single source of truth —
Today's Claude session is searchable from tomorrow's session — in any of the four agents.

No new dependencies. One JSON entry. One shell script that mirrors the shape of the hooks already on disk.

~/.claude/settings.json  ·  hooks diff +9
{
  "hooks": {
    // … existing PreToolUse, PostToolUse, Stop, SessionStart …

    "PreCompact": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "/root/qdrant-memory-mcp/scripts/claude_pre_compact_hook.sh"
          }
        ]
      }
    ]
  }
}
01 · Trigger

Fires once, immediately before the summarizer runs. The full transcript — every message, tool call, tool output — is delivered to the script's stdin as JSON.

02 · Distill

Pipe through extract-chat to produce a 2–4k token narrative: user goal, key decisions, files touched, unresolved questions.

03 · Persist

Write the narrative to Qdrant via memory_store_session_summary: collection sessions, topic session/{id}/compact-{ts}, type compaction_snapshot.

04 · Archive

Mirror the raw JSON to ~/.claude/compactions/{session_id}-{ts}.json. Qdrant for search; flat file for full fidelity recovery.

Four small things, cumulatively useful.

i.

Cross-session recall

"That bug we hit with the auth middleware" returns the actual context, not a vague reference. memory_find queries the snapshots like any other record.

qdrant · sessions
ii.

Recoverable detail

If compaction drops a critical file path or error message, grep the flat-file archive. The exact bytes are there, indexed by session and timestamp.

~/.claude/compactions/
iii.

Pattern mining

Snapshots accumulate. Ask: which kinds of tasks compact most often? Which sessions ended with a failed build? The corpus answers.

retrospective · workflow
iv.

Tool symmetry

Codex, Gemini CLI, OpenCode all share qmem-memory. Today's Claude snapshot is searchable from tomorrow's session in any of them.

cross-agent
§ 06 — Cost · Risk

One Qdrant write per compaction event — negligible. Storage grows linearly; prune snapshots older than ~90 days. The hook delays the model's next response for at most the timeout. Worst case is bounded; best case is a memory you didn't know you'd want, available the day you need it.

Ship the field note.

This page is a single static index.html. No build step, no framework. Drop the directory at Cloudflare Pages — or run the command below from the project root.

wrangler pages deploy . --project-name=precompact-explainer
  1. Authenticate once with wrangler login.
  2. From /root/precompact-explainer/, run the deploy command.
  3. First run prompts to create the precompact-explainer project; subsequent runs publish a new deployment.
  4. Custom domain: wrangler pages project create then attach via the Cloudflare dashboard.