Skip to content

Let the model provider compact long conversations - #1907

Merged
fazxes merged 9 commits into
mainfrom
fazxes/provider-written-compaction
Nov 25, 2026
Merged

fazxes merged 9 commits into
mainfrom
fazxes/provider-written-compaction

Conversation

@fazxes

@fazxes fazxes commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

  • On models whose provider can compact a conversation, fx asks the provider to compact it, and later requests send the provider's block, then the newest 4 turns unchanged with their reasoning.
  • If the provider's compaction fails, fx writes its own summary and keeps no turns after it.
  • After a model switch, the earlier model's kept steps are sent as text.
  • Models without provider compaction keep fx's own compactor.
  • Stopping a compaction while the provider writes its block leaves the conversation as it was.
  • A session killed after the block is saved resumes with the block and the kept turns.
  • A running turn that doesn't fit is compacted whole, and only its message follows the block.
  • A request the provider refuses for its size is compacted with fx's summary and sent again.
  • /compact on these models runs right before the next message is sent.
  • When the provider rejects a compaction because the sign-in expired, fx renews the sign-in once and asks again, as it does for its own summary.
  • The compaction row keeps moving while the provider writes its block.
  • Resuming a session compacted in the middle of a turn no longer drops the steps saved after the compaction.

Why

  • These models bind each reply's reasoning to everything sent before it. fx's own summary changes that start, so the turns fx kept after its summary lost their reasoning. The provider's own block is what the provider reads in place of the older turns, so the newest turns can follow it unchanged.
  • Keeping no turns after fx's summary keeps the reasoning valid, but it drops the newest turns at every compaction. It is the fallback when the provider writes no block, not the main path.
  • The checkpoint saves the provider's block, how many turns it kept, and the system messages those turns were sent with. A resumed session has to send the same request, or the kept turns' reasoning no longer follows what it was written after.

Before and after

  • Before: every compaction sent fx's summary, then the newest turns, whose reasoning no longer matched what came before it.
  • After, on these models: the provider's block, then the newest 4 turns unchanged with their reasoning. Any other model reads fx's exact text of the same turns.
  • /compact on these models now shows "Context will be compacted before your next message." and compacts right before that message is sent, the one request the provider can compact exactly as it was sent.

Tests

  • tests/e2e/provider-compaction.test.ts drives the built binary through a fake AI Gateway: both providers' blocks, each fallback (error statuses, no block, an answer, empty, cut and oversized replies), a model switch, /compact, a stopped compaction, a kill and resume, 30-turn sessions, a running turn compacted whole, a request refused for its size, and a fork of a compacted session, which sends the block to the model that wrote it and fx's text to another model. compaction-policy.test.ts now counts the provider's compaction request.
  • tests/tla/compaction/ProviderCompaction.tla models the cut, the block, the fallback and the kept turns. check.sh runs TLC on it, shows that the old behavior breaks NoDroppedReasoning, and checks that four planted bugs each break a property. tests/tla/drivers/compaction/ walks the built binary along the model.
  • Two fuzz targets cover the block check and its replay, and the checkpoint's provider block round trip.
  • CI: the PGSO corpus lists the E2E file as a verification scenario, and it has a shard weight.
  • Checked live after the rebase, on three models from two providers: each session compacted on the 7th of 8 turns, the request after it started with the provider's block and sent the 7 kept messages unchanged with their reasoning, no reasoning was dropped, the model recalled the facts from the compacted turns, and the resumed 8th turn started with exactly the 7th turn's conversation.

Size

  • This change adds about 20 KB of shared compactor, session and gateway code to fx-core.wasm and to fx-term.wasm, which would put both over the budgets main sets (main measures 2,471,087 of 2,475,000 and 4,270,750 of 4,276,000 bytes). Their budgets in sdk/tests/package-size-budgets.json go just above the measured sizes, as main raises them: fx-core.wasm to 2,505,000 bytes (2,491,219 measured) and fx-term.wasm to 4,310,000 bytes (4,292,140 measured). SDK agents keep fx's own compactor: their model connection sets no compaction hooks.

Notes on the checks

