Skip to main content

khive_pack_knowledge/
lib.rs

1//! pack-knowledge — knowledge corpus verbs for khive.
2
3mod feedback;
4pub(crate) mod handlers;
5pub(crate) mod knowledge;
6mod pack;
7mod vocab;
8
9pub use knowledge::snapshot_invalidation::invalidate_legacy_vamana_snapshots;
10pub use pack::KnowledgePack;
11
12use khive_runtime::{KhiveRuntime, NamespaceToken, RuntimeError};
13use serde_json::{json, Value};
14
15/// Options for [`reindex_knowledge`].
16#[derive(Debug, Clone, Copy)]
17pub struct KnowledgeReindexOptions {
18    /// Embed atoms (and rebuild the atom Vamana ANN).
19    pub atoms: bool,
20    /// Embed sections into `knowledge_sections.embedding` (ADR-051).
21    pub sections: bool,
22    /// Re-embed everything; when false, only fill missing vectors.
23    pub drop_existing: bool,
24    /// Rebuild the atom Vamana ANN snapshot (only meaningful with `atoms`).
25    pub rebuild_ann: bool,
26    /// Records per embedding batch.
27    pub batch_size: Option<u32>,
28}
29
30/// Reindex the knowledge corpus for `token`'s namespace: embed atoms and/or
31/// sections with the default embedder and (optionally) rebuild the atom Vamana
32/// ANN snapshot.
33///
34/// Library entry for `kkernel reindex` — callable without an MCP server. Every
35/// effect is scoped to `token`'s namespace; the whole-database FTS rebuild is
36/// deliberately not an option here but a separate operator entry point,
37/// [`rebuild_knowledge_fts_indexes`], so that holding a namespace token never
38/// grants a cost that spans every namespace.
39///
40/// Knowledge is single-model: atom indexing, section indexing, and search all
41/// use the default embedder. Returns `{atoms_indexed, sections_indexed, failed,
42/// ann_failed, sections_failed, sections_superseded, truncation_by_model}`.
43/// `sections_superseded` counts conditional vector writes rejected because the
44/// section or its atom changed while embedding was in flight; these are not
45/// storage failures.
46///
47/// Optional progress callbacks receive `(processed, total)` after each batch.
48pub async fn reindex_knowledge(
49    runtime: &KhiveRuntime,
50    token: &NamespaceToken,
51    opts: KnowledgeReindexOptions,
52    on_atom_progress: Option<&(dyn Fn(u64, u64) + Send + Sync)>,
53    on_section_progress: Option<&(dyn Fn(u64, u64) + Send + Sync)>,
54) -> Result<Value, RuntimeError> {
55    let mut atoms_indexed = 0u64;
56    let mut failed = 0u64;
57    let mut ann_failed = false;
58    let mut truncation_by_model = serde_json::Map::new();
59    if opts.atoms {
60        let ann = knowledge::vamana::new_shared();
61        let mut params = serde_json::Map::new();
62        params.insert("rebuild_ann".into(), Value::Bool(opts.rebuild_ann));
63        params.insert("insert_only".into(), Value::Bool(!opts.drop_existing));
64        if let Some(bs) = opts.batch_size {
65            params.insert("batch_size".into(), Value::from(bs));
66        }
67        let result = knowledge::KnowledgeHandlers::index(
68            runtime,
69            token,
70            Value::Object(params),
71            &ann,
72            on_atom_progress,
73        )
74        .await?;
75        atoms_indexed = result.get("indexed").and_then(|n| n.as_u64()).unwrap_or(0);
76        failed = result.get("failed").and_then(|n| n.as_u64()).unwrap_or(0);
77        ann_failed = result
78            .get("ann_failed")
79            .and_then(|b| b.as_bool())
80            .unwrap_or(false);
81        truncation_by_model = result
82            .get("truncation_by_model")
83            .and_then(Value::as_object)
84            .cloned()
85            .unwrap_or_default();
86    }
87
88    let mut sections_indexed = 0u64;
89    let mut sections_failed = 0u64;
90    let mut sections_superseded = 0u64;
91    if opts.sections {
92        let batch = opts
93            .batch_size
94            .map(|b| b as usize)
95            .unwrap_or(knowledge::util::DEFAULT_EMBED_BATCH);
96        let (indexed, _skipped, superseded, sec_failed, section_truncation) =
97            knowledge::sections_index::embed_sections(
98                runtime,
99                token,
100                opts.drop_existing,
101                batch,
102                on_section_progress,
103                None,
104            )
105            .await?;
106        sections_indexed = indexed as u64;
107        sections_failed = sec_failed as u64;
108        sections_superseded = superseded as u64;
109        if section_truncation.any_truncated() {
110            let model = runtime.default_embedder_name().to_string();
111            let existing = truncation_by_model
112                .remove(&model)
113                .and_then(|value| {
114                    serde_json::from_value::<khive_runtime::retrieval::EmbeddingTruncationReport>(
115                        value,
116                    )
117                    .ok()
118                })
119                .unwrap_or_default();
120            let mut combined: khive_runtime::retrieval::EmbeddingTruncationReport = existing;
121            combined.merge(section_truncation);
122            truncation_by_model.insert(
123                model,
124                serde_json::to_value(combined)
125                    .expect("EmbeddingTruncationReport serialization cannot fail"),
126            );
127        }
128    }
129
130    Ok(json!({
131        "atoms_indexed": atoms_indexed,
132        "sections_indexed": sections_indexed,
133        "failed": failed,
134        "ann_failed": ann_failed,
135        "sections_failed": sections_failed,
136        "sections_superseded": sections_superseded,
137        "truncation_by_model": truncation_by_model,
138    }))
139}
140
141/// Rebuild and rank-1 integrity-check both external-content knowledge FTS
142/// indexes (`fts_knowledge`, `fts_sections`).
143///
144/// Operator entry point for `kkernel reindex --rebuild-fts`. Both indexes are
145/// global — they cover every namespace in the database — so this takes no
146/// namespace token and is never reachable from a namespace-scoped verb: the
147/// `knowledge.index` MCP verb does not accept it, and [`reindex_knowledge`]
148/// does not offer it as an option, because neither has per-caller admission
149/// control to bound a whole-database cost. The writer batch is atomic, and the
150/// returned `{indexes, elapsed_ms, integrity_ok}` reports what the rebuild
151/// actually did.
152pub async fn rebuild_knowledge_fts_indexes(runtime: &KhiveRuntime) -> Result<Value, RuntimeError> {
153    knowledge::index_handler::rebuild_fts_indexes(runtime).await
154}