clm-kernel 0.0.1

Cubical Logic Model (CLM) sovereign execution engine, CPN-VM, and Cordis spatiotemporal substrate
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
use serde_json::Value;
use std::path::Path;

/// Optional per-relation tuning parsed from the spec YAML.
#[derive(Default, Debug)]
struct RelationOptions {
    normalize_newlines: bool,
    trim_whitespace: bool,
    ignore_ansi: bool,
    ignore_case: bool,
    stop_on_first_mismatch: bool,
    required_degree: Option<f64>,
}

fn strip_ansi(s: &str) -> String {
    // Remove ANSI escape sequences: ESC [ ... m
    let re = regex::Regex::new(r"\x1B\[[0-9;]*m").unwrap_or_else(|_| regex::Regex::new(r"").unwrap());
    re.replace_all(s, "").into_owned()
}

fn normalize_output(s: &str, opts: &RelationOptions) -> String {
    let mut out = s.to_string();
    if opts.normalize_newlines {
        out = out.replace("\r\n", "\n").replace('\r', "\n");
    }
    if opts.trim_whitespace {
        out = out.trim().to_string();
    }
    if opts.ignore_ansi {
        out = strip_ansi(&out);
    }
    if opts.ignore_case {
        out = out.to_lowercase();
    }
    out
}

fn find_divergence(a: &str, b: &str) -> Option<(usize, char, char)> {
    let a_chars: Vec<char> = a.chars().collect();
    let b_chars: Vec<char> = b.chars().collect();
    for i in 0..a_chars.len().min(b_chars.len()) {
        if a_chars[i] != b_chars[i] {
            return Some((i, a_chars[i], b_chars[i]));
        }
    }
    if a_chars.len() != b_chars.len() {
        let idx = a_chars.len().max(b_chars.len()) - 1;
        let ac = a_chars.get(idx).copied().unwrap_or('\0');
        let bc = b_chars.get(idx).copied().unwrap_or('\0');
        return Some((idx, ac, bc));
    }
    None
}

/// Parses an invariant expression of the form:
/// prefix:func_name(arg1, arg2, ...) or prefix:func_name
pub fn parse_expression(expr: &str) -> Result<(String, String, Vec<String>), String> {
    let expr = expr.trim();
    let colon_idx = expr.find(':')
        .ok_or_else(|| format!("Invalid expression format (missing prefix): {}", expr))?;
    let prefix = expr[0..colon_idx].trim().to_string();
    let rest = &expr[colon_idx + 1..].trim();
    
    if let Some(paren_start) = rest.find('(') {
        let paren_end = rest.rfind(')')
            .ok_or_else(|| format!("Missing closing parenthesis in expression: {}", expr))?;
        let func_name = rest[0..paren_start].trim().to_string();
        let args_str = rest[paren_start + 1..paren_end].trim();
        let mut args = Vec::new();
        if !args_str.is_empty() {
            for arg in args_str.split(',') {
                args.push(arg.trim().to_string());
            }
        }
        Ok((prefix, func_name, args))
    } else {
        let func_name = rest.to_string();
        Ok((prefix, func_name, Vec::new()))
    }
}

