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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
use super::*;
use khive_types::Pack;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Mutex as StdMutex;

/// A probe hook shaped like a real kind-owning pack: `normalize_note_update`
/// moves a caller-supplied top-level field into `properties`, and
/// `validate_note_update` refuses based on what it finds there. The value
/// `validate_note_update` inspects does not exist in `properties` until
/// `normalize_note_update` puts it there, so a passing refusal assertion
/// proves both the ordering and that normalize's mutation reached validate.
#[derive(Debug, Default)]
struct SequencerProbeHook {
    normalize_calls: AtomicUsize,
    validate_calls: AtomicUsize,
    validate_saw_marker: StdMutex<Option<bool>>,
    effects_calls: AtomicUsize,
    effects: StdMutex<Vec<NoteUpdateEffect>>,
}

#[async_trait]
impl KindHook for SequencerProbeHook {
    async fn prepare_create(
        &self,
        _runtime: &KhiveRuntime,
        _args: &mut Value,
    ) -> Result<(), RuntimeError> {
        Ok(())
    }

    async fn normalize_note_update(
        &self,
        _runtime: &KhiveRuntime,
        _token: &NamespaceToken,
        _note: &khive_storage::Note,
        args: &mut Value,
    ) -> Result<(), RuntimeError> {
        self.normalize_calls.fetch_add(1, Ordering::SeqCst);
        let Some(raw) = args.get("raw_marker").and_then(Value::as_bool) else {
            return Ok(());
        };
        let root = args.as_object_mut().expect("probe test args are an object");
        root.remove("raw_marker");
        let mut properties = serde_json::Map::new();
        properties.insert("marker".into(), Value::Bool(raw));
        root.insert("properties".into(), Value::Object(properties));
        Ok(())
    }

    async fn validate_note_update(
        &self,
        _runtime: &KhiveRuntime,
        _token: &NamespaceToken,
        _note: &khive_storage::Note,
        properties: Option<&Value>,
    ) -> Result<(), RuntimeError> {
        self.validate_calls.fetch_add(1, Ordering::SeqCst);
        let marker = properties
            .and_then(|value| value.get("marker"))
            .and_then(Value::as_bool);
        *self.validate_saw_marker.lock().unwrap() = marker;
        if marker == Some(true) {
            return Err(RuntimeError::InvalidInput(
                "probe validator refuses marker=true".into(),
            ));
        }
        Ok(())
    }
    async fn note_update_effects(
        &self,
        _runtime: &KhiveRuntime,
        _token: &NamespaceToken,
        _note: &khive_storage::Note,
        _patch: &crate::NotePatch,
    ) -> Result<Vec<NoteUpdateEffect>, RuntimeError> {
        self.effects_calls.fetch_add(1, Ordering::SeqCst);
        Ok(self.effects.lock().unwrap().clone())
    }
}

struct ProbePack(Arc<SequencerProbeHook>);

impl Pack for ProbePack {
    const NAME: &'static str = "sequencer-probe";
    const NOTE_KINDS: &'static [&'static str] = &["probe-note"];
    const ENTITY_KINDS: &'static [&'static str] = &[];
    const HANDLERS: &'static [HandlerDef] = &[];
}

#[async_trait]
impl PackRuntime for ProbePack {
    fn name(&self) -> &str {
        Self::NAME
    }
    fn note_kinds(&self) -> &'static [&'static str] {
        Self::NOTE_KINDS
    }
    fn entity_kinds(&self) -> &'static [&'static str] {
        Self::ENTITY_KINDS
    }
    fn handlers(&self) -> &'static [HandlerDef] {
        Self::HANDLERS
    }
    fn kind_hook(&self, kind: &str) -> Option<Arc<dyn KindHook>> {
        (kind == "probe-note").then(|| self.0.clone() as Arc<dyn KindHook>)
    }
    async fn dispatch(
        &self,
        verb: &str,
        _params: Value,
        _registry: &VerbRegistry,
        _token: &NamespaceToken,
    ) -> Result<Value, RuntimeError> {
        Err(RuntimeError::InvalidInput(format!(
            "ProbePack has no verbs: {verb}"
        )))
    }
}

