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
//! The host's own queue of control requests, and the relay that feeds it to each engine one
//! command at a time. A request stays in the queue until an engine has taken it, so one an
//! engine never read goes to the next engine, or back to `pending-answers.jsonl` when the host
//! exits.
//!
//! Some commands (`Stop`, `StopSession`, `Pause`, `CancelPause`, `ApprovalMode`,
//! `TakeOverEnded`) carry no reply channel in the engine contract, so for them "taken" is all
//! the host can confirm, never "applied".

use std::collections::VecDeque;

use argonauts_core::control::{ControlReply, ControlRequest};
use argonauts_core::engine::{EngineCommand, EngineExit};
use argonauts_core::events::run::{RunOutcome, WaitReason};
use tokio::sync::{mpsc, oneshot};

use super::server::{EngineReply, RUN_HOST_EXITING, command, error};

/// One request, and its way back to whoever sent it.
pub(crate) struct Entry {
    pub request: ControlRequest,
    reply: oneshot::Sender<Delivery>,
}

enum Delivery {
    /// An engine took the command, and this is what it will answer.
    Taken(EngineReply),
    /// The host answered, without an engine.
    Answered(Box<ControlReply>),
}

/// Waits for the reply to one queued request.
pub(crate) struct Receipt(oneshot::Receiver<Delivery>);

impl Entry {
    pub(crate) fn new(request: ControlRequest) -> (Self, Receipt) {
        let (reply, receipt) = oneshot::channel();
        (Self { request, reply }, Receipt(receipt))
    }

    /// A sender that hung up no longer wants the reply.
    pub(crate) fn answer(self, reply: ControlReply) {
        let _ = self.reply.send(Delivery::Answered(Box::new(reply)));
    }

    fn taken(self, reply: EngineReply) {
        let _ = self.reply.send(Delivery::Taken(reply));
    }
}

impl Receipt {
    pub(crate) async fn reply(self) -> ControlReply {
        match self.0.await {
            Ok(Delivery::Taken(reply)) => reply.wait().await,
            Ok(Delivery::Answered(reply)) => *reply,
            Err(_) => error(RUN_HOST_EXITING),
        }
    }
}

/// The host's end of the queue. `put_back` puts a request ahead of the ones behind it.
pub(crate) struct Queue {
    incoming: mpsc::UnboundedReceiver<Entry>,
    backlog: VecDeque<Entry>,
}

impl Queue {
    pub(crate) fn new(incoming: mpsc::UnboundedReceiver<Entry>) -> Self {
        Self {
            incoming,
            backlog: VecDeque::new(),
        }
    }

    async fn next(&mut self) -> Option<Entry> {
        match self.backlog.pop_front() {
            Some(entry) => Some(entry),
            None => self.incoming.recv().await,
        }
    }

    /// The next request if one is waiting now.
    fn try_next(&mut self) -> Option<Entry> {
        self.backlog
            .pop_front()
            .or_else(|| self.incoming.try_recv().ok())
    }

    fn put_back(&mut self, entry: Entry) {
        self.backlog.push_front(entry);
    }

    pub(crate) fn is_empty(&self) -> bool {
        self.backlog.is_empty() && self.incoming.is_empty()
    }

    /// Everything waiting now, oldest first.
    pub(crate) fn take_all(&mut self) -> Vec<Entry> {
        let mut entries: Vec<Entry> = self.backlog.drain(..).collect();
        while let Ok(entry) = self.incoming.try_recv() {
            entries.push(entry);
        }
        entries
    }

    /// Returns entries taken with `take_all` to the front, in their order.
    pub(crate) fn restore(&mut self, entries: Vec<Entry>) {
        for entry in entries.into_iter().rev() {
            self.put_back(entry);
        }
    }

    /// Stops taking requests and returns every one still waiting.
    pub(crate) fn close(&mut self) -> Vec<Entry> {
        self.incoming.close();
        self.take_all()
    }
}

