fix(agentic): anchor context compression to provider token usage#1474
Merged
Conversation
- replace ratio-based compression thresholds with an input limit that reserves output and safety tokens - persist per-session token anchors from provider prompt usage and reuse matching anchors for later pressure estimates - prune token anchors after context rewrites, restores, and emergency truncation - remove compressionThreshold/threshold from session config, events, desktop APIs, and web UI contracts - improve compression diagnostics, stream usage logging, and saved-token percentage display
wsp1911
marked this pull request as ready for review
July 8, 2026 14:37
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
compressionThreshold/ compression-startthresholdcontract from Rust DTOs, frontend API types, event projection, and UI handlers.Fixes #
Type and Areas
Type:
bug fix / refactor
Areas:
Rust core, agentic session/execution/persistence, desktop/Tauri APIs, event contracts, AI adapters, web UI
Motivation / Impact
The compression trigger now tracks provider-visible request pressure more accurately and reserves response headroom before sending another round. This reduces the chance that local token estimates or stale ratio thresholds delay compression until the request is too close to the model context limit.
The change also removes the old threshold field from internal session/config/event surfaces and keeps the frontend compression UI aligned with the new contract. Compression cards now display saved percentage as the amount reduced, not the remaining compression ratio.
Verification
pnpm run type-check:webpassed.cargo test -p bitfun-core token_anchor --libpassed: 7 passed.cargo test -p bitfun-core compression_trigger_budget --libpassed: 2 passed.cargo test -p bitfun-core auto_compression_pressure --libpassed: 4 passed.cargo check --workspacepassed.Reviewer Notes
token-anchors.jsonand are pruned when context is rewritten, restored, or emergency-truncated.total_tokens >= input_limit, whereinput_limit = context_window - output_reserve - safety_reserve.max_tokensis unset; safety reserve is 10k tokens.compressionThresholdsession config andthresholdcompression-start event payload.Checklist