syncular-ssp2 0.65.2

Syncular v2 SSP2 wire codec, implemented from SPEC.md alone (language-neutrality POC)
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
//! SSP2 envelope decoding (SPEC.md §1.2), request/response frame grammar
//! (§1.5, §1.6), and the closed decode-error boundary of §1.7: exactly the
//! listed violations are rejected here, nothing more.

use crate::error::{DecodeError, Result};
use crate::model::frame_type as ft;
use crate::model::{
    fenced_in_order, valid_outbox_id, valid_round_failure_code, valid_unit_id, Change, EchoStatus,
    FencedRecord, Frame, LiveStatus, MediaType, Message, MsgKind, Op, OpResult, Operation,
    PushStatus, SubStatus,
};
use crate::primitives::Reader;
use crate::segment::decode_rows_segment;

pub const SSP2_MAGIC: &[u8; 4] = b"SSP2";
/// The one wire version this codec speaks; every other version is rejected
/// as unknown (§1.2, §9).
pub const WIRE_VERSION: u16 = 4;

/// Decode a complete SSP2 message, enforcing envelope rules and the per-kind
/// frame grammar. Unknown frame types are preserved byte-for-byte (§1.2
/// rule 2).
pub fn decode_message(bytes: &[u8]) -> Result<Message> {
    let mut r = Reader::new(bytes);
    let magic = r.take(4, "envelope magic")?;
    if magic != SSP2_MAGIC {
        return Err(DecodeError::invalid("bad envelope magic (expected SSP2)"));
    }
    let wire_version = r.u16("wireVersion")?;
    if wire_version != WIRE_VERSION {
        return Err(DecodeError::invalid(format!(
            "unsupported wireVersion {wire_version}"
        )));
    }
    let msg_kind = match r.u8("msgKind")? {
        0x01 => MsgKind::Request,
        0x02 => MsgKind::Response,
        other => {
            return Err(DecodeError::invalid(format!(
                "unknown msgKind 0x{other:02x}"
            )))
        }
    };
    let flags = r.u8("flags")?;
    if flags != 0 {
        return Err(DecodeError::invalid(format!(
            "envelope flags must be 0x00, got 0x{flags:02x}"
        )));
    }

    let frames = match msg_kind {
        MsgKind::Request => decode_request_frames(&mut r)?,
        MsgKind::Response => decode_response_frames(&mut r)?,
    };
    if !r.is_empty() {
        return Err(DecodeError::invalid(
            "trailing bytes after the END frame (canonical encoding)",
        ));
    }
    Ok(Message {
        wire_version,
        msg_kind,
        frames,
    })
}

/// Read one frame header + payload. Returns `None` for END.
fn next_frame<'a>(r: &mut Reader<'a>) -> Result<Option<(u8, &'a [u8])>> {
    if r.is_empty() {
        return Err(DecodeError::invalid(
            "truncated message: body ends without an END frame",
        ));
    }
    let ty = r.u8("frameType")?;
    let len = r.u32("frameLength")? as usize;
    if len > r.remaining() {
        return Err(DecodeError::invalid(format!(
            "frame 0x{ty:02x} length {len} exceeds the {} remaining bytes",
            r.remaining()
        )));
    }
    let payload = r.take(len, "frame payload")?;
    if ty == ft::END {
        if len != 0 {
            return Err(DecodeError::invalid("END frame must have frameLength 0"));
        }
        return Ok(None);
    }
    Ok(Some((ty, payload)))
}

/// "Unknown" per §1.2 rule 2: a frameType with no layout in this wire version
/// for *either* message kind. A type registered for the other kind is a
/// decode error, not unknown.
fn classify(ty: u8, kind: MsgKind) -> Result<bool> {
    let (own, other) = match kind {
        MsgKind::Request => (ft::REQUEST_TYPES, ft::RESPONSE_TYPES),
        MsgKind::Response => (ft::RESPONSE_TYPES, ft::REQUEST_TYPES),
    };
    if own.contains(&ty) {
        Ok(true)
    } else if other.contains(&ty) {
        Err(DecodeError::invalid(format!(
            "frame type 0x{ty:02x} is registered for the other message kind"
        )))
    } else {
        Ok(false)
    }
}

