asimov-flow 25.5.0

ASIMOV Software Development Kit (SDK) for Rust
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
// This is free and unencumbered software released into the public domain.

//! Bounded, latency-aware batches for Rust-facing JSONL streams.
//!
//! Batch boundaries are transport groupings, not RDF graphs or transactions.
//! Pulling batches supplies backpressure. Source errors are retained and emitted
//! after buffered complete lines. Local processes, HTTP, and other transports
//! share the same payload types while choosing their own error types.
//! Types and batch flattening are runtime-independent; timed batching requires
//! the `tokio` feature. Vectored wire slices require only `std`.

use crate::{Bytes, JsonlLine, JsonlLineError, Stream, StreamExt};
use alloc::{boxed::Box, vec::Vec};
use core::{
    iter::FusedIterator,
    num::{NonZeroUsize, TryFromIntError},
    ops::Range,
    pin::Pin,
    time::Duration,
};

/// Validated JSONL lines, retaining their original bytes and line endings.
///
/// Public [`JsonlLine`] constructors validate framing. Neither JSON nor UTF-8 is
/// validated. An empty line is distinct from an empty
/// batch: graph inputs ignore empty batches but write an LF for an empty line.
/// SDK producers never emit empty batches.
///
/// Storage is private: reader-produced batches can retain one `Bytes` view plus
/// line-end offsets, while batches assembled from individual lines preserve
/// those line values. Borrowed iteration does not materialize individual shared
/// handles. Extracting lines produces ordinary `Bytes` slices; rebuilding a
/// batch from them does not infer allocation identity or recover a wider view.
///
/// ```
/// use asimov_flow::{Bytes, JsonlBatch, JsonlLine, JsonlLineError};
/// let batch = JsonlBatch::new(vec![
///     JsonlLine::owned(b"{}\n".to_vec())?,
///     JsonlLine::shared(Bytes::from_static(b"[]\r\n"))?,
/// ]);
/// assert_eq!(batch.len(), 2);
/// let raw = JsonlBatch::try_from(vec![b"{}\n".to_vec()])?;
/// assert_eq!(raw.byte_len(), 3);
/// # Ok::<(), JsonlLineError>(())
/// ```
#[derive(Clone, Debug)]
pub struct JsonlBatch {
    storage: BatchStorage,
    byte_len: usize,
}

#[derive(Clone, Debug)]
enum BatchStorage {
    // Bytes is the exact batch view. End offsets are relative to that view and
    // preserve even empty/unterminated line boundaries. No per-line Bytes handles
    // are created until a caller extracts owned lines.
    Contiguous { bytes: Bytes, line_ends: Vec<usize> },
    Lines(Vec<JsonlLine>),
}

impl Default for JsonlBatch {
    fn default() -> Self {
        Self {
            storage: BatchStorage::Lines(Vec::new()),
            byte_len: 0,
        }
    }
}

impl PartialEq for JsonlBatch {
    fn eq(&self, other: &Self) -> bool {
        self.byte_len == other.byte_len
            && self.len() == other.len()
            && self.lines().eq(other.lines())
    }
}
impl Eq for JsonlBatch {}

impl JsonlBatch {
    /// Takes ownership of already-validated lines without copying their bytes.
    /// Panics if their aggregate stored byte length overflows `usize`.
    pub fn new(lines: Vec<JsonlLine>) -> Self {
        let byte_len = lines
            .iter()
            .try_fold(0usize, |total, line| total.checked_add(line.len()))
            .expect("JSONL batch byte length exceeds usize");
        Self {
            storage: BatchStorage::Lines(lines),
            byte_len,
        }
    }

    /// Frames a complete JSONL byte buffer into a contiguous batch without copying
    /// its payload. Retains LF/CRLF endings and a final nonempty unterminated line;
    /// an empty buffer means an empty batch. JSON and UTF-8 are not validated.
    ///
    /// Use this for a complete buffer, not arbitrary I/O chunks that can split a
    /// line. Use [`crate::jsonl_lines_from_chunks`] for arbitrary chunks, or
    /// `jsonl_batches` (with the `tokio` feature) for streaming readers.
    ///
    /// ```
    /// use asimov_flow::{Bytes, JsonlBatch};
    /// let batch = JsonlBatch::from_bytes(Bytes::from_static(b"{}\n[]\r\n"));
    /// assert_eq!(batch.len(), 2);
    /// assert_eq!(batch.as_contiguous_bytes(), Some(b"{}\n[]\r\n".as_slice()));
    /// let lines = batch.into_lines(); // Cheap shared views of individual lines.
    /// assert_eq!(lines[1].content(), b"[]");
    /// ```
    pub fn from_bytes(bytes: Bytes) -> Self {
        let mut line_ends: Vec<_> = memchr::memchr_iter(b'\n', &bytes)
            .map(|index| index + 1)
            .collect();
        if !bytes.is_empty() && line_ends.last().copied() != Some(bytes.len()) {
            line_ends.push(bytes.len());
        }
        Self::contiguous(bytes, line_ends)
    }