TLA+ models that cover changed files and stay unchanged:

  • agent_runtime/: the provider's compaction request is not an agent step, and the step loop's empty-reply nudge is untouched.
  • auth/: a provider compaction rejected with 401 renews through the same renewCredential path as fx's own summary request, once per compaction.
  • configured_provider/: custom model connections never turn provider compaction on (only the AI Gateway provider sets the hooks, src/builtins/gateway.zig), and the fields they send do not change.
  • compaction_progress/: the provider's request uses the same ask step as a summary request, so the row earns its waiting share; if fx then writes its own summary, that request asks again, as the model allows.
  • gateway_open/: the compaction request opens its connection through the same streamModelCompletion path.
  • saved_turn/: the order of a turn's messages at each step boundary does not change; the block is a checkpoint message before the turns. In the turns a block kept, another model's reasoning goes as text (see session/ below).
  • session/: how a turn streams and is written again after a compaction cut does not change, and that is what this model covers. Two other parts of the session code change. Restore no longer trims a turn written again, since it already starts after the cut. And a checkpoint's provider block now goes to the model that wrote it, with another model's reasoning in the turns kept after it sent as text; tests/tla/compaction/ProviderCompaction.tla models that projection (Wire, KeptAfterBlock, checked by Readable and NoDroppedReasoning).
  • toolbox/: the compaction request sends the conversation as it was sent, toolbox menu included; compaction-policy.test.ts checks the menu across compactions.
  • subagent/: the compaction call in the step loop reads no subagent message queue, stop, or close, so SteerQueue and StopMessage still hold.

Other notes:

  • Two tests kill a stuck child after 60 s and 90 s (tests/e2e/provider-compaction.test.ts, tests/tla/drivers/compaction/provider.test.ts). They wait on the child's exit; the timer only stops a hang.
  • No new thread, lock or signal handler in Zig code. The new atomics are a cancel flag in two tests and the compactor's borrowed pointer to the turn's existing cancel flag (src/core/compactor/model.zig). The tests' SIGINT and SIGKILL go to the fx child process.
  • The change edits the fuzz setup, so the Fuzz workflow runs on this PR, including the two new targets.

@fazxes fazxes added the type: improvement Improves existing user-facing behavior label Nov 12, 2026
@fazxes
fazxes force-pushed the fazxes/provider-written-compaction branch 2 times, most recently from d72e656 to cff4c8f Compare November 12, 2026 22:59
@fazxes
fazxes force-pushed the fazxes/provider-written-compaction branch 2 times, most recently from e0517b3 to 629e366 Compare November 23, 2026 03:47
@fazxes
fazxes marked this pull request as ready for review November 23, 2026 05:34
- On models whose provider can compact a conversation, fx asks the provider to compact it, and later requests send the provider's block, then the newest 4 turns unchanged with their reasoning.
- If the provider's compaction fails, fx writes its own summary and keeps no turns after it.
- After a model switch, the earlier model's kept steps are sent as text.
- Models without provider compaction keep fx's own compactor.
- Stopping a compaction while the provider writes its block leaves the conversation as it was.
- A session killed after the block is saved resumes with the block and the kept turns.
- A running turn that doesn't fit is compacted whole, and only its message follows the block.
- A request the provider refuses for its size is compacted with fx's summary and sent again.
- `/compact` on these models runs right before the next message is sent.
- Resuming a session compacted in the middle of a turn no longer drops the steps saved after the compaction.
main's fx-core.wasm was already at 99.8% of its budget. Provider
compaction adds about 20 KB of shared compactor, session and gateway
code to it, which puts it over. The budget goes back to about 10%
above the measured size, as when it was set.
A fork of a session that holds a provider's block resumes with the block
first on the model that wrote it, and with fx's text summary of the same
turns on another model.
@fazxes
fazxes force-pushed the fazxes/provider-written-compaction branch from 629e366 to 3a380d6 Compare November 25, 2026 03:07
fx-term.wasm also carries the compactor, session and gateway code that
provider compaction adds, about 21 KB, which put it over its budget. Set
both wasm budgets just above the sizes CI measured, as main raises them.
The recorded compaction replies held the gateway's client session
fingerprint; replace it with zeros, which the tests never read.
@fazxes
fazxes merged commit 0176544 into main Nov 25, 2026
210 of 212 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: improvement Improves existing user-facing behavior

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant