prolog8 26.10.11

Prolog8 — byte-capped proof engine for policy, workflow, and agent action admission
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
//! Integration tests for the Prolog8 kernel — end-to-end proof + receipt flows.
//!
//! These tests only touch the public API (`pub` items) and verify:
//! 1. Fact admission → query → Allow decision with proof + receipt.
//! 2. Missing-fact query → Deny decision with negative proof.
//! 3. One-step rule chaining.
//! 4. Receipt integrity: `receipt_hash` matches `compute_hash()`.
//! 5. Receipt replay verification via `replay()`.
//! 6. `admit_atom` / `admit_rule` boundary enforcement.
//! 7. BLAKE3 domain keys are distinct (collision resistance).

use prolog8::{
    admit_atom,
    catalog::{Catalog, PredicateMeta, PredicateProofPolicy},
    kernel::{Decision, Kernel, QueryResult},
    replay,
    types::{
        Atom8, CatalogId, DecisionKind, EpochId, FactBlock8, FactRow8, FeatureBit, PlanId,
        PredicateId, ProofKind, ProofMode, QueryAtom8, Rule8, RuleId, SourceId, TermId, ARITY_CAP,
        BODY_CAP,
    },
    RejectionCode, ReplayStatus,
};

// ── helpers ──────────────────────────────────────────────────────────────────

fn base_catalog() -> Catalog {
    let mut cat = Catalog::new(CatalogId(1));
    cat.add_predicate(PredicateMeta {
        pred_id: PredicateId(1),
        label: "parent".into(),
        arity: 2,
        access_orders: vec![],
        proof_policy: PredicateProofPolicy::OnRequest,
        materialized: false,
    });
    cat.add_predicate(PredicateMeta {
        pred_id: PredicateId(2),
        label: "ancestor".into(),
        arity: 2,
        access_orders: vec![],
        proof_policy: PredicateProofPolicy::OnRequest,
        materialized: false,
    });
    cat
}

fn build_kernel_with_facts() -> Kernel {
    let mut cat = base_catalog();
    let alice = cat.intern_term("alice");
    let bob = cat.intern_term("bob");
    let carol = cat.intern_term("carol");

    let mut k = Kernel::new(cat);
    k.load_facts(FactBlock8::new(
        PredicateId(1),
        2,
        vec![
            FactRow8::new(PredicateId(1), 2, &[alice, bob], SourceId(0)),
            FactRow8::new(PredicateId(1), 2, &[bob, carol], SourceId(0)),
        ],
    ))
    .expect("load_facts must succeed for valid predicate");
    k
}

fn query_for(k: &Kernel, pred: PredicateId, a: &str, b: &str, mode: ProofMode) -> QueryAtom8 {
    let ta = k.catalog.term_id(a).expect("term a must be interned");
    let tb = k.catalog.term_id(b).expect("term b must be interned");
    let mut atom = Atom8::new(pred, 2, &[ta, tb]);
    atom.binding_mask = 0b11;
    QueryAtom8 {
        atom,
        output_mask: 0,
        proof_mode: mode,
        epoch: EpochId(0),
    }
}

// ── test: known fact → Allow with proof and valid receipt ─────────────────────

#[test]
fn known_fact_query_yields_allow_with_proof_and_receipt() {
    let k = build_kernel_with_facts();
    let q = query_for(&k, PredicateId(1), "alice", "bob", ProofMode::PositiveOnly);

    let answers = match k.query(&q) {
        QueryResult::Answered(v) => v,
        other => unreachable!("expected Answered, got {other:?}"),
    };

    assert_eq!(answers.len(), 1);
    let Decision {
        kind,
        proof,
        receipt,
        ..
    } = &answers[0];
    assert_eq!(*kind, DecisionKind::Allow);
    assert!(!proof.is_empty(), "positive proof must be present");
    assert_eq!(proof[0].kind, ProofKind::Fact);

    // Receipt integrity: recomputing must equal the stored hash.
    assert_ne!(receipt.receipt_hash, [0u8; 32]);
    assert_eq!(receipt.compute_hash(), receipt.receipt_hash);
}

// ── test: unknown fact → Deny with negative proof ────────────────────────────

#[test]
fn unknown_fact_query_yields_deny_with_negative_proof() {
    let k = build_kernel_with_facts();
    let q = query_for(
        &k,
        PredicateId(1),
        "alice",
        "carol",
        ProofMode::NegativeOnly,
    );

    let denied = match k.query(&q) {
        QueryResult::Denied(d) => d,
        other => unreachable!("expected Denied, got {other:?}"),
    };

    assert_eq!(denied.kind, DecisionKind::Deny);
    assert_eq!(denied.proof.len(), 1);
    assert_eq!(denied.proof[0].kind, ProofKind::MissingFact);
    assert_ne!(denied.receipt.receipt_hash, [0u8; 32]);
}

