khive-runtime 0.15.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
use super::*;
use async_trait::async_trait;
use khive_gate::{Gate, GateError};
use khive_storage::{EventFilter, PageRequest};
use khive_types::{HandlerDef, Pack, VerbCategory, Visibility};
use serde_json::json;
use std::sync::Mutex;

use crate::pack::{
    AuditSuccessRowsMode, PackRuntime, RequestIdentity, VerbRegistry, VerbRegistryBuilder,
};
use crate::runtime::NamespaceToken;
use crate::{KhiveRuntime, RuntimeConfig};

// These literals are the test oracle, independent of the production predicate.
const EXPECTED_VERBS: [&str; 6] = [
    "comm.inbox",
    "comm.cleanup_expired_quarantine",
    "whoami",
    "comm.probe",
    "comm.cursor_get",
    "comm.heartbeat",
];

fn expected_retention() -> Value {
    json!({"class": "expendable", "policy": "poll-dispatch-v1", "horizon_seconds": 604800})
}

const fn handler(name: &'static str) -> HandlerDef {
    HandlerDef {
        name,
        description: "retention dispatch fixture",
        visibility: Visibility::Subhandler,
        category: VerbCategory::Declaration,
        params: &[],
    }
}

const COMM: &[HandlerDef] = &[
    handler("comm.inbox"),
    handler("comm.cleanup_expired_quarantine"),
    handler("comm.probe"),
    handler("comm.cursor_get"),
    handler("comm.heartbeat"),
    handler("comm.inbox.extra"),
    handler("comm.inbox_preview"),
];
const KG: &[HandlerDef] = &[handler("whoami"), handler("stats")];
const KNOWLEDGE: &[HandlerDef] = &[handler("knowledge.get")];
const GTD: &[HandlerDef] = &[handler("gtd.tasks")];

struct FixturePack {
    owner: &'static str,
    handlers: &'static [HandlerDef],
    response: Arc<Mutex<Value>>,
    fail: bool,
}

impl Pack for FixturePack {
    const NAME: &'static str = "retention_dispatch_fixture";
    const NOTE_KINDS: &'static [&'static str] = &[];
    const ENTITY_KINDS: &'static [&'static str] = &[];
    const HANDLERS: &'static [HandlerDef] = &[];
}

#[async_trait]
impl PackRuntime for FixturePack {
    fn name(&self) -> &str {
        self.owner
    }
    fn note_kinds(&self) -> &'static [&'static str] {
        &[]
    }
    fn entity_kinds(&self) -> &'static [&'static str] {
        &[]
    }
    fn handlers(&self) -> &'static [HandlerDef] {
        self.handlers
    }

    async fn dispatch(
        &self,
        _verb: &str,
        _params: Value,
        _registry: &VerbRegistry,
        _token: &NamespaceToken,
    ) -> Result<Value, RuntimeError> {
        if self.fail {
            Err(RuntimeError::InvalidInput(
                "retention fixture handler error".into(),
            ))
        } else {
            Ok(self.response.lock().unwrap().clone())
        }
    }
}

#[derive(Debug)]
struct FixtureGate {
    deny: bool,
}

impl Gate for FixtureGate {
    fn check(&self, _request: &GateRequest) -> Result<GateDecision, GateError> {
        Ok(if self.deny {
            GateDecision::deny("retention fixture policy denial")
        } else {
            GateDecision::Allow {
                obligations: vec![],
            }
        })
    }
}

struct Rig {
    _runtime: KhiveRuntime,
    store: Arc<dyn EventStore>,
    registry: VerbRegistry,
    response: Arc<Mutex<Value>>,
    fail: bool,
}

fn rig(mode: AuditSuccessRowsMode, outcome: EventOutcome) -> Rig {
    let runtime = KhiveRuntime::new(RuntimeConfig {
        db_path: None,
        events_split: None,
        actor_id: Some("retention-fixture".into()),
        packs: vec!["kg".into()],
        ..RuntimeConfig::no_embeddings()
    })
    .unwrap();
    let token = runtime
        .authorize(Namespace::parse("local").unwrap())
        .unwrap();
    let store = runtime.events(&token).unwrap();
    let response = Arc::new(Mutex::new(
        json!({"outcome": "success", "deleted": 0, "routed_owners_detached": 0}),
    ));
    let mut builder = VerbRegistryBuilder::new();
    builder.with_event_store(store.clone());
    builder.with_audit_success_rows_mode(mode);
    for (owner, handlers) in [
        ("comm", COMM),
        ("kg", KG),
        ("knowledge", KNOWLEDGE),
        ("gtd", GTD),
    ] {
        builder.register_trusted(FixturePack {
            owner,
            handlers,
            response: response.clone(),
            fail: outcome == EventOutcome::Error,
        });
    }
    let mut registry = builder.build().unwrap();
    registry.gate = Arc::new(FixtureGate {
        deny: outcome == EventOutcome::Denied,
    });
    Rig {
        _runtime: runtime,
        store,
        registry,
        response,
        fail: outcome == EventOutcome::Error,
    }
}

