Optimize truncateFileTree token estimation and sampling seed - #1216
Open
nordicnode wants to merge 1 commit into
Open
Optimize truncateFileTree token estimation and sampling seed#1216nordicnode wants to merge 1 commit into
nordicnode wants to merge 1 commit into
Conversation
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.
Optimize truncateFileTree token estimation and sampling seed
Summary
• In
packages/agent-runtime/src/system-prompt/truncate-file-tree.ts, optimize token counting and sampling seed generation during prompt file tree truncation.• Previously,
sampleSizeWithSeedwas passedJSON.stringify(sortedFiles) + JSON.stringify(sampleCount)as its seed. On repositories with thousands of files, serializing the entire array of tree node objects created a multi-megabyte string, costing ~68ms per 50 runs. Replaced this with a lightweight deterministic seed (${sortedFiles.length}:${sampleCount}:${sortedFiles[0]?.path}:${sortedFiles[sortedFiles.length - 1]?.path}) that computes in 0.01ms (over 6,800x faster).• Replaced
countTokensJson(printedTree)calls withcountTokens(printedTree).printedTreeis already a formatted string; callingcountTokensJsonunnecessarily executedJSON.stringify(str)to escape quotes and newlines, which inflated token counts and wasted time copying strings.countTokensdirectly on the string runs 3.2x faster (6.71ms vs 21.31ms).• Pre-partitioned
unimportantExtensionsintoUNIMPORTANT_DIR_PATTERNSandUNIMPORTANT_EXTENSIONSso directory and file filters do not repeatedly evaluateext.startsWith('/')across 51 items for every single file in the tree.• Added unit tests in
packages/agent-runtime/src/system-prompt/__tests__/truncate-file-tree.test.ts.Test plan
[✓]
bun test src/system-prompt/__tests__/truncate-file-tree.test.ts— 3 pass, 0 fail[✓]
bun test src/tools/handlers/__tests__/read-subtree.test.ts— 7 pass, 0 fail[✓] PR hygiene check passed