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
//! Your edits at the plan approval. Lines of the note in these forms change the graph, and
//! the whole note becomes a decision record:
//!
//! ```text
//! task-002 worker codex/gpt-5.6
//! task-002 reviewers agy, claude-code/opus
//! task-002 paths src/store/**, src/cache.rs
//! task-002 limit 4.5
//! limit tasks 0.45
//! ```
//!
//! An edited graph goes through the same rules as a proposal, so an edit that breaks one
//! reopens the approval with the reason.

use argonauts_core::TaskId;
use argonauts_core::events::plan::{DesignDecision, LimitPlan, TaskGraph};
use argonauts_core::events::task::{TaskKind, TaskSpec};
use argonauts_core::names::{DecisionId, ModelRef, RoleName};
use argonauts_core::settings::Settings;
use argonauts_core::settings::limit::{LimitAmount, LimitLineKind, LimitUnit};

use super::{graph, limit_lines};
use crate::roles;

/// Its title gives the record's file name, `D-NNN-your-edits.md`.
pub const DECISION_TITLE: &str = "Your edits";

#[derive(Debug, Clone, PartialEq)]
pub enum Edit {
    Worker {
        task: TaskId,
        worker: ModelRef,
    },
    Reviewers {
        task: TaskId,
        workers: Vec<ModelRef>,
    },
    Paths {
        task: TaskId,
        paths: Vec<String>,
    },
    TaskLimit {
        task: TaskId,
        amount: f64,
    },
    Line {
        line: LimitLineKind,
        share: f64,
    },
}

/// The graph and limit after your edits, and what changed.
#[derive(Debug, Clone, PartialEq)]
pub struct Edited {
    pub graph: TaskGraph,
    pub limit: LimitPlan,
    pub workers: Vec<TaskId>,
    pub reviewers: Vec<TaskId>,
    pub limit_changed: bool,
}

/// The edits in the note. A line that starts like an edit but doesn't parse is an error, so a
/// typo never passes as prose.
pub fn parse(note: &str) -> Result<Vec<Edit>, String> {
    let mut edits = Vec::new();
    for line in note.lines().map(str::trim) {
        let mut words = line.splitn(3, char::is_whitespace);
        let (Some(first), Some(second)) = (words.next(), words.next()) else {
            continue;
        };
        let rest = words.next().unwrap_or("").trim();
        let keyword = second.trim_end_matches(':').to_ascii_lowercase();
        if first.eq_ignore_ascii_case("limit") {
            let Some(line_kind) = limit_lines::line_named(second.trim_end_matches(':')) else {
                continue;
            };
            let share = number(rest, line)?;
            edits.push(Edit::Line {
                line: line_kind,
                share,
            });
            continue;
        }
        if !looks_like_task(first)
            || !matches!(keyword.as_str(), "worker" | "reviewers" | "paths" | "limit")
        {
            continue;
        }
        let task = TaskId::new(first).map_err(|_| format!("`{first}` isn't a task id."))?;
        let edit = match keyword.as_str() {
            "worker" => Edit::Worker {
                task,
                worker: worker(rest, line)?,
            },
            "reviewers" => Edit::Reviewers {
                task,
                workers: list(rest)
                    .iter()
                    .map(|item| worker(item, line))
                    .collect::<Result<_, _>>()?,
            },
            "paths" => Edit::Paths {
                task,
                paths: list(rest),
            },
            _ => Edit::TaskLimit {
                task,
                amount: number(rest, line)?,
            },
        };
        edits.push(edit);
    }
    Ok(edits)
}

