argonauts-app 0.1.0

Argonauts Code's app controller and run host: the conversation, routing, choosing Captain or crew, and the run host each run has.
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
//! Your conversation's Claude Code session while it works, through the real controller loop:
//! a message goes in at once, send now and take back name it by its uuid, and the session is
//! joined as a screen only when You should know is on.

use std::time::Duration;

use argonauts_core::app::{AppEvent, AppMode, ConversationEvent, UiCommand};
use argonauts_core::event::AgentEvent;
use argonauts_core::events::context::ContextDetail;
use argonauts_core::events::session::{SubagentKind, SubagentStatus, SubagentUpdate};
use argonauts_core::input::SlashCommand;
use argonauts_core::screen::{ScreenChange, ScreenRequest};
use argonauts_core::session_runner::{RunnerEvent, SessionControl};
use argonauts_testkit::FakeSession;

use crate::controller::test_support::{CliOptions, TestDriver};

fn init(capabilities: &[&str], plugins: &[&str]) -> RunnerEvent {
    let names = |list: &[&str]| list.iter().map(|&name| name.to_owned()).collect();
    RunnerEvent::Agent(AgentEvent::Init {
        session_id: Some("s-1".into()),
        model: Some("claude-opus-5-5".into()),
        tools: Vec::new(),
        plugins: names(plugins),
        capabilities: names(capabilities),
        version: Some("2.1.289".into()),
    })
}

fn writing() -> RunnerEvent {
    RunnerEvent::Agent(AgentEvent::TextDelta {
        text: "Running the slow test".into(),
        parent: None,
    })
}

/// Direct Captain, whose turns answer in the CLI's own words, so a message can join them.
async fn direct() -> TestDriver {
    TestDriver::start_with(CliOptions {
        prepare: Some(Box::new(|repo: &argonauts_testkit::TempRepo| {
            std::fs::write(
                repo.global_config(),
                "[captain_or_crew]\nforce = \"captain\"\n",
            )
            .unwrap();
        })),
        ..CliOptions::default()
    })
    .await
}

async fn working(driver: &mut TestDriver, events: Vec<RunnerEvent>) {
    driver.agent.push(FakeSession::until_stopped(events));
    driver
        .submit("check release.md, then run ./slow-test.sh")
        .await;
    driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Conversation {
                    event: ConversationEvent::Agent {
                        event: AgentEvent::TextDelta { .. },
                        ..
                    }
                }
            )
        })
        .await;
}

/// What the session got, once something came.
async fn controls_after(driver: &TestDriver, count: usize) -> Vec<SessionControl> {
    let agent = driver.agent.clone();
    for _ in 0..200 {
        let controls: Vec<SessionControl> = agent.controls().into_iter().map(|(_, c)| c).collect();
        if controls.len() >= count {
            return controls;
        }
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    agent.controls().into_iter().map(|(_, c)| c).collect()
}

#[tokio::test]
async fn a_message_goes_in_at_once_and_send_now_and_take_back_name_it() {
    let mut driver = direct().await;
    working(
        &mut driver,
        vec![init(&["interrupt_send_now_v1"], &[]), writing()],
    )
    .await;
    driver.submit("also check the changelog").await;
    let controls = controls_after(&driver, 1).await;
    let Some(SessionControl::Send { text, uuid, .. }) = controls.first() else {
        panic!("{controls:?}");
    };
    assert_eq!(text, "also check the changelog");
    let uuid = uuid.clone();

    driver.send(UiCommand::SendNow).await;
    driver.send(UiCommand::TakeBackConversation).await;
    let controls = controls_after(&driver, 3).await;
    assert_eq!(
        controls[1..3],
        [
            SessionControl::SendNow { uuid: uuid.clone() },
            SessionControl::TakeBack { uuid }
        ]
    );
    // No second turn waits: the message went into this one.
    driver.send(UiCommand::CancelTurn).await;
    driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Mode {
                    mode: AppMode::Idle
                }
            )
        })
        .await;
    driver.exit().await;
}

