kkernel 0.9.0

khive kernel — stdio MCP server binary and admin CLI (sync, pack introspection, db ops)
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
//! Public CLI atomic context updates must keep the task property and its
//! annotation consistent without exposing internal companions as extra ops.

use std::process::{Command, Output};

use khive_pack_gtd::GtdPack;
use khive_pack_kg::KgPack;
use khive_runtime::{KhiveRuntime, Namespace, RuntimeConfig, VerbRegistryBuilder};
use khive_storage::{Edge, EdgeFilter, EdgeRelation, Note, PageRequest, SqlStatement};
use serde_json::{json, Value};
use tempfile::TempDir;
use uuid::Uuid;

struct Fixture {
    home: TempDir,
    runtime: KhiveRuntime,
    task: Uuid,
    a: Uuid,
    b: Uuid,
    unrelated: Uuid,
}

impl Fixture {
    async fn new(with_context: bool) -> Self {
        let home = tempfile::tempdir().expect("isolated CLI fixture");
        std::fs::write(
            home.path().join("config.toml"),
            "[runtime]\npacks = ['kg', 'gtd']\n",
        )
        .expect("explicit fixture config");
        let runtime = KhiveRuntime::new(RuntimeConfig {
            db_path: Some(home.path().join("context.db")),
            packs: vec!["kg".into(), "gtd".into()],
            actor_id: None,
            brain_profile: None,
            ..RuntimeConfig::no_embeddings()
        })
        .expect("model-less seed runtime");
        let mut builder = VerbRegistryBuilder::new();
        builder.register(KgPack::new(runtime.clone()));
        builder.register(GtdPack::new(runtime.clone()));
        let registry = builder.build().expect("KG and GTD registry");
        runtime.install_edge_rules(registry.all_edge_rules());
        registry
            .apply_schema_plans_with_map(&Default::default(), runtime.backend())
            .expect("real pack schemas");
        let token = runtime.authorize(Namespace::local()).expect("seed token");
        let mut ids = Vec::new();
        for name in ["Context A", "Context B", "Independent annotation"] {
            ids.push(
                runtime
                    .create_entity(&token, "concept", None, name, None, None, vec![])
                    .await
                    .expect("seed context entity")
                    .id,
            );
        }
        let [a, b, unrelated] = <[Uuid; 3]>::try_from(ids).expect("three entities");
        let mut args = json!({"title": "Task context atomic contract"});
        if with_context {
            args["context_entity_id"] = json!(a);
        }
        let assigned = registry
            .dispatch("gtd.assign", args)
            .await
            .expect("canonical GTD seed task");
        let task = Uuid::parse_str(assigned["full_id"].as_str().expect("full task ID"))
            .expect("task UUID");
        runtime
            .link(
                &token,
                task,
                unrelated,
                EdgeRelation::Annotates,
                0.75,
                Some(json!({"purpose": "independent annotation"})),
            )
            .await
            .expect("unrelated annotation control");
        Self {
            home,
            runtime,
            task,
            a,
            b,
            unrelated,
        }
    }

    fn run(&self, ops: &[Value]) -> (Output, Value) {
        let contents: String = ops.iter().map(|op| format!("{op}\n")).collect();
        let ops_file = self.home.path().join("operations.jsonl");
        std::fs::write(&ops_file, contents).expect("write JSONL operations");
        let output = Command::new(env!("CARGO_BIN_EXE_kkernel"))
            .args(["exec", "--atomic", "--strict", "--ops-file"])
            .arg(ops_file)
            .arg("--config")
            .arg(self.home.path().join("config.toml"))
            .arg("--db")
            .arg(self.home.path().join("context.db"))
            .args([
                "--actor",
                "test:context",
                "--expect-actor",
                "test:context",
                "--namespace",
                "local",
            ])
            .current_dir(self.home.path())
            .env_clear()
            .env("HOME", self.home.path())
            .env("TMPDIR", self.home.path())
            .env("KHIVE_NO_DAEMON", "1")
            .env("KHIVE_SOCKET", self.home.path().join("unused.sock"))
            .env("KHIVE_PACKS", "kg,gtd")
            .env("RUST_LOG", "error")
            .output()
            .expect("run actual kkernel --atomic binary");
        let envelope = serde_json::from_slice(&output.stdout).unwrap_or_else(|error| {
            panic!(
                "atomic output is not JSON: {error}; stdout={} stderr={}",
                String::from_utf8_lossy(&output.stdout),
                String::from_utf8_lossy(&output.stderr),
            )
        });
        (output, envelope)
    }