/// §1.5 grammar: REQ_HEADER, ROUND_FAILURE 0|1 immediately after it,
/// PUSH_COMMIT × N, PULL_HEADER 0|1, UNIT × M then PROGRESS × K (only if
/// PULL_HEADER present), END. A request with neither push nor pull frames is
/// invalid.
fn decode_request_frames(r: &mut Reader<'_>) -> Result<Vec<Frame>> {
    let mut frames = Vec::new();
    let mut seen_header = false;
    let mut seen_body = false;
    let mut seen_failure = false;
    let mut seen_pull = false;
    let mut seen_push = false;
    let mut seen_progress = false;

    while let Some((ty, payload)) = next_frame(r)? {
        let known = classify(ty, MsgKind::Request)?;
        if !known {
            if !seen_header {
                return Err(DecodeError::invalid(
                    "unknown frame before REQ_HEADER (unknown frames are only legal between the header frame and END)",
                ));
            }
            frames.push(Frame::Unknown {
                frame_type: ty,
                payload: payload.to_vec(),
            });
            seen_body = true;
            continue;
        }
        if !seen_header {
            if ty != ft::REQ_HEADER {
                return Err(DecodeError::invalid(
                    "first frame of a request must be REQ_HEADER",
                ));
            }
            frames.push(decode_req_header(payload)?);
            seen_header = true;
            continue;
        }
        match ty {
            ft::REQ_HEADER => {
                return Err(DecodeError::invalid("duplicate REQ_HEADER frame"));
            }
            ft::ROUND_FAILURE => {
                if seen_failure {
                    return Err(DecodeError::invalid("duplicate ROUND_FAILURE frame"));
                }
                if seen_body {
                    return Err(DecodeError::invalid(
                        "ROUND_FAILURE must immediately follow REQ_HEADER",
                    ));
                }
                frames.push(decode_round_failure(payload)?);
                seen_body = true;
                seen_failure = true;
            }
            ft::PUSH_COMMIT => {
                if seen_pull {
                    return Err(DecodeError::invalid(
                        "PUSH_COMMIT after PULL_HEADER (out of grammar order)",
                    ));
                }
                frames.push(decode_push_commit(payload)?);
                seen_body = true;
                seen_push = true;
            }
            ft::PULL_HEADER => {
                if seen_pull {
                    return Err(DecodeError::invalid("duplicate PULL_HEADER frame"));
                }
                frames.push(decode_pull_header(payload)?);
                seen_body = true;
                seen_pull = true;
            }
            ft::UNIT => {
                if !seen_pull {
                    return Err(DecodeError::invalid(
                        "UNIT frame without a preceding PULL_HEADER",
                    ));
                }
                if seen_progress {
                    return Err(DecodeError::invalid("UNIT frame after a PROGRESS frame"));
                }
                frames.push(frame_payload(payload, "UNIT", |r| {
                    Ok(Frame::Unit {
                        scopes: r.str_map("scopes")?,
                    })
                })?);
                seen_body = true;
            }
            ft::PROGRESS => {
                if !seen_pull {
                    return Err(DecodeError::invalid(
                        "PROGRESS frame without a preceding PULL_HEADER",
                    ));
                }
                frames.push(decode_progress(payload)?);
                seen_body = true;
                seen_progress = true;
            }
            _ => unreachable!("classified as known request type"),
        }
    }
    if !seen_header {
        return Err(DecodeError::invalid("request has no REQ_HEADER frame"));
    }
    if !seen_push && !seen_pull {
        return Err(DecodeError::invalid(
            "request has neither PUSH_COMMIT nor PULL_HEADER frames",
        ));
    }
    Ok(frames)
}

/// The open section of a response (§1.4 rule 3).
#[derive(PartialEq)]
enum Section {
    None,
    Unit,
    Live,
}

