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