// ── test: receipt is deterministic across independent runs ────────────────────

#[test]
fn receipt_hash_is_deterministic_across_runs() {
    let k = build_kernel_with_facts();
    let q = query_for(&k, PredicateId(1), "alice", "bob", ProofMode::PositiveOnly);

    let h1 = match k.query(&q) {
        QueryResult::Answered(a) => a[0].receipt.receipt_hash,
        _ => unreachable!(),
    };
    let h2 = match k.query(&q) {
        QueryResult::Answered(a) => a[0].receipt.receipt_hash,
        _ => unreachable!(),
    };

    assert_eq!(h1, h2);
}

// ── test: one-step rule chaining ──────────────────────────────────────────────

#[test]
fn one_step_rule_chain_yields_allow_with_rule_proof_node() {
    let mut k = build_kernel_with_facts();
    let alice = k.catalog.term_id("alice").unwrap();
    let bob = k.catalog.term_id("bob").unwrap();

    // ancestor(alice, bob) :- parent(alice, bob).
    let head = Atom8::new(PredicateId(2), 2, &[alice, bob]);
    let body_atom = Atom8::new(PredicateId(1), 2, &[alice, bob]);
    let mut body_arr = [Atom8::new(PredicateId(1), 0, &[]); BODY_CAP as usize];
    body_arr[0] = body_atom;

    let rule = Rule8 {
        rule_id: RuleId(10),
        head,
        body: body_arr,
        body_len: 1,
        body_mask: 0b1,
        negation_mask: 0,
        builtin_mask: 0,
        var_count: 0,
        var_live_mask: 0,
        feature_mask: FeatureBit::Facts.mask() | FeatureBit::HornRules.mask(),
        proof_mask: 0,
        plan_id: PlanId::default(),
    };
    k.load_rule(rule).expect("rule must be admitted");

    let q = query_for(&k, PredicateId(2), "alice", "bob", ProofMode::Both);
    let answers = match k.query(&q) {
        QueryResult::Answered(v) => v,
        other => unreachable!("expected Answered, got {other:?}"),
    };

    assert!(!answers.is_empty());
    let rule_nodes: Vec<_> = answers[0]
        .proof
        .iter()
        .filter(|n| n.kind == ProofKind::Rule)
        .collect();
    assert_eq!(
        rule_nodes.len(),
        1,
        "exactly one Rule node expected in proof"
    );
}

// ── test: replay verifies a fresh receipt ────────────────────────────────────

#[test]
fn replay_verifies_fresh_allow_receipt() {
    let k = build_kernel_with_facts();
    let q = query_for(&k, PredicateId(1), "alice", "bob", ProofMode::PositiveOnly);

    let receipt = match k.query(&q) {
        QueryResult::Answered(a) => a[0].receipt.clone(),
        _ => unreachable!(),
    };

    assert_eq!(replay(&k, &q, &receipt), ReplayStatus::Verified);
}

#[test]
fn replay_detects_tampered_receipt_hash() {
    let k = build_kernel_with_facts();
    let q = query_for(&k, PredicateId(1), "alice", "bob", ProofMode::PositiveOnly);

    let mut receipt = match k.query(&q) {
        QueryResult::Answered(a) => a[0].receipt.clone(),
        _ => unreachable!(),
    };

    // Flip one byte in the receipt_hash — integrity check must fire.
    receipt.receipt_hash[0] ^= 0xFF;
    assert_eq!(replay(&k, &q, &receipt), ReplayStatus::ReceiptInvalid);
}

#[test]
fn replay_detects_tampered_proof_root() {
    let k = build_kernel_with_facts();
    let q = query_for(&k, PredicateId(1), "alice", "bob", ProofMode::PositiveOnly);

    let mut receipt = match k.query(&q) {
        QueryResult::Answered(a) => a[0].receipt.clone(),
        _ => unreachable!(),
    };

    // Alter proof_root, recompute the receipt hash so integrity passes,
    // but the freshly-replayed roots will not match → Mismatch.
    receipt.proof_root[0] ^= 0xFF;
    receipt.receipt_hash = receipt.compute_hash();
    assert_eq!(replay(&k, &q, &receipt), ReplayStatus::Mismatch);
}

// ── test: admission boundary enforcement ──────────────────────────────────────

#[test]
fn admit_atom_rejects_unknown_predicate() {
    let cat = base_catalog();
    let atom = Atom8::new(PredicateId(999), 2, &[TermId::new(1), TermId::new(2)]);
    assert_eq!(
        admit_atom(&atom, &cat),
        Err(RejectionCode::PredicateNotInCatalog)
    );
}

