rho-coding-agent 3.7.0

A fast Rust agent harness with a small footprint and opinionated defaults
Documentation
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
use std::{
    path::{Path, PathBuf},
    sync::Arc,
    time::Duration,
};

use pretty_assertions::assert_eq;
use tempfile::TempDir;
use tokio::sync::{mpsc, watch};

use super::{DaemonView, PromptStart, StartedTurn, TurnLimit, TurnTask, UNEXPECTED_STOP};
use crate::app::daemon_host::sessions::{
    prompt_keys::PromptMessage,
    queued::Queued,
    test_events::{
        approval, approval_withdrawn, plan, plan_withdrawn, question, question_withdrawn,
    },
};
use crate::app::daemon_host::settings::ConfigStore;
use crate::app::prompt_content::Prompt;
use crate::{
    app::{
        daemon_host::{
            jobs::{JobId, JobRecord, JobRegistry, JobState, JobStore, TurnEnd, TurnRecord},
            sessions::{open::OpenedSession, t3_wake::WakeDir, LoadedSession},
        },
        session_runtime::{
            host_woken_runtime_on, unattended_runtime_on, HangUntilCancel, InsertedMessage,
            QuestionSupport, SessionEvent, TurnKeys,
        },
    },
    config::Config,
    session_environment::SessionEnvironment,
};

/// A prompt of `text` alone.
fn text_prompt(text: &str) -> Prompt {
    Prompt::parse(&[rho_protocol::ContentBlock::Text { text: text.into() }]).unwrap()
}

/// Bounds each wait on an explicit signal; never the way a test syncs.
const TEST_BUDGET: Duration = Duration::from_secs(5);

fn inserted() -> SessionEvent {
    SessionEvent::MessageInserted(InsertedMessage::Unreplayed)
}

/// A job in `jobs` whose turn has started.
fn started_job(jobs: &JobRegistry) -> JobId {
    let record = JobRecord::new_session(
        JobId::generate(),
        PathBuf::from("/work"),
        rho_sdk::SessionId::new(),
    );
    let job = record.id.clone();
    jobs.register(record).unwrap();
    jobs.start_turn(
        &job,
        TurnRecord::starting(/*key*/ None),
        crate::caller_context::CallerContext::ROOT,
    )
    .unwrap();
    job
}

/// The id of the turn `job` has under way.
fn turn_of(jobs: &JobRegistry, job: &JobId) -> rho_protocol::TurnId {
    jobs.get(job).unwrap().turn.unwrap().id
}

/// A view of `session`'s turn under way, and where it tells how the turn
/// began.
fn view_of(session: Arc<LoadedSession>) -> (DaemonView, watch::Receiver<PromptStart>) {
    let (started, start) = watch::channel(PromptStart::Pending);
    let turn = turn_of(&session.jobs, &session.job);
    let message = PromptMessage::default();
    (DaemonView::new(session, turn, started, message), start)
}

/// The turn `session` has under way, as a prompt starts it, which `cancel`
/// cancels, and where it tells how it began.
fn started_turn(
    session: &LoadedSession,
    cancel: rho_sdk::CancellationToken,
) -> (StartedTurn, watch::Receiver<PromptStart>) {
    let (start, started) = watch::channel(PromptStart::Pending);
    let turn = StartedTurn {
        id: turn_of(&session.jobs, &session.job),
        cancel,
        runtime: Queued::join(&session.runtime),
        budget: None,
        start,
        message: PromptMessage::default(),
        user_key: None,
        limit: None,
    };
    (turn, started)
}

/// How a turn began, if it has said.
fn told(start: &watch::Receiver<PromptStart>) -> Option<PromptStart> {
    Some(start.borrow().clone()).filter(|start| *start != PromptStart::Pending)
}

/// How a turn began, once it says, or `None` for one that ended without
/// saying.
async fn began(mut start: watch::Receiver<PromptStart>) -> Option<PromptStart> {
    let told = tokio::time::timeout(
        TEST_BUDGET,
        start.wait_for(|start| *start != PromptStart::Pending),
    )
    .await
    .expect("the turn answers its prompt");
    told.ok().map(|start| start.clone())
}

