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
use pretty_assertions::assert_eq;
use rho_protocol::{
    AnswerStop, Ask, InputAsker, InputRequestId, Question, QuestionAsk, Resolution, Selection,
};

use super::*;

/// A watch of a job in `state`, of turn `turn`.
fn watch(state: JobState, turn: &str) -> Watch {
    Watch {
        session: SessionId::new("s"),
        stream: StreamId::new("stream"),
        seq: Seq(0),
        state,
        turn: Some(TurnId::new(turn)),
        ended: None,
        inputs: Vec::new(),
        activity: None,
        reported: None,
    }
}

fn turn_ended(turn: &str, outcome: TurnOutcome) -> SessionEvent {
    SessionEvent::TurnEnded {
        turn: TurnId::new(turn),
        outcome,
    }
}

fn state(state: JobState, turn: &str) -> SessionEvent {
    SessionEvent::State {
        state,
        turn: Some(TurnId::new(turn)),
    }
}

fn answered(text: &str) -> TurnOutcome {
    TurnOutcome::Answered {
        text: text.into(),
        stop: AnswerStop::EndTurn,
    }
}

// Covers: a turn's outcome is reported by the turn's id: a reset's replay
// of the turn before's end does not pass for the running turn's, which
// `wait` reports once that turn ends, with a cursor at the last event seen.
// Owner: session-control wait.
#[test]
fn only_the_turn_that_ended_reports_an_outcome() {
    let mut watching = watch(JobState::Running, "t2");
    // The replay: the last turn's end, then the new turn starting.
    watching.push(Seq(0), &turn_ended("t1", answered("old")));
    watching.push(Seq(7), &state(JobState::Running, "t2"));
    watching.finish_replay(JobState::Running, Some(TurnId::new("t2")));
    let mut ended = watch(JobState::Running, "t2");
    ended.seq = watching.seq;
    ended.push(Seq(8), &turn_ended("t2", answered("new")));
    ended.push(Seq(9), &state(JobState::Completed, "t2"));

    assert_eq!(
        (watching.result(), ended.result()),
        (
            WaitResult {
                session: SessionId::new("s"),
                state: JobState::Running,
                done: false,
                outcome: None,
                turn: Some(TurnId::new("t2")),
                inputs: Vec::new(),
                cursor: Some("stream:7".into()),
            },
            WaitResult {
                session: SessionId::new("s"),
                state: JobState::Completed,
                done: true,
                outcome: Some(answered("new")),
                turn: Some(TurnId::new("t2")),
                inputs: Vec::new(),
                cursor: Some("stream:9".into()),
            },
        )
    );
}

// Covers: a wait that ends between the running turn's end and its terminal
// state does not acknowledge an outcome it has not returned; the end of
// another turn, as a reset replays, holds the cursor back for nothing.
// Owner: session-control wait checkpoint.
#[test]
fn a_wait_boundary_does_not_skip_an_unreturned_outcome() {
    for (ended, checkpoint) in [("t2", "stream:7"), ("t1", "stream:8")] {
        let mut watching = watch(JobState::Running, "t2");
        watching.seq = Seq(7);
        watching.push(Seq(8), &turn_ended(ended, answered("final answer")));
        assert_eq!(
            watching.result(),
            WaitResult {
                session: SessionId::new("s"),
                state: JobState::Running,
                done: false,
                outcome: None,
                turn: Some(TurnId::new("t2")),
                inputs: Vec::new(),
                cursor: Some(checkpoint.into()),
            },
            "the end of turn {ended}"
        );
    }
}

/// Question `id`, asked by `asker`.
fn question(id: u64, asker: InputAsker) -> InputRequest {
    InputRequest {
        id: InputRequestId(id),
        ask: Ask::Question(QuestionAsk {
            title: "Color".into(),
            reason: None,
            questions: vec![Question {
                id: "color".into(),
                prompt: "Which color?".into(),
                header: None,
                choices: Vec::new(),
                selection: Selection::One,
                allow_other: true,
                help: None,
                default: None,
                required: true,
            }],
        }),
        tool_call: None,
        timeout: None,
        asker,
        subagent: None,
    }
}