/// §1.6 grammar: RESP_HEADER, LEASE 0|1, PUSH_RESULT × N, then per unit
/// SUB_START segment frames × m SUB_END, then at most one live section
/// LIVE_START COMMIT × k LIVE_END; ERROR 0|1 anywhere after RESP_HEADER, and
/// if present the next frame MUST be END (no frame of any type, unknown
/// included, may follow it). §4.5 "Section order" lists the order errors.
fn decode_response_frames(r: &mut Reader<'_>) -> Result<Vec<Frame>> {
    let mut frames = Vec::new();
    let mut seen_header = false;
    let mut seen_lease = false; // §7.3.2: at most one LEASE, before the body
    let mut seen_body = false; // any non-RESP_HEADER/non-LEASE frame
    let mut section = Section::None;
    let mut seen_section = false; // a unit or live section has started
    let mut seen_live = false;
    let mut error_seen = false;
    let mut last_push_result: Option<(String, bool, Vec<i32>)> = None;
    let mut last_push_result_has_details = false;

    while let Some((ty, payload)) = next_frame(r)? {
        if error_seen {
            return Err(DecodeError::invalid(
                "frame after ERROR: the next frame after ERROR must be END",
            ));
        }
        let known = classify(ty, MsgKind::Response)?;
        if !known {
            if !seen_header {
                return Err(DecodeError::invalid(
                    "unknown frame before RESP_HEADER (unknown frames are only legal between the header frame and END)",
                ));
            }
            frames.push(Frame::Unknown {
                frame_type: ty,
                payload: payload.to_vec(),
            });
            seen_body = true;
            last_push_result = None;
            continue;
        }
        if !seen_header {
            if ty != ft::RESP_HEADER {
                return Err(DecodeError::invalid(
                    "first frame of a response must be RESP_HEADER",
                ));
            }
            frames.push(decode_resp_header(payload)?);
            seen_header = true;
            continue;
        }
        match ty {
            ft::RESP_HEADER => {
                return Err(DecodeError::invalid("duplicate RESP_HEADER frame"));
            }
            ft::LEASE => {
                // §7.3.2: at most one LEASE, immediately after RESP_HEADER.
                if seen_lease {
                    return Err(DecodeError::invalid("duplicate LEASE frame"));
                }
                if seen_body {
                    return Err(DecodeError::invalid(
                        "LEASE frame must immediately follow RESP_HEADER",
                    ));
                }
                frames.push(decode_lease(payload)?);
                seen_lease = true;
            }
            ft::PUSH_RESULT => {
                if seen_section {
                    return Err(DecodeError::invalid(
                        "PUSH_RESULT after a unit or live section (out of grammar order)",
                    ));
                }
                let frame = decode_push_result(payload)?;
                if let Frame::PushResult {
                    client_commit_id,
                    status,
                    results,
                    ..
                } = &frame
                {
                    let error_indexes = results
                        .iter()
                        .filter_map(|result| match result {
                            OpResult::Error { op_index, .. } => Some(*op_index),
                            _ => None,
                        })
                        .collect();
                    last_push_result = Some((
                        client_commit_id.clone(),
                        *status == PushStatus::Rejected,
                        error_indexes,
                    ));
                    last_push_result_has_details = false;
                }
                frames.push(frame);
                seen_body = true;
            }
            ft::PUSH_RESULT_DETAILS => {
                if seen_section {
                    return Err(DecodeError::invalid(
                        "PUSH_RESULT_DETAILS after a unit or live section (out of grammar order)",
                    ));
                }
                let frame = decode_push_result_details(payload)?;
                let Some((result_commit_id, result_rejected, error_indexes)) = &last_push_result
                else {
                    return Err(DecodeError::invalid(
                        "PUSH_RESULT_DETAILS without a preceding PUSH_RESULT",
                    ));
                };
                if last_push_result_has_details {
                    return Err(DecodeError::invalid(
                        "duplicate PUSH_RESULT_DETAILS companion",
                    ));
                }
                if let Frame::PushResultDetails {
                    client_commit_id,
                    entries,
                } = &frame
                {
                    if !*result_rejected || client_commit_id != result_commit_id {
                        return Err(DecodeError::invalid(
                            "PUSH_RESULT_DETAILS does not match its rejected PUSH_RESULT",
                        ));
                    }
                    if entries
                        .iter()
                        .any(|entry| !error_indexes.contains(&entry.op_index))
                    {
                        return Err(DecodeError::invalid(
                            "PUSH_RESULT_DETAILS entry does not match an error result",
                        ));
                    }
                }
                last_push_result_has_details = true;
                frames.push(frame);
                seen_body = true;
            }
            ft::SUB_START => {
                if section == Section::Unit {
                    return Err(DecodeError::invalid(
                        "SUB_START while a unit section is already open",
                    ));
                }
                if seen_live {
                    return Err(DecodeError::invalid(
                        "SUB_START inside or after the live section",
                    ));
                }
                frames.push(decode_sub_start(payload)?);
                section = Section::Unit;
                seen_section = true;
                seen_body = true;
                last_push_result = None;
            }
            ft::SEGMENT_REF | ft::SEGMENT_INLINE => {
                if section != Section::Unit {
                    return Err(DecodeError::invalid("segment frame outside a unit section"));
                }
                if ty == ft::SEGMENT_REF {
                    frames.push(decode_segment_ref(payload)?);
                } else {
                    // §5.7: the payload must be a structurally valid rows
                    // segment; the raw bytes are preserved for re-encoding.
                    decode_rows_segment(payload)?;
                    frames.push(Frame::SegmentInline {
                        payload: payload.to_vec(),
                    });
                }
                seen_body = true;
            }
            ft::SUB_END => {
                if section != Section::Unit {
                    return Err(DecodeError::invalid("SUB_END without an open unit section"));
                }
                frames.push(decode_sub_end(payload)?);
                section = Section::None;
                seen_body = true;
            }
            ft::LIVE_START => {
                if section == Section::Unit {
                    return Err(DecodeError::invalid("LIVE_START inside a unit section"));
                }
                if seen_live {
                    return Err(DecodeError::invalid("second LIVE_START frame"));
                }
                frames.push(decode_live_start(payload)?);
                section = Section::Live;
                seen_section = true;
                seen_live = true;
                seen_body = true;
                last_push_result = None;
            }
            ft::COMMIT => {
                if section != Section::Live {
                    return Err(DecodeError::invalid(
                        "COMMIT frame outside the live section",
                    ));
                }
                frames.push(decode_commit(payload)?);
                seen_body = true;
            }
            ft::LIVE_END => {
                if section != Section::Live {
                    return Err(DecodeError::invalid("LIVE_END without an open LIVE_START"));
                }
                frames.push(decode_live_end(payload)?);
                section = Section::None;
                seen_body = true;
            }
            ft::ERROR => {
                frames.push(decode_error_frame(payload)?);
                error_seen = true;
                seen_body = true;
            }
            _ => unreachable!("classified as known response type"),
        }
    }
    if !seen_header {
        return Err(DecodeError::invalid("response has no RESP_HEADER frame"));
    }
    if section != Section::None && !error_seen {
        return Err(DecodeError::invalid(
            "message ended with an open unit or live section",
        ));
    }
    Ok(frames)
}