/// The host a test session runs under.
#[derive(Clone, Copy, Debug)]
enum Host {
    /// Nobody watches it.
    Unattended,
    /// It runs the session's children and wakes the idle session, as T3
    /// Code does, so `/rho-wake` prompts wait for its signal.
    Waking,
}

/// Session `job`, loaded in `root` with a provider that never answers, so
/// nothing but the test drives its turns.
async fn loaded_session(root: &Path, jobs: &Arc<JobRegistry>, job: JobId) -> Arc<LoadedSession> {
    loaded_session_under(Host::Unattended, root, jobs, job).await
}

/// [`loaded_session`] under `host`.
async fn loaded_session_under(
    host: Host,
    root: &Path,
    jobs: &Arc<JobRegistry>,
    job: JobId,
) -> Arc<LoadedSession> {
    let config = Config::default();
    let provider: Arc<dyn rho_sdk::provider::ModelProvider> = Arc::new(HangUntilCancel);
    let sdk = rho_sdk::Rho::builder()
        .provider_shared(Arc::clone(&provider))
        .build()
        .unwrap();
    let runtime = match host {
        Host::Unattended => {
            unattended_runtime_on(root, &config, QuestionSupport::Unavailable, sdk, provider).await
        }
        Host::Waking => host_woken_runtime_on(root, &config, sdk, provider).await,
    };
    Arc::new(LoadedSession::new(
        &jobs.get(&job).unwrap(),
        Arc::clone(jobs),
        OpenedSession {
            runtime,
            config,
            global: Arc::new(Config::default()),
            environment: SessionEnvironment::default(),
            setup: rho_protocol::SessionSetup::default(),
            fork_of: None,
            held_context: Default::default(),
        },
        /*auths*/ &[],
        &WakeDir::in_home(root),
        &Arc::new(ConfigStore::new(
            root,
            crate::app::agent_concurrency::AgentConcurrency::from_config(
                crate::config::DEFAULT_AGENT_CONCURRENCY,
            ),
        )),
    ))
}

/// Shows `events` to `view` as one model turn.
async fn show(view: &mut DaemonView, events: Vec<SessionEvent>) {
    let (event_sender, event_receiver) = mpsc::unbounded_channel();
    for event in events {
        event_sender.send(event).unwrap();
    }
    drop(event_sender);
    let (control, _control_receiver) = mpsc::unbounded_channel();
    view.show(event_receiver, control, super::cost::TurnCost::unpriced())
        .await;
}

/// A turn that shows `events`, and the job's `state` and the prompt
/// request's `start` after them.
struct WaitingCase {
    name: &'static str,
    events: Vec<SessionEvent>,
    state: JobState,
    start: Option<PromptStart>,
}

/// A turn that shows `events`, then lets go of its view as `release` says;
/// the prompt request then got `start`.
struct EndCase {
    name: &'static str,
    events: Vec<SessionEvent>,
    release: Release,
    start: Option<PromptStart>,
}

// Covers: a session's job is waiting_for_input exactly while some approval,
// plan approval, or question of its turn is open, whichever kind and in
// whatever order they open and close, and running otherwise; and the prompt
// request is answered once the turn saved its message, not before.
// Owner: daemon session turn view.
#[tokio::test]
async fn the_job_waits_for_input_while_any_request_is_open() {
    let began = || Some(PromptStart::Began);
    let cases = vec![
        WaitingCase {
            name: "no request",
            events: vec![
                inserted(),
                SessionEvent::RunStarted {
                    keys: TurnKeys::for_tests("turn"),
                },
            ],
            state: JobState::Running,
            start: began(),
        },
        WaitingCase {
            name: "an open approval",
            events: vec![inserted(), approval(0)],
            state: JobState::WaitingForInput,
            start: began(),
        },
        WaitingCase {
            name: "an approval withdrawn",
            events: vec![inserted(), approval(0), approval_withdrawn(0)],
            state: JobState::Running,
            start: began(),
        },
        WaitingCase {
            name: "one of two approvals withdrawn",
            events: vec![inserted(), approval(0), approval(1), approval_withdrawn(1)],
            state: JobState::WaitingForInput,
            start: began(),
        },
        WaitingCase {
            name: "a plan open after the question closed",
            events: vec![inserted(), question("q"), plan(0), question_withdrawn("q")],
            state: JobState::WaitingForInput,
            start: began(),
        },
        WaitingCase {
            name: "every kind opened and withdrawn",
            events: vec![
                inserted(),
                approval(0),
                plan(0),
                question("q"),
                plan_withdrawn(0),
                approval_withdrawn(0),
                question_withdrawn("q"),
            ],
            state: JobState::Running,
            start: began(),
        },
        WaitingCase {
            name: "a withdrawal of a request never opened",
            events: vec![inserted(), approval_withdrawn(3), question_withdrawn("x")],
            state: JobState::Running,
            start: began(),
        },
        WaitingCase {
            name: "events before the message is saved",
            events: vec![SessionEvent::Notice {
                text: "reopened".into(),
            }],
            state: JobState::Running,
            start: None,
        },
    ];
    let root = TempDir::new().unwrap();
    let home = TempDir::new().unwrap();
    let jobs = Arc::new(JobRegistry::open(JobStore::open(home.path()).unwrap()));
    for case in cases {
        let job = started_job(&jobs);
        let session = loaded_session(root.path(), &jobs, job.clone()).await;
        let (mut view, start) = view_of(session);

        show(&mut view, case.events).await;

        assert_eq!(
            (jobs.get(&job).map(|record| record.state), told(&start)),
            (Some(case.state), case.start),
            "{}",
            case.name
        );
    }
}

