speccoder-runtime 0.8.0

Application services of speccoder: the only crate that executes anything.
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
//! `speccoder run` over a whole feature, and `converge`.

use std::path::Path;
use std::sync::Arc;

use speccoder_core::{CommandData, CommandResult, CommandStatus};

use crate::pipeline::*;
use crate::{controls, convergence, hosting, progress, substrate, worktree};

/// `speccoder run` without `--task`: attempts ready tasks in document order
/// (read from the worktree) until the feature is done or an attempt does
/// not pass. The provider comes from `[model]` and its key variable.
#[must_use]
pub fn run_feature(
    path: &Path,
    feature: Option<&str>,
    options: &RunOptions,
    converge: bool,
) -> CommandResult {
    with_driver("run", path, feature, |state, provider, key| {
        if converge {
            run_converged_with(path, feature, state, provider, Some(key), options)
        } else {
            run_feature_with(path, feature, state, provider, Some(key), options)
        }
    })
}

/// `speccoder converge`: assess the finished feature and append what remains
///. The provider comes from `[model]` and its key variable.
#[must_use]
pub fn converge(path: &Path, feature: Option<&str>) -> CommandResult {
    converge_in(path, feature, false)
}

/// [`converge`], in the person's checkout when `in_place`.
#[must_use]
pub fn converge_in(path: &Path, feature: Option<&str>, in_place: bool) -> CommandResult {
    let options = RunOptions {
        in_place,
        ..RunOptions::default()
    };
    with_driver("converge", path, feature, |state, provider, key| {
        convergence::converge_with_options(
            path,
            feature,
            state,
            Arc::clone(provider),
            Some(key),
            &options,
        )
    })
}

/// Opens the configured model driver (key from the environment only) and
/// hands it to `f` with the state directory.
pub(crate) fn with_driver(
    command: &str,
    path: &Path,
    feature: Option<&str>,
    f: impl FnOnce(&Path, &Arc<dyn agentplane::model::ModelProvider>, &str) -> CommandResult,
) -> CommandResult {
    let Some(state) = worktree::state_dir() else {
        return CommandResult::new(
            command,
            CommandStatus::ConfigError,
            "no state directory (set SPECCODER_STATE_DIR or HOME)",
        )
        .with_code("NO-STATE-DIR");
    };
    let model = match setup(path, feature, &state, command) {
        Ok(s) => s.model,
        Err(r) => return *r,
    };
    let (driver, key) = match substrate::model_config::open_driver(&model) {
        Ok((d, k)) => (Ok::<_, String>(d), k.unwrap_or_default()),
        Err(e) => return substrate::model_config::refused(command, &e),
    };
    match driver {
        Ok(provider) => f(&state, &provider, &key),
        Err(e) => CommandResult::new(command, CommandStatus::ConfigError, &redact(&e, Some(&key))),
    }
}

/// `run --converge`: work the feature through, converge, and work through
/// what convergence appended — until it converges, stops making progress,
/// reaches `[converge] max_rounds`, or an attempt needs a person.
pub fn run_converged_with(
    path: &Path,
    feature: Option<&str>,
    state: &Path,
    provider: &Arc<dyn agentplane::model::ModelProvider>,
    secret: Option<&str>,
    options: &RunOptions,
) -> CommandResult {
    // In place: one session over every round.
    if options.in_place && options.session.is_none() {
        return in_session("run", path, feature, state, options, |o| {
            run_converged_with(path, feature, state, provider, secret, o)
        });
    }
    let mut attempts = Vec::new();
    let mut rounds = Vec::new();
    loop {
        let res = run_feature_with(path, feature, state, provider, secret, options);
        let Some(CommandData::Feature(fr)) = res.data() else {
            return res;
        };
        let mut fr = (**fr).clone();
        attempts.append(&mut fr.attempts);
        let finished = res.status() == CommandStatus::Ok && fr.stop.starts_with("feature done");
        if !finished {
            fr.attempts = attempts;
            fr.converge = rounds;
            return CommandResult::carried("run", &res)
                .with_data(CommandData::Feature(Box::new(fr)));
        }
        if let Some(p) = &options.progress {
            p.event(&progress::Event::Notice {
                text: format!("converge: assessing {} against its spec", fr.feature),
            });
        }
        let c = convergence::converge_with_options(
            path,
            feature,
            state,
            Arc::clone(provider),
            secret,
            options,
        );
        if let Some(p) = &options.progress {
            p.event(&progress::Event::Notice {
                text: format!("converge: {}", c.message()),
            });
        }
        let outcome = match c.data() {
            Some(CommandData::Converge(r)) => {
                let outcome = r.outcome.clone();
                rounds.push((**r).clone());
                outcome
            }
            _ => String::new(),
        };
        if outcome != "tasks_appended" {
            fr.attempts = attempts;
            fr.converge = rounds;
            fr.stop = format!("converge: {}", c.message());
            return CommandResult::new("run", c.status(), &fr.stop.clone())
                .with_data(CommandData::Feature(Box::new(fr)));
        }
    }
}