fn activity(state: SessionActivity) -> SessionEvent {
    SessionEvent::Activity { state }
}

// Covers: a turn waiting for input stops `wait` with the requests still
// open, and one a person answered drops out.
// Owner: session-control wait.
#[test]
fn a_turn_waiting_for_input_reports_its_open_requests() {
    let request = |id| question(id, InputAsker::Turn);
    let mut watching = watch(JobState::Running, "t1");
    for event in [
        SessionEvent::InputRequested {
            request: Box::new(request(1)),
        },
        SessionEvent::InputRequested {
            request: Box::new(request(2)),
        },
        SessionEvent::InputResolved {
            id: InputRequestId(1),
            resolution: Resolution::Answered,
        },
        state(JobState::WaitingForInput, "t1"),
    ] {
        watching.push(Seq(1), &event);
    }

    let waited = watching.result();
    assert_eq!(
        (waited.state, waited.done, waited.inputs),
        (JobState::WaitingForInput, true, vec![request(2)])
    );
}

/// What `wait` returns of a watch.
#[derive(Debug, PartialEq)]
struct Waited {
    done: bool,
    outcome: Option<TurnOutcome>,
    turn: Option<TurnId>,
    inputs: Vec<InputRequest>,
    cursor: Option<String>,
}

// Covers: `wait` is done once the session settles, not once a turn ends: a
// turn that ended while background work is still due (a result to deliver,
// or a task left running that may never let it settle) keeps it waiting
// but still returns the turn's outcome, with the cursor held before the
// turn's end so the next wait reads it again; the delivery turn's outcome
// then replaces it; a subagent question open at rest stops it with the
// question listed; one open while a turn runs does not stop it, but is
// listed, so the check of an answer finds it; a stream that never reports activity, as a subagent
// run's, is done once its turn ends.
// Owner: session-control wait.
#[test]
fn wait_is_done_once_the_session_settles() {
    let first_turn_ended = || {
        vec![
            activity(SessionActivity::Active),
            state(JobState::Running, "t1"),
            turn_ended("t1", answered("first")),
            state(JobState::Completed, "t1"),
        ]
    };
    let delivered = || {
        let mut events = first_turn_ended();
        events.extend([
            state(JobState::Running, "t2"),
            turn_ended("t2", answered("delivered")),
            state(JobState::Completed, "t2"),
            activity(SessionActivity::Settled),
        ]);
        events
    };
    let asked = || {
        let mut events = first_turn_ended();
        events.push(SessionEvent::InputRequested {
            request: Box::new(question(1, InputAsker::Subagent)),
        });
        events
    };
    let asked_while_running = || {
        vec![
            activity(SessionActivity::Active),
            state(JobState::Running, "t1"),
            SessionEvent::InputRequested {
                request: Box::new(question(1, InputAsker::Subagent)),
            },
        ]
    };
    let unreported = || {
        vec![
            state(JobState::Running, "t1"),
            turn_ended("t1", answered("first")),
            state(JobState::Completed, "t1"),
        ]
    };
    let cases = [
        (
            "a turn ended, background work still due",
            first_turn_ended(),
            Waited {
                done: false,
                outcome: Some(answered("first")),
                turn: Some(TurnId::new("t1")),
                inputs: Vec::new(),
                cursor: Some("stream:2".into()),
            },
        ),
        (
            "the delivery turn ended and the session settled",
            delivered(),
            Waited {
                done: true,
                outcome: Some(answered("delivered")),
                turn: Some(TurnId::new("t2")),
                inputs: Vec::new(),
                cursor: Some("stream:8".into()),
            },
        ),
        (
            "a subagent asks at rest",
            asked(),
            Waited {
                done: true,
                outcome: Some(answered("first")),
                turn: Some(TurnId::new("t1")),
                inputs: vec![question(1, InputAsker::Subagent)],
                cursor: Some("stream:5".into()),
            },
        ),
        (
            "a subagent asks while a turn runs",
            asked_while_running(),
            Waited {
                done: false,
                outcome: None,
                turn: Some(TurnId::new("t1")),
                inputs: vec![question(1, InputAsker::Subagent)],
                cursor: Some("stream:3".into()),
            },
        ),
        (
            "a stream that reports no activity",
            unreported(),
            Waited {
                done: true,
                outcome: Some(answered("first")),
                turn: Some(TurnId::new("t1")),
                inputs: Vec::new(),
                cursor: Some("stream:3".into()),
            },
        ),
    ];
    for (name, events, expected) in cases {
        let mut watching = watch(JobState::Idle, "t0");
        for (seq, event) in (1..).zip(&events) {
            watching.push(Seq(seq), event);
        }
        let WaitResult {
            done,
            outcome,
            turn,
            inputs,
            cursor,
            ..
        } = watching.result();
        assert_eq!(
            Waited {
                done,
                outcome,
                turn,
                inputs,
                cursor,
            },
            expected,
            "{name}"
        );
    }
}