#[tokio::test]
async fn a_follow_up_typed_during_the_estimate_waits_for_the_next_turn() {
    // The estimate's answer has a schema, read from the session's last result. A follow-up
    // that joined it would be answered instead of the request, whose answer would be lost.
    let mut driver = TestDriver::start().await;
    driver.agent.push(FakeSession::until_stopped(vec![
        init(&["interrupt_send_now_v1"], &[]),
        writing(),
    ]));
    driver.submit("check release.md").await;
    // The screen isn't offered send now for this turn either.
    let shown = driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Conversation {
                    event: ConversationEvent::Agent {
                        event: AgentEvent::Init { .. },
                        ..
                    }
                }
            )
        })
        .await;
    let AppEvent::Conversation {
        event:
            ConversationEvent::Agent {
                event: AgentEvent::Init { capabilities, .. },
                ..
            },
    } = shown
    else {
        unreachable!()
    };
    assert!(capabilities.is_empty(), "{capabilities:?}");
    driver.submit("also check the changelog").await;
    driver.send(UiCommand::SendNow).await;
    tokio::time::sleep(Duration::from_millis(50)).await;
    let controls = driver.agent.controls();
    assert!(
        !controls.iter().any(|(_, control)| matches!(
            control,
            SessionControl::Send { .. } | SessionControl::SendNow { .. }
        )),
        "{controls:?}"
    );
    driver.send(UiCommand::CancelTurn).await;
    let taken = driver
        .until(|event| matches!(event, AppEvent::TakenBack { .. }))
        .await;
    assert_eq!(
        taken,
        AppEvent::TakenBack {
            texts: vec!["also check the changelog".to_owned()]
        }
    );
    driver.exit().await;
}

#[tokio::test]
async fn without_send_now_a_message_waits_for_the_next_turn() {
    let mut driver = TestDriver::start().await;
    working(&mut driver, vec![init(&[], &[]), writing()]).await;
    driver.submit("also check the changelog").await;
    driver.send(UiCommand::SendNow).await;
    tokio::time::sleep(Duration::from_millis(50)).await;
    let controls = driver.agent.controls();
    assert!(
        !controls.iter().any(|(_, control)| matches!(
            control,
            SessionControl::Send { .. } | SessionControl::SendNow { .. }
        )),
        "{controls:?}"
    );
    driver.send(UiCommand::CancelTurn).await;
    let taken = driver
        .until(|event| matches!(event, AppEvent::TakenBack { .. }))
        .await;
    assert_eq!(
        taken,
        AppEvent::TakenBack {
            texts: vec!["also check the changelog".to_owned()]
        }
    );
    driver.exit().await;
}

const NOTICE_PLUGIN: &str = "cc-plugin-you-should-know@builtin";

async fn screen_requests(options: CliOptions, capabilities: &[&str], plugins: &[&str]) -> usize {
    let mut driver = TestDriver::start_with(options).await;
    working(&mut driver, vec![init(capabilities, plugins), writing()]).await;
    tokio::time::sleep(Duration::from_millis(50)).await;
    let asked = driver
        .agent
        .controls()
        .iter()
        .filter(|(_, control)| matches!(control, SessionControl::Screen(_)))
        .count();
    driver.send(UiCommand::CancelTurn).await;
    driver.exit().await;
    asked
}

