Phase C: Decoupled embedding refresh

Implements the llms3.com-inspired pattern: embeddings refresh
asynchronously, decoupled from transactional row writes. New rows arrive,
ingest marks the vector index stale, a later refresh embeds only the
delta (doc_ids not already in the index).

Schema additions (DatasetManifest):
- last_embedded_at: Option<DateTime> - when the index was last refreshed
- embedding_stale_since: Option<DateTime> - set when data written, cleared on refresh
- embedding_refresh_policy: Option<RefreshPolicy> - Manual | OnAppend | Scheduled

Ingest paths (pipeline::ingest_file + pg_stream) call
registry.mark_embeddings_stale after writing. No-op if the dataset has
never been embedded — stale semantics only kick in once last_embedded_at
is set.

Refresh pipeline (vectord::refresh::refresh_index):
- Reads the dataset Parquet, extracts (doc_id, text) pairs
- Accepts Utf8 / Int32 / Int64 id columns (covers both CSV and pg schemas)
- Loads existing embeddings via EmbeddingCache (empty on first-time build)
- Filters to rows whose doc_id is NOT in the existing set
- Chunks (chunker::chunk_column), embeds via Ollama (batches of 32),
  writes combined index, clears stale flag

Endpoints:
- POST /vectors/refresh/{dataset_name} - body {index_name, id_column,
  text_column, chunk_size?, overlap?}
- GET /vectors/stale - lists datasets whose embedding_stale_since is set

End-to-end verified on threat_intel (knowledge_base.threat_intel):
- Initial refresh: 20 rows -> 20 chunks -> embedded in 2.1s,
  last_embedded_at set
- Idempotent second refresh: 0 new docs -> 1.8ms (pure delta check)
- Re-ingest to 54 rows: mark_embeddings_stale fires -> stale_since set
- /vectors/stale surfaces threat_intel with timestamps + policy
- Delta refresh: 34 new docs embedded in 970ms (6x faster than full
  re-embed); stale_cleared = true

Not in MVP scope:
- UPDATE semantics (same doc_id, different content) - would need
  per-row content hashing
- OnAppend policy auto-trigger - just declares intent; actual scheduler
  deferred
- Scheduler runtime - the Scheduled(cron) variant declares the intent so
  operators can see which datasets expect what, but the cron itself is
  separate

Per ADR-019: when a profile switches to vector_backend=Lance, this
refresh path benefits — Lance's native append replaces our "read all +
rewrite" Parquet rebuild pattern. Current MVP works well enough at
~500-5K rows to validate the architecture; Lance unblocks the 5M+ case.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
root
2026-04-16 03:00:43 -05:00
co-authored by Claude Opus 4.6
parent 76f6fba5de
commit 97a376482c
10 changed files with 472 additions and 2 deletions
+40
View File
@@ -116,4 +116,44 @@ pub struct DatasetManifest {
/// Row count (updated on ingest/compact)
#[serde(default)]
pub row_count: Option<u64>,
// --- Embedding freshness (Phase C) ---
/// When the attached vector index was last refreshed. `None` means this
/// dataset has no vector index yet, or has never been embedded.
#[serde(default)]
pub last_embedded_at: Option<chrono::DateTime<chrono::Utc>>,
/// When data was written that hasn't yet been embedded. `Some(t)` means
/// the vector index is out-of-date as of timestamp `t`. Cleared on refresh.
#[serde(default)]
pub embedding_stale_since: Option<chrono::DateTime<chrono::Utc>>,
/// How this dataset wants stale embeddings handled.
#[serde(default)]
pub embedding_refresh_policy: Option<RefreshPolicy>,
}
/// Controls what happens when new data is written to a dataset with an
/// attached vector index.
///
/// - `Manual` (default): data writes set `embedding_stale_since`; nothing
/// embeds until an operator or agent calls `/vectors/refresh/{dataset}`.
/// - `OnAppend`: ingest fires a background refresh immediately after writing.
/// Suitable for datasets where vector freshness matters more than ingest
/// latency.
/// - `Scheduled(cron)`: a timer or external scheduler triggers refresh at
/// the named cadence. The scheduler itself is not in this ADR scope —
/// this just declares the intent so operators can see which policy a
/// dataset expects.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(tag = "kind", rename_all = "snake_case")]
pub enum RefreshPolicy {
Manual,
OnAppend,
Scheduled { cron: String },
}
impl Default for RefreshPolicy {
fn default() -> Self { Self::Manual }
}