// Covers: a follow reports each time the session is done with something
// new, and only then: the first look at a session done already, a request
// for a person, the session settling after a person answered it, and a
// second request while the first is open; events while it stays done with
// the same thing, as a title or a repeated activity, are not news again.
// A webhook watch POSTs each piece of news, so a repeat is a billed run.
// Owner: session-control follow news.
#[test]
fn a_follow_reports_each_new_reason_it_is_done_once() {
    let request = |id| SessionEvent::InputRequested {
        request: Box::new(question(id, InputAsker::Turn)),
    };
    let resolved = |id| SessionEvent::InputResolved {
        id: InputRequestId(id),
        resolution: Resolution::Answered,
    };
    let cases = [
        (
            "settled already, then quiet",
            JobState::Completed,
            vec![
                activity(SessionActivity::Settled),
                SessionEvent::TitleChanged {
                    title: "named".into(),
                },
                activity(SessionActivity::Settled),
            ],
            vec![(JobState::Completed, Vec::new())],
        ),
        (
            "asks, is answered, settles",
            JobState::Running,
            vec![
                activity(SessionActivity::Active),
                request(3),
                state(JobState::WaitingForInput, "t1"),
                resolved(3),
                state(JobState::Running, "t1"),
                turn_ended("t1", answered("done")),
                state(JobState::Completed, "t1"),
                activity(SessionActivity::Settled),
            ],
            vec![
                (
                    JobState::WaitingForInput,
                    vec![question(3, InputAsker::Turn)],
                ),
                // Answered, before the turn runs again: still done, with
                // nothing open, which a watch does not deliver.
                (JobState::WaitingForInput, Vec::new()),
                (JobState::Completed, Vec::new()),
            ],
        ),
        (
            "a second request while the first is open",
            JobState::Running,
            vec![
                request(3),
                state(JobState::WaitingForInput, "t1"),
                request(4),
            ],
            vec![
                (
                    JobState::WaitingForInput,
                    vec![question(3, InputAsker::Turn)],
                ),
                (
                    JobState::WaitingForInput,
                    vec![question(3, InputAsker::Turn), question(4, InputAsker::Turn)],
                ),
            ],
        ),
    ];
    for (name, start, events, expected) in cases {
        let mut watching = watch(start, "t1");
        let mut news = Vec::new();
        let mut look = |watching: &mut Watch| {
            if let Some(result) = watching.news() {
                news.push((result.state, result.inputs));
            }
        };
        look(&mut watching);
        for (seq, event) in (1..).zip(&events) {
            watching.push(Seq(seq), event);
            look(&mut watching);
        }
        assert_eq!(news, expected, "{name}");
    }
}