/// Evaluates a formal invariant checking gate.
/// Resolves variables in arguments using the provided JSON context.
pub fn evaluate_invariant(expr: &str, clm_root: &str, context: &Value) -> Result<bool, String> {
    let expr = expr.trim();
    if expr.is_empty() {
        return Ok(true);
    }

    // Check if it is a prefix expression prefix:func_name(args)
    // We assume it is prefix if it contains ':' and does not contain infix comparison operators.
    let is_prefix = expr.contains(':')
        && !expr.contains('>')
        && !expr.contains('<')
        && !expr.contains("==")
        && !expr.contains("!=")
        && !expr.contains('%');

    if is_prefix {
        let (prefix, func_name, args) = parse_expression(expr)?;
        
        let mut resolved_args = Vec::new();
        for arg in &args {
            if arg.starts_with('"') && arg.ends_with('"') {
                resolved_args.push(arg[1..arg.len() - 1].to_string());
            } else if arg.starts_with("inputs.") || arg.starts_with("outputs.") {
                let val = resolve_value(arg, context)
                    .ok_or_else(|| format!("Could not resolve path reference: {}", arg))?;
                resolved_args.push(val.as_str().map(|s| s.to_string()).unwrap_or_else(|| val.to_string()));
            } else {
                if let Some(val) = resolve_value(arg, context) {
                    resolved_args.push(val.as_str().map(|s| s.to_string()).unwrap_or_else(|| val.to_string()));
                } else {
                    resolved_args.push(arg.clone());
                }
            }
        }

        match (prefix.as_str(), func_name.as_str()) {
            ("gate", "file_exists") => {
                if resolved_args.is_empty() {
                    return Err("gate:file_exists requires at least 1 argument".to_string());
                }
                let path_str = &resolved_args[0];
                let path = Path::new(path_str);
                let resolved_path = if path.is_absolute() {
                    path.to_path_buf()
                } else {
                    Path::new(clm_root).join(path_str)
                };
                Ok(resolved_path.exists())
            }
            ("gate", "is_sha256") => {
                if resolved_args.is_empty() {
                    return Err("gate:is_sha256 requires at least 1 argument".to_string());
                }
                let hash_str = &resolved_args[0];
                if !hash_str.starts_with(crate::hash::DEFAULT_HASH_PREFIX) || hash_str.len() != 71 {
                    return Ok(false);
                }
                let hex_part = crate::hash::strip_prefix(hash_str);
                Ok(hex_part.chars().all(|c| c.is_ascii_hexdigit()))
            }
            ("gate", "payload_nonempty") => {
                let payload = if resolved_args.is_empty() {
                    std::env::var("CLM_PAYLOAD").unwrap_or_default()
                } else {
                    resolved_args[0].clone()
                };
                Ok(!payload.trim().is_empty())
            }
            ("gate", "result_sealed") => {
                let hash = if resolved_args.is_empty() {
                    if let Some(v) = resolve_value("outputs.a_hash", context) {
                        v.as_str().unwrap_or("").to_string()
                    } else if let Some(v) = resolve_value("outputs.c_hash", context) {
                        v.as_str().unwrap_or("").to_string()
                    } else {
                        // Try env CLM_ACTIVE_C_HASH
                        std::env::var("CLM_ACTIVE_C_HASH").unwrap_or_default()
                    }
                } else {
                    resolved_args[0].clone()
                };
                if hash.is_empty() {
                    return Ok(false);
                }
                let exists = crate::db::card_exists("content_memory", clm_root, &hash)
                    .map_err(|e| format!("Database check failed: {}", e))?;
                Ok(exists)
            }
            ("conservation", "noetherian_check") => {
                for hash in &resolved_args {
                    let exists = crate::db::card_exists("content_memory", clm_root, hash)
                        .map_err(|e| format!("Database check failed: {}", e))?;
                    if !exists {
                        return Ok(false);
                    }
                }
                Ok(true)
            }
            _ => Err(format!("Unknown invariant function: {}:{}", prefix, func_name)),
        }
    } else {
        // Fall back to math/comparison expression
        evaluate_infix_expression(expr, context)
    }
}

fn evaluate_infix_expression(expr: &str, context: &Value) -> Result<bool, String> {
    let expr = expr.trim();
    let operators = vec!["==", "!=", ">=", "<=", ">", "<"];
    let mut found_op = None;
    for op in &operators {
        if let Some(idx) = expr.find(op) {
            found_op = Some((*op, idx));
            break;
        }
    }

    if let Some((op, idx)) = found_op {
        let lhs_str = expr[0..idx].trim();
        let rhs_str = expr[idx + op.len()..].trim();

        let lhs_val = evaluate_arithmetic(lhs_str, context)?;
        let rhs_val = evaluate_arithmetic(rhs_str, context)?;

        match op {
            "==" => Ok(lhs_val == rhs_val),
            "!=" => Ok(lhs_val != rhs_val),
            ">" => {
                let l = lhs_val.as_f64().ok_or_else(|| format!("LHS is not a number: {}", lhs_str))?;
                let r = rhs_val.as_f64().ok_or_else(|| format!("RHS is not a number: {}", rhs_str))?;
                Ok(l > r)
            }
            ">=" => {
                let l = lhs_val.as_f64().ok_or_else(|| format!("LHS is not a number: {}", lhs_str))?;
                let r = rhs_val.as_f64().ok_or_else(|| format!("RHS is not a number: {}", rhs_str))?;
                Ok(l >= r)
            }
            "<" => {
                let l = lhs_val.as_f64().ok_or_else(|| format!("LHS is not a number: {}", lhs_str))?;
                let r = rhs_val.as_f64().ok_or_else(|| format!("RHS is not a number: {}", rhs_str))?;
                Ok(l < r)
            }
            "<=" => {
                let l = lhs_val.as_f64().ok_or_else(|| format!("LHS is not a number: {}", lhs_str))?;
                let r = rhs_val.as_f64().ok_or_else(|| format!("RHS is not a number: {}", rhs_str))?;
                Ok(l <= r)
            }
            _ => unreachable!(),
        }
    } else {
        let val = evaluate_arithmetic(expr, context)?;
        if let Some(b) = val.as_bool() {
            Ok(b)
        } else if let Some(n) = val.as_i64() {
            Ok(n != 0)
        } else if let Some(s) = val.as_str() {
            Ok(!s.is_empty())
        } else {
            Ok(!val.is_null())
        }
    }
}

