fix: dedupe chunks across queries in a multi-query ctx_search call#921
Open
uwuclxdy wants to merge 1 commit into
Open
fix: dedupe chunks across queries in a multi-query ctx_search call#921uwuclxdy wants to merge 1 commit into
uwuclxdy wants to merge 1 commit into
Conversation
A chunk matching more than one query was printed verbatim under each query's section because no state was shared across the query loop. Track emitted chunks across the call's queries and keep only the first occurrence, so a chunk matching several queries appears once. Only multi-query calls change; single-query output stays byte-identical. When a later query's matches were all shown already, its section reads `(all matches already shown above)`.
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.
What / Why / How
Root cause:
ctx_searchruns each query inqueryListindependently and pushes a## ${q}section per query, with no state shared across the loop. A chunk that matches more than one query is printed verbatim under every matching query's section, spending context on duplicates.Fix: dedupe chunks across the call's queries with a shared
Setthat keeps the first occurrence. It lives in a small exported helper,dedupeAcrossQueries, so tests can hit it directly. NeitherSearchResultnorUnifiedSearchResulthas a stable chunk id. The key is therefore the query-independentsource+title+contenttuple; the per-query snippet cannot serve, becauseextractSnippetwindows it around each query's terms. Single-query output stays byte-identical: the dedup only runs whenqueryList.length > 1. A later query whose matches were all shown earlier now reads(all matches already shown above).Affected platforms
Test plan
npx tsc -b --noEmitclean.npx vitest run tests/core/search.test.tsgreen. Addedctx_search cross-query dedup, covering: a chunk matching two queries is emitted once with the first occurrence kept, and distinct chunks that sharesourceandtitleare not falsely deduped.Checklist
npm testpassesnpm run typecheckpassesnextbranch (unless hotfix)Cross-platform notes
Our CI runs on Ubuntu, macOS, and Windows.
path.join()/path.resolve(), never hardcode/separatorsreadFileSync(0)breaks on Windowsos.tmpdir(), never hardcode/tmp