    fn contiguous(bytes: Bytes, line_ends: Vec<usize>) -> Self {
        debug_assert_eq!(line_ends.last().copied().unwrap_or(0), bytes.len());
        debug_assert!({
            let mut start = 0;
            line_ends.iter().all(|&end| {
                let valid = JsonlLine::shared_slice(bytes.clone(), start..end).is_ok();
                start = end;
                valid
            })
        });
        Self {
            byte_len: bytes.len(),
            storage: BatchStorage::Contiguous { bytes, line_ends },
        }
    }

    /// Number of lines, including blank or unterminated lines.
    pub fn len(&self) -> usize {
        match &self.storage {
            BatchStorage::Contiguous { line_ends, .. } => line_ends.len(),
            BatchStorage::Lines(lines) => lines.len(),
        }
    }

    /// Whether the batch has no lines.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Total stored line bytes, including existing line endings. This excludes
    /// any LF a graph input may append to unterminated lines when writing them.
    pub fn byte_len(&self) -> usize {
        self.byte_len
    }

    /// Borrows byte views of the lines in their original order. Use
    /// [`into_lines`](Self::into_lines) when individual lines need owned lifetimes.
    pub fn lines(&self) -> impl ExactSizeIterator<Item = &[u8]> + DoubleEndedIterator {
        BatchLines {
            batch: self,
            front: 0,
            back: self.len(),
        }
    }

    /// Returns owned line values without copying payload bytes. Contiguous batches
    /// create shared `Bytes` slices here; independently constructed lines retain
    /// their existing storage mode. Shared values may retain larger allocations.
    pub fn into_lines(self) -> Vec<JsonlLine> {
        match self.storage {
            BatchStorage::Lines(lines) => lines,
            BatchStorage::Contiguous { bytes, line_ends } => {
                let mut start = 0;
                line_ends
                    .into_iter()
                    .map(|end| {
                        let line = JsonlLine::framed(bytes.slice(start..end));
                        start = end;
                        line
                    })
                    .collect()
            },
        }
    }

    fn line_bytes(&self, index: usize) -> &[u8] {
        match &self.storage {
            BatchStorage::Lines(lines) => lines[index].as_bytes(),
            BatchStorage::Contiguous { bytes, line_ends } => {
                let start = if index == 0 { 0 } else { line_ends[index - 1] };
                &bytes[start..line_ends[index]]
            },
        }
    }

    /// Returns a ready-to-write contiguous JSONL encoding without copying, when
    /// all lines are terminated and the batch retains a contiguous backing view
    /// (or contains just one line). Batches made from separate lines do not infer
    /// shared allocation identity from pointer adjacency. Unterminated lines need
    /// LF insertion and return `None`. Empty batches return an empty slice.
    pub fn as_contiguous_bytes(&self) -> Option<&[u8]> {
        if self.is_empty() {
            return Some(&[]);
        }
        match &self.storage {
            BatchStorage::Contiguous { bytes, .. }
                if self.lines().all(|line| line.ends_with(b"\n")) =>
            {
                Some(bytes)
            },
            BatchStorage::Lines(lines) if lines.len() == 1 && lines[0].is_terminated() => {
                Some(lines[0].as_bytes())
            },
            _ => None,
        }
    }

    /// Copies stored bytes into one compact backing buffer, preserving line
    /// boundaries and endings. This releases references to larger read buffers
    /// when a filter keeps only a small subset. `byte_len` measures logical bytes,
    /// not the allocation size retained by shared lines.
    pub fn into_compact(self) -> Self {
        let mut bytes = Vec::with_capacity(self.byte_len);
        let mut line_ends = Vec::with_capacity(self.len());
        for line in self.lines() {
            bytes.extend_from_slice(line);
            line_ends.push(bytes.len());
        }
        Self::contiguous(Bytes::from(bytes), line_ends)
    }

    /// Builds a bounded set of wire slices, including missing LF terminators.
    /// Highly fragmented batches fall back to the caller's reusable copy buffer.
    #[cfg(feature = "std")]
    pub fn wire_slices(&self, maximum: usize) -> Option<Vec<std::io::IoSlice<'_>>> {
        use std::io::IoSlice;
        let mut slices = Vec::new();
        match &self.storage {
            BatchStorage::Lines(lines) => {
                for line in lines {
                    if !line.is_empty() {
                        slices.push(IoSlice::new(line.as_bytes()));
                    }
                    if !line.is_terminated() {
                        slices.push(IoSlice::new(b"\n"));
                    }
                    if slices.len() > maximum {
                        return None;
                    }
                }
            },
            BatchStorage::Contiguous { bytes, line_ends } => {
                let mut start = 0;
                let mut run_start = 0;
                for &end in line_ends {
                    if !bytes[start..end].ends_with(b"\n") {
                        if end != run_start {
                            slices.push(IoSlice::new(&bytes[run_start..end]));
                        }
                        slices.push(IoSlice::new(b"\n"));
                        run_start = end;
                    }
                    if slices.len() > maximum {
                        return None;
                    }
                    start = end;
                }
                if run_start < bytes.len() {
                    slices.push(IoSlice::new(&bytes[run_start..]));
                }
                if slices.len() > maximum {
                    return None;
                }
            },
        }
        Some(slices)
    }
}