/// Run `parse` over a frame payload; trailing bytes inside a known frame are
/// a decode error (§1.2 rule 3).
fn frame_payload<T>(
    payload: &[u8],
    name: &str,
    parse: impl FnOnce(&mut Reader<'_>) -> Result<T>,
) -> Result<T> {
    let mut r = Reader::new(payload);
    let value = parse(&mut r)?;
    if !r.is_empty() {
        return Err(DecodeError::invalid(format!(
            "trailing bytes inside {name} frame ({} bytes unconsumed)",
            r.remaining()
        )));
    }
    Ok(value)
}

fn decode_req_header(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "REQ_HEADER", |r| {
        let client_id = r.str("clientId")?;
        if client_id.is_empty() {
            return Err(DecodeError::invalid("clientId must be non-empty"));
        }
        let schema_version = r.i32("schemaVersion")?;
        if schema_version < 1 {
            return Err(DecodeError::invalid(format!(
                "schemaVersion must be ≥ 1, got {schema_version}"
            )));
        }
        let log_epoch = if r.presence("logEpoch")? {
            let value = r.str("logEpoch")?;
            if value.is_empty() {
                return Err(DecodeError::invalid(
                    "REQ_HEADER.logEpoch must be non-empty",
                ));
            }
            Some(value)
        } else {
            None
        };
        let outbox_id = if r.presence("outboxId")? {
            let value = r.str("outboxId")?;
            if !valid_outbox_id(&value) {
                return Err(DecodeError::invalid(
                    "REQ_HEADER.outboxId must be non-empty and at most 64 bytes",
                ));
            }
            Some(value)
        } else {
            None
        };
        Ok(Frame::ReqHeader {
            client_id,
            schema_version,
            log_epoch,
            outbox_id,
        })
    })
}

fn decode_round_failure(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "ROUND_FAILURE", |r| {
        let code = r.str("code")?;
        if !valid_round_failure_code(&code) {
            return Err(DecodeError::invalid(format!(
                "ROUND_FAILURE.code {code:?} is not a lowercase dotted code of at most 64 bytes"
            )));
        }
        Ok(Frame::RoundFailure { code })
    })
}

fn decode_op(r: &mut Reader<'_>) -> Result<Op> {
    match r.u8("op")? {
        1 => Ok(Op::Upsert),
        2 => Ok(Op::Delete),
        other => Err(DecodeError::invalid(format!("unknown op byte {other}"))),
    }
}

