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
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
//! A workflow the runner runs as one step of a run's own workflow. The run's own phases say
//! only that the workflow's steps run, and then the merge. What those steps are is reported as
//! they run: the workflow's own name, its phases in the order it declares them, and its
//! agents, in the shape a CLI reports its own workflow in.
//!
//! The view shows the reported phases in place of the step's one phase, so what you see is the
//! workflow that runs, whatever it is called and however many phases it has.

use argonauts_core::events::session::{CliAgent, CliAgentState, CliWorkflowRun};
use argonauts_core::names::{SessionKey, WorkflowId};
use argonauts_core::views::{
    AgentRow, PhaseCount, PhaseRow, RowNote, RowStatus, Tone, WorkflowRow, children_inside,
};
use argonauts_core::workflow::{StandardPhase, StepRef};

use time::OffsetDateTime;

use crate::views::sessions::{RunFacts, SessionFacts};

/// One step's workflow as the runner last reported it.
pub(super) struct StepWorkflow {
    /// The step that runs it.
    pub step: StepRef,
    pub workflow: CliWorkflowRun,
    /// The session behind each agent that has one yet, by the agent's index in the report.
    pub agent_sessions: Vec<(u32, SessionKey)>,
}

/// What a reported workflow adds to the view of the workflow whose step runs it.
pub(super) struct Reported {
    /// Agents the report puts in no phase of its own. They stay in the step's own phase.
    pub loose: Vec<AgentRow>,
    /// Its phases in the order it declares them, each with the agents that have started.
    pub phases: Vec<PhaseRow>,
    /// Done of all its agents, the ones still queued among them.
    pub agents_done: u32,
    pub agents_total: u32,
}

impl StepWorkflow {
    pub fn runs_in(&self, workflow: &WorkflowId, phase: u32) -> bool {
        self.step.workflow == *workflow && self.step.phase == phase
    }

    /// Whether `row` is one of its agents: the session that works one now, or an earlier try
    /// under the same label, which the newer session stands for.
    pub fn holds(&self, row: &AgentRow) -> bool {
        let label = row.label.trim();
        self.agent_sessions
            .iter()
            .any(|(_, session)| *session == row.session)
            || (!label.is_empty()
                && self
                    .workflow
                    .agents
                    .iter()
                    .any(|agent| agent.label.trim() == label))
    }

    /// The agent's row once it has a session, under the label the workflow gave it.
    fn row(&self, agent: &CliAgent, sessions: &RunFacts) -> Option<AgentRow> {
        let by_index = self
            .agent_sessions
            .iter()
            .find(|(index, _)| *index == agent.index)
            .and_then(|(_, session)| sessions.session(session));
        let facts = by_index.or_else(|| self.under_label(agent, sessions))?;
        let mut row = sessions.agent_row(facts);
        if !agent.label.trim().is_empty() {
            row.label.clone_from(&agent.label);
        }
        // The session ended well, but the workflow found no answer in it: the agent failed, as
        // its report says, and its row says why in red, as Claude Code draws a failed agent.
        let failed = matches!(agent.state, CliAgentState::Failed | CliAgentState::Blocked);
        if failed && row.status == RowStatus::Done {
            row.status = RowStatus::Failed;
            let why = agent
                .error
                .as_deref()
                .map(str::trim)
                .filter(|why| !why.is_empty())
                .unwrap_or("ended without an answer");
            row.note = Some(RowNote::new(why, Tone::Bad));
        }
        Some(row)
    }

