argonauts-crew 0.1.0

Argonauts Code run engine: a workflow runner for Understand, planning, dividing, task pipelines, merge, and the run review.
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
//! Understand: readings, differences, what was settled, the drafted and approved goal
//! file, and goal changes. And the goals being kept: the run's own, which its workflow goes
//! round until it holds, and each task's, when its role works a task as a goal.
//!
//! A round is readings, then listings, then one `Settled`, then drafts. A reading or a
//! listing that arrives after a round settled starts the next round (a send back), and so does a
//! second `Settled`, which is how a round with no readings left still shows up.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use argonauts_core::TaskId;
use argonauts_core::events::RunEvent;
use argonauts_core::events::goal::{
    Assumption, CliGoalSaid, Decider, GoalChangeResolution, GoalChecked, GoalEvent, GoalFile,
    GoalQuestion, GoalRun, GoalStatus, Try,
};
use argonauts_core::events::run::Party;
use argonauts_core::names::{MessageId, ModelRef};
use argonauts_core::steps::understand::{Difference, Reading};

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct GoalState {
    /// 1 for the first round of readings, +1 for each send back.
    pub round: u32,
    /// `(label, worker, reading)` in the order they were recorded, labels A, B, C.
    pub readings: Vec<(String, ModelRef, Reading)>,
    pub compared: Vec<(ModelRef, Vec<Difference>)>,
    pub settled: Option<SettledGoal>,
    /// The seq of this round's `Settled`. Understand approvals opened after it belong to this
    /// round.
    pub settled_at: Option<u64>,
    /// The newest draft of this round, answers and edits included.
    pub drafted: Option<GoalFile>,
    /// Frozen once set: every build and experiment session carries its hash. Only an approved goal
    /// change replaces it, with a higher version.
    pub approved: Option<GoalFile>,
    pub approved_by: Option<Party>,
    pub changes: Vec<GoalChange>,
    /// The run's own goal, with its rounds and status as they stand.
    #[serde(default)]
    pub pursuit: Option<GoalRun>,
    /// Every check, oldest first: the run's goal's and each task's.
    #[serde(default)]
    pub checks: Vec<GoalChecked>,
    /// `(question, answer, who)` for each question answered, oldest first.
    #[serde(default)]
    pub answers: Vec<(String, String, Decider)>,
    /// The rounds after whose check the run's goal changed approach, so the next one goes to
    /// another provider's model.
    #[serde(default)]
    pub stuck_at: Vec<u32>,
    /// Why the run's goal ended short of holding.
    #[serde(default)]
    pub ended_because: Option<String>,
    /// The goals of the tasks worked as goals.
    #[serde(default)]
    pub tasks: BTreeMap<TaskId, TaskGoal>,
}

/// One task's goal as it's kept.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct TaskGoal {
    pub goal: GoalRun,
    /// The try after whose check it changed approach, which happens once.
    pub approach_changed_after: Option<u32>,
    /// Why it ended short of holding.
    pub ended_because: Option<String>,
    /// The CLI's own account of the goal, when the CLI keeps it, with the turns it has reported
    /// on so far.
    pub cli_says: Option<CliGoalSaid>,
    /// Turns in a row, in this try, that the CLI ended with its goal still active.
    #[serde(default)]
    pub continues: u32,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct SettledGoal {
    pub agreements: Vec<String>,
    pub assumptions: Vec<Assumption>,
    pub questions: Vec<GoalQuestion>,
    pub dropped: u32,
}

/// A goal change asked for mid-run, and every resolution recorded for it, the final one last.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct GoalChange {
    pub id: MessageId,
    pub from: Party,
    pub text: String,
    pub clarification: bool,
    pub resolutions: Vec<GoalChangeResolution>,
    pub new_sha256: Option<String>,
}

impl GoalChange {
    /// Recorded, refused, or changed. `AskedYou` alone is still open.
    pub fn resolved(&self) -> Option<GoalChangeResolution> {
        self.resolutions
            .last()
            .copied()
            .filter(|resolution| *resolution != GoalChangeResolution::AskedYou)
    }
}

