Skip to content

fix(kb): stop orphan sweep from purging an empty but present scan root - #1393

Merged
jakeaturner merged 1 commit into
devfrom
fix/1378-orphan-sweep-empty-root
Sep 29, 2026
Merged

jakeaturner merged 1 commit into
devfrom
fix/1378-orphan-sweep-empty-root

Conversation

@jakeaturner

Copy link
Copy Markdown
Collaborator

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

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

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jakeaturner
jakeaturner merged commit b021530 into dev Sep 29, 2026
2 checks passed
@jakeaturner
jakeaturner deleted the fix/1378-orphan-sweep-empty-root branch September 29, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants