tensogram 0.22.0

Fast binary N-tensor message format for scientific data — encode, decode, file I/O, streaming
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
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
// (C) Copyright 2026- ECMWF and individual contributors.
//
// This software is licensed under the terms of the Apache Licence Version 2.0
// which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
// In applying this licence, ECMWF does not waive the privileges and immunities
// granted to it by virtue of its status as an intergovernmental organisation nor
// does it submit to any jurisdiction.

use std::collections::BTreeMap;
use std::io::Write;

use crate::encode::{
    EncodeOptions, MaskMethod, build_pipeline_config, populate_base_entries,
    populate_reserved_provenance, validate_no_szip_offsets_for_non_szip, validate_object,
    validate_szip_block_offsets,
};
use crate::error::{Result, TensogramError};
use crate::framing::EncodedObject;

use crate::metadata::{self, RESERVED_KEY};
use crate::substitute_and_mask;
use crate::types::{DataObjectDescriptor, GlobalMetadata, HashFrame, IndexFrame};
use crate::wire::{
    FRAME_COMMON_FOOTER_SIZE, FRAME_END, FRAME_HEADER_SIZE, FrameFlags, FrameHeader, FrameType,
    MessageFlags, POSTAMBLE_SIZE, PREAMBLE_SIZE, Postamble, Preamble,
};
use tensogram_encodings::pipeline;

/// A streaming encoder that writes Tensogram frames progressively to a sink.
///
/// Unlike [`crate::encode::encode`], which builds the entire message in memory,
/// `StreamingEncoder` writes each data object frame immediately. This allows
/// encoding to a socket or pipe without buffering the full message.
///
/// The trade-off is that header-based index and hash frames are not possible;
/// instead, these are written as footer frames when [`finish`](StreamingEncoder::finish)
/// is called.
///
/// # Example
/// ```no_run
/// use std::io::BufWriter;
/// use std::fs::File;
/// use tensogram::streaming::StreamingEncoder;
/// use tensogram::{GlobalMetadata, EncodeOptions};
///
/// let file = BufWriter::new(File::create("output.tgm").unwrap());
/// let meta = GlobalMetadata::default();
/// let mut enc = StreamingEncoder::new(file, &meta, &EncodeOptions::default()).unwrap();
/// // enc.write_object(&desc, &data).unwrap();
/// // enc.finish().unwrap();
/// ```
pub struct StreamingEncoder<W: Write> {
    writer: W,
    /// Byte offsets of each data object frame from message start.
    object_offsets: Vec<u64>,
    /// Total byte length of each data object frame, excluding alignment padding.
    object_lengths: Vec<u64>,
    /// Per-object hash entries: (hash_type, hash_value).
    hash_entries: Vec<Option<(String, String)>>,
    /// Descriptors of completed objects (payloads not retained) — used to
    /// populate per-object payload entries in the footer metadata frame.
    completed_objects: Vec<EncodedObject>,
    /// Total bytes written so far.
    bytes_written: u64,
    /// Whether to compute frame-body integrity hashes (xxh3-64).
    /// True when [`EncodeOptions.hashing`] was true at construction.
    hashing: bool,
    /// Whether to emit an aggregate `FooterHash` frame at finish-time
    /// (v3).  The header-hash aggregate is unavailable in streaming
    /// mode — if the caller's [`AggregateHashPolicy`] resolves to
    /// `Header` or `Both`, [`StreamingEncoder::new`] errors at
    /// construction time.
    ///
    /// [`AggregateHashPolicy`]: crate::encode::AggregateHashPolicy
    emit_footer_hash_frame: bool,
    /// Original global metadata — re-used to build the footer metadata frame.
    global_meta: GlobalMetadata,
    /// True when a PrecederMetadata frame has been written but the
    /// corresponding DataObject has not yet been written.
    pending_preceder: bool,
    /// Per-object preceder payloads — stored so the footer metadata can
    /// include all per-object metadata (for decoders that skip preceders).
    preceder_payloads: Vec<Option<BTreeMap<String, ciborium::Value>>>,
    /// Intra-codec thread budget resolved from `EncodeOptions.threads`
    /// at construction time.  Passed through to every `write_object`
    /// pipeline call; axis A is not applicable in streaming mode
    /// because each `write_object` is a separate caller-paced event.
    intra_codec_threads: u32,
    /// Snapshot of the parallel-threshold option for the same reason.
    parallel_threshold_bytes: Option<usize>,
    /// Snapshot of `EncodeOptions.allow_nan` / `allow_inf` — determines
    /// whether `write_object` substitutes non-finite values and emits
    /// a mask companion section (see `plans/WIRE_FORMAT.md` §6.5).
    allow_nan: bool,
    allow_inf: bool,
    /// Per-kind mask compression method snapshots.
    nan_mask_method: MaskMethod,
    pos_inf_mask_method: MaskMethod,
    neg_inf_mask_method: MaskMethod,
    /// Snapshot of the small-mask fallback threshold.
    small_mask_threshold_bytes: usize,
}

impl<W: Write> StreamingEncoder<W> {
    /// Begin a new streaming message.
    ///
    /// Writes the preamble (with `total_length = 0` for streaming mode)
    /// and a header metadata frame containing the global metadata.
    pub fn new(
        mut writer: W,
        global_meta: &GlobalMetadata,
        options: &EncodeOptions,
    ) -> Result<Self> {
        // Strict-input contract: streaming mode rejects placements it
        // cannot satisfy (`Header`, `Both`).  `Auto` resolves to
        // `Footer` — the only valid placement when the header is
        // written before any data object.
        //
        // Earlier versions silently coerced `create_header_hashes=true`
        // into a footer hash, which made the docs disagree with the
        // code.  The new contract surfaces the mismatch as a typed
        // error at construction time.
        let resolved = options.aggregate_hash.resolved_streaming()?;
        let emit_footer_hash = options.hashing && resolved.emits_footer();

        let mut bytes_written = 0u64;
        write_preamble_and_header(
            &mut writer,
            &mut bytes_written,
            global_meta,
            options.hashing,
            emit_footer_hash,
        )?;

        // Snapshot the thread budget now so that mid-message changes to
        // TENSOGRAM_THREADS don't leak in between write_object calls —
        // one message is deterministic.
        let intra_codec_threads = crate::parallel::resolve_budget(options.threads)?;

        Ok(Self {
            writer,
            object_offsets: Vec::new(),
            object_lengths: Vec::new(),
            hash_entries: Vec::new(),
            completed_objects: Vec::new(),
            bytes_written,
            hashing: options.hashing,
            emit_footer_hash_frame: emit_footer_hash,
            global_meta: global_meta.clone(),
            pending_preceder: false,
            preceder_payloads: Vec::new(),
            intra_codec_threads,
            parallel_threshold_bytes: options.parallel_threshold_bytes,
            allow_nan: options.allow_nan,
            allow_inf: options.allow_inf,
            nan_mask_method: options.nan_mask_method.clone(),
            pos_inf_mask_method: options.pos_inf_mask_method.clone(),
            neg_inf_mask_method: options.neg_inf_mask_method.clone(),
            small_mask_threshold_bytes: options.small_mask_threshold_bytes,
        })
    }

    /// Write a PrecederMetadata frame for the next data object.
    ///
    /// The `metadata` map becomes `base[0]` in a `GlobalMetadata` CBOR
    /// wrapper.  Must be followed by exactly one
    /// [`write_object`](Self::write_object) or
    /// [`write_object_pre_encoded`](Self::write_object_pre_encoded) call
    /// before another `write_preceder` or [`finish`](Self::finish).
    pub fn write_preceder(&mut self, metadata: BTreeMap<String, ciborium::Value>) -> Result<()> {
        if self.pending_preceder {
            return Err(TensogramError::Framing(
                "write_preceder called twice without an intervening write_object/write_object_pre_encoded".to_string(),
            ));
        }

        // Reject _reserved_ in preceder metadata — this namespace is library-managed
        // and would collide with the encoder's auto-populated _reserved_.tensor.
        if metadata.contains_key(RESERVED_KEY) {
            return Err(TensogramError::Metadata(format!(
                "client code must not write '{RESERVED_KEY}' in preceder metadata; \
                     this field is populated by the library"
            )));
        }

        let preceder_meta = GlobalMetadata {
            base: vec![metadata.clone()],
            ..Default::default()
        };
        let cbor = crate::metadata::global_metadata_to_cbor(&preceder_meta)?;
        let frame_bytes = build_frame(FrameType::PrecederMetadata, 1, 0, &cbor, self.hashing);
        self.writer.write_all(&frame_bytes)?;
        self.bytes_written += frame_bytes.len() as u64;

        write_padding(&mut self.writer, &mut self.bytes_written)?;

        self.pending_preceder = true;
        // Store for inclusion in footer metadata
        self.preceder_payloads.push(Some(metadata));
        Ok(())
    }