impl GoalState {
    pub fn apply(&mut self, event: &GoalEvent, envelope: &RunEvent) {
        match event {
            GoalEvent::Read {
                label,
                worker,
                reading,
            } => {
                if self.approved.is_some() {
                    return;
                }
                if self.round_settled() || !self.compared.is_empty() {
                    self.next_round();
                }
                self.round = self.round.max(1);
                self.readings
                    .push((label.clone(), worker.clone(), reading.clone()));
            }
            GoalEvent::Compared {
                worker,
                differences,
            } => {
                if self.approved.is_some() {
                    return;
                }
                if self.round_settled() {
                    self.next_round();
                }
                self.round = self.round.max(1);
                self.compared.push((worker.clone(), differences.clone()));
            }
            GoalEvent::Settled {
                agreements,
                assumptions,
                questions,
                dropped,
            } => {
                if self.approved.is_some() {
                    return;
                }
                if self.round_settled() {
                    self.next_round();
                }
                self.round = self.round.max(1);
                self.settled = Some(SettledGoal {
                    agreements: agreements.clone(),
                    assumptions: assumptions.clone(),
                    questions: questions.clone(),
                    dropped: *dropped,
                });
                self.settled_at = Some(envelope.seq);
            }
            GoalEvent::Drafted(file) => {
                if self.approved.is_none() {
                    self.drafted = Some(file.clone());
                }
            }
            GoalEvent::Approved { file, by } => {
                let newer = self
                    .approved
                    .as_ref()
                    .is_none_or(|approved| file.version > approved.version);
                if newer {
                    self.approved = Some(file.clone());
                    self.approved_by = Some(by.clone());
                }
            }
            GoalEvent::ChangeRequested {
                id,
                from,
                text,
                clarification,
            } => {
                if self.change(id).is_none() {
                    self.changes.push(GoalChange {
                        id: id.clone(),
                        from: from.clone(),
                        text: text.clone(),
                        clarification: *clarification,
                        resolutions: Vec::new(),
                        new_sha256: None,
                    });
                }
            }
            GoalEvent::ChangeResolved {
                id,
                resolution,
                new_sha256,
            } => {
                if let Some(change) = self.changes.iter_mut().find(|change| change.id == *id) {
                    change.resolutions.push(*resolution);
                    if new_sha256.is_some() {
                        change.new_sha256.clone_from(new_sha256);
                    }
                }
            }
            GoalEvent::Set(goal) => match &goal.task {
                Some(task) => {
                    let kept = self.tasks.remove(task);
                    self.tasks.insert(
                        task.clone(),
                        TaskGoal {
                            goal: (**goal).clone(),
                            approach_changed_after: kept
                                .as_ref()
                                .and_then(|kept| kept.approach_changed_after),
                            ended_because: None,
                            continues: kept.as_ref().map_or(0, |kept| kept.continues),
                            cli_says: kept.and_then(|kept| kept.cli_says),
                        },
                    );
                }
                None => self.pursuit = Some((**goal).clone()),
            },
            GoalEvent::TryStarted {
                n,
                title,
                phase,
                task,
            } => {
                if let Some(goal) = self.goal_mut(task.as_ref()) {
                    goal.tries.push(Try {
                        n: *n,
                        title: title.clone(),
                        phase: *phase,
                        verdict: None,
                    });
                }
                if let Some(kept) = task.as_ref().and_then(|task| self.tasks.get_mut(task)) {
                    kept.continues = 0;
                }
            }
            GoalEvent::Checked(checked) => {
                if let Some(a_try) = self.goal_mut(checked.task.as_ref()).and_then(|goal| {
                    goal.tries
                        .iter_mut()
                        .find(|a_try| a_try.n == checked.try_number)
                }) {
                    a_try.verdict = Some(checked.overall);
                }
                self.checks.push((**checked).clone());
            }
            GoalEvent::StatusChanged {
                status, task, why, ..
            } => {
                if let Some(goal) = self.goal_mut(task.as_ref()) {
                    goal.status = *status;
                }
                let because = match status {
                    GoalStatus::Pursuing | GoalStatus::Met => None,
                    _ => why.clone(),
                };
                match task {
                    Some(task) => {
                        if let Some(kept) = self.tasks.get_mut(task) {
                            kept.ended_because = because;
                        }
                    }
                    None if self.pursuit.is_some() => self.ended_because = because,
                    None => {}
                }
            }
            GoalEvent::ApproachChanged { after, task, .. } => match task {
                Some(task) => {
                    if let Some(kept) = self.tasks.get_mut(task) {
                        kept.approach_changed_after = Some(*after);
                    }
                }
                None => {
                    if !self.stuck_at.contains(after) {
                        self.stuck_at.push(*after);
                    }
                }
            },
            GoalEvent::CliSaid { task, said } => {
                if let Some(kept) = task.as_ref().and_then(|task| self.tasks.get_mut(task)) {
                    let turns = kept.cli_says.as_ref().and_then(|said| said.turns);
                    kept.continues = if crate::goal::cli::still_going(said) {
                        kept.continues + 1
                    } else {
                        0
                    };
                    kept.cli_says = Some(CliGoalSaid {
                        turns: Some(turns.unwrap_or(0) + 1),
                        ..said.clone()
                    });
                }
            }
            GoalEvent::Answered {
                question,
                answer,
                by,
            } => self.answers.push((question.clone(), answer.clone(), *by)),
            // The changed assumption reaches the goal file as its next version.
            GoalEvent::AssumptionChanged { .. } => {}
            // A check you asked for decides nothing, so it stays out of `checks`, which says
            // what follows each try.
            GoalEvent::CheckingNow { .. }
            | GoalEvent::CheckedNow(_)
            | GoalEvent::CheckNowDropped { .. } => {}
        }
    }

    pub fn approved_sha256(&self) -> Option<&str> {
        self.approved.as_ref().map(|file| file.sha256.as_str())
    }

    /// The goal `task` is worked as, or the run's own for `None`.
    pub fn goal_of(&self, task: Option<&TaskId>) -> Option<&GoalRun> {
        match task {
            Some(task) => self.tasks.get(task).map(|kept| &kept.goal),
            None => self.pursuit.as_ref(),
        }
    }

    fn goal_mut(&mut self, task: Option<&TaskId>) -> Option<&mut GoalRun> {
        match task {
            Some(task) => self.tasks.get_mut(task).map(|kept| &mut kept.goal),
            None => self.pursuit.as_mut(),
        }
    }

    /// The newest check of each try of that goal, oldest try first.
    pub fn checks_of(&self, task: Option<&TaskId>) -> Vec<&GoalChecked> {
        let Some(goal) = self.goal_of(task) else {
            return Vec::new();
        };
        goal.tries
            .iter()
            .filter_map(|a_try| {
                self.checks
                    .iter()
                    .rev()
                    .find(|checked| checked.try_number == a_try.n && checked.task.as_ref() == task)
            })
            .collect()
    }

    /// Whether that goal already changed approach, which it does once.
    pub fn approach_changed(&self, task: Option<&TaskId>) -> bool {
        match task {
            Some(task) => self
                .tasks
                .get(task)
                .is_some_and(|kept| kept.approach_changed_after.is_some()),
            None => !self.stuck_at.is_empty(),
        }
    }

    pub fn change(&self, id: &MessageId) -> Option<&GoalChange> {
        self.changes.iter().find(|change| change.id == *id)
    }

    fn round_settled(&self) -> bool {
        self.settled.is_some() || self.drafted.is_some()
    }

    fn next_round(&mut self) {
        self.round += 1;
        self.readings.clear();
        self.compared.clear();
        self.settled = None;
        self.settled_at = None;
        self.drafted = None;
    }
}

#[cfg(test)]
mod tests {
    use std::path::PathBuf;

    use argonauts_core::RunId;
    use argonauts_core::events::RunEventKind;
    use argonauts_core::events::goal::GoalSource;
    use argonauts_core::steps::understand::DiffTag;
    use time::OffsetDateTime;

    use super::*;

    fn envelope(seq: u64, event: GoalEvent) -> RunEvent {
        RunEvent {
            seq,
            ts: OffsetDateTime::UNIX_EPOCH,
            run_id: RunId::new("run-01").unwrap(),
            task_id: None,
            session: None,
            source: "argonauts-code".to_owned(),
            kind: RunEventKind::Goal(event),
        }
    }

    fn fold(events: Vec<GoalEvent>) -> GoalState {
        let mut state = GoalState::default();
        for (seq, event) in events.into_iter().enumerate() {
            let envelope = envelope(seq as u64, event.clone());
            state.apply(&event, &envelope);
        }
        state
    }

    fn read_event(label: &str) -> GoalEvent {
        GoalEvent::Read {
            label: label.to_owned(),
            worker: ModelRef::new(format!("w{label}"), None),
            reading: Reading {
                summary: format!("reading {label}"),
                success_criteria: vec!["it works".to_owned()],
                in_scope: Vec::new(),
                out_of_scope: Vec::new(),
                assumptions: Vec::new(),
                open_questions: Vec::new(),
            },
        }
    }

