Summary
opencode-rag query fails intermittently with:
Query failed: Cannot read properties of undefined (reading 'optimized')
at dedupeSimilar (dist/retriever/context-optimizer.js:129:46)
at optimizeContext (dist/retriever/context-optimizer.js:182:21)
Root cause
In dedupeSimilar (dist/retriever/context-optimizer.js), when two chunks from the same file are similar (Jaccard similarity > similarityThreshold) and the higher-scored chunk sits at a higher index than the lower-scored one:
const [keepIdx, removeIdx] = kept[i].score >= kept[j].score ? [i, j] : [j, i];
const removedId = kept[removeIdx].chunk.id;
kept.splice(removeIdx, 1); // shifts every element down by one
kept[keepIdx] = { ...kept[keepIdx], ... }; // <-- kept[keepIdx] is undefined -> crash
When removeIdx < keepIdx, the splice shifts the keeper down, so kept[keepIdx] is undefined and reading .optimized throws.
Suggested fix
Build the keeper before splicing and adjust the index after removal:
const [keepIdx, removeIdx] = kept[i].score >= kept[j].score ? [i, j] : [j, i];
const removedId = kept[removeIdx].chunk.id;
const keeper = {
...kept[keepIdx],
optimized: {
...kept[keepIdx].optimized,
dedupedFrom: [
...(kept[keepIdx].optimized?.dedupedFrom ?? []),
removedId,
],
},
};
if (removeIdx < keepIdx) {
kept.splice(removeIdx, 1);
kept[keepIdx - 1] = keeper;
} else {
kept[keepIdx] = keeper;
kept.splice(removeIdx, 1);
}
Repro
- opencode-rag 1.22.0
opencode-rag query "authentication middleware" in a workspace whose index contains near-duplicate chunks from the same file — crashes every time; simple queries (e.g. opencode-rag query "test") work fine
- Verified locally that the fix restores results for previously-crashing queries and dedup bookkeeping is preserved (
dedupedFrom accumulates correctly)
Summary
opencode-rag queryfails intermittently with:Root cause
In
dedupeSimilar(dist/retriever/context-optimizer.js), when two chunks from the same file are similar (Jaccard similarity >similarityThreshold) and the higher-scored chunk sits at a higher index than the lower-scored one:When
removeIdx < keepIdx, thespliceshifts the keeper down, sokept[keepIdx]isundefinedand reading.optimizedthrows.Suggested fix
Build the keeper before splicing and adjust the index after removal:
Repro
opencode-rag query "authentication middleware"in a workspace whose index contains near-duplicate chunks from the same file — crashes every time; simple queries (e.g.opencode-rag query "test") work finededupedFromaccumulates correctly)