hf2q 0.1.10

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
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
use std::collections::{BTreeMap, BTreeSet};

use serde_json::Value;
use sha2::{Digest, Sha256};

use super::logical_hash::LOGICAL_F32_HASH_ENCODING;
use super::types::*;

#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum TensorExecutionManifestError {
    #[error("tensor execution manifest serialization failed: {0}")]
    Serialization(String),
    #[error("tensor execution manifest is invalid: {0}")]
    Invalid(String),
    #[error("tensor execution manifest digest mismatch")]
    DigestMismatch,
}

#[derive(Debug, Clone)]
/// Structurally validated evidence. D2b's source/GGUF/runtime producer is
/// responsible for independently rehashing the referenced physical bytes
/// before this can become production execution evidence.
pub struct ValidatedTensorExecutionManifest {
    manifest: TensorExecutionManifest,
}

impl ValidatedTensorExecutionManifest {
    pub fn manifest(&self) -> &TensorExecutionManifest {
        &self.manifest
    }

    pub fn into_manifest(self) -> TensorExecutionManifest {
        self.manifest
    }
}

fn is_sha256(value: &str) -> bool {
    value.len() == 64
        && value
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
}

fn normalize_json(value: Value) -> Value {
    match value {
        Value::Object(object) => {
            let mut sorted = BTreeMap::new();
            for (key, value) in object {
                sorted.insert(key, normalize_json(value));
            }
            let mut normalized = serde_json::Map::new();
            for (key, value) in sorted {
                normalized.insert(key, value);
            }
            Value::Object(normalized)
        }
        Value::Array(values) => Value::Array(values.into_iter().map(normalize_json).collect()),
        other => other,
    }
}

fn validate_json_evidence(evidence: &CanonicalJsonEvidence) -> Result<(), String> {
    if !is_sha256(&evidence.sha256) {
        return Err("canonical JSON evidence has an invalid digest".into());
    }
    let parsed: Value = serde_json::from_str(&evidence.canonical_json)
        .map_err(|error| format!("canonical JSON evidence is invalid JSON: {error}"))?;
    let canonical = serde_json::to_string(&normalize_json(parsed))
        .map_err(|error| format!("canonical JSON evidence cannot serialize: {error}"))?;
    if canonical != evidence.canonical_json
        || hex::encode(Sha256::digest(canonical.as_bytes())) != evidence.sha256
    {
        return Err("canonical JSON evidence bytes or digest do not match".into());
    }
    Ok(())
}

fn normalize_manifest(manifest: &TensorExecutionManifest) -> TensorExecutionManifest {
    let mut normalized = manifest.clone();
    normalized
        .artifacts
        .sort_by(|left, right| left.artifact_id.cmp(&right.artifact_id));
    normalized
        .nodes
        .sort_by(|left, right| left.node_id.cmp(&right.node_id));
    normalized
        .transforms
        .sort_by(|left, right| left.edge_id.cmp(&right.edge_id));
    normalized
        .operations
        .sort_by(|left, right| left.binding_id.cmp(&right.binding_id));
    normalized
        .dispositions
        .sort_by(|left, right| left.source_node_id.cmp(&right.source_node_id));
    normalized.scope.included_paths.sort();
    for disposition in &mut normalized.dispositions {
        disposition.terminal_node_ids.sort();
    }
    for edge in &mut normalized.transforms {
        edge.inputs
            .sort_by(|left, right| left.role.cmp(&right.role));
        edge.outputs
            .sort_by(|left, right| left.role.cmp(&right.role));
    }
    for operation in &mut normalized.operations {
        operation.source_tensor_names.sort();
        operation
            .inputs
            .sort_by(|left, right| left.role.cmp(&right.role));
    }
    normalized
}

pub fn canonicalized_tensor_execution_manifest(
    manifest: &TensorExecutionManifest,
) -> TensorExecutionManifest {
    normalize_manifest(manifest)
}

pub fn canonical_tensor_execution_manifest_bytes(
    manifest: &TensorExecutionManifest,
) -> Result<Vec<u8>, TensorExecutionManifestError> {
    let mut normalized = normalize_manifest(manifest);
    normalized.manifest_sha256.clear();
    serde_json::to_vec(&normalized)
        .map_err(|error| TensorExecutionManifestError::Serialization(error.to_string()))
}

pub fn tensor_execution_manifest_sha256(
    manifest: &TensorExecutionManifest,
) -> Result<String, TensorExecutionManifestError> {
    Ok(hex::encode(Sha256::digest(
        canonical_tensor_execution_manifest_bytes(manifest)?,
    )))
}

pub fn unique_stored_payload_bytes(
    validated: &ValidatedTensorExecutionManifest,
    node_ids: &[String],
) -> Result<u64, TensorExecutionManifestError> {
    let nodes: BTreeMap<_, _> = validated
        .manifest
        .nodes
        .iter()
        .map(|node| (node.node_id.as_str(), node))
        .collect();
    let mut unique = BTreeSet::new();
    let mut total = 0u64;
    for node_id in node_ids {
        if !unique.insert(node_id.as_str()) {
            continue;
        }
        let node = nodes.get(node_id.as_str()).ok_or_else(|| {
            TensorExecutionManifestError::Invalid(format!(
                "stored payload references unknown node `{node_id}`"
            ))
        })?;
        if node.stage != TensorStateStage::Stored {
            return Err(TensorExecutionManifestError::Invalid(format!(
                "payload node `{node_id}` is not stored"
            )));
        }
        total = total.checked_add(node.byte_len).ok_or_else(|| {
            TensorExecutionManifestError::Invalid("stored payload byte sum overflows u64".into())
        })?;
    }
    Ok(total)
}

fn normalize_lineage_slice(slice: &TensorLineageSlice) -> TensorLineageSlice {
    let mut normalized = slice.clone();
    normalized
        .nodes
        .sort_by(|left, right| left.node_id.cmp(&right.node_id));
    normalized
        .transforms
        .sort_by(|left, right| left.edge_id.cmp(&right.edge_id));
    normalized
        .operations
        .sort_by(|left, right| left.binding_id.cmp(&right.binding_id));
    for edge in &mut normalized.transforms {
        edge.inputs
            .sort_by(|left, right| left.role.cmp(&right.role));
        edge.outputs
            .sort_by(|left, right| left.role.cmp(&right.role));
    }
    for operation in &mut normalized.operations {
        operation.source_tensor_names.sort();
        operation
            .inputs
            .sort_by(|left, right| left.role.cmp(&right.role));
    }
    normalized
}