#[test]
fn admit_atom_rejects_arity_beyond_cap() {
    let mut cat = Catalog::new(CatalogId(1));
    cat.add_predicate(PredicateMeta {
        pred_id: PredicateId(1),
        label: "big".into(),
        arity: ARITY_CAP,
        access_orders: vec![],
        proof_policy: PredicateProofPolicy::Never,
        materialized: false,
    });
    // Manually tamper arity after construction so admission must reject it.
    let mut atom = Atom8::new(
        PredicateId(1),
        ARITY_CAP,
        &[TermId::new(1); ARITY_CAP as usize],
    );
    atom.arity = ARITY_CAP + 1; // exceeds cap
    assert_eq!(
        admit_atom(&atom, &cat),
        Err(RejectionCode::ArityCapExceeded)
    );
}

#[test]
fn load_facts_rejects_predicate_not_in_catalog() {
    let cat = base_catalog();
    let mut k = Kernel::new(cat);
    let result = k.load_facts(FactBlock8::new(PredicateId(999), 2, vec![]));
    assert_eq!(result, Err(RejectionCode::PredicateNotInCatalog));
}

// ── test: invalid atom query returns Invalid ──────────────────────────────────

#[test]
fn query_with_predicate_not_in_catalog_returns_invalid() {
    let k = build_kernel_with_facts();
    let mut atom = Atom8::new(PredicateId(999), 2, &[TermId::new(1), TermId::new(2)]);
    atom.binding_mask = 0b11;
    let q = QueryAtom8 {
        atom,
        output_mask: 0,
        proof_mode: ProofMode::PositiveOnly,
        epoch: EpochId(0),
    };
    assert!(matches!(k.query(&q), QueryResult::Invalid(_)));
}

// ── test: output_mask extracts requested bindings from answers ────────────────

#[test]
fn output_mask_returns_requested_bindings_in_decision() {
    let k = build_kernel_with_facts();
    let alice = k.catalog.term_id("alice").unwrap();

    // Query: parent(alice, ?) — position 0 bound, position 1 free (output).
    let mut atom = Atom8::new(PredicateId(1), 2, &[alice, TermId::sentinel()]);
    atom.binding_mask = 0b01; // bind position 0 (alice), position 1 is free
    let q = QueryAtom8 {
        atom,
        output_mask: 0b10, // request position 1 as output
        proof_mode: ProofMode::PositiveOnly,
        epoch: EpochId(0),
    };

    match k.query(&q) {
        QueryResult::Answered(answers) => {
            // alice is parent of bob only.
            assert_eq!(answers.len(), 1, "exactly one parent(alice, ?) in facts");
            // output_mask=0b10 → position 1 should be in bindings.
            assert_eq!(answers[0].bindings.len(), 1);
            let bob = k.catalog.term_id("bob").unwrap();
            assert_eq!(answers[0].bindings[0], bob);
        }
        other => unreachable!("expected Answered, got {other:?}"),
    }
}

// ── test: unbound query returns all matching rows ─────────────────────────────

#[test]
fn unbound_query_returns_all_matching_rows() {
    // Build kernel with both parent facts.
    let k = build_kernel_with_facts();
    // Query: parent(?, ?) — no bindings.
    let atom = Atom8::new(PredicateId(1), 2, &[TermId::sentinel(), TermId::sentinel()]);
    // binding_mask = 0: both positions free.
    let q = QueryAtom8 {
        atom,
        output_mask: 0b11, // request both positions as output
        proof_mode: ProofMode::PositiveOnly,
        epoch: EpochId(0),
    };

    match k.query(&q) {
        QueryResult::Answered(answers) => {
            assert_eq!(answers.len(), 2, "two parent facts in the block");
            // Each answer should return exactly 2 bindings.
            for ans in &answers {
                assert_eq!(ans.bindings.len(), 2);
            }
        }
        other => unreachable!("expected Answered, got {other:?}"),
    }
}

// ── test: MAX_ANSWERS limit — kernel returns all rows; wasm layer truncates ──
// The kernel itself has no answer cap; the cap is enforced in wasm.rs at the
// WASM boundary. This test confirms the kernel returns all 130 answers so that
// the wasm truncation layer has correct data to truncate.