    fn settled() -> GoalEvent {
        GoalEvent::Settled {
            agreements: vec!["shared".to_owned()],
            assumptions: Vec::new(),
            questions: Vec::new(),
            dropped: 1,
        }
    }

    fn file(version: u32, sha: &str) -> GoalFile {
        GoalFile {
            run: RunId::new("run-01").unwrap(),
            version,
            summary: "s".to_owned(),
            criteria: Vec::new(),
            in_scope: Vec::new(),
            out_of_scope: Vec::new(),
            assumptions: Vec::new(),
            answers: Vec::new(),
            source: GoalSource::Readings,
            path: PathBuf::from(".argonauts/goal/run-01.md"),
            sha256: sha.to_owned(),
        }
    }

    #[test]
    fn a_round_folds_in_order() {
        let state = fold(vec![
            read_event("A"),
            read_event("B"),
            GoalEvent::Compared {
                worker: ModelRef::new("d", None),
                differences: vec![Difference {
                    topic: "t".to_owned(),
                    readings: Vec::new(),
                    tag: DiffTag::Wording,
                    question: None,
                }],
            },
            settled(),
            GoalEvent::Drafted(file(1, "aa")),
        ]);
        assert_eq!(state.round, 1);
        assert_eq!(state.readings.len(), 2);
        assert_eq!(state.compared.len(), 1);
        assert_eq!(state.settled_at, Some(3));
        assert_eq!(state.drafted.as_ref().unwrap().sha256, "aa");
        assert_eq!(state.approved_sha256(), None);
    }

    #[test]
    fn a_send_back_starts_the_next_round() {
        let state = fold(vec![
            read_event("A"),
            read_event("B"),
            settled(),
            GoalEvent::Drafted(file(1, "aa")),
            read_event("A"),
        ]);
        assert_eq!(state.round, 2);
        assert_eq!(state.readings.len(), 1);
        assert!(state.settled.is_none() && state.drafted.is_none());

        let empty_round = fold(vec![
            read_event("A"),
            settled(),
            GoalEvent::Drafted(file(1, "aa")),
            settled(),
        ]);
        assert_eq!(empty_round.round, 2);
        assert!(empty_round.readings.is_empty());
        assert_eq!(empty_round.settled_at, Some(3));
        assert!(empty_round.drafted.is_none());
    }

    #[test]
    fn the_approved_goal_is_frozen() {
        let state = fold(vec![
            read_event("A"),
            settled(),
            GoalEvent::Drafted(file(1, "aa")),
            GoalEvent::Approved {
                file: file(1, "aa"),
                by: Party::User,
            },
            GoalEvent::Drafted(file(1, "bb")),
            GoalEvent::Approved {
                file: file(1, "cc"),
                by: Party::Rules,
            },
            read_event("B"),
        ]);
        assert_eq!(state.approved_sha256(), Some("aa"));
        assert_eq!(state.approved_by, Some(Party::User));
        assert_eq!(state.drafted.as_ref().unwrap().sha256, "aa");
        assert_eq!(state.readings.len(), 1);

        let changed = fold(vec![
            GoalEvent::Approved {
                file: file(1, "aa"),
                by: Party::User,
            },
            GoalEvent::Approved {
                file: file(2, "dd"),
                by: Party::User,
            },
        ]);
        assert_eq!(changed.approved_sha256(), Some("dd"));
    }

    #[test]
    fn goal_changes_keep_every_resolution() {
        let id = MessageId::numbered(4);
        let state = fold(vec![
            GoalEvent::ChangeRequested {
                id: id.clone(),
                from: Party::User,
                text: "also cover the CLI".to_owned(),
                clarification: false,
            },
            GoalEvent::ChangeResolved {
                id: id.clone(),
                resolution: GoalChangeResolution::AskedYou,
                new_sha256: None,
            },
        ]);
        assert_eq!(state.change(&id).unwrap().resolved(), None);

        let mut state = state;
        let event = GoalEvent::ChangeResolved {
            id: id.clone(),
            resolution: GoalChangeResolution::Changed,
            new_sha256: Some("ee".to_owned()),
        };
        state.apply(&event, &envelope(9, event.clone()));
        let change = state.change(&id).unwrap();
        assert_eq!(change.resolved(), Some(GoalChangeResolution::Changed));
        assert_eq!(change.new_sha256.as_deref(), Some("ee"));
    }
}