impl Rig {
    async fn call(
        &self,
        verb: &str,
        args: Value,
        intercepted: bool,
    ) -> Result<Value, crate::error::DispatchError> {
        let identity = RequestIdentity {
            namespace: "local".into(),
            actor_id: Some("retention-fixture".into()),
            request_id: Some(17),
            ..RequestIdentity::default()
        };
        if intercepted {
            self.registry
                .dispatch_intercepted_with_metadata_and_disposition(
                    verb,
                    &args,
                    Some(&identity),
                    |_| async {
                        let result = if self.fail {
                            Err(RuntimeError::InvalidInput(
                                "retention fixture handler error".into(),
                            ))
                        } else {
                            Ok(self.response.lock().unwrap().clone())
                        };
                        result.map(|value| crate::pack::InterceptedDispatchResult::new(value, ()))
                    },
                )
                .await
                .map(|outcome| outcome.result)
        } else {
            self.registry
                .dispatch_with_disposition(verb, args, Some(identity))
                .await
        }
    }

    async fn rows(&self) -> Vec<Event> {
        let page = self
            .store
            .query_events(
                EventFilter::default(),
                PageRequest {
                    offset: 0,
                    limit: 128,
                },
            )
            .await
            .expect("actual SQLite event readback");
        assert!(
            page.items.len() < 128,
            "fixture must read its complete bounded event population"
        );
        page.items
    }

    async fn receipt(&self, verb: &str) -> Event {
        let rows: Vec<_> = self
            .rows()
            .await
            .into_iter()
            .filter(|row| row.kind == EventKind::Audit && row.verb == verb)
            .collect();
        assert_eq!(rows.len(), 1, "one actual generic Audit receipt for {verb}");
        rows.into_iter().next().unwrap()
    }
}

fn assert_stamp(row: &Event) {
    assert_eq!(
        row.payload.get("retention"),
        Some(&expected_retention()),
        "stored generic receipt must contain the complete runtime retention object"
    );
    assert_eq!(
        row.payload["retention"]["horizon_seconds"].as_u64(),
        Some(604800)
    );
}