struct BatchLines<'a> {
    batch: &'a JsonlBatch,
    front: usize,
    back: usize,
}

impl<'a> Iterator for BatchLines<'a> {
    type Item = &'a [u8];
    fn next(&mut self) -> Option<Self::Item> {
        if self.front == self.back {
            return None;
        }
        let index = self.front;
        self.front += 1;
        Some(self.batch.line_bytes(index))
    }
    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.back - self.front;
        (remaining, Some(remaining))
    }
}
impl DoubleEndedIterator for BatchLines<'_> {
    fn next_back(&mut self) -> Option<Self::Item> {
        if self.front == self.back {
            return None;
        }
        self.back -= 1;
        Some(self.batch.line_bytes(self.back))
    }
}
impl ExactSizeIterator for BatchLines<'_> {}
impl FusedIterator for BatchLines<'_> {}

/// A line with backing-buffer provenance for executor-level framing adapters.
/// Applications normally use [`JsonlLine`] or [`JsonlBatch`].
#[doc(hidden)]
pub struct FramedLine(FrameStorage);

enum FrameStorage {
    Line(JsonlLine),
    Buffer { bytes: Bytes, range: Range<usize> },
}

impl FramedLine {
    pub(crate) fn new(bytes: Bytes, range: Range<usize>) -> Self {
        debug_assert!(JsonlLine::shared_slice(bytes.clone(), range.clone()).is_ok());
        Self(FrameStorage::Buffer { bytes, range })
    }
    pub fn as_bytes(&self) -> &[u8] {
        match &self.0 {
            FrameStorage::Line(line) => line.as_bytes(),
            FrameStorage::Buffer { bytes, range } => &bytes[range.clone()],
        }
    }
    #[cfg(feature = "tokio")]
    fn len(&self) -> usize {
        self.as_bytes().len()
    }
    pub fn into_line(self) -> JsonlLine {
        match self.0 {
            FrameStorage::Line(line) => line,
            FrameStorage::Buffer { bytes, range } => JsonlLine::framed(bytes.slice(range)),
        }
    }
}

impl From<JsonlLine> for FramedLine {
    fn from(line: JsonlLine) -> Self {
        Self(FrameStorage::Line(line))
    }
}

#[doc(hidden)]
pub type FrameStream<E> = Pin<Box<dyn Stream<Item = Result<FramedLine, E>> + Send>>;

#[cfg(feature = "tokio")]
#[derive(Default)]
enum BuilderStorage {
    #[default]
    Empty,
    Buffer {
        bytes: Bytes,
        start: usize,
        line_ends: Vec<usize>,
    },
    Lines(Vec<JsonlLine>),
}

#[cfg(feature = "tokio")]
impl BuilderStorage {
    fn finish(self, byte_len: usize) -> JsonlBatch {
        match self {
            Self::Empty => JsonlBatch::default(),
            Self::Lines(lines) => JsonlBatch {
                storage: BatchStorage::Lines(lines),
                byte_len,
            },
            Self::Buffer {
                bytes,
                start,
                mut line_ends,
            } => {
                let end = *line_ends.last().expect("buffered batch contains a line");
                for offset in &mut line_ends {
                    *offset -= start;
                }
                JsonlBatch::contiguous(bytes.slice(start..end), line_ends)
            },
        }
    }
}

#[cfg(feature = "tokio")]
#[derive(Default)]
struct BatchBuilder {
    storage: BuilderStorage,
    byte_len: usize,
    len: usize,
}

