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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
//! Memory identity is published only by the final DML of its atomic create.

use khive_storage::note::Note;
use khive_storage::{SqlStatement, SqlValue};
use khive_types::{Details, KhiveError};
use serde_json::Value;
use uuid::Uuid;

use crate::atomic_message::{AtomicNoteOptions, AtomicNoteSpec};
use crate::atomic_runner::{run_atomic_unit, AtomicOpFailure, AtomicRunOutcome};
use crate::note_create::{prepare_note_create, KeyPublication, KEY_CLAIM};
use crate::{KhiveRuntime, NamespaceToken, RuntimeError, RuntimeResult};

pub struct KeyedMemorySpec<'a> {
    pub content: &'a str,
    pub key: &'a str,
    pub salience: f64,
    pub decay_factor: f64,
    pub properties: Value,
    pub source_id: Option<Uuid>,
    pub embedding_model: Option<&'a str>,
}

pub fn validate_memory_key(key: &str) -> RuntimeResult<()> {
    if key.len() > 512 || key.contains('\0') {
        return Err(RuntimeError::InvalidInput(
            "key must be at most 512 UTF-8 bytes and must not contain U+0000".into(),
        ));
    }
    Ok(())
}

fn idempotency_conflict(key: &str, existing: &Note) -> RuntimeError {
    KhiveError::conflict(format!(
        "idempotency_key_conflict: key {key:?} already exists; stored content differs (existing memory {})",
        existing.id
    ))
        .with_details(Details::new_owned([
            ("reason", "idempotency_key_conflict".into()),
            ("key", key.to_owned()),
            ("existing_id", existing.id.to_string()),
        ]))
        .into()
}

async fn resolve_holder(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    key: &str,
) -> RuntimeResult<Option<Note>> {
    let mut matches = runtime
        .notes(token)?
        .get_live_notes_by_key(token.namespace().as_str(), key, Some("memory"))
        .await?;
    match matches.len() {
        0 => Ok(None),
        1 => Ok(matches.pop()),
        _ => Err(RuntimeError::Internal(
            "memory key lookup returned multiple live holders".into(),
        )),
    }
}

struct VisibilityReceiptState {
    note_present: bool,
    epoch: Option<String>,
    receipt_present: bool,
    fences: Option<Vec<(String, u64)>>,
}

/// Join identity, independent provenance and original receipt in one snapshot.
/// Joins use note identity first so a conflicting namespace cannot disappear
/// behind a filter and masquerade as a genuinely absent receipt or marker.
async fn visibility_receipt_state(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    note_id: Uuid,
) -> RuntimeResult<VisibilityReceiptState> {
    let unavailable = || {
        crate::visibility_receipts::receipt_failure(
            "receipt_store_unavailable",
            Some(note_id),
            false,
        )
    };
    runtime
        .require_visibility_cutover()
        .map_err(|_| unavailable())?;
    let mut reader = runtime.sql().reader().await.map_err(|_| unavailable())?;
    let rows = reader
        .query_all(SqlStatement {
            sql: "SELECT n.kind AS note_kind, n.namespace AS note_namespace, \
              e.namespace AS epoch_namespace, e.epoch, \
              r.note_id AS receipt_id, r.namespace AS receipt_namespace, r.model_count, \
              f.namespace AS fence_namespace, f.model, f.ann_write_log_seq \
              FROM notes n LEFT JOIN memory_visibility_epochs e ON e.note_id = n.id \
              LEFT JOIN memory_visibility_receipts r ON r.note_id = n.id \
              LEFT JOIN memory_visibility_fences f ON f.note_id = n.id \
              WHERE n.id = ?1 ORDER BY r.namespace, f.namespace, f.model"
                .into(),
            params: vec![SqlValue::Text(note_id.to_string())],
            label: Some("memory-visibility-receipt-read".into()),
        })
        .await
        .map_err(|_| unavailable())?;
    let mut state = VisibilityReceiptState {
        note_present: !rows.is_empty(),
        epoch: None,
        receipt_present: false,
        fences: None,
    };
    let Some(first) = rows.first() else {
        return Ok(state);
    };
    let namespace = token.namespace().as_str();
    let text = |value: Option<&SqlValue>, expected: &str| match value {
        Some(SqlValue::Text(actual)) => actual == expected,
        _ => false,
    };
    let identity_matches = text(first.get("note_kind"), "memory")
        && text(first.get("note_namespace"), namespace)
        && text(first.get("epoch_namespace"), namespace);
    if identity_matches {
        if let Some(SqlValue::Text(epoch)) = first.get("epoch") {
            state.epoch = Some(epoch.clone());
        }
    }
    state.receipt_present = rows
        .iter()
        .any(|row| matches!(row.get("receipt_id"), Some(SqlValue::Text(_))));
    let expected_count = match first.get("model_count") {
        Some(SqlValue::Integer(count)) if *count >= 0 => usize::try_from(*count).ok(),
        _ => None,
    };
    let mut valid = text(first.get("note_kind"), "memory")
        && text(first.get("note_namespace"), namespace)
        && state.receipt_present
        && expected_count.is_some();
    let mut fences = std::collections::BTreeMap::new();
    for row in &rows {
        if (matches!(row.get("receipt_id"), Some(SqlValue::Text(_)))
            && !text(row.get("receipt_namespace"), namespace))
            || matches!(row.get("fence_namespace"), Some(SqlValue::Text(ns))
                if ns != namespace || !text(row.get("receipt_namespace"), ns))
        {
            state.epoch = None;
        }
        valid &= text(row.get("receipt_namespace"), namespace)
            && matches!(row.get("model_count"), Some(SqlValue::Integer(count))
                if usize::try_from(*count).ok() == expected_count);
        match (
            row.get("fence_namespace"),
            row.get("model"),
            row.get("ann_write_log_seq"),
        ) {
            (
                Some(SqlValue::Text(ns)),
                Some(SqlValue::Text(model)),
                Some(SqlValue::Integer(seq)),
            ) if ns == namespace && !model.is_empty() && *seq > 0 => {
                valid &= fences.insert(model.clone(), *seq as u64).is_none();
            }
            (
                Some(SqlValue::Null) | None,
                Some(SqlValue::Null) | None,
                Some(SqlValue::Null) | None,
            ) => {}
            _ => valid = false,
        }
    }
    if valid && expected_count == Some(fences.len()) {
        state.fences = Some(fences.into_iter().collect());
    }
    Ok(state)
}

