rho-coding-agent 3.6.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
use std::{path::Path, sync::Arc};

use pretty_assertions::assert_eq;
use rho_protocol::{self as wire, SessionSetup, TurnId};
use tempfile::TempDir;
use tokio::{sync::watch, time::timeout};

use super::super::{
    background::SessionBackground,
    idle_wake::try_give_up,
    pull_request_wake::watch_for_tests,
    test_sessions::{load, load_waiting_on, seen, until, Load, Seen, TEST_BUDGET},
    Entered, LoadedSession, OpenWith, Sessions,
};
use crate::{
    app::{
        agent_executor::AgentRunHandle,
        daemon_host::jobs::{JobRegistry, JobStore, TurnEnd},
        pull_requests::ScriptedForge,
        session_runtime::BackgroundWait,
        subagent_manager::SubagentManager,
        subagent_pool::SubagentPool,
    },
    subagent::{RunState, RunStatus},
    tools::process::{KeepWarm, ProcessLimits, ProcessManager, ProcessRole},
};

/// A command that outlives every wait here, in the shell of either platform.
const LONG_RUNNING_COMMAND: &str = "sleep 300";

const SUBAGENT: &str = "abc123";

/// A session's job state or activity, as its clients hear them, in order.
#[derive(Debug, PartialEq)]
enum Heard {
    State(wire::JobState),
    Activity(wire::SessionActivity),
}

/// What the session's feed told of its states and activity.
fn heard(session: &LoadedSession) -> Vec<Heard> {
    session
        .feed
        .buffered_for_tests()
        .into_iter()
        .filter_map(|event| match event {
            wire::SessionEvent::State { state, .. } => Some(Heard::State(state)),
            wire::SessionEvent::Activity { state } => Some(Heard::Activity(state)),
            _ => None,
        })
        .collect()
}

/// The activity the session's clients heard last.
fn activity(session: &LoadedSession) -> Option<wire::SessionActivity> {
    heard(session)
        .into_iter()
        .rev()
        .find_map(|heard| match heard {
            Heard::Activity(state) => Some(state),
            Heard::State(_) => None,
        })
}

/// Where a fixture session's background work is read from.
#[derive(Clone, Copy)]
enum Work {
    /// The fixture's managers, waited for as `wait`.
    Managers(BackgroundWait),
    /// The session's runtime's tools, as the daemon reads them. Its client
    /// asked the daemon to wake it idle (`idle_wake: turn`), so they hold
    /// its pull request inbox.
    Tools,
}

/// A session loaded in its own daemon, whose pull requests `forge`
/// scripts.
struct Fixture {
    root: TempDir,
    _home: TempDir,
    forge: Arc<ScriptedForge>,
    sessions: Arc<Sessions>,
    session: Arc<LoadedSession>,
    processes: ProcessManager,
    subagents: SubagentManager,
}

impl Fixture {
    /// A session whose work waits as `wait`, from the fixture's managers.
    async fn new(wait: BackgroundWait) -> Self {
        Self::build(Work::Managers(wait)).await
    }

    /// A session whose work is its runtime's tools', its pull requests
    /// among them.
    async fn watching() -> Self {
        Self::build(Work::Tools).await
    }

    async fn build(work: Work) -> Self {
        let (root, home) = (TempDir::new().unwrap(), TempDir::new().unwrap());
        let forge = ScriptedForge::new();
        let sessions = Sessions::new(
            Arc::new(JobRegistry::open(JobStore::open(home.path()).unwrap())),
            home.path(),
            forge.clone(),
        );
        let processes = ProcessManager::new(ProcessLimits::default());
        let subagents = SubagentManager::new(
            crate::config::Config::default(),
            home.path().join("config.toml"),
            home.path().to_path_buf(),
            SubagentPool::Own,
        );
        let session = match work {
            Work::Managers(wait) => {
                load_waiting_on(&sessions, &root, |id| {
                    let (processes, subagents) = (Some(processes.clone()), Some(subagents.clone()));
                    SessionBackground::for_tests(id, wait, processes, subagents)
                })
                .await
            }
            Work::Tools => load(&sessions, &root, Load::Waking).await,
        };
        // As the session is added.
        session.report_activity();
        Self {
            root,
            _home: home,
            forge,
            sessions,
            session,
            processes,
            subagents,
        }
    }

    /// Waits until the session's clients heard `turns` of its turns start.
    async fn turns_started(&self, turns: usize) {
        let jobs = &self.sessions.jobs;
        let wait = async {
            loop {
                // Enabled before the look, so a start after it wakes this.
                let changed = jobs.changed();
                tokio::pin!(changed);
                changed.as_mut().enable();
                let started = seen(&self.session)
                    .into_iter()
                    .filter(|seen| *seen == Seen::TurnRunning)
                    .count();
                if started >= turns {
                    return;
                }
                changed.await;
            }
        };
        timeout(TEST_BUDGET, wait)
            .await
            .expect("the daemon starts a delivery turn");
    }