    /// Encode and write a single data object frame.
    ///
    /// The descriptor's encoding/filter/compression pipeline is applied,
    /// the payload is hashed (if configured), and the frame is written
    /// immediately — no buffering.
    ///
    /// When `EncodeOptions.threads > 0` was passed to
    /// [`StreamingEncoder::new`], the pipeline call may use up to that
    /// many threads internally (axis B).  Axis A is not available in
    /// streaming mode — each `write_object` is a caller-paced event
    /// with no cross-object parallelism opportunity.
    pub fn write_object(&mut self, desc: &DataObjectDescriptor, data: &[u8]) -> Result<()> {
        validate_object(desc, data.len())?;

        let num_elements = desc.num_elements()?;

        // Honour the intra-codec thread budget captured at construction.
        // Small-message threshold: if the payload is below the threshold,
        // skip the pool (the overhead would outweigh any codec win).
        let parallel = crate::parallel::should_parallelise(
            self.intra_codec_threads,
            data.len(),
            self.parallel_threshold_bytes,
        );

        // Pre-pipeline substitute-and-mask stage — see
        // [`crate::encode::encode_one_object`] for semantics.
        // `write_object_pre_encoded` treats its input as opaque and
        // bypasses the stage, matching buffered `encode_pre_encoded`.
        let (pipeline_input, mask_set) = substitute_and_mask::substitute_and_mask(
            data,
            desc.dtype,
            desc.byte_order,
            self.allow_nan,
            self.allow_inf,
            parallel,
        )?;
        let intra = if parallel {
            self.intra_codec_threads
        } else {
            0
        };

        // Resolve simple_packing params up front (auto-compute from the
        // ORIGINAL pre-substitute data when the user left out
        // sp_reference_value / sp_binary_scale_factor).  Must happen
        // BEFORE pipeline config construction because the pipeline reads
        // the four sp_* keys.  See the parallel comment in
        // encode::encode_one_object for why we use `data` rather than
        // `pipeline_input`.
        let mut final_desc = desc.clone();
        crate::encode::resolve_simple_packing_params(&mut final_desc, data)?;

        let config = crate::encode::build_pipeline_config_with_backend(
            &final_desc,
            num_elements,
            desc.dtype,
            tensogram_encodings::pipeline::CompressionBackend::default(),
            intra,
        )?;

        let result =
            crate::parallel::run_maybe_pooled(self.intra_codec_threads, parallel, intra, || {
                pipeline::encode_pipeline(pipeline_input.as_ref(), &config)
            })
            .map_err(|e| TensogramError::Encoding(e.to_string()))?;

        if let Some(offsets) = &result.block_offsets {
            final_desc.params.insert(
                "szip_block_offsets".to_string(),
                ciborium::Value::Array(
                    offsets
                        .iter()
                        .map(|&o| ciborium::Value::Integer(o.into()))
                        .collect(),
                ),
            );
        }

        // Compose the payload region: [encoded_payload][masks...] — see
        // `plans/WIRE_FORMAT.md` §6.5.  When no masks were produced
        // (the common case) this is a zero-cost passthrough.
        let (payload_region, masks_metadata) = crate::encode::compose_payload_region(
            result.encoded_bytes,
            mask_set,
            &self.nan_mask_method,
            &self.pos_inf_mask_method,
            &self.neg_inf_mask_method,
            self.small_mask_threshold_bytes,
        )?;
        if let Some(m) = masks_metadata {
            final_desc.masks = Some(m);
        }

        self.write_object_inner(final_desc, &payload_region)
    }

    /// Write a pre-encoded data object frame directly.
    ///
    /// Unlike [`write_object`](Self::write_object), this method does **not**
    /// run the encoding pipeline — `pre_encoded_bytes` are written to the
    /// stream as-is.  The descriptor must accurately describe the encoding
    /// that was already applied (encoding, filter, compression, params) so
    /// that decoders can reconstruct the original payload.
    ///
    /// This method participates in the same preceder consumption logic as
    /// [`write_object`](Self::write_object) and can be freely intermixed
    /// with it.
    ///
    /// # Errors
    ///
    /// Returns an error if the descriptor is invalid or the frame cannot be
    /// written to the underlying writer.
    #[tracing::instrument(skip(self, descriptor, pre_encoded_bytes))]
    pub fn write_object_pre_encoded(
        &mut self,
        descriptor: &DataObjectDescriptor,
        pre_encoded_bytes: &[u8],
    ) -> Result<()> {
        validate_object(descriptor, pre_encoded_bytes.len())?;

        let num_elements = descriptor.num_elements()?;

        // Validate descriptor pipeline configuration without encoding.
        build_pipeline_config(descriptor, num_elements, descriptor.dtype)?;

        // Validate szip metadata — same checks as buffered encode_pre_encoded.
        validate_no_szip_offsets_for_non_szip(descriptor)?;
        if descriptor.compression == "szip" && descriptor.params.contains_key("szip_block_offsets")
        {
            validate_szip_block_offsets(&descriptor.params, pre_encoded_bytes.len())?;
        }

        self.write_object_inner(descriptor.clone(), pre_encoded_bytes)
    }

    /// Shared inner implementation for both [`write_object`](Self::write_object) and
    /// [`write_object_pre_encoded`](Self::write_object_pre_encoded).
    ///
    /// Writes the data object frame directly to the sink and — when a hash
    /// algorithm is configured — computes the xxh3-64 digest inline with
    /// the payload write.  The payload bytes are therefore walked exactly
    /// once: no intermediate frame buffer is built in memory.
    ///
    /// Updates all bookkeeping and consumes any pending preceder.
    fn write_object_inner(
        &mut self,
        mut final_desc: DataObjectDescriptor,
        encoded_bytes: &[u8],
    ) -> Result<()> {
        let start_offset = self.bytes_written;

        let (frame_len, inline_digest) = write_data_object_frame_hashed(
            &mut self.writer,
            &mut final_desc,
            encoded_bytes,
            self.hashing,
        )?;
        self.bytes_written += frame_len;

        // v3: the per-object hash lives in the inline slot of the
        // frame footer (see `plans/WIRE_FORMAT.md` §2.4).  The
        // streaming encoder captures each object's digest in
        // `hash_entries` as it's written so the aggregate
        // `FooterHash` frame can be emitted at `finish()` without
        // a second pass over the payload.
        let hash_entry = inline_digest.map(|d| {
            (
                crate::hash::HASH_ALGORITHM_NAME.to_string(),
                crate::hash::format_xxh3_digest(d),
            )
        });

        self.object_offsets.push(start_offset);
        self.object_lengths.push(frame_len);
        self.hash_entries.push(hash_entry);
        // Retain only the descriptor for footer metadata population.
        // The encoded payload has already been written to the stream;
        // keeping it in memory would negate streaming's memory benefits.
        self.completed_objects.push(EncodedObject {
            descriptor: final_desc,
            encoded_payload: Vec::new(),
        });

        // Consume pending preceder — if no preceder was written for this
        // object, record None so preceder_payloads stays aligned with objects.
        if self.pending_preceder {
            self.pending_preceder = false;
        } else {
            self.preceder_payloads.push(None);
        }

        // Align to 8 bytes
        write_padding(&mut self.writer, &mut self.bytes_written)?;

        Ok(())
    }

    /// Finalize the streaming message.
    ///
    /// Writes footer frames (payload metadata + hash + index) and the postamble.
    /// Consumes the encoder and returns the underlying writer.
    pub fn finish(mut self) -> Result<W> {
        self.write_footer_frames_and_postamble()?;
        self.writer.flush()?;
        Ok(self.writer)
    }