/// Read original fences without issuing a token. Callers needing a replay token
/// must additionally apply the independent provenance classification below.
pub async fn memory_visibility_receipt(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    note_id: Uuid,
) -> RuntimeResult<Option<Vec<(String, u64)>>> {
    Ok(visibility_receipt_state(runtime, token, note_id)
        .await?
        .fences)
}

async fn classified_visibility_receipt(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    note_id: Uuid,
    replay: bool,
) -> RuntimeResult<Vec<(String, u64)>> {
    let state = visibility_receipt_state(runtime, token, note_id).await?;
    if replay && !state.note_present {
        // The attempt's own unit rolled back on the key claim, so this request
        // wrote nothing; the reason carries that to the error projection.
        return Err(KhiveError::not_found("memory", "keyed replay holder")
            .with_details(Details::new_owned([(
                "reason",
                "keyed_replay_holder_missing".into(),
            )]))
            .into());
    }
    let reason = match state.epoch.as_deref() {
        Some("modern") => {
            if let Some(fences) = state.fences {
                return Ok(fences);
            }
            "receipt_temporarily_unavailable"
        }
        Some("legacy") if !state.receipt_present => "legacy_receipt_absent",
        _ => "receipt_epoch_unknown",
    };
    Err(crate::visibility_receipts::receipt_failure(
        reason,
        Some(note_id),
        replay,
    ))
}

pub async fn create_keyed_memory(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    spec: KeyedMemorySpec<'_>,
) -> RuntimeResult<(Note, Option<Uuid>, bool)> {
    let (note, edge_id, replayed, _) =
        create_keyed_memory_with_report(runtime, token, spec).await?;
    Ok((note, edge_id, replayed))
}

/// Same as [`create_keyed_memory`], also returning the embedding-input
/// truncation report computed for this call. A replay stores nothing new but
/// retains the report from preparing this call's identical content.
pub async fn create_keyed_memory_with_report(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    spec: KeyedMemorySpec<'_>,
) -> RuntimeResult<(
    Note,
    Option<Uuid>,
    bool,
    crate::retrieval::EmbeddingTruncationReport,
)> {
    let (note, edge_id, replayed, _, report) =
        create_keyed_memory_with_receipt_and_report(runtime, token, spec).await?;
    Ok((note, edge_id, replayed, report))
}

/// Receipt-bearing keyed create. The original per-model receipt is retained
/// on replay, including an explicit receipt for a write with zero models.
pub async fn create_keyed_memory_with_receipt(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    spec: KeyedMemorySpec<'_>,
) -> RuntimeResult<(Note, Option<Uuid>, bool, Vec<(String, u64)>)> {
    let (note, edge_id, replayed, fences, _) =
        create_keyed_memory_with_receipt_and_report(runtime, token, spec).await?;
    Ok((note, edge_id, replayed, fences))
}

