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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
//! A question from a report: answered from an earlier record when it's settled, else sent to
//! the worker of the piece it concerns, the consultant, or you. On autopilot nobody is asked: a
//! question that stops the work gets the main agent's answer, and one in a report that finished
//! stands as what its agent decided. Either way one line says so, and the task carries on.

use argonauts_core::events::RunEventKind;
use argonauts_core::events::approval::{
    ApprovalKind, ApprovalPacket, FreeText, MergeQuestion, QuestionPacket,
};
use argonauts_core::events::run::Party;
use argonauts_core::events::shared::{
    AnsweredForYou, BlackboardEvent, BlackboardKind, BlackboardMessage, Recipient,
};
use argonauts_core::events::task::TaskState;
use argonauts_core::names::{MessageId, ModelRef, RoleName, SessionKey};
use argonauts_core::policy::EvidenceFacts;
use argonauts_core::{Note, NoteLevel, TaskId};

use crate::approvals::autopilot::{self, OWN_ANSWER, OWN_CALL};
use crate::approvals::{self, ApprovalRequest};
use crate::blackboard::requests::move_if;
use crate::blackboard::{Post, delivery, post_once, records, refs, reply_to, route};
use crate::consultant;
use crate::engine::host::Host;
use crate::error::{EngineError, EngineResult};
use crate::roles;
use crate::state::RunState;
use crate::status::folder::words;
use crate::task::machine::Trigger;
use crate::understand::materiality::one_line;

/// Posts the question and sends it where it belongs. `blocks` says the asking report stopped for
/// the answer; a report that finished only mentions its question.
pub async fn ask(
    host: &Host,
    task: &TaskId,
    from: &Party,
    text: &str,
    blocks: bool,
) -> EngineResult<()> {
    let cited = refs::refs_in(text);
    let to = match host.read(|state| route::question(state, text, &cited)) {
        // A worker asking about its own piece asks you.
        Recipient::Task { task: asked } if &asked == task => Recipient::User,
        other => other,
    };
    let id = post_once(
        host,
        Post {
            kind: BlackboardKind::Question,
            from: from.clone(),
            to: to.clone(),
            text: text.to_owned(),
            in_reply_to: None,
            refs: cited,
            from_task: Some(task.clone()),
        },
    )?;
    if host.read(|state| state.blackboard.settled(&id).is_some()) {
        return Ok(());
    }
    if let Some(record) = settled_earlier(host, &id) {
        return answer_with_record(host, &id, &record);
    }
    match to {
        Recipient::Role {
            role: RoleName::Consultant,
        } => {
            consultant::answer(host, &id).await?;
        }
        // Nothing waits for you on autopilot: the main agent answers what stops the work, and
        // the rest stands as its agent's own call.
        Recipient::User if autopilot::applies_now(host, ApprovalKind::Question) => {
            if blocks {
                answer_for_you(host, task, &id).await?;
            } else {
                own_call(host, task, &id)?;
            }
        }
        // A report that finished asks in passing: its task goes on to its reviews, and the
        // question stays open for the merge question to show.
        Recipient::User if !blocks => carried(host, task, &id)?,
        Recipient::User => ask_you(host, task, &id).await?,
        _ => {
            delivery::to_running(host, &id)?;
        }
    }
    Ok(())
}

/// A question in a report that finished, outside autopilot: nothing waits on it. One line says
/// the task goes on, and the question stays open on the board, where the merge question finds it.
fn carried(host: &Host, task: &TaskId, id: &MessageId) -> EngineResult<()> {
    let (question, _) = asked(host, task, id)?;
    let line = format!(
        "{} asked, and goes on to its reviews: {} · the merge question shows it",
        task_label(host, task),
        one_line(&question.text)
    );
    note(host, task, NoteLevel::Info, line)
}

/// Whether the task's report left a question for you that nothing waits on, so a report that
/// needs a decision goes on to its reviews with it.
pub fn carried_for_the_merge(state: &RunState, task: &TaskId) -> bool {
    let board = &state.blackboard;
    board.messages.iter().any(|message| {
        message.kind == BlackboardKind::Question
            && message.to == Recipient::User
            && board.posted_from.get(&message.id) == Some(task)
            && board.settled(&message.id).is_none()
    }) && state
        .approvals
        .open_for(ApprovalKind::Question, Some(task))
        .is_none()
}