fn evaluate_arithmetic(expr: &str, context: &Value) -> Result<Value, String> {
    let expr = expr.trim();
    if let Some(idx) = expr.find('%') {
        let lhs_str = expr[0..idx].trim();
        let rhs_str = expr[idx + 1..].trim();
        let lhs_val = evaluate_arithmetic(lhs_str, context)?;
        let rhs_val = evaluate_arithmetic(rhs_str, context)?;
        let l = lhs_val.as_i64().ok_or_else(|| format!("LHS of % is not an integer: {}", lhs_str))?;
        let r = rhs_val.as_i64().ok_or_else(|| format!("RHS of % is not an integer: {}", rhs_str))?;
        if r == 0 {
            return Err("Modulo by zero".to_string());
        }
        return Ok(Value::from(l % r));
    }

    if expr.starts_with('"') && expr.ends_with('"') {
        return Ok(Value::String(expr[1..expr.len() - 1].to_string()));
    }

    if let Ok(n) = expr.parse::<i64>() {
        return Ok(Value::from(n));
    }
    if let Ok(f) = expr.parse::<f64>() {
        return Ok(Value::from(f));
    }

    if expr == "true" {
        return Ok(Value::Bool(true));
    }
    if expr == "false" {
        return Ok(Value::Bool(false));
    }

    if let Some(val) = resolve_value(expr, context) {
        return Ok(val.clone());
    }

    Err(format!("Could not resolve token or value: {}", expr))
}

fn resolve_value<'a>(path: &str, context: &'a Value) -> Option<&'a Value> {
    let parts: Vec<&str> = path.split('.').collect();
    let mut current = context;
    for part in parts {
        {
            let val = current.get(part)?;
            current = val;
        }
    }
    Some(current)
}

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

    #[test]
    fn test_parse_expression() {
        let (p, f, args) = parse_expression("gate:file_exists(inputs.clm_file)").unwrap();
        assert_eq!(p, "gate");
        assert_eq!(f, "file_exists");
        assert_eq!(args, vec!["inputs.clm_file"]);

        let (p2, f2, args2) = parse_expression("gate:payload_nonempty").unwrap();
        assert_eq!(p2, "gate");
        assert_eq!(f2, "payload_nonempty");
        assert_eq!(args2.len(), 0);
    }

    #[test]
    fn test_evaluate_invariant_prefix() {
        let context = json!({
            "inputs": {
                "clm_file": "Cargo.toml"
            }
        });
        let res = evaluate_invariant("gate:file_exists(inputs.clm_file)", ".", &context).unwrap();
        assert!(res);

        let res2 = evaluate_invariant("gate:file_exists(\"non_existent_file.yaml\")", ".", &context).unwrap();
        assert!(!res2);
    }

    #[test]
    fn test_evaluate_invariant_infix_math() {
        let context = json!({
            "inputs": {
                "val": 5
            },
            "outputs": {
                "result": 8
            }
        });
        
        let res1 = evaluate_invariant("inputs.val > 0", ".", &context).unwrap();
        assert!(res1);

        let res2 = evaluate_invariant("inputs.val < 0", ".", &context).unwrap();
        assert!(!res2);

        let res3 = evaluate_invariant("outputs.result % 2 == 0", ".", &context).unwrap();
        assert!(res3);

        let res4 = evaluate_invariant("outputs.result % 3 == 0", ".", &context).unwrap();
        assert!(!res4);
    }
}

