asimov-runner 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
// This is free and unencumbered software released into the public domain.

//! Linear pipelines of graph-compatible programs.
//!
//! [`Pipeline::new`] consumes a configured program; [`Pipeline::pipe`] appends a
//! graph consumer. Connections carry JSON-LD over JSONL by contract, without
//! parsing or transcoding. Native edges use cross-platform [`std::io::pipe`]
//! handles connected directly to child stdin/stdout: no shell, platform-specific
//! handle code, or full intermediate-output buffer is involved. Tokio drives
//! boundary I/O, stderr draining, and process supervision.
//!
//! # Reader → Writer
//!
//! ```no_run
//! use asimov_runner::{AnyInput, AnyOutput, GraphInput, GraphOutput, Pipeline, Reader, Writer};
//! use std::io::Cursor;
//!
//! # async fn example() -> Result<(), asimov_runner::PipelineError> {
//! let reader = Reader::new(
//!     "asimov-example-reader",
//!     AnyInput::AsyncRead(Box::new(Cursor::new(b"source document".to_vec()))),
//!     GraphOutput::Captured,
//!     Default::default(),
//! );
//! let writer = Writer::new(
//!     "asimov-example-writer", GraphInput::Ignored, AnyOutput::Captured, Default::default(),
//! );
//! let bytes = Pipeline::new(reader).pipe(writer).execute().await?.into_inner();
//! # Ok(())
//! # }
//! ```
//!
//! # Fetcher → Reasoner → Indexer
//!
//! ```no_run
//! use asimov_runner::{Fetcher, GraphInput, GraphOutput, Indexer, IndexerOptions, Pipeline, Reasoner};
//!
//! # async fn example() -> Result<(), asimov_runner::PipelineError> {
//! let fetcher = Fetcher::new(
//!     "asimov-example-fetcher", "https://example.com/resource",
//!     GraphOutput::Captured, Default::default(),
//! );
//! let reasoner = Reasoner::new(
//!     "asimov-example-reasoner", GraphInput::Ignored, GraphOutput::Captured, Default::default(),
//! );
//! let indexer = Indexer::new(
//!     "asimov-example-indexer", GraphInput::Ignored,
//!     IndexerOptions::builder().other("./catalog.index").build(),
//! );
//! Pipeline::new(fetcher).pipe(reasoner).pipe(indexer).execute().await?;
//! # Ok(())
//! # }
//! ```
//!
//! # Routing and completion
//!
//! A pipeline is a one-shot owner of its configured programs. Only the first
//! program supplies external input; later programs must use `Input::Ignored` as
//! the placeholder replaced by the connection. Intermediate stdout routing is
//! replaced by pipes; an intermediate `Output::AsyncWrite` is rejected rather
//! than silently discarding its writer. The final program's output policy is
//! honored. Explicit graph format options must be `jsonl` (or left unset).
//! Programs must agree on a JSON-LD profile and use the connected standard streams;
//! arbitrary `other` arguments and file operands are not interpreted by this API.
//!
//! Graph-producing tails return a live [`PipelineStream`] of [`crate::JsonlBatch`]
//! values. [`Pipeline::with_batching`] overrides the tail program's default
//! batching policy; intermediate native pipe edges remain byte streams. A [`Writer`] tail
//! returns buffered arbitrary-format bytes, and an [`Indexer`] tail returns `()`.
//! Success requires every stage to complete successfully, not just the tail.
//! Failures include their zero-based stage index and executable. The first
//! observed failure is reported, preferring downstream stages when multiple
//! outcomes are ready. Other directly supervised children are terminated and reaped before
//! returning that failure. Dropping execution or its stream requests termination
//! through each owned child's kill-on-drop policy; it does not synchronously reap
//! children, terminate arbitrary descendants, or roll back external side effects.
//!
//! All stages spawn before output is consumed, and command-owned copies of pipe
//! endpoints are released immediately after spawning. Polling the execution or
//! returned stream drives supervision and boundary I/O; no detached tasks are
//! created. Stderr and buffered final output have no configured size bound.
//! Graph batch sizes and collection delay follow [`BatchOptions`]. EOF flushes
//! a partial batch. Already-read complete lines are delivered before a terminal
//! error, without delaying cleanup once that error is observed.
//!
//! A limited [`Lister`] applies its line cap before producing batches at the first edge
//! so native pipe wiring cannot bypass the runner's limit. That edge is relayed
//! with backpressure through `Input::Jsonl` (which terminates unterminated input
//! lines with LF); other edges remain direct OS pipes. A zero-limit lister starts
//! no source child and supplies EOF downstream. Reaching its limit intentionally
//! cancels that source without checking its eventual exit status, as for standalone
//! lister execution, including its kill-on-drop/reaping policy. Native pipes
//! otherwise preserve bytes exactly. Neither a
//! successful exit nor writing to a pipe proves application-level processing.
//!
//! # Batch-oriented postprocessing
//!
//! ```no_run
//! use asimov_runner::{BatchOptions, Fetcher, GraphOutput, Pipeline, StreamExt};
//! use std::time::Duration;
//!
//! # async fn example() -> Result<(), asimov_runner::PipelineError> {
//! let fetcher = Fetcher::new(
//!     "asimov-example-fetcher", "https://example.com/resource",
//!     GraphOutput::Captured, Default::default(),
//! );
//! let policy = BatchOptions::new(128, 64 * 1024, Duration::from_millis(5))
//!     .expect("nonzero thresholds");
//! let mut batches = Pipeline::new(fetcher).with_batching(policy).execute().await?;
//! while let Some(batch) = batches.next().await {
//!     let batch = batch?;
//!     // Submit the whole batch to a network service, or iterate batch.lines().
//! }
//! # Ok(())
//! # }
//! ```