pub fn canonical_tensor_lineage_slice_bytes(
    slice: &TensorLineageSlice,
) -> Result<Vec<u8>, TensorExecutionManifestError> {
    let mut normalized = normalize_lineage_slice(slice);
    normalized.slice_sha256.clear();
    serde_json::to_vec(&normalized)
        .map_err(|error| TensorExecutionManifestError::Serialization(error.to_string()))
}

pub fn tensor_lineage_slice_sha256(
    slice: &TensorLineageSlice,
) -> Result<String, TensorExecutionManifestError> {
    Ok(hex::encode(Sha256::digest(
        canonical_tensor_lineage_slice_bytes(slice)?,
    )))
}

/// Hash the complete physical identity of one state node. Allocator evidence
/// must use this projection instead of repeating selected node fields.
pub fn tensor_state_node_sha256(
    node: &TensorStateNode,
) -> Result<String, TensorExecutionManifestError> {
    let bytes = serde_json::to_vec(node)
        .map_err(|error| TensorExecutionManifestError::Serialization(error.to_string()))?;
    Ok(hex::encode(Sha256::digest(bytes)))
}

fn normalize_runtime_operation(operation: &RuntimeOperationBinding) -> RuntimeOperationBinding {
    let mut normalized = operation.clone();
    normalized.source_tensor_names.sort();
    normalized
        .inputs
        .sort_by(|left, right| left.role.cmp(&right.role));
    normalized
}

/// Hash every regime-specific physical binding for one stable logical
/// operation. This prevents an allocator option from substituting a cheaper
/// request, route, or executed tensor while retaining a real manifest hash.
pub fn runtime_capability_binding_bundle_sha256(
    validated: &ValidatedTensorExecutionManifest,
    operation_id: &str,
) -> Result<String, TensorExecutionManifestError> {
    let mut bindings: Vec<_> = validated
        .manifest()
        .operations
        .iter()
        .filter(|binding| binding.operation_id == operation_id)
        .map(normalize_runtime_operation)
        .collect();
    if bindings.is_empty() {
        return Err(TensorExecutionManifestError::Invalid(format!(
            "logical operation `{operation_id}` has no runtime bindings"
        )));
    }
    bindings.sort_by(|left, right| left.binding_id.cmp(&right.binding_id));
    let bytes = serde_json::to_vec(&bindings)
        .map_err(|error| TensorExecutionManifestError::Serialization(error.to_string()))?;
    Ok(hex::encode(Sha256::digest(bytes)))
}

/// Hash the exact physical bindings for one logical operation and workload
/// regime. Regime cost evidence must reference this projection, never a
/// caller-authored route or shape label.
pub fn runtime_regime_binding_bundle_sha256(
    validated: &ValidatedTensorExecutionManifest,
    operation_id: &str,
    regime: TensorExecutionRegime,
) -> Result<String, TensorExecutionManifestError> {
    let mut bindings: Vec<_> = validated
        .manifest()
        .operations
        .iter()
        .filter(|binding| binding.operation_id == operation_id && binding.workload_regime == regime)
        .map(normalize_runtime_operation)
        .collect();
    if bindings.is_empty() {
        return Err(TensorExecutionManifestError::Invalid(format!(
            "logical operation `{operation_id}` has no {regime:?} runtime bindings"
        )));
    }
    bindings.sort_by(|left, right| left.binding_id.cmp(&right.binding_id));
    let bytes = serde_json::to_vec(&bindings)
        .map_err(|error| TensorExecutionManifestError::Serialization(error.to_string()))?;
    Ok(hex::encode(Sha256::digest(bytes)))
}

pub fn tensor_lineage_slice(
    validated: &ValidatedTensorExecutionManifest,
    source_tensor_name: &str,
) -> Result<TensorLineageSlice, TensorExecutionManifestError> {
    let manifest = validated.manifest();
    let source = manifest
        .nodes
        .iter()
        .find(|node| {
            node.stage == TensorStateStage::Source && node.semantic_name == source_tensor_name
        })
        .ok_or_else(|| {
            TensorExecutionManifestError::Invalid(format!(
                "source tensor `{source_tensor_name}` is absent from execution manifest"
            ))
        })?;
    let mut adjacency: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
    for edge in &manifest.transforms {
        for input in &edge.inputs {
            for output in &edge.outputs {
                adjacency
                    .entry(input.node_id.as_str())
                    .or_default()
                    .push(output.node_id.as_str());
            }
        }
    }
    let mut reachable = BTreeSet::new();
    let mut pending = vec![source.node_id.as_str()];
    while let Some(node_id) = pending.pop() {
        if reachable.insert(node_id) {
            if let Some(outputs) = adjacency.get(node_id) {
                pending.extend(outputs.iter().copied());
            }
        }
    }
    // Follow an edge when any input is reachable so either member of a fused
    // gate+up source pair discovers the shared output. Then close the set
    // backwards over every producer so the projection also contains all
    // co-input sources and producer edges. A physical node can therefore
    // never appear in a slice without the transformation that created it.
    let producer_by_output: BTreeMap<&str, &TensorTransformEdge> = manifest
        .transforms
        .iter()
        .flat_map(|edge| {
            edge.outputs
                .iter()
                .map(move |output| (output.node_id.as_str(), edge))
        })
        .collect();
    let mut closed = reachable.clone();
    let mut included_edges = BTreeSet::new();
    let mut pending: Vec<_> = reachable.iter().copied().collect();
    for operation in manifest.operations.iter().filter(|operation| {
        operation
            .source_tensor_names
            .iter()
            .any(|name| name == source_tensor_name)
    }) {
        for input in &operation.inputs {
            if closed.insert(input.node_id.as_str()) {
                pending.push(input.node_id.as_str());
            }
        }
    }
    while let Some(node_id) = pending.pop() {
        if let Some(edge) = producer_by_output.get(node_id) {
            included_edges.insert(edge.edge_id.as_str());
            for input in &edge.inputs {
                if closed.insert(input.node_id.as_str()) {
                    pending.push(input.node_id.as_str());
                }
            }
            for output in &edge.outputs {
                closed.insert(output.node_id.as_str());
            }
        }
    }

    let mut slice = TensorLineageSlice {
        execution_manifest_sha256: manifest.manifest_sha256.clone(),
        source_tensor_name: source_tensor_name.into(),
        nodes: manifest
            .nodes
            .iter()
            .filter(|node| closed.contains(node.node_id.as_str()))
            .cloned()
            .collect(),
        transforms: manifest
            .transforms
            .iter()
            .filter(|edge| included_edges.contains(edge.edge_id.as_str()))
            .cloned()
            .collect(),
        operations: manifest
            .operations
            .iter()
            .filter(|operation| {
                operation
                    .source_tensor_names
                    .iter()
                    .any(|name| name == source_tensor_name)
            })
            .cloned()
            .collect(),
        slice_sha256: String::new(),
    };
    slice.slice_sha256 = tensor_lineage_slice_sha256(&slice)?;
    Ok(normalize_lineage_slice(&slice))
}

fn validate_codec(codec: &PhysicalTensorCodec) -> bool {
    match codec {
        PhysicalTensorCodec::Dense { .. } => true,
        PhysicalTensorCodec::Ggml {
            wire_type_id,
            type_name,
        } => matches!(
            (*wire_type_id, type_name.as_str()),
            (0, "F32")
                | (1, "F16")
                | (2, "Q4_0")
                | (3, "Q4_1")
                | (6, "Q5_0")
                | (7, "Q5_1")
                | (8, "Q8_0")
                | (10, "Q2_K")
                | (11, "Q3_K")
                | (12, "Q4_K")
                | (13, "Q5_K")
                | (14, "Q6_K")
                | (20, "IQ4_NL")
                | (23, "IQ4_XS")
                | (30, "BF16")
        ),
        // Schema v1 models the current Qwen GGUF execution path. Affine
        // physical layout admission requires a separately proven MLX ABI.
        PhysicalTensorCodec::MlxAffine { .. } => false,
    }
}

fn expected_physical_bytes(node: &TensorStateNode) -> Option<u64> {
    let elements = node
        .shape
        .iter()
        .try_fold(1u64, |total, dim| total.checked_mul(*dim))?;
    let (block_values, block_bytes) = match &node.codec {
        PhysicalTensorCodec::Dense { dtype } => {
            return elements.checked_mul(match dtype {
                TensorScalarDType::F16 | TensorScalarDType::Bf16 => 2,
                TensorScalarDType::F32 => 4,
            });
        }
        PhysicalTensorCodec::Ggml {
            wire_type_id,
            type_name,
        } => match (*wire_type_id, type_name.as_str()) {
            (0, "F32") => (1, 4),
            (1, "F16") | (30, "BF16") => (1, 2),
            (2, "Q4_0") => (32, 18),
            (3, "Q4_1") => (32, 20),
            (6, "Q5_0") => (32, 22),
            (7, "Q5_1") => (32, 24),
            (8, "Q8_0") => (32, 34),
            (10, "Q2_K") => (256, 84),
            (11, "Q3_K") => (256, 110),
            (12, "Q4_K") => (256, 144),
            (13, "Q5_K") => (256, 176),
            (14, "Q6_K") => (256, 210),
            (20, "IQ4_NL") => (32, 18),
            (23, "IQ4_XS") => (256, 136),
            _ => return None,
        },
        PhysicalTensorCodec::MlxAffine { .. } => return None,
    };
    let row_width = *node.shape.last()?;
    if row_width % block_values != 0 {
        return None;
    }
    let outer_rows = elements / row_width;
    outer_rows
        .checked_mul(row_width / block_values)?
        .checked_mul(block_bytes)
}

pub fn verify_tensor_execution_manifest(
    manifest: &TensorExecutionManifest,
) -> Result<ValidatedTensorExecutionManifest, TensorExecutionManifestError> {
    let invalid = |message: String| TensorExecutionManifestError::Invalid(message);
    if manifest.schema_version != TENSOR_EXECUTION_MANIFEST_SCHEMA_VERSION
        || !is_sha256(&manifest.source_manifest_sha256)
        || !is_sha256(&manifest.source_tensor_inventory_sha256)
        || !is_sha256(&manifest.tensor_partition_manifest_sha256)
        || !is_sha256(&manifest.conversion_receipt_sha256)
        || manifest.logical_hash_encoding != LOGICAL_F32_HASH_ENCODING
        || !is_sha256(&manifest.runtime.routing_policy_sha256)
        || !is_sha256(&manifest.runtime.graph_configuration_sha256)
        || !is_sha256(&manifest.runtime.capability_profile_sha256)
        || !is_sha256(&manifest.runtime.hardware_profile_sha256)
        || manifest.runtime.dwq_overlay_sha256.is_some()
        || manifest.runtime.hf2q_revision.len() != 40
        || !manifest
            .runtime
            .hf2q_revision
            .bytes()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
        || manifest.runtime.mlx_native_version.is_empty()
        || manifest.runtime.mlx_native_capability_schema_version != 1
        || manifest.scope.model_family.is_empty()
        || manifest.scope.profile.is_empty()
        || manifest.scope.included_paths.is_empty()
    {
        return Err(invalid("manifest identity or scope is incomplete".into()));
    }
    let included_paths: BTreeSet<_> = manifest.scope.included_paths.iter().collect();
    if manifest.scope.included_paths.iter().any(String::is_empty)
        || included_paths.len() != manifest.scope.included_paths.len()
        || manifest.scope.excluded_paths.iter().any(|(path, reason)| {
            path.is_empty() || reason.is_empty() || manifest.scope.included_paths.contains(path)
        })
    {
        return Err(invalid("execution scope paths are invalid".into()));
    }

    let mut artifact_ids = BTreeSet::new();
    for artifact in &manifest.artifacts {
        if artifact.artifact_id.is_empty()
            || artifact.role.is_empty()
            || artifact.byte_len == 0
            || !is_sha256(&artifact.sha256)
            || !artifact_ids.insert(artifact.artifact_id.as_str())
        {
            return Err(invalid(
                "artifact evidence is incomplete or duplicated".into(),
            ));
        }
    }
    if artifact_ids.is_empty() {
        return Err(invalid("at least one artifact is required".into()));
    }

    let mut nodes = BTreeMap::new();
    let mut source_semantic_names = BTreeSet::new();
    for node in &manifest.nodes {
        if node.node_id.is_empty()
            || node.semantic_name.is_empty()
            || node.shape.is_empty()
            || node.shape.contains(&0)
            || node.layout != TENSOR_LAYOUT_ROW_MAJOR_OUTERMOST_FIRST_V1
            || !validate_codec(&node.codec)
            || expected_physical_bytes(node) != Some(node.byte_len)
            || node.byte_len == 0
            || !is_sha256(&node.byte_sha256)
            || !is_sha256(&node.logical_f32_sha256)
            || nodes.insert(node.node_id.as_str(), node).is_some()
        {
            return Err(invalid("tensor node is incomplete or duplicated".into()));
        }
        if node.stage == TensorStateStage::Source
            && !source_semantic_names.insert(node.semantic_name.as_str())
        {
            return Err(invalid("source semantic tensor name is duplicated".into()));
        }
        if let Some(region) = &node.artifact_region {
            let Some(artifact) = manifest
                .artifacts
                .iter()
                .find(|artifact| artifact.artifact_id == region.artifact_id)
            else {
                return Err(invalid(format!(
                    "node `{}` references an unknown artifact",
                    node.node_id
                )));
            };
            let Some(end) = region.byte_offset.checked_add(region.byte_len) else {
                return Err(invalid("artifact region arithmetic overflow".into()));
            };
            if region.byte_len != node.byte_len || end > artifact.byte_len {
                return Err(invalid(format!(
                    "node `{}` has an invalid artifact region",
                    node.node_id
                )));
            }
        }
        match node.stage {
            TensorStateStage::Source | TensorStateStage::Stored
                if node.artifact_region.is_none() =>
            {
                return Err(invalid(format!(
                    "physical node `{}` has no artifact region",
                    node.node_id
                )));
            }
            TensorStateStage::Converted | TensorStateStage::Loaded | TensorStateStage::Executed
                if node.artifact_region.is_some() =>
            {
                return Err(invalid(format!(
                    "ephemeral node `{}` has an artifact region",
                    node.node_id
                )));
            }
            _ => {}
        }
    }
    if nodes.is_empty() {
        return Err(invalid("at least one tensor node is required".into()));
    }

    let mut regions_by_artifact: BTreeMap<&str, Vec<(u64, u64, &str)>> = BTreeMap::new();
    for node in &manifest.nodes {
        if let Some(region) = &node.artifact_region {
            regions_by_artifact
                .entry(region.artifact_id.as_str())
                .or_default()
                .push((
                    region.byte_offset,
                    region.byte_offset + region.byte_len,
                    node.node_id.as_str(),
                ));
        }
    }
    for regions in regions_by_artifact.values_mut() {
        regions.sort_by_key(|region| region.0);
        for pair in regions.windows(2) {
            if pair[0].1 > pair[1].0 {
                return Err(invalid(format!(
                    "artifact regions for `{}` and `{}` overlap",
                    pair[0].2, pair[1].2
                )));
            }
        }
    }

    let mut edge_ids = BTreeSet::new();
    let mut produced_by = BTreeMap::new();
    let mut edge_inputs_by_output: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
    for edge in &manifest.transforms {
        if edge.edge_id.is_empty()
            || edge.inputs.is_empty()
            || edge.outputs.is_empty()
            || edge.implementation_revision.len() != 40
            || !edge
                .implementation_revision
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte))
            || !is_sha256(&edge.receipt_sha256)
            || !edge_ids.insert(edge.edge_id.as_str())
        {
            return Err(invalid("transform edge is incomplete or duplicated".into()));
        }
        let input_ids: BTreeSet<_> = edge.inputs.iter().map(|port| &port.node_id).collect();
        let output_ids: BTreeSet<_> = edge.outputs.iter().map(|port| &port.node_id).collect();
        let input_roles: BTreeSet<_> = edge.inputs.iter().map(|port| &port.role).collect();
        let output_roles: BTreeSet<_> = edge.outputs.iter().map(|port| &port.role).collect();
        if edge
            .inputs
            .iter()
            .chain(&edge.outputs)
            .any(|port| port.role.is_empty())
            || input_ids.len() != edge.inputs.len()
            || output_ids.len() != edge.outputs.len()
            || input_roles.len() != edge.inputs.len()
            || output_roles.len() != edge.outputs.len()
            || !input_ids.is_disjoint(&output_ids)
        {
            return Err(invalid(format!("edge `{}` repeats a node", edge.edge_id)));
        }
        let input_nodes = edge
            .inputs
            .iter()
            .map(|port| nodes.get(port.node_id.as_str()))
            .collect::<Option<Vec<_>>>()
            .ok_or_else(|| invalid(format!("edge `{}` has an unknown input", edge.edge_id)))?;
        let input_stages: BTreeSet<_> = input_nodes.iter().map(|node| node.stage).collect();
        let output_nodes = edge
            .outputs
            .iter()
            .map(|port| nodes.get(port.node_id.as_str()))
            .collect::<Option<Vec<_>>>()
            .ok_or_else(|| invalid(format!("edge `{}` has an unknown output", edge.edge_id)))?;
        let output_stages: BTreeSet<_> = output_nodes.iter().map(|node| node.stage).collect();
        let legal_signature = match &edge.operation {
            TensorTransformOperation::SourceDecode => {
                input_stages == [TensorStateStage::Source].into()
                    && output_stages == [TensorStateStage::Converted].into()
            }
            TensorTransformOperation::ArchitectureBake { .. }
            | TensorTransformOperation::Concatenate { .. }
            | TensorTransformOperation::F16Roundtrip => {
                input_stages == [TensorStateStage::Converted].into()
                    && output_stages == [TensorStateStage::Converted].into()
            }
            TensorTransformOperation::GgufQuantize { .. } => {
                input_stages == [TensorStateStage::Converted].into()
                    && output_stages == [TensorStateStage::Stored].into()
            }
            TensorTransformOperation::GgufDenseStore { .. } => {
                input_stages == [TensorStateStage::Converted].into()
                    && output_stages == [TensorStateStage::Stored].into()
            }
            TensorTransformOperation::GgufDequantize { .. }
            | TensorTransformOperation::DirectBlockLoad => {
                input_stages == [TensorStateStage::Stored].into()
                    && output_stages == [TensorStateStage::Loaded].into()
            }
            TensorTransformOperation::SplitInterleavedQGate { .. } => {
                input_stages == [TensorStateStage::Loaded].into()
                    && output_stages == [TensorStateStage::Loaded].into()
            }
            TensorTransformOperation::Transpose { .. }
            | TensorTransformOperation::Squeeze { .. }
            | TensorTransformOperation::ZeroPad { .. } => {
                input_stages.len() == 1
                    && input_stages == output_stages
                    && input_stages.iter().all(|stage| {
                        matches!(
                            stage,
                            TensorStateStage::Converted | TensorStateStage::Loaded
                        )
                    })
            }
            TensorTransformOperation::Qwen35LoadQ4Amax7V1 => {
                input_stages == [TensorStateStage::Loaded].into()
                    && output_stages == [TensorStateStage::Executed].into()
            }
            TensorTransformOperation::RuntimeBind => {
                input_stages == [TensorStateStage::Loaded].into()
                    && output_stages == [TensorStateStage::Executed].into()
            }
        };
        if !legal_signature {
            return Err(invalid(format!(
                "edge `{}` has an illegal stage signature",
                edge.edge_id
            )));
        }
        let single_input_output = || {
            (input_nodes.len() == 1 && output_nodes.len() == 1)
                .then_some((input_nodes[0], output_nodes[0]))
        };
        let input_role_set: BTreeSet<_> =
            edge.inputs.iter().map(|port| port.role.as_str()).collect();
        let output_role_set: BTreeSet<_> =
            edge.outputs.iter().map(|port| port.role.as_str()).collect();
        let geometry_valid = match &edge.operation {
            TensorTransformOperation::SourceDecode => {
                single_input_output().is_some_and(|(input, output)| {
                    input_role_set == ["source"].into()
                        && output_role_set == ["converted"].into()
                        && input.shape == output.shape
                        && matches!(input.codec, PhysicalTensorCodec::Dense { .. })
                        && matches!(
                            output.codec,
                            PhysicalTensorCodec::Dense {
                                dtype: TensorScalarDType::F32
                            }
                        )
                        && input.logical_f32_sha256 == output.logical_f32_sha256
                })
            }
            TensorTransformOperation::ArchitectureBake { .. } => {
                single_input_output().is_some_and(|(input, output)| {
                    input.shape == output.shape
                        && matches!(
                            input.codec,
                            PhysicalTensorCodec::Dense {
                                dtype: TensorScalarDType::F32
                            }
                        )
                        && matches!(
                            output.codec,
                            PhysicalTensorCodec::Dense {
                                dtype: TensorScalarDType::F32
                            }
                        )
                })
            }
            TensorTransformOperation::Concatenate { axis } => {
                output_nodes.len() == 1
                    && usize::try_from(*axis).is_ok_and(|axis| {
                        let output = output_nodes[0];
                        axis < output.shape.len()
                            && input_nodes.iter().all(|input| {
                                input.shape.len() == output.shape.len()
                                    && matches!(
                                        input.codec,
                                        PhysicalTensorCodec::Dense {
                                            dtype: TensorScalarDType::F32
                                        }
                                    )
                                    && matches!(
                                        output.codec,
                                        PhysicalTensorCodec::Dense {
                                            dtype: TensorScalarDType::F32
                                        }
                                    )
                                    && input.shape.iter().zip(&output.shape).enumerate().all(
                                        |(index, (input_dim, output_dim))| {
                                            index == axis || input_dim == output_dim
                                        },
                                    )
                            })
                            && input_nodes
                                .iter()
                                .try_fold(0u64, |total, input| total.checked_add(input.shape[axis]))
                                == Some(output.shape[axis])
                    })
            }
            TensorTransformOperation::F16Roundtrip => {
                single_input_output().is_some_and(|(input, output)| {
                    input.shape == output.shape
                        && matches!(
                            input.codec,
                            PhysicalTensorCodec::Dense {
                                dtype: TensorScalarDType::F32
                            }
                        )
                        && matches!(
                            output.codec,
                            PhysicalTensorCodec::Dense {
                                dtype: TensorScalarDType::F32
                            }
                        )
                })
            }
            TensorTransformOperation::GgufQuantize { .. } => {
                single_input_output().is_some_and(|(input, output)| {
                    input_role_set == ["converted"].into()
                        && output_role_set == ["stored"].into()
                        && matches!(
                            input.codec,
                            PhysicalTensorCodec::Dense {
                                dtype: TensorScalarDType::F32
                            }
                        )
                        && matches!(
                            output.codec,
                            PhysicalTensorCodec::Ggml {
                                wire_type_id: 2 | 3 | 6 | 7 | 8 | 10 | 11 | 12 | 13 | 14 | 20 | 23,
                                ..
                            }
                        )
                        && input.shape == output.shape
                })
            }
            TensorTransformOperation::GgufDenseStore { .. } => {
                single_input_output().is_some_and(|(input, output)| {
                    input_role_set == ["converted"].into()
                        && output_role_set == ["stored"].into()
                        && matches!(
                            input.codec,
                            PhysicalTensorCodec::Dense {
                                dtype: TensorScalarDType::F32
                            }
                        )
                        && matches!(
                            output.codec,
                            PhysicalTensorCodec::Ggml {
                                wire_type_id: 0 | 1 | 30,
                                ..
                            }
                        )
                        && input.shape == output.shape
                })
            }
            TensorTransformOperation::GgufDequantize { .. } => {
                single_input_output().is_some_and(|(input, output)| {
                    input_role_set == ["stored"].into()
                        && output_role_set == ["loaded"].into()
                        && input.shape == output.shape
                        && matches!(input.codec, PhysicalTensorCodec::Ggml { .. })
                        && matches!(
                            output.codec,
                            PhysicalTensorCodec::Dense {
                                dtype: TensorScalarDType::F32
                            }
                        )
                        && input.logical_f32_sha256 == output.logical_f32_sha256
                })
            }
            TensorTransformOperation::DirectBlockLoad | TensorTransformOperation::RuntimeBind => {
                single_input_output().is_some_and(|(input, output)| {
                    let roles_match = match &edge.operation {
                        TensorTransformOperation::DirectBlockLoad => {
                            input_role_set == ["stored"].into()
                                && output_role_set == ["loaded"].into()
                        }
                        TensorTransformOperation::RuntimeBind => {
                            input_role_set == ["loaded"].into()
                                && output_role_set == ["executed"].into()
                        }
                        _ => unreachable!(),
                    };
                    roles_match
                        && input.shape == output.shape
                        && input.codec == output.codec
                        && input.byte_len == output.byte_len
                        && input.byte_sha256 == output.byte_sha256
                        && input.logical_f32_sha256 == output.logical_f32_sha256
                })
            }
            TensorTransformOperation::SplitInterleavedQGate {
                heads,
                head_dim,
                hidden_size,
                ..
            } => {
                let projection_rows = u64::from(*heads).checked_mul(u64::from(*head_dim));
                let fused_rows = projection_rows.and_then(|rows| rows.checked_mul(2));
                input_nodes.len() == 1
                    && output_nodes.len() == 2
                    && input_role_set == ["loaded"].into()
                    && output_role_set == ["gate", "q"].into()
                    && projection_rows.is_some_and(|rows| {
                        rows > 0
                            && fused_rows.is_some_and(|fused_rows| {
                                input_nodes[0].shape == [fused_rows, u64::from(*hidden_size)]
                            })
                            && output_nodes
                                .iter()
                                .all(|node| node.shape == [rows, u64::from(*hidden_size)])
                    })
                    && input_nodes.iter().chain(&output_nodes).all(|node| {
                        matches!(
                            node.codec,
                            PhysicalTensorCodec::Dense {
                                dtype: TensorScalarDType::F32
                            }
                        )
                    })
            }
            TensorTransformOperation::Transpose { permutation } => single_input_output()
                .is_some_and(|(input, output)| {
                    permutation.len() == input.shape.len()
                        && permutation.len() == output.shape.len()
                        && permutation
                            .iter()
                            .enumerate()
                            .all(|(output_axis, input_axis)| {
                                usize::try_from(*input_axis).is_ok_and(|input_axis| {
                                    input_axis < input.shape.len()
                                        && output.shape[output_axis] == input.shape[input_axis]
                                })
                            })
                        && input.codec == output.codec
                }),
            TensorTransformOperation::Squeeze { axis } => {
                single_input_output().is_some_and(|(input, output)| {
                    usize::try_from(*axis).is_ok_and(|axis| {
                        axis < input.shape.len()
                            && input.shape[axis] == 1
                            && input.shape.len() == output.shape.len() + 1
                            && input
                                .shape
                                .iter()
                                .enumerate()
                                .filter_map(|(index, dimension)| {
                                    (index != axis).then_some(*dimension)
                                })
                                .eq(output.shape.iter().copied())
                            && matches!(
                                input.codec,
                                PhysicalTensorCodec::Dense {
                                    dtype: TensorScalarDType::F32
                                }
                            )
                            && input.codec == output.codec
                            && input.logical_f32_sha256 == output.logical_f32_sha256
                    })
                })
            }
            TensorTransformOperation::ZeroPad {
                axis,
                before,
                after,
            } => single_input_output().is_some_and(|(input, output)| {
                usize::try_from(*axis).is_ok_and(|axis| {
                    axis < input.shape.len()
                        && input.shape.len() == output.shape.len()
                        && input.codec == output.codec
                        && input.shape.iter().zip(&output.shape).enumerate().all(
                            |(index, (input_dim, output_dim))| {
                                if index == axis {
                                    input_dim
                                        .checked_add(*before)
                                        .and_then(|value| value.checked_add(*after))
                                        == Some(*output_dim)
                                } else {
                                    input_dim == output_dim
                                }
                            },
                        )
                })
            }),
            TensorTransformOperation::Qwen35LoadQ4Amax7V1 => {
                input_nodes.len() == 1
                    && output_nodes.len() == 1
                    && input_role_set == ["loaded"].into()
                    && output_role_set == ["executed"].into()
                    && input_nodes.iter().all(|node| {
                        matches!(
                            node.codec,
                            PhysicalTensorCodec::Dense {
                                dtype: TensorScalarDType::F32
                            }
                        )
                    })
                    && output_nodes.iter().all(|output| {
                        matches!(
                            &output.codec,
                            PhysicalTensorCodec::Ggml {
                                wire_type_id: 2,
                                type_name
                            } if type_name == "Q4_0"
                        ) && input_nodes[0].shape == output.shape
                    })
            }
        };
        if !geometry_valid {
            return Err(invalid(format!(
                "edge `{}` has invalid codec or geometry semantics",
                edge.edge_id
            )));
        }
        for output_port in &edge.outputs {
            if produced_by
                .insert(output_port.node_id.as_str(), edge.edge_id.as_str())
                .is_some()
            {
                return Err(invalid(format!(
                    "edge `{}` has a multiply-produced output",
                    edge.edge_id
                )));
            }
            edge_inputs_by_output.insert(
                output_port.node_id.as_str(),
                edge.inputs
                    .iter()
                    .map(|port| port.node_id.as_str())
                    .collect(),
            );
        }
        match &edge.operation {
            TensorTransformOperation::ArchitectureBake {
                operation,
                parameters_sha256,
            } if operation.is_empty() || !is_sha256(parameters_sha256) => {
                return Err(invalid(format!(
                    "edge `{}` has invalid bake evidence",
                    edge.edge_id
                )));
            }
            TensorTransformOperation::GgufQuantize {
                implementation_id,
                calibration_receipt_sha256,
            } if implementation_id.is_empty()
                || calibration_receipt_sha256
                    .as_ref()
                    .is_some_and(|digest| !is_sha256(digest)) =>
            {
                return Err(invalid(format!(
                    "edge `{}` has invalid quantization evidence",
                    edge.edge_id
                )));
            }
            TensorTransformOperation::GgufDenseStore { implementation_id }
                if implementation_id.is_empty() =>
            {
                return Err(invalid(format!(
                    "edge `{}` has invalid dense-store evidence",
                    edge.edge_id
                )));
            }
            TensorTransformOperation::GgufDequantize { implementation_id }
            | TensorTransformOperation::SplitInterleavedQGate {
                implementation_id, ..
            } if implementation_id.is_empty() => {
                return Err(invalid(format!(
                    "edge `{}` has no implementation identity",
                    edge.edge_id
                )));
            }
            TensorTransformOperation::Transpose { permutation }
                if permutation.is_empty()
                    || permutation.iter().copied().collect::<BTreeSet<_>>().len()
                        != permutation.len() =>
            {
                return Err(invalid(format!(
                    "edge `{}` has an invalid permutation",
                    edge.edge_id
                )));
            }
            TensorTransformOperation::ZeroPad { before, after, .. }
                if *before == 0 && *after == 0 =>
            {
                return Err(invalid(format!("edge `{}` has an empty pad", edge.edge_id)));
            }
            _ => {}
        }
    }
    // The converter's quantized path rounds converted F32 through F16
    // immediately before quantization. Dense F32/F16/BF16 stores encode
    // directly from converted F32 and therefore must not invent this node.
    for edge in &manifest.transforms {
        if matches!(
            edge.operation,
            TensorTransformOperation::GgufQuantize { .. }
        ) {
            let input = edge.inputs.first().expect("store arity validated above");
            let Some(producer_id) = produced_by.get(input.node_id.as_str()) else {
                return Err(invalid(format!(
                    "edge `{}` does not consume a canonical F16 roundtrip",
                    edge.edge_id
                )));
            };
            let producer = manifest
                .transforms
                .iter()
                .find(|candidate| candidate.edge_id == **producer_id)
                .expect("producer id came from the manifest");
            if !matches!(producer.operation, TensorTransformOperation::F16Roundtrip) {
                return Err(invalid(format!(
                    "edge `{}` does not consume a canonical F16 roundtrip",
                    edge.edge_id
                )));
            }
        }
    }
    for node in manifest
        .nodes
        .iter()
        .filter(|node| node.stage != TensorStateStage::Source)
    {
        if !produced_by.contains_key(node.node_id.as_str()) {
            return Err(invalid(format!(
                "non-source node `{}` has no producing edge",
                node.node_id
            )));
        }
    }

    let mut adjacency: BTreeMap<&str, Vec<&str>> = BTreeMap::new();
    let mut indegree: BTreeMap<&str, usize> = manifest
        .nodes
        .iter()
        .map(|node| (node.node_id.as_str(), 0))
        .collect();
    for edge in &manifest.transforms {
        for input in &edge.inputs {
            for output in &edge.outputs {
                adjacency
                    .entry(input.node_id.as_str())
                    .or_default()
                    .push(output.node_id.as_str());
                *indegree
                    .get_mut(output.node_id.as_str())
                    .expect("edge output was validated above") += 1;
            }
        }
    }
    let mut ready: Vec<_> = indegree
        .iter()
        .filter_map(|(node_id, count)| (*count == 0).then_some(*node_id))
        .collect();
    let mut visited_count = 0usize;
    while let Some(node_id) = ready.pop() {
        visited_count += 1;
        for output in adjacency.get(node_id).into_iter().flatten() {
            let count = indegree
                .get_mut(output)
                .expect("edge output was validated above");
            *count -= 1;
            if *count == 0 {
                ready.push(output);
            }
        }
    }
    if visited_count != manifest.nodes.len() {
        return Err(invalid("tensor execution graph contains a cycle".into()));
    }

    let ancestor_stages = |node_id: &str| {
        let mut stages = BTreeSet::new();
        let mut pending = vec![node_id];
        let mut visited = BTreeSet::new();
        while let Some(current) = pending.pop() {
            if !visited.insert(current) {
                continue;
            }
            stages.insert(nodes[current].stage);
            if let Some(inputs) = edge_inputs_by_output.get(current) {
                pending.extend(inputs.iter().copied());
            }
        }
        stages
    };
    for node in &manifest.nodes {
        let stages = ancestor_stages(node.node_id.as_str());
        let valid = match node.stage {
            TensorStateStage::Source => true,
            TensorStateStage::Converted | TensorStateStage::Stored => {
                stages.contains(&TensorStateStage::Source)
            }
            TensorStateStage::Loaded => {
                stages.contains(&TensorStateStage::Source)
                    && stages.contains(&TensorStateStage::Stored)
            }
            TensorStateStage::Executed => {
                stages.contains(&TensorStateStage::Source)
                    && stages.contains(&TensorStateStage::Stored)
                    && stages.contains(&TensorStateStage::Loaded)
            }
        };
        if !valid {
            return Err(invalid(format!(
                "node `{}` does not have the required physical ancestry",
                node.node_id
            )));
        }
    }

    let mut binding_ids = BTreeSet::new();
    let mut consumed_executed_nodes = BTreeSet::new();
    for operation in &manifest.operations {
        if operation.binding_id.is_empty()
            || operation.operation_id.is_empty()
            || operation.graph_path.is_empty()
            || operation.entrypoint.is_empty()
            || operation.source_tensor_names.is_empty()
            || operation.inputs.is_empty()
            || !binding_ids.insert(operation.binding_id.as_str())
        {
            return Err(invalid(
                "runtime operation is incomplete or duplicated".into(),
            ));
        }
        let executed_ids: BTreeSet<_> = operation.inputs.iter().map(|port| &port.node_id).collect();
        let source_names: BTreeSet<&str> = operation
            .source_tensor_names
            .iter()
            .map(String::as_str)
            .collect();
        let input_roles: BTreeSet<_> = operation.inputs.iter().map(|port| &port.role).collect();
        if operation.inputs.iter().any(|port| port.role.is_empty())
            || operation.source_tensor_names.iter().any(String::is_empty)
            || source_names.len() != operation.source_tensor_names.len()
            || executed_ids.len() != operation.inputs.len()
            || input_roles.len() != operation.inputs.len()
            || operation.inputs.iter().any(|port| {
                nodes
                    .get(port.node_id.as_str())
                    .is_none_or(|node| node.stage != TensorStateStage::Executed)
            })
        {
            return Err(invalid(format!(
                "operation `{}` does not bind unique executed nodes",
                operation.operation_id
            )));
        }
        let mut source_closure = BTreeSet::new();
        let mut pending: Vec<_> = operation
            .inputs
            .iter()
            .map(|port| port.node_id.as_str())
            .collect();
        let mut visited = BTreeSet::new();
        while let Some(node_id) = pending.pop() {
            if !visited.insert(node_id) {
                continue;
            }
            let node = nodes[node_id];
            if node.stage == TensorStateStage::Source {
                source_closure.insert(node.semantic_name.as_str());
            } else if let Some(inputs) = edge_inputs_by_output.get(node_id) {
                pending.extend(inputs.iter().copied());
            }
        }
        if source_closure != source_names {
            return Err(invalid(format!(
                "operation `{}` has the wrong source-tensor closure",
                operation.operation_id
            )));
        }
        consumed_executed_nodes.extend(operation.inputs.iter().map(|port| port.node_id.as_str()));
        let ggml_input_count = operation
            .inputs
            .iter()
            .filter(|port| {
                matches!(
                    nodes[port.node_id.as_str()].codec,
                    PhysicalTensorCodec::Ggml { .. }
                )
            })
            .count();
        let has_ggml_input = ggml_input_count > 0;
        match &operation.capability {
            RuntimeCapabilityEvidence::Ggml {
                request,
                decision,
                requires_device_probe,
                resolved_runtime_trace,
            } => {
                if ggml_input_count != operation.inputs.len() {
                    return Err(invalid(format!(
                        "operation `{}` claims GGML capability without all-GGML inputs",
                        operation.operation_id
                    )));
                }
                validate_json_evidence(request).map_err(invalid)?;
                validate_json_evidence(decision).map_err(invalid)?;
                if *requires_device_probe && resolved_runtime_trace.is_none() {
                    return Err(invalid(format!(
                        "operation `{}` has no required device-probe evidence",
                        operation.operation_id
                    )));
                }
                if let Some(trace) = resolved_runtime_trace {
                    validate_json_evidence(trace).map_err(invalid)?;
                }
            }
            RuntimeCapabilityEvidence::NonGgml {
                implementation_id,
                contract_sha256,
                resolved_runtime_trace,
            } => {
                if has_ggml_input || implementation_id.is_empty() || !is_sha256(contract_sha256) {
                    return Err(invalid(format!(
                        "operation `{}` has invalid non-GGML evidence",
                        operation.operation_id
                    )));
                }
                if let Some(trace) = resolved_runtime_trace {
                    validate_json_evidence(trace).map_err(invalid)?;
                }
            }
        }
        if operation.invocation_count == 0 {
            return Err(invalid(format!(
                "operation `{}` has zero invocations",
                operation.operation_id
            )));
        }
    }
    let all_executed_nodes: BTreeSet<_> = manifest
        .nodes
        .iter()
        .filter(|node| node.stage == TensorStateStage::Executed)
        .map(|node| node.node_id.as_str())
        .collect();
    if all_executed_nodes != consumed_executed_nodes {
        return Err(invalid(
            "every executed tensor must be consumed by a runtime operation".into(),
        ));
    }

    let source_ids: BTreeSet<_> = manifest
        .nodes
        .iter()
        .filter(|node| node.stage == TensorStateStage::Source)
        .map(|node| node.node_id.as_str())
        .collect();
    let mut disposition_ids = BTreeSet::new();
    for disposition in &manifest.dispositions {
        let terminal_ids: BTreeSet<&str> = disposition
            .terminal_node_ids
            .iter()
            .map(String::as_str)
            .collect();
        if disposition.reason.is_empty()
            || !source_ids.contains(disposition.source_node_id.as_str())
            || !disposition_ids.insert(disposition.source_node_id.as_str())
            || terminal_ids.len() != disposition.terminal_node_ids.len()
            || disposition
                .terminal_node_ids
                .iter()
                .any(|node_id| !nodes.contains_key(node_id.as_str()))
            || (matches!(
                disposition.disposition,
                SourceTensorDispositionKind::Variable | SourceTensorDispositionKind::Fixed
            ) && disposition.terminal_node_ids.is_empty())
            || (matches!(
                disposition.disposition,
                SourceTensorDispositionKind::Excluded
            ) && !disposition.terminal_node_ids.is_empty())
        {
            return Err(invalid(
                "source disposition is incomplete or duplicated".into(),
            ));
        }
        if disposition
            .terminal_node_ids
            .iter()
            .any(|node_id| nodes[node_id.as_str()].stage != TensorStateStage::Executed)
        {
            return Err(invalid(
                "source dispositions may terminate only at executed tensors".into(),
            ));
        }
        let mut reachable_executed = BTreeSet::new();
        let mut pending = vec![disposition.source_node_id.as_str()];
        let mut reachable = BTreeSet::new();
        while let Some(node_id) = pending.pop() {
            if !reachable.insert(node_id) {
                continue;
            }
            if nodes[node_id].stage == TensorStateStage::Executed {
                reachable_executed.insert(node_id);
            }
            if let Some(outputs) = adjacency.get(node_id) {
                pending.extend(outputs.iter().copied());
            }
        }
        if terminal_ids != reachable_executed {
            return Err(invalid(format!(
                "source `{}` disposition does not cover its exact executed descendants",
                disposition.source_node_id
            )));
        }
        for terminal_id in &disposition.terminal_node_ids {
            let mut pending = vec![terminal_id.as_str()];
            let mut ancestors = BTreeSet::new();
            while let Some(node_id) = pending.pop() {
                if ancestors.insert(node_id) {
                    if let Some(inputs) = edge_inputs_by_output.get(node_id) {
                        pending.extend(inputs.iter().copied());
                    }
                }
            }
            if !ancestors.contains(disposition.source_node_id.as_str()) {
                return Err(invalid(format!(
                    "terminal `{terminal_id}` is not reachable from source `{}`",
                    disposition.source_node_id
                )));
            }
        }
    }
    if source_ids != disposition_ids {
        return Err(invalid(
            "every source tensor must have exactly one disposition".into(),
        ));
    }

    if !is_sha256(&manifest.manifest_sha256)
        || tensor_execution_manifest_sha256(manifest)? != manifest.manifest_sha256
    {
        return Err(TensorExecutionManifestError::DigestMismatch);
    }
    Ok(ValidatedTensorExecutionManifest {
        manifest: normalize_manifest(manifest),
    })
}