pub fn assess_equivalence(clm_root: &str, clm_file: &str) -> Result<(i32, String), String> {
    use std::fs;
    use std::path::{Path, PathBuf};

    // 1. Read and Parse Spec Properties
    let content = fs::read_to_string(clm_file)
        .map_err(|e| format!("Failed to read equivalence spec: {}", e))?;
    let parsed: serde_yaml::Value = serde_yaml::from_str(&content)
        .map_err(|e| format!("Failed to parse equivalence YAML: {}", e))?;

    let clm_val = parsed.get("clm").unwrap_or(&parsed);
    let process_val = clm_val.get("concrete_impl")
        .and_then(|v| v.get("process"))
        .or_else(|| clm_val.get("process"))
        .ok_or_else(|| "No process block found in equivalence spec".to_string())?;

    let ref_pcard = process_val.get("process_ref")
        .and_then(|v| v.as_str())
        .ok_or_else(|| "Missing process_ref in spec".to_string())?;

    let cand_pcard = process_val.get("process_cand")
        .and_then(|v| v.as_str())
        .ok_or_else(|| "Missing process_cand in spec".to_string())?;

    let relation_type = process_val.get("relation_type")
        .and_then(|v| v.as_str())
        .unwrap_or("identity");

    let domain_val = process_val.get("domain")
        .ok_or_else(|| "Missing domain in spec".to_string())?;

    let mut inputs = Vec::new();
    if let Some(arr) = domain_val.as_sequence() {
        for item in arr {
            if let Some(s) = item.as_str() {
                inputs.push(s.to_string());
            } else if let Some(n) = item.as_i64() {
                inputs.push(n.to_string());
            } else if let Some(b) = item.as_bool() {
                inputs.push(b.to_string());
            }
        }
    }

    if inputs.is_empty() {
        return Err("Equivalence domain cannot be empty".to_string());
    }

    // Parse optional relation_options (backward-compatible: all default to false/None)
    let mut opts = RelationOptions::default();
    if let Some(ro) = process_val.get("relation_options") {
        opts.normalize_newlines = ro.get("normalize_newlines").and_then(|v| v.as_bool()).unwrap_or(false);
        opts.trim_whitespace = ro.get("trim_whitespace").and_then(|v| v.as_bool()).unwrap_or(false);
        opts.ignore_ansi = ro.get("ignore_ansi").and_then(|v| v.as_bool()).unwrap_or(false);
        opts.ignore_case = ro.get("ignore_case").and_then(|v| v.as_bool()).unwrap_or(false);
        opts.stop_on_first_mismatch = ro.get("stop_on_first_mismatch").and_then(|v| v.as_bool()).unwrap_or(false);
        opts.required_degree = ro.get("required_degree").and_then(|v| v.as_f64());
    }

    // Resolve absolute path to process files
    let ref_path = if Path::new(ref_pcard).is_absolute() {
        PathBuf::from(ref_pcard)
    } else {
        Path::new(clm_root).join(ref_pcard)
    };
    let cand_path = if Path::new(cand_pcard).is_absolute() {
        PathBuf::from(cand_pcard)
    } else {
        Path::new(clm_root).join(cand_pcard)
    };

    let ref_hash = crate::fs_utils::compute_stripped_file_hash(&ref_path)
        .unwrap_or_else(|_| format!("{}unknown_ref", crate::hash::DEFAULT_HASH_PREFIX));
    let cand_hash = crate::fs_utils::compute_stripped_file_hash(&cand_path)
        .unwrap_or_else(|_| format!("{}unknown_cand", crate::hash::DEFAULT_HASH_PREFIX));

    let domain_json = serde_json::to_string(&inputs).unwrap_or_default();
    let domain_hash = crate::fs_utils::compute_content_hash(&domain_json);

    let mut matched = 0;
    let mut total = 0;
    let mut witness_lines = Vec::new();
    let mut ref_total_ms = 0;
    let mut cand_total_ms = 0;

    let mut thread_results = Vec::new();
    std::thread::scope(|s| {
        let mut handles = Vec::new();
        for input in &inputs {
            let handle_ref = s.spawn(|| {
                let start = std::time::Instant::now();
                let res = execute_pcard(clm_root, ref_pcard, input);
                let elapsed = start.elapsed().as_millis() as u64;
                (res, elapsed)
            });
            let handle_cand = s.spawn(|| {
                let start = std::time::Instant::now();
                let res = execute_pcard(clm_root, cand_pcard, input);
                let elapsed = start.elapsed().as_millis() as u64;
                (res, elapsed)
            });
            handles.push((input, handle_ref, handle_cand));
        }

        for (input, h_ref, h_cand) in handles {
            let (out_ref_res, ref_elapsed) = h_ref.join().unwrap();
            let (out_cand_res, cand_elapsed) = h_cand.join().unwrap();
            thread_results.push((input, out_ref_res, ref_elapsed, out_cand_res, cand_elapsed));
        }
    });

    for (input, out_ref_res, ref_elapsed, out_cand_res, cand_elapsed) in thread_results {
        total += 1;
        ref_total_ms += ref_elapsed;
        cand_total_ms += cand_elapsed;

        let (out_ref, ref_err) = match out_ref_res {
            Ok(s) => (s, None),
            Err(e) => (String::new(), Some(e)),
        };
        let (out_cand, cand_err) = match out_cand_res {
            Ok(s) => (s, None),
            Err(e) => (String::new(), Some(e)),
        };

        // Normalize outputs before comparison
        let norm_ref = normalize_output(&out_ref, &opts);
        let norm_cand = normalize_output(&out_cand, &opts);

        let mut result = "fail";
        let mut comp_made = false;

        // If both executed successfully, perform comparison
        if ref_err.is_none() && cand_err.is_none() {
            comp_made = true;
            match relation_type {
                "identity" => {
                    if norm_ref == norm_cand {
                        result = "pass";
                        matched += 1;
                    }
                }
                "structural" => {
                    if let (Ok(ref_v), Ok(cand_v)) = (parse_json_or_yaml(&norm_ref), parse_json_or_yaml(&norm_cand)) {
                        let clean_ref = canonical_value(&clean_data(&ref_v));
                        let clean_cand = canonical_value(&clean_data(&cand_v));
                        if clean_ref == clean_cand {
                            result = "pass";
                            matched += 1;
                        }
                    }
                }
                "numerical" => {
                    if compare_numerical(&norm_ref, &norm_cand) {
                        result = "pass";
                        matched += 1;
                    }
                }
                "hash_invariant" => {
                    if norm_ref == norm_cand {
                        result = "pass";
                        matched += 1;
                    } else {
                        if let Ok(eq) = check_content_equivalent(clm_root, &norm_ref, &norm_cand)
                            && eq {
                                result = "pass";
                                matched += 1;
                            }
                    }
                }
                "invariant" => {
                    if norm_ref == norm_cand {
                        result = "pass";
                        matched += 1;
                    }
                }
                _ => return Err(format!("Unknown comparison relation: {}", relation_type)),
            }
        } else {
            // Execution failure is a first-class outcome, distinct from a
            // genuine output mismatch (WI-2). It is not counted as matched.
            result = "error";
        }

        // Build witness JSON with richer fields
        let ref_trunc = if out_ref.len() > 64 { &out_ref[0..64] } else { &out_ref };
        let cand_trunc = if out_cand.len() > 64 { &out_cand[0..64] } else { &out_cand };

        let mut witness_parts = vec![
            format!("\"input\":\"{}\"", input.replace('"', "\\\"")),
            format!("\"ref\":\"{}\"", ref_trunc.trim().replace('"', "\\\"").replace('\n', " ")),
            format!("\"cand\":\"{}\"", cand_trunc.trim().replace('"', "\\\"").replace('\n', " ")),
            format!("\"result\":\"{}\"", result),
        ];
        if let Some(ref_e) = &ref_err {
            witness_parts.push(format!("\"ref_error\":\"{}\"", ref_e.replace('"', "\\\"").replace('\n', " ")));
        }
        if let Some(cand_e) = &cand_err {
            witness_parts.push(format!("\"cand_error\":\"{}\"", cand_e.replace('"', "\\\"").replace('\n', " ")));
        }
        if result == "fail" && comp_made
            && let Some((idx, ac, bc)) = find_divergence(&norm_ref, &norm_cand) {
                witness_parts.push(format!("\"diff_at\":{},\"expected_char\":\"{}\",\"actual_char\":\"{}\"", idx, ac, bc));
            }

        let witness = format!("{{{}}}", witness_parts.join(","));
        witness_lines.push(witness);

        // Optional early stop on first mismatch
        if opts.stop_on_first_mismatch && result == "fail" {
            break;
        }
    }

    let degree = (matched as f64) / (total as f64);
    let timestamp = chrono::Utc::now().format("%Y-%m-%dT%H:%M:%SZ").to_string();
    let agent_did = std::env::var("CLM_AGENT_DID").unwrap_or_else(|_| "did:clm:bootstrap".to_string());

    let vcard_yaml = format!(
        "clm:\n  equivalence_vcard:\n    process_ref_hash: \"{}\"\n    process_cand_hash: \"{}\"\n    domain_hash: \"{}\"\n    relation_type: \"{}\"\n    degree_of_equivalence: {:.4}\n    witness_trail: [{}]\n    performance_differential:\n      ref_total_ms: {}\n      cand_total_ms: {}\n    timestamp: \"{}\"\n    agent_did: \"{}\"",
        ref_hash,
        cand_hash,
        domain_hash,
        relation_type,
        degree,
        witness_lines.join(","),
        ref_total_ms,
        cand_total_ms,
        timestamp,
        agent_did
    );

    let vcard_hash = crate::fs_utils::compute_content_hash(&vcard_yaml);

    // Save to Database
    if let Ok(conn) = crate::db::get_connection("content_memory", clm_root) {
        let _ = conn.execute(
            "INSERT OR IGNORE INTO card (hash, content, g_time) VALUES (?, ?, ?);",
            rusqlite::params![vcard_hash, vcard_yaml, timestamp],
        );
        let handle_name = format!("vcard:equivalence:{}", vcard_hash);
        let _ = conn.execute(
            "INSERT OR IGNORE INTO handle_registry (handle, current_hash, created_at, updated_at) VALUES (?, ?, ?, ?);",
            rusqlite::params![handle_name, vcard_hash, timestamp, timestamp],
        );
    }

    // Cache write-through
    let handle_name = format!("vcard:equivalence:{}", vcard_hash);
    crate::cache::setter_mcard_cache_handle(&handle_name, &vcard_hash);
    crate::cache::setter_mcard_cache(&vcard_hash, &vcard_yaml);

    // Save to execution_logs dir unless Zero-FS is true
    let zero_fs = crate::utils::is_zero_fs();
    if !zero_fs {
        let logs_dir = Path::new(clm_root).join("data").join("execution_logs");
        let _ = fs::create_dir_all(&logs_dir);
        let file_path = logs_dir.join(format!("equiv_{}.yaml", &vcard_hash[0..8]));
        let _ = fs::write(&file_path, &vcard_yaml);
    }

    // Output VCard hash
    let threshold = opts.required_degree.unwrap_or(1.0);
    let exit_code = if degree >= threshold { 0 } else { 1 };
    Ok((exit_code, format!("{}\n", vcard_hash)))
}

