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
//! Experiments: the tie-breaker proposes; code checks each proposal against `experiments.limit_usd`
//! and `time_box_min`. A fitting one becomes experiment tasks, one per option, in sandboxed
//! worktrees, ending with an `ExperimentReport`. Experiment worktrees are never merged.

use std::path::PathBuf;

use futures::future::join_all;

use argonauts_core::TaskId;
use argonauts_core::events::RunEventKind;
use argonauts_core::events::plan::PlanEvent;
use argonauts_core::events::session::InputPartKind;
use argonauts_core::events::task::{TaskEvent, TaskKind, TaskLimit, TaskSpec, TaskState};
use argonauts_core::names::{ModelRef, RoleName, SessionKey};
use argonauts_core::session_runner::Access;
use argonauts_core::settings::limit::{LimitAmount, LimitUnit};
use argonauts_core::settings::protocol::ExperimentSettings;
use argonauts_core::steps::plan::{
    ExperimentProposal, ExperimentReport, ExperimentReportStep, ProposeExperiment,
    ProposeExperimentInput,
};
use argonauts_core::steps::{StepName, last_json_object, output_schema};

use crate::engine::host::{Ask, Fallbacks, Host, SessionPlan};
use crate::error::{EngineError, EngineResult};
use crate::plan::context::{crew_for, part, warn};
use crate::plan::map;
use crate::prompts::plan::{EXPERIMENT_REPORT, PROPOSE_EXPERIMENT};
use crate::state::RunState;
use crate::task::worktree;
use crate::tie_breaker;
use crate::workflow::StepCall;

/// Whether one proposal fits on its own: experiments on, at least one option, each option within
/// the time box, and the whole proposal within the run's experiment limit.
pub fn fits(proposal: &ExperimentProposal, settings: &ExperimentSettings) -> bool {
    fits_within(proposal, settings, 0.0)
}

fn fits_within(
    proposal: &ExperimentProposal,
    settings: &ExperimentSettings,
    spent_usd: f64,
) -> bool {
    let estimates_sane = proposal.est_minutes.is_finite()
        && proposal.est_cost_usd.is_finite()
        && proposal.est_minutes > 0.0
        && proposal.est_cost_usd >= 0.0;
    settings.enabled
        && !proposal.options.is_empty()
        && estimates_sane
        && proposal.est_minutes <= settings.time_box_min
        && spent_usd + proposal.est_cost_usd <= settings.limit_usd
}

/// Which proposals fit, in order, each counting against what the earlier fitting ones took
/// from the run's experiment limit.
pub fn within_limit(proposals: &[ExperimentProposal], settings: &ExperimentSettings) -> Vec<bool> {
    let mut spent = 0.0;
    proposals
        .iter()
        .map(|proposal| {
            let fits = fits_within(proposal, settings, spent);
            if fits {
                spent += proposal.est_cost_usd;
            }
            fits
        })
        .collect()
}

/// Dollar limits for the experiment tasks, one per option in task order. A proposal's estimate
/// covers all its options, so each option gets an even share, and no task gets more than the
/// earlier tasks left of the run's experiment limit.
fn task_limits_usd(proposals: &[ExperimentProposal], limit_usd: f64) -> Vec<f64> {
    let mut left = limit_usd.max(0.0);
    let mut limits = Vec::new();
    for proposal in proposals {
        let share = proposal.est_cost_usd / proposal.options.len().max(1) as f64;
        for _ in &proposal.options {
            let limit = share.min(left).max(0.0);
            left -= limit;
            limits.push(limit);
        }
    }
    limits
}

/// Proposes experiments once per run when topics are still disputed, runs every fitting one as
/// experiment tasks (one per option), and returns every experiment report the run has.
pub async fn run(host: &Host, call: &StepCall) -> EngineResult<Vec<ExperimentReport>> {
    let settings = host.settings().experiments.clone();
    let (proposed, disputed) = host.read(|state| {
        (
            state.plan.experiments.clone(),
            state
                .plan
                .disputed()
                .into_iter()
                .map(map::to_step_topic)
                .collect::<Vec<_>>(),
        )
    });
    if settings.enabled && !disputed.is_empty() {
        if proposed.is_empty() {
            propose(host, call, &settings, disputed).await?;
        }
        build(host, call, &settings).await?;
    }
    Ok(host.read(|state| {
        state
            .plan
            .experiment_reports
            .iter()
            .map(|result| result.report.clone())
            .collect()
    }))
}