/// The arm the issue reported: a normalizer that moves a caller field into
/// `properties` and a validator that refuses it must both run by the time
/// `prepare_note_update_hook` returns its error.
///
/// This arm was confirmed to be load-bearing by a mutation run before the
/// change landed, recorded here as a result rather than as a procedure. With
/// the sequencing reduced to a single `validate_note_update` call — the
/// pre-fix shape, and what a caller that skips the normalizer reproduces —
/// `normalize_note_update` did not run, `marker` did not reach `properties`,
/// `validate_note_update` observed `None` where it expects `Some(true)`, and
/// the call returned `Ok` instead of the expected `Err`, reddening the first
/// assertion below.
#[tokio::test]
async fn prepare_note_update_hook_runs_normalize_before_validate_and_validate_can_refuse() {
    let runtime = KhiveRuntime::memory().expect("memory runtime");
    let token = runtime
        .authorize(Namespace::local())
        .expect("authorize local namespace");
    let hook = Arc::new(SequencerProbeHook::default());
    let mut builder = VerbRegistryBuilder::new();
    builder.register(ProbePack(hook.clone()));
    let registry = builder.build().expect("registry builds");

    let note = khive_storage::Note::new("local", "probe-note", "body");
    let mut args = serde_json::json!({"raw_marker": true});

    let result = registry
        .prepare_note_update_hook(&runtime, &token, &note, &mut args)
        .await;

    let error = result.expect_err("the refusal must fire");
    assert!(
        error.to_string().contains("marker=true"),
        "the error must be the probe validator's own refusal: {error}"
    );
    assert_eq!(
        args["properties"]["marker"],
        serde_json::json!(true),
        "normalization must land in args even on the arm that ends in refusal"
    );
    assert_eq!(
        hook.normalize_calls.load(Ordering::SeqCst),
        1,
        "normalize must run even though validate goes on to refuse"
    );
    assert_eq!(
        hook.validate_calls.load(Ordering::SeqCst),
        1,
        "validate must run exactly once"
    );
    assert_eq!(
        *hook.validate_saw_marker.lock().unwrap(),
        Some(true),
        "validate must see the property normalize just wrote, not the caller's raw field"
    );
}

/// Mirror of the arm above: the same probe, with input that makes the
/// validator accept. `prepare_note_update_hook` must still return `Ok`
/// AND normalization must still have landed in `args` — an accepting
/// validator is not a reason to have skipped normalization.
///
/// Under the same mutation described on the arm above,
/// `args["properties"]["marker"]` was left unset — `properties` did not
/// exist at all — reddening the first assertion below.
#[tokio::test]
async fn prepare_note_update_hook_runs_normalize_before_validate_and_validate_can_accept() {
    let runtime = KhiveRuntime::memory().expect("memory runtime");
    let token = runtime
        .authorize(Namespace::local())
        .expect("authorize local namespace");
    let hook = Arc::new(SequencerProbeHook::default());
    let mut builder = VerbRegistryBuilder::new();
    builder.register(ProbePack(hook.clone()));
    let registry = builder.build().expect("registry builds");

    let note = khive_storage::Note::new("local", "probe-note", "body");
    let mut args = serde_json::json!({"raw_marker": false});

    registry
        .prepare_note_update_hook(&runtime, &token, &note, &mut args)
        .await
        .expect("an accepting validator must not refuse");

    assert_eq!(
        args["properties"]["marker"],
        serde_json::json!(false),
        "normalization must land in args even when validation accepts"
    );
    assert_eq!(hook.normalize_calls.load(Ordering::SeqCst), 1);
    assert_eq!(hook.validate_calls.load(Ordering::SeqCst), 1);
    assert_eq!(*hook.validate_saw_marker.lock().unwrap(), Some(false));
}
async fn effects_fixture() -> (
    KhiveRuntime,
    NamespaceToken,
    VerbRegistry,
    Arc<SequencerProbeHook>,
    khive_storage::Note,
) {
    let runtime = KhiveRuntime::memory().unwrap();
    let token = runtime.authorize(Namespace::local()).unwrap();
    let hook = Arc::new(SequencerProbeHook::default());
    let mut builder = VerbRegistryBuilder::new();
    builder.register(ProbePack(hook.clone()));
    let registry = builder.build().unwrap();
    let note = khive_storage::Note::new("local", "probe-note", "before");
    let id = note.id;
    runtime
        .notes(&token)
        .unwrap()
        .upsert_note(note)
        .await
        .unwrap();
    let note = runtime
        .notes(&token)
        .unwrap()
        .get_note(id)
        .await
        .unwrap()
        .unwrap();
    (runtime, token, registry, hook, note)
}