#[test]
fn kernel_returns_all_answers_beyond_wasm_max() {
    // Build a kernel with 130 rows for the same predicate.
    // 130 > MAX_ANSWERS (128) to confirm the kernel itself does not truncate.
    let mut cat = Catalog::new(CatalogId(42));
    cat.add_predicate(PredicateMeta {
        pred_id: PredicateId(10),
        label: "item".into(),
        arity: 1,
        access_orders: vec![],
        proof_policy: PredicateProofPolicy::OnRequest,
        materialized: false,
    });
    let n_rows = 130usize;
    let terms: Vec<TermId> = (0..n_rows)
        .map(|i| cat.intern_term(format!("item_{}", i)))
        .collect();

    let rows: Vec<FactRow8> = terms
        .iter()
        .map(|&t| FactRow8::new(PredicateId(10), 1, &[t], SourceId(0)))
        .collect();

    let mut k = Kernel::new(cat);
    k.load_facts(FactBlock8::new(PredicateId(10), 1, rows))
        .unwrap();

    // Query with no bindings — should match all 130 rows.
    let atom = Atom8::new(PredicateId(10), 1, &[TermId::sentinel()]);
    let q = QueryAtom8 {
        atom,
        output_mask: 0b1,
        proof_mode: ProofMode::PositiveOnly,
        epoch: EpochId(0),
    };

    match k.query(&q) {
        QueryResult::Answered(answers) => {
            assert_eq!(
                answers.len(),
                n_rows,
                "kernel must return all {n_rows} answers without truncation; \
                 truncation to MAX_ANSWERS (128) is the WASM boundary's responsibility"
            );
        }
        other => unreachable!("expected Answered, got {other:?}"),
    }
}

// ── test: byte-cap enforcement — input exceeding MAX_INPUT_LEN must be caught
// at the WASM boundary (wasm.rs). This test verifies the guard condition by
// constructing the oversized-input branch path directly in the wasm module
// without invoking the actual WASM bindgen export (which requires wasm32 target).
// We test the Rust-level logic: a string larger than 10MiB is caught.

#[test]
fn byte_cap_guard_length_check() {
    // 10 * 1024 * 1024 = 10485760 bytes = MAX_INPUT_LEN defined in wasm.rs.
    const MAX_INPUT_LEN: usize = 10 * 1024 * 1024;

    // Build a string just under the cap: must NOT be rejected by length check.
    let under = "x".repeat(MAX_INPUT_LEN);
    assert!(
        under.len() <= MAX_INPUT_LEN,
        "under-cap string passes length guard"
    );

    // Build a string just over the cap.
    let over = "x".repeat(MAX_INPUT_LEN + 1);
    assert!(
        over.len() > MAX_INPUT_LEN,
        "over-cap string triggers length guard"
    );

    // Verify the boundary: exactly MAX_INPUT_LEN bytes is allowed (<=).
    let max_len_boundary = MAX_INPUT_LEN;
    assert!(max_len_boundary <= MAX_INPUT_LEN);
    assert!(max_len_boundary + 1 > MAX_INPUT_LEN);
}

// ── test: replay with altered fact block → Mismatch (not panic) ──────────────

#[test]
fn replay_with_wrong_facts_yields_mismatch_not_panic() {
    use prolog8::replay;

    let k = build_kernel_with_facts();
    let q = query_for(&k, PredicateId(1), "alice", "bob", ProofMode::PositiveOnly);

    let receipt = match k.query(&q) {
        QueryResult::Answered(a) => a[0].receipt.clone(),
        _ => unreachable!(),
    };

    // Build a second kernel with a DIFFERENT fact (bob→carol instead of alice→bob).
    // Receipt was issued against alice→bob; replaying against bob→carol should mismatch.
    let mut cat2 = base_catalog();
    let bob2 = cat2.intern_term("bob");
    let carol2 = cat2.intern_term("carol");
    let alice2 = cat2.intern_term("alice"); // still intern alice so query is valid
    let _ = alice2;

    let mut k2 = Kernel::new(cat2);
    k2.load_facts(FactBlock8::new(
        PredicateId(1),
        2,
        vec![FactRow8::new(
            PredicateId(1),
            2,
            &[bob2, carol2],
            SourceId(0),
        )],
    ))
    .unwrap();

    // Re-issue the query on k2 to get a fresh query atom with k2's catalog IDs.
    let alice2_id = k2.catalog.term_id("alice").unwrap();
    let bob2_id = k2.catalog.term_id("bob").unwrap();
    let mut atom2 = Atom8::new(PredicateId(1), 2, &[alice2_id, bob2_id]);
    atom2.binding_mask = 0b11;
    let q2 = QueryAtom8 {
        atom: atom2,
        output_mask: 0,
        proof_mode: ProofMode::PositiveOnly,
        epoch: EpochId(0),
    };

    // The receipt from k was computed against different catalog term IDs
    // (both catalogs have alice=1, bob=2 since intern order is the same, but
    // k has alice→bob in facts while k2 has bob→carol). The replay on k2
    // will either produce Denied (alice→bob not in k2) or Mismatch on the
    // fact/catalog roots; either way it must not panic.
    let status = replay(&k2, &q2, &receipt);
    assert_ne!(
        status,
        prolog8::ReplayStatus::Verified,
        "receipt from k cannot verify against k2 with different facts"
    );
}