vyre-primitives 0.6.1

Compositional primitives for vyre - marker types (always on) + Tier 2.5 LEGO substrate (feature-gated per domain).
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
//! Probabilistic knowledge compilation primitive (#38).
//!
//! Knowledge compilation (Darwiche 2002) compiles a probabilistic
//! logic program into a tractable circuit (d-DNNF, SDD). The
//! compilation step is host-side; the **evaluation** of a compiled
//! circuit is GPU-shaped  -  exactly what #10 sum_product_circuit
//! does. This file ships a thin wrapper that confirms the compose
//! contract and adds a host-side d-DNNF satisfiability oracle helper.
//!
//! # Why this primitive is dual-use
//!
//! | Consumer | Use |
//! |---|---|
//! | `vyre-libs::ml::probabilistic_logic` | neuro-symbolic systems |
//! | `vyre-libs::security::policy_engine` | rule-conflict resolution as probabilistic logic |

use std::sync::Arc;

use vyre_foundation::ir::model::expr::Ident;
use vyre_foundation::ir::{BufferAccess, BufferDecl, DataType, Expr, Node, Program};

/// d-DNNF "literal kind" tag.
pub const LITERAL_TRUE: u32 = 1;
/// d-DNNF "literal kind" tag for false.
pub const LITERAL_FALSE: u32 = 2;
/// AND node tag.
pub const AND_NODE: u32 = 3;
/// OR node tag.
pub const OR_NODE: u32 = 4;

/// Op id for the GPU-shaped d-DNNF evaluator.
pub const OP_ID: &str = "vyre-primitives::graph::ddnnf_evaluate";

/// Emit one bottom-up d-DNNF evaluation step. The dispatch is
/// `n_nodes` lanes; each lane evaluates one node from already-evaluated
/// children. Callers compose this with `level_wave_program` or another
/// topological wave scheduler when parent nodes must wait for child
/// outputs.
///
/// Buffers:
/// - `node_kinds`: u32 per node, using [`LITERAL_TRUE`],
///   [`LITERAL_FALSE`], [`AND_NODE`], [`OR_NODE`].
/// - `node_var`: u32 per node, meaningful for literal nodes.
/// - `child_offsets`: u32 per node into `children`.
/// - `child_counts`: u32 per node.
/// - `children`: concatenated child node indices.
/// - `var_assignments`: u32 per variable, 0/1/`u32::MAX` unknown.
/// - `out`: u32 per node.
#[must_use]
#[allow(clippy::too_many_arguments)]
pub fn ddnnf_evaluate(
    node_kinds: &str,
    node_var: &str,
    child_offsets: &str,
    child_counts: &str,
    children: &str,
    var_assignments: &str,
    out: &str,
    n_nodes: u32,
    n_children: u32,
    n_vars: u32,
) -> Program {
    match try_ddnnf_evaluate(
        node_kinds,
        node_var,
        child_offsets,
        child_counts,
        children,
        var_assignments,
        out,
        n_nodes,
        n_children,
        n_vars,
    ) {
        Ok(program) => program,
        Err(error) => crate::invalid_output_program(OP_ID, out, DataType::U32, error),
    }
}

