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
//! ADR-174 Amendment 9 acceptance: the single-head fence pins identity, and
//! both routes resolve it through one predicate.
//!
//! Arm 1 pairs the routes on one fixture and compares the refusals against each
//! other, for the reason the deadline amendment did: two predicates that agree
//! today pass a pair of separate assertions and fail this one.

use super::*;

use crate::note_write::{NoteFences, NoteWriteOptions};
use crate::pack::VerbRegistryBuilder;
use crate::{StreamAppendSpec, StreamBatchMember, StreamBatchRefusal, StreamObservation};
use serde_json::Value;
use uuid::Uuid;

async fn head(runtime: &KhiveRuntime, token: &NamespaceToken, key: &str, content: Value) -> Note {
    runtime
        .create_note_with_options(
            token,
            "head",
            None,
            &content.to_string(),
            None,
            None,
            None,
            None,
            vec![],
            None,
            NoteWriteOptions {
                key: Some(key.into()),
                embed: Some(false),
                ..Default::default()
            },
        )
        .await
        .unwrap()
        .0
}

/// Hard-delete the live holder and create a new note under the same key, which
/// is the state a fence cannot see by version alone: the replacement starts at
/// version 1, exactly where the caller's observation was.
async fn replace(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    key: &str,
    content: Value,
) -> (Uuid, Uuid) {
    let original = runtime
        .notes(token)
        .unwrap()
        .get_live_notes_by_key(token.namespace().as_str(), key, Some("head"))
        .await
        .unwrap()
        .remove(0);
    runtime.delete_note(token, original.id, true).await.unwrap();
    let replacement = head(runtime, token, key, content).await;
    assert_eq!(
        replacement.version, original.version,
        "the replacement must sit where the observation did, or this fixture proves nothing"
    );
    assert_ne!(replacement.id, original.id);
    (original.id, replacement.id)
}

fn append(stream: &str) -> StreamBatchMember {
    StreamBatchMember::Append(StreamAppendSpec {
        stream: stream.into(),
        record: json!({"event": "fence-identity"}),
        expected_seq: None,
        embed: Some(false),
        embedding_model: None,
        note_kind: "observation".into(),
        tags: None,
        fence: None,
    })
}

async fn observed_batch(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    registry: &crate::VerbRegistry,
    stream: &str,
    observed: Vec<StreamObservation>,
) -> RuntimeResult<Result<Vec<Value>, StreamBatchRefusal>> {
    runtime
        .stream_batch_atomic(token, vec![append(stream)], None, observed, registry)
        .await
}

async fn fenced_update(
    runtime: &KhiveRuntime,
    token: &NamespaceToken,
    target: &Note,
    fence: Value,
) -> AtomicRunOutcome {
    let plan = crate::atomic_prepare::prepare_update(
        runtime,
        token,
        &json!({"id": target.id, "content": "{\"written\":true}", "fence": fence}),
        None,
    )
    .await
    .unwrap();
    run_atomic_unit(runtime.sql().as_ref(), vec![plan])
        .await
        .unwrap()
}

fn refusal(outcome: AtomicRunOutcome) -> Value {
    let AtomicRunOutcome::RolledBack {
        failure: crate::atomic_runner::AtomicOpFailure::NoteConflict(conflict),
        ..
    } = outcome
    else {
        panic!("expected a fence refusal, got {outcome:?}");
    };
    serde_json::to_value(conflict.into_error().details().unwrap()).unwrap()
}

fn batch_refusal(result: RuntimeResult<Result<Vec<Value>, StreamBatchRefusal>>) -> Value {
    let Err(RuntimeError::Khive(error)) = result else {
        panic!("expected a structured observation refusal");
    };
    serde_json::to_value(error.details().expect("conflict details")).unwrap()
}

fn field_names(value: &Value) -> Vec<String> {
    let mut names: Vec<String> = value.as_object().unwrap().keys().cloned().collect();
    names.sort();
    names
}

async fn version_of(runtime: &KhiveRuntime, token: &NamespaceToken, note: &Note) -> i64 {
    runtime
        .notes(token)
        .unwrap()
        .get_note(note.id)
        .await
        .unwrap()
        .unwrap()
        .version
}