#[cfg(feature = "tokio")]
impl BatchBuilder {
    fn len(&self) -> usize {
        self.len
    }
    fn byte_len(&self) -> usize {
        self.byte_len
    }
    fn push(&mut self, line: FramedLine) {
        let previous_bytes = self.byte_len;
        self.byte_len = self
            .byte_len
            .checked_add(line.len())
            .expect("JSONL batch byte length exceeds usize");
        self.len += 1;
        match (&mut self.storage, line) {
            (BuilderStorage::Empty, FramedLine(FrameStorage::Buffer { bytes, range })) => {
                self.storage = BuilderStorage::Buffer {
                    bytes,
                    start: range.start,
                    line_ends: alloc::vec![range.end],
                };
            },
            (
                BuilderStorage::Buffer {
                    bytes, line_ends, ..
                },
                FramedLine(FrameStorage::Buffer { bytes: next, range }),
            ) if bytes.as_ptr() == next.as_ptr()
                && bytes.len() == next.len()
                && line_ends.last().copied() == Some(range.start) =>
            {
                line_ends.push(range.end);
            },
            (BuilderStorage::Lines(lines), line) => lines.push(line.into_line()),
            (_, line) => {
                let storage = core::mem::take(&mut self.storage);
                let mut lines = storage.finish(previous_bytes).into_lines();
                lines.push(line.into_line());
                self.storage = BuilderStorage::Lines(lines);
            },
        }
    }
    fn finish(self) -> JsonlBatch {
        self.storage.finish(self.byte_len)
    }
}

impl From<Vec<JsonlLine>> for JsonlBatch {
    fn from(lines: Vec<JsonlLine>) -> Self {
        Self::new(lines)
    }
}

impl TryFrom<Vec<Vec<u8>>> for JsonlBatch {
    type Error = JsonlLineError;
    /// Validates every raw line. On failure, the error offset refers to the
    /// offending line's bytes rather than the concatenated batch.
    fn try_from(lines: Vec<Vec<u8>>) -> Result<Self, Self::Error> {
        lines.into_iter().map(JsonlLine::owned).collect()
    }
}

impl FromIterator<JsonlLine> for JsonlBatch {
    fn from_iter<T: IntoIterator<Item = JsonlLine>>(iter: T) -> Self {
        Self::new(iter.into_iter().collect())
    }
}

/// Transport batching policy, independent of subprocess options and listing limits.
///
/// Defaults are 256 lines, a 256 KiB byte target, and 10 ms from adding the first
/// complete line to a new batch. The first threshold reached flushes the batch.
/// Counts are nonzero by construction. One oversized line is emitted alone;
/// lines are never split.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct BatchOptions {
    /// Maximum lines per batch, not a total result limit.
    pub max_lines: NonZeroUsize,
    /// Typical maximum serialized batch size. A larger single line is allowed.
    pub target_bytes: NonZeroUsize,
    /// Maximum time spent collecting more lines after starting a batch,
    /// while the stream is being polled. Downstream backpressure still applies.
    /// Zero emits each line immediately. No empty timer batches are emitted.
    pub max_delay: Duration,
}

impl BatchOptions {
    /// Creates a policy, rejecting zero line/byte thresholds.
    ///
    /// ```
    /// use asimov_flow::BatchOptions;
    /// use std::time::Duration;
    /// let options = BatchOptions::new(128, 64 * 1024, Duration::from_millis(5))?;
    /// assert_eq!(options.max_lines.get(), 128);
    /// # Ok::<(), std::num::TryFromIntError>(())
    /// ```
    pub fn new(
        max_lines: usize,
        target_bytes: usize,
        max_delay: Duration,
    ) -> Result<Self, TryFromIntError> {
        Ok(Self {
            max_lines: NonZeroUsize::try_from(max_lines)?,
            target_bytes: NonZeroUsize::try_from(target_bytes)?,
            max_delay,
        })
    }
}

impl Default for BatchOptions {
    fn default() -> Self {
        Self::new(256, 256 * 1024, Duration::from_millis(10)).expect("nonzero batch thresholds")
    }
}

/// A fallible stream of batches, with an implementation-specific error type.
pub type BatchStream<E> = Pin<Box<dyn Stream<Item = Result<JsonlBatch, E>> + Send>>;

/// Validated [`JsonlLine`] values for framing and line-at-a-time adapters.
pub type LineStream<E> = Pin<Box<dyn Stream<Item = Result<JsonlLine, E>> + Send>>;

/// Groups complete lines by count, byte target, or elapsed collection time.
///
/// These input lines are already detached values. For a byte reader, use
/// [`crate::jsonl_batches`] to preserve contiguous backing metadata directly
/// through the framer and batch builder instead of detaching and regrouping lines.
///
/// Preserves order and bytes. EOF flushes a partial batch. On a source error,
/// the source is dropped immediately, buffered complete lines are yielded first,
/// and the error is yielded once as the final item. This does not prefetch while
/// the consumer holds a batch or delay cleanup after observing a source error.
///
/// Timed batching requires a Tokio runtime with time enabled, including when
/// the input is immediately ready.
/// Buffering is bounded by the configured batch plus at most one lookahead line
/// and the source's own buffers; individual line size is not bounded here.
#[cfg(feature = "tokio")]
pub fn batch_lines<E: Send + 'static>(
    source: impl Stream<Item = Result<JsonlLine, E>> + Send + 'static,
    options: BatchOptions,
) -> BatchStream<E> {
    batch_frames(source.map(|line| line.map(FramedLine::from)), options)
}

