rho-coding-agent 3.11.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
use std::{sync::mpsc, time::Duration};

use futures_util::FutureExt;
use pretty_assertions::assert_eq;

use super::{
    test_support::{answer, Turn},
    Lending, ModelSlot,
};
use crate::acp_runtime::agent_tools::{AgentRequest, Readied};

#[derive(Clone, Copy, Debug, PartialEq)]
enum Runtime {
    Rho,
    Acp,
}

/// The agent's request to the session's served tools, as it stands.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Request {
    None,
    /// Outstanding, its call running: the agent waits on its answer.
    Blocking,
    /// Answered while its call runs on, as a call the bridge parked past
    /// one MCP call's wait answers that it is still running.
    Parked,
    /// Outstanding while a tool call of the agent's own is in progress, as
    /// a Claude Code Task subagent runs beside its agent's wait.
    BesideOwnCall,
}

// Covers: a budgeted turn lends its slot only while it does not run alone;
// a Claude Code or Antigravity turn only while, besides, its agent waits on
// the served tools alone: not with no request, not while a call runs on
// past its answer (parked), and not while a call of the agent's own is in
// progress; a Rho turn whenever it does not run alone.
// Kills: `agent_allows && !runs_alone` as `||` (the alone, blocking row
// lends); the `WhileAgentBlocked` arm as `true` (the no request, parked and
// own call rows lend); `waits_on_served_tools` as `calls > 0` (the parked
// row lends); `&& self.agent_calls == 0` dropped (the own call row lends);
// `!jobs.runs_alone(job)` as `jobs.runs_alone(job)` (the rows the agent
// allows flip: Acp blocking, alone and not, and both Rho rows).
// Owner: daemon shared agent budget, started-session turns.
#[tokio::test(flavor = "current_thread")]
async fn a_turn_lends_its_slot_only_as_its_runtime_allows() {
    // (runtime, runs alone, the agent's request, how a parked request's
    // answer readied, lent)
    let cases = [
        (Runtime::Acp, true, Request::None, None, false),
        (Runtime::Acp, true, Request::Blocking, None, false),
        (Runtime::Acp, false, Request::None, None, false),
        (Runtime::Acp, false, Request::Blocking, None, true),
        (
            Runtime::Acp,
            false,
            Request::Parked,
            Some(Readied::Ready),
            false,
        ),
        (Runtime::Acp, false, Request::BesideOwnCall, None, false),
        (Runtime::Rho, true, Request::None, None, false),
        (Runtime::Rho, false, Request::None, None, true),
    ];
    let mut seen = Vec::new();
    for &(runtime, alone, request, _, _) in &cases {
        let (turn, budgeted) = Turn::admitted().await;
        if !alone {
            turn.start_child();
        }
        let activity = turn.model.activity();
        let running = match request {
            Request::None => None,
            Request::Blocking | Request::Parked | Request::BesideOwnCall => {
                Some(activity.call_running())
            }
        };
        let (outstanding, readied) = match request {
            Request::None => (None, None),
            Request::Blocking => (Some(turn.request()), None),
            Request::BesideOwnCall => {
                activity.set_agent_calls(1);
                (Some(turn.request()), None)
            }
            // Answered before the turn could lend its slot, so it takes
            // none back.
            Request::Parked => (None, answer(turn.request()).now_or_never()),
        };
        let lending = match runtime {
            Runtime::Rho => Lending::WhileWaiting,
            Runtime::Acp => Lending::WhileAgentBlocked,
        };
        let lend = turn.run(budgeted, lending);
        tokio::pin!(lend);
        assert!(lend.as_mut().now_or_never().is_none());
        seen.push((runtime, alone, request, readied, turn.slot_free()));
        drop((running, outstanding));
    }

    assert_eq!(seen, cases);
}