/// Arm 1. One fixture, both routes, and the refusals compared against each
/// other rather than each against its own expectation.
#[tokio::test]
async fn fence_identity_arm1_both_routes_refuse_one_replaced_key_with_the_same_evidence() {
    let (runtime, token, _) = fixture();
    let registry = VerbRegistryBuilder::new().build().unwrap();
    head(&runtime, &token, "lease/replaced", json!({})).await;
    let target = head(&runtime, &token, "target/replaced", json!({})).await;
    let (observed_id, current_id) = replace(
        &runtime,
        &token,
        "lease/replaced",
        json!({"owner": "other"}),
    )
    .await;

    let batch = batch_refusal(
        observed_batch(
            &runtime,
            &token,
            &registry,
            "arm1",
            vec![StreamObservation {
                key: "lease/replaced".into(),
                kind: "head".into(),
                version: Some(1),
                id: Some(observed_id),
                live_until: None,
            }],
        )
        .await,
    );
    let fenced = refusal(
        fenced_update(
            &runtime,
            &token,
            &target,
            json!([{"kind":"head","key":"lease/replaced","expected_version":1,"id":observed_id}]),
        )
        .await,
    );

    assert_eq!(field_names(&batch), field_names(&fenced));
    for field in [
        "reason",
        "key",
        "kind",
        "version",
        "id",
        "current_id",
        "index",
    ] {
        assert_eq!(
            batch[field], fenced[field],
            "{field} differs between the two routes: {batch} vs {fenced}"
        );
    }
    assert_eq!(batch["reason"], "identity_conflict");
    assert_eq!(batch["id"], observed_id.to_string());
    assert_eq!(batch["current_id"], current_id.to_string());
    assert_eq!(batch["version"], "1");
    assert_eq!(version_of(&runtime, &token, &target).await, 1);
}

/// Arm 2. Identity is read before the deadline, so a caller whose note was
/// replaced is not told about a deadline belonging to a document it never
/// observed.
#[tokio::test]
async fn fence_identity_arm2_identity_refuses_before_the_deadline() {
    let (runtime, token, _) = fixture();
    let expired = (chrono::Utc::now() - chrono::Duration::hours(1))
        .to_rfc3339_opts(chrono::SecondsFormat::Micros, true);
    head(
        &runtime,
        &token,
        "lease/both",
        json!({"expires_at": expired}),
    )
    .await;
    let target = head(&runtime, &token, "target/both", json!({})).await;
    let (observed_id, _) = replace(
        &runtime,
        &token,
        "lease/both",
        json!({"expires_at": expired}),
    )
    .await;

    let fenced = refusal(
        fenced_update(
            &runtime,
            &token,
            &target,
            json!([{"kind":"head","key":"lease/both","expected_version":1,
                    "id":observed_id,"live_until":"expires_at"}]),
        )
        .await,
    );
    assert_eq!(fenced["reason"], "identity_conflict");
    assert!(fenced.get("value").is_none(), "{fenced}");
    assert!(fenced.get("now").is_none(), "{fenced}");
}

/// Arm 3. A note that was replaced and also sits at a different version reports
/// the identity refusal on both routes, which is the arm that would catch them
/// disagreeing about which failure a caller is shown.
#[tokio::test]
async fn fence_identity_arm3_identity_refuses_before_the_version_on_both_routes() {
    let (runtime, token, _) = fixture();
    let registry = VerbRegistryBuilder::new().build().unwrap();
    head(&runtime, &token, "lease/moved", json!({})).await;
    let target = head(&runtime, &token, "target/moved", json!({})).await;
    let (observed_id, _) = replace(&runtime, &token, "lease/moved", json!({})).await;
    // Move the replacement past the observed version, so the version half would
    // also refuse if it were consulted first.
    let replacement = runtime
        .notes(&token)
        .unwrap()
        .get_live_notes_by_key(token.namespace().as_str(), "lease/moved", Some("head"))
        .await
        .unwrap()
        .remove(0);
    runtime
        .update_note(&token, replacement.id, patch("{\"bumped\":true}", 1, None))
        .await
        .unwrap();

    let batch = batch_refusal(
        observed_batch(
            &runtime,
            &token,
            &registry,
            "arm3",
            vec![StreamObservation {
                key: "lease/moved".into(),
                kind: "head".into(),
                version: Some(1),
                id: Some(observed_id),
                live_until: None,
            }],
        )
        .await,
    );
    let fenced = refusal(
        fenced_update(
            &runtime,
            &token,
            &target,
            json!([{"kind":"head","key":"lease/moved","expected_version":1,"id":observed_id}]),
        )
        .await,
    );
    assert_eq!(batch["reason"], "identity_conflict");
    assert_eq!(fenced["reason"], "identity_conflict");
    assert_eq!(field_names(&batch), field_names(&fenced));
}

/// Arm 4. A matching identity admits, so the check is not refusing everything.
#[tokio::test]
async fn fence_identity_arm4_a_matching_identity_admits() {
    let (runtime, token, _) = fixture();
    let lease = head(&runtime, &token, "lease/held", json!({})).await;
    let target = head(&runtime, &token, "target/held", json!({})).await;
    let outcome = fenced_update(
        &runtime,
        &token,
        &target,
        json!([{"kind":"head","key":"lease/held","expected_version":1,"id":lease.id}]),
    )
    .await;
    assert!(
        matches!(outcome, AtomicRunOutcome::Committed { .. }),
        "a matching identity must commit: {outcome:?}"
    );
    assert_eq!(version_of(&runtime, &token, &target).await, 2);
}