/// Applies the edits and checks the result like a proposal. `Err` is the reason to reopen the
/// approval with.
pub fn apply(
    graph: &TaskGraph,
    limit: &LimitPlan,
    edits: &[Edit],
    settings: &Settings,
    provider: &dyn Fn(&ModelRef) -> String,
) -> Result<Edited, String> {
    let mut tasks = graph.tasks.clone();
    let mut limit = limit.clone();
    let (mut workers, mut reviewers, mut limit_changed) = (Vec::new(), Vec::new(), false);
    let worker_pool = roles::candidates(settings, RoleName::Worker);
    let reviewer_pool = roles::candidates(settings, RoleName::Reviewer);
    for edit in edits {
        match edit {
            Edit::Worker {
                task,
                worker: model,
            } => {
                if !worker_pool.contains(model) {
                    return Err(format!("{model} isn't one of the worker candidates."));
                }
                find(&mut tasks, task)?.worker = Some(model.clone());
                workers.push(task.clone());
            }
            Edit::Reviewers {
                task,
                workers: models,
            } => {
                if let Some(model) = models.iter().find(|w| !reviewer_pool.contains(w)) {
                    return Err(format!("{model} isn't one of the reviewer candidates."));
                }
                find(&mut tasks, task)?.reviewers.clone_from(models);
                reviewers.push(task.clone());
            }
            Edit::Paths { task, paths } => {
                find(&mut tasks, task)?.owned_paths.clone_from(paths);
            }
            Edit::TaskLimit { task, amount } => {
                if !amount.is_finite() || *amount <= 0.0 {
                    return Err(format!("{task}'s limit must be more than 0."));
                }
                let spec = find(&mut tasks, task)?;
                let unit = spec.limit.limit.map_or(LimitUnit::Usd, |limit| limit.unit);
                spec.limit.limit = Some(LimitAmount {
                    unit,
                    amount: *amount,
                });
            }
            Edit::Line { line, share } => {
                if !(0.0..=1.0).contains(share) {
                    return Err(format!(
                        "The {} limit line's share must be between 0 and 1.",
                        limit_lines::line_name(*line)
                    ));
                }
                let total = limit.total.amount;
                let entry = limit
                    .lines
                    .iter_mut()
                    .find(|entry| entry.line == *line)
                    .ok_or_else(|| {
                        format!("The plan has no {} line.", limit_lines::line_name(*line))
                    })?;
                entry.amount.amount = total * share;
                limit_changed = true;
            }
        }
    }
    follow_reviewers(&mut tasks, &reviewers, &workers);
    let mut broken = graph::rules(&tasks, settings);
    broken.extend(conflicts(&tasks, settings, provider));
    if limit_changed && let Err(reason) = limit_lines::check(&limit) {
        broken.push(reason);
    }
    if !broken.is_empty() {
        return Err(broken.join(" "));
    }
    Ok(Edited {
        graph: TaskGraph {
            critical_path: graph.critical_path.clone(),
            tasks,
        },
        limit,
        workers,
        reviewers,
        limit_changed,
    })
}

/// The decision record for an approval with edits.
pub fn decision(id: DecisionId, note: &str) -> DesignDecision {
    DesignDecision {
        id,
        title: DECISION_TITLE.to_owned(),
        choice: note.trim().to_owned(),
        rationale: "You approved the plan with these edits at the plan approval.".to_owned(),
        alternatives: Vec::new(),
        path: None,
    }
}

/// A test task is written by its task's first reviewer, so it follows a reviewer edit unless
/// you set its worker yourself.
fn follow_reviewers(tasks: &mut [TaskSpec], reviewed: &[TaskId], owned: &[TaskId]) {
    let firsts: Vec<(TaskId, ModelRef)> = tasks
        .iter()
        .filter(|task| reviewed.contains(&task.id))
        .filter_map(|task| Some((task.id.clone(), task.reviewers.first()?.clone())))
        .collect();
    for task in tasks.iter_mut() {
        if let TaskKind::AcceptanceTests { for_task } = &task.kind
            && !owned.contains(&task.id)
            && let Some((_, first)) = firsts.iter().find(|(id, _)| id == for_task)
        {
            task.worker = Some(first.clone());
        }
    }
}