#[tokio::test]
async fn note_update_effects_are_not_computed_for_invalid_or_known_stale_patches() {
    use crate::atomic_prepare::prepare_update_from_note_snapshot;
    use serde_json::json;
    let (runtime, token, registry, hook, snapshot) = effects_fixture().await;
    let mut invalid = json!({"id": snapshot.id, "raw_marker": true});
    registry
        .prepare_note_update_hook(&runtime, &token, &snapshot, &mut invalid)
        .await
        .expect_err("kind validator refuses");
    assert_eq!(hook.effects_calls.load(Ordering::SeqCst), 0);
    for extra in [
        json!({"salience": 2.0}),
        json!({"expected_version": snapshot.version + 1}),
    ] {
        let mut args = json!({"id": snapshot.id, "raw_marker": false});
        args.as_object_mut()
            .unwrap()
            .extend(extra.as_object().unwrap().clone());
        let policy = registry
            .prepare_note_update_policy(&runtime, &token, &snapshot, &mut args)
            .await
            .unwrap();
        prepare_update_from_note_snapshot(
            &runtime,
            &token,
            &args,
            None,
            snapshot.clone(),
            policy,
            &registry,
        )
        .await
        .expect_err("invalid/stale update must be refused before effects");
        assert_eq!(hook.effects_calls.load(Ordering::SeqCst), 0);
        assert_eq!(
            runtime
                .notes(&token)
                .unwrap()
                .get_note(snapshot.id)
                .await
                .unwrap()
                .unwrap(),
            snapshot
        );
    }
    runtime
        .update_note(
            &token,
            snapshot.id,
            crate::NotePatch::new(None, Some("winner".into()), None, None, None),
        )
        .await
        .unwrap();
    let current = runtime
        .notes(&token)
        .unwrap()
        .get_note(snapshot.id)
        .await
        .unwrap()
        .unwrap();
    let mut args = json!({"id": snapshot.id, "raw_marker": false});
    let policy = registry
        .prepare_note_update_policy(&runtime, &token, &snapshot, &mut args)
        .await
        .unwrap();
    prepare_update_from_note_snapshot(
        &runtime,
        &token,
        &args,
        None,
        snapshot.clone(),
        policy,
        &registry,
    )
    .await
    .expect_err("known stale snapshot");
    assert_eq!(hook.effects_calls.load(Ordering::SeqCst), 0);
    assert_eq!(
        runtime
            .notes(&token)
            .unwrap()
            .get_note(snapshot.id)
            .await
            .unwrap()
            .unwrap(),
        current
    );
}

#[tokio::test]
async fn note_update_effects_missing_target_must_fail_without_note_changes() {
    use serde_json::json;
    let (runtime, token, registry, hook, snapshot) = effects_fixture().await;
    *hook.effects.lock().unwrap() = vec![NoteUpdateEffect::Link(LinkSpec {
        namespace: None,
        source_id: snapshot.id,
        target_id: uuid::Uuid::new_v4(),
        relation: khive_storage::EdgeRelation::Annotates,
        weight: 1.0,
        metadata: None,
        resurrect: false,
    })];
    let mut args = json!({"id": snapshot.id, "raw_marker": false});
    let policy = registry
        .prepare_note_update_policy(&runtime, &token, &snapshot, &mut args)
        .await
        .unwrap();
    let error = runtime
        .update_note_from_snapshot_with_kind_effects(
            &token,
            snapshot.clone(),
            &args,
            policy,
            &registry,
        )
        .await
        .expect_err("missing link target must not become a successful note update");
    assert!(matches!(error, RuntimeError::NotFound(_)), "{error}");
    assert_eq!(hook.effects_calls.load(Ordering::SeqCst), 1);
    assert_eq!(
        runtime
            .notes(&token)
            .unwrap()
            .get_note(snapshot.id)
            .await
            .unwrap()
            .unwrap(),
        snapshot
    );
}