fn assert_no_stamp(row: &Event) {
    let payload = row
        .payload
        .as_object()
        .expect("real fixture payload object");
    assert!(
        !payload.contains_key("retention"),
        "unlisted or non-generic receipt must not have retention"
    );
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn generic_dispatch_retention_covers_closed_literals_and_all_outcomes() {
    for intercepted in [false, true] {
        for outcome in [
            EventOutcome::Success,
            EventOutcome::Denied,
            EventOutcome::Error,
        ] {
            for verb in EXPECTED_VERBS {
                let rig = rig(AuditSuccessRowsMode::All, outcome);
                assert!(rig
                    .rows()
                    .await
                    .into_iter()
                    .all(|row| row.kind != EventKind::Audit));
                let result = rig.call(verb, json!({}), intercepted).await;
                match outcome {
                    EventOutcome::Success => {
                        result.expect("healthy actual dispatch");
                    }
                    EventOutcome::Denied => assert!(matches!(
                        result.unwrap_err().into_source(),
                        RuntimeError::PermissionDenied { .. }
                    )),
                    EventOutcome::Error => assert!(matches!(
                        result.unwrap_err().into_source(),
                        RuntimeError::InvalidInput(_)
                    )),
                }
                let row = rig.receipt(verb).await;
                assert_eq!(row.kind, EventKind::Audit);
                assert_eq!(row.verb, verb);
                assert_eq!(row.outcome, outcome);
                assert_stamp(&row);
            }
        }
    }
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn generic_dispatch_retention_preserves_unlisted_and_caller_arguments() {
    let forged = json!({"class": "caller", "policy": "caller-policy", "horizon_seconds": 1});
    for intercepted in [false, true] {
        for verb in [
            "knowledge.get",
            "gtd.tasks",
            "stats",
            "comm.inbox.extra",
            "comm.inbox_preview",
        ] {
            let rig = rig(AuditSuccessRowsMode::All, EventOutcome::Success);
            rig.call(verb, json!({"retention": forged.clone()}), intercepted)
                .await
                .expect("healthy unlisted dispatch");
            let row = rig.receipt(verb).await;
            assert_eq!(row.outcome, EventOutcome::Success);
            assert_no_stamp(&row);
        }
        // Ordinary unknown dispatch provides a stored differently-cased Error row.
        if !intercepted {
            let rig = rig(AuditSuccessRowsMode::All, EventOutcome::Success);
            let error = rig
                .call("Comm.Inbox", json!({}), false)
                .await
                .unwrap_err()
                .into_source();
            assert!(matches!(error, RuntimeError::UnknownVerb(_)));
            let row = rig.receipt("Comm.Inbox").await;
            assert_eq!(row.outcome, EventOutcome::Error);
            assert_no_stamp(&row);
        }
        for verb in EXPECTED_VERBS {
            let rig = rig(AuditSuccessRowsMode::All, EventOutcome::Success);
            rig.call(verb, json!({"retention": forged.clone()}), intercepted)
                .await
                .expect("healthy forged-argument dispatch");
            assert_stamp(&rig.receipt(verb).await);
        }
    }
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn generic_dispatch_retention_kind_and_non_generic_survival() {
    let rig = rig(AuditSuccessRowsMode::All, EventOutcome::Success);
    let kinds = [
        EventKind::FeedbackExplicit,
        EventKind::PhaseStarted,
        EventKind::PhaseCompleted,
        EventKind::PhaseCancelled,
        EventKind::ChannelPollStarted,
        EventKind::ChannelPollSucceeded,
        EventKind::ChannelPollFailed,
        EventKind::ChannelBackoffArmed,
        EventKind::ChannelBackoffReset,
        EventKind::ChannelHeartbeatPersistFailed,
    ];
    for kind in kinds {
        for verb in EXPECTED_VERBS {
            let event = Event::new(
                "local",
                verb,
                kind,
                SubstrateKind::Event,
                "retention-fixture",
            )
            .with_payload(json!({"fixture": "independent non-generic row"}));
            let id = event.id;
            rig.store
                .append_event(event)
                .await
                .expect("real non-generic append");
            let row = rig
                .rows()
                .await
                .into_iter()
                .find(|row| row.id == id)
                .expect("actual non-generic row exists");
            assert_eq!(row.kind, kind);
            assert_eq!(row.verb, verb);
            assert_no_stamp(&row);
            assert!(
                !expendable_dispatch_audit(kind, verb),
                "non-audit training/lifecycle kind must not classify as expendable"
            );
        }
    }
    // The direct append API is not the generic dispatch producer even when its
    // Audit key has an allowlisted spelling.
    let event = Event::new(
        "local",
        "comm.inbox",
        EventKind::Audit,
        SubstrateKind::Event,
        "retention-fixture",
    )
    .with_payload(json!({"fixture": "dedicated Audit append"}));
    let id = event.id;
    rig.store.append_event(event).await.unwrap();
    let row = rig
        .rows()
        .await
        .into_iter()
        .find(|row| row.id == id)
        .expect("actual dedicated Audit row exists");
    assert_no_stamp(&row);
}

fn fresh_sampling_process() -> bool {
    const MARKER: &str = "KHIVE_RETENTION_SAMPLING_CHILD";
    if std::env::var_os(MARKER).is_some() {
        return true;
    }
    let selector = "pack::audit::retention_tests::generic_dispatch_retention_retains_sampled_and_forced_success_metadata";
    let mut child = std::process::Command::new(std::env::current_exe().unwrap())
        .args(["--exact", selector, "--nocapture"])
        .env(MARKER, "1")
        .spawn()
        .unwrap();
    let deadline = std::time::Instant::now() + std::time::Duration::from_secs(60);
    loop {
        if let Some(status) = child.try_wait().unwrap() {
            assert!(status.success(), "fresh sampling fixture failed: {status}");
            return false;
        }
        if std::time::Instant::now() >= deadline {
            child.kill().unwrap();
            child.wait().unwrap();
            panic!("fresh sampling fixture exceeded its deadline");
        }
        std::thread::sleep(std::time::Duration::from_millis(10));
    }
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn generic_dispatch_retention_retains_sampled_and_forced_success_metadata() {
    if !fresh_sampling_process() {
        return;
    }
    for verb in ["comm.heartbeat", "comm.cleanup_expired_quarantine"] {
        let rig = rig(AuditSuccessRowsMode::Reduced, EventOutcome::Success);
        for (seq, selected_by) in [(0_u64, "sample"), (1, "forced")] {
            *rig.response.lock().unwrap() = if seq == 0 {
                json!({"outcome": "success", "deleted": 0, "routed_owners_detached": 0})
            } else {
                json!({"outcome": "failed", "deleted": 1, "routed_owners_detached": 0})
            };
            rig.call(verb, json!({}), false)
                .await
                .expect("retained sampled/forced dispatch");
            let rows: Vec<_> = rig
                .rows()
                .await
                .into_iter()
                .filter(|row| row.kind == EventKind::Audit && row.verb == verb)
                .collect();
            assert_eq!(rows.len(), usize::try_from(seq + 1).unwrap());
            let row = rows
                .iter()
                .find(|row| row.payload["resource"]["sampling"]["seq"] == seq)
                .expect("actual retained sequence row");
            assert_eq!(
                row.payload["resource"]["sampling"],
                json!({"every": 10, "seq": seq, "selected_by": selected_by})
            );
            assert_stamp(row);
        }
    }
}