/// Emit one bottom-up d-DNNF evaluation step with checked domain shape.
#[allow(clippy::too_many_arguments)]
pub fn try_ddnnf_evaluate(
    node_kinds: &str,
    node_var: &str,
    child_offsets: &str,
    child_counts: &str,
    children: &str,
    var_assignments: &str,
    out: &str,
    n_nodes: u32,
    n_children: u32,
    n_vars: u32,
) -> Result<Program, String> {
    if n_nodes == 0 {
        return Err(format!(
            "Fix: ddnnf_evaluate requires n_nodes > 0, got {n_nodes}."
        ));
    }
    if n_vars == 0 {
        return Err(format!(
            "Fix: ddnnf_evaluate requires n_vars > 0, got {n_vars}."
        ));
    }

    let lane = Expr::InvocationId { axis: 0 };
    let child_index = Expr::add(Expr::var("child_base"), Expr::var("k"));
    let body = vec![Node::if_then(
        Expr::lt(lane.clone(), Expr::u32(n_nodes)),
        vec![
            Node::let_bind("kind", Expr::load(node_kinds, lane.clone())),
            Node::let_bind("var_id", Expr::load(node_var, lane.clone())),
            Node::let_bind("child_base", Expr::load(child_offsets, lane.clone())),
            Node::let_bind("child_count", Expr::load(child_counts, lane.clone())),
            Node::if_then(
                Expr::eq(Expr::var("kind"), Expr::u32(LITERAL_TRUE)),
                vec![
                    Node::let_bind(
                        "assigned_true",
                        Expr::load(var_assignments, Expr::var("var_id")),
                    ),
                    Node::store(
                        out,
                        lane.clone(),
                        Expr::select(
                            Expr::or(
                                Expr::eq(Expr::var("assigned_true"), Expr::u32(1)),
                                Expr::eq(Expr::var("assigned_true"), Expr::u32(u32::MAX)),
                            ),
                            Expr::u32(1),
                            Expr::u32(0),
                        ),
                    ),
                ],
            ),
            Node::if_then(
                Expr::eq(Expr::var("kind"), Expr::u32(LITERAL_FALSE)),
                vec![
                    Node::let_bind(
                        "assigned_false",
                        Expr::load(var_assignments, Expr::var("var_id")),
                    ),
                    Node::store(
                        out,
                        lane.clone(),
                        Expr::select(
                            Expr::or(
                                Expr::eq(Expr::var("assigned_false"), Expr::u32(0)),
                                Expr::eq(Expr::var("assigned_false"), Expr::u32(u32::MAX)),
                            ),
                            Expr::u32(1),
                            Expr::u32(0),
                        ),
                    ),
                ],
            ),
            Node::if_then(
                Expr::eq(Expr::var("kind"), Expr::u32(AND_NODE)),
                vec![
                    Node::let_bind("acc_and", Expr::u32(1)),
                    Node::loop_for(
                        "k",
                        Expr::u32(0),
                        Expr::var("child_count"),
                        vec![
                            Node::let_bind("child_node", Expr::load(children, child_index.clone())),
                            Node::assign(
                                "acc_and",
                                Expr::mul(
                                    Expr::var("acc_and"),
                                    Expr::load(out, Expr::var("child_node")),
                                ),
                            ),
                        ],
                    ),
                    Node::store(out, lane.clone(), Expr::var("acc_and")),
                ],
            ),
            Node::if_then(
                Expr::eq(Expr::var("kind"), Expr::u32(OR_NODE)),
                vec![
                    Node::let_bind("acc_or", Expr::u32(0)),
                    Node::loop_for(
                        "kk",
                        Expr::u32(0),
                        Expr::var("child_count"),
                        vec![
                            Node::let_bind(
                                "or_child_node",
                                Expr::load(
                                    children,
                                    Expr::add(Expr::var("child_base"), Expr::var("kk")),
                                ),
                            ),
                            Node::assign(
                                "acc_or",
                                Expr::add(
                                    Expr::var("acc_or"),
                                    Expr::load(out, Expr::var("or_child_node")),
                                ),
                            ),
                        ],
                    ),
                    Node::store(out, lane.clone(), Expr::var("acc_or")),
                ],
            ),
            Node::if_then(
                Expr::and(
                    Expr::and(
                        Expr::ne(Expr::var("kind"), Expr::u32(LITERAL_TRUE)),
                        Expr::ne(Expr::var("kind"), Expr::u32(LITERAL_FALSE)),
                    ),
                    Expr::and(
                        Expr::ne(Expr::var("kind"), Expr::u32(AND_NODE)),
                        Expr::ne(Expr::var("kind"), Expr::u32(OR_NODE)),
                    ),
                ),
                vec![Node::store(out, lane.clone(), Expr::u32(0))],
            ),
        ],
    )];

    Ok(Program::wrapped(
        vec![
            BufferDecl::storage(node_kinds, 0, BufferAccess::ReadOnly, DataType::U32)
                .with_count(n_nodes),
            BufferDecl::storage(node_var, 1, BufferAccess::ReadOnly, DataType::U32)
                .with_count(n_nodes),
            BufferDecl::storage(child_offsets, 2, BufferAccess::ReadOnly, DataType::U32)
                .with_count(n_nodes),
            BufferDecl::storage(child_counts, 3, BufferAccess::ReadOnly, DataType::U32)
                .with_count(n_nodes),
            BufferDecl::storage(children, 4, BufferAccess::ReadOnly, DataType::U32)
                .with_count(n_children.max(1)),
            BufferDecl::storage(var_assignments, 5, BufferAccess::ReadOnly, DataType::U32)
                .with_count(n_vars),
            BufferDecl::storage(out, 6, BufferAccess::ReadWrite, DataType::U32).with_count(n_nodes),
        ],
        [256, 1, 1],
        vec![Node::Region {
            generator: Ident::from(OP_ID),
            source_region: None,
            body: Arc::new(body),
        }],
    ))
}