    /// The step's newest session under the agent's label. A report sent before the agent's
    /// session started names no session for it yet, and the runner reports again only when the
    /// agent ends, so until then its label is how its session is known. A session that started
    /// before the agent was asked is another's, as a first round's review is to the next
    /// round's review asked under the same label.
    fn under_label<'a>(
        &self,
        agent: &CliAgent,
        sessions: &'a RunFacts,
    ) -> Option<&'a SessionFacts> {
        let label = agent.label.trim();
        if label.is_empty() {
            return None;
        }
        let asked = agent.started_at_ms.and_then(|ms| {
            OffsetDateTime::from_unix_timestamp_nanos(i128::from(ms) * 1_000_000).ok()
        });
        sessions.sessions.iter().rev().find(|facts| {
            facts.started.workflow_step.as_ref() == Some(&self.step)
                && facts.started.label.as_deref().map(str::trim) == Some(label)
                && asked.is_none_or(|asked| facts.started_at + ASK_SKEW >= asked)
        })
    }

    /// Its phases as the view's rows. `standard` is the step's own phase's, which every phase
    /// of the workflow it runs shares.
    pub fn rows(&self, sessions: &RunFacts, standard: StandardPhase) -> Reported {
        let workflow = &self.workflow;
        let declared = |agent: &CliAgent| {
            agent
                .phase
                .is_some_and(|index| workflow.phases.iter().any(|phase| phase.index == index))
        };
        let loose = workflow
            .agents
            .iter()
            .filter(|agent| !declared(agent))
            .filter_map(|agent| self.row(agent, sessions))
            .collect();
        let phases: Vec<(u32, PhaseRow)> = workflow
            .phases
            .iter()
            .map(|phase| {
                let agents: Vec<(&CliAgent, Option<AgentRow>)> = workflow
                    .agents_in(phase.index)
                    .map(|agent| (agent, self.row(agent, sessions)))
                    .collect();
                // Once the workflow is over, a phase none of its agents ran in stays, marked as
                // never run, so the workflow reads as it was declared.
                let status = if workflow.ended() && agents.is_empty() {
                    RowStatus::Skipped
                } else {
                    phase_status(&agents)
                };
                let row = PhaseRow {
                    name: argonauts_core::text::shown_name(&phase.title),
                    detail: phase.detail.clone(),
                    standard,
                    status,
                    try_number: None,
                    count: workflow
                        .count(phase.index)
                        .and_then(|(done, total)| PhaseCount::of(done, total)),
                    rows: agents
                        .into_iter()
                        .filter_map(|(_, row)| row.map(WorkflowRow::Agent))
                        .collect(),
                    note: None,
                };
                (phase.index, row)
            })
            .collect();
        // A workflow its script runs as a step is a row of the phase that started it.
        let phases = children_inside(phases, workflow, None, None);
        let done = workflow
            .agents
            .iter()
            .filter(|agent| agent.state == CliAgentState::Done)
            .count();
        Reported {
            loose,
            phases,
            agents_done: u32::try_from(done).unwrap_or(u32::MAX),
            agents_total: u32::try_from(workflow.agents.len()).unwrap_or(u32::MAX),
        }
    }
}

/// How far a session's logged start may come before the runner's time for the ask it answers:
/// the two are written by different parts of the program.
const ASK_SKEW: time::Duration = time::Duration::seconds(1);

/// Where a phase stands by its agents. One with a session goes by what its session says,
/// which knows when it waits for you; one still queued has only the report's word.
fn phase_status(agents: &[(&CliAgent, Option<AgentRow>)]) -> RowStatus {
    let statuses: Vec<RowStatus> = agents
        .iter()
        .map(|(agent, row)| match row {
            Some(row) => row.status,
            None => match agent.state {
                CliAgentState::Queued => RowStatus::Queued,
                CliAgentState::Running | CliAgentState::Other => RowStatus::Running,
                CliAgentState::Done => RowStatus::Done,
                CliAgentState::Failed | CliAgentState::Blocked => RowStatus::Failed,
                CliAgentState::Skipped => RowStatus::Skipped,
            },
        })
        .collect();
    let any = |status: RowStatus| statuses.contains(&status);
    let all = |status: RowStatus| statuses.iter().all(|known| *known == status);
    if any(RowStatus::NeedsYou) {
        RowStatus::NeedsYou
    } else if any(RowStatus::Running) {
        RowStatus::Running
    } else if all(RowStatus::Queued) {
        // None started, or it has no agents yet.
        RowStatus::Waiting
    } else if any(RowStatus::Queued) {
        // Between two of its agents.
        RowStatus::Running
    } else if any(RowStatus::Failed) {
        // As Claude Code marks a phase: nobody is at work, and one of its agents failed.
        RowStatus::Failed
    } else if any(RowStatus::Paused) || any(RowStatus::Skipped) {
        // Held with its work kept, or stopped part-way: the phase isn't over, and nothing
        // in it failed.
        RowStatus::Paused
    } else {
        RowStatus::Done
    }
}

#[cfg(test)]
mod tests {
    use argonauts_core::events::session::{
        CliPhase, CliWorkflowRun, EndReason, SessionEvent, SubagentStatus,
    };
    use argonauts_core::events::workflow::{WorkflowEvent, WorkflowStarted};
    use argonauts_core::events::{RunEvent, RunEventKind};
    use argonauts_core::names::RoleName;
    use argonauts_core::views::WorkflowView;
    use argonauts_core::workflow::Workflow;