/// The common batching policy. Reader provenance is available here, allowing
/// contiguous batches without storing backing metadata in public line values.
#[doc(hidden)]
#[cfg(feature = "tokio")]
pub fn batch_frames<E: Send + 'static>(
    source: impl Stream<Item = Result<FramedLine, E>> + Send + 'static,
    options: BatchOptions,
) -> BatchStream<E> {
    Box::pin(async_stream::stream! {
        let mut source = Box::pin(source);
        let mut lookahead = None;
        loop {
            let first = match lookahead.take() {
                Some(line) => Some(Ok(line)),
                None => source.next().await,
            };
            let first = match first {
                Some(Ok(line)) => line,
                Some(Err(error)) => {
                    drop(source);
                    yield Err(error);
                    return;
                },
                None => return,
            };
            let mut batch = BatchBuilder::default();
            batch.push(first);
            let deadline = tokio::time::Instant::now().checked_add(options.max_delay);
            let timer = async {
                match deadline {
                    Some(deadline) => tokio::time::sleep_until(deadline).await,
                    None => core::future::pending::<()>().await,
                }
            };
            tokio::pin!(timer);
            let mut terminal = None;
            while batch.len() < options.max_lines.get()
                && batch.byte_len() < options.target_bytes.get()
                && !deadline.is_some_and(|deadline| tokio::time::Instant::now() >= deadline)
            {
                let next = tokio::select! {
                    biased;
                    _ = &mut timer => break,
                    next = source.next() => next,
                };
                match next {
                    Some(Ok(line)) => {
                        if line.len() > options.target_bytes.get() - batch.byte_len() {
                            lookahead = Some(line);
                            break;
                        }
                        batch.push(line);
                    },
                    Some(Err(error)) => {
                        terminal = Some(Err(error));
                        break;
                    },
                    None => {
                        terminal = Some(Ok(()));
                        break;
                    },
                }
            }
            if let Some(terminal) = terminal {
                // Release process/pipe owners before suspending at the partial
                // batch, rather than waiting for another downstream poll.
                drop(source);
                yield Ok(batch.finish());
                if let Err(error) = terminal {
                    yield Err(error);
                }
                return;
            }
            yield Ok(batch.finish());
        }
    })
}

/// Adapts batches to individual [`JsonlLine`] values without copying payload bytes.
/// Empty batches are skipped. Order, line endings, and terminal errors are preserved.
pub fn flatten_batches<E: Send + 'static>(
    source: impl Stream<Item = Result<JsonlBatch, E>> + Send + 'static,
) -> LineStream<E> {
    Box::pin(async_stream::stream! {
        let mut source = Box::pin(source);
        while let Some(batch) = source.next().await {
            match batch {
                Ok(batch) => {
                    if batch.is_empty() {
                        futures_lite::future::yield_now().await;
                    }
                    for line in batch.into_lines() {
                        yield Ok(line);
                    }
                },
                Err(error) => {
                    drop(source);
                    yield Err(error);
                    return;
                },
            }
        }
    })
}

#[cfg(all(test, feature = "std"))]
mod tests {
    use super::*;
    use alloc::vec;
    use core::sync::atomic::{AtomicBool, Ordering};
    use std::sync::Arc;

    #[test]
    fn contiguous_and_detached_batches_have_the_same_line_semantics() {
        for (bytes, expected) in [
            (b"".as_slice(), vec![]),
            (b"\n", vec![b"\n".as_slice()]),
            (b"\r\n{}", vec![b"\r\n".as_slice(), b"{}"]),
            (b"a\nb\nc\n", vec![b"a\n".as_slice(), b"b\n", b"c\n"]),
            (b"first\nlast", vec![b"first\n".as_slice(), b"last"]),
        ] {
            let batch = JsonlBatch::from_bytes(Bytes::copy_from_slice(bytes));
            assert_eq!(batch.len(), expected.len());
            assert_eq!(batch.byte_len(), bytes.len());
            assert_eq!(batch.lines().collect::<Vec<_>>(), expected);
            let detached = JsonlBatch::new(batch.clone().into_lines());
            assert_eq!(batch, detached);
            let mut actual = batch.lines();
            let mut expected_iter = expected.iter().copied();
            assert_eq!(actual.next(), expected_iter.next());
            assert_eq!(actual.len(), expected_iter.len());
            assert_eq!(actual.next_back(), expected_iter.next_back());
            assert_eq!(actual.len(), expected_iter.len());
            for expected_line in expected_iter {
                assert_eq!(actual.next(), Some(expected_line));
            }
            assert_eq!(actual.size_hint(), (0, Some(0)));
            assert_eq!(actual.next(), None);
            assert_eq!(actual.next_back(), None);
        }
    }