use crate::batch::{FrameStream, batch_frames};
use crate::{
    BatchOptions, BatchStream, Executor, ExecutorError, Indexer, Input, InputCompletion, Lister,
    Output, StreamExt, Writer,
};
use alloc::{boxed::Box, vec, vec::Vec};
use core::{
    fmt,
    future::{Future, poll_fn},
    marker::PhantomData,
    pin::Pin,
    task::Poll,
};
use std::{
    ffi::OsString,
    io::{self, Cursor},
    process::Stdio,
};
use tokio::{process::Child, sync::watch};

/// A pipeline failure attributed to a configured program.
#[derive(Debug)]
pub struct PipelineError {
    /// Zero-based stage index, in construction order.
    pub stage: usize,
    /// Executable name or resolved path used for that stage.
    pub program: OsString,
    /// Configuration, spawn, transport, input, or exit failure.
    pub error: ExecutorError,
}

impl fmt::Display for PipelineError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "Pipeline stage {} ({}): {}",
            self.stage,
            self.program.to_string_lossy(),
            self.error
        )
    }
}

impl core::error::Error for PipelineError {
    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
        Some(&self.error)
    }
}

/// Live final graph batches; consume to EOF to check every stage's outcome.
/// Dropping the stream requests termination of all owned children.
pub type PipelineStream = BatchStream<PipelineError>;

mod sealed {
    pub trait Sealed {}
}

/// A built-in program that can participate in a linear graph pipeline.
/// This trait is sealed; use the configured runner types to construct stages.
pub trait PipelineProgram: sealed::Sealed + Into<PipelineStage> {}

/// A program whose output can supply a graph consumer's stdin.
pub trait GraphProducer: PipelineProgram {}

/// A program accepting JSONL graph input from a preceding stage.
pub trait GraphConsumer: PipelineProgram {}

macro_rules! programs {
    (producer: $($producer:ty),*; consumer: $($consumer:ty),*; both: $($both:ty),*) => {
        $(impl sealed::Sealed for $producer {}
          impl PipelineProgram for $producer {}
          impl GraphProducer for $producer {})*
        $(impl sealed::Sealed for $consumer {}
          impl PipelineProgram for $consumer {}
          impl GraphConsumer for $consumer {})*
        $(impl sealed::Sealed for $both {}
          impl PipelineProgram for $both {}
          impl GraphProducer for $both {}
          impl GraphConsumer for $both {})*
    };
}
programs! {
    producer: crate::Adapter, crate::Emitter, crate::Fetcher, Lister, crate::Reader;
    consumer: Writer, Indexer;
    both: crate::Matcher, crate::Reasoner
}

/// An owned, nonempty linear pipeline, typed by its final program.
///
/// Only graph producers can be followed by graph consumers; writer and indexer
/// tails terminate construction. For example, a fetcher cannot consume graph input:
///
/// ```compile_fail
/// use asimov_runner::{Fetcher, GraphOutput, Pipeline};
/// let a = Fetcher::new("a", "example:", GraphOutput::Captured, Default::default());
/// let b = Fetcher::new("b", "example:", GraphOutput::Captured, Default::default());
/// let invalid = Pipeline::new(a).pipe(b);
/// ```
///
/// A writer is a terminal, arbitrary-format output stage, not a graph producer:
///
/// ```compile_fail
/// use asimov_runner::{Input, Output, Pipeline, Reasoner, Writer};
/// let writer = Writer::new("writer", Input::Ignored, Output::Captured, Default::default());
/// let reasoner = Reasoner::new("reasoner", Input::Ignored, Output::Captured, Default::default());
/// let invalid = Pipeline::new(writer).pipe(reasoner);
/// ```
#[derive(Debug)]
pub struct Pipeline<P> {
    stages: Vec<PipelineStage>,
    batching: Option<BatchOptions>,
    tail: PhantomData<fn() -> P>,
}