/// Arm 5. An identity has nothing to compare against an absence assertion, so
/// the pairing is refused while the call is still being shaped, in both fence
/// forms.
#[tokio::test]
async fn fence_identity_arm5_an_absence_assertion_cannot_carry_an_identity() {
    let (runtime, token, _) = fixture();
    let target = head(&runtime, &token, "target/pairing", json!({})).await;
    let id = Uuid::new_v4();
    for fence in [
        json!({"kind":"head","key":"lease/pairing","expected_version":null,"id":id}),
        json!([{"kind":"head","key":"lease/pairing","expected_version":null,"id":id}]),
    ] {
        let message = serde_json::from_value::<NoteFences>(fence.clone())
            .unwrap_err()
            .to_string();
        assert!(
            message.contains("fence id requires a positive expected_version"),
            "{message}"
        );
        let error = crate::atomic_prepare::prepare_update(
            &runtime,
            &token,
            &json!({"id": target.id, "content": "{}", "fence": fence}),
            None,
        )
        .await
        .unwrap_err();
        assert!(
            matches!(error, RuntimeError::InvalidInput(_)),
            "{error:?}, and no plan may exist to be run"
        );
    }
    assert_eq!(version_of(&runtime, &token, &target).await, 1);
}

/// Arm 6. An absent holder has no identity to name, so it is a version refusal
/// on both routes, not an identity one.
#[tokio::test]
async fn fence_identity_arm6_an_absent_holder_is_a_version_refusal() {
    let (runtime, token, _) = fixture();
    let registry = VerbRegistryBuilder::new().build().unwrap();
    let lease = head(&runtime, &token, "lease/gone", json!({})).await;
    let target = head(&runtime, &token, "target/gone", json!({})).await;
    runtime.delete_note(&token, lease.id, true).await.unwrap();

    let batch = batch_refusal(
        observed_batch(
            &runtime,
            &token,
            &registry,
            "arm6",
            vec![StreamObservation {
                key: "lease/gone".into(),
                kind: "head".into(),
                version: Some(1),
                id: Some(lease.id),
                live_until: None,
            }],
        )
        .await,
    );
    assert_eq!(batch["reason"], "version_conflict");
    let fenced = refusal(
        fenced_update(
            &runtime,
            &token,
            &target,
            json!([{"kind":"head","key":"lease/gone","expected_version":1,"id":lease.id}]),
        )
        .await,
    );
    assert_eq!(fenced["reason"], "fence_conflict");
    assert!(fenced.get("current_id").is_none(), "{fenced}");
}

/// Arm 7. The fenced write may be a creation, and the object fence form's
/// refusal differs from the list form's by the entry index alone.
#[tokio::test]
async fn fence_identity_arm7_a_creation_is_fenced_too_and_the_object_form_has_no_index() {
    let (runtime, token, _) = fixture();
    head(&runtime, &token, "lease/create", json!({})).await;
    let (observed_id, current_id) =
        replace(&runtime, &token, "lease/create", json!({"owner": "other"})).await;

    let create = |fence: Value| {
        let runtime = &runtime;
        let token = &token;
        async move {
            runtime
                .create_note_with_options(
                    token,
                    "head",
                    None,
                    "{}",
                    None,
                    None,
                    None,
                    None,
                    vec![],
                    None,
                    NoteWriteOptions {
                        key: Some("created/refused".into()),
                        embed: Some(false),
                        fence: Some(serde_json::from_value(fence).unwrap()),
                        ..Default::default()
                    },
                )
                .await
                .unwrap_err()
        }
    };

    let object = details(
        create(json!({"kind":"head","key":"lease/create","expected_version":1,"id":observed_id}))
            .await,
    );
    assert_eq!(object["reason"], "identity_conflict");
    assert_eq!(object["current_id"], current_id.to_string());
    assert!(object.get("index").is_none(), "{object}");

    let listed = details(
        create(json!([{"kind":"head","key":"lease/create","expected_version":1,"id":observed_id}]))
            .await,
    );
    assert_eq!(listed["index"], "0");
    let mut without_index = listed.clone();
    without_index.as_object_mut().unwrap().remove("index");
    assert_eq!(without_index, object);

    assert!(runtime
        .notes(&token)
        .unwrap()
        .get_live_notes_by_key(token.namespace().as_str(), "created/refused", Some("head"))
        .await
        .unwrap()
        .is_empty());
}