/// The tie-breaker proposes; code checks each proposal against the limit and records it.
async fn propose(
    host: &Host,
    call: &StepCall,
    settings: &ExperimentSettings,
    disputed: Vec<argonauts_core::steps::plan::MapTopic>,
) -> EngineResult<()> {
    let tie_breaker = host
        .read(|state| tie_breaker::tie_breaker(state, host.settings()))
        .unwrap_or_else(|| host.settings().crew.main.clone());
    let input = ProposeExperimentInput {
        disputed,
        limit_usd: settings.limit_usd,
        time_box_min: settings.time_box_min,
    };
    let mut ask = Ask::new(
        Some(call.at.clone()),
        RoleName::TieBreaker,
        tie_breaker,
        PROPOSE_EXPERIMENT,
        input,
    );
    ask.access = Access::NoTools;
    ask.blind = true;
    let proposals = match host.ask::<ProposeExperiment>(ask).await {
        Ok(answered) => answered.output.proposals,
        Err(EngineError::Interrupted) => return Err(EngineError::Interrupted),
        Err(error) => {
            return warn(
                host,
                None,
                format!("No experiments: the proposal failed: {error}"),
            );
        }
    };
    for (proposal, fits_limit) in proposals.iter().zip(within_limit(&proposals, settings)) {
        host.record(
            None,
            None,
            RunEventKind::Plan(PlanEvent::ExperimentProposed {
                proposal: proposal.clone(),
                fits_limit,
            }),
        )?;
    }
    Ok(())
}

/// One experiment task per option of every fitting proposal, numbered in proposal order so a
/// re-entered step finds the tasks it already made. Worktrees are made one at a time, since
/// they share the repository's refs; the experiments then run side by side.
async fn build(host: &Host, call: &StepCall, settings: &ExperimentSettings) -> EngineResult<()> {
    let proposals: Vec<ExperimentProposal> = host.read(|state| {
        state
            .plan
            .experiments
            .iter()
            .filter(|record| record.fits_limit)
            .map(|record| record.proposal.clone())
            .collect()
    });
    let builders: Vec<ModelRef> = crew_for(host.settings(), RoleName::Experiment);
    if builders.is_empty() {
        return warn(
            host,
            None,
            "No experiment worker is available; the disputes stay open".to_owned(),
        );
    }
    let mut limits = task_limits_usd(&proposals, settings.limit_usd).into_iter();
    let mut ready = Vec::new();
    let mut n = 0u32;
    for proposal in &proposals {
        for (index, option) in proposal.options.iter().enumerate() {
            n += 1;
            let task = TaskId::new(format!("experiment-{n:03}"))
                .map_err(|error| EngineError::Refused(error.to_string()))?;
            let worker = builders[index % builders.len()].clone();
            // One option's experiment carries that option's share of the estimate, which is
            // also its task's limit.
            let experiment = ExperimentProposal {
                options: vec![option.clone()],
                est_cost_usd: limits.next().unwrap_or(0.0),
                ..proposal.clone()
            };
            match prepare(host, settings, &task, &worker, &experiment).await {
                Ok(Some(workdir)) => ready.push((task, worker, experiment, workdir)),
                Ok(None) => {}
                Err(EngineError::Interrupted) => return Err(EngineError::Interrupted),
                Err(error) => warn(
                    host,
                    Some(&task),
                    format!("{task} couldn't start; its dispute stays open: {error}"),
                )?,
            }
        }
    }
    let runs = ready
        .into_iter()
        .map(|(task, worker, proposal, workdir)| async move {
            let outcome = experiment(host, call, &task, worker, &proposal, workdir).await;
            (task, outcome)
        });
    for (task, outcome) in join_all(runs).await {
        match outcome {
            Ok(()) => {}
            Err(EngineError::Interrupted) => return Err(EngineError::Interrupted),
            Err(error) => warn(
                host,
                Some(&task),
                format!("{task} failed; its dispute stays open: {error}"),
            )?,
        }
    }
    Ok(())
}