    /// Queues news of the watched pull request's checks `failed`, as the
    /// poll `minutes` after the watch reads them, and cancels the delivery
    /// turn it starts, the session's `turn`th, while that turn waits for the
    /// runtime held here: it ends before it begins its prompt and leaves the
    /// news, as a delivery turn that fails first does.
    async fn news_left_by_delivery(&self, failed: &[&str], minutes: u64, turn: usize) {
        let held = self.session.runtime.lock().await;
        self.sessions
            .pull_requests
            .fail_checks_for_tests(&self.forge, failed, minutes)
            .await;
        self.turns_started(turn).await;
        self.session.cancel_turn();
        drop(held);
    }

    /// Starts a turn, as a prompt or the daemon's delivery does, without
    /// running it.
    fn start_turn(&self) -> TurnId {
        let session = &self.session;
        let with = OpenWith {
            environment: session.environment.current(),
            setup: SessionSetup::default(),
            grant: self.sessions.jobs.grant_of(&session.job).unwrap(),
        };
        let mut slot = session.turn();
        match self.sessions.start_turn(
            session, &mut slot, &with, /*key*/ None, /*limit*/ None,
        ) {
            Ok(Entered::Started(turn)) => turn.id,
            _ => panic!("the idle session starts a turn"),
        }
    }

    /// Ends `turn` as `end`, as the turn's task does.
    fn end_turn(&self, turn: TurnId, end: TurnEnd) {
        let outcome = match end {
            TurnEnd::Completed => wire::TurnOutcome::Answered {
                text: "done".into(),
                stop: wire::AnswerStop::EndTurn,
            },
            _ => wire::TurnOutcome::Cancelled,
        };
        let _slot = self.session.turn();
        self.session.end_turn(turn, Some(outcome), end);
    }

    async fn start_process(&self, role: ProcessRole) {
        self.processes
            .start_as(LONG_RUNNING_COMMAND.into(), Path::new("."), None, role)
            .await
            .unwrap();
    }

    /// Runs a subagent of the session until its returned senders say it
    /// finished.
    fn run_subagent(&self) -> (watch::Sender<RunStatus>, watch::Sender<bool>) {
        let (status, status_rx) = watch::channel(RunStatus {
            state: RunState::Running,
            ..Default::default()
        });
        let (completion, completion_rx) = watch::channel(false);
        self.subagents.insert_handle_for_test(
            SUBAGENT,
            self.session.id.as_str(),
            AgentRunHandle::controlled_for_test(
                status_rx,
                completion_rx,
                rho_tools::cancellation::RunCancellation::new(),
            ),
        );
        (status, completion)
    }

    fn finish_subagent(&self) {
        self.subagents.insert_completed_status_for_test(
            SUBAGENT,
            self.session.id.as_str(),
            RunStatus {
                state: RunState::Ok,
                ..Default::default()
            },
        );
    }

    async fn shutdown(self) {
        self.processes.shutdown().await;
    }
}

const TASK: ProcessRole = ProcessRole::Task {
    keep_warm: KeepWarm::Off,
};

/// What a session started before its turn was cancelled.
#[derive(Clone, Copy, Debug)]
enum Left {
    Task,
    Service,
    Subagent,
}

// Covers: a session settles once its turn ends unless the daemon or its host
// will start a turn for work it waits on: a session whose prompts hold for
// their work is never woken, so a cancelled turn that left a task running
// still settles; a service never keeps a session from settling; a task or
// subagent the daemon or host delivers keeps it active. A wrong level hangs
// `wait` or returns it before the result. Owner: daemon session settle.
#[tokio::test]
async fn a_session_settles_unless_its_waker_will_start_a_turn() {
    use wire::SessionActivity::{Active, Settled};
    let cases = [
        (BackgroundWait::HoldPrompt, Left::Task, Settled),
        (BackgroundWait::HoldPrompt, Left::Subagent, Settled),
        (BackgroundWait::HostTurns, Left::Service, Settled),
        (BackgroundWait::HostTurns, Left::Task, Active),
        (BackgroundWait::HostTurns, Left::Subagent, Active),
        (BackgroundWait::HostWakes, Left::Service, Settled),
        (BackgroundWait::HostWakes, Left::Task, Active),
    ];
    for (wait, left, expected) in cases {
        let fixture = Fixture::new(wait).await;
        let turn = fixture.start_turn();
        let _subagent = match left {
            Left::Task => {
                fixture.start_process(TASK).await;
                None
            }
            Left::Service => {
                fixture.start_process(ProcessRole::Service).await;
                None
            }
            Left::Subagent => Some(fixture.run_subagent()),
        };
        fixture.end_turn(turn, TurnEnd::Cancelled);

        assert_eq!(
            activity(&fixture.session),
            Some(expected),
            "{wait:?} with a {left:?} left running"
        );
        fixture.shutdown().await;
    }
}