fn conflicts(
    tasks: &[TaskSpec],
    settings: &Settings,
    provider: &dyn Fn(&ModelRef) -> String,
) -> Vec<String> {
    let mut broken = Vec::new();
    for task in tasks {
        let Some(worker) = &task.worker else { continue };
        for reviewer in &task.reviewers {
            if let Some(reason) =
                roles::conflict(RoleName::Reviewer, reviewer, Some(worker), provider)
            {
                broken.push(format!("{reason} ({}).", task.id));
            } else if settings.review.other_provider && provider(reviewer) == provider(worker) {
                broken.push(format!(
                    "{reviewer} is in the same provider as {worker}, {}'s worker.",
                    task.id
                ));
            }
        }
        if let TaskKind::AcceptanceTests { for_task } = &task.kind
            && let Some(tested) = tasks.iter().find(|c| &c.id == for_task)
            && let Some(tested_worker) = &tested.worker
            && roles::conflict(RoleName::Reviewer, worker, Some(tested_worker), provider).is_some()
        {
            broken.push(format!(
                "{} can't write the acceptance tests for its own task {for_task}.",
                roles::display_name(worker)
            ));
        }
    }
    broken
}

fn find<'a>(tasks: &'a mut [TaskSpec], id: &TaskId) -> Result<&'a mut TaskSpec, String> {
    tasks
        .iter_mut()
        .find(|task| &task.id == id)
        .ok_or_else(|| format!("There's no {id} in the plan."))
}

fn looks_like_task(word: &str) -> bool {
    word.split_once('-').is_some_and(|(prefix, number)| {
        !prefix.is_empty()
            && prefix.chars().all(|c| c.is_ascii_lowercase())
            && !number.is_empty()
            && number.chars().all(|c| c.is_ascii_digit())
    })
}

fn list(text: &str) -> Vec<String> {
    text.split(',')
        .map(|item| item.trim().to_owned())
        .filter(|item| !item.is_empty())
        .collect()
}

fn worker(text: &str, line: &str) -> Result<ModelRef, String> {
    text.trim()
        .parse()
        .map_err(|_| format!("\"{line}\" doesn't name an agent."))
}

fn number(text: &str, line: &str) -> Result<f64, String> {
    text.trim()
        .trim_start_matches('$')
        .parse()
        .map_err(|_| format!("\"{line}\" doesn't end in a number."))
}

#[cfg(test)]
mod tests {
    use argonauts_core::events::plan::LimitLine;
    use argonauts_core::events::task::TaskLimit;

    use super::*;

    fn worker(text: &str) -> ModelRef {
        text.parse().unwrap()
    }

    fn provider(worker: &ModelRef) -> String {
        worker.agent.clone()
    }

    fn spec(id: &str, kind: TaskKind, owned: &str, who: &str, reviewer: &str) -> TaskSpec {
        TaskSpec {
            id: TaskId::new(id).unwrap(),
            title: id.to_owned(),
            task: id.to_owned(),
            kind,
            owned_paths: vec![owned.to_owned()],
            read_only_paths: Vec::new(),
            forbidden_paths: Vec::new(),
            hidden_paths: Vec::new(),
            depends_on: Vec::new(),
            criteria: Vec::new(),
            worker: Some(worker(who)),
            reviewers: vec![worker(reviewer)],
            limit: TaskLimit::default(),
            predictions: None,
            item: None,
        }
    }

    fn fixture() -> (TaskGraph, LimitPlan, Settings) {
        let settings = Settings {
            crew: vec![worker("claude-code"), worker("codex"), worker("agy")].into(),
            ..Settings::default()
        };
        let tests = TaskKind::AcceptanceTests {
            for_task: TaskId::numbered(1),
        };
        let graph = TaskGraph {
            tasks: vec![
                spec(
                    "test-002",
                    tests,
                    "tests/acceptance/task-001/**",
                    "codex",
                    "agy",
                ),
                spec(
                    "task-001",
                    TaskKind::Build,
                    "src/store/**",
                    "claude-code",
                    "codex",
                ),
            ],
            critical_path: Vec::new(),
        };
        let limit = LimitPlan {
            lines: [(LimitLineKind::Tasks, 6.0), (LimitLineKind::Reserve, 4.0)]
                .map(|(line, amount)| LimitLine {
                    line,
                    amount: LimitAmount {
                        unit: LimitUnit::Usd,
                        amount,
                    },
                })
                .to_vec(),
            total: LimitAmount {
                unit: LimitUnit::Usd,
                amount: 10.0,
            },
            estimate_low: 5.0,
            estimate_high: 9.0,
        };
        (graph, limit, settings)
    }

