fix(kb): stop orphan sweep from purging an empty but present scan root - #1393
Merged
Merged
Conversation
Boot runs ensureDirectoryExists() on the zim root, so a separate volume
that failed to mount leaves an empty mountpoint behind. The scan walks it
without error and reports it as scanned. One upload in kb_uploads keeps
the overall scan non-empty, so the next manual sync purged the vectors
for every ZIM under that root.
decideOrphans() now decides per scanned root and returns
{ orphans, withheld }. A root keeps its indexed sources when:
- it holds no embeddable files (kiwix-library.xml does not count)
- the purge would remove more than 50% of its indexed sources and at
least 5 of them, which points to a wrong or stale mount rather than
hand deletions
scanAndSyncStorage() logs each withheld root and appends a note to the
sync result message, so the user sees a likely unmounted drive instead
of losing hours of embeddings.
Replaces the unit test that asserted the old empty-root purge with
regression coverage for #1378 and both guards.
Closes #1378
There was a problem hiding this comment.
Copilot review overview
🟢 Approval recommended
The safeguards correctly address the reported data-loss scenario and are supported by focused regression coverage.
Review effort: Balanced
Findings: None
What changed in this PR
Prevents orphan cleanup from deleting indexed vectors when a scanned storage root appears unexpectedly empty.
Changes:
- Adds per-root empty-directory and mass-removal safeguards.
- Reports withheld purges through logs and sync results.
- Adds regression and boundary coverage for orphan decisions.
| File | Description |
|---|---|
admin/app/utils/kb_orphan_decision.ts |
Implements guarded per-root orphan decisions. |
admin/app/services/rag_service.ts |
Integrates decisions and operator warnings. |
admin/tests/unit/kb_orphan_decision.spec.ts |
Covers empty roots, missing roots, and purge thresholds. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Boot runs ensureDirectoryExists() on the zim root, so a separate volume that failed to mount leaves an empty mountpoint behind. The scan walks it without error and reports it as scanned. One upload in kb_uploads keeps the overall scan non-empty, so the next manual sync purged the vectors for every ZIM under that root.
decideOrphans() now decides per scanned root and returns { orphans, withheld }. A root keeps its indexed sources when:
scanAndSyncStorage() logs each withheld root and appends a note to the sync result message, so the user sees a likely unmounted drive instead of losing hours of embeddings.
Replaces the unit test that asserted the old empty-root purge with regression coverage for #1378 and both guards.
Closes #1378