    use super::super::{WorkflowLog, view_from};
    use super::*;
    use crate::client::test_support::{event, run_id, session_ended, session_started};

    fn outer() -> WorkflowId {
        WorkflowId::numbered(1)
    }

    /// The step of the run's own workflow that runs the workflow in use.
    fn work() -> StepRef {
        StepRef {
            workflow: outer(),
            phase: 0,
            path: vec![0],
        }
    }

    /// A run of the kind the runner makes: the workflow's own steps as one step, then the merge.
    fn run_started() -> RunEvent {
        let workflow: Workflow = serde_json::from_value(serde_json::json!({
            "name": "script",
            "phases": [
                {"name": "Work", "standard": "do", "steps": [{"kind": "build"}]},
                {"name": "Merge", "standard": "decide", "steps": [{"kind": "merge"}]}
            ]
        }))
        .unwrap();
        event(
            0,
            RunEventKind::Workflow(WorkflowEvent::Started(Box::new(WorkflowStarted {
                id: outer(),
                from: None,
                version: "v1".to_owned(),
                parent: None,
                workflow,
            }))),
        )
    }

    /// A session the Work step started, under `label`.
    fn started_as(seq: u64, session: u32, label: &str) -> RunEvent {
        let key = SessionKey::numbered(session);
        let mut event = session_started(seq, &key, None, RoleName::Worker, "codex");
        if let RunEventKind::Session(SessionEvent::Started(started)) = &mut event.kind {
            started.label = Some(label.to_owned());
            started.workflow_step = Some(work());
        }
        event
    }

    /// A report naming `phases` from 1, as the runner numbers them, and `agents` by label,
    /// phase and state, from 1 too.
    fn report(
        name: &str,
        phases: &[&str],
        agents: &[(&str, Option<u32>, CliAgentState)],
    ) -> CliWorkflowRun {
        CliWorkflowRun {
            id: "steps".into(),
            call: None,
            name: name.into(),
            description: "what the workflow does".into(),
            phases: phases
                .iter()
                .zip(1..)
                .map(|(title, index)| CliPhase {
                    index,
                    title: (*title).to_owned(),
                    detail: String::new(),
                })
                .collect(),
            agents: agents
                .iter()
                .zip(1..)
                .map(|((label, phase, state), index)| CliAgent {
                    index,
                    label: (*label).to_owned(),
                    phase: *phase,
                    model: None,
                    state: *state,
                    tokens: 0,
                    tool_calls: 0,
                    duration_ms: 0,
                    started_at_ms: None,
                    try_number: 1,
                    prompt: None,
                    result: None,
                    ..CliAgent::default()
                })
                .collect(),
            status: SubagentStatus::Running,
            tokens: 0,
            tool_calls: 0,
            duration_ms: 0,
            summary: None,
            started_at_ms: None,
        }
    }

    /// The run's view once the runner reported `workflow`, with `sessions` behind its agents.
    fn view(
        events: &[RunEvent],
        workflow: &CliWorkflowRun,
        sessions: &[(u32, u32)],
    ) -> WorkflowView {
        let mut log = WorkflowLog::of(events);
        let sessions = sessions
            .iter()
            .map(|(agent, session)| (*agent, SessionKey::numbered(*session)))
            .collect();
        log.note_report(&work(), workflow, sessions);
        view_from(&log, events, &run_id(), None).unwrap()
    }

    fn names(view: &WorkflowView) -> Vec<&str> {
        view.phases
            .iter()
            .map(|phase| phase.name.as_str())
            .collect()
    }

    fn labels(phase: &PhaseRow) -> Vec<&str> {
        phase
            .rows
            .iter()
            .map(|row| match row {
                WorkflowRow::Agent(agent) => agent.label.as_str(),
                _ => "not an agent",
            })
            .collect()
    }

    use CliAgentState::{Done, Failed, Queued, Running};

    /// A phase is done only when every one of its agents ended well, as Claude Code marks
    /// it. With one that failed and nobody at work, it reads as failed, however many others
    /// finished.
    #[test]
    fn a_phase_with_an_agent_that_failed_isnt_done() {
        let events = [run_started()];
        let workflow = report(
            "council",
            &["Propose", "Challenge"],
            &[
                ("plan", Some(1), Done),
                ("propose:w0", Some(1), Failed),
                ("propose:w1", Some(1), Done),
                ("challenge:w0", Some(2), Done),
            ],
        );
        let shown = view(&events, &workflow, &[]);
        let propose = &shown.phases[0];
        assert_eq!(propose.status, RowStatus::Failed);
        assert_eq!(propose.count, PhaseCount::of(2, 3));
        assert_eq!(shown.phases[1].status, RowStatus::Done);
    }