/// A request sent on to an engine, and the reply its command brings, until the relay sees the
/// engine take it.
pub(crate) type InFlight = (Entry, EngineReply);

/// What one engine's relay did: how many commands it took, and the last one sent when the
/// engine closed its inbox before the relay saw it taken.
pub(crate) struct Forwarded {
    taken: usize,
    unsettled: Option<InFlight>,
}

impl Forwarded {
    /// Settles the last command once the engine's exit is known, and returns how many commands
    /// the engine took.
    ///
    /// An engine that takes a command and returns before the relay runs again looks the same
    /// as one that never took it. A command with a reply shows it by its answer. One without a
    /// reply counts as taken when the exit already does what it asks, such as a `Stop` and a
    /// run that finished stopped, so the next engine never gets it twice. Any other goes back to
    /// the queue: a second copy is safer than a lost one.
    pub(crate) fn settle(self, queue: &mut Queue, exit: Option<&EngineExit>) -> usize {
        let Some((entry, reply)) = self.unsettled else {
            return self.taken;
        };
        if reply.answered() || exit.is_some_and(|exit| done_by(&entry.request, exit)) {
            entry.taken(reply);
            return self.taken + 1;
        }
        queue.put_back(entry);
        self.taken
    }
}

/// Whether `exit` already did what `request` asks, so delivering it again changes nothing.
fn done_by(request: &ControlRequest, exit: &EngineExit) -> bool {
    matches!(
        (request, exit),
        (
            ControlRequest::Stop,
            EngineExit::Finished(RunOutcome::Stopped)
        ) | (
            ControlRequest::Pause { .. },
            EngineExit::Waiting(WaitReason::Paused)
        )
    )
}

/// Puts the first waiting request in a new engine's `inbox` before the engine runs. An engine
/// reads its inbox as it starts to learn what woke the run, such as your answer, and the relay
/// may not get its first turn until later. [`forward`] follows the command from here.
pub(crate) fn prime(queue: &mut Queue, inbox: &mpsc::Sender<EngineCommand>) -> Option<InFlight> {
    while let Some(entry) = queue.try_next() {
        let (command, reply) = match command(entry.request.clone()) {
            Ok(made) => made,
            Err(refused) => {
                entry.answer(error(refused));
                continue;
            }
        };
        // A new inbox has room for it. Were it refused, the relay would send it later.
        if inbox.try_send(command).is_err() {
            queue.put_back(entry);
            return None;
        }
        return Some((entry, reply));
    }
    None
}

/// Feeds `queue` to one engine through `inbox`, which holds one command, after `primed`, the
/// one [`prime`] put there. The next `reserve` succeeding, or a reply, is how the relay knows
/// the engine took the last one. Returns when the engine drops its inbox;
/// [`Forwarded::settle`] then decides the last command.
pub(crate) async fn forward(
    queue: &mut Queue,
    inbox: mpsc::Sender<EngineCommand>,
    primed: Option<InFlight>,
) -> Forwarded {
    let mut taken = 0;
    let mut in_flight = primed;
    loop {
        let Ok(permit) = inbox.reserve().await else {
            break;
        };
        if let Some((entry, reply)) = in_flight.take() {
            taken += 1;
            entry.taken(reply);
        }
        let next = tokio::select! {
            entry = queue.next() => entry,
            () = inbox.closed() => None,
        };
        let Some(entry) = next else {
            break;
        };
        match command(entry.request.clone()) {
            Ok((command, reply)) => {
                permit.send(command);
                in_flight = Some((entry, reply));
            }
            Err(refused) => entry.answer(error(refused)),
        }
    }
    Forwarded {
        taken,
        unsettled: in_flight,
    }
}

