khive-runtime 0.10.0

Composable Service API: entity/note CRUD, graph traversal, hybrid search, curation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use std::any::Any;

use khive_storage::{note::Note, AtomicUnitOp, Entity, SqlStatement, SqlValue, TextDocument};
use khive_types::SubstrateKind;
use serde::Serialize;
use uuid::Uuid;

use crate::{KhiveRuntime, NamespaceToken, PostCommitDegradation, RuntimeError, RuntimeResult};

/// Per-record index work that actually committed. Failed stages can coexist
/// with repairs; rerunning repairs only the gaps that still remain.
#[derive(Clone, Debug, Serialize)]
pub struct IndexRepairReport {
    pub id: Uuid,
    pub substrate: SubstrateKind,
    pub namespace: String,
    pub repaired: Vec<String>,
    pub failures: Vec<PostCommitDegradation>,
}

enum Record {
    Entity(Entity),
    Note(Note),
}

impl Record {
    fn document(&self) -> TextDocument {
        match self {
            Self::Entity(entity) => crate::entity_fts_document(entity),
            Self::Note(note) => crate::note_fts_document(note),
        }
    }

    fn version(&self) -> i64 {
        match self {
            Self::Entity(entity) => entity.version,
            Self::Note(note) => note.version,
        }
    }

    fn tables(&self) -> (&'static str, &'static str, &'static str) {
        match self {
            Self::Entity(_) => ("entities", "fts_entities", "entity.body"),
            Self::Note(_) => ("notes", "fts_notes", "note.content"),
        }
    }

    fn vector_statements(&self, table: &str, model: &str, vector: &[f32]) -> Vec<SqlStatement> {
        match self {
            Self::Entity(entity) => {
                KhiveRuntime::entity_vector_insert_statements(table, entity, model, vector)
            }
            Self::Note(note) => crate::atomic_message::vector_insert_statements(
                table,
                &note.namespace,
                note.id,
                "note.content",
                model,
                vector,
                "record-index-repair",
            )
            .into_iter()
            .map(|plan| plan.statement)
            .collect(),
        }
    }
}

#[derive(Clone, Copy)]
enum Publication {
    Repaired,
    Healthy,
    Changed,
    Occupied,
}

fn statement(sql: String, params: Vec<SqlValue>) -> SqlStatement {
    SqlStatement {
        sql,
        params,
        label: Some("record-index-repair".into()),
    }
}

fn vector_checks(record: &Record, doc: &TextDocument, model: &str) -> (SqlStatement, SqlStatement) {
    let table = format!("vec_{}", crate::config::sanitize_key(model));
    let healthy = statement(format!("SELECT 1 FROM {table} WHERE subject_id=?1 AND namespace=?2 AND kind=?3 AND field=?4 AND embedding_model=?5"), vec![
        SqlValue::Text(doc.subject_id.to_string()), SqlValue::Text(doc.namespace.clone()), SqlValue::Text(doc.kind.to_string()), SqlValue::Text(record.tables().2.into()), SqlValue::Text(model.into()),
    ]);
    // Even a mismatched identity may occupy the vec0 subject primary key.
    // Repair preserves it instead of silently replacing an existing vector.
    let occupied = statement(
        format!("SELECT 1 FROM {table} WHERE subject_id=?1"),
        vec![SqlValue::Text(doc.subject_id.to_string())],
    );
    (healthy, occupied)
}

fn same_document(a: &TextDocument, b: &TextDocument) -> bool {
    a.subject_id == b.subject_id
        && a.kind == b.kind
        && a.record_kind == b.record_kind
        && a.namespace == b.namespace
        && a.title.as_deref().unwrap_or_default() == b.title.as_deref().unwrap_or_default()
        && a.body == b.body
        && a.tags == b.tags
        && a.metadata == b.metadata
        && a.updated_at == b.updated_at
}

impl IndexRepairReport {
    fn failure(&mut self, stage: &'static str, error: impl ToString) {
        self.failures.push(PostCommitDegradation {
            stage,
            error: error.to_string(),
        });
    }

    fn publication(
        &mut self,
        result: RuntimeResult<Publication>,
        stage: &'static str,
        label: String,
    ) -> bool {
        match result {
            Ok(Publication::Repaired) => self.repaired.push(label),
            Ok(Publication::Healthy) => {},
            Ok(Publication::Changed) => {
                self.failure("source_revision", "record changed or was deleted before index publication; rerun against the current record");
                return false;
            }
            Ok(Publication::Occupied) => self.failure(stage, format!("{label}: another vector identity occupies the subject; existing vector was preserved")),
            Err(error) => self.failure(stage, format!("{label}: {error}")),
        }
        true
    }
}