// Covers: a Claude Code or Antigravity turn that does not run alone lends
// its slot once its agent is blocked on a request to the served tools; the
// request then takes a slot back for the turn before its answer goes back,
// in line behind the run that took the lent one, and the turn keeps it
// rather than lending it again for the answered request.
// Kills: the lend loop not waking on the agent's activity
// (`activity.changed()` dropped from its `select!`: nothing lent once the
// request comes); the answer not taking the slot back
// (`slot.retake().await` dropped from `ModelSlot::request`: ready while the
// other run holds the slot); `retake` not waiting in line (the same);
// `turn.held = Some(permits)` dropped (the slot free again once taken
// back).
// Owner: daemon shared agent budget, started-session turns on another
// runtime.
#[tokio::test(flavor = "current_thread")]
async fn a_request_takes_the_lent_slot_back_before_its_answer() {
    let (turn, budgeted) = Turn::admitted().await;
    turn.start_child();
    let running = turn.run(budgeted, Lending::WhileAgentBlocked);
    tokio::pin!(running);
    assert!(running.as_mut().now_or_never().is_none());
    let lent_before_the_request = turn.slot_free();

    let request = turn.request();
    assert!(running.as_mut().now_or_never().is_none());
    let other = turn.take_lent_slot();
    let mut answer = answer(request);
    let back_while_taken = answer.as_mut().now_or_never();
    drop(other);
    let back_once_freed = answer.as_mut().now_or_never();
    assert!(running.as_mut().now_or_never().is_none());
    let lent_after = turn.slot_free();

    assert_eq!(
        (
            lent_before_the_request,
            back_while_taken,
            back_once_freed,
            lent_after
        ),
        (false, None, Some(Readied::Ready), false)
    );
}

// Covers: requests answered together while their turn lent its slot take
// one slot back for it between them: the second waits behind the first,
// then finds the slot taken back and waits no more.
// Kills: the `retaking` lock dropped (the second request waits in line for
// a second slot the one-slot budget never frees, so its answer never goes
// back).
// Owner: daemon shared agent budget, started-session turns on another
// runtime.
#[tokio::test(flavor = "current_thread")]
async fn requests_answered_together_take_one_slot_back() {
    let (turn, budgeted) = Turn::admitted().await;
    turn.start_child();
    let running = turn.run(budgeted, Lending::WhileAgentBlocked);
    tokio::pin!(running);
    let (first, second) = (turn.request(), turn.request());
    assert!(running.as_mut().now_or_never().is_none());
    let other = turn.take_lent_slot();
    let (mut first_back, mut second_back) = (answer(first), answer(second));
    assert!(first_back.as_mut().now_or_never().is_none());
    assert!(second_back.as_mut().now_or_never().is_none());

    drop(other);
    let back = (
        first_back.as_mut().now_or_never(),
        second_back.as_mut().now_or_never(),
    );

    assert_eq!(
        (back, turn.slot_free()),
        ((Some(Readied::Ready), Some(Readied::Ready)), false)
    );
}

// Covers: a request takes nothing back while its turn holds its slot, nor
// for a turn the budget does not hold, as a person's: its answer goes back
// at once, and the held turn takes no second slot.
// Kills: `lent()` answering while the turn holds its slot (the request
// waits in line for a second slot the one-slot budget never frees).
// Owner: daemon shared agent budget, started-session turns on another
// runtime.
#[tokio::test(flavor = "current_thread")]
async fn a_request_takes_nothing_back_unless_its_turn_lent_its_slot() {
    let (turn, budgeted) = Turn::admitted().await;
    let running = turn.run(budgeted, Lending::WhileAgentBlocked);
    tokio::pin!(running);
    let request = turn.request();
    assert!(running.as_mut().now_or_never().is_none());

    let held = answer(request).now_or_never();
    let unbudgeted = ModelSlot::default().retake().now_or_never().is_some();

    assert_eq!(
        (held, unbudgeted, turn.slot_free()),
        (Some(Readied::Ready), true, false)
    );
}