fn decode_push_commit(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "PUSH_COMMIT", |r| {
        let client_commit_id = r.str("clientCommitId")?;
        if client_commit_id.is_empty() {
            return Err(DecodeError::invalid("clientCommitId must be non-empty"));
        }
        let client_seq = r.i64("clientSeq")?;
        if client_seq < 0 {
            return Err(DecodeError::invalid("PUSH_COMMIT.clientSeq must be >= 0"));
        }
        let count = r.u32("operations count")? as usize;
        if count == 0 {
            return Err(DecodeError::empty_commit(
                "PUSH_COMMIT with zero operations",
            ));
        }
        let mut operations = Vec::with_capacity(count.min(4096));
        for _ in 0..count {
            let table = r.str("operation table")?;
            let row_id = r.str("operation rowId")?;
            let op = decode_op(r)?;
            let base_version = if r.presence("baseVersion")? {
                Some(r.i64("baseVersion")?)
            } else {
                None
            };
            let payload_bytes = if r.presence("payload")? {
                Some(r.bytes("payload")?)
            } else {
                None
            };
            // §6.1 presence tie: payload present iff upsert.
            match op {
                Op::Upsert if payload_bytes.is_none() => {
                    return Err(DecodeError::invalid(
                        "upsert operation without a payload (presence invariant)",
                    ));
                }
                Op::Delete if payload_bytes.is_some() => {
                    return Err(DecodeError::invalid(
                        "delete operation with a payload (presence invariant)",
                    ));
                }
                _ => {}
            }
            operations.push(Operation {
                table,
                row_id,
                op,
                base_version,
                payload: payload_bytes,
            });
        }
        Ok(Frame::PushCommit {
            client_commit_id,
            client_seq,
            operations,
        })
    })
}

fn decode_pull_header(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "PULL_HEADER", |r| {
        let limit_commits = r.i32("limitCommits")?;
        let limit_snapshot_rows = r.i32("limitSnapshotRows")?;
        let max_snapshot_pages = r.i32("maxSnapshotPages")?;
        let accept = r.u8("accept")?;
        if accept & 0xF0 != 0 {
            return Err(DecodeError::invalid(format!(
                "accept bits 4–7 must be zero, got 0b{accept:08b}"
            )));
        }
        let cursor = r.i64("cursor")?;
        if cursor < -1 {
            return Err(DecodeError::invalid("PULL_HEADER.cursor must be >= -1"));
        }
        let registration_digest = r
            .bytes("registrationDigest")?
            .try_into()
            .map_err(|_| DecodeError::invalid("PULL_HEADER.registrationDigest must be 32 bytes"))?;
        let echo_digest = read_echo_digest(r, "PULL_HEADER.echoDigest")?;
        Ok(Frame::PullHeader {
            limit_commits,
            limit_snapshot_rows,
            max_snapshot_pages,
            accept,
            cursor,
            registration_digest,
            echo_digest,
        })
    })
}

/// §4.2, §4.4: an echo digest is empty or 32 bytes.
fn read_echo_digest(r: &mut Reader<'_>, field: &str) -> Result<Option<[u8; 32]>> {
    let bytes = r.bytes(field)?;
    if bytes.is_empty() {
        return Ok(None);
    }
    bytes
        .try_into()
        .map(Some)
        .map_err(|_| DecodeError::invalid(format!("{field} must be 0 or 32 bytes")))
}

fn decode_progress(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "PROGRESS", |r| {
        let id = r.str("id")?;
        if !valid_unit_id(&id) {
            return Err(DecodeError::invalid(
                "PROGRESS.id must be 32 lowercase hex characters",
            ));
        }
        let cursor = r.i64("cursor")?;
        if cursor < -1 {
            return Err(DecodeError::invalid("PROGRESS.cursor must be >= -1"));
        }
        let bootstrap_state = if r.presence("bootstrapState")? {
            Some(r.json("bootstrapState")?)
        } else {
            None
        };
        Ok(Frame::Progress {
            id,
            cursor,
            bootstrap_state,
        })
    })
}

fn decode_resp_header(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "RESP_HEADER", |r| {
        let required_schema_version = if r.presence("requiredSchemaVersion")? {
            Some(r.i32("requiredSchemaVersion")?)
        } else {
            None
        };
        let latest_schema_version = if r.presence("latestSchemaVersion")? {
            Some(r.i32("latestSchemaVersion")?)
        } else {
            None
        };
        let log_epoch = r.str("logEpoch")?;
        if log_epoch.is_empty() {
            return Err(DecodeError::invalid(
                "RESP_HEADER.logEpoch must be non-empty",
            ));
        }
        let reset_required = r.bool("resetRequired")?;
        Ok(Frame::RespHeader {
            required_schema_version,
            latest_schema_version,
            log_epoch,
            reset_required,
        })
    })
}

fn decode_lease(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "LEASE", |r| {
        let lease_id = r.str("leaseId")?;
        if lease_id.is_empty() {
            return Err(DecodeError::invalid("LEASE.leaseId must be non-empty"));
        }
        let expires_at_ms = r.i64("expiresAtMs")?;
        Ok(Frame::Lease {
            lease_id,
            expires_at_ms,
        })
    })
}