    /// OpenCode's challenger ended its session well but never gave the answer its schema asks
    /// for, so the report says it failed: its row is โœ— with why, in red, though its session
    /// ended fine, and its phase is marked as Claude Code marks one with a failed agent.
    #[test]
    fn a_phase_whose_agent_ended_well_but_gave_no_answer_fails() {
        let events = [
            run_started(),
            started_as(1, 1, "challenge:p0"),
            session_ended(2, &SessionKey::numbered(1), EndReason::Completed, 16_000),
            started_as(3, 2, "challenge:p3"),
            session_ended(4, &SessionKey::numbered(2), EndReason::Completed, 169_000),
        ];
        let mut workflow = report(
            "council",
            &["Propose", "Challenge"],
            &[
                ("propose:p0", Some(1), Done),
                ("challenge:p0", Some(2), Done),
                ("challenge:p3", Some(2), Failed),
            ],
        );
        let why = "ended without an answer: it tried to edit files in a read-only session";
        workflow.agents[2].error = Some(why.to_owned());
        let shown = view(&events, &workflow, &[(2, 1), (3, 2)]);
        let challenge = &shown.phases[1];
        assert_eq!(challenge.status, RowStatus::Failed);
        assert_eq!(challenge.count, PhaseCount::of(1, 2));
        let WorkflowRow::Agent(failed) = &challenge.rows[1] else {
            panic!("an agent row: {:?}", challenge.rows);
        };
        assert_eq!(failed.status, RowStatus::Failed);
        assert_eq!(failed.note, Some(RowNote::new(why, Tone::Bad)));
        let WorkflowRow::Agent(fine) = &challenge.rows[0] else {
            panic!("an agent row");
        };
        assert_eq!(fine.status, RowStatus::Done);
    }

    /// An agent that was stopped part-way is no failure and no ending: its phase is neither
    /// ticked nor crossed, and keeps its number.
    #[test]
    fn a_phase_with_a_stopped_agent_isnt_over() {
        let events = [
            run_started(),
            started_as(1, 1, "build:store"),
            session_ended(2, &SessionKey::numbered(1), EndReason::Stopped, 1_000),
        ];
        let workflow = report("steps", &["Build"], &[("build:store", Some(1), Running)]);
        let shown = view(&events, &workflow, &[(1, 1)]);
        assert_eq!(shown.phases[0].status, RowStatus::Paused);
    }

    /// A sample moment: the orchestrator wrote the steps, one builder is done, one
    /// builds, one waits its turn, and the reviewers haven't started.
    #[test]
    fn the_view_shows_the_phases_of_the_workflow_a_step_runs() {
        let events = vec![
            run_started(),
            started_as(1, 1, "plan:orchestrator"),
            session_ended(2, &SessionKey::numbered(1), EndReason::Completed, 1_000),
            started_as(3, 2, "build:store"),
            session_ended(4, &SessionKey::numbered(2), EndReason::Completed, 1_000),
            started_as(5, 3, "build:cli"),
        ];
        let workflow = report(
            "add-due-dates",
            &["Build", "Review"],
            &[
                ("build:store", Some(1), Done),
                ("build:cli", Some(1), Running),
                ("build:docs", Some(1), Queued),
                ("review:store", Some(2), Queued),
                ("review:cli", Some(2), Queued),
            ],
        );
        let view = view(&events, &workflow, &[(1, 2), (2, 3)]);

        // The workflow's own phases in its order are the view's: no phase of
        // the run's own around them. The agent that wrote the steps is none of its agents, so
        // it is no row of theirs.
        assert_eq!(names(&view), ["Build", "Review", "Merge"]);
        let [build, review, _merge] = view.phases.as_slice() else {
            panic!("two phases, then the merge");
        };
        assert_eq!(labels(build), ["build:store", "build:cli"]);

        // An agent has a row once it has a session, and the count knows the ones that wait.
        assert_eq!(build.count, PhaseCount::of(1, 3));
        assert_eq!(build.status, RowStatus::Running);
        assert!(review.rows.is_empty());
        assert_eq!(review.count, PhaseCount::of(0, 2));
        assert_eq!(review.status, RowStatus::Waiting);

        // Only the workflow's own five count, and the header's count is its phases' added up.
        assert_eq!((view.agents_done, view.agents_total), (1, 5));
        let added_up = view
            .phases
            .iter()
            .filter_map(|phase| phase.count)
            .fold((0, 0), |(done, total), count| {
                (done + count.done, total + count.total)
            });
        assert_eq!(added_up, (1, 5));
        // An unnamed run goes by the name of the workflow it runs, as its words.
        assert_eq!(view.name, "Add due dates");
        assert_eq!(view.description, "what the workflow does");
    }

