mirror of
https://github.com/Crosstalk-Solutions/project-nomad.git
synced 2026-10-17 05:14:19 +02:00
* feat(KB): per-file ingest state machine (Phase 1 of RFC #883) Adds a persistent state machine for AI knowledge-base ingestion so the scanner can distinguish "fully indexed", "user opted out", "failed", and "stalled" from each other — none of which were derivable from the prior binary "any chunks in Qdrant ⇒ embedded" check. ## What lands - New table `kb_ingest_state` keyed by `file_path` with enum state column (`pending_decision | indexed | browse_only | failed | stalled`). Independent of `installed_resources` so it covers both curated downloads and manually-uploaded KB files. - New KV key `rag.defaultIngestPolicy` (string: `Always | Manual`). Registered now but not consumed yet — JIT prompt + wizard step land in Phase 3 of the RFC. - `EmbedFileJob.handle` writes state on terminal outcomes: - Success (final batch) → `indexed` + chunks count - `UnrecoverableError` → `failed` + error message - Retryable errors are left to BullMQ's existing retry path - `scanAndSyncStorage` swaps the binary qdrant check for a state-aware decision tree (see `decideScanAction`). Existing installs auto-backfill on first scan: files with chunks in Qdrant but no state row become `indexed`; new files start as `pending_decision`. - `deleteFileBySource` drops the state row last, so removed files disappear entirely instead of leaving an orphan that the next scan would re-dispatch into nothing. ## What does NOT land here - Ratio registry (separate PR) — needed for partial-stall detection and cost estimates, but a separable concern. - #880 follow-up initial-progress anchor (separate tiny PR). - Phase 2 UI (status pill, per-card actions, conditional warnings). - Phase 3 policy surfaces (wizard step, JIT prompt, guardrail modal). - PR #886's bulk-action hookup — `_deletePointsBySource` / Re-embed All / Reset & Rebuild would also want to set state, but #886 isn't merged yet; that wiring goes in a follow-up once #886 lands. ## Target This is forward work for v1.40.0 (RFC #883). Branching off `rc` because that's the current latest base and post-GA Jake will sync rc→dev; a retarget at PR-open time is a fast-forward if requested. ## Tests - 9 new unit tests for `decideScanAction` covering all five states plus the no-row / chunks-present / chunks-missing combinations - Type-check clean - Smoke-tested end-to-end on NOMAD3 via hot-patch: - Backfill: 5 ZIMs + 2 KB uploads with existing chunks in Qdrant all came back `indexed` on first scan - Pending dispatch: a video-only ZIM with no chunks (`lrnselfreliance`) came back `pending_decision` and was correctly re-dispatched (Bull deduped to its historical `:completed` jobId — bgauger's #886 fix drains that) - Delete hook: deleting a KB upload via `DELETE /api/rag/files` removed both the disk file and the state row * feat(KB): Always/Manual ingest policy toggle (RFC #883 §1/§4) Activates the `rag.defaultIngestPolicy` KV registered in Phase 1 (#888) so users on a fresh install (or anyone who picks Manual mode) no longer get every new ZIM auto-dispatched to the embed pipeline. ## Stacks on #888 This PR's base is `feat/kb-ingest-state-machine` (#888). The state machine has to be in place for the decision function to be policy-aware; GitHub will fast-forward the base to `rc` once #888 merges. ## Backend changes - `decideScanAction` now takes a `policy: 'Always' | 'Manual'` argument (defaults to `Always` for backward compatibility). - New `ScanAction` kind: `create_pending`. Manual mode records that the scanner has seen a new file (so the UI can surface a per-card Index affordance later) without dispatching an EmbedFileJob. - `scanAndSyncStorage` reads the KV and passes it through. The scan-result log line now includes the active policy and a `waiting on user` count for Manual-mode hits. - `rag.defaultIngestPolicy` added to `SETTINGS_KEYS` so it's reachable through the existing `GET/PATCH /api/system/settings` surface — no new endpoint. ## Frontend changes - New section in the KB panel between "Why upload" and "Processing Queue": "Auto-index new content for AI? [Always | Manual]" — segmented radio with copy explaining the 5-10× disk multiplier. Default Always. - `useQuery('ingestPolicy')` reads the current value; clicking the inactive option mutates and shows a notification confirming the new behavior. ## Tests - 14 unit tests on `decideScanAction` (was 9) — split into Always-mode cases (preserves Phase 1's contract) and Manual-mode cases (`create_pending`, `pending_decision → skip`, etc.). - Type-check clean. - Hot-patch + browser verification deferred until #888 lands; the state machine smoke-tested cleanly on NOMAD3 in #888's PR, and this PR's decision-tree changes are exhaustively unit-tested. ## RFC open question §3 — policy-change re-trigger Switching Manual → Always doesn't auto-dispatch existing `pending_decision` rows immediately. The next scan re-evaluates and dispatches them under the new policy. This matches the RFC's "treat the switch as I've- thought-about-it" instinct for the guardrail; full guardrail implementation lands in Phase 3 task 14. --------- Co-authored-by: Jake Turner <52841588+jakeaturner@users.noreply.github.com>
71 lines
2.8 KiB
TypeScript
71 lines
2.8 KiB
TypeScript
import type { KbIngestStateValue } from '../../types/kb_ingest_state.js'
|
|
|
|
/**
|
|
* Decision returned by `decideScanAction` describing what scanAndSyncStorage
|
|
* should do for one file given its current state row (if any), whether Qdrant
|
|
* already has chunks for it, and the global ingest policy.
|
|
*
|
|
* - `skip` — file is in a settled state (already indexed, deliberately not
|
|
* indexed, or in a manual-recovery state); no auto-dispatch.
|
|
* - `dispatch` — file needs to be (re-)embedded; an EmbedFileJob should be
|
|
* dispatched. `createStateRow` indicates whether a new state row needs to
|
|
* be created before dispatch (i.e. first time the scanner has seen it).
|
|
* - `backfill_indexed` — Qdrant has chunks but no state row exists yet
|
|
* (pre-RFC install, or new admin instance pointed at an existing Qdrant
|
|
* volume). Create a row in `indexed` state without re-embedding.
|
|
* - `create_pending` — Manual mode: record that we've seen the file but
|
|
* don't dispatch. Frontend surfaces a per-card "Index" affordance.
|
|
*/
|
|
export type ScanAction =
|
|
| { kind: 'skip' }
|
|
| { kind: 'dispatch'; createStateRow: boolean }
|
|
| { kind: 'backfill_indexed' }
|
|
| { kind: 'create_pending' }
|
|
|
|
export interface KbIngestStateRow {
|
|
state: KbIngestStateValue
|
|
}
|
|
|
|
/**
|
|
* Global auto-index policy stored at KV `rag.defaultIngestPolicy`. Unset is
|
|
* treated as `Always` so existing installs keep their current behavior until
|
|
* the user opts into Manual mode through the KB panel.
|
|
*/
|
|
export type IngestPolicy = 'Always' | 'Manual'
|
|
|
|
/**
|
|
* Decide what scanAndSyncStorage should do for a single embeddable file.
|
|
*
|
|
* Replaces the earlier `!sourcesInQdrant.has(filePath)` binary check, which
|
|
* couldn't tell a fully-indexed file from a stalled mid-batch ingestion, and
|
|
* couldn't honor a user's "browse only" choice. The state row is now the
|
|
* authoritative answer; Qdrant chunk presence is corroborating evidence.
|
|
*/
|
|
export function decideScanAction(
|
|
stateRow: KbIngestStateRow | null,
|
|
hasChunksInQdrant: boolean,
|
|
policy: IngestPolicy = 'Always'
|
|
): ScanAction {
|
|
if (!stateRow) {
|
|
if (hasChunksInQdrant) return { kind: 'backfill_indexed' }
|
|
return policy === 'Always'
|
|
? { kind: 'dispatch', createStateRow: true }
|
|
: { kind: 'create_pending' }
|
|
}
|
|
|
|
switch (stateRow.state) {
|
|
case 'indexed':
|
|
return hasChunksInQdrant ? { kind: 'skip' } : { kind: 'dispatch', createStateRow: false }
|
|
case 'pending_decision':
|
|
// Manual mode: file is waiting for the user to opt in via per-card Index.
|
|
// Always mode: treat as "user-equivalent of auto-index" and dispatch.
|
|
return policy === 'Always'
|
|
? { kind: 'dispatch', createStateRow: false }
|
|
: { kind: 'skip' }
|
|
case 'browse_only':
|
|
case 'failed':
|
|
case 'stalled':
|
|
return { kind: 'skip' }
|
|
}
|
|
}
|