instinct-cli 0.3.7

Shared local memory for coding agents. Save a fix once and find it again in a new session or another connected tool.
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
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
// generated by hxc 1.0.0-acgt-compiler from src/check/kernel.hlx -- do not edit by hand

#[cfg(windows)]
#[allow(dead_code)]
const SYS_CURL: &str = "C:\\Windows\\System32\\curl.exe";
#[cfg(not(windows))]
#[allow(dead_code)]
const SYS_CURL: &str = "curl";
#[allow(dead_code)]
fn _helix_allowed(var: &str, what: &str) -> bool {
    let ok = std::env::var(var).map(|v| v == "1").unwrap_or(false);
    if !ok {
        eprintln!("helix: BLOCKED {} -- permission denied (set {}=1 to allow; `hxc run` allows by default, a `hxc build`/`export-rust` binary does not)", what, var);
    }
    ok
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_exec(mut c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
        std::process::exit(13);
    }
    use std::os::windows::process::CommandExt;
    c.creation_flags(0x08000000);
    c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_exec(c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_RUN", "process execution") {
        std::process::exit(13);
    }
    c
}
#[cfg(windows)]
#[allow(dead_code)]
fn _nw_net(mut c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
        std::process::exit(13);
    }
    use std::os::windows::process::CommandExt;
    c.creation_flags(0x08000000);
    c
}
#[cfg(not(windows))]
#[allow(dead_code)]
fn _nw_net(c: std::process::Command) -> std::process::Command {
    if !_helix_allowed("HELIX_ALLOW_NET", "network access") {
        std::process::exit(13);
    }
    c
}