    /// The runner reports an agent as running before its session starts, and names its session
    /// only in a later report. Until then the agent's label finds its session, so the phase
    /// shows who works in it.
    #[test]
    fn an_agent_whose_session_isnt_reported_yet_is_found_by_its_label() {
        let events = vec![
            run_started(),
            started_as(1, 1, "plan:orchestrator"),
            session_ended(2, &SessionKey::numbered(1), EndReason::Completed, 1_000),
            started_as(3, 2, "build:rate-limit"),
        ];
        let workflow = report(
            "rate-limit-per-key",
            &["Build", "Review"],
            &[("build:rate-limit", Some(1), Running)],
        );
        let view = view(&events, &workflow, &[]);
        let [build, _review, _merge] = view.phases.as_slice() else {
            panic!("two phases, then the merge");
        };
        assert_eq!(labels(build), ["build:rate-limit"]);
        assert_eq!(build.status, RowStatus::Running);
    }

    /// Nothing about the phases is known ahead: one, twelve, or the same name twice, in the
    /// workflow's own order, each agent in the phase it was put in.
    #[test]
    fn whatever_phases_the_workflow_reports_are_the_views() {
        let events = vec![run_started(), started_as(1, 1, "translate:changelog")];
        let one = report(
            "translate",
            &["Translate the changelog"],
            &[("translate:changelog", Some(1), Running)],
        );
        let shown = view(&events, &one, &[(1, 1)]);
        assert_eq!(names(&shown), ["Translate the changelog", "Merge"]);
        assert_eq!(labels(&shown.phases[0]), ["translate:changelog"]);

        let twelve = [
            "Survey", "Outline", "Draft", "Critique", "Draft", "Critique", "Numbers", "Figures",
            "Proof", "Typeset", "Package", "Announce",
        ];
        // The agent sits in the second Draft, the fifth phase, not the first of that name.
        let events = vec![run_started(), started_as(1, 1, "draft:two")];
        let paper = report(
            "write-the-paper",
            &twelve,
            &[("draft:two", Some(5), Running)],
        );
        let shown = view(&events, &paper, &[(1, 1)]);
        let mut listed = twelve.to_vec();
        listed.push("Merge");
        assert_eq!(names(&shown), listed);
        let with_agents: Vec<usize> = shown
            .phases
            .iter()
            .enumerate()
            .filter(|(_, phase)| !phase.rows.is_empty())
            .map(|(i, _)| i)
            .collect();
        assert_eq!(with_agents, [4]);
        assert_eq!(shown.phases[2].status, RowStatus::Waiting);
        assert_eq!(shown.phases[4].status, RowStatus::Running);
    }

    /// A phase's name starts with a capital, whatever case its script wrote it in.
    #[test]
    fn a_phase_name_starts_with_a_capital() {
        let events = vec![run_started(), started_as(1, 1, "build:due-flag")];
        let lower = report(
            "add-due-flag",
            &["build", "review", "sign-off"],
            &[("build:due-flag", Some(1), Running)],
        );
        let shown = view(&events, &lower, &[(1, 1)]);
        assert_eq!(names(&shown), ["Build", "Review", "Sign-off", "Merge"]);
    }

    /// A workflow the script runs as a step, which the runner reports as one more phase named
    /// for it, is a row of the phase that ran it, Argonauts Code's own, with its agents under it.
    #[test]
    fn a_workflow_the_script_runs_is_a_row_of_its_phase() {
        let events = vec![
            run_started(),
            started_as(1, 1, "check:types"),
            started_as(2, 2, "lint:clippy"),
        ];
        let release = report(
            "release",
            &["Check", "โ–ธ lint"],
            &[
                ("check:types", Some(1), CliAgentState::Done),
                ("lint:clippy", Some(2), Running),
            ],
        );
        let shown = view(&events, &release, &[(1, 1), (2, 2)]);
        assert_eq!(names(&shown), ["Check", "Merge"]);
        let check = &shown.phases[0];
        let Some(WorkflowRow::Workflow(lint)) = check.rows.last() else {
            panic!("lint is a row of Check: {check:#?}");
        };
        assert_eq!((lint.name.as_str(), lint.worker.as_deref()), ("lint", None));
        assert_eq!((lint.agents_done, lint.agents_total), (0, 1));
        assert_eq!(
            check.count.map(|count| count.total),
            Some(2),
            "its agents count in Check"
        );
    }

    /// Before the workflow says what its phases are, the step's own phase holds what there
    /// is, and an agent it puts in no phase stays there too.
    #[test]
    fn a_workflow_that_reports_no_phases_yet_keeps_the_steps_own_phase() {
        let events = vec![run_started()];
        let nothing = report("", &[], &[]);
        let shown = view(&events, &nothing, &[]);
        assert_eq!(names(&shown), ["Work", "Merge"]);
        assert_eq!((shown.agents_done, shown.agents_total), (0, 0));
        // It gave no name either, so the run's own stands.
        assert_eq!(shown.name, "script");

        let events = vec![run_started(), started_as(1, 1, "scout")];
        let loose = report("warming-up", &[], &[("scout", None, Running)]);
        let shown = view(&events, &loose, &[(1, 1)]);
        assert_eq!(names(&shown), ["Work", "Merge"]);
        assert_eq!(labels(&shown.phases[0]), ["scout"]);
        assert_eq!((shown.agents_done, shown.agents_total), (0, 1));

        // A phase it names later takes the agents put in it, and one in none stays.
        let events = vec![
            run_started(),
            started_as(1, 1, "scout"),
            started_as(2, 2, "build:a"),
        ];
        let grown = report(
            "warming-up",
            &["Build"],
            &[("scout", None, Done), ("build:a", Some(1), Running)],
        );
        let shown = view(&events, &grown, &[(1, 1), (2, 2)]);
        // Once it names one, its phases are the view's, and an agent in none goes first in
        // the first of them.
        assert_eq!(names(&shown), ["Build", "Merge"]);
        assert_eq!(labels(&shown.phases[0]), ["scout", "build:a"]);
    }

    /// An agent tried again is one row, the session that works it now, and a newer report of
    /// the same workflow replaces the older one.
    #[test]
    fn a_retried_agent_is_one_row_and_the_newest_report_stands() {
        let events = vec![
            run_started(),
            started_as(1, 1, "build:cli"),
            session_ended(
                2,
                &SessionKey::numbered(1),
                EndReason::Crashed {
                    message: "the CLI exited".to_owned(),
                },
                1_000,
            ),
            started_as(3, 2, "build:cli"),
        ];
        let mut log = WorkflowLog::of(&events);
        let first = report(
            "add-due-dates",
            &["Build"],
            &[("build:cli", Some(1), Running)],
        );
        log.note_report(&work(), &first, vec![(1, SessionKey::numbered(1))]);
        let again = report(
            "add-due-dates",
            &["Build", "Review"],
            &[("build:cli", Some(1), Running)],
        );
        log.note_report(&work(), &again, vec![(1, SessionKey::numbered(2))]);
        let shown = view_from(&log, &events, &run_id(), None).unwrap();
        assert_eq!(names(&shown), ["Build", "Review", "Merge"]);
        let WorkflowRow::Agent(row) = &shown.phases[0].rows[0] else {
            panic!("an agent");
        };
        assert_eq!(shown.phases[0].rows.len(), 1);
        assert_eq!(row.session, SessionKey::numbered(2));
        assert_eq!(row.status, RowStatus::Running);
        assert_eq!((shown.agents_done, shown.agents_total), (0, 1));
    }

    /// The runner's own event is all the view needs: recorded in the run, its report shows
    /// with no other word to the view.
    #[test]
    fn the_runners_report_event_reaches_the_view() {
        use argonauts_core::events::workflow::{AgentSession, WorkflowReport};

        let workflow = report(
            "rate-limit-per-key",
            &["Build", "Review"],
            &[
                ("build:rate-limit", Some(1), Done),
                ("review:rate-limit", Some(2), Running),
            ],
        );
        let agent_session = |agent: u32, session: u32| AgentSession {
            agent,
            session: SessionKey::numbered(session),
        };
        let events = vec![
            run_started(),
            started_as(1, 1, "plan:orchestrator"),
            session_ended(2, &SessionKey::numbered(1), EndReason::Completed, 1_000),
            started_as(3, 2, "build:rate-limit"),
            session_ended(4, &SessionKey::numbered(2), EndReason::Completed, 1_000),
            started_as(5, 3, "review:rate-limit"),
            event(
                6,
                RunEventKind::Workflow(WorkflowEvent::Reported(Box::new(WorkflowReport {
                    step: work(),
                    workflow,
                    sessions: vec![agent_session(1, 2), agent_session(2, 3)],
                    size_asked: None,
                }))),
            ),
        ];
        let shown = super::super::workflow_view(&events, &run_id(), None).unwrap();
        assert_eq!(names(&shown), ["Build", "Review", "Merge"]);
        assert_eq!(labels(&shown.phases[0]), ["build:rate-limit"]);
        assert_eq!(labels(&shown.phases[1]), ["review:rate-limit"]);
        assert_eq!(shown.phases[1].status, RowStatus::Running);
        assert_eq!((shown.agents_done, shown.agents_total), (1, 2));
        assert_eq!(shown.name, "Rate limit per key");
    }

    /// Once the workflow is over, a phase none of its agents ran in stays, marked as never run.
    #[test]
    fn a_finished_workflow_marks_the_phases_nothing_ran_in() {
        let events = vec![
            run_started(),
            started_as(1, 1, "build:a"),
            session_ended(2, &SessionKey::numbered(1), EndReason::Completed, 1_000),
        ];
        let mut done = report(
            "small-fix",
            &["Build", "Review", "Sign off"],
            &[("build:a", Some(1), Done)],
        );
        done.status = SubagentStatus::Done;
        let shown = view(&events, &done, &[(1, 1)]);
        assert_eq!(names(&shown), ["Build", "Review", "Sign off", "Merge"]);
        assert_eq!(shown.phases[0].status, RowStatus::Done);
        assert_eq!(shown.phases[1].status, RowStatus::Skipped);
        assert_eq!(shown.phases[2].status, RowStatus::Skipped);
    }

    /// A session the Work step started for a council: `role`, reading only or not, on no task.
    fn council_session(
        seq: u64,
        session: u32,
        label: &str,
        role: RoleName,
        reads: bool,
    ) -> RunEvent {
        let mut event = started_as(seq, session, label);
        if let RunEventKind::Session(SessionEvent::Started(started)) = &mut event.kind {
            started.role = role;
            if reads {
                started.access = argonauts_core::session_runner::Access::ReadOnly;
            }
        }
        event
    }

    fn answered(seq: u64, session: u32, answer: serde_json::Value) -> RunEvent {
        let mut event = session_ended(
            seq,
            &SessionKey::numbered(session),
            EndReason::Completed,
            1_000,
        );
        if let RunEventKind::Session(SessionEvent::Ended(end)) = &mut event.kind {
            end.structured = Some(answer);
        }
        event
    }

    /// A council's rows say what each agent gave: a proposal is ready, a challenge says what it
    /// argued against, the judge that decides what it chose, and the sign-off that it read the
    /// task's change. A judge's plan is no verdict, so no review word shows on its row.
    /// A challenge that argues against no proposal says so, and a review a script asked of a
    /// read-only agent reads as its verdict, never as a proposal. A session the run's stop cut
    /// short stopped with the run.
    #[test]
    fn a_challenge_against_none_a_scripts_review_and_a_stopped_build_say_what_they_were() {
        use argonauts_core::events::run::{RunLifecycle, RunOutcome};
        use serde_json::json;
        let mut council = crate::client::test_support::run_started(0, "Per key rate limit", None);
        if let RunEventKind::Run(RunLifecycle::Started(started)) = &mut council.kind {
            started.workflow = "council".to_owned();
        }
        let events = vec![
            council,
            run_started(),
            council_session(1, 1, "challenge:p2", RoleName::Worker, true),
            answered(2, 1, json!({"against": [], "points": ["both are sound"]})),
            council_session(3, 2, "review:keying", RoleName::Worker, true),
            answered(
                4,
                2,
                json!({"accept": false, "findings": ["no Retry-After header", "no test for it"]}),
            ),
            started_as(5, 3, "build:keying"),
            session_ended(6, &SessionKey::numbered(3), EndReason::Paused, 1_000),
            crate::client::test_support::event(
                7,
                RunEventKind::Run(RunLifecycle::Finished {
                    outcome: RunOutcome::Stopped,
                    detail: None,
                }),
            ),
        ];
        let workflow = report(
            "council",
            &["Challenge", "Cross-review", "Build"],
            &[
                ("challenge:p2", Some(1), Done),
                ("review:keying", Some(2), Done),
                ("build:keying", Some(3), Running),
            ],
        );
        let shown = view(&events, &workflow, &[(1, 1), (2, 2), (3, 3)]);
        let notes: Vec<String> = shown.phases[..3]
            .iter()
            .map(|phase| match phase.rows.first() {
                Some(WorkflowRow::Agent(agent)) => agent
                    .note
                    .as_ref()
                    .map(|note| note.text.clone())
                    .unwrap_or_default(),
                _ => String::new(),
            })
            .collect();
        assert_eq!(notes[0], "against none ยท 1 point");
        assert!(notes[1].starts_with("sent it back"), "{notes:?}");
        assert_eq!(notes[2], "stopped with the run ยท work so far kept");
    }