impl<P: PipelineProgram> Pipeline<P> {
    /// Starts a pipeline without spawning processes. Consumes the program and
    /// preserves its arguments, source input, and final-output configuration.
    pub fn new(program: P) -> Self {
        Self {
            stages: vec![program.into()],
            batching: None,
            tail: PhantomData,
        }
    }
}

impl<P: GraphProducer> Pipeline<P> {
    /// Appends a graph consumer. Its `Input::Ignored` is replaced by a pipe from
    /// the preceding program. No processes are started during construction.
    pub fn pipe<N: GraphConsumer>(mut self, program: N) -> Pipeline<N> {
        self.stages.push(program.into());
        Pipeline {
            stages: self.stages,
            batching: self.batching,
            tail: PhantomData,
        }
    }

    /// Overrides batching for the final Rust-facing graph stream. Native pipe
    /// edges are unchanged. This override survives `pipe` calls; otherwise the
    /// final program's batching policy is used. Byte/unit tails do not batch output.
    #[must_use]
    pub fn with_batching(mut self, options: BatchOptions) -> Self {
        self.batching = Some(options);
        self
    }

    /// Spawns the pipeline and returns final JSONL batches without waiting for exit.
    /// Configuration/launch errors are returned directly; later failures are
    /// final stream items after any buffered complete lines. Non-captured final
    /// output yields no payload batches.
    pub async fn execute(self) -> Result<PipelineStream, PipelineError> {
        let batching = self
            .batching
            .unwrap_or(self.stages.last().unwrap().batching);
        Ok(batch_frames(self.execute_frames().await?, batching))
    }

    async fn execute_frames(self) -> Result<FrameStream<PipelineError>, PipelineError> {
        let mut running = start(self.stages, true).await?;
        let mut lines = running.lines.take();
        let tail = running.tail.clone();
        Ok(Box::pin(async_stream::try_stream! {
            let mut completed = false;
            if let Some(ref mut lines) = lines {
                loop {
                    let event = tokio::select! {
                        result = running.wait(), if !completed => {
                            completed = true;
                            result.map(|_| None)
                        },
                        line = lines.next() => Ok(Some(line)),
                    }?;
                    let Some(line) = event else { continue };
                    match line {
                        Some(Ok(line)) => yield line,
                        Some(Err(error)) => {
                            running.cancel().await;
                            Err(tail.error(error))?;
                        },
                        None => break,
                    }
                }
            }
            if !completed {
                running.wait().await?;
            }
        }))
    }
}

impl Pipeline<Writer> {
    /// Runs every stage and returns the writer's captured arbitrary-format bytes.
    /// Forwarded, ignored, and inherited stdout return an empty cursor. Any stage
    /// failure fails the pipeline, even if the writer exits successfully.
    pub async fn execute(self) -> Result<Cursor<Vec<u8>>, PipelineError> {
        let mut running = start(self.stages, false).await?;
        Ok(Cursor::new(running.wait().await?))
    }
}

impl Pipeline<Indexer> {
    /// Runs every stage and waits for successful indexing and upstream completion.
    pub async fn execute(self) -> Result<(), PipelineError> {
        start(self.stages, false).await?.wait().await?;
        Ok(())
    }
}

/// Opaque owned stage configuration used by the sealed pipeline traits.
#[doc(hidden)]
#[derive(Debug)]
pub struct PipelineStage {
    program: OsString,
    kind: StageKind,
    error: Option<ExecutorError>,
    external_input: bool,
    external_writer: bool,
    batching: BatchOptions,
}

#[derive(Debug)]
enum StageKind {
    Native {
        executor: Executor,
        input: Input,
        output: Output,
    },
    LimitedLister(Box<Lister>),
}