/// What a task's agent asked you that the merge question shows: each question still open, and
/// each one answered in your place with its answer. One you answered yourself, or left with
/// Esc, you have already seen.
pub fn for_the_merge(state: &RunState, task: &TaskId) -> Vec<MergeQuestion> {
    let board = &state.blackboard;
    board
        .messages
        .iter()
        .filter(|message| {
            message.kind == BlackboardKind::Question
                && message.to == Recipient::User
                && board.posted_from.get(&message.id) == Some(task)
        })
        .filter_map(|question| {
            let open = MergeQuestion {
                text: question.text.clone(),
                answer: None,
                answered_by: None,
            };
            let Some(answer) = board.settled(&question.id) else {
                return Some(open);
            };
            let (text, by) = match &answer.from {
                Party::User | Party::Declined => return None,
                Party::Agent { worker, .. } => (answer.text.clone(), Some(worker.clone())),
                Party::Judge { model } => (answer.text.clone(), Some(model.clone())),
                _ if answer.text == OWN_CALL => ("its agent's own call".to_owned(), None),
                _ if answer.text == OWN_ANSWER => ("its agent's best judgment".to_owned(), None),
                _ => (answer.text.clone(), None),
            };
            Some(MergeQuestion {
                answer: Some(text),
                answered_by: by,
                ..open
            })
        })
        .collect()
}

/// The earlier record that settles this message, unless it cites evidence the record lacked.
pub fn settled_earlier(host: &Host, message: &MessageId) -> Option<MessageId> {
    host.read(|state| {
        let asked = state.blackboard.get(message)?;
        let record = records::settled_by(state, asked)?;
        let settled = state.blackboard.get(&record)?;
        (!records::reopens(settled, asked)).then_some(record)
    })
}

/// Answers a question, or decides a change request, with the id of the record that already
/// settled it.
pub fn answer_with_record(
    host: &Host,
    message: &MessageId,
    record: &MessageId,
) -> EngineResult<()> {
    let reply = host.read(|state| {
        let asked = state.blackboard.get(message)?;
        let settled = state.blackboard.get(record)?;
        let kind = match asked.kind {
            BlackboardKind::ChangeRequest => BlackboardKind::Decision,
            _ => BlackboardKind::Answer,
        };
        let mut cited = settled.refs.clone();
        cited.push(record.to_string());
        Some(Post {
            kind,
            from: Party::ArgonautsCode,
            to: reply_to(state, asked),
            text: format!("Already settled by {record}: {}", settled.text),
            in_reply_to: Some(message.clone()),
            refs: cited,
            from_task: None,
        })
    });
    let reply = reply.ok_or_else(|| {
        EngineError::Refused(format!(
            "{message} or its record {record} isn't on the board"
        ))
    })?;
    let answer = post_once(host, reply)?;
    delivery::to_running(host, &answer)?;
    resume_asker(host, message)?;
    Ok(())
}

/// A task blocked on its questions goes back to work once the last of them is answered, so its
/// session takes the answers at its next turn. An answer that comes while its report is still
/// being read finds it running, and the report's end sees the answer instead.
pub fn resume_asker(host: &Host, question: &MessageId) -> EngineResult<()> {
    let task = host.read(|state| {
        let task = state.blackboard.posted_from.get(question)?;
        (!waits_on_the_board(state, task)).then(|| task.clone())
    });
    if let Some(task) = task {
        move_if(
            host,
            &task,
            TaskState::Blocked,
            Trigger::Answered,
            &format!("{question} was answered"),
        )?;
    }
    Ok(())
}

/// Whether a question the task asked is still open, or a change request of another task still
/// holds it.
pub fn waits_on_the_board(state: &RunState, task: &TaskId) -> bool {
    let board = &state.blackboard;
    let asked_and_open = board.messages.iter().any(|message| {
        message.kind == BlackboardKind::Question
            && board.posted_from.get(&message.id) == Some(task)
            && board.settled(&message.id).is_none()
    });
    asked_and_open || crate::blackboard::requests::paused(state, task)
}