    #[test]
    fn detaching_lines_does_not_infer_a_contiguous_allocation() {
        let original = JsonlBatch::from_bytes(Bytes::from_static(b"a\nb\n"));
        assert!(original.as_contiguous_bytes().is_some());
        let detached = JsonlBatch::new(original.into_lines());
        assert!(detached.as_contiguous_bytes().is_none());
        let wire: Vec<_> = detached
            .wire_slices(16)
            .unwrap()
            .iter()
            .flat_map(|slice| slice.iter().copied())
            .collect();
        assert_eq!(wire, b"a\nb\n");
    }

    #[test]
    fn raw_batch_construction_rejects_embedded_records() {
        assert_eq!(
            JsonlBatch::try_from(vec![b"{}\n[]".to_vec()]).unwrap_err(),
            JsonlLineError::EmbeddedLf { offset: 2 }
        );
    }

    #[test]
    fn contiguous_views_never_include_filtered_out_lines_or_reorder_records() {
        let backing = Bytes::from_static(b"a\nsecret\nb\n");
        let a = JsonlLine::shared_slice(backing.clone(), 0..2).unwrap();
        let b = JsonlLine::shared_slice(backing, 9..11).unwrap();
        for (lines, expected) in [
            (vec![a.clone(), b.clone()], b"a\nb\n"),
            (vec![b, a], b"b\na\n"),
        ] {
            let batch = JsonlBatch::new(lines);
            assert_eq!(batch.byte_len(), 4);
            assert!(batch.as_contiguous_bytes().is_none());
            let bytes: Vec<_> = batch
                .wire_slices(16)
                .unwrap()
                .iter()
                .flat_map(|slice| slice.iter().copied())
                .collect();
            assert_eq!(bytes, expected);
            let compact = batch.into_compact();
            assert_eq!(compact.as_contiguous_bytes().unwrap(), expected);
        }
    }

    #[test]
    fn compacting_preserves_empty_and_unterminated_line_boundaries() {
        let batch = JsonlBatch::try_from(vec![
            Vec::new(),
            b"{}".to_vec(),
            b"\r\n".to_vec(),
            b"x\n".to_vec(),
        ])
        .unwrap();
        let compact = batch.clone().into_compact();
        assert_eq!(compact, batch);
        assert!(compact.as_contiguous_bytes().is_none());
        let bytes: Vec<_> = compact
            .wire_slices(16)
            .unwrap()
            .iter()
            .flat_map(|slice| slice.iter().copied())
            .collect();
        assert_eq!(bytes, b"\n{}\n\r\nx\n");
    }

    #[test]
    fn compacting_a_sparse_batch_releases_its_backing_owner() {
        struct Owner {
            bytes: Vec<u8>,
            dropped: Arc<AtomicBool>,
        }
        impl AsRef<[u8]> for Owner {
            fn as_ref(&self) -> &[u8] {
                &self.bytes
            }
        }
        impl Drop for Owner {
            fn drop(&mut self) {
                self.dropped.store(true, Ordering::SeqCst);
            }
        }
        let dropped = Arc::new(AtomicBool::new(false));
        let mut bytes = vec![b'x'; 256 * 1024];
        bytes[..11].copy_from_slice(b"a\nsecret\nb\n");
        let backing = Bytes::from_owner(Owner {
            bytes,
            dropped: dropped.clone(),
        });
        let batch = JsonlBatch::new(vec![
            JsonlLine::shared_slice(backing.clone(), 0..2).unwrap(),
            JsonlLine::shared_slice(backing, 9..11).unwrap(),
        ]);
        assert!(!dropped.load(Ordering::SeqCst));
        let compact = batch.into_compact();
        assert!(dropped.load(Ordering::SeqCst));
        assert_eq!(compact.as_contiguous_bytes().unwrap(), b"a\nb\n");
    }

    #[test]
    fn validates_thresholds_and_reports_batch_dimensions() {
        assert!(BatchOptions::new(0, 1, Duration::ZERO).is_err());
        assert!(BatchOptions::new(1, 0, Duration::ZERO).is_err());
        assert_eq!(BatchOptions::default().max_lines.get(), 256);
        assert_eq!(BatchOptions::default().target_bytes.get(), 256 * 1024);
        assert_eq!(BatchOptions::default().max_delay, Duration::from_millis(10));
        let batch =
            JsonlBatch::try_from(vec![b"{}\r\n".to_vec(), Vec::new(), b"tail".to_vec()]).unwrap();
        assert_eq!(batch.len(), 3);
        assert_eq!(batch.byte_len(), 8);
        assert!(!batch.is_empty());
    }