impl PipelineStage {
    pub(crate) fn native(
        mut executor: Executor,
        input: Input,
        output: Output,
        error: Option<ExecutorError>,
    ) -> Self {
        Self {
            program: executor.command().as_std().get_program().to_os_string(),
            batching: executor.batch_options(),
            external_input: !matches!(input, Input::Ignored),
            external_writer: matches!(output, Output::AsyncWrite(_)),
            kind: StageKind::Native {
                executor,
                input,
                output,
            },
            error,
        }
    }

    pub(crate) fn limited_lister(
        lister: Lister,
        program: OsString,
        external_writer: bool,
        error: Option<ExecutorError>,
        batching: BatchOptions,
    ) -> Self {
        Self {
            program,
            kind: StageKind::LimitedLister(Box::new(lister)),
            error,
            external_input: false,
            external_writer,
            batching,
        }
    }
}

pub(crate) fn graph_formats(input: Option<&str>, output: Option<&str>) -> Option<ExecutorError> {
    for (option, format) in [("input", input), ("output", output)] {
        if let Some(format) = format {
            if format != "jsonl" {
                return Some(invalid(alloc::format!(
                    "pipeline graph {option} format must be jsonl, got {format}"
                )));
            }
        }
    }
    None
}

fn invalid(message: impl Into<alloc::string::String>) -> ExecutorError {
    io::Error::new(io::ErrorKind::InvalidInput, message.into()).into()
}

macro_rules! stage {
    ($program:ty, $value:ident, $input:expr, $output:expr, $input_format:expr, $output_format:expr) => {
        impl From<$program> for crate::pipeline::PipelineStage {
            fn from($value: $program) -> Self {
                let error = crate::pipeline::graph_formats($input_format, $output_format);
                Self::native($value.executor, $input, $output, error)
            }
        }
    };
}
pub(crate) use stage;

#[derive(Clone)]
struct StageInfo {
    index: usize,
    program: OsString,
}

impl StageInfo {
    fn error(&self, error: impl Into<ExecutorError>) -> PipelineError {
        PipelineError {
            stage: self.index,
            program: self.program.clone(),
            error: error.into(),
        }
    }
}

type Job = Pin<Box<dyn Future<Output = Result<Vec<u8>, PipelineError>> + Send>>;

struct Running {
    jobs: Vec<Option<Job>>,
    stop: watch::Sender<bool>,
    lines: Option<FrameStream>,
    tail: StageInfo,
    failure: Option<PipelineError>,
    output: Vec<u8>,
}

impl Running {
    // State lives in Running, not this future: selecting on wait while reading
    // final output can cancel the future without losing completed jobs or errors.
    async fn next(&mut self) -> Option<(usize, Result<Vec<u8>, PipelineError>)> {
        poll_fn(|cx| {
            let mut pending = false;
            for (index, job) in self.jobs.iter_mut().enumerate().rev() {
                if let Some(future) = job {
                    match future.as_mut().poll(cx) {
                        Poll::Ready(result) => {
                            *job = None;
                            return Poll::Ready(Some((index, result)));
                        },
                        Poll::Pending => pending = true,
                    }
                }
            }
            if pending {
                Poll::Pending
            } else {
                Poll::Ready(None)
            }
        })
        .await
    }

    async fn wait(&mut self) -> Result<Vec<u8>, PipelineError> {
        while let Some((index, result)) = self.next().await {
            match result {
                Ok(bytes) if index + 1 == self.jobs.len() => self.output = bytes,
                Ok(_) => {},
                Err(error) if self.failure.is_none() => {
                    self.failure = Some(error);
                    self.stop.send_replace(true);
                },
                Err(_) => {},
            }
        }
        match self.failure.take() {
            Some(error) => Err(error),
            None => Ok(core::mem::take(&mut self.output)),
        }
    }

    async fn cancel(&mut self) {
        self.stop.send_replace(true);
        while self.next().await.is_some() {}
    }
}

struct Spawned {
    child: Child,
    input: Input,
    output: Output,
    info: StageInfo,
}

async fn run_stage(
    mut stage: Spawned,
    mut stop: watch::Receiver<bool>,
    source: Option<StageInfo>,
) -> Result<Vec<u8>, PipelineError> {
    let cancelled = *stop.borrow();
    let completion = if cancelled {
        None
    } else {
        tokio::select! {
            biased;
            result = crate::executor::communicate_child(&mut stage.child, &mut stage.input, &mut stage.output) => Some(result),
            _ = stop.changed() => None,
        }
    };
    match completion {
        Some(result) => {
            let completion = result.map_err(|error| stage.info.error(error))?;
            let info = if matches!(completion.input, InputCompletion::SourceFailed(_)) {
                source.as_ref().unwrap_or(&stage.info)
            } else {
                &stage.info
            };
            completion
                .into_result()
                .map(Cursor::into_inner)
                .map_err(|error| info.error(error))
        },
        None => {
            let _ = stage.child.start_kill();
            let _ = stage.child.wait().await;
            Ok(Vec::new())
        },
    }
}