/// CPU helper: evaluate a d-DNNF compiled circuit under a partial
/// variable assignment. Returns the model count weighted by node
/// types (the canonical KC inference query).
///
/// `var_assignments[var_id] = 0/1/u32::MAX` (unknown).
/// `nodes[i] = (kind, child_offset, child_count)` row-major.
/// `node_var[i]` = variable id (only meaningful for literal nodes).
/// `topo_order` is the bottom-up evaluation order.
#[must_use]
#[cfg(any(test, feature = "cpu-parity"))]
pub fn ddnnf_evaluate_cpu(
    nodes: &[(u32, u32, u32)],
    node_var: &[u32],
    children: &[u32],
    var_assignments: &[u32],
    topo_order: &[u32],
) -> Vec<u32> {
    match try_ddnnf_evaluate_cpu(nodes, node_var, children, var_assignments, topo_order) {
        Ok(out) => out,
        Err(_) => Vec::new(),
    }
}

/// CPU helper with checked compiled-circuit indexing and arithmetic.
#[cfg(any(test, feature = "cpu-parity"))]
pub fn try_ddnnf_evaluate_cpu(
    nodes: &[(u32, u32, u32)],
    node_var: &[u32],
    children: &[u32],
    var_assignments: &[u32],
    topo_order: &[u32],
) -> Result<Vec<u32>, String> {
    let mut out = Vec::new();
    try_ddnnf_evaluate_cpu_into(
        nodes,
        node_var,
        children,
        var_assignments,
        topo_order,
        &mut out,
    )?;
    Ok(out)
}

/// CPU helper with checked indexing/arithmetic and caller-owned output storage.
#[cfg(any(test, feature = "cpu-parity"))]
pub fn try_ddnnf_evaluate_cpu_into(
    nodes: &[(u32, u32, u32)],
    node_var: &[u32],
    children: &[u32],
    var_assignments: &[u32],
    topo_order: &[u32],
    out: &mut Vec<u32>,
) -> Result<(), String> {
    let mut scratch = DdnnfCpuScratch::default();
    try_ddnnf_evaluate_cpu_into_with_scratch(
        nodes,
        node_var,
        children,
        var_assignments,
        topo_order,
        out,
        &mut scratch,
    )
}

/// Caller-owned workspace for d-DNNF CPU evaluation.
#[cfg(any(test, feature = "cpu-parity"))]
#[derive(Debug, Default, Clone)]
pub struct DdnnfCpuScratch {
    /// Transactional value buffer populated before committing to caller output.
    pub values: Vec<u32>,
}

#[cfg(any(test, feature = "cpu-parity"))]
impl DdnnfCpuScratch {
    /// Create an empty reusable d-DNNF evaluation workspace.
    pub fn new() -> Self {
        Self::default()
    }
}