/// Creates the experiment task and its worktree, or finds them. `None` when it already reported.
async fn prepare(
    host: &Host,
    settings: &ExperimentSettings,
    task: &TaskId,
    worker: &ModelRef,
    experiment: &ExperimentProposal,
) -> EngineResult<Option<PathBuf>> {
    let (created, finished) = host.read(|state| {
        (
            state.tasks.get(task).is_some(),
            state
                .plan
                .experiment_reports
                .iter()
                .any(|r| &r.task == task),
        )
    });
    if finished {
        return Ok(None);
    }
    let Some(option) = experiment.options.first() else {
        return Ok(None);
    };
    if !created {
        let spec = TaskSpec {
            id: task.clone(),
            title: format!("Experiment: {}: {}", experiment.topic, option.label),
            task: format!(
                "{}\n\nBuild: {}\nMeasure: {}\nDecides by: {}",
                experiment.question, option.build, experiment.measure, experiment.decides_by
            ),
            kind: TaskKind::Experiment {
                topic: experiment.topic.clone(),
                option: option.label.clone(),
            },
            owned_paths: Vec::new(),
            read_only_paths: Vec::new(),
            forbidden_paths: Vec::new(),
            hidden_paths: Vec::new(),
            depends_on: Vec::new(),
            criteria: Vec::new(),
            worker: Some(worker.clone()),
            reviewers: Vec::new(),
            limit: TaskLimit {
                limit: Some(LimitAmount {
                    unit: LimitUnit::Usd,
                    amount: experiment.est_cost_usd,
                }),
                max_minutes: Some(settings.time_box_min),
                ..TaskLimit::default()
            },
            predictions: None,
            item: Some(argonauts_core::labels::slug(&option.label, 3))
                .filter(|item| !item.is_empty()),
        };
        host.record(
            Some(task),
            None,
            RunEventKind::Task(TaskEvent::Created(Box::new(spec))),
        )?;
        host.record(
            Some(task),
            None,
            RunEventKind::Task(TaskEvent::State {
                from: None,
                to: TaskState::Running,
                reason: "experiment started".to_owned(),
            }),
        )?;
    }
    Ok(Some(worktree::ensure(host, task).await?.path))
}

/// Runs one option's experiment session, keeps its report, and closes the task unmerged.
async fn experiment(
    host: &Host,
    call: &StepCall,
    task: &TaskId,
    worker: ModelRef,
    experiment: &ExperimentProposal,
    workdir: PathBuf,
) -> EngineResult<()> {
    let plan = SessionPlan {
        at: Some(call.at.clone()),
        role: RoleName::Experiment,
        worker,
        task: Some(task.clone()),
        access: Access::Experiment,
        step: Some(StepName::ExperimentReport),
        workdir,
        parts: vec![
            part(
                InputPartKind::Prompt,
                "experiment",
                EXPERIMENT_REPORT.to_owned(),
            ),
            part(
                InputPartKind::StepInput,
                "experiment",
                serde_json::to_string_pretty(experiment)?,
            ),
        ],
        json_schema: Some(output_schema::<ExperimentReportStep>()),
        resume: None,
        slot: 0,
        label: None,
        reviews: false,
        fresh: false,
        independent_of: Vec::new(),
        in_place_of: None,
        fork_of: None,
    };
    // A CLI error has the experiment go again, or to another agent; when nobody can run it, the
    // dispute stays open, as without a report.
    let (session, end) = match host.run_session(plan, Fallbacks::Allowed).await {
        Ok(ran) => (ran.key, Some(ran.end)),
        Err(EngineError::Stalled(stall)) => {
            warn(host, Some(task), stall.line.clone())?;
            let session = host.read(|state| newest_experiment_session(state, task));
            let Some(session) = session else {
                return Ok(());
            };
            (session, None)
        }
        Err(err) => return Err(err),
    };
    worktree::commit_after_end(host, task, &session).await?;
    let report = end.and_then(|end| {
        end.structured
            .clone()
            .or_else(|| end.result_text.as_deref().and_then(last_json_object))
            .and_then(|value| serde_json::from_value::<ExperimentReport>(value).ok())
    });
    match report {
        Some(report) => {
            host.record(
                Some(task),
                Some(&session),
                RunEventKind::Plan(PlanEvent::ExperimentFinished {
                    task: task.clone(),
                    report,
                }),
            )?;
        }
        None => warn(
            host,
            Some(task),
            format!("{task} gave no experiment report; the dispute stays open"),
        )?,
    }
    // Experiment worktrees are never merged; only the report is kept.
    host.record(
        Some(task),
        Some(&session),
        RunEventKind::Task(TaskEvent::State {
            from: Some(TaskState::Running),
            to: TaskState::Cancelled,
            reason: "experiment done; experiments are never merged into the run".to_owned(),
        }),
    )?;
    Ok(())
}