#[tokio::test]
async fn the_main_session_joins_as_a_screen_only_when_you_should_know_is_on() {
    let all = ["interrupt_send_now_v1", "ui_surface_v1"];
    let mut driver = TestDriver::start().await;
    working(&mut driver, vec![init(&all, &[NOTICE_PLUGIN]), writing()]).await;
    let controls = controls_after(&driver, 2).await;
    assert_eq!(
        controls[..2],
        [
            SessionControl::Screen(ScreenRequest::Attach),
            SessionControl::Screen(ScreenRequest::Render)
        ]
    );
    // A press names a button the session drew; one it never drew says so on the notice line
    // and sends nothing.
    driver
        .send(UiCommand::PressNotice {
            plugin: "cc-plugin-you-should-know".into(),
            handle: 7,
        })
        .await;
    driver
        .until(|event| matches!(event, AppEvent::Hint { .. }))
        .await;
    driver.send(UiCommand::CancelTurn).await;
    driver.exit().await;

    assert_eq!(
        screen_requests(CliOptions::default(), &all, &[]).await,
        0,
        "the mod isn't on"
    );
    assert_eq!(
        screen_requests(CliOptions::default(), &all[..1], &[NOTICE_PLUGIN]).await,
        0,
        "the CLI can't take a screen"
    );
    let off = CliOptions {
        flags: vec![r#"ui.notices="off""#.to_owned()],
        ..CliOptions::default()
    };
    assert_eq!(
        screen_requests(off, &all, &[NOTICE_PLUGIN]).await,
        0,
        "ui.notices = off"
    );
}

fn draws_nothing(event: &AppEvent) -> bool {
    matches!(
        event,
        AppEvent::Conversation {
            event: ConversationEvent::Agent {
                event: AgentEvent::ScreenChanged {
                    change: ScreenChange::Drawn { drawn },
                },
                ..
            },
        } if drawn.is_empty()
    )
}

/// A notice an earlier session drew stays above the input only while a session joins as a
/// screen. A turn whose session won't, as when you turned the mod off in Claude Code or the
/// conversation went to another CLI, says nothing is drawn there, and so does `/clear`.
#[tokio::test]
async fn a_turn_that_wont_join_and_clear_take_the_notice_away() {
    let mut driver = TestDriver::start().await;
    working(
        &mut driver,
        vec![
            init(&["interrupt_send_now_v1", "ui_surface_v1"], &[]),
            writing(),
        ],
    )
    .await;
    assert!(driver.seen().iter().any(draws_nothing));
    driver.send(UiCommand::CancelTurn).await;
    driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Mode {
                    mode: AppMode::Idle
                }
            )
        })
        .await;
    driver
        .send(UiCommand::Slash {
            command: SlashCommand::Clear,
        })
        .await;
    driver
        .until(|event| matches!(event, AppEvent::ConversationCleared { .. }))
        .await;
    driver.exit().await;

    // A session that joins draws for itself.
    let mut driver = TestDriver::start().await;
    working(
        &mut driver,
        vec![
            init(
                &["interrupt_send_now_v1", "ui_surface_v1"],
                &[NOTICE_PLUGIN],
            ),
            writing(),
        ],
    )
    .await;
    assert!(!driver.seen().iter().any(draws_nothing));
    driver.send(UiCommand::CancelTurn).await;
    driver.exit().await;
}

#[tokio::test]
async fn context_opening_and_a_watch_reach_the_running_session() {
    let mut driver = TestDriver::start().await;
    driver
        .send(UiCommand::WatchContext {
            detail: Some(ContextDetail::Summary),
        })
        .await;
    working(&mut driver, vec![init(&[], &[]), writing()]).await;
    // The watch set between turns goes to the session as it starts.
    let controls = controls_after(&driver, 1).await;
    assert_eq!(
        controls[0],
        SessionControl::WatchContext(Some(ContextDetail::Summary))
    );
    driver
        .send(UiCommand::Slash {
            command: SlashCommand::Context {
                all: false,
                action: Default::default(),
            },
        })
        .await;
    driver
        .until(|event| matches!(event, AppEvent::View { .. }))
        .await;
    driver.send(UiCommand::WatchContext { detail: None }).await;
    let controls = controls_after(&driver, 3).await;
    assert_eq!(
        controls[1..3],
        [
            // Opening the tab counts as Claude Code's own /context does.
            SessionControl::AskContext(ContextDetail::Full),
            SessionControl::WatchContext(None)
        ]
    );
    driver.send(UiCommand::CancelTurn).await;
    driver.exit().await;
}

