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
//! An update of the deeper map, and opening it. An update prunes what deleted runs left,
//! summarizes only actions without a summary, embeds only summaries without a vector, and
//! places only the new dots, the old ones staying where they were. The dots are placed as the
//! summaries come, a share at a time, so the map fills in while it goes. Opening reads what's
//! kept and calls no model.

use std::collections::{HashMap, HashSet};
use std::io;

use tokio_util::sync::CancellationToken;

use super::action::{ActionKey, DeepAction, text_key};
use super::embed::{Embedder, PER_EMBEDDING_REQUEST, word_vectors};
use super::project::{Frame, ToPlace, project};
use super::prompt::{PER_REQUEST, PROMPT_VERSION};
use super::store::{Layout, MapMaker, Placed, Store, VectorLine};
use super::summaries::{Progress, Summarized, needed, summarize};
use super::summarize::Summarizer;
use argonauts_core::settings::behaviors::WORD_VECTORS;

/// The fewest texts summarized between two placements, and how many placements an update
/// makes at most before its last.
const PLACE_AFTER_LEAST: usize = 4 * PER_REQUEST;
const PLACEMENTS: usize = 8;

/// What turns the summaries into vectors for this update.
pub(crate) enum EmbedWith<'a> {
    Model(&'a dyn Embedder),
    /// Word vectors, made from the summaries themselves, as the settings ask.
    Words,
    /// The embedding model the settings would use doesn't answer now, for this reason.
    Missing(String),
}

/// Whether an update may go ahead as it is.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum Before {
    Go,
    /// The map was made by another summarizer or prompt. One map never mixes two, so this one
    /// would summarize every action again, which asks first.
    Redo {
        made_by: String,
        actions: usize,
    },
}

/// Whether summarizing with `summarizer` keeps the map that's there.
pub(crate) fn before(store: &Store, summarizer: &str) -> Before {
    match store.layout() {
        Some(layout)
            if !layout.points.is_empty()
                && (layout.maker.summarizer != summarizer
                    || layout.maker.prompt != PROMPT_VERSION) =>
        {
            Before::Redo {
                made_by: layout.maker.summarizer,
                actions: layout.points.len(),
            }
        }
        _ => Before::Go,
    }
}

/// What an update did.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub(crate) struct Updated {
    pub pruned: usize,
    pub summarized: Summarized,
    /// Summaries embedded now.
    pub embedded: usize,
    /// Dots on the map, and how many of them are new.
    pub placed: usize,
    pub new_places: usize,
    /// Why the embedder wasn't used: a new map was placed by word vectors instead, and a map
    /// an embedding model placed took no new dots.
    pub embedder_failed: Option<String>,
}

/// What one placement did.
#[derive(Debug, Default)]
struct Placing {
    embedded: usize,
    placed: usize,
    new_places: usize,
    embedder_failed: Option<String>,
}

/// Updates the map from `recorded`, every action recorded, summarizing those of them `wanted`
/// says. The dots summarized so far are placed after each share of the summaries, `placed`
/// hearing of it. Stopping keeps every summary it finished and the dots placed so far, and
/// embeds and places nothing more.
#[allow(clippy::too_many_arguments)]
pub(crate) async fn update(
    recorded: &[DeepAction],
    wanted: impl Fn(&DeepAction) -> bool,
    store: &Store,
    summarizer: &dyn Summarizer,
    embed_with: EmbedWith<'_>,
    stop: &CancellationToken,
    progress: &mut (dyn FnMut(Progress) + Send),
    placed: &mut (dyn FnMut() + Send),
) -> io::Result<Updated> {
    let runs: HashSet<(String, String)> = recorded
        .iter()
        .map(|action| (action.key.repo.clone(), action.key.run.clone()))
        .collect();
    let pruned = store.prune(&runs)?;
    let scope: Vec<DeepAction> = recorded.iter().filter(|a| wanted(a)).cloned().collect();
    let summarizer_id = summarizer.id().to_string();
    let to_send = needed(&scope, store, &summarizer_id).texts.len();
    let share = to_send.div_ceil(PLACEMENTS).max(PLACE_AFTER_LEAST);
    let mut summarized: Option<Summarized> = None;
    let mut done = Placing::default();
    loop {
        let part = summarize(&scope, store, summarizer, stop, progress, Some(share)).await?;
        let more = part.paused;
        let total = summarized.get_or_insert_with(|| Summarized {
            already: part.already,
            ..Summarized::default()
        });
        total.reused += part.reused;
        total.written += part.written;
        total.requests += part.requests;
        total.missing = part.missing;
        total.stopped = part.stopped;
        total.failed = part.failed;
        if total.stopped {
            break;
        }
        let placing = place(recorded, store, &summarizer_id, &embed_with, stop).await?;
        done.embedded += placing.embedded;
        done.new_places += placing.new_places;
        done.placed = placing.placed;
        done.embedder_failed = done.embedder_failed.or(placing.embedder_failed);
        if !more || total.failed.is_some() {
            break;
        }
        if placing.new_places > 0 {
            placed();
        }
    }
    if done.placed == 0 {
        done.placed = store.layout().map_or(0, |layout| layout.points.len());
    }
    Ok(Updated {
        pruned,
        summarized: summarized.unwrap_or_default(),
        embedded: done.embedded,
        placed: done.placed,
        new_places: done.new_places,
        embedder_failed: done.embedder_failed,
    })
}