/// Extract an inline shell `script:` from a PCard YAML, if the process is a
/// trivial `type: shell` card with an inline script (no `script_handle`).
/// Returns None for polyglot, handle-based, or gated PCards so they take the
/// full evaluate path.
fn extract_inline_shell_script(clm_root: &str, clm_file: &str) -> Option<String> {
    let path = if std::path::Path::new(clm_file).is_absolute() {
        std::path::PathBuf::from(clm_file)
    } else {
        std::path::Path::new(clm_root).join(clm_file)
    };
    let content = std::fs::read_to_string(&path).ok()?;
    let parsed: serde_yaml::Value = serde_yaml::from_str(&content).ok()?;

    let clm_val = parsed.get("clm").unwrap_or(&parsed);
    let process = clm_val
        .get("concrete_impl")
        .and_then(|v| v.get("process"))
        .or_else(|| clm_val.get("process"))?;

    // Process may be a mapping or a sequence of runtime impls; handle the shell case.
    let shell_map = match process {
        serde_yaml::Value::Mapping(_) => Some(process),
        serde_yaml::Value::Sequence(seq) => seq.iter().find(|v| {
            v.get("type").and_then(|t| t.as_str()) == Some("shell")
        }),
        _ => None,
    }?;

    let ptype = shell_map.get("type").and_then(|t| t.as_str()).unwrap_or("shell");
    if ptype != "shell" {
        return None;
    }
    // Only fast-path inline scripts; defer handle-based scripts to the full evaluator.
    if shell_map.get("script_handle").and_then(|v| v.as_str()).is_some() {
        return None;
    }
    let script = shell_map.get("script").and_then(|v| v.as_str())?;
    Some(script.to_string())
}