/// `/context print` while the session works: the session is asked to count itself as its own
/// /context does, and the rows print when its count is in. Here it never answers, so they
/// print as the turn ends, from what the session last said.
#[tokio::test]
async fn context_print_counts_the_working_session_first() {
    use argonauts_core::app::ViewData;
    use argonauts_core::input::ViewAction;

    let mut driver = TestDriver::start().await;
    let filled = RunnerEvent::Agent(AgentEvent::ContextFill {
        used: 48_200,
        size: Some(200_000),
        sent: None,
    });
    working(&mut driver, vec![init(&[], &[]), filled, writing()]).await;
    driver
        .send(UiCommand::Slash {
            command: SlashCommand::Context {
                all: false,
                action: ViewAction::Print,
            },
        })
        .await;
    let controls = controls_after(&driver, 1).await;
    assert_eq!(controls[0], SessionControl::AskContext(ContextDetail::Full));
    let printed = |event: &AppEvent| matches!(event, AppEvent::View { data } if matches!(**data, ViewData::ContextPrint { .. }));
    assert!(
        !driver.seen().iter().any(printed),
        "nothing prints before the count"
    );
    driver.send(UiCommand::CancelTurn).await;
    let AppEvent::View { data } = driver.until(printed).await else {
        unreachable!()
    };
    let ViewData::ContextPrint { view } = *data else {
        unreachable!()
    };
    let rows: Vec<(Option<&argonauts_core::RunId>, u64, Option<u64>)> = view
        .rows
        .iter()
        .map(|row| (row.run.as_ref(), row.used, row.window))
        .collect();
    assert_eq!(rows, [(None, 48_200, Some(200_000))]);
    driver.exit().await;
}

/// `x` on a subagent of the conversation's own session: the session is asked to stop that one
/// and goes on. With no turn running there's nothing to ask, and you're told.
#[tokio::test]
async fn stopping_a_subagent_asks_the_session_and_leaves_the_turn_running() {
    let mut driver = direct().await;
    working(&mut driver, vec![init(&[], &[]), writing()]).await;
    driver
        .send(UiCommand::StopSubagent {
            id: "toolu_a".into(),
        })
        .await;
    let controls = controls_after(&driver, 1).await;
    assert_eq!(
        controls,
        [SessionControl::StopSubagent {
            id: "toolu_a".into()
        }]
    );
    driver.send(UiCommand::CancelTurn).await;
    driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Conversation {
                    event: ConversationEvent::TurnCancelled { .. }
                }
            )
        })
        .await;
    driver
        .send(UiCommand::StopSubagent {
            id: "toolu_a".into(),
        })
        .await;
    let told = driver
        .until(|event| matches!(event, AppEvent::Notice { .. }))
        .await;
    let AppEvent::Notice { text, .. } = told else {
        unreachable!()
    };
    assert_eq!(
        text,
        "That subagent isn't running, so there's nothing to stop."
    );
    driver.exit().await;
}

/// Enter on a subagent's row: the main agent gets your words with how to pass them on, as
/// its own command, so a name whose first word reads like an agent's is never a mention, and
/// the conversation shows only `@name` and your words.
#[tokio::test]
async fn a_message_for_a_subagent_goes_to_the_main_agent_whatever_its_name() {
    let mut driver = direct().await;
    driver.agent.push(FakeSession::until_stopped(vec![
        init(&["interrupt_send_now_v1"], &[]),
        writing(),
    ]));
    driver
        .send(UiCommand::MessageSubagent {
            name: "Count slowly".into(),
            text: "stop at 5".into(),
        })
        .await;
    let started = driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Conversation {
                    event: ConversationEvent::TurnStarted { .. }
                }
            )
        })
        .await;
    let AppEvent::Conversation {
        event: ConversationEvent::TurnStarted { text, .. },
    } = started
    else {
        unreachable!()
    };
    assert_eq!(text, "@Count slowly stop at 5");
    let prompt: String = driver
        .agent
        .specs()
        .last()
        .map(|spec| spec.prompt.iter().map(|part| part.text.as_str()).collect())
        .unwrap_or_default();
    assert!(
        prompt.contains("For your subagent \"Count slowly\": stop at 5")
            && prompt.contains("SendMessage"),
        "{prompt}"
    );

    // While that turn runs, the next goes in at once.
    driver
        .send(UiCommand::MessageSubagent {
            name: "Explore the auth flow".into(),
            text: "skip the tests".into(),
        })
        .await;
    let controls = controls_after(&driver, 1).await;
    let Some(SessionControl::Send { text, .. }) = controls.first() else {
        panic!("{controls:?}");
    };
    assert!(
        text.starts_with("For your subagent \"Explore the auth flow\": skip the tests"),
        "{text}"
    );
    assert!(
        !driver.seen().iter().any(|event| matches!(
            event,
            AppEvent::Notice { text, .. } if text.contains("mention")
        )),
        "never read as a mention"
    );
    driver.exit().await;
}