/// CPU helper with caller-owned output and transactional scratch storage.
///
/// Malformed compiled circuits are rejected before `out` or `scratch` are
/// cleared. Arithmetic failures leave `out` unchanged because all writes happen
/// in the scratch buffer until the full evaluation succeeds.
#[cfg(any(test, feature = "cpu-parity"))]
pub fn try_ddnnf_evaluate_cpu_into_with_scratch(
    nodes: &[(u32, u32, u32)],
    node_var: &[u32],
    children: &[u32],
    var_assignments: &[u32],
    topo_order: &[u32],
    out: &mut Vec<u32>,
    scratch: &mut DdnnfCpuScratch,
) -> Result<(), String> {
    validate_ddnnf_evaluate_inputs(nodes, node_var, children, var_assignments, topo_order)?;
    let n_nodes = nodes.len();
    scratch.values.clear();
    resize_ddnnf_cpu_vec(
        &mut scratch.values,
        n_nodes,
        0u32,
        "ddnnf_evaluate CPU scratch",
    )?;
    for &node in topo_order {
        let i = node as usize;
        let (kind, co, cc) = nodes[i];
        match kind {
            LITERAL_TRUE => {
                let assigned = var_assignments[node_var[i] as usize];
                scratch.values[i] = if assigned == 1 || assigned == u32::MAX {
                    1
                } else {
                    0
                };
            }
            LITERAL_FALSE => {
                let assigned = var_assignments[node_var[i] as usize];
                scratch.values[i] = if assigned == 0 || assigned == u32::MAX {
                    1
                } else {
                    0
                };
            }
            AND_NODE => {
                let mut acc = 1u32;
                for k in 0..cc as usize {
                    let child_index = co as usize + k;
                    let cn = children[child_index] as usize;
                    let child_value = scratch.values[cn];
                    acc = acc.checked_mul(child_value).ok_or_else(|| {
                        format!(
                            "ddnnf_evaluate CPU oracle AND node {i} model count overflowed u32. Fix: shard or widen model-count accumulation."
                        )
                    })?;
                }
                scratch.values[i] = acc;
            }
            OR_NODE => {
                let mut acc = 0u32;
                for k in 0..cc as usize {
                    let child_index = co as usize + k;
                    let cn = children[child_index] as usize;
                    let child_value = scratch.values[cn];
                    acc = acc.checked_add(child_value).ok_or_else(|| {
                        format!(
                            "ddnnf_evaluate CPU oracle OR node {i} model count overflowed u32. Fix: shard or widen model-count accumulation."
                        )
                    })?;
                }
                scratch.values[i] = acc;
            }
            _ => {
                scratch.values[i] = 0;
            }
        }
    }
    if n_nodes > out.len() {
        crate::graph::scratch::reserve_graph_items(
            out,
            n_nodes - out.len(),
            "d-DNNF CPU oracle",
            "ddnnf_evaluate CPU output",
        )?;
    }
    out.clear();
    out.extend_from_slice(&scratch.values);
    Ok(())
}