fn decode_push_result(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "PUSH_RESULT", |r| {
        let client_commit_id = r.str("clientCommitId")?;
        let status = match r.u8("status")? {
            1 => PushStatus::Applied,
            2 => PushStatus::Cached,
            3 => PushStatus::Rejected,
            other => {
                return Err(DecodeError::invalid(format!(
                    "unknown PUSH_RESULT status byte {other}"
                )))
            }
        };
        let commit_seq = if r.presence("commitSeq")? {
            Some(r.i64("commitSeq")?)
        } else {
            None
        };
        // §6.3 presence tie: commitSeq present for applied/cached, absent for
        // rejected.
        match status {
            PushStatus::Rejected if commit_seq.is_some() => {
                return Err(DecodeError::invalid(
                    "commitSeq present on a rejected PUSH_RESULT (presence invariant)",
                ));
            }
            PushStatus::Applied | PushStatus::Cached if commit_seq.is_none() => {
                return Err(DecodeError::invalid(
                    "commitSeq absent on an applied/cached PUSH_RESULT (presence invariant)",
                ));
            }
            _ => {}
        }
        let count = r.u32("results count")? as usize;
        let mut results = Vec::with_capacity(count.min(4096));
        for _ in 0..count {
            let op_index = r.i32("opIndex")?;
            results.push(match r.u8("result status")? {
                1 => OpResult::Applied { op_index },
                2 => OpResult::Conflict {
                    op_index,
                    code: r.str("conflict code")?,
                    message: r.str("conflict message")?,
                    server_version: r.i64("serverVersion")?,
                    server_row: r.bytes("serverRow")?,
                    conflict_columns: r.bytes("conflictColumns")?,
                },
                3 => OpResult::Error {
                    op_index,
                    code: r.str("error code")?,
                    message: r.str("error message")?,
                    retryable: r.bool("retryable")?,
                },
                other => {
                    return Err(DecodeError::invalid(format!(
                        "unknown result record status byte {other}"
                    )))
                }
            });
        }
        Ok(Frame::PushResult {
            client_commit_id,
            status,
            commit_seq,
            results,
        })
    })
}

fn valid_detail_identifier(segment: &str) -> bool {
    let mut chars = segment.chars();
    matches!(chars.next(), Some(first) if first == '_' || first.is_ascii_alphabetic())
        && chars.all(|c| c == '_' || c.is_ascii_alphanumeric())
}

fn valid_detail_token(token: &str, max: usize) -> bool {
    if token.is_empty() || token.len() > max || !token.starts_with(|c: char| c.is_ascii_lowercase())
    {
        return false;
    }
    let mut prev_sep = false;
    for c in token.chars() {
        if c.is_ascii_lowercase() || c.is_ascii_digit() {
            prev_sep = false;
        } else if matches!(c, '.' | '_' | '-') && !prev_sep {
            prev_sep = true;
        } else {
            return false;
        }
    }
    !prev_sep
}

fn validate_rejection_details(raw: &str) -> Result<()> {
    if raw.len() > 4_096 {
        return Err(DecodeError::invalid(
            "PUSH_RESULT_DETAILS.details exceeds 4096 encoded bytes",
        ));
    }
    let val: serde_json::Value = serde_json::from_str(raw)
        .map_err(|e| DecodeError::invalid(format!("invalid PUSH_RESULT_DETAILS.details: {e}")))?;
    let Some(obj) = val.as_object() else {
        return Err(DecodeError::invalid(
            "PUSH_RESULT_DETAILS.details must be a JSON object",
        ));
    };
    if obj.is_empty() {
        return Err(DecodeError::invalid(
            "PUSH_RESULT_DETAILS.details must not be empty",
        ));
    }
    for (k, v) in obj {
        match k.as_str() {
            "fieldPaths" => {
                let Some(arr) = v.as_array() else {
                    return Err(DecodeError::invalid("fieldPaths must be an array"));
                };
                if arr.is_empty() || arr.len() > 32 {
                    return Err(DecodeError::invalid("fieldPaths must contain 1-32 paths"));
                }
                let mut seen = std::collections::BTreeSet::new();
                for item in arr {
                    let Some(s) = item.as_str() else {
                        return Err(DecodeError::invalid("fieldPaths entries must be strings"));
                    };
                    if s.is_empty()
                        || s.len() > 160
                        || !s.split('.').all(valid_detail_identifier)
                        || !seen.insert(s)
                    {
                        return Err(DecodeError::invalid(
                            "fieldPaths contains an invalid or duplicate path",
                        ));
                    }
                }
            }
            "reason" | "requiredAction" => {
                let Some(s) = v.as_str() else {
                    return Err(DecodeError::invalid(format!("{k} must be a string")));
                };
                if !valid_detail_token(s, 96) {
                    return Err(DecodeError::invalid(format!(
                        "{k} must be a lowercase stable token"
                    )));
                }
            }
            "references" => {
                let Some(refs) = v.as_object() else {
                    return Err(DecodeError::invalid("references must be a JSON object"));
                };
                if refs.is_empty() || refs.len() > 16 {
                    return Err(DecodeError::invalid("references must contain 1-16 entries"));
                }
                for (rk, rv) in refs {
                    let Some(rvs) = rv.as_str() else {
                        return Err(DecodeError::invalid("references values must be strings"));
                    };
                    if !valid_detail_token(rk, 64)
                        || rvs.is_empty()
                        || rvs.len() > 256
                        || rvs.trim() != rvs
                        || rvs.chars().any(char::is_control)
                    {
                        return Err(DecodeError::invalid(
                            "references contains an invalid key or value",
                        ));
                    }
                }
            }
            other => {
                return Err(DecodeError::invalid(format!(
                    "unknown rejection details member {other:?}"
                )));
            }
        }
    }
    Ok(())
}