    fn update(&self, context: Value) -> Value {
        json!({"tool": "update", "args": {
            "id": self.task, "properties": {"context_entity_id": context}
        }})
    }

    async fn note(&self) -> Note {
        let token = self
            .runtime
            .authorize(Namespace::local())
            .expect("read token");
        self.runtime
            .notes(&token)
            .expect("live fixture note store")
            .get_note(self.task)
            .await
            .expect("read task")
            .expect("task remains present")
    }

    async fn annotations(&self) -> Vec<Edge> {
        let token = self
            .runtime
            .authorize(Namespace::local())
            .expect("read token");
        let mut edges = self
            .runtime
            .graph(&token)
            .expect("live fixture graph store")
            .query_edges(
                EdgeFilter {
                    source_ids: vec![self.task],
                    relations: vec![EdgeRelation::Annotates],
                    ..EdgeFilter::default()
                },
                vec![],
                PageRequest {
                    offset: 0,
                    limit: 100,
                },
            )
            .await
            .expect("read live annotation edges")
            .items;
        edges.sort_by_key(|edge| edge.id.to_string());
        edges
    }

    async fn assert_targets(&self, expected: &[Uuid]) {
        let mut actual: Vec<Uuid> = self
            .annotations()
            .await
            .iter()
            .map(|edge| edge.target_id)
            .collect();
        let mut expected = expected.to_vec();
        actual.sort();
        expected.sort();
        assert_eq!(actual, expected);
    }

    async fn snapshot(&self) -> Value {
        // Keep the original writable pool alive; a WAL file is not a frozen
        // read-only snapshot. Include tombstones so rollback cannot hide an edge.
        let token = self
            .runtime
            .authorize(Namespace::local())
            .expect("read token");
        let mut entities = Vec::new();
        for id in [self.a, self.b, self.unrelated] {
            entities.push(
                self.runtime
                    .entities(&token)
                    .expect("entity store")
                    .get_entity_including_deleted(id)
                    .await
                    .expect("read entity including tombstone"),
            );
        }
        let edges = self
            .runtime
            .sql()
            .reader()
            .await
            .expect("live SQL reader")
            .query_all(SqlStatement {
                sql: "SELECT * FROM graph_edges ORDER BY namespace, id".into(),
                params: vec![],
                label: Some("gtd-context-atomic-domain-snapshot".into()),
            })
            .await
            .expect("snapshot every edge column, including deleted rows");
        json!({"task": self.note().await, "entities": entities, "edges": edges})
    }
}

fn assert_slots(envelope: &Value, tools: &[&str]) {
    let results = envelope["results"].as_array().expect("result slots");
    assert_eq!(results.len(), tools.len(), "{envelope}");
    assert_eq!(
        envelope["summary"]["total"],
        json!(tools.len()),
        "{envelope}"
    );
    for (index, tool) in tools.iter().enumerate() {
        assert_eq!(results[index]["op_index"], json!(index), "{envelope}");
        assert_eq!(results[index]["tool"], *tool, "{envelope}");
    }
}