#[cfg(any(test, feature = "cpu-parity"))]
fn validate_ddnnf_evaluate_inputs(
    nodes: &[(u32, u32, u32)],
    node_var: &[u32],
    children: &[u32],
    var_assignments: &[u32],
    topo_order: &[u32],
) -> Result<(), String> {
    let n_nodes = nodes.len();
    if node_var.len() != n_nodes {
        return Err(format!(
            "ddnnf_evaluate CPU oracle received node_var_len={} for node_count={n_nodes}. Fix: pass one variable slot per compiled node.",
            node_var.len()
        ));
    }
    for &node in topo_order {
        let i = node as usize;
        let Some(&(kind, co, cc)) = nodes.get(i) else {
            return Err(format!(
                "ddnnf_evaluate CPU oracle topo node {node} is outside node_count={n_nodes}. Fix: rebuild the compiled-circuit topological order."
            ));
        };
        match kind {
            LITERAL_TRUE | LITERAL_FALSE => {
                let v = node_var[i] as usize;
                if v >= var_assignments.len() {
                    return Err(format!(
                        "ddnnf_evaluate CPU oracle literal node {i} references var {v} outside assignment_count={}. Fix: pass a complete assignment vector.",
                        var_assignments.len()
                    ));
                }
            }
            AND_NODE | OR_NODE => {
                let co = co as usize;
                let cc = cc as usize;
                let end = co.checked_add(cc).ok_or_else(|| {
                    format!(
                        "ddnnf_evaluate CPU oracle child offset overflow at node {i}. Fix: rebuild child_offsets before parity comparison."
                    )
                })?;
                if end > children.len() {
                    return Err(format!(
                        "ddnnf_evaluate CPU oracle node {i} child range {co}..{end} exceeds child_count={}. Fix: pass a complete child list.",
                        children.len()
                    ));
                }
                for child_index in co..end {
                    let cn = children[child_index] as usize;
                    if cn >= n_nodes {
                        return Err(format!(
                            "ddnnf_evaluate CPU oracle node {i} references child node {cn} outside node_count={n_nodes}. Fix: rebuild compiled child ids."
                        ));
                    }
                }
            }
            _ => {}
        }
    }
    Ok(())
}

#[cfg(any(test, feature = "cpu-parity"))]