    #[test]
    fn flattening_is_runtime_independent_and_preserves_errors() {
        futures_lite::future::block_on(async {
            let bytes = Bytes::from_static(b"{}\r\nlast");
            let pointer = bytes.as_ptr();
            let batches: BatchStream<&'static str> = Box::pin(crate::stream::iter([
                Ok(JsonlBatch::default()),
                Ok(JsonlBatch::from_bytes(bytes)),
                Err("source failed"),
                Ok(JsonlBatch::from_bytes(Bytes::from_static(b"unreachable\n"))),
            ]));
            let mut lines: LineStream<&'static str> = flatten_batches(batches);
            let first = lines.next().await.unwrap().unwrap();
            assert_eq!(first.as_bytes(), b"{}\r\n");
            assert_eq!(first.as_bytes().as_ptr(), pointer);
            assert_eq!(lines.next().await.unwrap().unwrap().as_bytes(), b"last");
            assert_eq!(lines.next().await.unwrap().unwrap_err(), "source failed");
            assert!(lines.next().await.is_none());
        });
    }

    #[cfg(feature = "tokio")]
    mod runtime {
        use super::*;
        use core::{
            convert::Infallible,
            sync::atomic::AtomicUsize,
            task::{Context, Poll},
        };
        use std::io;
        use tokio::{
            io::{AsyncRead, AsyncWriteExt, ReadBuf},
            time::{Instant, timeout},
        };

        fn options(lines: usize, bytes: usize, delay: Duration) -> BatchOptions {
            BatchOptions::new(lines, bytes, delay).unwrap()
        }

        fn lines(values: &[&[u8]]) -> LineStream<Infallible> {
            Box::pin(crate::stream::iter(
                values
                    .iter()
                    .map(|line| Ok(JsonlLine::copy_from_slice(line).unwrap()))
                    .collect::<Vec<_>>(),
            ))
        }

        #[tokio::test(start_paused = true)]
        async fn line_threshold_and_backpressure_do_not_prefetch_more_batches() {
            let read = Arc::new(AtomicUsize::new(0));
            let counter = read.clone();
            let source = lines(&[b"a\n", b"b\n", b"c\n", b"d\n", b"e"]).map(move |line| {
                counter.fetch_add(1, Ordering::SeqCst);
                line
            });
            let mut batches = batch_lines(source, options(2, 1024, Duration::from_secs(1)));
            assert_eq!(batches.next().await.unwrap().unwrap().len(), 2);
            assert_eq!(read.load(Ordering::SeqCst), 2);
            tokio::time::advance(Duration::from_secs(5)).await;
            assert_eq!(
                read.load(Ordering::SeqCst),
                2,
                "holding a batch must backpressure the source"
            );
            assert_eq!(batches.next().await.unwrap().unwrap().len(), 2);
            assert_eq!(
                batches
                    .next()
                    .await
                    .unwrap()
                    .unwrap()
                    .lines()
                    .collect::<Vec<_>>(),
                vec![b"e".to_vec()]
            );
            assert!(batches.next().await.is_none());
            assert!(batches.next().await.is_none());
        }

        #[tokio::test]
        async fn byte_target_uses_complete_lines_and_allows_oversized_singletons() {
            let mut batches = batch_lines(
                lines(&[b"a\n", b"b\n", b"ccc\n", b"oversized", b"z"]),
                options(100, 5, Duration::from_secs(1)),
            );
            for expected in [
                vec![b"a\n".to_vec(), b"b\n".to_vec()],
                vec![b"ccc\n".to_vec()],
                vec![b"oversized".to_vec()],
                vec![b"z".to_vec()],
            ] {
                assert_eq!(
                    batches
                        .next()
                        .await
                        .unwrap()
                        .unwrap()
                        .lines()
                        .collect::<Vec<_>>(),
                    expected
                );
            }
            assert!(batches.next().await.is_none());
        }

        #[tokio::test(start_paused = true)]
        async fn flushes_sparse_stream_on_deadline_without_waiting_for_eof() {
            let source = lines(&[b"first\n"]).chain(crate::stream::pending());
            let mut batches = batch_lines(source, options(100, 1024, Duration::from_millis(10)));
            let start = Instant::now();
            assert_eq!(
                batches
                    .next()
                    .await
                    .unwrap()
                    .unwrap()
                    .lines()
                    .collect::<Vec<_>>(),
                vec![b"first\n".to_vec()]
            );
            assert_eq!(start.elapsed(), Duration::from_millis(10));
        }

        #[tokio::test(start_paused = true)]
        async fn never_emits_empty_batches_and_zero_delay_emits_immediately() {
            let mut pending = batch_lines(
                crate::stream::pending::<Result<JsonlLine, Infallible>>(),
                BatchOptions::default(),
            );
            assert!(
                timeout(Duration::from_secs(1), pending.next())
                    .await
                    .is_err()
            );
            let mut empty = batch_lines(lines(&[]), BatchOptions::default());
            assert!(empty.next().await.is_none());
            let mut ready = batch_lines(lines(&[b"a", b"b"]), options(100, 1024, Duration::ZERO));
            let start = Instant::now();
            assert_eq!(ready.next().await.unwrap().unwrap().len(), 1);
            assert_eq!(ready.next().await.unwrap().unwrap().len(), 1);
            assert_eq!(start.elapsed(), Duration::ZERO);
        }

        #[tokio::test(start_paused = true)]
        async fn deadline_does_not_discard_a_partially_read_next_line() {
            let (reader, mut writer) = tokio::io::duplex(128);
            let writing = tokio::spawn(async move {
                writer.write_all(b"{}\n{\"pa").await.unwrap();
                tokio::time::sleep(Duration::from_millis(20)).await;
                writer.write_all(b"rt\":true}\n").await.unwrap();
            });
            let mut batches =
                crate::jsonl_batches(reader, options(100, 1024, Duration::from_millis(5)));
            let start = Instant::now();
            assert_eq!(
                batches
                    .next()
                    .await
                    .unwrap()
                    .unwrap()
                    .lines()
                    .collect::<Vec<_>>(),
                vec![b"{}\n".to_vec()]
            );
            assert_eq!(start.elapsed(), Duration::from_millis(5));
            assert_eq!(
                batches
                    .next()
                    .await
                    .unwrap()
                    .unwrap()
                    .lines()
                    .collect::<Vec<_>>(),
                vec![b"{\"part\":true}\n".to_vec()]
            );
            assert!(batches.next().await.is_none());
            writing.await.unwrap();
        }

        struct FailingSource {
            step: usize,
            dropped: Arc<AtomicBool>,
        }
        impl Drop for FailingSource {
            fn drop(&mut self) {
                self.dropped.store(true, Ordering::SeqCst);
            }
        }
        impl Stream for FailingSource {
            type Item = Result<JsonlLine, &'static str>;
            fn poll_next(
                mut self: Pin<&mut Self>,
                _: &mut Context<'_>,
            ) -> Poll<Option<Self::Item>> {
                self.step += 1;
                Poll::Ready(Some(match self.step {
                    1 => Ok(JsonlLine::owned(b"first\n".to_vec()).unwrap()),
                    2 => Err("source failed"),
                    _ => panic!("source must not be polled after failure"),
                }))
            }
        }

        #[tokio::test]
        async fn flushes_partial_batch_before_error_but_drops_source_before_yielding() {
            let dropped = Arc::new(AtomicBool::new(false));
            let mut batches = batch_lines(
                FailingSource {
                    step: 0,
                    dropped: dropped.clone(),
                },
                BatchOptions::default(),
            );
            assert_eq!(
                batches
                    .next()
                    .await
                    .unwrap()
                    .unwrap()
                    .lines()
                    .collect::<Vec<_>>(),
                vec![b"first\n".to_vec()]
            );
            assert!(
                dropped.load(Ordering::SeqCst),
                "error cleanup must not wait for the consumer's next poll"
            );
            assert_eq!(batches.next().await.unwrap().unwrap_err(), "source failed");
            assert!(batches.next().await.is_none());
        }

        struct FailingReader(bool);
        impl AsyncRead for FailingReader {
            fn poll_read(
                mut self: Pin<&mut Self>,
                _: &mut Context<'_>,
                buffer: &mut ReadBuf<'_>,
            ) -> Poll<io::Result<()>> {
                if self.0 {
                    return Poll::Ready(Err(io::Error::other("read failed")));
                }
                self.0 = true;
                buffer.put_slice(b"{}\npartial");
                Poll::Ready(Ok(()))
            }
        }

        #[tokio::test]
        async fn read_error_never_emits_an_incomplete_line() {
            let mut batches = crate::jsonl_batches(FailingReader(false), BatchOptions::default());
            assert_eq!(
                batches
                    .next()
                    .await
                    .unwrap()
                    .unwrap()
                    .lines()
                    .collect::<Vec<_>>(),
                vec![b"{}\n".to_vec()]
            );
            assert!(batches.next().await.unwrap().is_err());
            assert!(batches.next().await.is_none());
        }

        #[tokio::test]
        async fn flattening_round_trips_raw_lines_and_ignores_empty_batches() {
            let expected = [b"{}\n".as_slice(), b"\r\n", b"\xff\n", b"tail"];
            let source = batch_lines(lines(&expected), options(2, 1024, Duration::from_secs(1)));
            let empty = crate::stream::iter([Ok(JsonlBatch::default())]);
            let mut flattened = flatten_batches(empty.chain(source));
            for line in expected {
                assert_eq!(flattened.next().await.unwrap().unwrap().as_bytes(), line);
            }
            assert!(flattened.next().await.is_none());
        }
    }
}