/// What a client does with the request when no run host answers: queue it for the next one.
/// A take-back isn't one, since only a live run host knows which messages it still holds.
pub(crate) fn deferrable(request: &ControlRequest) -> bool {
    matches!(
        request,
        ControlRequest::Answer { .. }
            | ControlRequest::Guidance { .. }
            | ControlRequest::Mention { .. }
            | ControlRequest::Reassign { .. }
            | ControlRequest::Stop
            | ControlRequest::StopSession { .. }
            | ControlRequest::ApprovalMode { .. }
            | ControlRequest::TakeOverEnded { .. }
            | ControlRequest::SpotCheck { .. }
            | ControlRequest::Goal { .. }
    )
}

/// The reply for a request saved to `pending-answers.jsonl`, as a client reports a deferred one.
pub(crate) fn deferred_reply(request: &ControlRequest) -> ControlReply {
    match request {
        ControlRequest::Guidance { .. } | ControlRequest::Mention { .. } => {
            error("the run is waiting; the message is delivered when it resumes")
        }
        _ => ControlReply::Ok,
    }
}

#[cfg(test)]
mod tests {
    use std::time::Duration;

    use argonauts_core::settings::approvals::ApprovalMode;

    use super::*;
    use crate::host::server::tests::TOKEN;

    fn approval_mode() -> ControlRequest {
        ControlRequest::ApprovalMode {
            mode: ApprovalMode::Auto,
            token: TOKEN.to_owned(),
        }
    }

    #[tokio::test]
    async fn a_command_the_engine_never_took_goes_to_the_next_engine() {
        let (sender, incoming) = mpsc::unbounded_channel();
        let mut queue = Queue::new(incoming);
        let (entry, receipt) = Entry::new(approval_mode());
        sender.send(entry).unwrap();
        let mut replied = tokio::spawn(receipt.reply());

        let (inbox, unread) = mpsc::channel(1);
        let (forwarded, ()) = tokio::join!(forward(&mut queue, inbox, None), async {
            tokio::task::yield_now().await;
            drop(unread);
        });
        assert_eq!(forwarded.settle(&mut queue, None), 0);
        assert!(!queue.is_empty(), "the command is still the host's");
        assert!(
            tokio::time::timeout(Duration::from_millis(50), &mut replied)
                .await
                .is_err(),
            "nothing is acknowledged before an engine takes it"
        );

        let (inbox, mut read) = mpsc::channel(1);
        let (release, released) = oneshot::channel::<()>();
        let engine = tokio::spawn(async move {
            let command = read.recv().await;
            let _ = released.await;
            command
        });
        let (forwarded, ()) = tokio::join!(forward(&mut queue, inbox, None), async {
            let reply = (&mut replied).await.unwrap();
            assert_eq!(reply, ControlReply::Ok, "acknowledged once taken");
            release.send(()).unwrap();
        });
        assert_eq!(forwarded.settle(&mut queue, None), 1);
        assert!(queue.is_empty());
        assert!(matches!(
            engine.await.unwrap(),
            Some(EngineCommand::ApprovalMode { .. })
        ));
    }

    /// What `prime` puts in the inbox is followed like any other command: one engine never
    /// takes it, so the next gets it, and that one's taking it is what acknowledges it.
    #[tokio::test]
    async fn a_primed_command_is_followed_like_any_other() {
        let (sender, incoming) = mpsc::unbounded_channel();
        let mut queue = Queue::new(incoming);
        let subscribe = ControlRequest::Subscribe {
            from_seq: 0,
            from_snapshot: false,
        };
        sender.send(Entry::new(subscribe).0).unwrap();
        let (entry, receipt) = Entry::new(approval_mode());
        sender.send(entry).unwrap();
        let mut replied = tokio::spawn(receipt.reply());

        let (inbox, unread) = mpsc::channel(1);
        let primed = prime(&mut queue, &inbox);
        assert!(
            queue.is_empty(),
            "past the one no engine takes, it waits in the inbox"
        );
        drop(unread);
        let forwarded = forward(&mut queue, inbox, primed).await;
        assert_eq!(forwarded.settle(&mut queue, None), 0);
        assert!(!queue.is_empty(), "the next engine gets it");
        assert!(
            tokio::time::timeout(Duration::from_millis(50), &mut replied)
                .await
                .is_err(),
            "nothing is acknowledged before an engine takes it"
        );

        let (inbox, mut read) = mpsc::channel(1);
        let primed = prime(&mut queue, &inbox);
        assert!(
            matches!(read.try_recv(), Ok(EngineCommand::ApprovalMode { .. })),
            "it's there before the engine first looks"
        );
        let (forwarded, ()) = tokio::join!(forward(&mut queue, inbox, primed), async {
            let reply = (&mut replied).await.unwrap();
            assert_eq!(reply, ControlReply::Ok, "acknowledged once taken");
            drop(read);
        });
        assert_eq!(forwarded.settle(&mut queue, None), 1);
        assert!(queue.is_empty());
    }