fn resize_ddnnf_cpu_vec<T: Clone>(
    out: &mut Vec<T>,
    len: usize,
    value: T,
    context: &str,
) -> Result<(), String> {
    if len > out.len() {
        crate::graph::scratch::reserve_graph_items(
            out,
            len - out.len(),
            "d-DNNF CPU oracle",
            context,
        )?;
    }
    out.resize(len, value);
    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn cpu_single_true_literal_with_assigned_var() {
        // 1 node, kind=LITERAL_TRUE, var 0 assigned to 1 → out = 1.
        let nodes = vec![(LITERAL_TRUE, 0, 0)];
        let node_var = vec![0];
        let children = vec![];
        let assigns = vec![1];
        let order = vec![0];
        let out = ddnnf_evaluate_cpu(&nodes, &node_var, &children, &assigns, &order);
        assert_eq!(out[0], 1);
    }

    #[test]
    fn cpu_single_true_literal_with_unset_var() {
        // var 0 unknown → output 1 (counts both true assignments).
        let nodes = vec![(LITERAL_TRUE, 0, 0)];
        let node_var = vec![0];
        let children = vec![];
        let assigns = vec![u32::MAX];
        let order = vec![0];
        let out = ddnnf_evaluate_cpu(&nodes, &node_var, &children, &assigns, &order);
        assert_eq!(out[0], 1);
    }

    #[test]
    fn cpu_and_of_two_literals() {
        // (x_0=true) AND (x_1=true), both unknown → mc = 1
        let nodes = vec![(LITERAL_TRUE, 0, 0), (LITERAL_TRUE, 0, 0), (AND_NODE, 0, 2)];
        let node_var = vec![0, 1, 0];
        let children = vec![0, 1];
        let assigns = vec![u32::MAX; 2];
        let order = vec![0, 1, 2];
        let out = ddnnf_evaluate_cpu(&nodes, &node_var, &children, &assigns, &order);
        assert_eq!(out[2], 1);
    }

    #[test]
    fn cpu_or_of_two_literals_counts_both() {
        // (x_0=true) OR (x_1=true), both unknown → mc = 2
        let nodes = vec![(LITERAL_TRUE, 0, 0), (LITERAL_TRUE, 0, 0), (OR_NODE, 0, 2)];
        let node_var = vec![0, 1, 0];
        let children = vec![0, 1];
        let assigns = vec![u32::MAX; 2];
        let order = vec![0, 1, 2];
        let out = ddnnf_evaluate_cpu(&nodes, &node_var, &children, &assigns, &order);
        assert_eq!(out[2], 2);
    }

    #[test]
    fn cpu_partial_assignment_constrains_count() {
        // With var 0 fixed to true, the OR (x_0 OR x_1) becomes
        // mc = 1 (x_0 satisfied) for any x_1.
        let nodes = vec![(LITERAL_TRUE, 0, 0), (LITERAL_TRUE, 0, 0), (OR_NODE, 0, 2)];
        let node_var = vec![0, 1, 0];
        let children = vec![0, 1];
        let assigns = vec![1, 0]; // x_0 = true, x_1 = false
        let order = vec![0, 1, 2];
        let out = ddnnf_evaluate_cpu(&nodes, &node_var, &children, &assigns, &order);
        // out[0] = 1 (x_0 = true literal evaluates to 1)
        // out[1] = 0 (x_1 = true literal but x_1 is assigned false)
        // out[2] = 1 + 0 = 1
        assert_eq!(out[2], 1);
    }

    #[test]
    fn checked_cpu_oracle_rejects_missing_assignment() {
        let nodes = vec![(LITERAL_TRUE, 0, 0)];
        let node_var = vec![7];
        let children = vec![];
        let assigns = vec![u32::MAX];
        let order = vec![0];
        let error = try_ddnnf_evaluate_cpu(&nodes, &node_var, &children, &assigns, &order)
            .expect_err("checked d-DNNF oracle must reject missing variable assignments");

        assert!(
            error.contains("outside assignment_count"),
            "error should describe the missing assignment: {error}"
        );
    }

    #[test]
    fn checked_cpu_oracle_rejects_missing_child() {
        let nodes = vec![(LITERAL_TRUE, 0, 0), (AND_NODE, 0, 1)];
        let node_var = vec![0, 0];
        let children = vec![];
        let assigns = vec![u32::MAX];
        let order = vec![0, 1];
        let error = try_ddnnf_evaluate_cpu(&nodes, &node_var, &children, &assigns, &order)
            .expect_err("checked d-DNNF oracle must reject missing child list entries");

        assert!(
            error.contains("exceeds child_count"),
            "error should describe the missing child entry: {error}"
        );
    }

    #[test]
    fn scratch_cpu_oracle_rejects_bad_child_without_clobbering_storage() {
        let nodes = vec![(LITERAL_TRUE, 0, 0), (AND_NODE, 0, 1)];
        let node_var = vec![0, 0];
        let children = vec![9];
        let assigns = vec![u32::MAX];
        let order = vec![0, 1];
        let mut out = vec![0xDEAD_BEEF, 0xCAFE_BABE];
        let mut scratch = DdnnfCpuScratch {
            values: vec![7, 8, 9],
        };

        let error = try_ddnnf_evaluate_cpu_into_with_scratch(
            &nodes,
            &node_var,
            &children,
            &assigns,
            &order,
            &mut out,
            &mut scratch,
        )
        .expect_err("checked d-DNNF oracle must reject out-of-range child ids");

        assert!(
            error.contains("outside node_count"),
            "error should describe the invalid child node: {error}"
        );
        assert_eq!(
            out,
            vec![0xDEAD_BEEF, 0xCAFE_BABE],
            "Fix: malformed d-DNNF inputs must not clobber caller output."
        );
        assert_eq!(
            scratch.values,
            vec![7, 8, 9],
            "Fix: structural validation failures must not clear reusable scratch."
        );
    }

    #[test]
    fn scratch_cpu_oracle_reuses_values_and_clears_stale_tail() {
        let nodes = vec![(LITERAL_TRUE, 0, 0), (LITERAL_FALSE, 0, 0), (OR_NODE, 0, 2)];
        let node_var = vec![0, 1, 0];
        let children = vec![0, 1];
        let assigns = vec![1, 0];
        let order = vec![0, 1, 2];
        let mut out = Vec::with_capacity(8);
        out.extend_from_slice(&[99, 98, 97, 96]);
        let mut scratch = DdnnfCpuScratch {
            values: Vec::with_capacity(8),
        };
        scratch.values.extend_from_slice(&[11, 12, 13, 14, 15]);
        let out_capacity = out.capacity();
        let scratch_capacity = scratch.values.capacity();

        try_ddnnf_evaluate_cpu_into_with_scratch(
            &nodes,
            &node_var,
            &children,
            &assigns,
            &order,
            &mut out,
            &mut scratch,
        )
        .expect("Fix: replace expect with fallible API or document caller precondition; panic only on programmer error - valid d-DNNF circuit must evaluate into reusable storage");

        assert_eq!(out, vec![1, 1, 2]);
        assert_eq!(scratch.values, vec![1, 1, 2]);
        assert_eq!(out.capacity(), out_capacity);
        assert_eq!(scratch.values.capacity(), scratch_capacity);

        try_ddnnf_evaluate_cpu_into_with_scratch(
            &nodes[..1],
            &node_var[..1],
            &[],
            &assigns,
            &[0],
            &mut out,
            &mut scratch,
        )
        .expect("Fix: replace expect with fallible API or document caller precondition; panic only on programmer error - smaller d-DNNF circuit must reuse and truncate storage");

        assert_eq!(out, vec![1]);
        assert_eq!(scratch.values, vec![1]);
        assert_eq!(out.capacity(), out_capacity);
        assert_eq!(scratch.values.capacity(), scratch_capacity);
    }

    #[test]
    fn generated_cpu_oracle_matches_independent_ddnnf_evaluator() {
        let mut out = Vec::new();
        let mut scratch = DdnnfCpuScratch::new();
        for case in 0..2048usize {
            let n_literals = 1 + case % 6;
            let n_nodes = n_literals + 4;
            let n_vars = 1 + (case / 7) % 6;
            let mut nodes = Vec::new();
            let mut node_var = Vec::new();
            let mut children = Vec::new();

            for idx in 0..n_literals {
                let kind = if (case + idx) % 2 == 0 {
                    LITERAL_TRUE
                } else {
                    LITERAL_FALSE
                };
                nodes.push((kind, 0, 0));
                node_var.push((idx % n_vars) as u32);
            }

            for op_idx in 0..4usize {
                let child_count = 1 + ((case + op_idx) % n_literals);
                let offset = children.len() as u32;
                for child in 0..child_count {
                    children.push(((child + op_idx) % (n_literals + op_idx)) as u32);
                }
                let kind = if op_idx % 2 == 0 { AND_NODE } else { OR_NODE };
                nodes.push((kind, offset, child_count as u32));
                node_var.push(0);
            }

            let assignments: Vec<u32> = (0..n_vars)
                .map(|idx| match (case + idx) % 3 {
                    0 => 0,
                    1 => 1,
                    _ => u32::MAX,
                })
                .collect();
            let topo_order: Vec<u32> = (0..n_nodes as u32).collect();

            try_ddnnf_evaluate_cpu_into_with_scratch(
                &nodes,
                &node_var,
                &children,
                &assignments,
                &topo_order,
                &mut out,
                &mut scratch,
            )
            .expect("Fix: caller must pre-size buffers; use fallible reserve or return ResourceExhausted - generated d-DNNF CPU oracle should reserve and evaluate");
            let expected =
                independent_ddnnf_evaluate(&nodes, &node_var, &children, &assignments, &topo_order);

            assert_eq!(out, expected, "case {case}: d-DNNF evaluation mismatch");
        }
    }

    fn independent_ddnnf_evaluate(
        nodes: &[(u32, u32, u32)],
        node_var: &[u32],
        children: &[u32],
        assignments: &[u32],
        topo_order: &[u32],
    ) -> Vec<u32> {
        let mut out = Vec::new();
        out.resize(nodes.len(), 0);
        for &node in topo_order {
            let i = node as usize;
            let (kind, offset, count) = nodes[i];
            out[i] = match kind {
                LITERAL_TRUE => {
                    let assigned = assignments[node_var[i] as usize];
                    u32::from(assigned == 1 || assigned == u32::MAX)
                }
                LITERAL_FALSE => {
                    let assigned = assignments[node_var[i] as usize];
                    u32::from(assigned == 0 || assigned == u32::MAX)
                }
                AND_NODE => {
                    let mut acc = 1u32;
                    for k in 0..count as usize {
                        acc *= out[children[offset as usize + k] as usize];
                    }
                    acc
                }
                OR_NODE => {
                    let mut acc = 0u32;
                    for k in 0..count as usize {
                        acc += out[children[offset as usize + k] as usize];
                    }
                    acc
                }
                _ => 0,
            };
        }
        out
    }

    #[test]
    fn gpu_program_builder_exposes_ddnnf_buffers() {
        let program = ddnnf_evaluate(
            "kinds",
            "node_var",
            "child_offsets",
            "child_counts",
            "children",
            "assignments",
            "out",
            3,
            2,
            2,
        );
        assert_eq!(program.buffers().len(), 7);
        assert_eq!(program.workgroup_size(), [256, 1, 1]);
        assert!(
            program
                .entry()
                .iter()
                .any(|node| matches!(node, vyre_foundation::ir::Node::Region { generator, .. } if generator.as_str() == OP_ID))
        );
    }

    #[test]
    fn gpu_program_builder_rejects_empty_node_count_with_trap_program() {
        let program = ddnnf_evaluate(
            "kinds",
            "node_var",
            "child_offsets",
            "child_counts",
            "children",
            "assignments",
            "out",
            0,
            0,
            1,
        );
        assert_eq!(program.buffers().len(), 1);
        assert!(
            program
                .entry()
                .iter()
                .any(|node| matches!(node, vyre_foundation::ir::Node::Region { body, .. } if body.iter().any(|inner| matches!(inner, vyre_foundation::ir::Node::Trap { .. }))))
        );
    }

    #[test]
    fn checked_gpu_builder_rejects_empty_var_domain() {
        let error = try_ddnnf_evaluate(
            "kinds",
            "node_var",
            "child_offsets",
            "child_counts",
            "children",
            "assignments",
            "out",
            1,
            0,
            0,
        )
        .expect_err("checked d-DNNF builder must reject empty variable domains");

        assert!(
            error.contains("requires n_vars > 0"),
            "error should describe the invalid variable domain: {error}"
        );
    }

    #[test]
    fn gpu_builder_source_has_checked_api_without_panics() {
        let source = include_str!("knowledge_compile.rs");
        let builder_source = source
            .split("pub fn ddnnf_evaluate(")
            .nth(1)
            .expect("Fix: d-DNNF GPU builder source must be present")
            .split("/// CPU helper:")
            .next()
            .expect("Fix: d-DNNF GPU builder source must precede CPU oracle");

        assert!(
            builder_source.contains("pub fn try_ddnnf_evaluate(")
                && !builder_source.contains(concat!("panic", "!("))
                && !builder_source.contains(".unwrap_or_else("),
            "Fix: ddnnf_evaluate must expose a checked release API and avoid production panics."
        );
    }

    #[test]
    fn ddnnf_cpu_source_uses_fallible_reusable_output() {
        let source = include_str!("knowledge_compile.rs");
        let cpu_source = source
            .split("/// CPU helper:")
            .nth(1)
            .expect("Fix: d-DNNF CPU source must be present")
            .split("#[cfg(test)]")
            .next()
            .expect("Fix: d-DNNF CPU source must precede tests");

        assert!(
            cpu_source.contains("try_ddnnf_evaluate_cpu_into")
                && cpu_source.contains("resize_ddnnf_cpu_vec")
                && cpu_source.contains("crate::graph::scratch::reserve_graph_items")
                && !cpu_source.contains("fn reserve_ddnnf_cpu_vec")
                && !cpu_source.contains("vec![0u32; n_nodes]")
                && !cpu_source.contains("Vec::with_capacity")
                && !cpu_source.contains(".reserve("),
            "Fix: d-DNNF CPU oracle must use fallible caller-owned output storage."
        );
    }
}