/// [`run_feature`] with an explicit state directory and model driver.
pub fn run_feature_with(
    path: &Path,
    feature: Option<&str>,
    state: &Path,
    provider: &std::sync::Arc<dyn agentplane::model::ModelProvider>,
    secret: Option<&str>,
    options: &RunOptions,
) -> CommandResult {
    // In place: one session around the whole feature.
    if options.in_place && options.session.is_none() {
        return in_session("run", path, feature, state, options, |o| {
            run_feature_with(path, feature, state, provider, secret, o)
        });
    }
    let max_tasks = options.max_tasks;
    use speccoder_core::FeatureReport;
    use speccoder_core::snapshot::TaskState;

    let mut attempts = Vec::new();
    let mut feature_name = feature.map(str::to_owned);
    let hook_lines: std::cell::RefCell<Vec<String>> = std::cell::RefCell::new(Vec::new());
    let parallel_lines: std::cell::RefCell<Vec<String>> = std::cell::RefCell::new(Vec::new());
    let full_tier: std::cell::RefCell<Option<speccoder_core::FullTier>> =
        std::cell::RefCell::new(None);
    let finish = |name: Option<String>,
                  attempts: Vec<speccoder_core::RunReport>,
                  status: CommandStatus,
                  stop: String| {
        // An autopilot run lists every call it refused instead of asking.
        let autopilot_denied: Vec<speccoder_core::oracle::AutopilotDenial> =
            if options.mode == speccoder_core::oracle::RunMode::Autopilot {
                attempts
                    .iter()
                    .flat_map(|a| crate::oracle::autopilot_denials(&a.task, &a.tool_calls))
                    .collect()
            } else {
                Vec::new()
            };
        if let Some(n) = name.as_deref() {
            crate::oracle::record_denials(
                &worktree::project_state_dir(state, path),
                n,
                &autopilot_denied,
            );
        }
        let stop = if autopilot_denied.is_empty() {
            stop
        } else {
            format!(
                "{stop} — autopilot refused {} call(s) instead of asking; they are listed for you (speccoder inbox)",
                autopilot_denied.len()
            )
        };
        CommandResult::new("run", status, &stop).with_data(CommandData::Feature(Box::new(
            FeatureReport {
                feature: name.unwrap_or_default(),
                attempts,
                stop,
                converge: Vec::new(),
                hooks: hook_lines.borrow().clone(),
                parallel: parallel_lines.borrow().clone(),
                full_tier: full_tier.borrow().clone(),
                mode: options.mode,
                autopilot_denied,
            },
        )))
    };
    // `[YOU]` tasks the person gave a hint for: attempted after all.
    let mut hinted: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    // Tasks that are yours and nobody answered for: passed by like a
    // skipped one, listed at the end with their three actions.
    let mut passed: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    // Tasks that must run on their own after a parallel group did not merge.
    let mut one_at_a_time: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    let mut parallel_off = false;
    let mut budget = max_tasks.unwrap_or(usize::MAX);
    // Unfinished checklists stop the run before anything runs.
    if let Err(stop) = checklist_gate(path, feature, options.ignore_checklists) {
        return *stop;
    }
    // The format's `before_implement` hooks.
    match hosting::run_hooks(
        "before_implement",
        "",
        path,
        state,
        provider,
        secret,
        options,
    ) {
        Ok(lines) => hook_lines.borrow_mut().extend(lines),
        Err(stop) => {
            return finish(
                feature_name,
                attempts,
                stop.status(),
                stop.message().to_owned(),
            );
        }
    }
    let mut planned = false;
    while budget > 0 {
        if options
            .cancel
            .as_ref()
            .is_some_and(|c| c.load(std::sync::atomic::Ordering::SeqCst))
        {
            return finish(
                feature_name,
                attempts,
                CommandStatus::NeedsHuman,
                "cancelled by the person before the next task".to_owned(),
            );
        }
        // The next ready task, read from the worktree.
        let wt = workspace(path, feature_name.as_deref(), state, options);
        let Some(CommandData::Worktree(w)) = wt.data() else {
            return CommandResult::carried("run", &wt);
        };
        if wt.status() != CommandStatus::Ok {
            return CommandResult::carried("run", &wt).with_data(CommandData::Worktree(w.clone()));
        }
        feature_name = Some(w.feature.clone());
        let project = std::path::PathBuf::from(w.project_dir.clone().unwrap_or_default());
        let format = crate::format::at(&project);
        let Some(snap) = format.inspect(&project, Some(&w.feature)) else {
            return CommandResult::new(
                "run",
                CommandStatus::NotAProject,
                format.messages().no_project_in_worktree,
            );
        };
        let schedule = snap
            .feature
            .as_ref()
            .and_then(|f| f.schedule.clone())
            .unwrap_or_else(|| speccoder_core::snapshot::Schedule {
                tasks: Vec::new(),
                totals: speccoder_core::snapshot::ScheduleTotals::default(),
            });
        if !planned {
            planned = true;
            if let Some(p) = &options.progress {
                let skipped = controls::skipped(&project, &w.feature);
                let titles: std::collections::HashMap<&str, &str> = snap
                    .feature
                    .as_ref()
                    .and_then(|f| f.tasks.as_ref())
                    .map(|t| {
                        t.tasks
                            .iter()
                            .map(|x| (x.id.as_str(), x.title.as_str()))
                            .collect()
                    })
                    .unwrap_or_default();
                p.event(&crate::progress::Event::RunStarted {
                    feature: w.feature.clone(),
                    tasks: schedule
                        .tasks
                        .iter()
                        .map(|t| crate::progress::PlannedTask {
                            id: t.id.clone(),
                            title: titles
                                .get(t.id.as_str())
                                .copied()
                                .unwrap_or_default()
                                .to_owned(),
                            state: if skipped.contains_key(&t.id) {
                                "skipped".to_owned()
                            } else {
                                match t.state {
                                    TaskState::Done => "done",
                                    TaskState::Ready => "ready",
                                    _ => "blocked",
                                }
                                .to_owned()
                            },
                        })
                        .collect(),
                });
                // Who does what, said once: information, not a warning.
                if let Some(l) = crate::status::lanes(path, Some(&w.feature)) {
                    p.event(&crate::progress::Event::Notice { text: l.line() });
                }
                // The plan checks, said once per run start; they never
                // stop, delay or re-order it.
                let warnings = crate::plan_lint::lints(format, &snap, &project).len();
                if warnings > 0 {
                    p.event(&crate::progress::Event::Notice {
                        text: format!(
                            "plan: {warnings} warning{} — speccoder doctor --feature {}",
                            if warnings == 1 { "" } else { "s" },
                            w.feature
                        ),
                    });
                }
            }
        }
        // No task to work: the feature is not done, its tasks are not
        // written yet (no task list, or one without a task).
        if let Some(stop) = snap.feature.as_ref().and_then(|f| no_tasks(format, f)) {
            return finish(
                feature_name,
                attempts,
                CommandStatus::TaskNotReady,
                stop.message,
            )
            .with_code("NO-TASKS")
            .with_fix(stop.fix)
            .with_next(stop.next);
        }
        // Skipped tasks are passed by; what depends on them stays blocked.
        let skipped = controls::skipped(&project, feature_name.as_deref().unwrap_or_default());
        // A task that is yours — marked `[YOU]`, or its last attempt stopped
        // `BLOCKED` — is not attempted: the person is asked, or it is passed
        // by until they answer (`run --task` and `task hint` attempt it).
        let parsed: Vec<&speccoder_core::snapshot::Task> = snap
            .feature
            .as_ref()
            .and_then(|f| f.tasks.as_ref())
            .map(|t| t.tasks.iter().collect())
            .unwrap_or_default();
        let saved = crate::attempts::list(&project, &w.feature);
        let stopped = |id: &str| {
            saved
                .iter()
                .find(|a| a.task == id && !controls::is_set_aside(a))
                .filter(|a| a.outcome == "blocked")
                .map(|a| a.reason.clone())
        };
        let yours = |id: &str| {
            !hinted.contains(id)
                && (parsed.iter().any(|t| t.id == id && t.yours) || stopped(id).is_some())
        };
        let Some(next) = schedule.tasks.iter().find(|t| {
            t.state == TaskState::Ready && !skipped.contains_key(&t.id) && !passed.contains(&t.id)
        }) else {
            let open: Vec<String> = schedule
                .tasks
                .iter()
                .filter(|t| t.state != TaskState::Done)
                .map(|t| {
                    if skipped.contains_key(&t.id) {
                        format!("{} (skipped)", t.id)
                    } else {
                        t.id.clone()
                    }
                })
                .collect();
            return if open.is_empty() {
                // The `full` tier, once, when this run ticked the last task:
                // a failure is reported and stops `pr`; the ticks stand.
                if attempts
                    .iter()
                    .any(|a: &speccoder_core::RunReport| a.ticked)
                {
                    let wt_root = std::path::PathBuf::from(w.path.clone().unwrap_or_default());
                    let tier = full::run_once(&project, &wt_root);
                    full::record(&wt_root, feature_name.as_deref().unwrap_or_default(), &tier);
                    let failed = (!tier.passed).then(|| tier.failed.join(", "));
                    *full_tier.borrow_mut() = Some(tier);
                    if let Some(failed) = failed {
                        return finish(
                            feature_name,
                            attempts,
                            CommandStatus::VerificationFailed,
                            format!(
                                "every task is ticked, but the full-tier checks failed: {failed}; fix them, or `speccoder pr --force`"
                            ),
                        );
                    }
                }
                // The held-out sets, now that no later task can read them.
                if let (Some(name), Ok(opened)) =
                    (feature_name.as_deref(), crate::format::open(path, feature))
                {
                    let wt_root = std::path::PathBuf::from(w.path.clone().unwrap_or_default());
                    let plane = crate::worktree::project_state_dir(
                        state,
                        std::path::Path::new(&opened.snapshot.root),
                    );
                    if let Err(e) =
                        crate::held_out::publish(&wt_root, &plane, name, &plane.join("tmp"))
                    {
                        hook_lines
                            .borrow_mut()
                            .push(format!("held-out sets not published: {e}"));
                    }
                }
                // The format's `after_implement` hooks.
                match hosting::run_hooks(
                    "after_implement",
                    "",
                    path,
                    state,
                    provider,
                    secret,
                    options,
                ) {
                    Ok(lines) => hook_lines.borrow_mut().extend(lines),
                    Err(stop) => {
                        return finish(
                            feature_name,
                            attempts,
                            stop.status(),
                            stop.message().to_owned(),
                        );
                    }
                }
                finish(
                    feature_name,
                    attempts,
                    CommandStatus::Ok,
                    "feature done: every task is ticked".to_owned(),
                )
            } else if let Some(first) = passed.iter().next() {
                // What waits is the person's: named, with its three actions.
                let fix = speccoder_core::codes::lookup("TASK-YOURS")
                    .map(|e| {
                        speccoder_core::codes::fixes(e, "run", Some(first), feature_name.as_deref())
                    })
                    .unwrap_or_default();
                let mine: Vec<String> = passed.iter().cloned().collect();
                let message = match mine.as_slice() {
                    [one] => format!(
                        "{one} is yours — {}: a step only a person can take",
                        parsed
                            .iter()
                            .find(|t| t.id == *one)
                            .map(|t| t.title.as_str())
                            .unwrap_or_default()
                    ),
                    many => format!(
                        "{} are yours: steps only a person can take",
                        many.join(", ")
                    ),
                };
                finish(feature_name, attempts, CommandStatus::NeedsHuman, message)
                    .with_code("TASK-YOURS")
                    .with_fix(fix)
            } else {
                finish(
                    feature_name,
                    attempts,
                    CommandStatus::TaskNotReady,
                    format!("no task can run; open: {}", open.join(", ")),
                )
            };
        };
        let title_of = |id: &str| {
            parsed
                .iter()
                .find(|t| t.id == id)
                .map(|t| t.title.clone())
                .unwrap_or_default()
        };
        if yours(&next.id) {
            let id = next.id.clone();
            let stop = crate::approvals::StopRequest {
                task: format!("{}/{id}", w.feature),
                title: title_of(&id),
                claim: stopped(&id),
                dependents: speccoder_core::graph::dependents(&schedule, &id),
            };
            if let Some(failed) = settle_stop(
                path,
                feature_name.as_deref(),
                state,
                options,
                &stop,
                &mut hinted,
                &mut passed,
            ) {
                return finish(
                    feature_name,
                    attempts,
                    failed.status(),
                    format!("{id}: {}", failed.message()),
                )
                .caused_by(&failed);
            }
            continue;
        }
        // ready [P] tasks run at once, each in its own lane.
        let parallel_ids: std::collections::BTreeSet<String> = snap
            .feature
            .as_ref()
            .and_then(|f| f.tasks.as_ref())
            .map(|t| {
                t.tasks
                    .iter()
                    .filter(|t| t.parallel)
                    .map(|t| t.id.clone())
                    .collect()
            })
            .unwrap_or_default();
        let group: Vec<String> = schedule
            .tasks
            .iter()
            .filter(|t| t.state == TaskState::Ready && !skipped.contains_key(&t.id))
            .filter(|t| {
                parallel_ids.contains(&t.id) && !one_at_a_time.contains(&t.id) && !yours(&t.id)
            })
            // A story whose oracle is still to be written goes first, alone.
            .filter(|t| {
                !super::story::oracle_pending(
                    std::path::Path::new(w.path.as_deref().unwrap_or_default()),
                    &crate::worktree::project_state_dir(state, path),
                    format,
                    &snap,
                    &t.id,
                )
            })
            .map(|t| t.id.clone())
            .take(options.parallel.min(budget))
            .collect();
        if !parallel_off && group.len() >= 2 {
            budget = budget.saturating_sub(group.len());
            let feature = feature_name.clone().unwrap_or_default();
            let feature_wt = std::path::PathBuf::from(w.path.clone().unwrap_or_default());
            let lanes = run_lanes(path, &feature, &group, state, provider, secret, options);
            let outcome = merge_lanes(path, &feature_wt, &project, &feature, lanes);
            attempts.extend(outcome.reports);
            parallel_lines.borrow_mut().extend(outcome.lines);
            one_at_a_time.extend(outcome.one_at_a_time);
            parallel_off |= outcome.reverted;
            if let Some((task, status, message)) = outcome.stop {
                // An honest stop in a lane is asked about like any other.
                let claim = attempts
                    .iter()
                    .rev()
                    .find(|a| a.task == task && a.attempt_status == "blocked")
                    .map(|a| a.reason.clone().unwrap_or_default());
                let Some(claim) = claim else {
                    return finish(feature_name, attempts, status, format!("{task}: {message}"));
                };
                let stop = crate::approvals::StopRequest {
                    task: format!("{}/{task}", w.feature),
                    title: title_of(&task),
                    claim: Some(claim),
                    dependents: speccoder_core::graph::dependents(&schedule, &task),
                };
                if let Some(failed) = settle_stop(
                    path,
                    feature_name.as_deref(),
                    state,
                    options,
                    &stop,
                    &mut hinted,
                    &mut passed,
                ) {
                    return finish(
                        feature_name,
                        attempts,
                        failed.status(),
                        format!("{task}: {}", failed.message()),
                    )
                    .caused_by(&failed);
                }
            }
            continue;
        }
        budget -= 1;
        let task = next.id.clone();
        let res = run_task(
            path,
            feature_name.as_deref(),
            &task,
            state,
            std::sync::Arc::clone(provider),
            secret,
            options,
        );
        if let Some(CommandData::Run(r)) = res.data() {
            attempts.push((**r).clone());
        }
        // An honest stop: the person is asked in place — I did it, a hint
        // and a new attempt, or skip — or it is passed by; the run goes on.
        let claim = match res.data() {
            Some(CommandData::Run(r)) if r.attempt_status == "blocked" => {
                Some(r.reason.clone().unwrap_or_default())
            }
            _ => None,
        };
        if let Some(claim) = claim.filter(|_| res.status() != CommandStatus::Ok) {
            let stop = crate::approvals::StopRequest {
                task: format!("{}/{task}", w.feature),
                title: title_of(&task),
                claim: Some(claim),
                dependents: speccoder_core::graph::dependents(&schedule, &task),
            };
            if let Some(failed) = settle_stop(
                path,
                feature_name.as_deref(),
                state,
                options,
                &stop,
                &mut hinted,
                &mut passed,
            ) {
                return finish(
                    feature_name,
                    attempts,
                    failed.status(),
                    format!("{task}: {}", failed.message()),
                )
                .caused_by(&failed);
            }
            continue;
        }
        if res.status() != CommandStatus::Ok {
            return finish(
                feature_name,
                attempts,
                res.status(),
                format!("{task}: {}", res.message()),
            )
            .caused_by(&res);
        }
    }
    finish(
        feature_name,
        attempts,
        CommandStatus::Ok,
        "stopped at --max-tasks".to_owned(),
    )
}