// Covers: a turn's lending looks again as the agent and the slot change,
// not only as jobs do: once the agent's own call ends while it waits on a
// request, the turn lends; once one request's answer took the slot back,
// in line, while the agent still waits on another, the turn lends it
// again at once.
// Kills: the lend loop not waking on the agent's activity
// (`activity.changed()` dropped: still held once the own call ends);
// `self.retaken.notify_waiters()` dropped from `retake`, or `retaken`
// dropped from the loop's `select!` (the slot taken back stays held while
// the agent waits on the second request, until a job changes).
// Owner: daemon shared agent budget, started-session turns on another
// runtime.
#[tokio::test(flavor = "current_thread")]
async fn a_turn_lends_again_as_its_agent_and_slot_change() {
    let (turn, budgeted) = Turn::admitted().await;
    turn.start_child();
    let activity = turn.model.activity();
    activity.set_agent_calls(1);
    let first = turn.request();
    let running = turn.run(budgeted, Lending::WhileAgentBlocked);
    tokio::pin!(running);
    assert!(running.as_mut().now_or_never().is_none());
    let lent_beside_own_call = turn.slot_free();

    activity.set_agent_calls(0);
    assert!(running.as_mut().now_or_never().is_none());
    let lent_once_it_ended = turn.slot_free();

    let _second = turn.request();
    let other = turn.take_lent_slot();
    let mut first_back = answer(first);
    // Waits in line behind the other run while the turn looks again at the
    // first request no longer blocking: nothing to lend.
    assert!(first_back.as_mut().now_or_never().is_none());
    assert!(running.as_mut().now_or_never().is_none());
    drop(other);
    let first_readied = first_back.as_mut().now_or_never();
    assert!(running.as_mut().now_or_never().is_none());
    let lent_again = turn.slot_free();

    assert_eq!(
        (
            lent_beside_own_call,
            lent_once_it_ended,
            first_readied,
            lent_again
        ),
        (false, true, Some(Readied::Ready), true)
    );
}

// Covers: the turn decides to lend under the lock a request's answer looks
// for a lent slot under, reading the agent's activity as it stands then: an
// answer that stops blocking the agent while the turn decides holds off
// its `retake` until the turn decided, and the turn, seeing the agent no
// longer blocked, keeps its slot, so the answer finds it held. Forced on two
// threads: the turn's decision pauses until the answer stopped blocking.
// Kills: `give_back_if` asking `lend` before it takes the state lock (the
// lock is free while the turn decides, so the answer's `retake` could pass,
// find the slot held, take nothing back, and the turn then lend it with the
// agent running): `locked_while_deciding` catches it, false, since the
// interleaving it allows may not show in `lent` on one run; `lends_now`
// reading the activity before the decision (lent with no request
// outstanding).
// Owner: daemon shared agent budget, started-session turns on another
// runtime.
#[tokio::test(flavor = "current_thread")]
async fn an_answer_and_the_turns_lending_take_turns_on_the_slot() {
    let (turn, budgeted) = Turn::admitted().await;
    turn.start_child();
    let _budgeted = turn.model.budget(budgeted);
    let (model, jobs, job) = (&*turn.model, &*turn.jobs, &turn.job);
    let blocking = model.activity().request_outstanding();
    let (deciding, decides) = mpsc::channel();
    let (unblocked, answered) = mpsc::channel();

    let (locked_while_deciding, lent) = std::thread::scope(|scope| {
        let decision = scope.spawn(move || {
            model.give_back_if(move || {
                deciding.send(()).unwrap();
                answered.recv().unwrap();
                Lending::WhileAgentBlocked.lends_now(model.activity(), jobs, job)
            })
        });
        decides.recv().unwrap();
        let locked = model.turn.try_lock().is_err();
        // The answer's first step: the agent is blocked no more.
        drop(blocking);
        unblocked.send(()).unwrap();
        (locked, decision.join().unwrap())
    });
    // Its second: it finds the slot held, and goes back at once.
    let retook = model.retake().now_or_never().is_some();

    assert_eq!(
        (locked_while_deciding, lent, retook, turn.slot_free()),
        (true, false, true, false)
    );
}

// Covers: an answer whose transport's deadline passes while it waits in
// line for the turn's slot goes back anyway, the turn holding none: the
// daemon has the run show that the turn runs over the budget, and the turn
// stays lent, so a later answer takes a slot back.
// Kills: `overdue` saying nothing while the turn holds no slot (an
// over-budget turn nobody sees); the deadline ignored (the answer waits
// for the slot the other run holds, past the transport's cut-off).
// Owner: daemon shared agent budget, answers past a transport's deadline.
#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn an_answer_past_its_deadline_runs_the_turn_over_the_budget() {
    let (turn, budgeted) = Turn::admitted().await;
    turn.start_child();
    let running = turn.run(budgeted, Lending::WhileAgentBlocked);
    tokio::pin!(running);
    let request = turn.request();
    assert!(running.as_mut().now_or_never().is_none());
    let _other = turn.take_lent_slot();

    let deadline = tokio::time::Instant::now() + Duration::from_secs(1);
    let readied = tokio::time::timeout(
        Duration::from_secs(2),
        tokio::task::unconstrained(request.answer_by(Some(deadline))),
    )
    .await
    .map(|readied| matches!(readied, Readied::Overdue { notice: Some(_) }));

    assert_eq!((readied, turn.model.lent().is_some()), (Ok(true), true));
}