fn background(running: &[&str]) -> RunnerEvent {
    RunnerEvent::Agent(AgentEvent::BackgroundTasks {
        running: running.iter().map(|&task| task.to_owned()).collect(),
        ended: running.is_empty(),
    })
}

/// One of the session's own subagents, going or done.
fn subagent(status: SubagentStatus) -> RunnerEvent {
    RunnerEvent::Agent(AgentEvent::Subagent {
        update: SubagentUpdate {
            id: "toolu_a".into(),
            parent: None,
            kind: SubagentKind::Subagent,
            label: Some("Count slowly".into()),
            prompt: None,
            status,
            last_message: None,
            log_uri: None,
            steps_visible: true,
            tokens: None,
        },
    })
}

/// Esc while the conversation's own session runs subagents: only the main agent's turn is
/// asked to stop. The turn stays for them, so one can still be stopped by
/// itself. With nothing in the background, or only a command the session left running, which
/// has no row to stop it from, Esc stops the session whole, as before.
#[tokio::test]
async fn esc_stops_the_main_agents_turn_alone_while_its_own_subagents_run() {
    let mut driver = direct().await;
    working(
        &mut driver,
        vec![
            init(&[], &[]),
            subagent(SubagentStatus::Running),
            background(&["b1"]),
            writing(),
        ],
    )
    .await;
    driver.send(UiCommand::CancelTurn).await;
    driver
        .send(UiCommand::StopSubagent {
            id: "toolu_a".into(),
        })
        .await;
    let controls = controls_after(&driver, 2).await;
    assert_eq!(
        controls,
        [
            SessionControl::StopTurn,
            SessionControl::StopSubagent {
                id: "toolu_a".into()
            }
        ]
    );
    let cancelled = driver.seen().iter().any(|event| {
        matches!(
            event,
            AppEvent::Conversation {
                event: ConversationEvent::TurnCancelled { .. }
            }
        )
    });
    assert!(!cancelled, "the turn goes on for its subagents");
    driver.exit().await;

    let alone = vec![init(&[], &[]), writing()];
    let with_a_command = vec![
        init(&[], &[]),
        subagent(SubagentStatus::Done),
        background(&["bash-1"]),
        writing(),
    ];
    for events in [alone, with_a_command] {
        let mut driver = direct().await;
        working(&mut driver, events).await;
        driver.send(UiCommand::CancelTurn).await;
        driver
            .until(|event| {
                matches!(
                    event,
                    AppEvent::Conversation {
                        event: ConversationEvent::TurnCancelled { .. }
                    }
                )
            })
            .await;
        assert_eq!(controls_after(&driver, 1).await, [SessionControl::Stop]);
        driver.exit().await;
    }
}