/// Places every summarized action of `recorded` not on the map yet, the others staying where
/// they were, and keeps the map. A map keeps the embedder that placed it: when that one doesn't
/// answer, nothing new is placed and the summaries wait for the next update. A new map whose
/// embedding model doesn't answer is placed by word vectors, and says why.
async fn place(
    recorded: &[DeepAction],
    store: &Store,
    summarizer_id: &str,
    embed_with: &EmbedWith<'_>,
    stop: &CancellationToken,
) -> io::Result<Placing> {
    let kept = store.summaries(summarizer_id, PROMPT_VERSION);
    let on_map: Vec<(&DeepAction, &String)> = recorded
        .iter()
        .filter_map(|action| Some((action, kept.by_action.get(&action.key)?)))
        .collect();
    let texts: Vec<String> = on_map
        .iter()
        .map(|(_, summary)| (*summary).clone())
        .collect();
    let last = store
        .layout()
        .filter(|layout| layout.maker.summarizer == summarizer_id)
        .filter(|layout| layout.maker.prompt == PROMPT_VERSION);
    let as_it_was = |why: String| Placing {
        placed: last.as_ref().map_or(0, |layout| layout.points.len()),
        embedder_failed: Some(why),
        ..Placing::default()
    };
    let words = || (word_vectors(&texts), WORD_VECTORS.to_owned());
    let map_embedder = last.as_ref().map(|layout| layout.maker.embedder.as_str());
    let mut embedder_failed = None;
    let mut embedded = 0;
    let (vectors, embedder) = match (embed_with, map_embedder) {
        (_, Some(WORD_VECTORS)) | (EmbedWith::Words, None) => words(),
        (EmbedWith::Missing(why), None) => {
            embedder_failed = Some(why.clone());
            words()
        }
        (EmbedWith::Missing(why), Some(_)) => return Ok(as_it_was(why.clone())),
        (EmbedWith::Words, Some(_)) => words(),
        (EmbedWith::Model(model), map_embedder) => match model_vectors(&texts, store, *model).await
        {
            Ok((vectors, made)) => {
                embedded = made;
                (vectors, model.id())
            }
            Err(why) if map_embedder.is_some() => return Ok(as_it_was(why)),
            Err(why) => {
                embedder_failed = Some(why);
                words()
            }
        },
    };
    if stop.is_cancelled() {
        return Ok(Placing {
            embedded,
            placed: last.as_ref().map_or(0, |layout| layout.points.len()),
            ..Placing::default()
        });
    }
    let maker = MapMaker {
        summarizer: summarizer_id.to_owned(),
        prompt: PROMPT_VERSION.to_owned(),
        embedder,
    };
    let last = last.filter(|layout| layout.maker == maker);
    let old: HashMap<&ActionKey, (f32, f32)> = last
        .iter()
        .flat_map(|layout| &layout.points)
        .map(|point| (&point.action, (point.x, point.y)))
        .collect();
    let rows: Vec<Row> = on_map
        .iter()
        .zip(vectors)
        .map(|((action, _), vector)| Row {
            vector,
            group: group_of(action),
            old: old.get(&action.key).copied(),
        })
        .collect();
    let new_places = rows.iter().filter(|row| row.old.is_none()).count();
    let frame: Option<Frame> = last.as_ref().and_then(|layout| layout.frame);
    let watching = stop.clone();
    // The fit is seconds of arithmetic on a big map, kept off the threads that answer you.
    let placement = tokio::task::spawn_blocking(move || {
        let points: Vec<ToPlace<'_>> = rows
            .iter()
            .map(|row| ToPlace {
                vector: &row.vector,
                group: &row.group,
                old: row.old,
            })
            .collect();
        project(&points, frame, &|| watching.is_cancelled())
    })
    .await
    .map_err(io::Error::other)?;
    let Some(placement) = placement else {
        return Ok(Placing {
            embedded,
            placed: last.as_ref().map_or(0, |layout| layout.points.len()),
            ..Placing::default()
        });
    };
    let layout = Layout {
        maker,
        frame: Some(placement.frame),
        points: on_map
            .iter()
            .zip(placement.places)
            .map(|((action, _), (x, y))| Placed {
                action: action.key.clone(),
                x,
                y,
            })
            .collect(),
    };
    store.save_layout(&layout)?;
    Ok(Placing {
        embedded,
        placed: layout.points.len(),
        new_places,
        embedder_failed,
    })
}