async fn start(
    mut stages: Vec<PipelineStage>,
    capture_graph: bool,
) -> Result<Running, PipelineError> {
    let count = stages.len();
    // Validate the entire chain before starting any process or consuming a source.
    for (index, stage) in stages.iter_mut().enumerate() {
        let info = StageInfo {
            index,
            program: stage.program.clone(),
        };
        if let Some(error) = stage.error.take() {
            return Err(info.error(error));
        }
        if index != 0 && stage.external_input {
            return Err(info.error(invalid(
                "piped stages must use Input::Ignored; their input comes from the preceding stage",
            )));
        }
        if index + 1 != count && stage.external_writer {
            return Err(info.error(invalid("an intermediate pipeline stage cannot also forward stdout to an AsyncWrite destination")));
        }
    }
    let tail = StageInfo {
        index: count - 1,
        program: stages.last().unwrap().program.clone(),
    };
    let (stop, receiver) = watch::channel(false);
    let mut running = Running {
        jobs: Vec::new(),
        stop,
        lines: None,
        tail,
        failure: None,
        output: Vec::new(),
    };
    let mut source_info = None;
    let mut limited_source = None;
    if matches!(stages[0].kind, StageKind::LimitedLister(_)) {
        let stage = stages.remove(0);
        let info = StageInfo {
            index: 0,
            program: stage.program,
        };
        let StageKind::LimitedLister(mut lister) = stage.kind else {
            unreachable!()
        };
        if stages.is_empty() {
            running.lines = Some(
                lister
                    .execute_frames()
                    .await
                    .map_err(|error| info.error(error))?,
            );
            return Ok(running);
        }
        limited_source = Some(lister);
        source_info = Some(info);
    }
    let base = usize::from(source_info.is_some());
    for index in 0..stages.len().saturating_sub(1) {
        let (reader, writer) = io::pipe().map_err(|error| {
            StageInfo {
                index: index + base,
                program: stages[index].program.clone(),
            }
            .error(error)
        })?;
        let StageKind::Native { executor, .. } = &mut stages[index].kind else {
            unreachable!()
        };
        executor.command().stdout(Stdio::from(writer));
        let StageKind::Native { executor, .. } = &mut stages[index + 1].kind else {
            unreachable!()
        };
        executor.command().stdin(Stdio::from(reader));
    }
    if let Some(lister) = limited_source {
        let source = lister
            .into_pipeline_source()
            .await
            .map_err(|error| source_info.as_ref().unwrap().error(error))?;
        let StageKind::Native {
            executor, input, ..
        } = &mut stages[0].kind
        else {
            unreachable!()
        };
        *input = Input::Jsonl(source);
        executor.command().stdin(input.as_stdio());
    }
    let mut spawned: Vec<Spawned> = Vec::new();
    let mut plans = stages.into_iter().enumerate();
    while let Some((index, stage)) = plans.next() {
        let info = StageInfo {
            index: index + base,
            program: stage.program,
        };
        let StageKind::Native {
            mut executor,
            input,
            mut output,
        } = stage.kind
        else {
            unreachable!()
        };
        if info.index + 1 != count {
            output = Output::Ignored;
        }
        let result = executor.spawn().await;
        // Command stores pipe handles too. Keeping it alive would prevent EOF.
        drop(executor);
        let mut child = match result {
            Ok(child) => child,
            Err(error) => {
                drop(plans);
                drop(input);
                for stage in &mut spawned {
                    stage.input = Input::Ignored;
                    let _ = stage.child.start_kill();
                }
                for stage in &mut spawned {
                    let _ = stage.child.wait().await;
                }
                return Err(info.error(error));
            },
        };
        if info.index + 1 == count && capture_graph && matches!(output, Output::Captured) {
            running.lines = child.stdout.take().map(crate::jsonl::jsonl_frames);
        }
        spawned.push(Spawned {
            child,
            input,
            output,
            info,
        });
    }
    for (index, stage) in spawned.into_iter().enumerate() {
        running.jobs.push(Some(Box::pin(run_stage(
            stage,
            receiver.clone(),
            if index == 0 {
                source_info.clone()
            } else {
                None
            },
        ))));
    }
    Ok(running)
}