gortex icon indicating copy to clipboard operation
gortex copied to clipboard

Precompact hook saves session knowledge but can't prevent token re-inflation from re-injected file reads

Open Bobyfil94 opened this issue 5 months ago • 1 comments

Summary

Gortex's precompact hook correctly fires distill_session before context compaction, preserving decisions and pinned notes across sessions. However, it has no authority over what Claude Code's harness re-injects as system-reminder blocks into the new context window after compaction. When the tail of the pre-compaction session included raw Read tool results on large indexed files, those full file contents are replayed verbatim — bypassing gortex enforcement hooks and re-inflating the context with content that gortex could have served at 30–40% of the tokens.

Observed behavior

After a session compaction, the resumed context contained system-reminder blocks replaying:

  • CosmosCommands.cpp (~10 KB, full file) — readable via get_symbol_source + compress_bodies: true at ~3–4 KB
  • Three Python helper scripts written during the session

The gortex pre-tool enforcement hooks (which deny Read on indexed source) do not fire on auto-injected system-reminder content, so there is no interception point.

Root cause

The precompact hook fires before summarization — at that point the Read results are already in context. The harness's decision about what to carry forward into the new window happens after the hook returns, outside gortex's reach.

Proposed fix

One or more of:

  1. Postcompact hook — a hook that fires after the new context window is initialized, receiving a list of re-injected tool results. Gortex could replace any re-injected Read result for an indexed file with a get_symbol_source / get_file_summary compressed equivalent, emitted as a leaner system-reminder substitute.

  2. Precompact content advisory — gortex emits a structured hint during precompact (e.g. a session_files_accessed note) that Claude Code's harness can use to decide not to re-inject raw file content for those paths, trusting the agent to re-fetch via graph tools.

  3. resume_context MCP tool — a dedicated tool the agent calls at session start (prompted by distill_session output) that takes a list of previously-accessed paths/symbols and returns them in compressed form, allowing the agent to explicitly swap injected content for a cheaper equivalent.

Environment

  • OS: Windows 11
  • Gortex daemon: vv0.41.0+29a0849
  • Host: Claude Code (claude-sonnet-4-6)

Bobyfil94 avatar Jun 10 '26 10:06 Bobyfil94

Thanks for this, and sorry it sat — you were right, and the investigation turned up something worse underneath it.

Your mechanism is real, and now documented

Compaction rebuilds the window from the summary, the most recent exchanges, and up to five recently-read files, which Claude Code re-reads from disk and re-injects as system-reminder blocks. It is in the docs now, under Resume from a summary:

Claude Code sends one summarization request over the full history, then replaces the history with the summary, your most recent exchanges, and up to five recently read files.

Two details worth adding to your report:

  • It is not a verbatim replay of the old tool result — the file is re-read fresh from disk at compaction time through the real Read tool, so the bytes reflect current disk state.
  • Files you merely wrote or edited are eligible too, which is why your three Python helper scripts came back alongside the .cpp.

Your read on the interception point was also correct: that path invokes the Read tool directly, so PreToolUse never fires and none of gortex's redirects apply.

Underneath it, gortex's PreCompact briefing was going nowhere

While checking this I found the PreCompact hook was emitting its snapshot as hookSpecificOutput.additionalContext — an output PreCompact does not support. Per the hooks reference, additionalContext is honoured for SessionStart, Setup, SubagentStart, the tool events, Stop and SubagentStop. PreCompact's only documented output is decision: "block", which blocks compaction. So the briefing you saw described in the docs was being discarded — including its "do not re-read indexed files" line. It appears never to have worked.

Disposition of your three proposals

(1) PostCompact hook with content authority — blocked upstream. PostCompact does exist now (Claude Code 2.1.76+), but the hooks reference lists its decision control as "None. Used for side effects like logging or cleanup." No injection path.

(2) Precompact advisory the harness honours — blocked upstream. No hook event can suppress or rewrite harness auto-injected content.

(3) resume_context tool — buildable, but it turned out the wrong lane. SessionStart(source="compact") fires after compaction, does honour additionalContext, and gortex already registers SessionStart with no matcher — so the injection point existed the whole time and was simply never used. A dedicated tool would still require the agent to choose to call it; this arrives unconditionally.

Fixed in #522

  • The orientation snapshot moves to SessionStart(source="compact"), so it actually reaches the model.
  • It now names the re-injection explicitly, so the agent knows those files are already in context and re-reading them pays twice.
  • vv0.41.0+29a0849 — your doubled v — was a real rendering bug, live on every session start. Also fixed.

One thing to check on your side

Your raw Read of CosmosCommands.cpp should have been denied and wasn't. At v0.41.0 the hard-deny probe called an HTTP endpoint that had been deleted weeks earlier, so it always returned "not indexed" and the deny was dead — it also disabled the Bash-cat/Edit/Write denies. That was fixed in v0.57.0, well after you filed.

You are on v0.41.0 and current is v0.63.2. Upgrading should restore the deny that would have kept that file out of your context in the first place — which matters here, because a file that never enters readFileState is never a candidate for post-compaction restore. Combined with #522, that closes both halves of what you hit.

Leaving this open until #522 lands.

zzet avatar Aug 09 '26 15:08 zzet