    /// The engine reads the command and returns before the relay polls again, which a
    /// single-threaded join makes certain.
    async fn taken_then_returned(request: ControlRequest, exit: EngineExit) -> (usize, Queue) {
        let (sender, incoming) = mpsc::unbounded_channel();
        let mut queue = Queue::new(incoming);
        sender.send(Entry::new(request).0).unwrap();
        let (inbox, mut read) = mpsc::channel(1);
        let (forwarded, read_one) = tokio::join!(forward(&mut queue, inbox, None), async move {
            let command = read.recv().await;
            drop(read);
            command.is_some()
        });
        assert!(read_one, "the engine took the command");
        let taken = forwarded.settle(&mut queue, Some(&exit));
        (taken, queue)
    }

    #[tokio::test]
    async fn a_command_the_exit_already_did_is_never_sent_again() {
        let stop = taken_then_returned(
            ControlRequest::Stop,
            EngineExit::Finished(RunOutcome::Stopped),
        );
        let (taken, queue) = stop.await;
        assert_eq!(taken, 1);
        assert!(
            queue.is_empty(),
            "the next engine doesn't stop the run again"
        );

        let pause = taken_then_returned(
            ControlRequest::Pause {
                now: true,
                outside_agent: None,
            },
            EngineExit::Waiting(WaitReason::Paused),
        );
        let (taken, queue) = pause.await;
        assert_eq!(taken, 1);
        assert!(
            queue.is_empty(),
            "the next engine doesn't pause the run again"
        );
    }

    #[tokio::test]
    async fn a_command_the_exit_doesnt_explain_goes_to_the_next_engine() {
        let (taken, queue) =
            taken_then_returned(approval_mode(), EngineExit::Waiting(WaitReason::Approval)).await;
        assert_eq!(taken, 0);
        assert!(!queue.is_empty(), "a second copy rather than a lost one");

        let (taken, queue) = taken_then_returned(
            ControlRequest::Stop,
            EngineExit::Waiting(WaitReason::Approval),
        )
        .await;
        assert_eq!(taken, 0);
        assert!(!queue.is_empty(), "the run didn't stop, so the stop waits");
    }

    #[tokio::test]
    async fn closing_returns_what_is_left_in_order() {
        let (sender, incoming) = mpsc::unbounded_channel();
        let mut queue = Queue::new(incoming);
        for request in [approval_mode(), ControlRequest::Stop] {
            sender.send(Entry::new(request).0).unwrap();
        }
        let first = queue.take_all();
        sender
            .send(Entry::new(ControlRequest::CancelPause).0)
            .unwrap();
        queue.restore(first);
        let left: Vec<_> = queue
            .close()
            .into_iter()
            .map(|entry| entry.request)
            .collect();
        assert_eq!(
            left,
            [
                approval_mode(),
                ControlRequest::Stop,
                ControlRequest::CancelPause
            ]
        );
        assert!(sender.send(Entry::new(ControlRequest::Stop).0).is_err());
    }
}