#[tokio::test]
async fn note_update_effects_do_not_replace_a_live_annotation_that_appeared_during_prepare() {
    use khive_storage::EdgeRelation;
    use serde_json::json;
    let (runtime, token, registry, hook, snapshot) = effects_fixture().await;
    let target = runtime
        .create_entity(&token, "concept", None, "new target", None, None, vec![])
        .await
        .unwrap();
    runtime
        .link(
            &token,
            snapshot.id,
            target.id,
            EdgeRelation::Annotates,
            0.3,
            Some(json!({"keep": true})),
        )
        .await
        .unwrap();
    let before = runtime
        .get_edge_by_natural_key_including_deleted(
            &token,
            "local",
            snapshot.id,
            target.id,
            EdgeRelation::Annotates,
        )
        .await
        .unwrap()
        .unwrap();
    // Models an owner's absent-edge observation followed by another writer
    // creating the natural key before the runtime prepares the Link request.
    *hook.effects.lock().unwrap() = vec![NoteUpdateEffect::Link(LinkSpec {
        namespace: None,
        source_id: snapshot.id,
        target_id: target.id,
        relation: EdgeRelation::Annotates,
        weight: 1.0,
        metadata: None,
        resurrect: true,
    })];
    let mut args = json!({"id": snapshot.id, "raw_marker": false});
    let policy = registry
        .prepare_note_update_policy(&runtime, &token, &snapshot, &mut args)
        .await
        .unwrap();
    let error = runtime
        .update_note_from_snapshot_with_kind_effects(
            &token,
            snapshot.clone(),
            &args,
            policy,
            &registry,
        )
        .await
        .expect_err("typed create must not replace an intervening live edge");
    assert!(error.to_string().contains("live edge appeared"), "{error}");
    assert_eq!(
        runtime
            .notes(&token)
            .unwrap()
            .get_note(snapshot.id)
            .await
            .unwrap()
            .unwrap(),
        snapshot
    );
    let after = runtime
        .get_edge_by_natural_key_including_deleted(
            &token,
            "local",
            snapshot.id,
            target.id,
            EdgeRelation::Annotates,
        )
        .await
        .unwrap()
        .unwrap();
    assert_eq!(
        serde_json::to_value(after).unwrap(),
        serde_json::to_value(before).unwrap()
    );
}

#[tokio::test]
async fn note_update_effects_commit_guards_roll_back_note_and_prior_edge_effects() {
    use crate::atomic_prepare::prepare_update_from_note_snapshot;
    use crate::{run_atomic_unit, AtomicRunOutcome, EdgeListFilter};
    use khive_storage::EdgeRelation;
    use serde_json::json;
    for stale_note in [false, true] {
        let (runtime, token, registry, hook, snapshot) = effects_fixture().await;
        let a = runtime
            .create_entity(&token, "concept", None, "A", None, None, vec![])
            .await
            .unwrap();
        let b = runtime
            .create_entity(&token, "concept", None, "B", None, None, vec![])
            .await
            .unwrap();
        let a_id = a.id;
        let b_id = b.id;
        runtime
            .link(
                &token,
                snapshot.id,
                a_id,
                EdgeRelation::Annotates,
                1.0,
                None,
            )
            .await
            .unwrap();
        let old = runtime
            .get_edge_by_natural_key_including_deleted(
                &token,
                "local",
                snapshot.id,
                a_id,
                EdgeRelation::Annotates,
            )
            .await
            .unwrap()
            .unwrap();
        *hook.effects.lock().unwrap() = vec![
            NoteUpdateEffect::DeleteEdge(old.clone()),
            NoteUpdateEffect::Link(LinkSpec {
                namespace: None,
                source_id: snapshot.id,
                target_id: b_id,
                relation: EdgeRelation::Annotates,
                weight: 1.0,
                metadata: None,
                resurrect: false,
            }),
        ];
        let mut args = json!({"id": snapshot.id, "raw_marker": false});
        let policy = registry
            .prepare_note_update_policy(&runtime, &token, &snapshot, &mut args)
            .await
            .unwrap();
        let (_, plan) = prepare_update_from_note_snapshot(
            &runtime,
            &token,
            &args,
            None,
            snapshot.clone(),
            policy,
            &registry,
        )
        .await
        .unwrap();
        let expected = if stale_note {
            runtime
                .update_note(
                    &token,
                    snapshot.id,
                    crate::NotePatch::new(None, Some("concurrent winner".into()), None, None, None),
                )
                .await
                .unwrap()
        } else {
            runtime.delete_entity(&token, b_id, false).await.unwrap();
            snapshot.clone()
        };
        let outcome = run_atomic_unit(runtime.sql().as_ref(), vec![plan])
            .await
            .unwrap();
        assert!(
            matches!(
                outcome,
                AtomicRunOutcome::RolledBack {
                    failed_op_index: 0,
                    ..
                }
            ),
            "missing endpoint / stale note MUST refuse: {outcome:?}"
        );
        assert_eq!(
            runtime
                .notes(&token)
                .unwrap()
                .get_note(snapshot.id)
                .await
                .unwrap()
                .unwrap(),
            expected
        );
        let edges = runtime
            .list_edges(
                &token,
                EdgeListFilter {
                    source_id: Some(snapshot.id),
                    ..Default::default()
                },
                10,
                0,
            )
            .await
            .unwrap();
        assert_eq!(serde_json::to_value(edges).unwrap(), json!([old]));
        assert!(runtime
            .get_edge_by_natural_key_including_deleted(
                &token,
                "local",
                snapshot.id,
                b_id,
                EdgeRelation::Annotates,
            )
            .await
            .unwrap()
            .is_none());
    }
}