// hlx:2
// Scoped deterministic checks, version one: compile the full definition, validate facts, evaluate within a budget.
// hlx:3
// Pure: bytes in, JSON text out. No I/O, no environment, no clock, no globals.
// hlx:4
use super::json::{self, J};
// hlx:5
#[path = "rational.rs"]
// hlx:6
mod rational;
// hlx:7
use std::collections::{BTreeMap, BTreeSet};
// hlx:8
pub const MAX_CLAIM_DEPTH: usize = 32;
// hlx:9
pub const MAX_CLAIM_NODES: usize = 4096;
// hlx:10
pub const MAX_FACT_ENTRIES: usize = 10000;
// hlx:11
pub const MAX_STEPS: u64 = 100000;
// hlx:12
pub const MAX_OUTPUT_BYTES: usize = 65536;
// hlx:13
const MAX_LISTED: usize = 128;
// hlx:14
const MAX_CLAIM_TEXT: usize = 16384;
// hlx:15
const DIMS: [&str; 5] = ["project", "persona", "harness", "version", "machine"];
// hlx:16
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
// hlx:17
pub enum Mode {
    // hlx:18
    Validate,
    // hlx:19
    Run,
    // hlx:20
    Explain,
    // hlx:21
}
// hlx:22
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
// hlx:23
enum Ty {
    // hlx:24
    Str,
    // hlx:25
    Bool,
    // hlx:26
    Int,
    // hlx:27
    Rational,
    // hlx:28
    Set,
    // hlx:29
}
// hlx:30
impl Ty {
    // hlx:31
    fn name(self) -> &'static str {
        // hlx:32
        match self {
            // hlx:33
            Ty::Str => "string",
            // hlx:34
            Ty::Bool => "bool",
            // hlx:35
            Ty::Int => "int",
            // hlx:36
            Ty::Rational => "rational",
            // hlx:37
            Ty::Set => "set<string>",
            // hlx:38
        }
        // hlx:39
    }
    // hlx:40
}
// hlx:41
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
// hlx:42
enum Cmp {
    // hlx:43
    Subset,
    // hlx:44
    Disjoint,
    // hlx:45
    Contains,
    // hlx:46
    Eq,
    // hlx:47
    Lt,
    // hlx:48
    Lte,
    // hlx:49
}
// hlx:50
impl Cmp {
    // hlx:51
    fn name(self) -> &'static str {
        // hlx:52
        match self {
            // hlx:53
            Cmp::Subset => "subset",
            // hlx:54
            Cmp::Disjoint => "disjoint",
            // hlx:55
            Cmp::Contains => "contains",
            // hlx:56
            Cmp::Eq => "eq",
            // hlx:57
            Cmp::Lt => "lt",
            // hlx:58
            Cmp::Lte => "lte",
            // hlx:59
        }
        // hlx:60
    }
    // hlx:61
}
// hlx:62
#[derive(Debug)]
// hlx:63
enum Node {
    // hlx:64
    Cmp(Cmp, String, String),
    // hlx:65
    All(Vec<Node>),
    // hlx:66
    Any(Vec<Node>),
    // hlx:67
    Not(Box<Node>),
    // hlx:68
}
// hlx:69
#[derive(Debug)]
// hlx:70
enum Val {
    // hlx:71
    S(String),
    // hlx:72
    B(bool),
    // hlx:73
    I(i128),
    // hlx:74
    Q(rational::Rational),
    // hlx:75
    Set(Vec<String>),
    // hlx:76
}
// hlx:77
struct Def {
    // hlx:78
    id: String,
    // hlx:79
    revision: u32,
    // hlx:80
    inputs: BTreeMap<String, Ty>,
    // hlx:81
    claim: Node,
    // hlx:82
    conditions: Vec<String>,
    // hlx:83
    scope: Vec<(&'static str, String)>,
    // hlx:84
}
// hlx:85
type R<T> = Result<T, String>;
// hlx:86
fn clip(s: &str) -> String {
    // hlx:87
    let mut out = String::new();
    // hlx:88
    for ch in s.chars().take(48) {
        // hlx:89
        if ch.is_control()
            || matches!(ch as u32, 0x00ad | 0x061c | 0x180e | 0x200b..=0x200f | 0x2028..=0x202e | 0x2060..=0x206f | 0xfeff | 0xfff9..=0xfffb | 0x1bca0..=0x1bca3 | 0x1d173..=0x1d17a | 0xe0001 | 0xe0020..=0xe007f)
        {
            // hlx:90
            out.push('?');
        // hlx:91
        } else {
            // hlx:92
            out.push(ch);
            // hlx:93
        }
        // hlx:94
    }
    // hlx:95
    if s.chars().count() > 48 {
        // hlx:96
        out.push_str("...");
        // hlx:97
    }
    // hlx:98
    out
    // hlx:99
}
// hlx:100
fn is_ident(s: &str) -> bool {
    // hlx:101
    let b = s.as_bytes();
    // hlx:102
    !b.is_empty()
// hlx:103
&& b.len() <= 128
// hlx:104
&& b[0].is_ascii_alphabetic()
// hlx:105
&& b.iter().all(|c| c.is_ascii_alphanumeric() || matches!(c, b'.' | b'_' | b'-'))
    // hlx:106
}
// hlx:107
fn obj<'a>(j: &'a J, what: &str) -> R<&'a [(String, J)]> {
    // hlx:108
    match j {
        // hlx:109
        J::Obj(o) => Ok(o),
        // hlx:110
        _ => Err(format!("malformed_definition: {what} must be an object")),
        // hlx:111
    }
    // hlx:112
}
// hlx:113
fn field<'a>(o: &'a [(String, J)], k: &str) -> Option<&'a J> {
    // hlx:114
    o.iter().find(|(key, _)| key == k).map(|(_, v)| v)
    // hlx:115
}
// hlx:116
fn only_keys(o: &[(String, J)], allowed: &[&str], what: &str) -> R<()> {
    // hlx:117
    for (k, _) in o {
        // hlx:118
        if !allowed.contains(&k.as_str()) {
            // hlx:119
            return Err(format!(
                "unknown_field: {what} has unknown key \"{}\"",
                clip(k)
            ));
            // hlx:120
        }
        // hlx:121
    }
    // hlx:122
    Ok(())
    // hlx:123
}
// hlx:124
fn need<'a>(o: &'a [(String, J)], k: &str, what: &str) -> R<&'a J> {
    // hlx:125
    field(o, k).ok_or_else(|| format!("malformed_definition: {what} requires \"{k}\""))
    // hlx:126
}
// hlx:127
fn text<'a>(j: &'a J, what: &str) -> R<&'a str> {
    // hlx:128
    match j {
        // hlx:129
        J::Str(s) => Ok(s),
        // hlx:130
        _ => Err(format!("malformed_definition: {what} must be a string")),
        // hlx:131
    }
    // hlx:132
}
// hlx:133
fn parse_scope(j: &J, what: &str) -> R<Vec<(&'static str, String)>> {
    // hlx:134
    let o = obj(j, what)?;
    // hlx:135
    only_keys(o, &DIMS, what)?;
    // hlx:136
    let mut out = Vec::new();
    // hlx:137
    for dim in DIMS {
        // hlx:138
        if let Some(v) = field(o, dim) {
            // hlx:139
            let s = text(v, what)?;
            // hlx:140
            if s.is_empty() || s == "*" {
                // hlx:141
                return Err(format!(
                    "invalid_scope: {what}.{dim} must be a nonempty literal, not \"*\""
                ));
                // hlx:142
            }
            // hlx:143
            out.push((dim, s.to_string()));
            // hlx:144
        }
        // hlx:145
    }
    // hlx:146
    Ok(out)
    // hlx:147
}
// hlx:148
fn canonical_int(s: &str) -> Option<i128> {
    // hlx:149
    let digits = s.strip_prefix('-').unwrap_or(s);
    // hlx:150
    if digits.is_empty() || !digits.bytes().all(|c| c.is_ascii_digit()) {
        // hlx:151
        return None;
        // hlx:152
    }
    // hlx:153
    if digits.len() > 1 && digits.starts_with('0') {
        // hlx:154
        return None;
        // hlx:155
    }
    // hlx:156
    if s.starts_with('-') && digits == "0" {
        // hlx:157
        return None;
        // hlx:158
    }
    // hlx:159
    s.parse::<i128>().ok()
    // hlx:160
}
// hlx:161
fn parse_ty(s: &str) -> R<Ty> {
    // hlx:162
    match s {
        // hlx:163
        "string" => Ok(Ty::Str),
        // hlx:164
        "bool" => Ok(Ty::Bool),
        // hlx:165
        "int" => Ok(Ty::Int),
        // hlx:166
        "set<string>" => Ok(Ty::Set),
        // hlx:167
        "rational" => Ok(Ty::Rational),
        // hlx:168
        "graph" => Err(format!(
            "unsupported_feature: type \"{s}\" is not supported in this version"
        )),
        // hlx:169
        _ => Err(format!("invalid_type: unknown type \"{}\"", clip(s))),
        // hlx:170
    }
    // hlx:171
}
// hlx:172
fn operand(j: &J, inputs: &BTreeMap<String, Ty>, nodes: &mut usize) -> R<(String, Ty)> {
    // hlx:173
    *nodes += 1;
    // hlx:174
    if *nodes > MAX_CLAIM_NODES {
        // hlx:175
        return Err("limit_exceeded: claim has more than 4096 nodes".to_string());
        // hlx:176
    }
    // hlx:177
    let o = obj(j, "operand")?;
    // hlx:178
    only_keys(o, &["fact"], "operand")?;
    // hlx:179
    let name = text(need(o, "fact", "operand")?, "operand fact")?;
    // hlx:180
    match inputs.get(name) {
        // hlx:181
        Some(t) => Ok((name.to_string(), *t)),
        // hlx:182
        None => Err(format!(
            "unknown_reference: fact \"{}\" is not declared in inputs",
            clip(name)
        )),
        // hlx:183
    }
    // hlx:184
}
// hlx:185
fn compile(j: &J, depth: usize, inputs: &BTreeMap<String, Ty>, nodes: &mut usize) -> R<Node> {
    // hlx:186
    if depth > MAX_CLAIM_DEPTH {
        // hlx:187
        return Err("limit_exceeded: claim is nested deeper than 32".to_string());
        // hlx:188
    }
    // hlx:189
    *nodes += 1;
    // hlx:190
    if *nodes > MAX_CLAIM_NODES {
        // hlx:191
        return Err("limit_exceeded: claim has more than 4096 nodes".to_string());
        // hlx:192
    }
    // hlx:193
    let o = obj(j, "claim expression")?;
    // hlx:194
    let op = text(need(o, "op", "claim expression")?, "op")?;
    // hlx:195
    let cmp = match op {
        // hlx:196
        "subset" => Cmp::Subset,
        // hlx:197
        "disjoint" => Cmp::Disjoint,
        // hlx:198
        "contains" => Cmp::Contains,
        // hlx:199
        "eq" => Cmp::Eq,
        // hlx:200
        "lt" => Cmp::Lt,
        // hlx:201
        "lte" => Cmp::Lte,
        // hlx:202
        "all" | "any" => {
            // hlx:203
            only_keys(o, &["op", "args"], op)?;
            // hlx:204
            let args = match need(o, "args", op)? {
                // hlx:205
                J::Arr(a) if !a.is_empty() => a,
                // hlx:206
                _ => {
                    return Err(format!(
                        "malformed_definition: {op} args must be a nonempty array"
                    ))
                }
                // hlx:207
            };
            // hlx:208
            let mut kids = Vec::new();
            // hlx:209
            for a in args {
                // hlx:210
                kids.push(compile(a, depth + 1, inputs, nodes)?);
                // hlx:211
            }
            // hlx:212
            return Ok(if op == "all" {
                Node::All(kids)
            } else {
                Node::Any(kids)
            });
            // hlx:213
        }
        // hlx:214
        "not" => {
            // hlx:215
            only_keys(o, &["op", "arg"], op)?;
            // hlx:216
            return Ok(Node::Not(Box::new(compile(
                need(o, "arg", op)?,
                depth + 1,
                inputs,
                nodes,
            )?)));
            // hlx:217
        }
        // hlx:218
        _ => return Err(format!("unknown_operator: \"{}\"", clip(op))),
        // hlx:219
    };
    // hlx:220
    only_keys(o, &["op", "left", "right"], op)?;
    // hlx:221
    let (l, lt) = operand(need(o, "left", op)?, inputs, nodes)?;
    // hlx:222
    let (r, rt) = operand(need(o, "right", op)?, inputs, nodes)?;
    // hlx:223
    let ok = match cmp {
        // hlx:224
        Cmp::Subset | Cmp::Disjoint => lt == Ty::Set && rt == Ty::Set,
        // hlx:225
        Cmp::Contains => lt == Ty::Set && rt == Ty::Str,
        // hlx:226
        Cmp::Eq => lt == rt,
        // hlx:227
        Cmp::Lt | Cmp::Lte => lt == rt && matches!(lt, Ty::Int | Ty::Rational),
        // hlx:228
    };
    // hlx:229
    if !ok {
        // hlx:230
        return Err(format!(
            "type_mismatch: {} cannot compare {} with {}",
            op,
            lt.name(),
            rt.name()
        ));
        // hlx:231
    }
    // hlx:232
    Ok(Node::Cmp(cmp, l, r))
    // hlx:233
}
// hlx:234
fn compile_def(j: &J) -> R<Def> {
    // hlx:235
    let o = obj(j, "check")?;
    // hlx:236
    only_keys(
        o,
        &[
            "schema",
            "id",
            "revision",
            "inputs",
            "claim",
            "required_conditions",
            "scope",
        ],
        "check",
    )?;
    // hlx:237
    let schema = text(need(o, "schema", "check")?, "schema")?;
    // hlx:238
    if schema != "instinct.check/v1" {
        // hlx:239
        return Err(format!("unsupported_schema: \"{}\"", clip(schema)));
        // hlx:240
    }
    // hlx:241
    let id = text(need(o, "id", "check")?, "id")?;
    // hlx:242
    if !is_ident(id) {
        // hlx:243
        return Err("malformed_definition: id must be 1-128 ASCII letters, digits or ._- starting with a letter".to_string());
        // hlx:244
    }
    // hlx:245
    let revision = match need(o, "revision", "check")? {
        // hlx:246
        J::Num(n) => match n.parse::<u32>() {
            // hlx:247
            Ok(v)
                if (1..=2147483647).contains(&v)
                    && n.bytes().all(|c| c.is_ascii_digit())
                    && !n.starts_with('0') =>
            {
                v
            }
            // hlx:248
            _ => {
                return Err(
                    "invalid_number: revision must be an integer literal from 1 to 2147483647"
                        .to_string(),
                )
            }
            // hlx:249
        },
        // hlx:250
        _ => {
            return Err(
                "invalid_number: revision must be an integer literal, not a string".to_string(),
            )
        }
        // hlx:251
    };
    // hlx:252
    let mut inputs = BTreeMap::new();
    // hlx:253
    for (k, v) in obj(need(o, "inputs", "check")?, "inputs")? {
        // hlx:254
        if !is_ident(k) {
            // hlx:255
            return Err(format!(
                "malformed_definition: input name \"{}\" is not a valid name",
                clip(k)
            ));
            // hlx:256
        }
        // hlx:257
        inputs.insert(k.clone(), parse_ty(text(v, "input type")?)?);
        // hlx:258
    }
    // hlx:259
    let mut conditions = Vec::new();
    // hlx:260
    if let Some(rc) = field(o, "required_conditions") {
        // hlx:261
        match rc {
            // hlx:262
            J::Arr(a) => {
                // hlx:263
                for c in a {
                    // hlx:264
                    let name = text(c, "required condition")?;
                    // hlx:265
                    if !is_ident(name) {
                        // hlx:266
                        return Err(format!(
                            "malformed_definition: condition name \"{}\" is not a valid name",
                            clip(name)
                        ));
                        // hlx:267
                    }
                    // hlx:268
                    if conditions.iter().any(|x: &String| x == name) {
                        // hlx:269
                        return Err(format!(
                            "malformed_definition: duplicate required condition \"{}\"",
                            clip(name)
                        ));
                        // hlx:270
                    }
                    // hlx:271
                    conditions.push(name.to_string());
                    // hlx:272
                }
                // hlx:273
            }
            // hlx:274
            _ => {
                return Err("malformed_definition: required_conditions must be an array".to_string())
            }
            // hlx:275
        }
        // hlx:276
    }
    // hlx:277
    let scope = match field(o, "scope") {
        // hlx:278
        Some(s) => parse_scope(s, "check scope")?,
        // hlx:279
        None => Vec::new(),
        // hlx:280
    };
    // hlx:281
    let mut nodes = 0usize;
    // hlx:282
    let claim = compile(need(o, "claim", "check")?, 1, &inputs, &mut nodes)?;
    // hlx:283
    Ok(Def {
        id: id.to_string(),
        revision,
        inputs,
        claim,
        conditions,
        scope,
    })
    // hlx:284
}
// hlx:285
/// Everything the caller supplied, validated against the definition.
// hlx:286
struct Request {
    // hlx:287
    facts: BTreeMap<String, Val>,
    // hlx:288
    entries: usize,
    // hlx:289
    conditions: BTreeMap<String, bool>,
    // hlx:290
    scope: Vec<(&'static str, String)>,
    // hlx:291
}
// hlx:292
fn read_request(top: &[(String, J)], def: &Def) -> R<Request> {
    // hlx:293
    let mut facts = BTreeMap::new();
    // hlx:294
    let mut entries = 0usize;
    // hlx:295
    if let Some(f) = field(top, "facts") {
        // hlx:296
        for (k, v) in obj(f, "facts")? {
            // hlx:297
            let ty = match def.inputs.get(k) {
                // hlx:298
                Some(t) => *t,
                // hlx:299
                None => {
                    return Err(format!(
                        "unknown_fact: \"{}\" is not declared in inputs",
                        clip(k)
                    ))
                }
                // hlx:300
            };
            // hlx:301
            entries += 1;
            // hlx:302
            let val = match (ty, v) {
// hlx:303
(Ty::Str, J::Str(s)) => Val::S(s.clone()),
// hlx:304
(Ty::Bool, J::Bool(b)) => Val::B(*b),
// hlx:305
(Ty::Int, J::Str(s)) => match canonical_int(s) {
// hlx:306
Some(n) => Val::I(n),
// hlx:307
None => return Err(format!("invalid_number: fact \"{}\" is not a canonical decimal i128 string", clip(k))),
// hlx:308
},
// hlx:309
(Ty::Rational, J::Str(s)) => match rational::Rational::parse(s) {
// hlx:310
Some(q) => Val::Q(q),
// hlx:311
None => return Err(format!("invalid_number: fact \"{}\" must be numerator/denominator with canonical i128 components and positive denominator", clip(k))),
// hlx:312
},
// hlx:313
(Ty::Set, J::Arr(items)) => {
// hlx:314
let mut members = Vec::new();
// hlx:315
for it in items {
// hlx:316
match it {
// hlx:317
J::Str(s) => members.push(s.clone()),
// hlx:318
_ => return Err(format!("type_mismatch: fact \"{}\" must be an array of strings", clip(k))),
// hlx:319
}
// hlx:320
}
// hlx:321
entries += members.len();
// hlx:322
members.sort();
// hlx:323
if members.windows(2).any(|w| w[0] == w[1]) {
// hlx:324
return Err(format!("duplicate_member: fact \"{}\" has a duplicate set member", clip(k)));
// hlx:325
}
// hlx:326
Val::Set(members)
// hlx:327
}
// hlx:328
_ => return Err(format!("type_mismatch: fact \"{}\" is not of declared type {}", clip(k), ty.name())),
// hlx:329
};
            // hlx:330
            facts.insert(k.clone(), val);
            // hlx:331
        }
        // hlx:332
    }
    // hlx:333
    let mut conditions = BTreeMap::new();
    // hlx:334
    if let Some(c) = field(top, "conditions") {
        // hlx:335
        for (k, v) in obj(c, "conditions")? {
            // hlx:336
            if !def.conditions.iter().any(|x| x == k) {
                // hlx:337
                return Err(format!(
                    "unknown_condition: \"{}\" is not a required condition",
                    clip(k)
                ));
                // hlx:338
            }
            // hlx:339
            match v {
                // hlx:340
                J::Bool(b) => {
                    // hlx:341
                    conditions.insert(k.clone(), *b);
                    // hlx:342
                }
                // hlx:343
                _ => {
                    return Err(format!(
                        "type_mismatch: condition \"{}\" must be a boolean",
                        clip(k)
                    ))
                }
                // hlx:344
            }
            // hlx:345
        }
        // hlx:346
    }
    // hlx:347
    let scope = match field(top, "scope") {
        // hlx:348
        Some(s) => parse_scope(s, "request scope")?,
        // hlx:349
        None => Vec::new(),
        // hlx:350
    };
    // hlx:351
    Ok(Request {
        facts,
        entries,
        conditions,
        scope,
    })
    // hlx:352
}
// hlx:353
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
// hlx:354
enum Tri {
    // hlx:355
    T,
    // hlx:356
    F,
    // hlx:357
    U,
    // hlx:358
}
// hlx:359
struct Exhausted;
// hlx:360
struct Ev<'a> {
    // hlx:361
    facts: &'a BTreeMap<String, Val>,
    // hlx:362
    steps: u64,
    // hlx:363
    missing: BTreeSet<String>,
    // hlx:364
}
// hlx:365
type Out = Result<(Tri, Option<String>), Exhausted>;
// hlx:366
fn tri(b: bool) -> Tri {
    // hlx:367
    if b {
        // hlx:368
        Tri::T
    // hlx:369
    } else {
        // hlx:370
        Tri::F
        // hlx:371
    }
    // hlx:372
}
// hlx:373
impl<'a> Ev<'a> {
    // hlx:374
    fn charge(&mut self, n: u64) -> Result<(), Exhausted> {
        // hlx:375
        self.steps = self.steps.saturating_add(n);
        // hlx:376
        if self.steps > MAX_STEPS {
            // hlx:377
            self.steps = MAX_STEPS;
            // hlx:378
            return Err(Exhausted);
            // hlx:379
        }
        // hlx:380
        Ok(())
        // hlx:381
    }
    // hlx:382
    fn contains(&mut self, set: &[String], x: &str) -> Result<bool, Exhausted> {
        // hlx:383
        let (mut lo, mut hi) = (0usize, set.len());
        // hlx:384
        while lo < hi {
            // hlx:385
            self.charge(1)?;
            // hlx:386
            let mid = lo + (hi - lo) / 2;
            // hlx:387
            match set[mid].as_str().cmp(x) {
                // hlx:388
                std::cmp::Ordering::Equal => return Ok(true),
                // hlx:389
                std::cmp::Ordering::Less => lo = mid + 1,
                // hlx:390
                std::cmp::Ordering::Greater => hi = mid,
                // hlx:391
            }
            // hlx:392
        }
        // hlx:393
        Ok(false)
        // hlx:394
    }
    // hlx:395
    fn eval(&mut self, n: &Node) -> Out {
        // hlx:396
        self.charge(1)?;
        // hlx:397
        match n {
            // hlx:398
            Node::Cmp(c, l, r) => self.cmp(*c, l, r),
            // hlx:399
            Node::Not(inner) => {
                // hlx:400
                let (t, _) = self.eval(inner)?;
                // hlx:401
                Ok((
                    // hlx:402
                    match t {
                        // hlx:403
                        Tri::T => Tri::F,
                        // hlx:404
                        Tri::F => Tri::T,
                        // hlx:405
                        Tri::U => Tri::U,
                        // hlx:406
                    },
                    // hlx:407
                    None,
                    // hlx:408
                ))
                // hlx:409
            }
            // hlx:410
            Node::All(kids) => {
                // hlx:411
                let mut unknown = false;
                // hlx:412
                for k in kids {
                    // hlx:413
                    let (t, w) = self.eval(k)?;
                    // hlx:414
                    match t {
                        // hlx:415
                        Tri::F => return Ok((Tri::F, w)),
                        // hlx:416
                        Tri::U => unknown = true,
                        // hlx:417
                        Tri::T => {} // hlx:418
                    }
                    // hlx:419
                }
                // hlx:420
                Ok((if unknown { Tri::U } else { Tri::T }, None))
                // hlx:421
            }
            // hlx:422
            Node::Any(kids) => {
                // hlx:423
                let mut unknown = false;
                // hlx:424
                for k in kids {
                    // hlx:425
                    let (t, _) = self.eval(k)?;
                    // hlx:426
                    match t {
                        // hlx:427
                        Tri::T => return Ok((Tri::T, None)),
                        // hlx:428
                        Tri::U => unknown = true,
                        // hlx:429
                        Tri::F => {} // hlx:430
                    }
                    // hlx:431
                }
                // hlx:432
                Ok((if unknown { Tri::U } else { Tri::F }, None))
                // hlx:433
            } // hlx:434
        }
        // hlx:435
    }
    // hlx:436
    fn cmp(&mut self, c: Cmp, l: &str, r: &str) -> Out {
        // hlx:437
        let facts = self.facts;
        // hlx:438
        let (lv, rv) = (facts.get(l), facts.get(r));
        // hlx:439
        if lv.is_none() {
            // hlx:440
            self.missing.insert(l.to_string());
            // hlx:441
        }
        // hlx:442
        if rv.is_none() {
            // hlx:443
            self.missing.insert(r.to_string());
            // hlx:444
        }
        // hlx:445
        let (lv, rv) = match (lv, rv) {
            // hlx:446
            (Some(a), Some(b)) => (a, b),
            // hlx:447
            _ => return Ok((Tri::U, None)),
            // hlx:448
        };
        // hlx:449
        match (c, lv, rv) {
            // hlx:450
            (Cmp::Subset, Val::Set(a), Val::Set(b)) => {
                // hlx:451
                let (mut i, mut j) = (0usize, 0usize);
                // hlx:452
                while i < a.len() {
                    // hlx:453
                    self.charge(1)?;
                    // hlx:454
                    if j >= b.len() || a[i] < b[j] {
                        // hlx:455
                        return Ok((Tri::F, Some(a[i].clone())));
                        // hlx:456
                    }
                    // hlx:457
                    if a[i] == b[j] {
                        // hlx:458
                        i += 1;
                        // hlx:459
                    }
                    // hlx:460
                    j += 1;
                    // hlx:461
                }
                // hlx:462
                Ok((Tri::T, None))
                // hlx:463
            }
            // hlx:464
            (Cmp::Disjoint, Val::Set(a), Val::Set(b)) => {
                // hlx:465
                let (mut i, mut j) = (0usize, 0usize);
                // hlx:466
                while i < a.len() && j < b.len() {
                    // hlx:467
                    self.charge(1)?;
                    // hlx:468
                    match a[i].cmp(&b[j]) {
                        // hlx:469
                        std::cmp::Ordering::Equal => return Ok((Tri::F, Some(a[i].clone()))),
                        // hlx:470
                        std::cmp::Ordering::Less => i += 1,
                        // hlx:471
                        std::cmp::Ordering::Greater => j += 1,
                        // hlx:472
                    }
                    // hlx:473
                }
                // hlx:474
                Ok((Tri::T, None))
                // hlx:475
            }
            // hlx:476
            (Cmp::Contains, Val::Set(a), Val::S(x)) => {
                // hlx:477
                let found = self.contains(a, x)?;
                // hlx:478
                Ok((tri(found), None))
                // hlx:479
            }
            // hlx:480
            (Cmp::Eq, Val::Set(a), Val::Set(b)) => {
                // hlx:481
                self.charge(1)?;
                // hlx:482
                if a.len() != b.len() {
                    // hlx:483
                    return Ok((Tri::F, None));
                    // hlx:484
                }
                // hlx:485
                for k in 0..a.len() {
                    // hlx:486
                    self.charge(1)?;
                    // hlx:487
                    if a[k] != b[k] {
                        // hlx:488
                        return Ok((Tri::F, None));
                        // hlx:489
                    }
                    // hlx:490
                }
                // hlx:491
                Ok((Tri::T, None))
                // hlx:492
            }
            // hlx:493
            (Cmp::Eq | Cmp::Lt | Cmp::Lte, Val::Q(a), Val::Q(b)) => {
                // hlx:494
                let (order, steps) = a.compare(*b);
                // hlx:495
                self.charge(steps)?;
                // hlx:496
                let pass = match c {
                    Cmp::Eq => order.is_eq(),
                    Cmp::Lt => order.is_lt(),
                    Cmp::Lte => !order.is_gt(),
                    _ => unreachable!(),
                };
                // hlx:497
                Ok((tri(pass), None))
                // hlx:498
            }
            // hlx:499
            (Cmp::Eq, Val::S(a), Val::S(b)) => Ok((tri(a == b), None)),
            // hlx:500
            (Cmp::Eq, Val::B(a), Val::B(b)) => Ok((tri(a == b), None)),
            // hlx:501
            (Cmp::Eq, Val::I(a), Val::I(b)) => Ok((tri(a == b), None)),
            // hlx:502
            (Cmp::Lt, Val::I(a), Val::I(b)) => Ok((tri(a < b), None)),
            // hlx:503
            (Cmp::Lte, Val::I(a), Val::I(b)) => Ok((tri(a <= b), None)),
            // hlx:504
            _ => Ok((Tri::U, None)),
            // hlx:505
        }
        // hlx:506
    }
    // hlx:507
}
// hlx:508
fn render_claim(n: &Node, out: &mut String) {
    // hlx:509
    if out.len() > MAX_CLAIM_TEXT {
        // hlx:510
        return;
        // hlx:511
    }
    // hlx:512
    match n {
        // hlx:513
        Node::Cmp(c, l, r) => out.push_str(&format!("{}({}, {})", c.name(), l, r)),
        // hlx:514
        Node::Not(k) => {
            // hlx:515
            out.push_str("not(");
            // hlx:516
            render_claim(k, out);
            // hlx:517
            out.push(')');
            // hlx:518
        }
        // hlx:519
        Node::All(kids) | Node::Any(kids) => {
            // hlx:520
            out.push_str(if matches!(n, Node::All(_)) {
                "all("
            } else {
                "any("
            });
            // hlx:521
            for (i, k) in kids.iter().enumerate() {
                // hlx:522
                if i > 0 {
                    // hlx:523
                    out.push_str(", ");
                    // hlx:524
                }
                // hlx:525
                render_claim(k, out);
                // hlx:526
            }
            // hlx:527
            out.push(')');
            // hlx:528
        } // hlx:529
    }
    // hlx:530
}
// hlx:531
fn jstr(s: &str) -> String {
    // hlx:532
    let mut o = String::with_capacity(s.len() + 2);
    // hlx:533
    o.push('"');
    // hlx:534
    for ch in s.chars() {
        // hlx:535
        match ch {
            // hlx:536
            '"' => o.push_str("\\\""),
            // hlx:537
            '\\' => o.push_str("\\\\"),
            // hlx:538
            '\n' => o.push_str("\\n"),
            // hlx:539
            '\r' => o.push_str("\\r"),
            // hlx:540
            '\t' => o.push_str("\\t"),
            // hlx:541
            c if (c as u32) < 0x20 => o.push_str(&format!("\\u{:04x}", c as u32)),
            // hlx:542
            c => o.push(c),
            // hlx:543
        }
        // hlx:544
    }
    // hlx:545
    o.push('"');
    // hlx:546
    o
    // hlx:547
}
// hlx:548
fn jlist(items: &[String]) -> String {
    // hlx:549
    let parts: Vec<String> = items.iter().map(|s| jstr(s)).collect();
    // hlx:550
    format!("[{}]", parts.join(","))
    // hlx:551
}
// hlx:552
fn jscope(scope: &[(&'static str, String)]) -> String {
    // hlx:553
    let parts: Vec<String> = scope
        .iter()
        .map(|(k, v)| format!("{}:{}", jstr(k), jstr(v)))
        .collect();
    // hlx:554
    format!("{{{}}}", parts.join(","))
    // hlx:555
}
// hlx:556
pub fn error_json(reason: &str) -> String {
    // hlx:557
    format!("{{\"schema\":\"instinct.check-error/v1\",\"verdict\":\"error\",\"reason\":{},\"authorization\":\"not_evaluated\"}}", jstr(reason))
    // hlx:558
}
// hlx:559
/// One pure entry point for CLI, MCP and WASM. Returns the JSON text and whether it is an error shape.
// hlx:560
pub fn evaluate(mode: Mode, input: &[u8]) -> (String, bool) {
    // hlx:561
    match evaluate_inner(mode, input) {
        // hlx:562
        Ok(s) => (s, false),
        // hlx:563
        Err(e) => (error_json(&e), true),
        // hlx:564
    }
    // hlx:565
}
// hlx:566
fn evaluate_inner(mode: Mode, input: &[u8]) -> R<String> {
    // hlx:567
    let tree = json::parse(input)?;
    // hlx:568
    let top = match &tree {
        // hlx:569
        J::Obj(o) => o,
        // hlx:570
        _ => return Err("malformed_definition: the request must be a JSON object".to_string()),
        // hlx:571
    };
    // hlx:572
    only_keys(top, &["check", "facts", "conditions", "scope"], "request")?;
    // hlx:573
    let def = compile_def(need(top, "check", "request")?)?;
    // hlx:574
    let req = read_request(top, &def)?;
    // hlx:575
    if mode == Mode::Validate {
        // hlx:576
        if req.entries > MAX_FACT_ENTRIES {
            // hlx:577
            return Err(
                "limit_exceeded: facts have more than 10000 entries and set members".to_string(),
            );
            // hlx:578
        }
        // hlx:579
        return Ok(format!(
// hlx:580
"{{\"schema\":\"instinct.check-validation/v1\",\"check_id\":{},\"revision\":{},\"valid\":true,\"authorization\":\"not_evaluated\"}}",
// hlx:581
jstr(&def.id),
// hlx:582
def.revision
// hlx:583
));
        // hlx:584
    }
    // hlx:585
    let mut missing: BTreeSet<String> = BTreeSet::new();
    // hlx:586
    let mut steps = 0u64;
    // hlx:587
    let mut witness: Option<String> = None;
    // hlx:588
    let (verdict, reason) = 'decide: {
        // hlx:589
        if req.entries > MAX_FACT_ENTRIES {
            // hlx:590
            break 'decide (
                "abstain",
                "resource_limit: facts exceed 10000 entries".to_string(),
            );
            // hlx:591
        }
        // hlx:592
        for (dim, want) in &def.scope {
            // hlx:593
            match req.scope.iter().find(|(d, _)| d == dim) {
                // hlx:594
                None => break 'decide ("abstain", format!("scope_missing: {dim}")),
                // hlx:595
                Some((_, have)) if have != want => {
                    break 'decide ("abstain", format!("scope_mismatch: {dim}"))
                }
                // hlx:596
                Some(_) => {} // hlx:597
            }
            // hlx:598
        }
        // hlx:599
        for c in &def.conditions {
            // hlx:600
            if req.conditions.get(c) != Some(&true) {
                // hlx:601
                missing.insert(c.clone());
                // hlx:602
            }
            // hlx:603
        }
        // hlx:604
        if !missing.is_empty() {
            // hlx:605
            break 'decide ("abstain", "required_condition_not_met".to_string());
            // hlx:606
        }
        // hlx:607
        let mut ev = Ev {
            facts: &req.facts,
            steps: 0,
            missing: BTreeSet::new(),
        };
        // hlx:608
        let res = ev.eval(&def.claim);
        // hlx:609
        steps = ev.steps;
        // hlx:610
        missing.extend(ev.missing);
        // hlx:611
        match res {
            // hlx:612
            Err(Exhausted) => (
                "abstain",
                "resource_limit: evaluation budget exhausted".to_string(),
            ),
            // hlx:613
            Ok((Tri::T, _)) => ("pass", "claim_holds".to_string()),
            // hlx:614
            Ok((Tri::F, w)) => {
                // hlx:615
                witness = w;
                // hlx:616
                ("fail", "claim_false".to_string())
                // hlx:617
            }
            // hlx:618
            Ok((Tri::U, _)) => ("abstain", "fact_missing".to_string()),
            // hlx:619
        }
        // hlx:620
    };
    // hlx:621
    let missing: Vec<String> = missing.into_iter().collect();
    // hlx:622
    let mut out = format!(
// hlx:623
"{{\"schema\":\"instinct.check-result/v1\",\"check_id\":{},\"revision\":{},\"verdict\":\"{}\",\"reason\":{},\"witness\":{},\"missing\":{},\"scope\":{},\"steps\":{}",
// hlx:624
jstr(&def.id),
// hlx:625
def.revision,
// hlx:626
verdict,
// hlx:627
jstr(&reason),
// hlx:628
match &witness {
// hlx:629
Some(w) => jstr(w),
// hlx:630
None => "null".to_string(),
// hlx:631
},
// hlx:632
jlist(&missing),
// hlx:633
jscope(&req.scope),
// hlx:634
steps
// hlx:635
);
    // hlx:636
    if mode == Mode::Explain {
        // hlx:637
        let mut claim = String::new();
        // hlx:638
        render_claim(&def.claim, &mut claim);
        // hlx:639
        let claim_truncated = claim.len() > MAX_CLAIM_TEXT;
        // hlx:640
        if claim_truncated {
            // hlx:641
            let mut end = MAX_CLAIM_TEXT;
            // hlx:642
            while !claim.is_char_boundary(end) {
                // hlx:643
                end -= 1;
                // hlx:644
            }
            // hlx:645
            claim.truncate(end);
            // hlx:646
            claim.push_str("...");
            // hlx:647
        }
        // hlx:648
        let inputs: Vec<String> = def
            .inputs
            .iter()
            .take(MAX_LISTED)
            .map(|(k, t)| format!("{}:{}", jstr(k), jstr(t.name())))
            .collect();
        // hlx:649
        let conds: Vec<String> = def.conditions.iter().take(MAX_LISTED).cloned().collect();
        // hlx:650
        out.push_str(&format!(
// hlx:651
",\"explanation\":{{\"claim\":{},\"claim_truncated\":{},\"inputs\":{{{}}},\"inputs_truncated\":{},\"required_conditions\":{},\"required_conditions_truncated\":{},\"required_scope\":{}}}",
// hlx:652
jstr(&claim),
// hlx:653
claim_truncated,
// hlx:654
inputs.join(","),
// hlx:655
def.inputs.len() > MAX_LISTED,
// hlx:656
jlist(&conds),
// hlx:657
def.conditions.len() > MAX_LISTED,
// hlx:658
jscope(&def.scope)
// hlx:659
));
        // hlx:660
    }
    // hlx:661
    out.push_str(",\"authorization\":\"not_evaluated\",\"evidence_origin\":\"caller_reported\"}");
    // hlx:662
    if out.len() > MAX_OUTPUT_BYTES {
        // hlx:663
        // Never echo an oversized request back and never truncate scope, missing or explanation: refuse instead.
        // hlx:664
        return Err(
            "limit_exceeded: result would exceed 64 KiB (scope, missing or explanation too large)"
                .to_string(),
        );
        // hlx:665
    }
    // hlx:666
    Ok(out)
    // hlx:667
}
// hlx:668
#[cfg(test)]
// hlx:669
mod tests {
    // hlx:670
    use super::*;
    // hlx:671
    fn subset_doc(member: &str) -> String {
        // hlx:672
        format!(
// hlx:673
"{{\"check\":{{\"schema\":\"instinct.check/v1\",\"id\":\"a\",\"revision\":1,\"inputs\":{{\"x\":\"set<string>\",\"y\":\"set<string>\"}},\"claim\":{{\"op\":\"subset\",\"left\":{{\"fact\":\"x\"}},\"right\":{{\"fact\":\"y\"}}}}}},\"facts\":{{\"x\":[\"{member}\"],\"y\":[]}}}}"
// hlx:674
)
        // hlx:675
    }
    // hlx:676
    #[test]
    // hlx:677
    fn diagnostic_clip_removes_display_controls_without_changing_fact_strings() {
        // hlx:678
        assert_eq!(clip("bad\u{202e}name\u{2066}x\u{200d}"), "bad?name?x?");
        // hlx:679
        assert_eq!(clip("café日本語"), "café日本語");
        // hlx:680
        assert!(jstr("\u{202e}fact").contains('\u{202e}'));
        // hlx:681
    }
    // hlx:682
    #[test]
    // hlx:683
    fn explanation_declares_when_display_claim_is_incomplete() {
        // hlx:684
        let leaf =
            r#"{"op":"eq","left":{"fact":"long_fact_name"},"right":{"fact":"long_fact_name"}}"#;
        // hlx:685
        let claim = format!(
            "{{\"op\":\"all\",\"args\":[{}]}}",
            vec![leaf; 1000].join(",")
        );
        // hlx:686
        let input = format!("{{\"check\":{{\"schema\":\"instinct.check/v1\",\"id\":\"explain-limit\",\"revision\":1,\"inputs\":{{\"long_fact_name\":\"bool\"}},\"claim\":{claim}}},\"facts\":{{\"long_fact_name\":true}}}}");
        // hlx:687
        let (out, error) = evaluate(Mode::Explain, input.as_bytes());
        // hlx:688
        assert!(!error, "{out}");
        // hlx:689
        assert!(out.contains("\"claim_truncated\":true"), "{out}");
        // hlx:690
        assert!(out.len() <= MAX_OUTPUT_BYTES);
        // hlx:691
    }
    // hlx:692
    #[test]
    // hlx:693
    fn long_witness_is_the_exact_member_when_the_result_fits() {
        // hlx:694
        let member = "z".repeat(5000);
        // hlx:695
        let (out, is_err) = evaluate(Mode::Run, subset_doc(&member).as_bytes());
        // hlx:696
        assert!(!is_err);
        // hlx:697
        assert!(out.contains(&format!("\"witness\":\"{member}\"")));
        // hlx:698
        assert!(!out.contains("truncated"));
        // hlx:699
    }
    // hlx:700
    #[test]
    // hlx:701
    fn a_witness_that_cannot_fit_is_an_error_never_a_fabricated_member() {
        // hlx:702
        let member = "z".repeat(70000);
        // hlx:703
        let (out, is_err) = evaluate(Mode::Run, subset_doc(&member).as_bytes());
        // hlx:704
        assert!(is_err);
        // hlx:705
        assert!(out.contains("limit_exceeded") && out.len() < 300);
        // hlx:706
    }
    // hlx:707
}