    /// Write footer frames (metadata + optional hash + index) and the postamble
    /// to the underlying writer.
    ///
    /// This is the shared inner implementation for [`finish`](Self::finish),
    /// [`finish_with_backfill`](StreamingEncoder::finish_with_backfill), and
    /// [`finish_and_reset`](StreamingEncoder::finish_and_reset).  It operates
    /// on `&mut self` so that the specialized `finish_and_reset` variants can
    /// reset the encoder state and write a new preamble without consuming the
    /// encoder.
    ///
    /// After a successful call the accumulated per-message bookkeeping
    /// (`object_offsets`, `object_lengths`) is consumed by the index frame
    /// (via [`std::mem::take`]) and left empty; callers that reset the encoder
    /// must also clear the remaining fields.
    fn write_footer_frames_and_postamble(&mut self) -> Result<()> {
        if self.pending_preceder {
            return Err(TensogramError::Framing(
                "dangling PrecederMetadata: finish called without a following write_object/write_object_pre_encoded"
                    .to_string(),
            ));
        }

        let footer_start = self.bytes_written;

        // Footer metadata frame: updated global metadata with per-object payload entries.
        // The header metadata was written without knowing the objects; here we write
        // a footer metadata frame that supersedes it with payload populated.
        // Preceder payloads are merged in so the footer is complete even for
        // decoders that skip PrecederMetadata frames.
        {
            let mut enriched_meta = self.global_meta.clone();
            populate_base_entries(&mut enriched_meta.base, &self.completed_objects);
            populate_reserved_provenance(&mut enriched_meta.reserved);

            // Merge preceder payloads into footer metadata base entries
            // (preceder wins).  preceder_payloads is aligned 1:1 with
            // completed_objects by write_preceder/write_object bookkeeping,
            // so the lengths must match.
            if self.preceder_payloads.len() != self.completed_objects.len() {
                return Err(TensogramError::Framing(format!(
                    "internal: preceder_payloads ({}) out of sync with completed_objects ({})",
                    self.preceder_payloads.len(),
                    self.completed_objects.len()
                )));
            }
            for (i, prec) in self.preceder_payloads.iter().enumerate() {
                if let Some(prec_map) = prec
                    && i < enriched_meta.base.len()
                {
                    for (k, v) in prec_map {
                        enriched_meta.base[i].insert(k.clone(), v.clone());
                    }
                }
            }
            let meta_cbor = metadata::global_metadata_to_cbor(&enriched_meta)?;
            let frame_bytes =
                build_frame(FrameType::FooterMetadata, 1, 0, &meta_cbor, self.hashing);
            self.writer.write_all(&frame_bytes)?;
            self.bytes_written += frame_bytes.len() as u64;
            write_padding(&mut self.writer, &mut self.bytes_written)?;
        }

        // Footer hash frame — only when the caller opted in via
        // `EncodeOptions.aggregate_hash` (resolved to `Footer` for
        // streaming mode at construction time).
        if self.emit_footer_hash_frame && self.hash_entries.iter().any(|e| e.is_some()) {
            let algorithm = if self.hashing {
                crate::hash::HASH_ALGORITHM_NAME.to_string()
            } else {
                String::new()
            };
            let hashes: Vec<String> = self
                .hash_entries
                .iter()
                .map(|e| e.as_ref().map(|(_, v)| v.clone()).unwrap_or_default())
                .collect();
            let hash_frame = HashFrame { algorithm, hashes };
            let hash_cbor = metadata::hash_frame_to_cbor(&hash_frame)?;
            let frame_bytes = build_frame(FrameType::FooterHash, 1, 0, &hash_cbor, self.hashing);
            self.writer.write_all(&frame_bytes)?;
            self.bytes_written += frame_bytes.len() as u64;

            write_padding(&mut self.writer, &mut self.bytes_written)?;
        }

        // Footer index frame — `mem::take` moves the vecs out without cloning,
        // leaving empty vecs in `self` (relevant for finish_and_reset callers).
        let index = IndexFrame {
            offsets: std::mem::take(&mut self.object_offsets),
            lengths: std::mem::take(&mut self.object_lengths),
        };
        let index_cbor = metadata::index_to_cbor(&index)?;
        let frame_bytes = build_frame(FrameType::FooterIndex, 1, 0, &index_cbor, self.hashing);
        self.writer.write_all(&frame_bytes)?;
        self.bytes_written += frame_bytes.len() as u64;

        write_padding(&mut self.writer, &mut self.bytes_written)?;

        // Postamble.
        //
        // Streaming mode writes `total_length = 0` by default — the
        // writer is typed `W: Write` and we cannot seek back to
        // rewrite the preamble.  Callers with a seekable sink can
        // opt into back-filling via `finish_with_backfill` (below),
        // which populates both the preamble's and postamble's
        // `total_length` fields.
        let postamble = Postamble {
            first_footer_offset: footer_start,
            total_length: 0,
        };
        let mut postamble_bytes = Vec::with_capacity(POSTAMBLE_SIZE);
        postamble.write_to(&mut postamble_bytes);
        self.writer.write_all(&postamble_bytes)?;
        self.bytes_written += postamble_bytes.len() as u64;

        Ok(())
    }

    /// Returns the number of data objects written so far.
    pub fn object_count(&self) -> usize {
        self.object_offsets.len()
    }

    /// Returns the total bytes written so far.
    pub fn bytes_written(&self) -> u64 {
        self.bytes_written
    }
}

// ── Seekable-sink specialisation ─────────────────────────────────────────────

impl<W: Write + std::io::Seek> StreamingEncoder<W> {
    /// Finalize the streaming message and back-fill the `total_length`
    /// field in both the preamble and postamble (v3 §7 and §9.4).
    ///
    /// Equivalent to [`finish`] but at the end seeks back to the
    /// preamble offset (0) and the postamble's `total_length` slot
    /// (`end_pos - 16`), writing the real message length to both.
    /// Readers can then backward-scan from EOF using the mirrored
    /// length without fallback to forward scanning.
    ///
    /// Use this when the writer backs a seekable sink (file, cursor
    /// over a buffer) — it's a no-op semantic change over
    /// `finish()` but enables O(1) backward scan on the produced
    /// file.
    ///
    /// # Errors
    ///
    /// Returns any I/O error from the underlying writer's `seek`,
    /// `write_all`, or `flush`.
    ///
    /// [`finish`]: StreamingEncoder::finish
    pub fn finish_with_backfill(mut self) -> Result<W> {
        use std::io::SeekFrom;
        self.write_footer_frames_and_postamble()?;

        // Determine the current file position, which equals the full
        // message length after the footer + postamble have been written.
        let end_pos = self.writer.stream_position()?;
        let total_length = end_pos;

        // Back-fill the preamble's total_length (bytes 16..24).
        self.writer.seek(SeekFrom::Start(16))?;
        self.writer.write_all(&total_length.to_be_bytes())?;

        // Back-fill the postamble's total_length (second u64 field,
        // located at `end - 16 .. end - 8`; the trailing 8 bytes are
        // the END_MAGIC).
        self.writer.seek(SeekFrom::Start(end_pos - 16))?;
        self.writer.write_all(&total_length.to_be_bytes())?;

        // Seek back to EOF so the returned writer matches
        // `finish()`'s post-condition (cursor at the very end).
        self.writer.seek(SeekFrom::Start(end_pos))?;
        self.writer.flush()?;
        Ok(self.writer)
    }
}

// ── In-memory cursor specialisation — finish_and_reset ───────────────────────

impl StreamingEncoder<std::io::Cursor<Vec<u8>>> {
    /// Finalise the current message and immediately begin the next one.
    ///
    /// Returns the wire bytes of the completed message (streaming mode:
    /// `total_length = 0` in both preamble and postamble, matching the
    /// [`finish`](StreamingEncoder::finish) contract).
    ///
    /// The encoder's accumulated per-message state is cleared and a fresh
    /// preamble + header metadata frame are written so the same encoder
    /// object is immediately ready for the next sequence of
    /// [`write_object`](StreamingEncoder::write_object) calls.
    ///
    /// This is semantically equivalent to:
    /// ```no_run
    /// # use tensogram::streaming::StreamingEncoder;
    /// # use tensogram::{GlobalMetadata, EncodeOptions};
    /// # let meta = GlobalMetadata::default();
    /// # let opts = EncodeOptions::default();
    /// # let mut enc = StreamingEncoder::new(
    /// #     std::io::Cursor::new(Vec::new()), &meta, &opts).unwrap();
    /// let bytes: Vec<u8> = enc.finish().unwrap().into_inner();
    /// enc = StreamingEncoder::new(
    ///     std::io::Cursor::new(Vec::new()), &meta, &opts).unwrap();
    /// ```
    /// but without re-allocating the encoder object.
    ///
    /// # Errors
    ///
    /// Returns [`TensogramError::Framing`] if a
    /// [`write_preceder`](StreamingEncoder::write_preceder) call has no
    /// corresponding [`write_object`](StreamingEncoder::write_object)
    /// (dangling preceder).
    pub fn finish_and_reset(&mut self) -> Result<Vec<u8>> {
        self.write_footer_frames_and_postamble()?;
        self.writer.flush()?;
        let finished_bytes = self.writer.get_ref().clone();
        self.reset_message_state()?;
        Ok(finished_bytes)
    }

    /// Finalise the current message with back-filled length fields and
    /// immediately begin the next one.
    ///
    /// Like [`finish_and_reset`](Self::finish_and_reset) but patches
    /// `total_length` in both the preamble (byte offset 16) and the
    /// postamble (byte offset `end - 16`) before returning the bytes,
    /// satisfying the backward-locatability invariant from wire-format §7.
    ///
    /// Readers can then O(1) backward-scan the concatenated output produced
    /// by repeated `finish_and_reset_with_backfill` calls; use this variant
    /// when the multi-message sequence must support the bidirectional remote
    /// walker.
    ///
    /// # Errors
    ///
    /// Returns [`TensogramError::Framing`] if a
    /// [`write_preceder`](StreamingEncoder::write_preceder) call has no
    /// corresponding [`write_object`](StreamingEncoder::write_object)
    /// (dangling preceder).
    pub fn finish_and_reset_with_backfill(&mut self) -> Result<Vec<u8>> {
        use std::io::{Seek, SeekFrom};
        self.write_footer_frames_and_postamble()?;

        // Back-fill total_length in preamble and postamble.
        let end_pos = self.writer.stream_position()?;
        let total_length = end_pos;
        self.writer.seek(SeekFrom::Start(16))?;
        self.writer.write_all(&total_length.to_be_bytes())?;
        self.writer.seek(SeekFrom::Start(end_pos - 16))?;
        self.writer.write_all(&total_length.to_be_bytes())?;
        self.writer.seek(SeekFrom::Start(end_pos))?;
        self.writer.flush()?;

        let finished_bytes = self.writer.get_ref().clone();
        self.reset_message_state()?;
        Ok(finished_bytes)
    }

    /// Clear all per-message accumulated state and write a fresh preamble +
    /// header metadata frame so the encoder is ready for the next message.
    fn reset_message_state(&mut self) -> Result<()> {
        // Replace the cursor with a fresh empty buffer.
        self.writer = std::io::Cursor::new(Vec::new());
        // Clear per-message bookkeeping.  object_offsets and object_lengths
        // were already consumed by write_footer_frames_and_postamble via
        // mem::take, but we clear them defensively.
        self.object_offsets.clear();
        self.object_lengths.clear();
        self.hash_entries.clear();
        self.completed_objects.clear();
        self.pending_preceder = false;
        self.preceder_payloads.clear();
        self.bytes_written = 0;
        // Snapshot fields that cannot be borrowed while self.writer is mutably
        // borrowed by write_preamble_and_header.
        let global_meta = self.global_meta.clone();
        let hashing = self.hashing;
        let emit_footer_hash_frame = self.emit_footer_hash_frame;
        write_preamble_and_header(
            &mut self.writer,
            &mut self.bytes_written,
            &global_meta,
            hashing,
            emit_footer_hash_frame,
        )
    }
}

