fix: stabilize gradient cache with budget snapshot, hysteresis, and per-session LTM#144
Merged
fix: stabilize gradient cache with budget snapshot, hysteresis, and per-session LTM#144
Conversation
Replace N individual TOML fetches from GitHub with a single request to https://models.dev/api.json — returns all providers/models with pricing. Filter for anthropic provider, extract cost.input per model. Separate 1h cost cache alongside model discovery cache. Falls back to hardcoded costs only when models.dev is unreachable.
…er-session LTM Addresses 79% global cache-bust rate (366/464 turns) caused by three compounding mechanisms in tryFitStable(): 1. Budget snapshot: store rawBudget in RawWindowCache.pinnedBudget at pin creation so the pin check uses the same budget the window was built against, not a budget that drifted from global state changes. 2. Hysteresis: use pinnedBudget * 1.10 threshold in pin validity check to absorb minor fluctuations from deduplicateToolOutputs() token estimate changes and overhead EMA drift. 3. Per-session ltmTokens: move from module-level global to SessionState to eliminate cross-session budget contamination (e.g. a 928K-usable gateway session polluting a 118K-usable OpenCode session's budget).
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
Fixes a 79% global cache-bust rate (366/464 turns across all active sessions) caused by three compounding instabilities in
tryFitStable().Root Cause
The raw window's leading-edge message ID shifts on nearly every transform due to:
Budget drift from shared globals —
calibratedOverhead(EMA) andltmTokensare module-level globals shared across all sessions. Interleaved sessions (e.g. gateway at 928K usable vs OpenCode at 118K) contaminate each other's budget calculations, causingrawBudgetto swing by ~8-24K tokens between consecutive transforms of the same session.Token estimate mutation —
deduplicateToolOutputs()replaces earlier duplicate tool outputs with compact annotations, changingestimateMessage()costs throughout the window. This shifts the backward-scan cutoff even when the budget is stable.No tolerance in pin check —
tryFitStableevicts the pinned window on any overflow (pinnedTokens > rawBudget), even a 1-token overshoot from estimation noise.Fix (three complementary approaches)
Budget snapshot — Store
rawBudgetinRawWindowCache.pinnedBudgetat pin creation. The pin check uses this snapshot instead of the current (possibly drifted) budget.Hysteresis — Pin validity uses
pinnedBudget * 1.10(10% margin) to absorb residual noise from dedup token estimate changes and overhead EMA drift.Per-session
ltmTokens— Moved from module-level global toSessionState.setLtmTokens(tokens, sessionID?)stores per-session when ID is provided, with module-level fallback for backward compatibility. Eliminates cross-session budget contamination at the source.Changes
packages/core/src/gradient.tsRawWindowCache.pinnedBudget, hysteresis check, per-sessionltmTokensinSessionStatepackages/core/test/gradient.test.tssetLtmTokens(tokens, sessionID)packages/opencode/src/index.tssessionIDtosetLtmTokens(6 call sites)packages/gateway/src/pipeline.tssessionIDtosetLtmTokens(4 call sites)packages/pi/src/index.tssessionIDtosetLtmTokens(2 call sites).lore.mdTest Results
All 861 tests pass across 33 files.