/// A task that needs the person, settled: asked ([`ask_at_stop`]) — after
/// a hint the run attempts it again — or, unanswered, passed by. `Some`:
/// taking the answer failed, and the run ends with that.
fn settle_stop(
    path: &Path,
    feature: Option<&str>,
    state: &Path,
    options: &RunOptions,
    stop: &crate::approvals::StopRequest,
    hinted: &mut std::collections::BTreeSet<String>,
    passed: &mut std::collections::BTreeSet<String>,
) -> Option<CommandResult> {
    let id = stop
        .task
        .rsplit('/')
        .next()
        .unwrap_or(&stop.task)
        .to_owned();
    match ask_at_stop(path, feature, state, options, stop) {
        Some((answer, done)) if done.status() == CommandStatus::Ok => {
            if matches!(answer, crate::approvals::StopAnswer::Hint(_)) {
                hinted.insert(id);
            }
            None
        }
        Some((_, failed)) => Some(failed),
        None => {
            passed.insert(id);
            None
        }
    }
}

/// Asks the person at a task that needs them — at a terminal, never in a
/// lane — and takes the answer — I did it, a hint, skip — through the one
/// dispatcher ([`crate::actions::answered_at_stop`]); after a hint the run
/// attempts the task again. `None`: nobody answered; the task waits in Needs You.
fn ask_at_stop(
    path: &Path,
    feature: Option<&str>,
    state: &Path,
    options: &RunOptions,
    stop: &crate::approvals::StopRequest,
) -> Option<(crate::approvals::StopAnswer, CommandResult)> {
    let approver = options
        .approver
        .as_ref()
        .filter(|_| options.lane.is_none())?;
    let answer = approver.at_stop(stop)?;
    let task = stop.task.rsplit('/').next().unwrap_or(&stop.task);
    let done = crate::actions::answered_at_stop(
        path,
        feature,
        task,
        &answer,
        (approver.via(), options),
        state,
    );
    Some((answer, done))
}