    /// A review asked again for a later round, under the same label, has no session of its own
    /// yet: the first round's review is its own row, never this one's as well.
    #[test]
    fn a_later_rounds_ask_under_the_same_label_isnt_the_earlier_session() {
        let ms = |ts: OffsetDateTime| u64::try_from(ts.unix_timestamp_nanos() / 1_000_000).unwrap();
        let events = vec![
            run_started(),
            started_as(1, 1, "review:keying"),
            session_ended(2, &SessionKey::numbered(1), EndReason::Completed, 1_000),
        ];
        let mut workflow = report(
            "per-key",
            &["Review"],
            &[
                ("review:keying", Some(1), Done),
                ("review:keying", Some(1), Running),
            ],
        );
        workflow.agents[0].started_at_ms = Some(ms(events[1].ts));
        workflow.agents[1].started_at_ms = Some(ms(events[2].ts + time::Duration::seconds(5)));
        let shown = view(&events, &workflow, &[]);
        let [review, _merge] = shown.phases.as_slice() else {
            panic!("the review, then the merge: {:?}", shown.phases);
        };
        assert_eq!(labels(review), ["review:keying"]);
    }

    #[test]
    fn a_councils_rows_say_what_each_agent_gave() {
        use serde_json::json;
        let mut council = crate::client::test_support::run_started(0, "Per key rate limit", None);
        if let RunEventKind::Run(argonauts_core::events::run::RunLifecycle::Started(started)) =
            &mut council.kind
        {
            started.workflow = "council".to_owned();
        }
        let events = vec![
            council,
            run_started(),
            council_session(1, 1, "propose:p1", RoleName::Worker, true),
            answered(2, 1, json!({"plan": "one bucket a key"})),
            council_session(3, 2, "challenge:p1", RoleName::Worker, true),
            answered(
                4,
                2,
                json!({"against": ["p2", "p3"], "points": ["a", "b", "c"]}),
            ),
            council_session(5, 3, "decide:plan", RoleName::Judge, false),
            answered(
                6,
                3,
                json!({"chose": "p2", "summary": "Chose p2.", "plan": "x"}),
            ),
            council_session(7, 4, "signoff:per-key", RoleName::Judge, false),
            answered(8, 4, json!({"accept": true, "findings": []})),
        ];
        let workflow = report(
            "council",
            &["Propose", "Challenge", "Decide", "Sign-off"],
            &[
                ("propose:p1", Some(1), Done),
                ("challenge:p1", Some(2), Done),
                ("decide:plan", Some(3), Done),
                ("signoff:per-key", Some(4), Done),
            ],
        );
        let shown = view(&events, &workflow, &[(1, 1), (2, 2), (3, 3), (4, 4)]);
        let notes: Vec<Option<String>> = shown.phases[..4]
            .iter()
            .map(|phase| match phase.rows.first() {
                Some(WorkflowRow::Agent(agent)) => {
                    agent.note.as_ref().map(|note| note.text.clone())
                }
                _ => None,
            })
            .collect();
        assert_eq!(
            notes,
            [
                Some("proposal ready".to_owned()),
                Some("against p2 and p3 ยท 3 points".to_owned()),
                Some("chose p2".to_owned()),
                Some("read the task's change".to_owned()),
            ]
        );
    }
}