fix(session): add pre-send token budget guard and compaction fallback (#269) - #279
Open
innocarpe wants to merge 1 commit into
Open
fix(session): add pre-send token budget guard and compaction fallback (#269)#279innocarpe wants to merge 1 commit into
innocarpe wants to merge 1 commit into
Conversation
…lessweb#269) Estimate the request token count before sending and force compaction when it would exceed the model context window, even if reported activeTokens undercounts (e.g. large tool outputs). compactSession now falls back to truncating the oldest messages without a summary if the summarization call itself fails, so the session can continue instead of deadlocking on repeated 400 errors. (cherry picked from commit ea97c568b56f0b5d7e458d727950b3e75bbc4deb)
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
activeTokensundercounts the real message size.compactSessionnow degrades gracefully: if the summarization call itself fails (e.g. the request already overflows the context), the oldest messages are still dropped so the session can continue instead of deadlocking on repeatedHTTP 400errors.Why
Long sessions (many file reads/edits, ~9.6 MB history) produced requests far larger than
activeTokensindicated (4.98M requested vs 524K reported), and compaction never kicked in — every retry failed with 400 and the session was unrecoverable except by starting over.Changes
packages/core/src/common/openai-message-converter.ts: addestimateOpenAIMessagesTokens()(chars/4 heuristic over the converted messages).packages/core/src/session.ts: before sending, compact whenestimatedTokens > contextWindow(in addition to the existingactiveTokensthreshold); rebuild messages after compaction.compactSessionfalls back to truncating oldest messages without a summary when the LLM summarization call fails (non-interrupt failures).packages/core/src/tests/openai-message-converter.test.ts: tests for the estimator (text, image data-URIs, tool calls).Validation
npm run typecheck✅npm test— estimator tests pass ✅Closes #269