/// The experiment task's newest session.
fn newest_experiment_session(state: &RunState, task: &TaskId) -> Option<SessionKey> {
    state
        .sessions
        .by_key
        .values()
        .filter(|session| session.task.as_ref() == Some(task))
        .max_by_key(|session| session.since)
        .map(|session| session.key.clone())
}

#[cfg(test)]
mod tests {
    use argonauts_core::steps::plan::ExperimentOption;

    use super::*;

    fn proposal(minutes: f64, cost: f64) -> ExperimentProposal {
        ExperimentProposal {
            topic: "counters".to_owned(),
            question: "Redis or memory?".to_owned(),
            options: vec![
                ExperimentOption {
                    label: "Redis".to_owned(),
                    build: "a counter in Redis".to_owned(),
                },
                ExperimentOption {
                    label: "memory".to_owned(),
                    build: "a counter in a map".to_owned(),
                },
            ],
            measure: "p50 latency".to_owned(),
            decides_by: "lower p50 wins".to_owned(),
            est_minutes: minutes,
            est_cost_usd: cost,
        }
    }

    #[test]
    fn a_proposal_fits_the_time_box_and_limit() {
        let settings = ExperimentSettings::default();
        assert!(fits(&proposal(10.0, 1.0), &settings));
        assert!(fits(&proposal(15.0, 2.0), &settings));
        assert!(!fits(&proposal(16.0, 1.0), &settings));
        assert!(!fits(&proposal(10.0, 2.5), &settings));
    }

    #[test]
    fn nonsense_estimates_or_no_options_never_fit() {
        let settings = ExperimentSettings::default();
        assert!(!fits(&proposal(f64::NAN, 1.0), &settings));
        assert!(!fits(&proposal(10.0, -1.0), &settings));
        assert!(!fits(&proposal(0.0, 1.0), &settings));
        let mut empty = proposal(10.0, 1.0);
        empty.options.clear();
        assert!(!fits(&empty, &settings));
    }

    #[test]
    fn experiments_off_means_nothing_fits() {
        let settings = ExperimentSettings {
            enabled: false,
            ..ExperimentSettings::default()
        };
        assert!(!fits(&proposal(1.0, 0.1), &settings));
    }

    #[test]
    fn experiment_task_limits_add_up_to_no_more_than_the_limit() {
        let settings = ExperimentSettings {
            limit_usd: 2.5,
            ..ExperimentSettings::default()
        };
        let mut three = proposal(10.0, 2.0);
        three.options.push(ExperimentOption {
            label: "sqlite".to_owned(),
            build: "a counter in sqlite".to_owned(),
        });
        let proposals = [three, proposal(10.0, 1.5), proposal(5.0, 0.5)];
        let fitting: Vec<ExperimentProposal> = proposals
            .iter()
            .zip(within_limit(&proposals, &settings))
            .filter(|(_, fits)| *fits)
            .map(|(proposal, _)| proposal.clone())
            .collect();
        let limits = task_limits_usd(&fitting, settings.limit_usd);
        // Three options sharing $2, then two sharing $0.50.
        let expected = [2.0 / 3.0, 2.0 / 3.0, 2.0 / 3.0, 0.25, 0.25];
        assert_eq!(limits.len(), expected.len());
        for (limit, want) in limits.iter().zip(expected) {
            assert!((limit - want).abs() < 1e-9, "{limits:?}");
        }
        assert!(limits.iter().sum::<f64>() <= settings.limit_usd + 1e-9);

        // A smaller limit than the proposals were checked against still caps the tasks.
        let limits = task_limits_usd(&fitting, 1.0);
        assert!(limits.iter().sum::<f64>() <= 1.0 + 1e-9, "{limits:?}");
        assert_eq!(limits.last(), Some(&0.0));
    }

    #[test]
    fn the_limit_is_shared_by_every_experiment_in_the_run() {
        let settings = ExperimentSettings::default();
        let proposals = [proposal(10.0, 1.5), proposal(10.0, 1.0), proposal(5.0, 0.5)];
        assert_eq!(within_limit(&proposals, &settings), vec![true, false, true]);
    }
}