/// What the run is shown of an answer readied by its transport's deadline.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Shown {
    /// It went back readied.
    Ready,
    /// It went back unreadied, with the notice that the turn runs over the
    /// budget.
    Notice,
    /// It went back unreadied, and the run is shown nothing.
    Nothing,
}

/// Readies `request`'s answer by a deadline a second away, which passes
/// while another run holds the slot the turn lent: how it went back, and
/// how long it waited.
async fn shown_past_deadline(request: AgentRequest) -> (Shown, Duration) {
    let asked = tokio::time::Instant::now();
    let deadline = asked + Duration::from_secs(1);
    let shown = match tokio::task::unconstrained(request.answer_by(Some(deadline))).await {
        Readied::Ready => Shown::Ready,
        Readied::Overdue { notice: Some(_) } => Shown::Notice,
        Readied::Overdue { notice: None } => Shown::Nothing,
    };
    (shown, asked.elapsed())
}

/// What happens between two answers, the first of which misses its
/// deadline.
#[derive(Clone, Copy, Debug, PartialEq)]
enum Between {
    /// Nothing: the other run still holds the only slot.
    Nothing,
    /// The other run gives the slot back.
    SlotFreed,
    /// An answer takes a slot back, and the turn lends it again.
    SlotTakenBack,
}

// Covers: a turn that runs over the budget stays over it until it actually
// gets a slot back, and meanwhile its answers wait in no line, so each of
// agy's later `await`s is not held to its deadline again: the second
// answer goes back at once, whether it finds the slot taken (still over
// the budget) or free (back within it, holding it). A turn that held a
// slot again waits in line again, and says once more that it runs over.
// Kills: `retake` waiting in line while the turn runs over the budget (the
// nothing row's second answer waits out its deadline); the over-budget
// `retake` taking no free slot (the freed row stays over the budget);
// `Budgeted::hold` not clearing `over_budget` (the slot taken back row's
// second answer goes back at once, unannounced).
// Owner: daemon shared agent budget, answers past a transport's deadline.
#[tokio::test(flavor = "current_thread", start_paused = true)]
async fn a_turn_over_the_budget_answers_at_once_until_it_gets_a_slot_again() {
    let missed = (Shown::Notice, Duration::from_secs(1));
    let at_once = (Shown::Ready, Duration::ZERO);
    let cases = [
        (
            Between::Nothing,
            [missed, at_once],
            /*over_budget*/ true,
        ),
        (Between::SlotFreed, [missed, at_once], false),
        (Between::SlotTakenBack, [missed, missed], true),
    ];
    let mut seen = Vec::new();
    for (between, _, _) in cases {
        let (turn, budgeted) = Turn::admitted().await;
        turn.start_child();
        let running = turn.run(budgeted, Lending::WhileAgentBlocked);
        tokio::pin!(running);
        let request = turn.request();
        assert!(running.as_mut().now_or_never().is_none());
        let mut other = Some(turn.take_lent_slot());
        let first = shown_past_deadline(request).await;

        let request = turn.request();
        match between {
            Between::Nothing => {}
            Between::SlotFreed => drop(other.take()),
            Between::SlotTakenBack => {
                drop(other.take());
                let back = answer(turn.request()).now_or_never();
                assert_eq!(back, Some(Readied::Ready));
                // Lends it again for the request still outstanding.
                assert!(running.as_mut().now_or_never().is_none());
                other = Some(turn.take_lent_slot());
            }
        }
        let second = shown_past_deadline(request).await;
        let over_budget = turn.model.lent().is_some();
        drop(other);
        seen.push((between, [first, second], over_budget));
    }

    assert_eq!(seen, cases);
}