/// The question and who asked it: the agent, else the task's worker, else the main agent.
fn asked(
    host: &Host,
    task: &TaskId,
    id: &MessageId,
) -> EngineResult<(BlackboardMessage, ModelRef)> {
    let (question, asker) = host
        .read(|state| {
            let question = state.blackboard.get(id)?.clone();
            let asker = match &question.from {
                Party::Agent { worker, .. } => Some(worker.clone()),
                _ => state.tasks.get(task).and_then(|record| {
                    record.worker.clone().or_else(|| record.spec.worker.clone())
                }),
            };
            Some((question, asker))
        })
        .ok_or_else(|| EngineError::Refused(format!("There's no question {id}")))?;
    Ok((
        question,
        asker.unwrap_or_else(|| host.settings().crew.main.clone()),
    ))
}

/// A `Question` approval. The task waits in `Blocked` until you answer, then the answer goes
/// to it for its next turn.
async fn ask_you(host: &Host, task: &TaskId, id: &MessageId) -> EngineResult<()> {
    let (question, asker) = asked(host, task, id)?;
    let blocked = move_if(
        host,
        task,
        TaskState::Running,
        Trigger::Question,
        &format!("waits for your answer to {id}"),
    )?;
    let from = task_label(host, task);
    let request = question_approval(task, &from, &question.text, asker);
    let decided = approvals::open_and_wait(host, request).await?;
    pass_on(host, task, &question, &decided, blocked)
}

/// Your answer to a question, posted to the board, the task waiting on it moved on, and the
/// answer delivered for its next turn.
fn pass_on(
    host: &Host,
    task: &TaskId,
    question: &BlackboardMessage,
    decided: &argonauts_core::events::approval::ApprovalDecided,
    blocked: bool,
) -> EngineResult<()> {
    let id = &question.id;
    let text = decided
        .text
        .clone()
        .filter(|text| !text.trim().is_empty())
        .unwrap_or_else(|| words(&decided.action));
    let reply = host.read(|state| Post {
        kind: BlackboardKind::Answer,
        from: decided.by.clone(),
        to: reply_to(state, question),
        refs: records::record_refs(question, &text),
        text,
        in_reply_to: Some(id.clone()),
        from_task: None,
    });
    let answer = post_once(host, reply)?;
    if blocked {
        move_if(
            host,
            task,
            TaskState::Blocked,
            Trigger::Answered,
            &format!("you answered {id}"),
        )?;
    }
    delivery::to_running(host, &answer)?;
    Ok(())
}

/// A task still blocked on a question you've answered. A run that stops to wait for you ends
/// the wait that would have passed the answer on, and the answer comes in as it resumes, so
/// the task takes it here, when its step goes on. Whether there was one.
pub fn answered_meanwhile(host: &Host, task: &TaskId) -> EngineResult<bool> {
    let found = host.read(|state| {
        if state.tasks.get(task)?.state != TaskState::Blocked {
            return None;
        }
        let record = state
            .approvals
            .last_decided(ApprovalKind::Question, Some(task))?;
        let ApprovalPacket::Question(packet) = &record.opened.packet else {
            return None;
        };
        let question = state.blackboard.messages.iter().rev().find(|message| {
            message.kind == BlackboardKind::Question
                && message.text == packet.text
                && state.blackboard.settled(&message.id).is_none()
        })?;
        Some((question.clone(), record.decided.clone()?))
    });
    let Some((question, decided)) = found else {
        return Ok(false);
    };
    pass_on(host, task, &question, &decided, true)?;
    Ok(true)
}