// ── Helpers ──────────────────────────────────────────────────────────────────

/// Write the streaming-mode preamble (`total_length = 0`) and the
/// `HeaderMetadata` frame to `writer`, updating `bytes_written` accordingly.
///
/// Extracted from [`StreamingEncoder::new`] so it can also be called by
/// [`StreamingEncoder::reset_message_state`] to begin a fresh message after
/// [`StreamingEncoder::finish_and_reset`].
fn write_preamble_and_header<W: Write>(
    writer: &mut W,
    bytes_written: &mut u64,
    global_meta: &GlobalMetadata,
    hashing: bool,
    emit_footer_hash_frame: bool,
) -> Result<()> {
    let (preamble_bytes, frame_bytes) =
        build_preamble_and_header_bytes(global_meta, hashing, emit_footer_hash_frame)?;
    writer.write_all(&preamble_bytes)?;
    *bytes_written += preamble_bytes.len() as u64;
    writer.write_all(&frame_bytes)?;
    *bytes_written += frame_bytes.len() as u64;
    write_padding(writer, bytes_written)?;
    Ok(())
}

/// Build the streaming-mode preamble + `HeaderMetadata` frame bytes.
///
/// Returns `(preamble_bytes, header_frame_bytes)`.  Shared by the sync
/// [`StreamingEncoder`] and the async
/// [`crate::streaming_async::AsyncStreamingEncoder`] so both produce
/// byte-identical output.
pub(crate) fn build_preamble_and_header_bytes(
    global_meta: &GlobalMetadata,
    hashing: bool,
    emit_footer_hash_frame: bool,
) -> Result<(Vec<u8>, Vec<u8>)> {
    let meta_cbor = metadata::global_metadata_to_cbor(global_meta)?;

    // Streaming preamble: total_length=0 signals unknown length at write time.
    // Always set PRECEDER_METADATA in streaming mode — the flag is advisory
    // and decoders handle the absence of actual preceder frames gracefully.
    let mut flags = MessageFlags::default();
    flags.set(MessageFlags::HEADER_METADATA);
    flags.set(MessageFlags::FOOTER_METADATA);
    flags.set(MessageFlags::FOOTER_INDEX);
    flags.set(MessageFlags::PRECEDER_METADATA);
    if hashing {
        // v3: HASHES_PRESENT signals per-frame inline hash slots
        // are populated; set whenever hashing is enabled.
        flags.set(MessageFlags::HASHES_PRESENT);
        if emit_footer_hash_frame {
            flags.set(MessageFlags::FOOTER_HASHES);
        }
    }

    let preamble = Preamble {
        version: crate::wire::WIRE_VERSION,
        flags,
        reserved: 0,
        total_length: 0,
    };
    let preamble_bytes = preamble_to_bytes(&preamble);
    let frame_bytes = build_frame(FrameType::HeaderMetadata, 1, 0, &meta_cbor, hashing);
    Ok((preamble_bytes, frame_bytes))
}

fn preamble_to_bytes(preamble: &Preamble) -> Vec<u8> {
    let mut out = Vec::with_capacity(PREAMBLE_SIZE);
    preamble.write_to(&mut out);
    out
}

/// Number of zero-padding bytes needed to align `bytes_written` to an
/// 8-byte boundary.  Used by the async sibling's padding helper; the
/// sync side here uses [`write_padding`] directly with a static
/// `ZERO_PAD` slice.  Gated on the async feature so default-feature
/// clippy doesn't flag it as dead code.
#[cfg(feature = "async")]
pub(crate) const fn padding_for(bytes_written: u64) -> usize {
    (8 - (bytes_written as usize % 8)) % 8
}

/// Build a non-data-object frame on the wire (v3).
///
/// Layout: `[frame header 16][payload][hash u64][ENDF]` — the
/// 12-byte common tail `[hash][ENDF]` is appended automatically.
/// When `hashing` is true, the frame header gains the
/// [`FrameFlags::HASH_PRESENT`] bit and the slot holds the
/// xxh3-64 digest of `payload` (including any legitimate zero
/// digest).  When false, the flag bit is clear and the slot is
/// written as zero by convention; readers must dispatch on the
/// per-frame flag rather than inspecting the slot value.
pub(crate) fn build_frame(
    frame_type: FrameType,
    version: u16,
    flags: u16,
    payload: &[u8],
    hashing: bool,
) -> Vec<u8> {
    debug_assert!(
        !frame_type.is_data_object(),
        "streaming::build_frame is for non-data-object frames only"
    );
    let total_length = (FRAME_HEADER_SIZE + payload.len() + FRAME_COMMON_FOOTER_SIZE) as u64;
    let header_flags = if hashing {
        flags | FrameFlags::HASH_PRESENT
    } else {
        flags & !FrameFlags::HASH_PRESENT
    };
    let fh = FrameHeader {
        frame_type,
        version,
        flags: header_flags,
        total_length,
    };
    let mut out = Vec::with_capacity(total_length as usize);
    fh.write_to(&mut out);
    out.extend_from_slice(payload);

    // Inline hash slot (8 bytes); see frame doc-comment above and
    // `plans/WIRE_FORMAT.md` §2.5 for the slot/flag contract.
    let hash_value: u64 = if hashing {
        xxhash_rust::xxh3::xxh3_64(payload)
    } else {
        0
    };
    out.extend_from_slice(&hash_value.to_be_bytes());
    out.extend_from_slice(FRAME_END);
    out
}

const ZERO_PAD: [u8; 7] = [0; 7];

fn write_padding(writer: &mut impl Write, bytes_written: &mut u64) -> std::io::Result<()> {
    let pad = (8 - (*bytes_written as usize % 8)) % 8;
    if pad > 0 {
        writer.write_all(&ZERO_PAD[..pad])?;
        *bytes_written += pad as u64;
    }
    Ok(())
}