/// How a test lets go of a view.
enum Release {
    /// The turn task records the turn's end: what clients are told, and
    /// what the job records.
    End((Option<rho_protocol::TurnOutcome>, TurnEnd)),
    /// The view drops without an end, as when the turn task panics.
    Drop,
}

impl Release {
    /// The end the job records once the view is let go.
    fn recorded_end(&self) -> TurnEnd {
        match self {
            Self::End((_, end)) => end.clone(),
            Self::Drop => TurnEnd::Failed(UNEXPECTED_STOP.into()),
        }
    }
}

// Covers: the job records how the turn ended, and the prompt request learns
// how the turn began: saved once the message is, else how the turn ended
// before it (a completed turn saved its prompt); a turn interrupted before
// saving leaves the request to the daemon's stop; a view dropped without an
// end, as by a panicking turn task, records the turn failed and tells a
// request still waiting so.
// Owner: daemon session turn view.
#[tokio::test]
async fn the_turn_end_is_recorded_once_and_reaches_a_waiting_prompt() {
    let completed = || {
        let answer = rho_protocol::TurnOutcome::Answered {
            text: "done".into(),
            stop: rho_protocol::AnswerStop::EndTurn,
        };
        (Some(answer), TurnEnd::Completed)
    };
    let failed = || {
        let (outcome, end) = super::failed(rho_protocol::TurnError {
            kind: rho_protocol::TurnErrorKind::Provider,
            message: "provider refused the request".into(),
        });
        (Some(outcome), end)
    };
    let cases = vec![
        EndCase {
            name: "completed after saving",
            events: vec![inserted()],
            release: Release::End(completed()),
            start: Some(PromptStart::Began),
        },
        EndCase {
            name: "failed after saving",
            events: vec![inserted()],
            release: Release::End(failed()),
            start: Some(PromptStart::Began),
        },
        EndCase {
            name: "completed without a saved message",
            events: vec![],
            release: Release::End(completed()),
            start: Some(PromptStart::Began),
        },
        EndCase {
            name: "cancelled before saving",
            events: vec![],
            release: Release::End((
                Some(rho_protocol::TurnOutcome::Cancelled),
                TurnEnd::Cancelled,
            )),
            start: Some(PromptStart::Ended(rho_protocol::TurnOutcome::Cancelled)),
        },
        EndCase {
            name: "interrupted before saving",
            events: vec![],
            release: Release::End((None, TurnEnd::Interrupted)),
            start: None,
        },
        EndCase {
            name: "failed before saving",
            events: vec![],
            release: Release::End(failed()),
            start: failed().0.map(PromptStart::Ended),
        },
        EndCase {
            name: "dropped while waiting for input",
            events: vec![inserted(), approval(0)],
            release: Release::Drop,
            start: Some(PromptStart::Began),
        },
        EndCase {
            name: "dropped before saving",
            events: vec![],
            release: Release::Drop,
            start: Some(PromptStart::Ended(rho_protocol::TurnOutcome::Failed {
                error: rho_protocol::TurnError {
                    kind: rho_protocol::TurnErrorKind::Other,
                    message: UNEXPECTED_STOP.into(),
                },
            })),
        },
    ];
    let root = TempDir::new().unwrap();
    let home = TempDir::new().unwrap();
    let jobs = Arc::new(JobRegistry::open(JobStore::open(home.path()).unwrap()));
    for case in cases {
        let job = started_job(&jobs);
        let session = loaded_session(root.path(), &jobs, job.clone()).await;
        let (mut view, start) = view_of(session);
        show(&mut view, case.events).await;
        let expected_end = case.release.recorded_end();

        match case.release {
            Release::End((outcome, end)) => view.end(outcome, end),
            Release::Drop => drop(view),
        }

        assert_eq!(
            (jobs.get(&job).map(|record| record.state), told(&start)),
            (Some(JobState::Ended(expected_end)), case.start),
            "{}",
            case.name
        );
    }
}