/// A task still blocked on a question nobody answered, as after something other than the
/// answer relaunched the run that waited on it: the task waits on the same question again, and
/// on autopilot the main agent answers it at once. Whether there was one.
pub async fn wait_again(host: &Host, task: &TaskId) -> EngineResult<bool> {
    let open = host.read(|state| {
        if state.tasks.get(task)?.state != TaskState::Blocked {
            return None;
        }
        let record = state
            .approvals
            .open_for(ApprovalKind::Question, Some(task))?;
        let ApprovalPacket::Question(packet) = &record.opened.packet else {
            return None;
        };
        let question = state.blackboard.messages.iter().rev().find(|message| {
            message.kind == BlackboardKind::Question
                && message.text == packet.text
                && state.blackboard.posted_from.get(&message.id) == Some(task)
                && state.blackboard.settled(&message.id).is_none()
        })?;
        Some((record.opened.id.clone(), question.clone()))
    });
    let Some((approval, question)) = open else {
        return Ok(false);
    };
    let decided = approvals::wait_again(host, &approval).await?;
    pass_on(host, task, &question, &decided, true)?;
    Ok(true)
}

/// Autopilot, a question that stops the work: the model `approvals.answers` picked answers it,
/// unless it's of the asker's own provider, else the main agent, or a model of another provider
/// than the asker's when the main agent is of the asker's own. The answer goes straight to the
/// agent, which carries on. One line says who answered and what, and the run's status keeps it
/// for you to review.
async fn answer_for_you(host: &Host, task: &TaskId, id: &MessageId) -> EngineResult<()> {
    let (question, asker) = asked(host, task, id)?;
    let from = task_label(host, task);
    let Some((answerer, session, answer)) =
        proposed_answer(host, task, &question, &asker, &from).await?
    else {
        // Nothing could answer, so the agent goes on with its own judgment.
        return reply(host, id, &question, OWN_ANSWER.to_owned(), Party::Autopilot);
    };
    let answered = AnsweredForYou {
        question: id.clone(),
        asker: from.clone(),
        answer: one_line(&answer),
        by: Some(answerer.clone()),
        role: answerer_role(host, &answerer),
        why: None,
        finished: false,
    };
    host.record(
        Some(task),
        None,
        RunEventKind::Blackboard(BlackboardEvent::AnsweredForYou(answered)),
    )?;
    let by = Party::Agent {
        session,
        worker: answerer,
    };
    reply(host, id, &question, answer, by)
}

/// Autopilot, a question in a report that finished its work: its agent already decided, so the
/// question stands as its own call. One line says so, and the closing answer lists it.
fn own_call(host: &Host, task: &TaskId, id: &MessageId) -> EngineResult<()> {
    let (question, asker) = asked(host, task, id)?;
    let line = format!(
        "Decided on autopilot · {} ({}) kept its own call on: {}",
        task_label(host, task),
        roles::display_name(&asker),
        one_line(&question.text)
    );
    note(host, task, NoteLevel::Info, line)?;
    reply(host, id, &question, OWN_CALL.to_owned(), Party::Autopilot)
}

/// The answer a model gives in your place, with who gave it and in which session: the model
/// [`answerer_for`] picks, else the tie-breaker, else the main agent though it shares the
/// asker's provider. Only a session that finished with text answers. Each one that couldn't
/// gets one line, and `None` means none could, so the agent goes with its own judgment.
async fn proposed_answer(
    host: &Host,
    task: &TaskId,
    question: &BlackboardMessage,
    asker: &ModelRef,
    from: &str,
) -> EngineResult<Option<(ModelRef, SessionKey, String)>> {
    let mut in_line = vec![answerer_for(host, asker)];
    for next in [
        tie_breaker_answerer(host, asker),
        host.settings().crew.main.clone(),
    ] {
        if !in_line.contains(&next) {
            in_line.push(next);
        }
    }
    for (n, answerer) in in_line.iter().enumerate() {
        let failed = match consultant::session_answer(
            host,
            question,
            answerer,
            RoleName::TieBreaker,
        )
        .await
        {
            Ok((session, text)) => return Ok(Some((answerer.clone(), session, text))),
            Err(EngineError::Interrupted) => return Err(EngineError::Interrupted),
            Err(err) => err,
        };
        let name = roles::display_name(answerer);
        let text = match in_line.get(n + 1) {
            Some(next) if roles::provider(host, next) == roles::provider(host, asker) => format!(
                "{name} couldn't answer the question from {from}, so the main agent, {}, answers \
                 it though it shares the asker's provider: {failed}",
                roles::display_name(next)
            ),
            Some(next) => format!(
                "{name} couldn't answer the question from {from}, so {} answers it: {failed}",
                roles::display_name(next)
            ),
            None => format!(
                "{name} couldn't answer the question from {from}, so its agent goes with its \
                 best judgment: {failed}"
            ),
        };
        note(host, task, NoteLevel::Warn, text)?;
    }
    Ok(None)
}