// Covers: a subagent that finishes while its session idles keeps the session
// active until the daemon's delivery turn took its result and ended, and
// `active` precedes the turn's `running`, `settled` follows its end. A
// `settled` between the finish and the delivery turn returns `wait` before
// the result. Owner: daemon session settle.
#[tokio::test]
async fn a_finished_subagent_keeps_its_session_active_until_its_delivery_ends() {
    use wire::{
        JobState::{Cancelled, Completed, Running},
        SessionActivity::{Active, Settled},
    };
    let fixture = Fixture::new(BackgroundWait::HostTurns).await;
    let prompt = fixture.start_turn();
    let (status, completion) = fixture.run_subagent();
    fixture.end_turn(prompt, TurnEnd::Cancelled);
    status.send_modify(|status| status.state = RunState::Ok);
    completion.send(true).unwrap();
    // As the background watcher does once the subagent ended.
    fixture.session.report_activity();
    let delivery = fixture.start_turn();
    fixture.subagents.observe(SUBAGENT);
    fixture.end_turn(delivery, TurnEnd::Completed);

    assert_eq!(
        heard(&fixture.session),
        vec![
            Heard::Activity(Settled),
            Heard::Activity(Active),
            Heard::State(Running),
            Heard::State(Cancelled),
            Heard::State(Running),
            Heard::State(Completed),
            Heard::Activity(Settled),
        ]
    );
    fixture.shutdown().await;
}

// Covers: a delivery turn that ended without taking its result, with
// nothing left running, is not started again, so the watcher gives up and
// the session settles instead of staying active forever; a give-up while
// the delivery turn still runs, or for a turn that is no longer the last,
// changes nothing, and the next turn makes the session active again.
// Owner: daemon session settle and idle wake.
#[tokio::test]
async fn a_delivery_that_left_its_result_settles_once_the_watcher_gives_up() {
    use wire::SessionActivity::{Active, Settled};
    let fixture = Fixture::new(BackgroundWait::HostTurns).await;
    fixture.finish_subagent();
    fixture.session.report_activity();
    let work = fixture.session.background.awaited().unwrap();
    let delivery = fixture.start_turn();
    // (whether it gave up, the activity heard after)
    let mut seen = vec![(
        try_give_up(&fixture.session, &work, &delivery),
        activity(&fixture.session),
    )];
    fixture.end_turn(delivery.clone(), TurnEnd::Cancelled);
    for turn in [TurnId::new("an earlier delivery"), delivery] {
        let gave_up = try_give_up(&fixture.session, &work, &turn);
        seen.push((gave_up, activity(&fixture.session)));
    }
    let next = fixture.start_turn();
    let rearmed = activity(&fixture.session);
    fixture.end_turn(next, TurnEnd::Cancelled);

    assert_eq!(
        (seen, rearmed),
        (
            vec![
                (false, Some(Active)),
                (false, Some(Active)),
                (true, Some(Settled))
            ],
            Some(Active)
        )
    );
    fixture.shutdown().await;
}

// Covers: news on a watched pull request, read through the inbox the
// session's tools hold, starts a delivery turn on the idle session, though
// no turn of its own started the watch's work; a quiet watch keeps nothing
// active; a delivery turn that left the news settles the session rather
// than loop, though every turn's end refreshes the session's tools; and
// later news wakes it again although its watcher gave up, which only the
// pull request inbox can do. A miss leaves the news unread until the user
// prompts, or starts delivery turns without end.
// Owner: daemon idle wake.
#[tokio::test]
async fn a_watched_pull_requests_news_starts_a_delivery_turn_on_the_idle_session() {
    use wire::SessionActivity::{Active, Settled};
    let fixture = Fixture::watching().await;
    let (sessions, session) = (&fixture.sessions, &fixture.session);
    let inbox = sessions
        .pull_requests
        .inbox(&session.id, fixture.root.path());
    watch_for_tests(&fixture.forge, &inbox).await;
    session.report_activity();
    let quiet = seen(session);
    sessions.watch_idle_results(session);

    let settled = || seen(session).last() == Some(&Seen::Activity(Settled));
    fixture.news_left_by_delivery(&["ci"], 1, 1).await;
    until(settled, "the watcher gives up on the news the turn left").await;
    let left = inbox.has_news();
    fixture.news_left_by_delivery(&["ci", "lint"], 2, 2).await;
    until(settled, "the watcher gives up again").await;

    assert_eq!(
        (quiet, left, seen(session)),
        (
            vec![Seen::Activity(Settled)],
            true,
            vec![
                Seen::Activity(Settled),
                Seen::Activity(Active),
                Seen::TurnRunning,
                Seen::TurnEnded,
                Seen::Activity(Settled),
                Seen::Activity(Active),
                Seen::TurnRunning,
                Seen::TurnEnded,
                Seen::Activity(Settled),
            ]
        )
    );
    fixture.shutdown().await;
}