/// A turn stopped alone ends when its session does. What the main agent said once its
/// subagents were done is shown as its words, and starts no work: you stopped that request.
#[tokio::test]
async fn a_turn_stopped_alone_starts_no_work_when_its_session_ends() {
    use argonauts_core::app::TurnReply;
    use argonauts_core::event::ErrorCategory;
    use serde_json::json;

    let change = json!({
        "kind": "change",
        "answer_markdown": "The README's install line names the wrong package.",
        "estimate": {
            "task_type": "docs",
            "summary": "Fix the install command in the README",
            "files_likely": ["README.md"],
            "file_count": 1,
            "risk_labels": [],
            "tests_exist": true,
            "unknowns": []
        }
    });
    let mut driver = TestDriver::start_with(CliOptions {
        prepare: Some(Box::new(|repo: &argonauts_testkit::TempRepo| {
            std::fs::write(
                repo.global_config(),
                "ultracode = true\n[captain]\nkind = \"checked\"\n",
            )
            .unwrap();
        })),
        ..CliOptions::default()
    })
    .await;
    let mut session = FakeSession::structured(change);
    let verdict = session.events.pop().expect("the result");
    session.events = vec![
        init(&[], &[]),
        subagent(SubagentStatus::Running),
        background(&["b1"]),
        writing(),
        RunnerEvent::Agent(AgentEvent::Error {
            category: ErrorCategory::Interrupted,
            message: "interrupted".into(),
            retryable: false,
        }),
        RunnerEvent::Agent(AgentEvent::Result {
            text: None,
            structured: None,
            is_error: true,
        }),
        subagent(SubagentStatus::Done),
        background(&[]),
        verdict,
    ];
    // Slow enough that Esc lands while the main agent still writes.
    driver
        .agent
        .push(session.with_delay(Duration::from_millis(200)));
    driver.submit("fix the install command in the README").await;
    driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Conversation {
                    event: ConversationEvent::Agent {
                        event: AgentEvent::TextDelta { .. },
                        ..
                    }
                }
            )
        })
        .await;
    driver.send(UiCommand::CancelTurn).await;
    let finished = driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Conversation {
                    event: ConversationEvent::TurnFinished { .. }
                        | ConversationEvent::TurnFailed { .. }
                }
            )
        })
        .await;
    let AppEvent::Conversation {
        event: ConversationEvent::TurnFinished { reply, .. },
    } = finished
    else {
        panic!("{finished:?}");
    };
    assert_eq!(
        reply,
        TurnReply::Answer {
            markdown: "The README's install line names the wrong package.".into()
        }
    );
    driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Mode {
                    mode: AppMode::Idle
                }
            )
        })
        .await;
    assert!(driver.conductor.starts().is_empty(), "no run was started");
    assert_eq!(controls_after(&driver, 1).await, [SessionControl::StopTurn]);
    driver.exit().await;
}

/// A turn stopped alone whose session ends with nothing more ends as any turn Esc stops: it
/// is cancelled, nothing failed, and no fallback takes the request.
#[tokio::test]
async fn a_turn_stopped_alone_with_nothing_after_it_ends_cancelled() {
    use argonauts_core::event::ErrorCategory;
    use argonauts_core::events::session::EndReason;

    let mut driver = direct().await;
    let mut session = FakeSession::ended(EndReason::Stopped);
    session.events = vec![
        init(&[], &[]),
        subagent(SubagentStatus::Running),
        background(&["b1"]),
        writing(),
        RunnerEvent::Agent(AgentEvent::Error {
            category: ErrorCategory::Interrupted,
            message: "interrupted".into(),
            retryable: false,
        }),
        RunnerEvent::Agent(AgentEvent::Result {
            text: None,
            structured: None,
            is_error: true,
        }),
    ];
    driver
        .agent
        .push(session.with_delay(Duration::from_millis(200)));
    driver.submit("count in the background").await;
    driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Conversation {
                    event: ConversationEvent::Agent {
                        event: AgentEvent::TextDelta { .. },
                        ..
                    }
                }
            )
        })
        .await;
    driver.send(UiCommand::CancelTurn).await;
    let ended = driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Conversation {
                    event: ConversationEvent::TurnCancelled { .. }
                        | ConversationEvent::TurnFailed { .. }
                        | ConversationEvent::TurnFinished { .. }
                }
            )
        })
        .await;
    assert!(
        matches!(
            ended,
            AppEvent::Conversation {
                event: ConversationEvent::TurnCancelled { .. }
            }
        ),
        "{ended:?}"
    );
    driver
        .until(|event| {
            matches!(
                event,
                AppEvent::Mode {
                    mode: AppMode::Idle
                }
            )
        })
        .await;
    assert_eq!(driver.agent.specs().len(), 1, "no fallback took it");
    driver.exit().await;
}