fn decode_push_result_details(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "PUSH_RESULT_DETAILS", |r| {
        let client_commit_id = r.str("clientCommitId")?;
        if client_commit_id.is_empty() {
            return Err(DecodeError::invalid(
                "PUSH_RESULT_DETAILS.clientCommitId must be non-empty",
            ));
        }
        let count = r.u32("entries count")? as usize;
        if count == 0 {
            return Err(DecodeError::invalid(
                "PUSH_RESULT_DETAILS must carry at least one entry",
            ));
        }
        let mut entries = Vec::with_capacity(count.min(64));
        let mut seen = std::collections::BTreeSet::new();
        for _ in 0..count {
            let op_index = r.i32("opIndex")?;
            if op_index < 0 || !seen.insert(op_index) {
                return Err(DecodeError::invalid(
                    "PUSH_RESULT_DETAILS opIndex values must be unique and non-negative",
                ));
            }
            let details = r.json("details")?;
            validate_rejection_details(&details.0)?;
            entries.push(crate::model::PushResultDetail { op_index, details });
        }
        Ok(Frame::PushResultDetails {
            client_commit_id,
            entries,
        })
    })
}

fn decode_sub_start(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "SUB_START", |r| {
        let id = r.str("id")?;
        if !valid_unit_id(&id) {
            return Err(DecodeError::invalid(
                "SUB_START.id must be 32 lowercase hex characters",
            ));
        }
        let status = match r.u8("status")? {
            1 => SubStatus::Active,
            2 => SubStatus::Revoked,
            3 => SubStatus::Reset,
            other => {
                return Err(DecodeError::invalid(format!(
                    "unknown SUB_START status byte {other}"
                )))
            }
        };
        let reason_code = r.str("reasonCode")?;
        let bootstrap = r.bool("bootstrap")?;
        let count = r.u32("fenced count")? as usize;
        let mut fenced = Vec::with_capacity(count.min(4096));
        for _ in 0..count {
            fenced.push(FencedRecord {
                table: r.str("fenced table")?,
                status: match r.u8("fenced status")? {
                    1 => EchoStatus::Active,
                    2 => EchoStatus::Revoked,
                    other => {
                        return Err(DecodeError::invalid(format!(
                            "unknown fenced record status byte {other}"
                        )))
                    }
                },
                scopes: r.scope_map("fenced scopes")?,
            });
        }
        if !fenced_in_order(&fenced) {
            return Err(DecodeError::invalid(
                "SUB_START.fenced tables must be unique and in ascending order",
            ));
        }
        Ok(Frame::SubStart {
            id,
            status,
            reason_code,
            bootstrap,
            fenced,
        })
    })
}

fn decode_live_start(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "LIVE_START", |r| {
        let status = match r.u8("status")? {
            1 => LiveStatus::Active,
            3 => LiveStatus::Reset,
            4 => LiveStatus::Unregistered,
            other => {
                return Err(DecodeError::invalid(format!(
                    "unknown LIVE_START status byte {other}"
                )))
            }
        };
        Ok(Frame::LiveStart {
            status,
            reason_code: r.str("reasonCode")?,
        })
    })
}