/// An action to place, as the fit is handed it.
struct Row {
    vector: Vec<f32>,
    group: String,
    old: Option<(f32, f32)>,
}

/// The map compares models, so their groups are oversampled to fair weight: the CLI and the
/// model it ran.
fn group_of(action: &DeepAction) -> String {
    format!("{}/{}", action.cli, action.model.as_deref().unwrap_or(""))
}

/// A vector for each of `texts`: kept ones read back, the rest embedded and kept.
async fn model_vectors(
    texts: &[String],
    store: &Store,
    embedder: &dyn Embedder,
) -> Result<(Vec<Vec<f32>>, usize), String> {
    let id = embedder.id();
    let mut kept = store.vectors(&id);
    let mut missing: Vec<String> = Vec::new();
    let mut seen: HashSet<String> = HashSet::new();
    for text in texts {
        let key = text_key(text);
        if !kept.contains_key(&key) && seen.insert(key) {
            missing.push(text.clone());
        }
    }
    let mut embedded = 0;
    for chunk in missing.chunks(PER_EMBEDDING_REQUEST) {
        let vectors = embedder.embed(chunk).await?;
        let lines: Vec<VectorLine> = chunk
            .iter()
            .zip(vectors)
            .map(|(text, vector)| VectorLine {
                embedder: id.clone(),
                summary: text_key(text),
                vector,
            })
            .collect();
        store.add_vectors(&lines).map_err(|err| err.to_string())?;
        embedded += lines.len();
        for line in lines {
            kept.insert(line.summary, line.vector);
        }
    }
    let vectors = texts
        .iter()
        .map(|text| kept.get(&text_key(text)).cloned().unwrap_or_default())
        .collect::<Vec<_>>();
    let width = vectors.iter().map(Vec::len).max().unwrap_or(0);
    if vectors.iter().any(|vector| vector.len() != width) {
        return Err(format!("{id} gave vectors of different lengths"));
    }
    Ok((vectors, embedded))
}

/// The deeper map's side of the tab: what it opens with, and what `d` and the command run,
/// with the summarizer and embedder chosen for this map.
pub(crate) struct Deep<'a> {
    pub store: &'a Store,
    pub summarizer: &'a dyn Summarizer,
    pub embed_with: EmbedWith<'a>,
}

impl Deep<'_> {
    pub async fn update(
        self,
        recorded: &[DeepAction],
        wanted: impl Fn(&DeepAction) -> bool,
        stop: &CancellationToken,
        progress: &mut (dyn FnMut(Progress) + Send),
        placed: &mut (dyn FnMut() + Send),
    ) -> io::Result<Updated> {
        update(
            recorded,
            wanted,
            self.store,
            self.summarizer,
            self.embed_with,
            stop,
            progress,
            placed,
        )
        .await
    }
}

#[cfg(test)]
mod tests {
    use std::sync::atomic::Ordering;

    use super::super::action::sample::shell;
    use super::super::embed::fakes::Letters;
    use super::super::summarize::fakes::{Echo, Refuses};
    use super::*;

