fix: expand chunk IDs in remove() when chunking is enabled#13
fix: expand chunk IDs in remove() when chunking is enabled#13
Conversation
Documents indexed with a chunker are stored as `{id}#chunk-{i}` but
remove() was passing parent IDs directly to drivers, matching nothing.
Now retriv expands parent IDs to their chunk IDs via listIds() before
forwarding to drivers. Also adds listIds() to the sqlite-fts driver.
📝 WalkthroughWalkthroughThis change adds a provider API Changes
Sequence DiagramsequenceDiagram
actor Caller
participant Retriv as retriv.remove()
participant Provider as SearchProvider (driver)
participant ParentMap as parentDocs Map
Caller->>Retriv: remove(['doc1'])
alt chunking enabled
Retriv->>Provider: listIds()
Provider-->>Retriv: ['doc1','doc1#chunk-0','doc1#chunk-1','doc2',...]
Retriv->>Retriv: compute removeIds (include chunks whose parent == 'doc1')
end
Retriv->>Provider: remove(removeIds)
Provider-->>Retriv: ok
Retriv->>ParentMap: delete entries for 'doc1'
ParentMap-->>Retriv: ok
Retriv-->>Caller: complete
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 3✅ Passed checks (3 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
1 issue found across 3 files
Prompt for AI agents (unresolved issues)
Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.
<file name="test/retriv.test.ts">
<violation number="1" location="test/retriv.test.ts:149">
P2: Vacuous-truth risk: `every()` on an empty array always returns `true`, so this assertion passes even if `remove()` was never exercised (e.g. indexing silently failed). Asserting the length directly is more robust.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/retriv.ts`:
- Around line 224-239: When chunker is enabled the code currently only handles
chunk-aware deletes if at least one driver implements listIds(); to avoid
silently leaving chunk rows behind, add a guard after computing lister: if
chunker is true and no lister is found but there are drivers with a remove
method, throw an error (or return a rejected promise) explaining that drivers
must implement listIds() for chunked deletes; reference the existing
variables/functions chunker, drivers, lister, listIds(), remove, and removeIds
so you locate the check and replace the silent fallback with a fast failure.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 9ccdb3ad-6392-44ad-a597-73ab65e12a70
📒 Files selected for processing (3)
src/db/sqlite-fts.tssrc/retriv.tstest/retriv.test.ts
Address review feedback: - throw instead of silently falling back to bare IDs - use filter().toHaveLength(0) instead of every() to avoid vacuous truth
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/retriv.ts`:
- Around line 223-228: If remove([]) is called with an empty ids array, avoid
running the chunker expansion and potential listIds lookup; add an early no-op
return when ids is empty at the start of remove() (before the chunker logic).
Specifically, in remove() check if ids is falsy or length === 0 and return
immediately so the subsequent code that creates lister via drivers.find(d =>
d.listIds) and calls lister.listIds() is skipped, preventing the error when no
listIds-capable driver exists.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 970fa173-43dd-41db-8570-c4fd89214241
📒 Files selected for processing (2)
src/retriv.tstest/retriv.test.ts
✅ Files skipped from review due to trivial changes (1)
- test/retriv.test.ts
| let removeIds = ids | ||
| if (chunker) { | ||
| const lister = drivers.find(d => d.listIds) | ||
| if (!lister) | ||
| throw new Error('remove() with chunking requires a driver that implements listIds()') | ||
| const allIds = await lister.listIds!() |
There was a problem hiding this comment.
Handle empty remove() input as a fast no-op.
At Line 224, chunk-aware expansion runs even when ids is empty. With chunking enabled and no listIds() driver, remove([]) throws instead of returning a no-op result.
Suggested fix
async remove(ids: string[]) {
+ if (ids.length === 0)
+ return { count: 0 }
let removeIds = ids
if (chunker) {
const lister = drivers.find(d => d.listIds)
if (!lister)
throw new Error('remove() with chunking requires a driver that implements listIds()')🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@src/retriv.ts` around lines 223 - 228, If remove([]) is called with an empty
ids array, avoid running the chunker expansion and potential listIds lookup; add
an early no-op return when ids is empty at the start of remove() (before the
chunker logic). Specifically, in remove() check if ids is falsy or length === 0
and return immediately so the subsequent code that creates lister via
drivers.find(d => d.listIds) and calls lister.listIds() is skipped, preventing
the error when no listIds-capable driver exists.
remove()is a no-op when chunking is on. The driver only knows about chunk IDs likedoc1#chunk-0, butremove()sends the bare parent IDdoc1- no match, nothing deleted.Funny thing is
listIds()already handles the reverse direction (collapsing chunks back to parent IDs), remove() just never got thesame treatment.
Fix: query stored IDs first, match chunks to their parents, then forward the expanded list. Added
listIds()to sqlite-fts along the way since it was the only driver missing it.Summary by CodeRabbit
Bug Fixes
Tests