Repository navigation
Add bounded snippets to retrieve responses - #345
Conversation
Closes #338 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
📝 WalkthroughWalkthroughThis PR adds token-budgeted code snippets to the ChangesSnippet Token Budgeting for Retrieve
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint skipped: no ESLint configuration detected in root package.json. To enable, add Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (2)
tests/unit/stdio-slice-surface.test.ts (1)
137-150: 💤 Low valueSnippet line comparison could be fragile with different line endings or formatting.
The snippet line comparison splits on
'\n'and trims whitespace. This works for controlled test data but could be fragile if line endings differ (e.g., CRLF vs LF on Windows) or if snippet formatting changes. However, since this is a controlled integration test with known fixture data, this approach is acceptable.Optional: More robust line comparison
If you want to make the comparison more robust:
const retrieveSnippetLines = new Set( (retrieveNode?.snippet ?? '') - .split('\n') + .split(/\r?\n/) .map((line) => line.trim()) .filter((line) => line.length > 0), ) const contextPackSnippetLines = new Set( (contextPackNode?.snippet ?? '') - .split('\n') + .split(/\r?\n/) .map((line) => line.trim()) .filter((line) => line.length > 0), )The regex
/\r?\n/handles both Unix (LF) and Windows (CRLF) line endings.🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unit/stdio-slice-surface.test.ts` around lines 137 - 150, The line-splitting in the test is fragile because it uses '\n' only; update the logic that builds retrieveSnippetLines and contextPackSnippetLines (the code that calls .split('\n') on retrieveNode?.snippet and contextPackNode?.snippet) to split on a regex that accepts both LF and CRLF (e.g., use /\r?\n/), then keep the existing .map(trim).filter steps and the final expect unchanged so comparisons succeed across platforms.tests/unit/retrieve.test.ts (1)
2814-2821: 💤 Low valueConsider extracting snippet token counting to a helper function.
The snippet token counting logic is duplicated here and in the third test (lines 2912-2915). While acceptable for test code, extracting it to a local helper function would reduce duplication and improve maintainability.
♻️ Optional refactor to reduce duplication
+ function estimateSnippetTokens(nodes: Array<{ snippet: string | null | undefined }>): number { + return nodes.reduce( + (total, node) => total + (typeof node.snippet === 'string' && node.snippet.length > 0 ? estimateQueryTokens(node.snippet) : 0), + 0, + ) + } + const snippetTokenCount = compactResult.matched_nodes.reduce( (total, node) => total + (typeof node.snippet === 'string' && node.snippet.length > 0 ? estimateQueryTokens(node.snippet) : 0), 0, )Or simply:
- const snippetTokenCount = compactResult.matched_nodes.reduce( - (total, node) => total + (typeof node.snippet === 'string' && node.snippet.length > 0 ? estimateQueryTokens(node.snippet) : 0), - 0, - ) + const snippetTokenCount = estimateSnippetTokens(compactResult.matched_nodes)🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@tests/unit/retrieve.test.ts` around lines 2814 - 2821, Extract the duplicated token-counting logic into a small helper function used by the tests: create a function (e.g., countMatchedNodeTokens(nodes)) that accepts compactResult.matched_nodes and returns both snippetTokenCount and compactTokenCount by calling estimateQueryTokens on node.snippet and on the composed node text (`${node.label} ${node.source_file}:${node.line_number} ${node.snippet ?? ''}`); replace the inline reductions that compute snippetTokenCount and compactTokenCount with calls to this helper to remove duplication and keep tests readable.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Nitpick comments:
In `@tests/unit/retrieve.test.ts`:
- Around line 2814-2821: Extract the duplicated token-counting logic into a
small helper function used by the tests: create a function (e.g.,
countMatchedNodeTokens(nodes)) that accepts compactResult.matched_nodes and
returns both snippetTokenCount and compactTokenCount by calling
estimateQueryTokens on node.snippet and on the composed node text
(`${node.label} ${node.source_file}:${node.line_number} ${node.snippet ?? ''}`);
replace the inline reductions that compute snippetTokenCount and
compactTokenCount with calls to this helper to remove duplication and keep tests
readable.
In `@tests/unit/stdio-slice-surface.test.ts`:
- Around line 137-150: The line-splitting in the test is fragile because it uses
'\n' only; update the logic that builds retrieveSnippetLines and
contextPackSnippetLines (the code that calls .split('\n') on
retrieveNode?.snippet and contextPackNode?.snippet) to split on a regex that
accepts both LF and CRLF (e.g., use /\r?\n/), then keep the existing
.map(trim).filter steps and the final expect unchanged so comparisons succeed
across platforms.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 6528ddf3-e624-4c49-a10a-b5ab2be553cc
📒 Files selected for processing (7)
src/runtime/retrieve.tssrc/runtime/stdio/definitions.tssrc/runtime/stdio/tools.tstests/unit/benchmark-artifact.test.tstests/unit/mcp-schema-budget.test.tstests/unit/retrieve.test.tstests/unit/stdio-slice-surface.test.ts
Summary
Testing
Closes #338
Summary by CodeRabbit
Release Notes
New Features
snippet_budget(default 3000 tokens) andtop_n_with_snippet(default 8 nodes) to control snippet text size and coverage.Updates