Skip to content

fix(session): add pre-send token budget guard and compaction fallback (#269) - #279

Open
innocarpe wants to merge 1 commit into
lessweb:mainfrom
innocarpe:fix/269-compaction-guard
Open

fix(session): add pre-send token budget guard and compaction fallback (#269)#279
innocarpe wants to merge 1 commit into
lessweb:mainfrom
innocarpe:fix/269-compaction-guard

Conversation

@innocarpe

Copy link
Copy Markdown

Summary

  • Adds a pre-send token budget guard so requests never exceed the model context window, even when the reported activeTokens undercounts the real message size.
  • compactSession now 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 repeated HTTP 400 errors.

Why

Long sessions (many file reads/edits, ~9.6 MB history) produced requests far larger than activeTokens indicated (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: add estimateOpenAIMessagesTokens() (chars/4 heuristic over the converted messages).
  • packages/core/src/session.ts: before sending, compact when estimatedTokens > contextWindow (in addition to the existing activeTokens threshold); rebuild messages after compaction. compactSession falls 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

…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)
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.

长会话上下文超限时自动压缩未触发,请求发送远超活跃 token 数,持续 HTTP 400

1 participant