/// Write a data object frame directly to `writer` while optionally hashing
/// the payload in a single pass.
///
/// This is the streaming equivalent of
/// [`crate::framing::encode_data_object_frame`], but instead of allocating a
/// `Vec<u8>` for the whole frame it streams pieces straight to the sink.
/// When `hash_algorithm` is `Some(_)`, the payload is fed to an
/// `Xxh3Default` hasher in 64 KiB chunks as it is written, so the payload
/// is walked exactly once end-to-end.
///
/// v3 frame layout (always `CBOR_AFTER_PAYLOAD`):
///
/// ```text
/// [FrameHeader 16B][Payload][CBOR][cbor_offset 8B][hash 8B][ENDF 4B]
/// ```
///
/// The inline hash slot is populated with the xxh3-64 digest of the
/// frame *body* (`payload + CBOR` in this layout — the hash scope
/// excludes the 16-byte header and the 20-byte type-specific
/// footer; see `plans/WIRE_FORMAT.md` §2.4).  In v3 the descriptor
/// no longer carries a `hash` field, so there is no CBOR-length
/// placeholder dance — the CBOR length is known exactly up front.
///
/// # Returns
///
/// `(total_length, inline_digest)` — the number of bytes written to
/// `writer` (excluding any trailing 8-byte alignment padding,
/// which is the caller's responsibility) and the raw xxh3-64
/// digest installed in the inline slot (or `None` when
/// `hash_algorithm` is `None`).
///
/// # Errors
///
/// * [`TensogramError::Framing`] if the frame's `total_length`
///   would overflow `u64`.  No bytes are written in that case.
/// * [`TensogramError::Metadata`] if CBOR serialisation fails.
/// * [`TensogramError::Io`] on any `writer` failure — partial
///   writes may already be on the sink.
fn write_data_object_frame_hashed<W: Write>(
    writer: &mut W,
    descriptor: &mut DataObjectDescriptor,
    payload: &[u8],
    hashing: bool,
) -> Result<(u64, Option<u64>)> {
    use crate::wire::{DATA_OBJECT_FOOTER_SIZE, DataObjectFlags, FRAME_END};

    // Serialise the CBOR descriptor up front — in v3 its length is
    // deterministic (no hash placeholder needed).
    let cbor_bytes = metadata::object_descriptor_to_cbor(descriptor)?;
    let cbor_len = cbor_bytes.len();
    let payload_len = payload.len();

    // Compute `total_length` with checked arithmetic.  An overflow
    // (only reachable on pathological 32-bit inputs) becomes a
    // clean `TensogramError::Framing` before any bytes are written.
    let total_length = (FRAME_HEADER_SIZE as u64)
        .checked_add(cbor_len as u64)
        .and_then(|n| n.checked_add(payload_len as u64))
        .and_then(|n| n.checked_add(DATA_OBJECT_FOOTER_SIZE as u64))
        .ok_or_else(|| {
            TensogramError::Framing(format!(
                "data object frame total_length overflows u64 \
                 (payload {payload_len} bytes, CBOR {cbor_len} bytes, \
                 framing {} bytes)",
                FRAME_HEADER_SIZE + DATA_OBJECT_FOOTER_SIZE
            ))
        })?;

    // ── 1) Frame header ──────────────────────────────────────────────
    //
    // v3 emits `NTensorFrame` (type 9).  Bit 0 = CBOR_AFTER_PAYLOAD
    // (always set — streaming layout puts payload first).  Bit 1 =
    // HASH_PRESENT, set when `hashing` is true so a reader can
    // identify the slot's contents from the frame header alone
    // without consulting the message preamble.
    let mut header_flags = DataObjectFlags::CBOR_AFTER_PAYLOAD;
    if hashing {
        header_flags |= FrameFlags::HASH_PRESENT;
    }
    let mut header_bytes = Vec::with_capacity(FRAME_HEADER_SIZE);
    FrameHeader {
        frame_type: FrameType::NTensorFrame,
        version: 1,
        flags: header_flags,
        total_length,
    }
    .write_to(&mut header_bytes);
    writer.write_all(&header_bytes)?;

    // ── 2) Payload — single walk, hashing inline in 64 KiB chunks ────
    //
    // The inline hash covers the frame *body* = payload + CBOR
    // (v3 §2.4).  We hash the payload chunks as we write them and
    // then fold the CBOR bytes into the same hasher in step 3.
    const CHUNK: usize = 64 * 1024;
    let mut inline_hasher: Option<xxhash_rust::xxh3::Xxh3Default> =
        hashing.then(xxhash_rust::xxh3::Xxh3Default::new);
    let mut offset = 0;
    while offset < payload_len {
        let end = (offset + CHUNK).min(payload_len);
        let chunk = &payload[offset..end];
        if let Some(h) = &mut inline_hasher {
            h.update(chunk);
        }
        writer.write_all(chunk)?;
        offset = end;
    }

    // ── 3) CBOR descriptor — written, then folded into the hash ─────
    writer.write_all(&cbor_bytes)?;
    if let Some(h) = &mut inline_hasher {
        h.update(&cbor_bytes);
    }

    // ── 4) cbor_offset — NOT part of the hash scope ─────────────────
    let cbor_offset = (FRAME_HEADER_SIZE + payload_len) as u64;
    writer.write_all(&cbor_offset.to_be_bytes())?;

    // ── 5) hash slot (8 bytes) ──────────────────────────────────────
    //
    // Finalised digest of the body (payload + CBOR).  When hashing
    // is disabled the slot is zero-filled; readers distinguish
    // via the preamble-level HASHES_PRESENT flag.
    let digest = inline_hasher.as_mut().map(|h| h.digest());
    let hash_slot = digest.unwrap_or(0);
    writer.write_all(&hash_slot.to_be_bytes())?;

    // ── 6) ENDF terminator ──────────────────────────────────────────
    writer.write_all(FRAME_END)?;

    Ok((total_length, digest))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Dtype;
    use crate::decode::{DecodeOptions, decode};
    use crate::encode::{EncodeOptions, encode};
    use crate::types::{ByteOrder, DataObjectDescriptor};
    use std::collections::BTreeMap;

    fn make_descriptor(shape: Vec<u64>) -> DataObjectDescriptor {
        let ndim = shape.len() as u64;
        let mut strides = vec![0u64; shape.len()];
        if !shape.is_empty() {
            strides[shape.len() - 1] = 1;
            for i in (0..shape.len() - 1).rev() {
                strides[i] = strides[i + 1] * shape[i + 1];
            }
        }
        DataObjectDescriptor {
            obj_type: "ntensor".to_string(),
            ndim,
            shape,
            strides,
            dtype: Dtype::Float32,
            byte_order: ByteOrder::native(),
            encoding: "none".to_string(),
            filter: "none".to_string(),
            compression: "none".to_string(),
            params: BTreeMap::new(),
            masks: None,
        }
    }

    #[test]
    fn streaming_single_object_round_trip() {
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);
        let data = vec![0u8; 4 * 4];

        // Streaming encode
        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();
        enc.write_object(&desc, &data).unwrap();
        let result = enc.finish().unwrap();

        // Decode should succeed
        let (_decoded_meta, objects) = decode(&result, &DecodeOptions::default()).unwrap();
        assert_eq!(objects.len(), 1);
        assert_eq!(objects[0].1, data);
    }

    #[test]
    fn streaming_multi_object_round_trip() {
        let meta = GlobalMetadata::default();
        let desc1 = make_descriptor(vec![4]);
        let desc2 = make_descriptor(vec![8]);
        let data1 = vec![1u8; 4 * 4];
        let data2 = vec![2u8; 8 * 4];

        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();
        enc.write_object(&desc1, &data1).unwrap();
        enc.write_object(&desc2, &data2).unwrap();
        assert_eq!(enc.object_count(), 2);
        let result = enc.finish().unwrap();

        let (_, objects) = decode(&result, &DecodeOptions::default()).unwrap();
        assert_eq!(objects.len(), 2);
        assert_eq!(objects[0].1, data1);
        assert_eq!(objects[1].1, data2);
    }

    #[test]
    fn streaming_matches_buffered_single_object() {
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);
        let data = vec![42u8; 4 * 4];
        let options = EncodeOptions {
            hashing: true,
            ..Default::default()
        };

        // Buffered encode
        let buffered = encode(&meta, &[(&desc, &data)], &options).unwrap();
        let (_buf_meta, buf_objects) = decode(&buffered, &DecodeOptions::default()).unwrap();

        // Streaming encode
        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &options).unwrap();
        enc.write_object(&desc, &data).unwrap();
        let streamed = enc.finish().unwrap();
        let (_str_meta, str_objects) = decode(&streamed, &DecodeOptions::default()).unwrap();

        // Data must match (wire bytes may differ due to header vs footer layout).
        assert_eq!(buf_objects.len(), str_objects.len());
        assert_eq!(buf_objects[0].0.shape, str_objects[0].0.shape);
        assert_eq!(buf_objects[0].0.dtype, str_objects[0].0.dtype);
        assert_eq!(buf_objects[0].1, str_objects[0].1);
        // v3: per-object hash lives in the frame footer's inline
        // slot, not the CBOR descriptor.  Phase 6 adds a slot-level
        // cross-check between the two encoders here.
    }

    /// After a streaming encode with hashing enabled, every frame
    /// in the message must carry a non-zero inline hash slot that
    /// matches `xxh3-64` of the frame body.  Pinned via
    /// [`crate::hash::verify_frame_hash`] which is also the
    /// validator's fast-path check.
    #[test]
    fn streaming_hash_verification() {
        use crate::framing::{decode_message, scan};
        use crate::hash::verify_frame_hash;
        use crate::wire::{FrameHeader, MessageFlags, Preamble};

        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);
        let data = vec![42u8; 4 * 4];
        let options = EncodeOptions {
            hashing: true,
            ..Default::default()
        };

        let mut enc = StreamingEncoder::new(Vec::new(), &meta, &options).unwrap();
        enc.write_object(&desc, &data).unwrap();
        let wire = enc.finish().unwrap();

        // HASHES_PRESENT must be set in the preamble.
        let preamble = Preamble::read_from(&wire).unwrap();
        assert!(
            preamble.flags.has(MessageFlags::HASHES_PRESENT),
            "streaming encode with hash_algorithm=Some must set HASHES_PRESENT"
        );

        // Verify every frame's inline hash slot against the body.
        let messages = scan(&wire);
        assert_eq!(messages.len(), 1);
        let (offset, len) = messages[0];
        let msg = &wire[offset..offset + len];
        let decoded = decode_message(msg).unwrap();

        for (_, _, _, frame_offset) in &decoded.objects {
            let frame = &msg[*frame_offset..];
            let fh = FrameHeader::read_from(frame).unwrap();
            let frame_bytes = &frame[..fh.total_length as usize];
            verify_frame_hash(frame_bytes, fh.frame_type, None)
                .expect("streaming data-object inline hash must verify");
        }
    }

    #[test]
    fn streaming_no_objects() {
        let meta = GlobalMetadata::default();
        let options = EncodeOptions {
            hashing: false,
            ..Default::default()
        };

        let buf = Vec::new();
        let enc = StreamingEncoder::new(buf, &meta, &options).unwrap();
        assert_eq!(enc.object_count(), 0);
        let result = enc.finish().unwrap();

        let (_decoded_meta, objects) = decode(&result, &DecodeOptions::default()).unwrap();
        assert_eq!(objects.len(), 0);
    }

    /// Threads budget on `StreamingEncoder` must not change the encoded
    /// payload for transparent pipelines.  This locks in the pass-3
    /// consistency: axis-B dispatch inside `write_object` is opt-in and
    /// transparent-codec output is byte-identical across thread counts.
    #[test]
    fn streaming_threads_byte_identical_transparent() {
        let meta = GlobalMetadata::default();
        // One large object — 200 KiB — above the 64 KiB default threshold.
        let desc = make_descriptor(vec![50_000]);
        let data: Vec<u8> = (0..50_000)
            .flat_map(|i| (250.0f32 + (i as f32).sin() * 30.0).to_ne_bytes())
            .collect();

        let mk = |threads: u32| -> Vec<u8> {
            let buf = Vec::new();
            let opts = EncodeOptions {
                threads,
                parallel_threshold_bytes: Some(0), // force parallel
                ..Default::default()
            };
            let mut enc = StreamingEncoder::new(buf, &meta, &opts).unwrap();
            enc.write_object(&desc, &data).unwrap();
            enc.finish().unwrap()
        };

        // Compare encoded payload bytes (ignore provenance).
        let payloads = |buf: &[u8]| -> Vec<Vec<u8>> {
            crate::framing::decode_message(buf)
                .unwrap()
                .objects
                .iter()
                .map(|(_, p, _, _)| p.to_vec())
                .collect()
        };

        let baseline = mk(0);
        let payloads_baseline = payloads(&baseline);

        for t in [1u32, 2, 4, 8] {
            let got = mk(t);
            assert_eq!(
                payloads_baseline,
                payloads(&got),
                "streaming threads={t} payload must match sequential"
            );
        }
    }

    #[test]
    fn streaming_with_metadata() {
        let mut extra = BTreeMap::new();
        extra.insert(
            "centre".to_string(),
            ciborium::Value::Text("ecmwf".to_string()),
        );
        let meta = GlobalMetadata {
            extra,
            ..Default::default()
        };

        let desc = make_descriptor(vec![4]);
        let data = vec![0u8; 4 * 4];

        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();
        enc.write_object(&desc, &data).unwrap();
        let result = enc.finish().unwrap();

        let (decoded_meta, _) = decode(&result, &DecodeOptions::default()).unwrap();
        assert_eq!(
            decoded_meta.extra.get("centre"),
            Some(&ciborium::Value::Text("ecmwf".to_string()))
        );
    }

    // ── PrecederMetadata tests ───────────────────────────────────────────

    #[test]
    fn streaming_preceder_round_trip() {
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);
        let data = vec![42u8; 4 * 4];

        let mut prec = BTreeMap::new();
        prec.insert(
            "mars".to_string(),
            ciborium::Value::Map(vec![(
                ciborium::Value::Text("param".to_string()),
                ciborium::Value::Text("2t".to_string()),
            )]),
        );

        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();
        enc.write_preceder(prec).unwrap();
        enc.write_object(&desc, &data).unwrap();
        let result = enc.finish().unwrap();

        let (decoded_meta, objects) = decode(&result, &DecodeOptions::default()).unwrap();
        assert_eq!(objects.len(), 1);
        assert_eq!(objects[0].1, data);

        // Preceder mars keys should be in base[0]
        let mars = decoded_meta.base[0].get("mars");
        assert!(mars.is_some(), "mars key should be in base[0]");
    }

    #[test]
    fn streaming_preceder_wins_over_footer() {
        // Pre-populate global_meta.base[0] with a value — the preceder
        // should override it after decode.
        let mut footer_base = BTreeMap::new();
        footer_base.insert(
            "source".to_string(),
            ciborium::Value::Text("footer".to_string()),
        );
        let meta = GlobalMetadata {
            base: vec![footer_base],
            ..Default::default()
        };

        let mut prec = BTreeMap::new();
        prec.insert(
            "source".to_string(),
            ciborium::Value::Text("preceder".to_string()),
        );

        let desc = make_descriptor(vec![4]);
        let data = vec![0u8; 4 * 4];

        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();
        enc.write_preceder(prec).unwrap();
        enc.write_object(&desc, &data).unwrap();
        let result = enc.finish().unwrap();

        let (decoded_meta, _) = decode(&result, &DecodeOptions::default()).unwrap();
        let source = decoded_meta.base[0].get("source").and_then(|v| match v {
            ciborium::Value::Text(s) => Some(s.as_str()),
            _ => None,
        });
        assert_eq!(source, Some("preceder"), "preceder should win over footer");
    }

    #[test]
    fn streaming_consecutive_preceder_error() {
        let meta = GlobalMetadata::default();
        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();

        enc.write_preceder(BTreeMap::new()).unwrap();
        let result = enc.write_preceder(BTreeMap::new());
        assert!(
            result.is_err(),
            "two write_preceder calls without intervening write_object should fail"
        );
    }

    #[test]
    fn streaming_dangling_preceder_error() {
        let meta = GlobalMetadata::default();
        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();

        enc.write_preceder(BTreeMap::new()).unwrap();
        let result = enc.finish();
        assert!(
            result.is_err(),
            "finish with a dangling preceder should fail"
        );
    }

    #[test]
    fn streaming_mixed_objects_with_and_without_preceders() {
        let meta = GlobalMetadata::default();
        let desc0 = make_descriptor(vec![4]);
        let desc1 = make_descriptor(vec![8]);
        let data0 = vec![1u8; 4 * 4];
        let data1 = vec![2u8; 8 * 4];

        let mut prec = BTreeMap::new();
        prec.insert(
            "note".to_string(),
            ciborium::Value::Text("only for obj 0".to_string()),
        );

        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();
        // Object 0: with preceder
        enc.write_preceder(prec).unwrap();
        enc.write_object(&desc0, &data0).unwrap();
        // Object 1: without preceder
        enc.write_object(&desc1, &data1).unwrap();
        let result = enc.finish().unwrap();

        let (decoded_meta, objects) = decode(&result, &DecodeOptions::default()).unwrap();
        assert_eq!(objects.len(), 2);
        assert_eq!(objects[0].1, data0);
        assert_eq!(objects[1].1, data1);

        // base[0] should have preceder entry
        assert!(decoded_meta.base[0].contains_key("note"));
        // base[1] should NOT have it
        assert!(!decoded_meta.base[1].contains_key("note"));
    }

    #[test]
    fn streaming_preceder_metadata_preservation() {
        // Verify application metadata from preceder survives the full
        // encode → footer-merge → decode → preceder-merge path.
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![2]);
        let data = vec![0u8; 2 * 4];

        let mut prec = BTreeMap::new();
        prec.insert("units".to_string(), ciborium::Value::Text("K".to_string()));
        prec.insert(
            "mars".to_string(),
            ciborium::Value::Map(vec![
                (
                    ciborium::Value::Text("param".to_string()),
                    ciborium::Value::Text("2t".to_string()),
                ),
                (
                    ciborium::Value::Text("levtype".to_string()),
                    ciborium::Value::Text("sfc".to_string()),
                ),
            ]),
        );

        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();
        enc.write_preceder(prec).unwrap();
        enc.write_object(&desc, &data).unwrap();
        let result = enc.finish().unwrap();

        let (decoded_meta, _) = decode(&result, &DecodeOptions::default()).unwrap();
        let p = &decoded_meta.base[0];
        assert_eq!(
            p.get("units"),
            Some(&ciborium::Value::Text("K".to_string()))
        );
        assert!(p.contains_key("mars"));
        // Structural keys (ndim, shape) should be under _reserved_.tensor
        assert!(p.contains_key("_reserved_"));
    }

    // ── Edge case: preceder with _reserved_ rejected ─────────────────────

    #[test]
    fn streaming_preceder_with_reserved_rejected() {
        let meta = GlobalMetadata::default();
        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();

        let mut prec = BTreeMap::new();
        prec.insert("_reserved_".to_string(), ciborium::Value::Map(vec![]));

        let result = enc.write_preceder(prec);
        assert!(result.is_err(), "_reserved_ in preceder should be rejected");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("_reserved_"),
            "error should mention _reserved_: {err}"
        );
    }

    #[test]
    fn streaming_preceder_reserved_stripped_on_decode() {
        // If a non-standard producer includes _reserved_ in a preceder,
        // the decoder strips it rather than failing, and the encoder's
        // _reserved_.tensor is preserved.

        // Build a raw message with a preceder that contains _reserved_.
        // We bypass the encoder's validation by constructing frames manually.
        let mut prec_entry = BTreeMap::new();
        prec_entry.insert(
            "mars".to_string(),
            ciborium::Value::Map(vec![(
                ciborium::Value::Text("param".to_string()),
                ciborium::Value::Text("2t".to_string()),
            )]),
        );
        prec_entry.insert(
            "_reserved_".to_string(),
            ciborium::Value::Map(vec![(
                ciborium::Value::Text("rogue".to_string()),
                ciborium::Value::Text("bad".to_string()),
            )]),
        );

        // Encode normally to get a valid message first, then decode
        // and verify _reserved_ from preceder doesn't clobber.
        // We test via the framing level directly.
        let preceder_meta = GlobalMetadata {
            base: vec![prec_entry],
            ..Default::default()
        };
        let preceder_cbor = crate::metadata::global_metadata_to_cbor(&preceder_meta).unwrap();

        // Build a raw message with preceder + data object
        let desc_for_frame = make_descriptor(vec![4]);
        let payload = vec![0u8; 4 * 4];
        let frame =
            crate::framing::encode_data_object_frame(&desc_for_frame, &payload, false, false)
                .unwrap();

        // Footer metadata with _reserved_.tensor
        let mut footer_base = BTreeMap::new();
        let tensor_map = ciborium::Value::Map(vec![
            (
                ciborium::Value::Text("ndim".to_string()),
                ciborium::Value::Integer(1.into()),
            ),
            (
                ciborium::Value::Text("shape".to_string()),
                ciborium::Value::Array(vec![ciborium::Value::Integer(4.into())]),
            ),
            (
                ciborium::Value::Text("strides".to_string()),
                ciborium::Value::Array(vec![ciborium::Value::Integer(1.into())]),
            ),
            (
                ciborium::Value::Text("dtype".to_string()),
                ciborium::Value::Text("float32".to_string()),
            ),
        ]);
        footer_base.insert(
            "_reserved_".to_string(),
            ciborium::Value::Map(vec![(
                ciborium::Value::Text("tensor".to_string()),
                tensor_map,
            )]),
        );
        let footer_meta = GlobalMetadata {
            base: vec![footer_base],
            ..Default::default()
        };
        let footer_cbor = crate::metadata::global_metadata_to_cbor(&footer_meta).unwrap();

        // Assemble raw message (v3 frame footers = `[hash u64][ENDF]`).
        use crate::wire::*;
        let header_meta_cbor =
            crate::metadata::global_metadata_to_cbor(&GlobalMetadata::default()).unwrap();

        let mut out = Vec::new();
        out.extend_from_slice(&[0u8; PREAMBLE_SIZE]);

        // Header metadata
        let total_length =
            (FRAME_HEADER_SIZE + header_meta_cbor.len() + FRAME_COMMON_FOOTER_SIZE) as u64;
        let fh = FrameHeader {
            frame_type: FrameType::HeaderMetadata,
            version: 1,
            flags: 0,
            total_length,
        };
        fh.write_to(&mut out);
        out.extend_from_slice(&header_meta_cbor);
        out.extend_from_slice(&0u64.to_be_bytes()); // hash slot
        out.extend_from_slice(FRAME_END);
        let pad = (8 - (out.len() % 8)) % 8;
        out.extend(std::iter::repeat_n(0u8, pad));

        // Preceder metadata
        let total_length =
            (FRAME_HEADER_SIZE + preceder_cbor.len() + FRAME_COMMON_FOOTER_SIZE) as u64;
        let fh = FrameHeader {
            frame_type: FrameType::PrecederMetadata,
            version: 1,
            flags: 0,
            total_length,
        };
        fh.write_to(&mut out);
        out.extend_from_slice(&preceder_cbor);
        out.extend_from_slice(&0u64.to_be_bytes()); // hash slot
        out.extend_from_slice(FRAME_END);
        let pad = (8 - (out.len() % 8)) % 8;
        out.extend(std::iter::repeat_n(0u8, pad));

        // Data object
        out.extend_from_slice(&frame);
        let pad = (8 - (out.len() % 8)) % 8;
        out.extend(std::iter::repeat_n(0u8, pad));

        // Footer metadata
        let total_length =
            (FRAME_HEADER_SIZE + footer_cbor.len() + FRAME_COMMON_FOOTER_SIZE) as u64;
        let fh = FrameHeader {
            frame_type: FrameType::FooterMetadata,
            version: 1,
            flags: 0,
            total_length,
        };
        fh.write_to(&mut out);
        out.extend_from_slice(&footer_cbor);
        out.extend_from_slice(&0u64.to_be_bytes()); // hash slot
        out.extend_from_slice(FRAME_END);
        let pad = (8 - (out.len() % 8)) % 8;
        out.extend(std::iter::repeat_n(0u8, pad));

        // Postamble (patched with total_length after preamble is
        // finalised).
        let postamble_offset = out.len();
        let postamble = Postamble {
            first_footer_offset: postamble_offset as u64,
            total_length: 0,
        };
        postamble.write_to(&mut out);

        // Patch preamble
        let total_length = out.len() as u64;
        let mut flags = MessageFlags::default();
        flags.set(MessageFlags::HEADER_METADATA);
        flags.set(MessageFlags::FOOTER_METADATA);
        flags.set(MessageFlags::PRECEDER_METADATA);
        let preamble = Preamble {
            version: crate::wire::WIRE_VERSION,
            flags,
            reserved: 0,
            total_length,
        };
        let mut preamble_bytes = Vec::new();
        preamble.write_to(&mut preamble_bytes);
        out[0..PREAMBLE_SIZE].copy_from_slice(&preamble_bytes);
        // Patch postamble total_length
        out[postamble_offset + 8..postamble_offset + 16]
            .copy_from_slice(&total_length.to_be_bytes());

        // Decode
        let decoded = crate::framing::decode_message(&out).unwrap();

        // The preceder's _reserved_ should have been stripped by the decoder.
        // The footer's _reserved_.tensor should be preserved.
        let base0 = &decoded.global_metadata.base[0];
        assert!(
            base0.contains_key("mars"),
            "mars from preceder should survive"
        );
        // _reserved_ should come from footer, not preceder
        let reserved = base0.get("_reserved_");
        assert!(
            reserved.is_some(),
            "_reserved_ from footer should be present"
        );
        if let Some(ciborium::Value::Map(pairs)) = reserved {
            let has_tensor = pairs
                .iter()
                .any(|(k, _)| *k == ciborium::Value::Text("tensor".to_string()));
            assert!(has_tensor, "tensor key from footer should be preserved");
            let has_rogue = pairs
                .iter()
                .any(|(k, _)| *k == ciborium::Value::Text("rogue".to_string()));
            assert!(
                !has_rogue,
                "rogue key from preceder's _reserved_ should have been stripped"
            );
        }
    }

    // ── write_object_pre_encoded tests ───────────────────────────────────

    #[test]
    fn test_streaming_mixed_mode_pre_encoded() {
        // write_object (raw), write_object_pre_encoded, write_object (raw) — decode all 3.
        let meta = GlobalMetadata::default();

        let desc0 = make_descriptor(vec![4]);
        let desc2 = make_descriptor(vec![6]);
        // Pre-encoded object: encoding="none" so pre-encoded bytes == raw bytes.
        let desc1 = make_descriptor(vec![5]);

        let data0 = vec![1u8; 4 * 4];
        let pre_encoded1 = vec![2u8; 5 * 4]; // treated as already-encoded
        let data2 = vec![3u8; 6 * 4];

        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();
        enc.write_object(&desc0, &data0).unwrap();
        enc.write_object_pre_encoded(&desc1, &pre_encoded1).unwrap();
        enc.write_object(&desc2, &data2).unwrap();
        assert_eq!(enc.object_count(), 3);
        let result = enc.finish().unwrap();

        let (_, objects) = decode(&result, &DecodeOptions::default()).unwrap();
        assert_eq!(objects.len(), 3);
        // Don't compare raw message bytes (provenance is non-deterministic).
        // Compare decoded payloads.
        assert_eq!(objects[0].1, data0, "object 0 payload mismatch");
        assert_eq!(objects[1].1, pre_encoded1, "object 1 payload mismatch");
        assert_eq!(objects[2].1, data2, "object 2 payload mismatch");
    }

    #[test]
    fn test_streaming_preceder_then_pre_encoded() {
        // write_preceder followed by write_object_pre_encoded — preceder metadata
        // should appear in base[0] after decode.
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);
        let pre_encoded = vec![42u8; 4 * 4];

        let mut prec = BTreeMap::new();
        prec.insert(
            "mars".to_string(),
            ciborium::Value::Map(vec![(
                ciborium::Value::Text("param".to_string()),
                ciborium::Value::Text("2t".to_string()),
            )]),
        );

        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();
        enc.write_preceder(prec).unwrap();
        enc.write_object_pre_encoded(&desc, &pre_encoded).unwrap();
        let result = enc.finish().unwrap();

        let (decoded_meta, objects) = decode(&result, &DecodeOptions::default()).unwrap();
        assert_eq!(objects.len(), 1);
        // Payload must round-trip correctly.
        assert_eq!(objects[0].1, pre_encoded, "pre-encoded payload mismatch");
        // Preceder mars key should be in base[0].
        let mars = decoded_meta.base[0].get("mars");
        assert!(
            mars.is_some(),
            "mars key from preceder should be in base[0]"
        );
    }

    #[test]
    fn streaming_finish_preserves_preceder_does_not_clobber_reserved_tensor() {
        // Verify that preceder metadata does NOT clobber the encoder's
        // _reserved_.tensor in the footer metadata.
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);
        let data = vec![42u8; 4 * 4];

        let mut prec = BTreeMap::new();
        prec.insert("units".to_string(), ciborium::Value::Text("K".to_string()));

        let buf = Vec::new();
        let mut enc = StreamingEncoder::new(buf, &meta, &EncodeOptions::default()).unwrap();
        enc.write_preceder(prec).unwrap();
        enc.write_object(&desc, &data).unwrap();
        let result = enc.finish().unwrap();

        let (decoded_meta, _) = decode(&result, &DecodeOptions::default()).unwrap();
        let base0 = &decoded_meta.base[0];

        // preceder key should be present
        assert!(base0.contains_key("units"));

        // _reserved_.tensor should also be present
        let reserved = base0.get("_reserved_").expect("_reserved_ missing");
        if let ciborium::Value::Map(pairs) = reserved {
            let has_tensor = pairs
                .iter()
                .any(|(k, _)| *k == ciborium::Value::Text("tensor".to_string()));
            assert!(
                has_tensor,
                "_reserved_.tensor should be present after preceder merge"
            );
        } else {
            panic!("_reserved_ should be a map");
        }
    }

    // ── AggregateHashPolicy strict-input tests ─────────────────────────

    #[test]
    fn streaming_rejects_aggregate_hash_header() {
        // Strict-input contract: Header placement is impossible in
        // streaming because the header is written before any data
        // object.  Earlier versions silently coerced this to Footer.
        let meta = GlobalMetadata::default();
        let opts = EncodeOptions {
            aggregate_hash: crate::encode::AggregateHashPolicy::Header,
            ..Default::default()
        };
        match StreamingEncoder::new(Vec::new(), &meta, &opts) {
            Ok(_) => panic!("expected Header to be rejected in streaming mode"),
            Err(TensogramError::Encoding(msg)) => {
                assert!(
                    msg.contains("Header is not supported in streaming"),
                    "expected Header rejection, got: {msg}"
                );
                assert!(msg.contains("Footer"), "should suggest Footer: {msg}");
            }
            Err(other) => panic!("expected Encoding error, got: {other:?}"),
        }
    }

    #[test]
    fn streaming_rejects_aggregate_hash_both() {
        let meta = GlobalMetadata::default();
        let opts = EncodeOptions {
            aggregate_hash: crate::encode::AggregateHashPolicy::Both,
            ..Default::default()
        };
        match StreamingEncoder::new(Vec::new(), &meta, &opts) {
            Ok(_) => panic!("expected Both to be rejected in streaming mode"),
            Err(TensogramError::Encoding(msg)) => {
                assert!(
                    msg.contains("Both is not supported in streaming"),
                    "expected Both rejection, got: {msg}"
                );
            }
            Err(other) => panic!("expected Encoding error, got: {other:?}"),
        }
    }

    #[test]
    fn streaming_accepts_aggregate_hash_auto() {
        // Auto resolves to Footer in streaming mode — no error.
        let meta = GlobalMetadata::default();
        let opts = EncodeOptions {
            aggregate_hash: crate::encode::AggregateHashPolicy::Auto,
            ..Default::default()
        };
        let res = StreamingEncoder::new(Vec::new(), &meta, &opts);
        assert!(res.is_ok(), "Auto must be accepted in streaming mode");
    }

    #[test]
    fn streaming_accepts_aggregate_hash_footer() {
        let meta = GlobalMetadata::default();
        let opts = EncodeOptions {
            aggregate_hash: crate::encode::AggregateHashPolicy::Footer,
            ..Default::default()
        };
        let res = StreamingEncoder::new(Vec::new(), &meta, &opts);
        assert!(res.is_ok(), "Footer must be accepted in streaming mode");
    }

    #[test]
    fn streaming_accepts_aggregate_hash_none() {
        // Explicit "no aggregate" is also valid; per-frame inline
        // hashes are still produced when hash_algorithm is set.
        let meta = GlobalMetadata::default();
        let opts = EncodeOptions {
            aggregate_hash: crate::encode::AggregateHashPolicy::None,
            ..Default::default()
        };
        let res = StreamingEncoder::new(Vec::new(), &meta, &opts);
        assert!(res.is_ok(), "None must be accepted in streaming mode");
    }

    // ── finish_and_reset tests ────────────────────────────────────────────

    #[test]
    fn finish_and_reset_produces_independently_decodable_messages() {
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);
        let data1 = vec![1u8; 4 * 4];
        let data2 = vec![2u8; 4 * 4];

        let mut enc = StreamingEncoder::new(
            std::io::Cursor::new(Vec::new()),
            &meta,
            &EncodeOptions::default(),
        )
        .unwrap();

        enc.write_object(&desc, &data1).unwrap();
        let msg1 = enc.finish_and_reset().unwrap();

        enc.write_object(&desc, &data2).unwrap();
        let msg2 = enc.finish_and_reset().unwrap();

        let (_, objs1) = decode(&msg1, &DecodeOptions::default()).unwrap();
        assert_eq!(objs1.len(), 1);
        assert_eq!(objs1[0].1, data1, "message 1 payload mismatch");

        let (_, objs2) = decode(&msg2, &DecodeOptions::default()).unwrap();
        assert_eq!(objs2.len(), 1);
        assert_eq!(objs2[0].1, data2, "message 2 payload mismatch");
    }

    #[test]
    fn finish_and_reset_streaming_mode_zero_lengths() {
        // finish_and_reset (without backfill) matches finish(): total_length = 0.
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);
        let data = vec![0u8; 4 * 4];

        let mut enc = StreamingEncoder::new(
            std::io::Cursor::new(Vec::new()),
            &meta,
            &EncodeOptions::default(),
        )
        .unwrap();
        enc.write_object(&desc, &data).unwrap();
        let msg = enc.finish_and_reset().unwrap();

        let preamble_len = u64::from_be_bytes(msg[16..24].try_into().unwrap());
        let postamble_len =
            u64::from_be_bytes(msg[msg.len() - 16..msg.len() - 8].try_into().unwrap());
        assert_eq!(
            preamble_len, 0,
            "streaming mode preamble total_length must be 0"
        );
        assert_eq!(
            postamble_len, 0,
            "streaming mode postamble total_length must be 0"
        );
    }

    #[test]
    fn finish_and_reset_with_backfill_fills_total_length() {
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);
        let data = vec![0u8; 4 * 4];

        let mut enc = StreamingEncoder::new(
            std::io::Cursor::new(Vec::new()),
            &meta,
            &EncodeOptions::default(),
        )
        .unwrap();
        enc.write_object(&desc, &data).unwrap();
        let msg = enc.finish_and_reset_with_backfill().unwrap();

        let total = msg.len() as u64;
        let preamble_len = u64::from_be_bytes(msg[16..24].try_into().unwrap());
        let postamble_len =
            u64::from_be_bytes(msg[msg.len() - 16..msg.len() - 8].try_into().unwrap());
        assert_eq!(
            preamble_len, total,
            "backfilled preamble total_length must equal message length"
        );
        assert_eq!(
            postamble_len, total,
            "backfilled postamble total_length must equal message length"
        );
    }

    #[test]
    fn finish_and_reset_allows_multiple_resets() {
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);

        let mut enc = StreamingEncoder::new(
            std::io::Cursor::new(Vec::new()),
            &meta,
            &EncodeOptions::default(),
        )
        .unwrap();

        let mut messages = Vec::new();
        for i in 0u8..5 {
            let data = vec![i; 4 * 4];
            enc.write_object(&desc, &data).unwrap();
            messages.push((enc.finish_and_reset_with_backfill().unwrap(), data));
        }

        for (msg, expected_data) in &messages {
            let (_, objs) = decode(msg, &DecodeOptions::default()).unwrap();
            assert_eq!(objs.len(), 1);
            assert_eq!(&objs[0].1, expected_data);
        }
    }

    #[test]
    fn finish_and_reset_encoder_still_usable_after_reset() {
        // After finish_and_reset(), calling finish() on the same encoder must work.
        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);
        let data1 = vec![10u8; 4 * 4];
        let data2 = vec![20u8; 4 * 4];

        let mut enc = StreamingEncoder::new(
            std::io::Cursor::new(Vec::new()),
            &meta,
            &EncodeOptions::default(),
        )
        .unwrap();

        enc.write_object(&desc, &data1).unwrap();
        let _msg1 = enc.finish_and_reset().unwrap();

        // enc is now reset; use finish() for the final message.
        enc.write_object(&desc, &data2).unwrap();
        let cursor = enc.finish().unwrap();
        let (_, objs) = decode(cursor.get_ref(), &DecodeOptions::default()).unwrap();
        assert_eq!(objs[0].1, data2);
    }

    #[test]
    fn finish_and_reset_dangling_preceder_returns_error() {
        let meta = GlobalMetadata::default();
        let mut prec = BTreeMap::new();
        prec.insert("key".to_string(), ciborium::Value::Text("val".to_string()));

        let mut enc = StreamingEncoder::new(
            std::io::Cursor::new(Vec::new()),
            &meta,
            &EncodeOptions::default(),
        )
        .unwrap();
        enc.write_preceder(prec).unwrap();
        // finish_and_reset without the corresponding write_object must fail.
        let result = enc.finish_and_reset();
        assert!(
            result.is_err(),
            "dangling preceder must cause finish_and_reset to fail"
        );
    }

    #[test]
    fn finish_and_reset_concatenated_messages_scannable() {
        // Messages produced by finish_and_reset_with_backfill can be concatenated
        // and scanned back as independent messages.
        use crate::framing::scan;

        let meta = GlobalMetadata::default();
        let desc = make_descriptor(vec![4]);

        let mut enc = StreamingEncoder::new(
            std::io::Cursor::new(Vec::new()),
            &meta,
            &EncodeOptions::default(),
        )
        .unwrap();

        let mut parts: Vec<Vec<u8>> = Vec::new();
        for i in 0u8..3 {
            enc.write_object(&desc, &[i; 16]).unwrap();
            parts.push(enc.finish_and_reset_with_backfill().unwrap());
        }

        let combined: Vec<u8> = parts.iter().flatten().copied().collect();
        let scanned = scan(&combined);
        assert_eq!(
            scanned.len(),
            3,
            "three concatenated messages must scan as three"
        );

        let mut offset = 0usize;
        for (i, part) in parts.iter().enumerate() {
            assert_eq!(
                scanned[i],
                (offset, part.len()),
                "scan offset/length mismatch for message {i}"
            );
            offset += part.len();
        }
    }

    #[test]
    fn finish_and_reset_empty_message_is_decodable() {
        // finish_and_reset with no objects must produce a valid empty message.
        let meta = GlobalMetadata::default();

        let mut enc = StreamingEncoder::new(
            std::io::Cursor::new(Vec::new()),
            &meta,
            &EncodeOptions::default(),
        )
        .unwrap();

        let msg = enc.finish_and_reset().unwrap();
        let (_, objs) = decode(&msg, &DecodeOptions::default()).unwrap();
        assert_eq!(
            objs.len(),
            0,
            "empty message after finish_and_reset must decode to 0 objects"
        );
    }
}