fn execute_pcard(clm_root: &str, clm_file: &str, payload: &str) -> Result<String, String> {
    // -- Zero-fork fast path: run trivial inline shell PCards directly --------
    // Avoids the heavy (and Windows-fragile) bash bootstrap for simple
    // `type: shell` cards, eliminating ~18s/run overhead and the
    // "clmEval: -c: line N: syntax error" failure on Git Bash/MSYS2.
    if let Some(script) = extract_inline_shell_script(clm_root, clm_file) {
        let bash = crate::fs_utils::get_bash_command();
        let mut cmd = std::process::Command::new(bash);
        cmd.current_dir(clm_root)
            .arg("-c")
            .arg(&script)
            .arg("clm_pcard") // $0
            .arg(payload)     // $1
            .env("CLM_PAYLOAD", payload)
            .env("CLM_NESTED_EXEC", "1")
            .stdin(std::process::Stdio::null());

        let output = cmd.output()
            .map_err(|e| format!("Failed to execute inline shell PCard: {}", e))?;
        if output.status.success() {
            let stdout_str = String::from_utf8_lossy(&output.stdout).into_owned();
            return Ok(stdout_str.trim().to_string());
        } else {
            let code = output.status.code().unwrap_or(-1);
            let stderr_str = String::from_utf8_lossy(&output.stderr).trim().to_string();
            let msg = if stderr_str.is_empty() {
                format!("inline shell PCard exited with code {}", code)
            } else {
                format!("exit {}: {}", code, stderr_str)
            };
            return Err(msg);
        }
    }

    let self_exe = std::env::current_exe()
        .map_err(|e| format!("Failed to get current executable path: {}", e))?;
    
    let mut cmd = std::process::Command::new(self_exe);
    cmd.current_dir(clm_root)
        .arg("evaluate")
        .arg(clm_file)
        .arg(payload)
        .arg("--quiet");
    
    cmd.envs(std::env::vars());
    cmd.env("CLM_PAYLOAD", payload);
    cmd.env("CLM_NESTED_EXEC", "1");
    cmd.env("CLM_ZERO_FS", "true"); // Nested runs should always run in memory-only mode to prevent lock collisions on DB
    cmd.env("CLM_SKIP_GRAMMAR", "1"); // Skip grammar checks for target process cards
    cmd.stdin(std::process::Stdio::null()); // explicitly close stdin to prevent hanging interactive shells

    
    let output = cmd.output()
        .map_err(|e| format!("Failed to execute process: {}", e))?;
    
    if output.status.success() {
        let stdout_str = String::from_utf8_lossy(&output.stdout).into_owned();
        let out = if let Some(idx) = stdout_str.find("[POST] V_post:") {
            stdout_str[..idx].to_string()
        } else {
            stdout_str
        };
        let log_prefixes: &[&str] = &[
            "[clmEval", "[VERIFY]", "[VCARD POST]", "[MCARD_KERNEL]",
            "[COMPOSE]", "[DEBUG", "[GATE]", "[TYPE", "[POST]", "[PAL]",
            "PASS:", "━━", "clmEval —", "Evaluating:", "SCRIPT CONTENT",
        ];
        let filtered: Vec<&str> = out.lines()
            .filter(|line| {
                let trimmed = line.trim();
                !trimmed.starts_with('[') && !trimmed.starts_with("SCRIPT CONTENT") && !log_prefixes.iter().any(|prefix| trimmed.starts_with(prefix))
            })
            .collect();
        let result = filtered.join("\n");
        Ok(result.trim().to_string())
    } else {
        Err(String::from_utf8_lossy(&output.stderr).to_string())
    }
}



