# ADR-0009: State after compaction = budgeted greedy selection over the session journal - Status: Accepted (2026-10-15). It replaces r1. The owner asked for an algorithm based on best practice, accepted `DECISIONS-r3.md` in full, and delegated the r4 fixes in `DECISIONS-r4.md`. - Evidence: workflow `wf_5b7e35b3-72f`, topic `compaction-algorithm`: 19 findings verified, 2 unverified, 1 refuted. Raw output: `https://github.com/mksglu/context-mode/blob/274600e2ffab843e89c1dfa09b0d649e650c962f/tickets/round4-evidence.json`. Prototype: `/tmp/wf4/sk/`. ## Best practice (sources) - **Anthropic context editing:** clear the oldest tool results first, put a placeholder in their place, keep the last N (default 3). This is *observation masking*. - **Anthropic memory tool:** "memory preserves the information that must survive summarization", kept as a progress log in an external store. - **JetBrains, "The Complexity Trap"** (arXiv 2508.21433): observation masking "halves cost … while matching … LLM summarization". - **Generative Agents:** score = recency decay + importance; take the best items that fit the budget. - **What Claude Code restores itself after compaction:** CLAUDE.md, auto memory, git status, the plan, and up to 5 recently changed files. Context that hooks added earlier is summarized away. So our block must **not** repeat file contents; paths are enough. - **Only re-injection point for Claude Code, Codex, Qwen, ZCode:** `SessionStart(source=compact)`; `PreCompact` never injects there. [V] live: a 234 B block reached the model whole. - **A second path exists for Gemini, Copilot CLI, VS Code:** `PreCompact` writes a pending flag (`meta('pending_compact:'+sid)`); the next `UserPromptSubmit` hook performs the actual injection (see delivery table, §6). The "only re-injection point" claim above does not hold universally — it is scoped to the first group. - **Reciprocal Rank Fusion** (Cormack et al., SIGIR '09): `1/(60+rank)` beat single rankers and Condorcet fuse on TREC runs. Used for cross-session ranking, §8. - **Time-aware retrieval** (LongMemEval, arXiv:2410.10813): normalize timestamps to one shape before ordering or comparing. Used for `sort:"timeline"`, §8. - **Mem0** (arXiv:2504.19413): update-beats-duplicate idea for memory rows, taken without its LLM step. Used for the `decision` tag design, §8. - Measured, not preference: a 12-case deterministic sweep, 0 model calls, backs the unchanged weights below (`https://github.com/mksglu/context-mode/blob/274600e2ffab843e89c1dfa09b0d649e650c962f/tickets/memory-r6/eval/results.md`; `https://github.com/mksglu/context-mode/blob/274600e2ffab843e89c1dfa09b0d649e650c962f/docs/v2/review/DECISIONS-r6.md` D1). - Delivery mode per agent (DECISIONS-r6 D6) is measured, not preference: `https://github.com/mksglu/context-mode/blob/274600e2ffab843e89c1dfa09b0d649e650c962f/tickets/memory-r6/eval/run2.mjs`, 20 real compaction points, 6 projects, scored in `https://github.com/mksglu/context-mode/blob/274600e2ffab843e89c1dfa09b0d649e650c962f/tickets/memory-r6/eval/results.md`. ## v1 failures this fixes - the snapshot had no byte budget - text was cut (80 chars, 400 chars, 150 chars) - the header contained the clock, so the output was not stable - "unresolved errors" were never resolved, and resolution used regex - eviction used fixed priority labels ## Decision The block is a deterministic function of this session's journal rows. No LLM, no regex, and no text is ever cut: whole items are selected, or an item becomes a pointer. ### 0. Journal write path One write path into the one shared `context.db` per project: the server writes journal rows from its own `ctx_*` handlers at handler-return time, for its own tool calls. Hooks (`PostToolUse`, `PostToolUseFailure`) write rows only for other adapters' host tools. Both paths use the single `#645` path resolver. This retires the earlier "server never writes" rule. ### 1. Journal (in `context.db`) ``` events(seq PK, session_id, worktree, agent, kind, tool, key, path, ok, exit_code, bytes, body, turn) INDEX (session_id, kind, seq), (session_id, kind, key, seq) ``` Rows by kind: - `prompt`: user prompts - `edit`: Write/Edit file path, ok/fail - `cmd`: shell / `ctx_execute`; key = tool + canonical command - `toolerr`: other failed tools; key = tool + stable JSON of the input - `index`: `ctx_index` source How the values are computed, with no regex: - **Canonical command:** a quote-aware state machine that only folds whitespace outside quotes. - **exit_code:** read from the first line of the error with `startsWith("Exit code ")` and a digit loop. - **Claude Code also registers PostToolUseFailure.** Without it, failures from host tools are never recorded. (`ctx_*` tool-call failures are still recorded by the server itself, per §0.) ### 2. Candidates Every query has `WHERE session_id = :current`. An empty id means nothing is injected. | Section | Rule | |---|---| | last request | the newest prompt, always included (pinned) | | open errors | keys whose last failure is newer than their last success: **resolved structurally**, when the same command or input later succeeds | | changed files | paths, last write wins, with an edit count | | earlier requests | the other prompts | | indexed sources | `ctx_index` sources | Each section gives at most 85 candidates. That is ⌊2048 / 24⌋, the shortest possible line, so no more lines could fit anyway. ### 3. Score (integers only) ``` w = open error 900 · prompt 500 · edit 400 · index 200 age = current turn − item turn decay = 0.25 + 0.75 · 2^(−age/4) half-life 4 turns, from a checked-in integer table bonus = 1 + ⌊log2(repeat count)⌋ score = w · decay · bonus ``` A `decision` row (§8) is never a candidate here. The prompt row already holds the text; a second line would waste budget. Scoring covers **sections**, not raw `EventKind` values: a `cmd` or `toolerr` row with `ok = 0` feeds the `open_errors` section (weight `WEIGHT.toolerr`, value `900`); a `cmd` row with `ok = 1` only resolves an existing open error, it is never itself a candidate; `cost` and `decision` rows are never candidates (DECISIONS-r9 F2/F8). ### 4. Selection: budgeted lazy greedy (knapsack by score/size) - Budget **B = 2,048 UTF-8 bytes**: 5× under Claude Code's 10,000-char spill limit. This is the compaction-state budget only, separate from the global `OUTPUT_CAP = 16,384` B used elsewhere (ADR-0008, ADR-0016; Q2). - `ratio = score · 0.7^k / bytes`, where k = lines already taken from that section. This decaying gain keeps the sections mixed. - Take items in ratio order. An item that does not fit is skipped, and smaller ones may still fit. - An item over 640 B is never cut. It becomes a pointer line: `# ( B) ctx_resume(seq:)`. - `ctx_resume(seq:N)` is a primary-key read of the whole row, so a pointer is never a dead end. - At most 340 candidates, O(m²) worst case: under 0.2 ms. Measured whole algorithm: 4.3–8.9 ms. ### 5. Render ``` … sections in a fixed order; lines in seq order … more: ctx_search(queries:[…], scope:"session") ``` - No clock, no user role, bare tool names, framed as data. - **Same rows → same bytes.** [V] 10 runs out of 10 identical, and rows from other sessions are ignored. - **The injected bytes must be byte-identical across every request within a turn** (#822 cache-prefix lesson): the block is computed once per turn, not re-rendered per request. ### 6. Delivery: one path per agent Delivery now also depends on `compactSummary` (`adapters/table.ts`: `"host" | "none"`), per DECISIONS-r6 D6. `"host"` means the agent writes its own compact summary: after compaction, inject one pointer line only (no state block): `context-mode: earlier work of this session is in ctx_resume (state block) and ctx_search(scope:"session", sort:"timeline").` The 2,048 B block (§4, unchanged) is still computed on demand — `ctx_resume()` with no `seq` returns it. `"none"` means the full 2,048 B block is injected through the agent's row below, as before. Claude Code is `"none"` (D-M7-CL-1): the earlier `"host"` choice rested on `https://github.com/mksglu/context-mode/blob/274600e2ffab843e89c1dfa09b0d649e650c962f/tickets/memory-r6/eval/run2.mjs` (pointer-only 0.85 vs 0.75), but T-CMP live failed arm (a), the pointer arm. Two runs, keyless, 10 sessions per arm, `--max-budget-usd 0.25` per session: Sonnet 5 low: correct 7/10 (gate 0.80), call rate 0/10 (gate 0.70). Re-measure on 2026-10-19, Sonnet 5.5 low: arm (a) correct 7/10, call rate 0/10; arm (b) 7/10, 0/10; (c) 7/10, 0/10; (d) 7/10, 0/10; (e) 7/10, 0/10; Opus 5.5 low, arm (a) only: correct 4/10, call rate 0/10 (3 sessions hit the cap). No session called `ctx_resume` or `ctx_search` after the reset turn, so the pointer line is not enough on its own. The row stays `"none"`: the full 2,048 B block is injected once after compaction. Clean re-measure 2026-10-21 (isolation from D-M7-CL-3: no host auto-memory, no CLAUDE.md, no claude.ai MCP; 0 of 50 sessions touched memory), Sonnet 5.5 low, 10 sessions per arm, all arms, 0.25 USD cap: correct (a) 7/10, (b) 7/10, (c) 7/10, (d) 7/10, (e) 7/10; ctx_resume/ctx_search call rate 0/10 in every arm; bytes returned by those tools 0 in all 50 sessions; cost (a) 0.8682, (b) 0.9114, (c) 0.9014, (d) 0.8898, (e) 0.9514, total 4.5222 USD. Every miss is fixture session-03 (case-sensitive exactMatch, open for the owner). Result: arm (a) still fails both gates, so claude-code stays "none" (D-M7-CL-1). Result file: e2e/live/compact/results/2026-10-21-sonnet-5-5-clean-all-arms.json. Result files: `e2e/live/compact/results/`. The `"host"` text of this section is kept for agents whose LV-COMPACT check passes. Every other agent stays `"none"` until its own LV-COMPACT live check (one per agent, milestone M3) shows the host writes a summary the model sees; then that row's column flips to `"host"`. Each flip is decided once per release by that agent's own live check, never re-decided per session (DECISIONS-r9 V9, V27). One path per agent, no fallback. | Agent | Path | |---|---| | Claude Code, Codex, Qwen, ZCode | computed inside SessionStart(compact). No PreCompact hook, no precomputing | | Gemini, Copilot CLI, VS Code | the compaction hook writes `meta('pending_compact:'+sid)`. The next prompt hook runs `DELETE … RETURNING` in a `BEGIN IMMEDIATE` transaction, so the injection happens exactly once | | Pi | `session_compact` flag, then `before_agent_start`, once | | OpenCode 2 | live test first (r1 notes still apply); delivered once, through a channel that keeps the prompt cache stable (Q12) | | Kiro | not supported — no compaction signal of any kind; documented as a known gap for Q20+ (live test showed no `SessionStart(compact)` signal and no usable `PreCompact` substitute) | Qwen sits in the `SessionStart(compact)` row above, not a separate "no PreCompact hook" row: Qwen does have a `PreCompact` hook, but it fires too early to carry enough state for the Gemini/Copilot CLI/VS Code pending-flag pattern, so `SessionStart(compact)` stays the delivery path for Qwen. This is confirmed, not an open question. ### 7. Memory - There is no injected memory, and the role/intent classifier is removed. - What the user decided are the user's own prompts. - Auto-memory is only a search source inside `ctx_search`. ## Golden cases (GP6) - the block is ≤ 2,048 B for a journal of 10,000 rows (#1022) - no foreign session rows (#1274) - an error fixed later is not listed - the same rows give identical bytes - an item over 640 B becomes a pointer, and `ctx_resume(seq)` returns it whole - not the user role (#1263), bare tool names (#1028), no "directive" text (#911, #1259) - a failed `ctx_execute` call with no `PostToolUse` fixture still produces a journal row (the server writes it at handler-return time, per §0) - the one measured open-error miss (12-case sweep, DECISIONS-r6 D1) is a budget skip, not a scoring bug — if a future run shows a scoring bug, fix the bug; do not raise B - an agent with `compactSummary: "host"` (DECISIONS-r6 D6) receives the pointer line only, never the 2,048 B block, unless `ctx_resume()` is called; an agent with `"none"` always receives the full block ## Open - **O1. Earlier-error lookup:** BM25 alone beats RRF (0.72 vs 0.59). Candidate: lower the session list's weight for `toolerr` rows. Must be measured with `run.mjs` first. Source: `https://github.com/mksglu/context-mode/blob/274600e2ffab843e89c1dfa09b0d649e650c962f/docs/v2/review/DECISIONS-r7.md`. - **O2. File by topic:** 0.04 for every variant; text search cannot link a later prompt to an old edit row. Candidate without embeddings: store the turn's prompt tokens on each `edit` row. Must be measured first. Source: `https://github.com/mksglu/context-mode/blob/274600e2ffab843e89c1dfa09b0d649e650c962f/docs/v2/review/DECISIONS-r7.md`. - Hook budget: the whole SessionStart process is 48.6 ms p95, of which bare Node is 45.6 ms. See ADR-0008 "Open". - Resume-quality eval (Memory-R6, 2026-10-16): `e2e/live/memory-r6/` holds a keyless, deterministic harness that scores `none`/`v1`/`v2` (this ADR)/`keeplastn`/`masking` on 12 real session cases. Deterministic sweep (12 cases, 0 model calls): `v2` holds the 2,048 B budget at 11-12/12 hit rate on naming the open error, last request, and changed files; `none`/`v1` carry no budget (155-191 KB mean); `keeplastn`/`masking` both drop below `v2` on naming the last request and changed files, the regression the research doc predicted. An LLM-judged quality pilot was started but stopped small (n=3) under time pressure — see `https://github.com/mksglu/context-mode/blob/274600e2ffab843e89c1dfa09b0d649e650c962f/tickets/memory-r6/eval/results.md` for the exact numbers and the command to resume it. ## 8. Memory-R6 additions: `sort:"timeline"`, the `decision` tag, cross-session ranking Owner decision (2026-10-16): yes to both `sort:"timeline"` and the `decision` tag, on the evidence in `https://github.com/mksglu/context-mode/blob/274600e2ffab843e89c1dfa09b0d649e650c962f/docs/v2/review/DECISIONS-r6.md` (D2–D4). No embeddings, no LLM call at query time — same constraint as §0–§7. ### 8.1 `ctx_search sort:"timeline"` ```ts interface CtxSearchInput { queries: string[]; scope?: "session" | "project" | "global"; // default per PRD §8 source?: string; offset?: number; // relevance mode only sort?: "relevance" | "timeline"; // default "relevance", v1 behavior unchanged after?: number; // timeline mode only; opaque seq cursor } ``` Rules, in order: 1. **Sources.** `"timeline"` reads three sources: ContentStore (current session), SessionDB (prior sessions), auto-memory. `"relevance"` reads ContentStore only, as in v1. 2. **Scope first.** Apply `scope` to all three sources the same way. Always filter by the current `projectDir`/worktree. Fixes #663 (timeline leaked across projects) and the #895/#1294/#1302 class. 3. **Fetch.** Fetch up to `limit` rows from each source on its own (≤ 3·limit candidates). Never clip one source before the merge. 4. **Normalize.** Convert every timestamp to ISO-8601 UTC `YYYY-MM-DDTHH:MM:SS.sssZ` before any compare. Fixes v1's `9de1c658` bug (SQLite and ISO shapes mixed). 5. **Order.** Ascending by normalized timestamp (oldest first). Ties break by `seq` ascending. Rows with no `seq` (auto-memory files) sort after journal rows at the same timestamp, then by path ascending. Same input, same order. 6. **Page.** `after` is an epoch-ms timestamp cursor (M18), not `seq`: drop rows with normalized timestamp ≤ `after`, then cut to `limit` — except a page never ends inside a group of rows sharing one timestamp; it extends to the end of that group so no row is skipped between pages. Reply ends with `more: ctx_search({…, sort:"timeline", after:})` when rows remain (M22). A non-zero `offset` with `sort:"timeline"` returns `isError` `INVALID_ARGS`; a non-zero `after` with `"relevance"` returns the same error. `0` is the first page of either cursor and is accepted, and a blank `source` names no source (#1354, D-R54-SEARCH-ZERO-1). 7. **Empty.** Empty `queries` is allowed in timeline mode; it returns the newest page in scope. 8. **Output.** Each line: `[] # `, cut by `capBytes(text, OUTPUT_CAP)`. 9. **Ordered read (relevance, #1372, D-1372-ORDERED-READ-1).** A `"relevance"` call with a `source` whose queries hold no word of that source (`[""]`, or the tool name in an MCP replace pointer) reads the stored text of that source in order: one head line (`Source in order, bytes - of :`), the next bytes up to `OUTPUT_CAP`, then `More: ctx_search({queries:[""], …, offset:})` when bytes are left. `offset` is a byte offset there. The pages put together are the stored text (a blank-line gap between chunker blocks comes back as one blank line). A query with a word in the source keeps the ranked reply, byte for byte. A lost replace bet costs ceil(S / OUTPUT_CAP) calls, and no replace stub gets a byte. Sources: v1 `e7dbf587` (#367), `9de1c658`, `19cb64b1`, #663; time-aware retrieval, LongMemEval (arXiv:2410.10813). ### 8.2 The `decision` tag The rule is structural, not a word list, and it never infers intent from prose. v1's keyword-and-shape approach is removed in full — it caused #856 (a casual phrase frozen as a standing directive) and the #895/D12 retune loop. A journal row with `kind = "decision"` is written only when one of these is true: 1. **Tool answer.** `PostToolUse` for `AskUserQuestion` with a JSON `tool_response.answers`. Body: `Q: → A: