phon-engine 0.2.0-rc.0

phon's backend-blind baseline: compact codec, compatibility planning, and the IR interpreter.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
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
//! Compatibility planning: translate a *writer* schema with a *reader* schema
//! into a [`Plan`], then decode the writer's compact bytes into a
//! reader-shaped [`Value`].
//!
//! The plan is built from the two schemas alone, before any payload is touched
//! (`r[compat.plan-first]`): if it cannot be built the schemas are incompatible
//! and decoding never begins. Struct fields are matched by name
//! (`r[compat.field-matching]`) — writer-only fields are skipped
//! (`r[compat.skip-writer-only]`), reader-only fields are defaulted or, when
//! required, fail the plan (`r[compat.reader-only-fields]`). Enum variants are
//! matched by name (`r[compat.enum]`). Types match only by the rules of
//! `r[compat.type-match]` — no implicit numeric widening.
//!
//! This is the dynamic-`Value` path: reader-only fields default to `null` (the
//! typed path will use a descriptor-supplied default instead). It builds on the
//! compact codec's registry, resolution, and decoders.
//!
//! Spec: "Compatibility".

use std::collections::{BTreeMap, HashMap, HashSet};

use facet_value::{VArray, VObject, VString, Value};
use phon_schema::bytes::Reader;
use phon_schema::{
    DecodeError, Field, Primitive, SchemaId, SchemaKind, SchemaRef, Variant, VariantPayload,
    read_value,
};

use phon_ir::ir::{EnumArm, Op, Program, ValueProgram};

use crate::compact::{self, CompactError, Registry, Resolved};
use crate::compat::{self, FieldMatch, VariantMatch, incompatible};

type Result<T> = core::result::Result<T, CompactError>;

const MAX_DEPTH: usize = 128;

// ============================================================================
// Plan
// ============================================================================

/// A built translation plan from a writer schema to a reader schema. Build it
/// once with [`build_plan`], then decode many messages with [`decode_with_plan`].
pub struct Plan {
    root: Node,
    blocks: BTreeMap<SchemaId, Node>,
}

enum Node {
    /// A primitive copied through (writer and reader primitive are identical).
    Scalar(Primitive),
    /// A back-edge into a recursive reader schema, resolved through
    /// [`Plan::blocks`] at execution/lowering time.
    CallBlock(SchemaId),
    Struct(StructPlan),
    /// Writer variant index -> how to produce the reader's variant. An index
    /// absent here is a writer-only variant: a decode error if it arrives.
    Enum(HashMap<u32, VariantPlan>),
    Tuple(Vec<Node>),
    /// A `list` (`set == false`) or `set` (`set == true`). `min_wire` is the
    /// element's minimum wire size for the `r[validate.lengths]` count guard —
    /// `0` for a zero-sized element (an empty struct, `unit`, …), else `1`.
    Seq {
        set: bool,
        element: Box<Node>,
        min_wire: usize,
    },
    Map {
        key: Box<Node>,
        value: Box<Node>,
    },
    /// A fixed-shape array. `min_wire` bounds `product(dims)` exactly as in `Seq`.
    Array {
        element: Box<Node>,
        dims: Vec<u64>,
        min_wire: usize,
    },
    Option(Box<Node>),
    Dynamic,
}

struct StructPlan {
    /// One step per writer field, in wire order.
    steps: Vec<Step>,
    /// Reader-only, non-required field names to fill with a default.
    defaults: Vec<String>,
}

enum Step {
    /// A writer field matched to this reader field; decode it with `node`.
    Take { reader: String, node: Node },
    /// A writer-only field: decode it by its writer schema and discard.
    Skip(SchemaRef),
}

struct VariantPlan {
    reader: String,
    payload: Payload,
}

enum Payload {
    Unit,
    Newtype(Box<Node>),
    Tuple(Vec<Node>),
    Struct(StructPlan),
}

struct RecCtx {
    recursive: HashSet<SchemaId>,
    blocks: BTreeMap<SchemaId, Node>,
    building: HashSet<SchemaId>,
}

// ============================================================================
// Public API
// ============================================================================

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum CompatDirection {
    /// The newer schema can read bytes written by the older schema.
    Backward,
    /// The older schema can read bytes written by the newer schema.
    Forward,
    /// Both schema versions can read each other's bytes.
    Bidirectional,
    /// Neither schema version can read the other's bytes.
    Incompatible,
}

/// Build the translation plan from `writer_root` to `reader_root`.
///
/// # Errors
/// [`CompactError::Incompatible`] (or a resolution error) if the schemas cannot
/// be translated.
// r[impl compat.plan-first]
pub fn build_plan(writer_root: SchemaId, reader_root: SchemaId, reg: &Registry) -> Result<Plan> {
    let mut rec = RecCtx {
        recursive: recursive_schema_ids(reader_root, reg),
        blocks: BTreeMap::new(),
        building: HashSet::new(),
    };
    let root = plan_ref(
        &SchemaRef::concrete(writer_root),
        &SchemaRef::concrete(reader_root),
        reg,
        &mut rec,
        0,
    )?;
    Ok(Plan {
        root,
        blocks: rec.blocks,
    })
}

/// Classify compatibility between an older and newer schema by planning both
/// directions. This is tooling over [`build_plan`], not a decode path.
// r[impl compat.direction]
#[must_use]
pub fn compatibility_direction(
    older_root: SchemaId,
    newer_root: SchemaId,
    reg: &Registry,
) -> CompatDirection {
    let backward = build_plan(older_root, newer_root, reg).is_ok();
    let forward = build_plan(newer_root, older_root, reg).is_ok();
    match (backward, forward) {
        (true, true) => CompatDirection::Bidirectional,
        (true, false) => CompatDirection::Backward,
        (false, true) => CompatDirection::Forward,
        (false, false) => CompatDirection::Incompatible,
    }
}

/// Decode writer compact `bytes` into a reader-shaped value using a prebuilt plan.
///
/// # Errors
/// [`CompactError`] for malformed input, or a writer-only enum variant.
pub fn decode_with_plan(bytes: &[u8], plan: &Plan, reg: &Registry) -> Result<Value> {
    let mut r = Reader::new(bytes);
    let v = exec(&plan.root, &mut r, reg, &plan.blocks, 0)?;
    if r.remaining() != 0 {
        return Err(CompactError::Decode(DecodeError::TrailingBytes(
            r.remaining(),
        )));
    }
    Ok(v)
}

/// Build a plan and decode in one step.
///
/// # Errors
/// As [`build_plan`] and [`decode_with_plan`].
pub fn decode(
    bytes: &[u8],
    writer_root: SchemaId,
    reader_root: SchemaId,
    reg: &Registry,
) -> Result<Value> {
    let plan = build_plan(writer_root, reader_root, reg)?;
    decode_with_plan(bytes, &plan, reg)
}

/// Build a plan, lower it to the linear IR, and run the interpreter — the flat
/// counterpart to [`decode`]. The interpreter must produce the same value the
/// recursive [`decode_with_plan`] would; the compat tests assert exactly that.
///
/// # Errors
/// As [`build_plan`] and [`crate::interp::run`].
pub fn decode_via_ir(
    bytes: &[u8],
    writer_root: SchemaId,
    reader_root: SchemaId,
    reg: &Registry,
) -> Result<Value> {
    let plan = build_plan(writer_root, reader_root, reg)?;
    let program = lower(&plan);
    crate::interp::run_lowered(&program, bytes, reg)
}

// ============================================================================
// Building the plan
// ============================================================================

fn plan_ref(
    w: &SchemaRef,
    r: &SchemaRef,
    reg: &Registry,
    rec: &mut RecCtx,
    depth: usize,
) -> Result<Node> {
    if depth > MAX_DEPTH {
        return Err(incompatible("schema nests too deep"));
    }
    if let SchemaRef::Concrete { id, .. } = r
        && rec.recursive.contains(id)
    {
        if !rec.blocks.contains_key(id) && !rec.building.contains(id) {
            rec.building.insert(*id);
            let body = plan_resolved(
                compact::resolve(reg, w)?,
                compact::resolve(reg, r)?,
                reg,
                rec,
                depth,
            )?;
            rec.building.remove(id);
            rec.blocks.insert(*id, body);
        }
        return Ok(Node::CallBlock(*id));
    }
    plan_resolved(
        compact::resolve(reg, w)?,
        compact::resolve(reg, r)?,
        reg,
        rec,
        depth,
    )
}

fn plan_resolved(
    w: Resolved,
    r: Resolved,
    reg: &Registry,
    rec: &mut RecCtx,
    depth: usize,
) -> Result<Node> {
    match (w, r) {
        (Resolved::Primitive(wp), Resolved::Primitive(rp)) => {
            if wp == rp {
                Ok(Node::Scalar(wp))
            } else {
                Err(incompatible(format!("primitive {wp:?} is not {rp:?}")))
            }
        }
        (Resolved::Composite(wk), Resolved::Composite(rk)) => plan_kind(wk, rk, reg, rec, depth),
        _ => Err(incompatible("primitive does not match composite")),
    }
}

// r[impl compat.type-match]
fn plan_kind(
    wk: SchemaKind,
    rk: SchemaKind,
    reg: &Registry,
    rec: &mut RecCtx,
    depth: usize,
) -> Result<Node> {
    match (wk, rk) {
        (SchemaKind::Primitive(wp), SchemaKind::Primitive(rp)) => {
            if wp == rp {
                Ok(Node::Scalar(wp))
            } else {
                Err(incompatible(format!("primitive {wp:?} is not {rp:?}")))
            }
        }
        (SchemaKind::Struct { fields: wf, .. }, SchemaKind::Struct { fields: rf, .. }) => {
            Ok(Node::Struct(plan_struct(&wf, &rf, reg, rec, depth)?))
        }
        (SchemaKind::Enum { variants: wv, .. }, SchemaKind::Enum { variants: rv, .. }) => {
            plan_enum(&wv, &rv, reg, rec, depth)
        }
        (SchemaKind::Tuple { elements: we }, SchemaKind::Tuple { elements: re }) => {
            if we.len() != re.len() {
                return Err(incompatible("tuple arity differs"));
            }
            let mut nodes = Vec::with_capacity(we.len());
            for (w, r) in we.iter().zip(&re) {
                nodes.push(plan_ref(w, r, reg, rec, depth + 1)?);
            }
            Ok(Node::Tuple(nodes))
        }
        (SchemaKind::List { element: we }, SchemaKind::List { element: re }) => Ok(Node::Seq {
            set: false,
            min_wire: compact::min_wire_size_ref(reg, &we),
            element: Box::new(plan_ref(&we, &re, reg, rec, depth + 1)?),
        }),
        (SchemaKind::Set { element: we }, SchemaKind::Set { element: re }) => Ok(Node::Seq {
            set: true,
            min_wire: compact::min_wire_size_ref(reg, &we),
            element: Box::new(plan_ref(&we, &re, reg, rec, depth + 1)?),
        }),
        (SchemaKind::Option { element: we }, SchemaKind::Option { element: re }) => Ok(
            Node::Option(Box::new(plan_ref(&we, &re, reg, rec, depth + 1)?)),
        ),
        (SchemaKind::Map { key: wk, value: wv }, SchemaKind::Map { key: rk, value: rv }) => {
            Ok(Node::Map {
                key: Box::new(plan_ref(&wk, &rk, reg, rec, depth + 1)?),
                value: Box::new(plan_ref(&wv, &rv, reg, rec, depth + 1)?),
            })
        }
        (
            SchemaKind::Array {
                element: we,
                dimensions: wd,
            },
            SchemaKind::Array {
                element: re,
                dimensions: rd,
            },
        ) => {
            if wd != rd {
                return Err(incompatible("array dimensions differ"));
            }
            Ok(Node::Array {
                min_wire: compact::min_wire_size_ref(reg, &we),
                element: Box::new(plan_ref(&we, &re, reg, rec, depth + 1)?),
                dims: wd,
            })
        }
        (SchemaKind::Dynamic, SchemaKind::Dynamic) => Ok(Node::Dynamic),
        (SchemaKind::Tensor { .. }, SchemaKind::Tensor { .. }) => {
            Err(CompactError::Unsupported("tensor"))
        }
        (SchemaKind::Channel { .. }, SchemaKind::Channel { .. }) => {
            Err(CompactError::Unsupported("channel"))
        }
        (SchemaKind::External { .. }, SchemaKind::External { .. }) => {
            Err(CompactError::Unsupported("external"))
        }
        _ => Err(incompatible("schema kinds differ")),
    }
}

// r[impl compat.field-matching]
// r[impl compat.skip-writer-only]
// r[impl compat.reader-only-fields]
// r[impl compat.defaults-are-reader-side]
fn plan_struct(
    w_fields: &[Field],
    r_fields: &[Field],
    reg: &Registry,
    rec: &mut RecCtx,
    depth: usize,
) -> Result<StructPlan> {
    let mut steps = Vec::with_capacity(w_fields.len());
    let mut defaults = Vec::new();
    for step in compat::match_fields(
        w_fields,
        r_fields,
        |_, rf| !rf.required,
        |rf| {
            incompatible(format!(
                "required reader field '{}' is absent from the writer",
                rf.name
            ))
        },
    )? {
        match step {
            FieldMatch::Take {
                writer,
                reader_index,
            } => {
                let rf = &r_fields[reader_index];
                let node = plan_ref(&writer.schema, &rf.schema, reg, rec, depth + 1)?;
                steps.push(Step::Take {
                    reader: rf.name.clone(),
                    node,
                });
            }
            FieldMatch::Skip { writer } => steps.push(Step::Skip(writer.schema.clone())),
            FieldMatch::Default { reader_index } => {
                defaults.push(r_fields[reader_index].name.clone());
            }
        }
    }

    Ok(StructPlan { steps, defaults })
}

// r[impl compat.enum]
fn plan_enum(
    w_variants: &[Variant],
    r_variants: &[Variant],
    reg: &Registry,
    rec: &mut RecCtx,
    depth: usize,
) -> Result<Node> {
    let mut by_index = HashMap::new();
    for step in compat::match_variants(w_variants, r_variants) {
        let VariantMatch::Take {
            writer,
            reader_index,
        } = step
        else {
            continue;
        };
        let rv = &r_variants[reader_index];
        let payload = plan_payload(&writer.payload, &rv.payload, reg, rec, depth)?;
        by_index.insert(
            writer.index,
            VariantPlan {
                reader: rv.name.clone(),
                payload,
            },
        );
    }
    Ok(Node::Enum(by_index))
}

fn plan_payload(
    w: &VariantPayload,
    r: &VariantPayload,
    reg: &Registry,
    rec: &mut RecCtx,
    depth: usize,
) -> Result<Payload> {
    match (w, r) {
        (VariantPayload::Unit, VariantPayload::Unit) => Ok(Payload::Unit),
        (VariantPayload::Newtype(wr), VariantPayload::Newtype(rr)) => Ok(Payload::Newtype(
            Box::new(plan_ref(wr, rr, reg, rec, depth + 1)?),
        )),
        (VariantPayload::Tuple(wrs), VariantPayload::Tuple(rrs)) => {
            if wrs.len() != rrs.len() {
                return Err(incompatible("variant tuple arity differs"));
            }
            let mut nodes = Vec::with_capacity(wrs.len());
            for (w, r) in wrs.iter().zip(rrs) {
                nodes.push(plan_ref(w, r, reg, rec, depth + 1)?);
            }
            Ok(Payload::Tuple(nodes))
        }
        (VariantPayload::Struct(wfs), VariantPayload::Struct(rfs)) => {
            Ok(Payload::Struct(plan_struct(wfs, rfs, reg, rec, depth)?))
        }
        _ => Err(incompatible("variant payload shapes differ")),
    }
}

fn recursive_schema_ids(root: SchemaId, reg: &Registry) -> HashSet<SchemaId> {
    let mut recursive = HashSet::new();
    let mut visited = HashSet::new();
    let mut on_stack = HashSet::new();
    let mut stack = Vec::new();
    visit_schema(
        root,
        reg,
        &mut recursive,
        &mut visited,
        &mut on_stack,
        &mut stack,
    );
    recursive
}

fn visit_schema(
    id: SchemaId,
    reg: &Registry,
    recursive: &mut HashSet<SchemaId>,
    visited: &mut HashSet<SchemaId>,
    on_stack: &mut HashSet<SchemaId>,
    stack: &mut Vec<SchemaId>,
) {
    visited.insert(id);
    on_stack.insert(id);
    stack.push(id);

    if let Ok(Resolved::Composite(kind)) = compact::resolve(reg, &SchemaRef::concrete(id)) {
        for target in ref_targets(&kind) {
            if on_stack.contains(&target) {
                for &candidate in stack.iter().rev() {
                    recursive.insert(candidate);
                    if candidate == target {
                        break;
                    }
                }
            } else if !visited.contains(&target) {
                visit_schema(target, reg, recursive, visited, on_stack, stack);
            }
        }
    }

    stack.pop();
    on_stack.remove(&id);
}

fn ref_targets(kind: &SchemaKind) -> Vec<SchemaId> {
    let mut out = Vec::new();
    match kind {
        SchemaKind::Struct { fields, .. } => {
            for f in fields {
                add_ref_targets(&f.schema, &mut out);
            }
        }
        SchemaKind::Enum { variants, .. } => {
            for v in variants {
                match &v.payload {
                    VariantPayload::Unit => {}
                    VariantPayload::Newtype(r) => add_ref_targets(r, &mut out),
                    VariantPayload::Tuple(refs) => {
                        for r in refs {
                            add_ref_targets(r, &mut out);
                        }
                    }
                    VariantPayload::Struct(fields) => {
                        for f in fields {
                            add_ref_targets(&f.schema, &mut out);
                        }
                    }
                }
            }
        }
        SchemaKind::Tuple { elements } => {
            for r in elements {
                add_ref_targets(r, &mut out);
            }
        }
        SchemaKind::List { element }
        | SchemaKind::Set { element }
        | SchemaKind::Array { element, .. }
        | SchemaKind::Tensor { element, .. }
        | SchemaKind::Option { element }
        | SchemaKind::Channel { element, .. } => add_ref_targets(element, &mut out),
        SchemaKind::Map { key, value } => {
            add_ref_targets(key, &mut out);
            add_ref_targets(value, &mut out);
        }
        SchemaKind::External { metadata, .. } => {
            if let Some(r) = metadata {
                add_ref_targets(r, &mut out);
            }
        }
        SchemaKind::Primitive(_) | SchemaKind::Dynamic => {}
    }
    out
}

fn add_ref_targets(r: &SchemaRef, out: &mut Vec<SchemaId>) {
    if let SchemaRef::Concrete { id, args } = r {
        out.push(*id);
        for arg in args {
            add_ref_targets(arg, out);
        }
    }
}

// ============================================================================
// Lowering the plan to the linear IR
// ============================================================================

/// Flatten a built plan's `Node` tree into a linear [`Program`]. Every
/// type-directed decision the tree encodes is resolved here, once; what the
/// interpreter runs carries only data-directed control flow. A struct of structs
/// of scalars lowers to a single branch-free run of ops.
// r[impl ir.two-forms]
#[must_use]
pub fn lower(plan: &Plan) -> ValueProgram {
    let mut out = Vec::new();
    lower_node(&plan.root, &mut out);
    let blocks = plan
        .blocks
        .iter()
        .map(|(id, node)| (*id, lower_subtree(node)))
        .collect();
    ValueProgram {
        program: out,
        blocks,
    }
}

fn lower_subtree(node: &Node) -> Program {
    let mut out = Vec::new();
    lower_node(node, &mut out);
    out
}

/// Append the ops for `node`. A complete node nets one value on the stack.
fn lower_node(node: &Node, out: &mut Program) {
    match node {
        Node::Scalar(p) => out.push(Op::Scalar(*p)),
        Node::Dynamic => out.push(Op::Dynamic),
        Node::CallBlock(schema) => out.push(Op::CallBlock { schema: *schema }),
        Node::Struct(sp) => lower_struct(sp, out),
        Node::Enum(by_index) => {
            let mut arms: Vec<EnumArm> = by_index
                .iter()
                .map(|(idx, vp)| EnumArm {
                    writer_index: *idx,
                    reader_name: vp.reader.clone(),
                    payload: lower_payload(&vp.payload),
                })
                .collect();
            // Deterministic order; the interpreter dispatches by writer_index.
            arms.sort_by_key(|a| a.writer_index);
            out.push(Op::Enum { arms });
        }
        Node::Tuple(nodes) => {
            for n in nodes {
                lower_node(n, out);
            }
            out.push(Op::Array { count: nodes.len() });
        }
        Node::Seq {
            set,
            element,
            min_wire,
        } => out.push(Op::Seq {
            set: *set,
            min_wire: *min_wire,
            body: lower_subtree(element),
        }),
        Node::Map { key, value } => out.push(Op::Map {
            key: lower_subtree(key),
            value: lower_subtree(value),
        }),
        Node::Array {
            element,
            dims,
            min_wire,
        } => out.push(Op::FixedArray {
            dimensions: dims.clone(),
            min_wire: *min_wire,
            body: lower_subtree(element),
        }),
        Node::Option(element) => out.push(Op::Option {
            some: lower_subtree(element),
        }),
    }
}

/// Each writer field in wire order (a matched field's value, or a skip for a
/// writer-only one), then a null per reader-only default, then assemble the
/// object. The `keys` track the stack values the `Object` op will name.
///
/// A field that is itself a fixed structure splices its ops inline here (via
/// `lower_node`); only dynamic-length and branching children become sub-programs.
// r[impl ir.inlining]
fn lower_struct(sp: &StructPlan, out: &mut Program) {
    let mut keys = Vec::new();
    for step in &sp.steps {
        match step {
            Step::Take { reader, node } => {
                lower_node(node, out);
                keys.push(reader.clone());
            }
            Step::Skip(writer_ref) => out.push(Op::Skip(writer_ref.clone())),
        }
    }
    for name in &sp.defaults {
        out.push(Op::Null);
        keys.push(name.clone());
    }
    out.push(Op::Object { keys });
}

fn lower_payload(payload: &Payload) -> Program {
    let mut out = Vec::new();
    match payload {
        Payload::Unit => out.push(Op::Null),
        Payload::Newtype(node) => lower_node(node, &mut out),
        Payload::Tuple(nodes) => {
            for n in nodes {
                lower_node(n, &mut out);
            }
            out.push(Op::Array { count: nodes.len() });
        }
        Payload::Struct(sp) => lower_struct(sp, &mut out),
    }
    out
}

// ============================================================================
// Executing the plan
// ============================================================================

fn exec(
    node: &Node,
    r: &mut Reader,
    reg: &Registry,
    blocks: &BTreeMap<SchemaId, Node>,
    depth: usize,
) -> Result<Value> {
    if depth > MAX_DEPTH {
        return Err(CompactError::Decode(DecodeError::DepthExceeded));
    }
    match node {
        Node::Scalar(p) => compact::decode_primitive(r, *p),
        Node::CallBlock(schema) => {
            let block = blocks
                .get(schema)
                .ok_or(CompactError::Decode(DecodeError::Malformed(
                    "missing recursion block",
                )))?;
            exec(block, r, reg, blocks, depth + 1)
        }
        Node::Struct(sp) => exec_struct(sp, r, reg, blocks, depth),
        Node::Enum(by_index) => {
            let idx = r.read_u32()?;
            let v = by_index
                .get(&idx)
                .ok_or(CompactError::WriterOnlyVariant(idx))?;
            let payload = exec_payload(&v.payload, r, reg, blocks, depth)?;
            let mut obj = VObject::new();
            obj.insert(VString::new(&v.reader), payload);
            Ok(obj.into())
        }
        Node::Tuple(nodes) => {
            let mut a = VArray::new();
            for n in nodes {
                a.push(exec(n, r, reg, blocks, depth + 1)?);
            }
            Ok(a.into())
        }
        Node::Seq {
            set,
            element,
            min_wire,
        } => {
            let n = r.read_len(*min_wire)?;
            let mut a = VArray::new();
            let mut seen = if *set { Some(HashSet::new()) } else { None };
            for _ in 0..n {
                let v = exec(element, r, reg, blocks, depth + 1)?;
                if let Some(s) = &mut seen
                    && !s.insert(v.clone())
                {
                    return Err(CompactError::Decode(DecodeError::DuplicateElement));
                }
                a.push(v);
            }
            Ok(a.into())
        }
        Node::Map { key, value } => {
            let n = r.read_len(1)?;
            let mut obj = VObject::new();
            for _ in 0..n {
                let k = exec(key, r, reg, blocks, depth + 1)?;
                let v = exec(value, r, reg, blocks, depth + 1)?;
                let ks = k
                    .as_string()
                    .ok_or(CompactError::Unsupported("map with non-string keys"))?;
                if obj.insert(VString::new(ks.as_str()), v).is_some() {
                    return Err(CompactError::Decode(DecodeError::DuplicateKey));
                }
            }
            Ok(obj.into())
        }
        Node::Array {
            element,
            dims,
            min_wire,
        } => {
            let count = compact::product(dims)?;
            compact::check_fixed_count(count, *min_wire, r.remaining())?;
            let mut a = VArray::new();
            for _ in 0..count {
                a.push(exec(element, r, reg, blocks, depth + 1)?);
            }
            Ok(a.into())
        }
        Node::Option(element) => match r.read_u8()? {
            0 => Ok(Value::NULL),
            1 => exec(element, r, reg, blocks, depth + 1),
            b => Err(CompactError::Decode(DecodeError::InvalidBool(b))),
        },
        Node::Dynamic => Ok(read_value(r)?),
    }
}

fn exec_struct(
    sp: &StructPlan,
    r: &mut Reader,
    reg: &Registry,
    blocks: &BTreeMap<SchemaId, Node>,
    depth: usize,
) -> Result<Value> {
    let mut obj = VObject::new();
    for step in &sp.steps {
        match step {
            Step::Take { reader, node } => {
                let v = exec(node, r, reg, blocks, depth + 1)?;
                obj.insert(VString::new(reader), v);
            }
            Step::Skip(writer_ref) => {
                // Walk the writer field by its own schema and discard it.
                compact::decode_ref(r, writer_ref, reg, depth + 1)?;
            }
        }
    }
    for name in &sp.defaults {
        obj.insert(VString::new(name), Value::NULL);
    }
    Ok(obj.into())
}

fn exec_payload(
    p: &Payload,
    r: &mut Reader,
    reg: &Registry,
    blocks: &BTreeMap<SchemaId, Node>,
    depth: usize,
) -> Result<Value> {
    match p {
        Payload::Unit => Ok(Value::NULL),
        Payload::Newtype(n) => exec(n, r, reg, blocks, depth + 1),
        Payload::Tuple(ns) => {
            let mut a = VArray::new();
            for n in ns {
                a.push(exec(n, r, reg, blocks, depth + 1)?);
            }
            Ok(a.into())
        }
        Payload::Struct(sp) => exec_struct(sp, r, reg, blocks, depth),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::compact;
    use phon_schema::{Schema, primitive_id};

    fn prim(p: Primitive) -> SchemaRef {
        SchemaRef::concrete(primitive_id(p))
    }

    fn schema(id: u64, kind: SchemaKind) -> Schema {
        Schema {
            id: SchemaId(id),
            type_params: Vec::new(),
            kind,
        }
    }

    fn field(name: &str, schema: SchemaRef, required: bool) -> Field {
        Field {
            name: name.to_string(),
            schema,
            required,
        }
    }

    fn point_struct(id: u64, fields: Vec<Field>) -> Schema {
        schema(
            id,
            SchemaKind::Struct {
                name: "P".to_string(),
                fields,
            },
        )
    }

    fn obj(entries: &[(&str, Value)]) -> Value {
        let mut o = VObject::new();
        for (k, v) in entries {
            o.insert(VString::new(k), v.clone());
        }
        o.into()
    }

    /// Decode through both the recursive `exec` and the lowered IR, assert they
    /// agree, and return the value. `exec` is the oracle for the interpreter.
    fn decode_both(bytes: &[u8], w: SchemaId, r: SchemaId, reg: &Registry) -> Value {
        let recursive = decode(bytes, w, r, reg).unwrap();
        let flat = decode_via_ir(bytes, w, r, reg).unwrap();
        assert_eq!(
            recursive, flat,
            "IR interpreter disagreed with recursive exec"
        );
        recursive
    }

    // r[verify compat.field-matching]
    #[test]
    fn field_reorder_is_transparent() {
        // writer: { x: u32, y: u32 }; reader: { y: u32, x: u32 }
        let writer = point_struct(
            1,
            vec![
                field("x", prim(Primitive::U32), true),
                field("y", prim(Primitive::U32), true),
            ],
        );
        let reader = point_struct(
            2,
            vec![
                field("y", prim(Primitive::U32), true),
                field("x", prim(Primitive::U32), true),
            ],
        );
        let reg = Registry::new([writer, reader]);
        let bytes = compact::to_bytes(
            &obj(&[("x", Value::from(7u32)), ("y", Value::from(9u32))]),
            SchemaId(1),
            &reg,
        )
        .unwrap();
        let got = decode_both(&bytes, SchemaId(1), SchemaId(2), &reg);
        assert_eq!(
            got,
            obj(&[("x", Value::from(7u32)), ("y", Value::from(9u32))])
        );
    }

    // r[verify compat.skip-writer-only]
    #[test]
    fn writer_only_field_is_skipped() {
        // writer: { x: u32, gone: string }; reader: { x: u32 }
        let writer = point_struct(
            1,
            vec![
                field("x", prim(Primitive::U32), true),
                field("gone", prim(Primitive::String), true),
            ],
        );
        let reader = point_struct(2, vec![field("x", prim(Primitive::U32), true)]);
        let reg = Registry::new([writer, reader]);
        let bytes = compact::to_bytes(
            &obj(&[("x", Value::from(7u32)), ("gone", Value::from("bye"))]),
            SchemaId(1),
            &reg,
        )
        .unwrap();
        let got = decode_both(&bytes, SchemaId(1), SchemaId(2), &reg);
        assert_eq!(got, obj(&[("x", Value::from(7u32))]));
    }

    // r[verify compat.plan-first]
    // r[verify compat.reader-only-fields]
    // r[verify compat.defaults-are-reader-side]
    #[test]
    fn reader_only_field_defaults_or_fails() {
        // writer: { x: u32 }; reader: { x: u32, extra: u32 }
        let writer = point_struct(1, vec![field("x", prim(Primitive::U32), true)]);
        let optional = point_struct(
            2,
            vec![
                field("x", prim(Primitive::U32), true),
                field("extra", prim(Primitive::U32), false), // non-required -> default
            ],
        );
        let required = point_struct(
            3,
            vec![
                field("x", prim(Primitive::U32), true),
                field("extra", prim(Primitive::U32), true), // required -> plan fails
            ],
        );
        let reg = Registry::new([writer, optional, required]);
        let bytes =
            compact::to_bytes(&obj(&[("x", Value::from(7u32))]), SchemaId(1), &reg).unwrap();

        let got = decode_both(&bytes, SchemaId(1), SchemaId(2), &reg);
        assert_eq!(
            got,
            obj(&[("x", Value::from(7u32)), ("extra", Value::NULL)])
        );

        assert!(matches!(
            build_plan(SchemaId(1), SchemaId(3), &reg),
            Err(CompactError::Incompatible(_))
        ));
        assert!(matches!(
            decode_via_ir(&bytes, SchemaId(1), SchemaId(3), &reg),
            Err(CompactError::Incompatible(_))
        ));
    }

    // r[verify compat.type-match]
    #[test]
    fn numeric_widening_is_not_implicit() {
        let writer = schema(
            1,
            SchemaKind::List {
                element: prim(Primitive::U32),
            },
        );
        let reader = schema(
            2,
            SchemaKind::List {
                element: prim(Primitive::U64),
            },
        );
        let reg = Registry::new([writer, reader]);
        assert!(matches!(
            build_plan(SchemaId(1), SchemaId(2), &reg),
            Err(CompactError::Incompatible(_))
        ));
        assert!(matches!(
            decode_via_ir(&[], SchemaId(1), SchemaId(2), &reg),
            Err(CompactError::Incompatible(_))
        ));
    }

    // r[verify compat.enum]
    #[test]
    fn enum_variant_added_and_removed() {
        // writer enum { A, B(u32) }; reader enum { A, B(u32), C } (C added).
        let writer = schema(
            1,
            SchemaKind::Enum {
                name: "E".to_string(),
                variants: vec![
                    Variant {
                        name: "A".to_string(),
                        index: 0,
                        payload: VariantPayload::Unit,
                    },
                    Variant {
                        name: "B".to_string(),
                        index: 1,
                        payload: VariantPayload::Newtype(prim(Primitive::U32)),
                    },
                ],
            },
        );
        let reader_more = schema(
            2,
            SchemaKind::Enum {
                name: "E".to_string(),
                variants: vec![
                    Variant {
                        name: "A".to_string(),
                        index: 0,
                        payload: VariantPayload::Unit,
                    },
                    Variant {
                        name: "B".to_string(),
                        index: 1,
                        payload: VariantPayload::Newtype(prim(Primitive::U32)),
                    },
                    Variant {
                        name: "C".to_string(),
                        index: 2,
                        payload: VariantPayload::Unit,
                    },
                ],
            },
        );
        // reader that lacks B: receiving B at runtime is a decode error.
        let reader_fewer = schema(
            3,
            SchemaKind::Enum {
                name: "E".to_string(),
                variants: vec![Variant {
                    name: "A".to_string(),
                    index: 0,
                    payload: VariantPayload::Unit,
                }],
            },
        );
        let reg = Registry::new([writer, reader_more, reader_fewer]);

        let b = obj(&[("B", Value::from(42u32))]);
        let bytes = compact::to_bytes(&b, SchemaId(1), &reg).unwrap();

        // reader_more can read B fine (C just goes unused).
        assert_eq!(decode_both(&bytes, SchemaId(1), SchemaId(2), &reg), b);

        // reader_fewer plans (A matches), but receiving B is a decode error.
        assert!(matches!(
            decode(&bytes, SchemaId(1), SchemaId(3), &reg),
            Err(CompactError::WriterOnlyVariant(1))
        ));
        assert!(matches!(
            decode_via_ir(&bytes, SchemaId(1), SchemaId(3), &reg),
            Err(CompactError::WriterOnlyVariant(1))
        ));
        // an A value still decodes against reader_fewer.
        let a = obj(&[("A", Value::NULL)]);
        let a_bytes = compact::to_bytes(&a, SchemaId(1), &reg).unwrap();
        assert_eq!(decode_both(&a_bytes, SchemaId(1), SchemaId(3), &reg), a);
    }

    #[test]
    fn nested_struct_compat() {
        // Inner differs (field added); Outer holds an Inner.
        let w_inner = point_struct(10, vec![field("a", prim(Primitive::U32), true)]);
        let r_inner = point_struct(
            20,
            vec![
                field("a", prim(Primitive::U32), true),
                field("b", prim(Primitive::Bool), false),
            ],
        );
        let w_outer = schema(
            1,
            SchemaKind::Struct {
                name: "Outer".to_string(),
                fields: vec![field("inner", SchemaRef::concrete(SchemaId(10)), true)],
            },
        );
        let r_outer = schema(
            2,
            SchemaKind::Struct {
                name: "Outer".to_string(),
                fields: vec![field("inner", SchemaRef::concrete(SchemaId(20)), true)],
            },
        );
        let reg = Registry::new([w_inner, r_inner, w_outer, r_outer]);
        let bytes = compact::to_bytes(
            &obj(&[("inner", obj(&[("a", Value::from(5u32))]))]),
            SchemaId(1),
            &reg,
        )
        .unwrap();
        let got = decode_both(&bytes, SchemaId(1), SchemaId(2), &reg);
        assert_eq!(
            got,
            obj(&[(
                "inner",
                obj(&[("a", Value::from(5u32)), ("b", Value::NULL)])
            )])
        );
    }

    // r[verify compat.direction]
    #[test]
    fn compatibility_direction_reports_both_ways() {
        let older = point_struct(1, vec![field("x", prim(Primitive::U32), true)]);
        let newer_optional = point_struct(
            2,
            vec![
                field("x", prim(Primitive::U32), true),
                field("y", prim(Primitive::U32), false),
            ],
        );
        let newer_required = point_struct(
            3,
            vec![
                field("x", prim(Primitive::U32), true),
                field("y", prim(Primitive::U32), true),
            ],
        );
        let different = point_struct(4, vec![field("x", prim(Primitive::U64), true)]);
        let reg = Registry::new([older, newer_optional, newer_required, different]);

        assert_eq!(
            compatibility_direction(SchemaId(1), SchemaId(2), &reg),
            CompatDirection::Bidirectional
        );
        assert_eq!(
            compatibility_direction(SchemaId(1), SchemaId(3), &reg),
            CompatDirection::Forward
        );
        assert_eq!(
            compatibility_direction(SchemaId(1), SchemaId(4), &reg),
            CompatDirection::Incompatible
        );
    }

    /// Regression (found by `tests/compat_fuzz.rs`): a `list` of *zero-sized*
    /// elements (an empty struct) encodes to nothing but its count, so after the
    /// count is read the buffer is empty. The `r[validate.lengths]` guard wrongly
    /// rejected this — it assumed every element costs at least one wire byte —
    /// even at writer==reader. The element's true minimum wire size (0 here) now
    /// flows into the guard, which falls back to a fixed count cap for zero-sized
    /// elements. Both decode paths must accept `[{}, {}, {}]`.
    #[test]
    fn list_of_zero_sized_structs_decodes() {
        // Inner = empty struct; List<Inner>.
        let inner = point_struct(1, vec![]);
        let list = schema(
            2,
            SchemaKind::List {
                element: SchemaRef::concrete(SchemaId(1)),
            },
        );
        let reg = Registry::new([inner, list]);

        // [ {}, {}, {} ] — three empty structs, zero payload bytes each.
        let mut arr = VArray::new();
        for _ in 0..3 {
            arr.push(obj(&[]));
        }
        let value = Value::from(arr);
        let bytes = compact::to_bytes(&value, SchemaId(2), &reg).unwrap();
        // The whole message is just the u32 count: 4 bytes.
        assert_eq!(bytes.len(), 4);

        let got = decode_both(&bytes, SchemaId(2), SchemaId(2), &reg);
        assert_eq!(got, value);
    }

    /// Regression: a fixed `array` of zero-sized elements has element count from
    /// the schema (here `[3]`) but zero wire bytes, so the in-decoder count bound
    /// (`count > remaining`) wrongly rejected it. The fixed-count check now uses
    /// the same zero-sized fallback as the wire-driven path.
    #[test]
    fn array_of_zero_sized_units_decodes() {
        // Array<unit, 3> — three units, zero payload bytes total.
        let arr_schema = schema(
            1,
            SchemaKind::Array {
                element: prim(Primitive::Unit),
                dimensions: vec![3],
            },
        );
        let reg = Registry::new([arr_schema]);
        let mut arr = VArray::new();
        for _ in 0..3 {
            arr.push(Value::NULL);
        }
        let value = Value::from(arr);
        let bytes = compact::to_bytes(&value, SchemaId(1), &reg).unwrap();
        assert_eq!(bytes.len(), 0);

        let got = decode_both(&bytes, SchemaId(1), SchemaId(1), &reg);
        assert_eq!(got, value);
    }
}