fn parse_json_or_yaml(s: &str) -> Result<Value, String> {
    let trimmed = s.trim();
    if trimmed.is_empty() {
        return Ok(Value::Null);
    }
    if let Ok(val) = serde_json::from_str::<Value>(trimmed) {
        return Ok(val);
    }
    if let Ok(yaml_val) = serde_yaml::from_str::<serde_yaml::Value>(trimmed) {
        let json_str = serde_json::to_string(&yaml_val)
            .map_err(|e| format!("Failed to serialize YAML value: {}", e))?;
        let val = serde_json::from_str::<Value>(&json_str)
            .map_err(|e| format!("Failed to parse deserialized YAML: {}", e))?;
        return Ok(val);
    }
    Err("Not valid JSON or YAML".to_string())
}

fn clean_data(val: &Value) -> Value {
    match val {
        Value::Object(obj) => {
            let mut cleaned = serde_json::Map::new();
            for (k, v) in obj {
                if k == "timestamp"
                    || k == "g_time"
                    || k == "run_id"
                    || k == "previous_hash"
                    || k == "changed_at"
                    || k == "created_at"
                    || k == "updated_at"
                    || k == "witness_trail"
                    || k == "performance_differential"
                    || k == "agent_did"
                    || k == "uuid"
                    || k == "g_time_note"
                    || k == "genesis_hash"
                    || k == "genesis_note"
                {
                    continue;
                }
                cleaned.insert(k.clone(), clean_data(v));
            }
            Value::Object(cleaned)
        }
        Value::Array(arr) => {
            let mut cleaned_list = Vec::new();
            for item in arr {
                cleaned_list.push(clean_data(item));
            }
            cleaned_list.sort_by_key(|v| serde_json::to_string(v).unwrap_or_default());
            Value::Array(cleaned_list)
        }
        _ => val.clone(),
    }
}

