feat(#531): max_tokens two-step packing for codedb_context#610
Merged
Conversation
Pick 5 of the #531 roadmap: every truncation in the composer was by lines/bytes, never tokens, and an oversized response could not be bounded by the caller. handleContext now renders in two steps. Step 1 writes each section — reader.md, head, symbol definitions (a rich variant with inlined bodies and a lean def-lines-only fallback), callers, calls, file list, snippets — into its own arena buffer. Step 2 admits sections by VALUE order (head always, then files, symbols rich-or-lean, reader.md, callers, calls, snippets) under the byte budget (max_tokens x 4, the ~4-chars-per-token estimate) and emits the admitted ones in DOCUMENT order; every omitted section leaves a one-line [max_tokens: omitted ...] marker with its approximate cost. Without max_tokens every section is admitted, so existing output is unchanged — the pre-existing context tests pass untouched. Suite: 814/814. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Benchmark Regression ReportThresholds: 10.00% and 50,000 ns absolute delta
|
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.
Pick 5 of the #531 roadmap (token-budget-aware context packing) — step 2 of the two-step finish.
What
codedb_contextgainsmax_tokens(~4 chars/token estimate, min 256). The handler now works in two steps:[max_tokens: omitted … (~N tokens)]marker so the agent knows what to raise the budget for or fetch directly.So "give me the best ~2k tokens for this task" keeps the cheap high-value head (file ranking, symbol definition locations) and sheds the fat tail (snippet blocks, inlined bodies) — instead of truncating mid-section.
Compatibility
Without
max_tokensevery section is admitted and emitted in the original order — output is unchanged, and the pre-existing context tests (issue-570 fallback, etc.) pass untouched. The pure-render refactor also tightened the shared-lock scope to just the snippet-extraction block.Tests
issue-531: codedb_context max_tokens packs sections by value under the budget— fat symbol body + five mention files; asserts: unbudgeted output unchanged (no markers) and larger; budgeted output ≤ budget×4 + marker slack; head and## Most-relevant filessurvive while## Top sitesis dropped.Suite: 814/814.
🤖 Generated with Claude Code