/// Return both the original visibility receipt and the embedding-input report.
/// Replays retain the stored fences and the report computed while preparing
/// this call's embedding input; the report does not describe the original write.
pub async fn create_keyed_memory_with_receipt_and_report(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    spec: KeyedMemorySpec<'_>,
) -> RuntimeResult<(
    Note,
    Option<Uuid>,
    bool,
    Vec<(String, u64)>,
    crate::retrieval::EmbeddingTruncationReport,
)> {
    validate_memory_key(spec.key)?;
    if spec.content.trim().is_empty() {
        return Err(RuntimeError::InvalidInput(
            "content must not be empty".into(),
        ));
    }
    let (mut prepared, annotation_ids) = prepare_note_create(
        runtime,
        AtomicNoteSpec {
            token,
            id: None,
            kind: "memory",
            name: None,
            content: spec.content,
            properties: Some(spec.properties),
        },
        AtomicNoteOptions {
            salience: Some(spec.salience),
            decay_factor: Some(spec.decay_factor),
            embedding_model: spec.embedding_model,
            key: Some(spec.key),
            memory_visibility_receipt: true,
            ..Default::default()
        },
        &spec.source_id.into_iter().collect::<Vec<_>>(),
        KeyPublication::AfterDependents,
    )
    .await?;
    let mut note = prepared.notes.remove(0);
    let edge_id = annotation_ids.first().copied();

    for _attempt in 0..2 {
        #[cfg(test)]
        crate::keyed_memory_tests::checkpoint(token.namespace().as_str(), _attempt, false).await;
        match run_atomic_unit(runtime.sql().as_ref(), prepared.plans.clone()).await {
            Ok(AtomicRunOutcome::Committed { .. }) => {
                note.key = Some(spec.key.to_owned());
                note.version = 2;
                let fences = classified_visibility_receipt(runtime, token, note.id, false).await?;
                return Ok((note, edge_id, false, fences, prepared.embedding_truncation));
            }
            Ok(AtomicRunOutcome::RolledBack {
                failure:
                    AtomicOpFailure::GuardFailed {
                        statement_label,
                        observed: 0,
                        ..
                    },
                ..
            }) if statement_label.as_deref() == Some(KEY_CLAIM) => {
                #[cfg(test)]
                crate::keyed_memory_tests::checkpoint(token.namespace().as_str(), _attempt, true)
                    .await;
                if let Some(holder) = resolve_holder(runtime, token, spec.key).await? {
                    if holder.content == spec.content {
                        let fences =
                            classified_visibility_receipt(runtime, token, holder.id, true).await?;
                        return Ok((holder, None, true, fences, prepared.embedding_truncation));
                    }
                    return Err(idempotency_conflict(spec.key, &holder));
                }
            }
            Ok(AtomicRunOutcome::RolledBack {
                failed_op_index,
                failure,
            }) => {
                // Admission is cached after the first successful cutover check,
                // so a receipt store that broke since then surfaces here.
                runtime.recheck_visibility_cutover()?;
                return Err(RuntimeError::Internal(format!(
                    "atomic memory write rolled back at op {failed_op_index}: {failure:?}"
                )));
            }
            Err(error) => return Err(RuntimeError::Storage(error.0)),
        }
    }
    Err(
        KhiveError::unavailable("memory key holder disappeared during reconciliation")
            .with_details(Details::new_owned([
                ("reason", "key_holder_unresolved".into()),
                ("key", spec.key.to_owned()),
            ]))
            .into(),
    )
}

#[cfg(test)]
mod receipt_read_tests {
    use super::*;
    use crate::DomainDisposition;

    const KEY: &str = "receipt-race-key";
    const CONTENT: &str = "private receipt race content";

    async fn captured_holder() -> (KhiveRuntime, NamespaceToken, Note) {
        let runtime = KhiveRuntime::memory().unwrap();
        runtime.install_kind_registry(vec![], vec!["memory".into()]);
        let token = runtime
            .authorize(khive_types::Namespace::parse("receipt-read-race").unwrap())
            .unwrap();
        let (note, _, replayed, fences) = create_keyed_memory_with_receipt(
            &runtime,
            &token,
            KeyedMemorySpec {
                content: CONTENT,
                key: KEY,
                salience: 0.7,
                decay_factor: 0.95,
                properties: serde_json::json!({}),
                source_id: None,
                embedding_model: None,
            },
        )
        .await
        .unwrap();
        assert!(!replayed);
        assert!(fences.is_empty());
        let holder = resolve_holder(&runtime, &token, KEY)
            .await
            .unwrap()
            .unwrap();
        assert_eq!(holder, note);
        assert!(
            classified_visibility_receipt(&runtime, &token, holder.id, true)
                .await
                .unwrap()
                .is_empty()
        );
        (runtime, token, holder)
    }