    const QWEN: &str = "ollama/qwen3-coder:30b";

    fn recorded() -> Vec<DeepAction> {
        let mut all = Vec::new();
        for (run, model, cli) in [
            ("run-1", "haiku", "claude-code"),
            ("run-2", "gpt-6-luna", "codex"),
        ] {
            for at in 0..8 {
                let mut action = shell(run, &format!("c{at}"), &format!("{model} step {at}"));
                action.model = Some(model.to_owned());
                action.cli = cli.to_owned();
                all.push(action);
            }
        }
        all
    }

    async fn update_with(
        recorded: &[DeepAction],
        store: &Store,
        summarizer: &dyn Summarizer,
        embedder: Option<&dyn Embedder>,
    ) -> Updated {
        let deep = Deep {
            store,
            summarizer,
            embed_with: embedder.map_or(EmbedWith::Words, EmbedWith::Model),
        };
        deep.update(
            recorded,
            |_| true,
            &CancellationToken::new(),
            &mut |_| {},
            &mut || {},
        )
        .await
        .unwrap()
    }

    #[tokio::test]
    async fn the_fallback_word_vectors_place_every_summarized_action() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::at(dir.path());
        let echo = Echo::new(QWEN);
        let done = update_with(&recorded(), &store, &echo, None).await;
        assert_eq!((done.placed, done.new_places, done.embedded), (16, 16, 0));
        let layout = store.layout().unwrap();
        assert_eq!(layout.maker.embedder, WORD_VECTORS);
        assert!(
            store.vectors(WORD_VECTORS).is_empty(),
            "word vectors are made, not kept"
        );
    }

    #[tokio::test]
    async fn a_second_update_embeds_and_places_only_whats_new() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::at(dir.path());
        let letters = Letters::new();
        let mut all = recorded();
        let first = update_with(&all[..12], &store, &Echo::new(QWEN), Some(&letters)).await;
        assert_eq!((first.embedded, first.placed), (12, 12));
        let before = store.layout().unwrap();
        all.truncate(16);
        let echo = Echo::new(QWEN);
        let second = update_with(&all, &store, &echo, Some(&letters)).await;
        assert_eq!(second.summarized.already, 12);
        assert_eq!(
            (second.embedded, second.new_places, second.placed),
            (4, 4, 16)
        );
        assert_eq!(letters.texts.load(Ordering::SeqCst), 16);
        let after = store.layout().unwrap();
        for old in &before.points {
            let now = after
                .points
                .iter()
                .find(|p| p.action == old.action)
                .unwrap();
            assert_eq!((now.x, now.y), (old.x, old.y), "{:?} moved", old.action);
        }
    }

    #[tokio::test]
    async fn one_summarizer_per_map_so_another_asks_before_redoing() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::at(dir.path());
        let all = recorded();
        let qwen = Echo::new(QWEN);
        assert_eq!(before(&store, QWEN), Before::Go);
        update_with(&all, &store, &qwen, None).await;
        assert_eq!(before(&store, QWEN), Before::Go);
        assert_eq!(
            before(&store, "claude-code/haiku"),
            Before::Redo {
                made_by: QWEN.to_owned(),
                actions: 16
            }
        );
        // Redone, the map is the new summarizer's alone, and the old summaries stay kept.
        let haiku = Echo::new("claude-code/haiku");
        let done = update_with(&all, &store, &haiku, None).await;
        assert_eq!((done.summarized.already, done.summarized.written), (0, 16));
        let map = store.layout().unwrap();
        assert_eq!(map.maker.summarizer, "claude-code/haiku");
        assert_eq!(map.points.len(), 16);
        assert_eq!(store.summaries(QWEN, PROMPT_VERSION).by_action.len(), 16);
    }

    #[tokio::test]
    async fn a_deleted_run_leaves_the_map() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::at(dir.path());
        let mut all = recorded();
        update_with(&all, &store, &Echo::new(QWEN), None).await;
        all.retain(|action| action.key.run == "run-2");
        assert_eq!(store.summary_lines().len(), 16);
        let refuses = Refuses(QWEN.parse().unwrap());
        let done = update_with(&all, &store, &refuses, None).await;
        assert_eq!(done.pruned, 8);
        assert_eq!(store.summary_lines().len(), 8);
        assert_eq!(store.layout().unwrap().points.len(), 8);
    }

    struct Broken;

    impl Embedder for Broken {
        fn id(&self) -> String {
            "ollama/nomic-embed-text".to_owned()
        }
        fn embed<'a>(
            &'a self,
            _texts: &'a [String],
        ) -> argonauts_core::future::BoxFuture<'a, Result<Vec<Vec<f32>>, String>> {
            Box::pin(async { Err("Ollama isn't running".to_owned()) })
        }
    }

    #[tokio::test]
    async fn a_new_maps_failing_embedder_places_it_by_word_vectors() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::at(dir.path());
        let done = update_with(&recorded(), &store, &Echo::new(QWEN), Some(&Broken)).await;
        assert_eq!(
            done.embedder_failed.as_deref(),
            Some("Ollama isn't running")
        );
        assert_eq!(store.layout().unwrap().maker.embedder, WORD_VECTORS);
        // Once placed by its words, the map keeps them, so no dot moves when a model answers.
        let letters = Letters::new();
        let more = update_with(&recorded(), &store, &Echo::new(QWEN), Some(&letters)).await;
        assert_eq!(more.new_places, 0);
        assert_eq!(store.layout().unwrap().maker.embedder, WORD_VECTORS);
    }

    #[tokio::test]
    async fn a_maps_own_embedder_failing_places_nothing_new_and_moves_nothing() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::at(dir.path());
        let letters = Letters::new();
        let all = recorded();
        update_with(&all[..12], &store, &Echo::new(QWEN), Some(&letters)).await;
        let before = store.layout().unwrap();
        let done = update_with(&all, &store, &Echo::new(QWEN), Some(&Broken)).await;
        assert_eq!(done.summarized.written, 4, "the summaries are kept");
        assert_eq!((done.new_places, done.placed), (0, 12));
        assert_eq!(
            done.embedder_failed.as_deref(),
            Some("Ollama isn't running")
        );
        assert_eq!(store.layout().unwrap(), before, "the map is as it was");
        let missing = update(
            &all,
            |_| true,
            &store,
            &Echo::new(QWEN),
            EmbedWith::Missing("no embedding model answers on this machine".to_owned()),
            &CancellationToken::new(),
            &mut |_| {},
            &mut || {},
        )
        .await
        .unwrap();
        assert_eq!(missing.new_places, 0);
        assert_eq!(store.layout().unwrap(), before);
        let back = update_with(&all, &store, &Echo::new(QWEN), Some(&letters)).await;
        assert_eq!(
            (back.new_places, back.placed),
            (4, 16),
            "once it answers again"
        );
    }

    #[tokio::test]
    async fn the_dots_are_placed_as_the_summaries_come() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::at(dir.path());
        let all: Vec<DeepAction> = (0..90)
            .map(|at| shell("run-1", &format!("c{at}"), &format!("step {at}")))
            .collect();
        let mut placements = 0;
        let mut sizes = Vec::new();
        let done = update(
            &all,
            |_| true,
            &store,
            &Echo::new(QWEN),
            EmbedWith::Words,
            &CancellationToken::new(),
            &mut |_| {},
            &mut || {
                placements += 1;
                sizes.push(store.layout().map_or(0, |layout| layout.points.len()));
            },
        )
        .await
        .unwrap();
        assert_eq!(placements, 2, "after each share but the last");
        assert_eq!(sizes, [40, 80]);
        assert_eq!((done.placed, done.new_places), (90, 90));
    }

    #[tokio::test]
    async fn a_stopped_update_places_nothing_more() {
        let dir = tempfile::tempdir().unwrap();
        let store = Store::at(dir.path());
        let stop = CancellationToken::new();
        let all = recorded();
        let done = update(
            &all,
            |_| true,
            &store,
            &Echo::new(QWEN),
            EmbedWith::Words,
            &stop,
            &mut |progress| {
                if progress.done >= 10 {
                    stop.cancel();
                }
            },
            &mut || {},
        )
        .await
        .unwrap();
        assert!(done.summarized.stopped);
        assert_eq!(done.summarized.written, 10, "what came back is kept");
        assert_eq!(done.new_places, 0);
        assert!(
            store.layout().is_none(),
            "nothing embedded or placed after the stop"
        );
    }
}