fn canonical_value(val: &Value) -> Value {
    match val {
        Value::Object(obj) => {
            let mut sorted = std::collections::BTreeMap::new();
            for (k, v) in obj {
                sorted.insert(k.clone(), canonical_value(v));
            }
            let mut res = serde_json::Map::new();
            for (k, v) in sorted {
                res.insert(k, v);
            }
            Value::Object(res)
        }
        Value::Array(arr) => {
            let mut res = Vec::new();
            for item in arr {
                res.push(canonical_value(item));
            }
            Value::Array(res)
        }
        _ => val.clone(),
    }
}

fn compare_numerical(s_a: &str, s_b: &str) -> bool {
    let val_a = s_a.trim().parse::<f64>();
    let val_b = s_b.trim().parse::<f64>();
    match (val_a, val_b) {
        (Ok(a), Ok(b)) => (a - b).abs() <= 1e-6,
        _ => false,
    }
}

fn check_content_equivalent(clm_root: &str, hash_a: &str, hash_b: &str) -> Result<bool, String> {
    if hash_a == hash_b {
        return Ok(true);
    }
    // Temporarily bypass sandbox so content lookups hit the real on-disk DB
    // rather than an ephemeral in-memory sandbox DB.
    let was_sandbox = crate::db::TEST_SANDBOX_ACTIVE.with(|a| a.get());
    let prev_id = crate::db::TEST_SANDBOX_DB_ID.with(|id| id.borrow().clone());
    let prev_depth = crate::db::thread_local_depth_get();
    crate::db::TEST_SANDBOX_ACTIVE.with(|a| a.set(false));
    crate::db::TEST_SANDBOX_DB_ID.with(|id| *id.borrow_mut() = None);
    crate::db::set_sandbox_depth(0);

    let result = (|| {
        let content_a = crate::db::get_card_content("content_memory", clm_root, hash_a)
            .map_err(|e| e.to_string())?;
        let content_b = crate::db::get_card_content("content_memory", clm_root, hash_b)
            .map_err(|e| e.to_string())?;
        Ok(match (content_a, content_b) {
            (Some(a), Some(b)) => a == b,
            _ => false,
        })
    })();

    crate::db::TEST_SANDBOX_ACTIVE.with(|a| a.set(was_sandbox));
    crate::db::TEST_SANDBOX_DB_ID.with(|id| *id.borrow_mut() = prev_id);
    crate::db::set_sandbox_depth(prev_depth);

    result
}