fn decode_live_end(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "LIVE_END", |r| {
        Ok(Frame::LiveEnd {
            through_seq: r.i64("throughSeq")?,
            echo_digest: read_echo_digest(r, "LIVE_END.echoDigest")?,
        })
    })
}

fn decode_commit(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "COMMIT", |r| {
        let commit_seq = r.i64("commitSeq")?;
        let created_at_ms = r.i64("createdAtMs")?;
        let actor_id = r.str("actorId")?;
        let table_count = r.u32("tables count")? as usize;
        let mut tables = Vec::with_capacity(table_count.min(4096));
        for _ in 0..table_count {
            tables.push(r.str("table")?);
        }
        let change_count = r.u32("changes count")? as usize;
        let mut changes = Vec::with_capacity(change_count.min(4096));
        for _ in 0..change_count {
            let table_index = r.u16("tableIndex")?;
            if table_index as usize >= tables.len() {
                return Err(DecodeError::invalid(format!(
                    "change tableIndex {table_index} out of range (tables: {})",
                    tables.len()
                )));
            }
            let row_id = r.str("rowId")?;
            let op = decode_op(r)?;
            let row_version = r.i64("rowVersion")?;
            if row_version < 1 {
                return Err(DecodeError::invalid("change rowVersion must be at least 1"));
            }
            let scopes = r.str_map("change scopes")?;
            let row = if r.presence("row")? {
                Some(r.bytes("row")?)
            } else {
                None
            };
            // §4.5 presence tie: row present iff upsert.
            if row.is_some() != (op == Op::Upsert) {
                return Err(DecodeError::invalid(
                    "change row presence does not match op (presence invariant)",
                ));
            }
            changes.push(Change {
                table_index,
                row_id,
                op,
                row_version,
                scopes,
                row,
            });
        }
        Ok(Frame::Commit {
            commit_seq,
            created_at_ms,
            actor_id,
            tables,
            changes,
        })
    })
}

fn decode_segment_ref(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "SEGMENT_REF", |r| {
        let segment_id = r.str("segmentId")?;
        let media_type = match r.u8("mediaType")? {
            1 => MediaType::Rows,
            2 => MediaType::Sqlite,
            other => {
                return Err(DecodeError::invalid(format!(
                    "unknown mediaType byte {other}"
                )))
            }
        };
        let table = r.str("table")?;
        let byte_length = r.i64("byteLength")?;
        let row_count = r.i64("rowCount")?;
        let as_of_commit_seq = r.i64("asOfCommitSeq")?;
        let scope_digest = r.str("scopeDigest")?;
        let row_cursor = if r.presence("rowCursor")? {
            Some(r.str("rowCursor")?)
        } else {
            None
        };
        let next_row_cursor = if r.presence("nextRowCursor")? {
            Some(r.str("nextRowCursor")?)
        } else {
            None
        };
        let url = if r.presence("url")? {
            Some(r.str("url")?)
        } else {
            None
        };
        let url_expires_at_ms = if r.presence("urlExpiresAtMs")? {
            Some(r.i64("urlExpiresAtMs")?)
        } else {
            None
        };
        // §5.4 presence tie: urlExpiresAtMs present iff url is.
        if url.is_some() != url_expires_at_ms.is_some() {
            return Err(DecodeError::invalid(
                "url and urlExpiresAtMs must be present together (presence invariant)",
            ));
        }
        Ok(Frame::SegmentRef {
            segment_id,
            media_type,
            table,
            byte_length,
            row_count,
            as_of_commit_seq,
            scope_digest,
            row_cursor,
            next_row_cursor,
            url,
            url_expires_at_ms,
        })
    })
}

fn decode_sub_end(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "SUB_END", |r| {
        let next_cursor = r.i64("nextCursor")?;
        let bootstrap_state = if r.presence("bootstrapState")? {
            Some(r.json("bootstrapState")?)
        } else {
            None
        };
        Ok(Frame::SubEnd {
            next_cursor,
            bootstrap_state,
        })
    })
}

fn decode_error_frame(payload: &[u8]) -> Result<Frame> {
    frame_payload(payload, "ERROR", |r| {
        let code = r.str("code")?;
        let message = r.str("message")?;
        let category = r.str("category")?;
        let retryable = r.bool("retryable")?;
        let recommended_action = r.str("recommendedAction")?;
        let details = if r.presence("details")? {
            Some(r.json("details")?)
        } else {
            None
        };
        Ok(Frame::Error {
            code,
            message,
            category,
            retryable,
            recommended_action,
            details,
        })
    })
}