/// Why a unit has nothing to run: no task list, or one without a task.
#[derive(Debug, Clone)]
pub(crate) struct NoTasks {
    /// `no tasks yet: …`, naming the format's file and how to write it.
    pub message: String,
    /// The planning skills that write the tasks, as commands.
    pub fix: Vec<String>,
    /// The same as next steps, then the run.
    pub next: Vec<speccoder_core::next::NextStep>,
}

/// [`NoTasks`] for `feature`, unless it has a task: such a unit is never
/// done (code `NO-TASKS`).
pub(crate) fn no_tasks(
    format: &dyn crate::format::Adapter,
    feature: &speccoder_core::snapshot::FeatureSnapshot,
) -> Option<NoTasks> {
    if feature.tasks.as_ref().is_some_and(|t| !t.tasks.is_empty()) {
        return None;
    }
    let messages = format.messages();
    let skills: Vec<String> = (format.planning().toward_tasks)(feature.artifacts.plan)
        .iter()
        .map(|s| (*s).to_owned())
        .collect();
    let name = &feature.feature.name;
    Some(NoTasks {
        message: format!(
            "no tasks yet: {name} {}; {}",
            if feature.tasks.is_some() {
                format!("has a {} without a task", messages.tasks_file)
            } else {
                format!("has no {}", messages.tasks_file)
            },
            messages.make_tasks
        ),
        fix: skills
            .iter()
            .map(|s| format!("speccoder spec {s}"))
            .collect(),
        next: speccoder_core::next::next(
            &speccoder_core::next::Outcome::Planning { skills },
            Some(name),
        ),
    })
}