// Covers: a cancel sent before the turn begins its prompt ends the turn
// cancelled before it saves the prompt or calls the model (whose provider
// would wait for a cancel), so the prompt request learns it was cancelled
// and the session holds no message.
// Owner: daemon session turn task.
#[tokio::test]
async fn a_cancel_before_the_prompt_begins_saves_nothing() {
    let root = TempDir::new().unwrap();
    let home = TempDir::new().unwrap();
    let jobs = Arc::new(JobRegistry::open(JobStore::open(home.path()).unwrap()));
    let job = started_job(&jobs);
    let session = loaded_session(root.path(), &jobs, job.clone()).await;
    let cancel = rho_sdk::CancellationToken::new();
    cancel.cancel();

    let (turn, start) = started_turn(&session, cancel);
    TurnTask {
        session: Arc::clone(&session),
        turn,
        stopping: rho_sdk::CancellationToken::new(),
    }
    .spawn(text_prompt("hello"), rho_protocol::PromptAuthor::Person);
    let start = began(start).await;
    // Released once the turn recorded its end.
    let messages = session
        .runtime
        .lock()
        .await
        .as_ref()
        .map(|runtime| runtime.history_for_tests().len());

    assert_eq!(
        (start, jobs.get(&job).map(|record| record.state), messages),
        (
            Some(PromptStart::Ended(rho_protocol::TurnOutcome::Cancelled)),
            Some(JobState::Ended(TurnEnd::Cancelled)),
            Some(0)
        )
    );
}

// Covers: a prompt the client sends on the person's behalf runs only as a
// workflow start; any other, a model prompt or a host-woken session's
// `/rho-wake` wait, ends the turn as a configuration failure before it
// saves or shows anything, so it never runs as the person's message.
// Owner: daemon session turn task.
#[tokio::test]
async fn a_client_prompt_that_starts_no_workflow_saves_nothing() {
    let cases = [
        (Host::Unattended, "hello".to_owned()),
        (
            Host::Waking,
            "/rho-wake 123e4567-e89b-12d3-a456-426614174000".to_owned(),
        ),
    ];

    for (host, prompt) in cases {
        let root = TempDir::new().unwrap();
        let home = TempDir::new().unwrap();
        let jobs = Arc::new(JobRegistry::open(JobStore::open(home.path()).unwrap()));
        let job = started_job(&jobs);
        let session = loaded_session_under(host, root.path(), &jobs, job.clone()).await;

        let (turn, start) = started_turn(&session, rho_sdk::CancellationToken::new());
        TurnTask {
            session: Arc::clone(&session),
            turn,
            stopping: rho_sdk::CancellationToken::new(),
        }
        .spawn(text_prompt(&prompt), rho_protocol::PromptAuthor::Client);
        let start = began(start).await;
        // Released once the turn recorded its end.
        let messages = session
            .runtime
            .lock()
            .await
            .as_ref()
            .map(|runtime| runtime.history_for_tests().len());
        let kind = match start {
            Some(PromptStart::Ended(rho_protocol::TurnOutcome::Failed { error })) => {
                Some(error.kind)
            }
            _ => None,
        };

        assert_eq!(
            (kind, messages),
            (Some(rho_protocol::TurnErrorKind::Configuration), Some(0)),
            "{host:?} {prompt}"
        );
    }
}