/// Posts `text` as the answer to question `id`, and sends it to the agent that asked.
fn reply(
    host: &Host,
    id: &MessageId,
    question: &BlackboardMessage,
    text: String,
    from: Party,
) -> EngineResult<()> {
    let reply = host.read(|state| Post {
        kind: BlackboardKind::Answer,
        from,
        to: reply_to(state, question),
        refs: records::record_refs(question, &text),
        text,
        in_reply_to: Some(id.clone()),
        from_task: None,
    });
    let answer = post_once(host, reply)?;
    delivery::to_running(host, &answer)?;
    resume_asker(host, id)
}

fn note(host: &Host, task: &TaskId, level: NoteLevel, text: String) -> EngineResult<()> {
    host.record(Some(task), None, RunEventKind::Note(Note { level, text }))?;
    Ok(())
}

/// What the model that answered in your place is to the run, when it is more than the model
/// `approvals.answers` picked.
fn answerer_role(host: &Host, answerer: &ModelRef) -> Option<RoleName> {
    if *answerer == host.settings().crew.main {
        return Some(RoleName::Main);
    }
    let tie_breaker = host.read(|state| crate::tie_breaker::tie_breaker(state, host.settings()));
    (tie_breaker.as_ref() == Some(answerer)).then_some(RoleName::TieBreaker)
}

/// Who answers an agent's question in your place: the model `approvals.answers` picked,
/// unless it's of the asker's own provider, else [`tie_breaker_answerer`].
fn answerer_for(host: &Host, asker: &ModelRef) -> ModelRef {
    let picked = host
        .settings()
        .approvals
        .answers
        .as_ref()
        .and_then(|answerer| answerer.model());
    let provider = |worker: &ModelRef| roles::provider(host, worker);
    match picked {
        Some(picked) if provider(picked) != provider(asker) => picked.clone(),
        _ => tie_breaker_answerer(host, asker),
    }
}

/// The phase's tie-breaker, the main agent before the run has a phase, unless it's of the asker's
/// own provider: then the first crew member, or other agent you can run, of another provider.
fn tie_breaker_answerer(host: &Host, asker: &ModelRef) -> ModelRef {
    let provider = |worker: &ModelRef| roles::provider(host, worker);
    let tie_breaker = host.read(|state| {
        crate::tie_breaker::tie_breaker(state, host.settings())
            .unwrap_or_else(|| host.settings().crew.main.clone())
    });
    let theirs = provider(asker);
    if provider(&tie_breaker) != theirs {
        return tie_breaker;
    }
    let settings = host.settings();
    settings
        .crew
        .members()
        .into_iter()
        .chain(host.services().workers.available())
        .find(|worker| provider(worker) != theirs)
        .unwrap_or(tie_breaker)
}

/// What the tasks that ask about `task` call it, its worker's label such as `build:flag`, never
/// its id.
fn task_label(host: &Host, task: &TaskId) -> String {
    host.read(|state| crate::labels::task_label(state, task))
}

/// A question approval, titled by the asking task's label `from`.
fn question_approval(task: &TaskId, from: &str, text: &str, asker: ModelRef) -> ApprovalRequest {
    ApprovalRequest {
        at: None,
        kind: ApprovalKind::Question,
        title: format!("Question from {from}"),
        task: Some(task.clone()),
        packet: ApprovalPacket::Question(QuestionPacket {
            from: asker,
            task: task.clone(),
            text: text.to_owned(),
            options: Vec::new(),
        }),
        facts: EvidenceFacts::default(),
        agent_outputs: Vec::new(),
        documents: Vec::new(),
        options: None,
        free_text: FreeText::Answer,
        authors: Vec::new(),
    }
}