    #[test]
    fn edits_parse_from_the_note() {
        let note = "Keep the store small.\ntask-001 worker: codex\ntask-001 paths src/store/**, src/cache.rs\nlimit tasks 0.5\ntest-002 limit $2\nThe limit matters.";
        let edits = parse(note).unwrap();
        assert_eq!(
            edits,
            [
                Edit::Worker {
                    task: TaskId::numbered(1),
                    worker: worker("codex")
                },
                Edit::Paths {
                    task: TaskId::numbered(1),
                    paths: vec!["src/store/**".to_owned(), "src/cache.rs".to_owned()]
                },
                Edit::Line {
                    line: LimitLineKind::Tasks,
                    share: 0.5
                },
                Edit::TaskLimit {
                    task: TaskId::new("test-002").unwrap(),
                    amount: 2.0
                },
            ]
        );
        assert!(parse("task-001 limit lots").is_err());
    }

    #[test]
    fn edits_are_checked_like_a_proposal() {
        let (graph, limit, settings) = fixture();
        let try_edit =
            |note: &str| apply(&graph, &limit, &parse(note).unwrap(), &settings, &provider);

        // The test task follows its task's new reviewer, and then needs another reviewer.
        let reason = try_edit("task-001 reviewers agy").unwrap_err();
        assert!(
            reason.contains("agy can't review its own task (test-002)"),
            "{reason}"
        );
        let edited =
            try_edit("task-001 reviewers agy\ntest-002 reviewers codex\ntask-001 limit 4").unwrap();
        let tests = &edited.graph.tasks[0];
        assert_eq!(
            tests.worker,
            Some(worker("agy")),
            "the test task follows the reviewer"
        );
        assert_eq!(
            edited.reviewers,
            [TaskId::numbered(1), TaskId::new("test-002").unwrap()]
        );
        assert_eq!(edited.graph.tasks[1].limit.limit.unwrap().amount, 4.0);
        assert!(!edited.limit_changed);

        let reason = try_edit("task-001 worker codex").unwrap_err();
        assert!(
            reason.contains("Codex can't review its own task"),
            "{reason}"
        );
        assert!(
            reason.contains("acceptance tests for its own task task-001"),
            "{reason}"
        );
        assert_eq!(
            try_edit("task-001 worker agy").unwrap().workers,
            [TaskId::numbered(1)]
        );
        let reason = try_edit("task-001 paths tests/acceptance/**").unwrap_err();
        assert!(reason.contains("overlap"), "{reason}");
        let reason = try_edit("limit tasks 0.7").unwrap_err();
        assert_eq!(
            reason,
            "The limit lines add up to $11.00, not the run limit of $10.00."
        );
        assert!(
            try_edit("limit tasks 0.7\nlimit reserve 0.3")
                .unwrap()
                .limit_changed
        );
        assert!(
            try_edit("task-009 worker agy")
                .unwrap_err()
                .contains("no task-009")
        );
        assert!(
            try_edit("task-001 worker ollama")
                .unwrap_err()
                .contains("worker candidates")
        );
        assert!(try_edit("Just be careful.").is_ok());
    }

    #[test]
    fn plan_edits_become_a_decision_titled_your_edits() {
        let record = decision(DecisionId::numbered(4), "  task-001 worker agy\n");
        assert_eq!(record.title, "Your edits");
        assert_eq!(record.choice, "task-001 worker agy");
        assert_eq!(record.id.as_str(), "D-004");
    }
}