/// A turn with a time limit of `timeout_ms`, whose session is busy until
/// the limit passed when `busy`, with a client's cancel sent before that when
/// `cancelled_first`. A turn whose session is free reaches its limit after
/// it saved its prompt, by the test moving the clock there.
struct LimitCase {
    name: &'static str,
    timeout_ms: u64,
    busy: bool,
    cancelled_first: bool,
    start: PromptStart,
    end: TurnEnd,
    saved: usize,
}

// Covers: the daemon cancels a turn still running at its time limit, and
// the turn ends timed out with the limit: one whose limit passed while it
// waited for the session, which the limit counts, saves nothing and answers
// the prompt with how it ended; one that saved first runs, its model call
// hanging, until the limit cancels it. A client's cancel that came before
// the limit stays a cancel, though the turn had not ended by then.
// Owner: daemon session turn task.
#[tokio::test]
async fn the_daemon_times_a_turn_out_at_its_limit() {
    let ended = |end: &TurnEnd| {
        PromptStart::Ended(match *end {
            TurnEnd::TimedOut { timeout_ms } => rho_protocol::TurnOutcome::TimedOut { timeout_ms },
            _ => rho_protocol::TurnOutcome::Cancelled,
        })
    };
    let cases = [
        LimitCase {
            name: "limit passed waiting for the session",
            timeout_ms: 1,
            busy: true,
            cancelled_first: false,
            start: ended(&TurnEnd::TimedOut { timeout_ms: 1 }),
            end: TurnEnd::TimedOut { timeout_ms: 1 },
            saved: 0,
        },
        LimitCase {
            name: "limit passed mid-turn",
            // Never reached in real time: the test moves the clock there.
            timeout_ms: 3_600_000,
            busy: false,
            cancelled_first: false,
            start: PromptStart::Began,
            end: TurnEnd::TimedOut {
                timeout_ms: 3_600_000,
            },
            saved: 1,
        },
        LimitCase {
            name: "cancelled before the limit passed",
            timeout_ms: 1,
            busy: true,
            cancelled_first: true,
            start: ended(&TurnEnd::Cancelled),
            end: TurnEnd::Cancelled,
            saved: 0,
        },
    ];
    let root = TempDir::new().unwrap();
    let home = TempDir::new().unwrap();
    let jobs = Arc::new(JobRegistry::open(JobStore::open(home.path()).unwrap()));
    for case in cases {
        let job = started_job(&jobs);
        let session = loaded_session(root.path(), &jobs, job.clone()).await;
        let runtime = case.busy.then(|| session.runtime.try_lock().unwrap());

        // The session's own cancel, as a prompt passes it, which the
        // limit's cancel trips.
        let cancel = session.turn().cancel.clone();
        let (turn, start) = started_turn(&session, cancel);
        let turn = StartedTurn {
            limit: Some(TurnLimit::starting_now(case.timeout_ms)),
            ..turn
        };
        TurnTask {
            session: Arc::clone(&session),
            turn,
            stopping: rho_sdk::CancellationToken::new(),
        }
        .spawn(text_prompt("hello"), rho_protocol::PromptAuthor::Person);
        if case.cancelled_first {
            session.cancel_turn();
        }
        if let Some(runtime) = runtime {
            // Waits on the clock, not on the turn, which waits for this.
            tokio::time::sleep(Duration::from_millis(case.timeout_ms + 1)).await;
            drop(runtime);
        }
        let start = began(start).await;
        if !case.busy {
            // The prompt is saved and the model call hangs: move the clock
            // to the limit rather than wait for it, then let it run again for
            // the budget below.
            tokio::time::pause();
            tokio::time::advance(Duration::from_millis(case.timeout_ms)).await;
            tokio::time::resume();
        }
        // Released once the turn recorded its end.
        let messages = tokio::time::timeout(TEST_BUDGET, session.runtime.lock())
            .await
            .expect("the limit ends the turn")
            .as_ref()
            .map(|runtime| runtime.history_for_tests().len());

        assert_eq!(
            (start, jobs.get(&job).map(|record| record.state), messages),
            (
                Some(case.start),
                Some(JobState::Ended(case.end)),
                Some(case.saved)
            ),
            "{}",
            case.name
        );
    }
}