Skip to content

fix(gjc): turn on adaptive compaction so long sessions stop resending their prefix - #163

Merged
devswha merged 1 commit into
mainfrom
fix/adaptive-compaction-for-app-sessions
Sep 16, 2026
Merged

devswha merged 1 commit into
mainfrom
fix/adaptive-compaction-for-app-sessions

Conversation

@devswha

@devswha devswha commented Sep 16, 2026

Copy link
Copy Markdown
Owner

Closes #159.

The bug

The runtime ships compaction.adaptive.enabled off for backward compatibility. With it off the only trigger is the legacy reserve threshold, roughly contextWindow - reserve and commonly near 85% of the window. anthropic/claude-opus-5 has a 1,000,000 token window, so the trigger sits around 850K — and a long app session never gets there. It grows its prefix for hundreds of turns and resends all of it every turn.

Measured on app session 01a0aaca-e1c4-74fb-8a41-a85d5fd35954, 681 billed turns from one user message:

total $140.95
  input       tok        1,362   $  0.01   ( 0.0% of cost)
  output      tok      270,351   $  6.76   ( 4.8%)
  cacheRead   tok  250,354,579   $125.18   (88.8%)   <---
  cacheWrite  tok    1,440,630   $  9.00   ( 6.4%)

Per-turn cacheRead never plateaus:

turns   0- 67   mean  96,013
turns 136-203   mean 231,823
turns 340-407   mean 401,708
turns 544-611   mean 554,887
turn  680             643,682     <- 64% of the window; trigger is ~85%

681 turns, zero compaction records in the transcript. 80% of turns read more than 200K from cache. $125.18 was spent re-reading the session's own history against $6.76 of actual output.

Why the app has to decide this

The runtime's own doc names this case exactly:

Adaptive compaction lowers the effective threshold when context is already large and many calls happen in a short window. This targets long sessions where a 150K-230K context can otherwise be resent many times before hitting the static threshold.

...and recommends, for long tool-heavy sessions, precisely the values used here (base 75, aggression 0.2, window 15, floor 50). It leaves the shipped default off "for exact legacy behavior".

App sessions are the long tool-heavy case and cannot opt in for themselves: the app's own system prompt tells users to use Settings rather than edit ~/.gjc, and Settings exposes no compaction control. So the app picks the default.

What this is not

This is a default the app chooses, not a boundary it enforces. Unlike the five overrides in applyGjcToolSettingsPolicy, each value is guarded by settings.has() — true only for loaded settings and runtime overrides, never for schema defaults. A user who did configure compaction keeps every value they set, including enabled: false.

Delegated child sessions are untouched: gjc-delegation-executor.ts disables compaction for them deliberately, and they are bounded by their receipt.

Verification

dist-native/bun test server/gjc-agent-tools.bun.test.ts       13 pass  0 fail
dist-native/bun test server/gjc-sdk-contract.bun.test.ts     124 pass  0 fail
dist-native/bun test server/gjc-browser-backend.bun.test.ts   (20 pass across 2 files)
npx tsc -p tsconfig.json --noEmit                             clean
npx tsc -p server/tsconfig.json --noEmit                      clean
npx eslint <changed files>                                    clean

New tests:

  • adaptive compaction is on for a session that configured none — also asserts the runtime default is still false, so this policy gets revisited if upstream flips it.
  • a user who configured compaction keeps every value they set
  • a partially configured session keeps its own value and gets the rest
  • the compaction policy leaves the tool boundary alone
  • starting a session turns adaptive compaction on — the live session.start path, not just the function.

Both contract fixtures needed has on their settings clones. Without it every session creation threw and 70 tests failed — the same trap the existing fixture comment already records for override.

… their prefix

The runtime ships adaptive compaction off for backward compatibility, so the
only trigger is the legacy reserve threshold - roughly `contextWindow - reserve`
and commonly near 85% of the window. On a 1M-token model that is ~850K, which a
long app session never reaches: it grows its prefix for hundreds of turns and
resends all of it every turn.

A measured 681-turn run on claude-opus-5 peaked at 644K context (64% of the
window), never compacted once, and spent $125.18 of its $140.95 on cache reads -
88.8% of the bill to re-read its own history, against $6.76 of output. Per-turn
cacheRead grew monotonically from 96K to 644K with no plateau.

The runtime's own guidance names this exact case: adaptive compaction "targets
long sessions where a 150K-230K context can otherwise be resent many times
before hitting the static threshold". The values here are the starting point it
recommends for long, tool-heavy sessions.

App sessions cannot opt in for themselves - the app tells them to use Settings
instead of editing ~/.gjc, and Settings has no compaction control - so the app
picks the default. `has()` is true only for loaded settings and overrides, never
schema defaults, so a user who did configure compaction keeps every value they
set. This is a default the app chooses, not a boundary it enforces.

Both contract fixtures needed `has` on their settings clones; without it every
session creation threw, which is the same trap the existing comment records for
`override`.

Refs #159
@devswha
devswha merged commit 096a0c1 into main Sep 16, 2026
6 checks passed
@devswha
devswha deleted the fix/adaptive-compaction-for-app-sessions branch September 16, 2026 17:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(cost): cache reads are 88.8% of session cost and grow unbounded without compaction

1 participant