impl KhiveRuntime {
    /// Repair indexes for one unambiguous live ID in the token namespace.
    /// Only missing/stale FTS and missing kind-selected vectors are repaired.
    /// Existing vectors, substrate rows and other records are never rewritten.
    /// A later failure does not undo earlier reported repairs.
    pub async fn repair_record_indexes(
        &self,
        token: &NamespaceToken,
        id: Uuid,
    ) -> RuntimeResult<IndexRepairReport> {
        if self.is_read_only() {
            return Err(RuntimeError::InvalidInput(
                "record index repair requires a writable runtime".into(),
            ));
        }
        let namespace = token.namespace().as_str();
        let entity = self
            .entities(token)?
            .get_entity(id)
            .await?
            .filter(|entity| entity.namespace == namespace);
        let note = self
            .notes(token)?
            .get_note(id)
            .await?
            .filter(|note| note.namespace == namespace);
        let record = match (entity, note) {
            (Some(entity), None) => Record::Entity(entity),
            (None, Some(note)) => Record::Note(note),
            (Some(_), Some(_)) => return Err(RuntimeError::InvalidInput(format!("record {id} is ambiguous: both an entity and a note exist in namespace {namespace}"))),
            (None, None) => return Err(RuntimeError::NotFound(format!("live entity or note {id} in namespace {namespace}"))),
        };
        let doc = record.document();
        let mut report = IndexRepairReport {
            id,
            substrate: doc.kind,
            namespace: doc.namespace.clone(),
            repaired: vec![],
            failures: vec![],
        };
        let mut models = match &record {
            Record::Entity(_) => self.registered_embedding_model_names(),
            Record::Note(note) => self.embedding_models_for_note_kind(&note.kind),
        };
        models.sort();
        models.dedup();
        let text = match &record {
            Record::Entity(_) => self.text(token),
            Record::Note(_) => self.text_for_notes(token),
        };
        match text {
            Ok(text) => match text.get_document(&doc.namespace, id).await {
                Ok(Some(current)) if same_document(&current, &doc) => {}
                Ok(_) => {
                    let result = self.repair_record_fts(&record, &doc).await;
                    if !report.publication(result, "fts", "fts".into()) {
                        return Ok(report);
                    }
                }
                Err(error) => report.failure("fts", error),
            },
            Err(error) => report.failure("fts", error),
        }
        let body = match &record {
            Record::Entity(_) => doc.body.as_str(),
            Record::Note(note) => crate::curation::note_embedding_text_ref(note),
        };
        if body.trim().is_empty() {
            return Ok(report);
        }
        for model in models {
            let (storage_model, dimensions) = match self.vector_model_metadata(&model) {
                Ok(metadata) => metadata,
                Err(error) => {
                    report.failure("vector_presence", format!("{model}: {error}"));
                    continue;
                }
            };
            let store = match self.backend().vectors_for_namespace(
                &crate::config::sanitize_key(&storage_model),
                &storage_model,
                dimensions,
                &doc.namespace,
            ) {
                Ok(store) => store,
                Err(error) => {
                    report.failure("vector_presence", format!("{model}: {error}"));
                    continue;
                }
            };
            let (healthy, occupied) = vector_checks(&record, &doc, &storage_model);
            let presence: RuntimeResult<Option<Publication>> = async {
                let mut reader = self.sql().reader().await?;
                if reader.query_scalar(healthy).await?.is_some() {
                    Ok(Some(Publication::Healthy))
                } else if reader.query_scalar(occupied).await?.is_some() {
                    Ok(Some(Publication::Occupied))
                } else {
                    Ok(None)
                }
            }
            .await;
            match presence {
                Ok(Some(Publication::Healthy)) => {
                    #[cfg(test)]
                    tests::pause("vector_read").await;
                    match store
                        .get_vectors(&[id], &doc.namespace, record.tables().2)
                        .await
                    {
                        Ok(vectors) if vectors.contains_key(&id) => continue,
                        Ok(_) => report.failure(
                            "vector_presence",
                            format!(
                                "{model}: vector identity was present for {id}, \
                                 but its vector was not returned"
                            ),
                        ),
                        Err(error) => {
                            report.failure("vector_presence", format!("{model}: {error}"))
                        }
                    }
                    continue;
                }
                Ok(Some(Publication::Occupied)) => {
                    report.failure("vector_presence", format!("{model}: another vector identity occupies the subject; existing vector was preserved"));
                    continue;
                }
                Ok(_) => {}
                Err(error) => {
                    report.failure("vector_presence", format!("{model}: {error}"));
                    continue;
                }
            }
            let outcome = match self
                .embed_document_with_model_outcome_for_token(token, &model, body)
                .await
            {
                Ok(outcome) => outcome,
                Err(error) => {
                    report.failure("embedding", format!("{model}: {error}"));
                    continue;
                }
            };
            if outcome.vector.len() != dimensions
                || outcome.vector.iter().any(|value| !value.is_finite())
            {
                report.failure(
                    "vector_publication",
                    format!("{model}: embedding has invalid dimensions or non-finite values"),
                );
                continue;
            }
            let result = self
                .repair_record_vector(&record, &doc, &storage_model, &outcome.vector)
                .await;
            if !report.publication(
                result,
                "vector_publication",
                format!("vector:{storage_model}"),
            ) {
                break;
            }
        }
        Ok(report)
    }