    #[tokio::test]
    async fn replay_receipt_reports_missing_after_captured_holder_is_hard_deleted() {
        let (runtime, token, holder) = captured_holder().await;
        // This is the exact boundary in the replay path: holder lookup has
        // completed, but its joined receipt/epoch read has not started.
        assert!(runtime.delete_note(&token, holder.id, true).await.unwrap());
        assert!(runtime
            .notes(&token)
            .unwrap()
            .get_note_including_deleted(holder.id)
            .await
            .unwrap()
            .is_none());
        let error = classified_visibility_receipt(&runtime, &token, holder.id, true)
            .await
            .unwrap_err();
        assert!(
            matches!(&error, RuntimeError::Khive(e) if e.kind() == khive_types::ErrorKind::NotFound)
        );
        let value = crate::error_projection::runtime_error_value(error, DomainDisposition::Unknown);
        assert_eq!(value["kind"], "not_found");
        assert_eq!(value["details"]["reason"], "keyed_replay_holder_missing");
        assert_eq!(value["domain_disposition"], "not_committed");
        assert!(value.get("retryable").is_none());
        let encoded = value.to_string();
        let id = holder.id.to_string();
        for private in [KEY, CONTENT, id.as_str()] {
            assert!(!encoded.contains(private));
        }
        assert!(memory_visibility_receipt(&runtime, &token, holder.id)
            .await
            .unwrap()
            .is_none());
        assert!(resolve_holder(&runtime, &token, KEY)
            .await
            .unwrap()
            .is_none());

        // The post-commit caller retains its existing uncertainty semantics.
        let error = classified_visibility_receipt(&runtime, &token, holder.id, false)
            .await
            .unwrap_err();
        let value = crate::error_projection::runtime_error_value(error, DomainDisposition::Unknown);
        assert_eq!(value["details"]["reason"], "receipt_epoch_unknown");
        assert_eq!(value["domain_disposition"], "unknown");
    }

    #[tokio::test]
    async fn present_holder_with_missing_or_invalid_epoch_is_not_missing() {
        for mutation in [
            "DELETE FROM memory_visibility_epochs WHERE note_id = ?1",
            "UPDATE memory_visibility_epochs SET epoch = 'unknown' WHERE note_id = ?1",
            "UPDATE memory_visibility_epochs SET epoch = 'malformed' WHERE note_id = ?1",
            "UPDATE memory_visibility_epochs SET epoch = 'legacy' WHERE note_id = ?1",
            "UPDATE memory_visibility_epochs SET namespace = 'foreign-private-namespace' WHERE note_id = ?1",
            "UPDATE notes SET namespace = 'foreign-private-namespace' WHERE id = ?1",
        ] {
            let (runtime, token, holder) = captured_holder().await;
            {
                // Model a damaged epoch value as well as valid but incomplete
                // provenance. Restore constraint checks before reading it.
                let writer = runtime.backend().pool().try_writer().unwrap();
                writer
                    .conn()
                    .pragma_update(None, "ignore_check_constraints", true)
                    .unwrap();
                writer.conn().execute(mutation, [holder.id.to_string()]).unwrap();
                writer
                    .conn()
                    .pragma_update(None, "ignore_check_constraints", false)
                    .unwrap();
            }
            assert!(runtime
                .notes(&token)
                .unwrap()
                .get_note_including_deleted(holder.id)
                .await
                .unwrap()
                .is_some());
            let error = classified_visibility_receipt(&runtime, &token, holder.id, true)
                .await
                .unwrap_err();
            let value = crate::error_projection::runtime_error_value(error, DomainDisposition::Unknown);
            assert_eq!(value["details"]["reason"], "receipt_epoch_unknown");
            assert_eq!(value["details"]["memory_id"], holder.id.to_string());
            assert_eq!(value["domain_disposition"], "not_committed");
            assert_eq!(value["retryable"], false);
            for private in [KEY, CONTENT, "foreign-private-namespace"] {
                assert!(!value.to_string().contains(private));
            }
        }
    }

    #[tokio::test]
    async fn unreadable_note_store_is_unavailable_not_missing() {
        let (runtime, token, holder) = captured_holder().await;
        runtime
            .sql()
            .writer()
            .await
            .unwrap()
            .execute(SqlStatement {
                sql: "ALTER TABLE notes RENAME TO private_unreadable_notes".into(),
                params: vec![],
                label: Some("receipt-unreadable-note-control".into()),
            })
            .await
            .unwrap();
        let error = classified_visibility_receipt(&runtime, &token, holder.id, true)
            .await
            .unwrap_err();
        let value = crate::error_projection::runtime_error_value(error, DomainDisposition::Unknown);
        assert_eq!(value["details"]["reason"], "receipt_store_unavailable");
        assert_eq!(value["details"]["memory_id"], holder.id.to_string());
        assert_eq!(value["domain_disposition"], "unknown");
        assert_eq!(value["retryable"], true);
        for private in [KEY, CONTENT, "private_unreadable_notes"] {
            assert!(!value.to_string().contains(private));
        }
    }
}