fn assert_one_committed_update(output: &Output, envelope: &Value) {
    assert!(
        output.status.success(),
        "stdout={} stderr={}",
        String::from_utf8_lossy(&output.stdout),
        String::from_utf8_lossy(&output.stderr)
    );
    assert_slots(envelope, &["update"]);
    assert_eq!(envelope["atomic"]["committed"], true, "{envelope}");
    assert_eq!(envelope["atomic"]["rolled_back"], false, "{envelope}");
    assert_eq!(envelope["summary"]["succeeded"], 1, "{envelope}");
    assert_eq!(envelope["results"][0]["ok"], true, "{envelope}");
    assert!(envelope["results"][0]["result"].is_object(), "{envelope}");
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn atomic_context_a_to_b_replaces_only_the_context_annotation_in_one_result_slot() {
    let fixture = Fixture::new(true).await;
    fixture
        .assert_targets(&[fixture.a, fixture.unrelated])
        .await;
    let before = fixture.note().await;
    let independent = fixture
        .annotations()
        .await
        .into_iter()
        .find(|edge| edge.target_id == fixture.unrelated)
        .unwrap();
    let (output, envelope) = fixture.run(&[fixture.update(json!(fixture.b))]);
    assert_one_committed_update(&output, &envelope);
    let after = fixture.note().await;
    assert_eq!(
        after.properties.as_ref().unwrap()["context_entity_id"],
        json!(fixture.b)
    );
    assert_eq!(after.version, before.version + 1);
    fixture
        .assert_targets(&[fixture.b, fixture.unrelated])
        .await;
    let preserved = fixture
        .annotations()
        .await
        .into_iter()
        .find(|edge| edge.target_id == fixture.unrelated)
        .unwrap();
    assert_eq!(
        serde_json::to_value(preserved).unwrap(),
        serde_json::to_value(independent).unwrap()
    );
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn atomic_context_a_to_null_removes_only_the_context_annotation() {
    let fixture = Fixture::new(true).await;
    fixture
        .assert_targets(&[fixture.a, fixture.unrelated])
        .await;
    let before = fixture.note().await;
    let (output, envelope) = fixture.run(&[fixture.update(Value::Null)]);
    assert_one_committed_update(&output, &envelope);
    let after = fixture.note().await;
    assert!(after.properties.as_ref().unwrap()["context_entity_id"].is_null());
    assert_eq!(after.version, before.version + 1);
    fixture.assert_targets(&[fixture.unrelated]).await;
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn atomic_same_context_preserves_edge_ids_timestamps_and_metadata() {
    let fixture = Fixture::new(true).await;
    let before = serde_json::to_value(fixture.annotations().await).unwrap();
    let (output, envelope) =
        fixture.run(&[fixture.update(json!(fixture.a.to_string().to_uppercase()))]);
    assert_one_committed_update(&output, &envelope);
    assert_eq!(
        serde_json::to_value(fixture.annotations().await).unwrap(),
        before
    );
    assert_eq!(
        fixture.note().await.properties.unwrap()["context_entity_id"],
        json!(fixture.a)
    );
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn atomic_first_context_creates_one_annotation_without_an_extra_result_slot() {
    let fixture = Fixture::new(false).await;
    fixture.assert_targets(&[fixture.unrelated]).await;
    let before = fixture.note().await;
    let (output, envelope) = fixture.run(&[fixture.update(json!(fixture.b))]);
    assert_one_committed_update(&output, &envelope);
    let after = fixture.note().await;
    assert_eq!(
        after.properties.as_ref().unwrap()["context_entity_id"],
        json!(fixture.b)
    );
    assert_eq!(after.version, before.version + 1);
    fixture
        .assert_targets(&[fixture.b, fixture.unrelated])
        .await;
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn atomic_invalid_context_refuses_before_any_domain_change() {
    let fixture = Fixture::new(true).await;
    let before = fixture.snapshot().await;
    let missing = Uuid::new_v4();
    let (output, envelope) = fixture.run(&[fixture.update(json!(missing))]);
    assert!(!output.status.success(), "{envelope}");
    assert_slots(&envelope, &["update"]);
    assert_eq!(envelope["atomic"]["committed"], false, "{envelope}");
    assert_eq!(envelope["atomic"]["rolled_back"], false, "{envelope}");
    assert_eq!(envelope["atomic"]["failed_op_index"], 0, "{envelope}");
    assert!(
        envelope["results"][0]["error"]
            .as_str()
            .unwrap()
            .contains("context_entity_id"),
        "{envelope}"
    );
    assert_eq!(
        fixture.snapshot().await,
        before,
        "invalid context must not change note revision or edges"
    );
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn atomic_context_link_companion_missing_endpoint_rolls_back_the_whole_batch() {
    let fixture = Fixture::new(true).await;
    fixture
        .assert_targets(&[fixture.a, fixture.unrelated])
        .await;
    let before = fixture.snapshot().await;
    // Preparation sees B. The first operation removes it inside the unit;
    // the update's link companion must recheck the endpoint during execution.
    let (output, envelope) = fixture.run(&[
        json!({"tool": "delete", "args": {"id": fixture.b, "hard": true}}),
        fixture.update(json!(fixture.b)),
    ]);
    // A rolled-back atomic unit exits non-zero, including under --strict.
    // The envelope and persisted snapshot below establish the exact failure.
    assert!(!output.status.success(), "{envelope}");
    assert_slots(&envelope, &["delete", "update"]);
    assert_eq!(envelope["atomic"]["committed"], false, "{envelope}");
    assert_eq!(envelope["atomic"]["rolled_back"], true, "{envelope}");
    assert_eq!(envelope["atomic"]["failed_op_index"], 1, "{envelope}");
    assert_eq!(envelope["summary"]["succeeded"], 0, "{envelope}");
    assert_eq!(envelope["summary"]["failed"], 2, "{envelope}");
    assert!(
        envelope["atomic"]["error"]
            .as_str()
            .unwrap()
            .contains("guard failed"),
        "{envelope}"
    );
    assert!(envelope["results"]
        .as_array()
        .unwrap()
        .iter()
        .all(|result| result["ok"] == false));
    assert_eq!(
        fixture.snapshot().await,
        before,
        "rollback must restore B, task properties/revision, and every old edge byte-for-byte"
    );
}

#[tokio::test]
#[serial_test::serial(config_ledger)]
async fn atomic_existing_context_annotation_rechecks_endpoint_and_preserves_the_edge_on_success() {
    let fixture = Fixture::new(true).await;
    let token = fixture
        .runtime
        .authorize(Namespace::local())
        .expect("seed token");
    fixture
        .runtime
        .link(
            &token,
            fixture.task,
            fixture.b,
            EdgeRelation::Annotates,
            0.35,
            Some(json!({"purpose": "separately authored B annotation"})),
        )
        .await
        .expect("seed an existing B annotation alongside context A");
    fixture
        .assert_targets(&[fixture.a, fixture.b, fixture.unrelated])
        .await;
    let before = fixture.snapshot().await;
    let retained: Vec<Edge> = fixture
        .annotations()
        .await
        .into_iter()
        .filter(|edge| edge.target_id != fixture.a)
        .collect();
    let update = fixture.update(json!(fixture.b));
    let (output, envelope) = fixture.run(&[
        json!({"tool": "delete", "args": {"id": fixture.b, "hard": true}}),
        update.clone(),
    ]);
    // A rolled-back atomic unit exits non-zero, including under --strict.
    // The envelope and persisted snapshot below establish the exact failure.
    assert!(!output.status.success(), "{envelope}");
    assert_slots(&envelope, &["delete", "update"]);
    assert_eq!(envelope["atomic"]["committed"], false, "{envelope}");
    assert_eq!(envelope["atomic"]["rolled_back"], true, "{envelope}");
    assert_eq!(envelope["atomic"]["failed_op_index"], 1, "{envelope}");
    assert_eq!(envelope["summary"]["succeeded"], 0, "{envelope}");
    assert_eq!(envelope["summary"]["failed"], 2, "{envelope}");
    assert!(
        envelope["atomic"]["error"]
            .as_str()
            .unwrap()
            .contains("guard failed"),
        "{envelope}"
    );
    assert!(envelope["results"]
        .as_array()
        .unwrap()
        .iter()
        .all(|result| result["ok"] == false));
    assert_eq!(
        fixture.snapshot().await,
        before,
        "reusing an existing B annotation must not bypass the execution-time endpoint guard"
    );

    // With B intact, the exact same update succeeds without rewriting the
    // independently authored B edge or creating a replacement for it.
    let (output, envelope) = fixture.run(&[update]);
    assert_one_committed_update(&output, &envelope);
    assert_eq!(
        fixture.note().await.properties.unwrap()["context_entity_id"],
        json!(fixture.b)
    );
    assert_eq!(
        serde_json::to_value(fixture.annotations().await).unwrap(),
        serde_json::to_value(retained).unwrap(),
        "successful context reuse preserves both B and unrelated C edge rows exactly"
    );
}