    async fn repair_record_fts(
        &self,
        record: &Record,
        doc: &TextDocument,
    ) -> RuntimeResult<Publication> {
        let (_, table, _) = record.tables();
        let canonical = khive_db::stores::text::insert_document_statement(table, doc);
        let map = khive_db::stores::text::rowid_map_table(table);
        let healthy = statement(
            format!(
                "SELECT 1 FROM {table} AS t JOIN {map} AS m ON m.rowid=t.rowid \
            WHERE m.subject_id=?1 AND m.namespace=?6 AND t.subject_id=?1 AND t.kind=?2 \
            AND t.title=?3 AND t.body=?4 AND t.tags=?5 AND t.namespace=?6 \
            AND t.metadata IS ?7 AND t.updated_at=?8 AND t.record_kind IS ?9"
            ),
            canonical.params,
        );
        let statements = khive_db::stores::text::delete_document_statements(
            table,
            &doc.namespace,
            doc.subject_id,
        )
        .into_iter()
        .chain(khive_db::stores::text::insert_document_statements(
            table, doc,
        ))
        .collect();
        #[cfg(test)]
        tests::pause("fts").await;
        self.publish_record_repair(record, doc, healthy, None, statements)
            .await
    }

    async fn repair_record_vector(
        &self,
        record: &Record,
        doc: &TextDocument,
        model: &str,
        vector: &[f32],
    ) -> RuntimeResult<Publication> {
        let table = format!("vec_{}", crate::config::sanitize_key(model));
        let (healthy, occupied) = vector_checks(record, doc, model);
        let statements = record.vector_statements(&table, model, vector);
        #[cfg(test)]
        tests::pause("vector").await;
        self.publish_record_repair(record, doc, healthy, Some(occupied), statements)
            .await
    }

    async fn publish_record_repair(
        &self,
        record: &Record,
        doc: &TextDocument,
        healthy: SqlStatement,
        occupied: Option<SqlStatement>,
        statements: Vec<SqlStatement>,
    ) -> RuntimeResult<Publication> {
        let source = statement(
            format!(
                "SELECT version FROM {} WHERE id=?1 AND namespace=?2 AND deleted_at IS NULL",
                record.tables().0
            ),
            vec![
                SqlValue::Text(doc.subject_id.to_string()),
                SqlValue::Text(doc.namespace.clone()),
            ],
        );
        let version = record.version();
        let op: AtomicUnitOp = Box::new(move |writer| {
            Box::pin(async move {
                let current = writer.query_scalar(source).await?;
                let result = if !matches!(current, Some(SqlValue::Integer(current)) if current == version)
                {
                    Publication::Changed
                } else if writer.query_scalar(healthy).await?.is_some() {
                    Publication::Healthy
                } else if let Some(occupied) = occupied {
                    if writer.query_scalar(occupied).await?.is_some() {
                        Publication::Occupied
                    } else {
                        for statement in statements {
                            writer.execute(statement).await?;
                        }
                        Publication::Repaired
                    }
                } else {
                    for statement in statements {
                        writer.execute(statement).await?;
                    }
                    Publication::Repaired
                };
                Ok(Box::new(result) as Box<dyn Any + Send>)
            })
        });
        self.sql()
            .atomic_unit(op)
            .await?
            .downcast::<Publication>()
            .map(|result| *result)
            .map_err(|_| RuntimeError::Internal("invalid record index repair outcome".into()))
    }
}

#[cfg(test)]
#[path = "index_repair_tests.rs"]
mod tests;