rpi-cli 0.26.1

Terminal coding-agent CLI (the `rpi` binary) built on the rpi-* library crates
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
use super::*;
use rpi_ai::providers::faux::{faux_assistant_message, FauxProvider, FauxScript};
use rpi_ai::types::StopReason;
use rpi_harness::types::AgentHarnessOptions;

async fn parent(script: FauxScript) -> AgentHarness {
    let provider = FauxProvider::new(script);
    let session = Session::new(
        Arc::new(InMemorySessionStorage::new(
            SessionMetadata {
                id: "parent".into(),
                created_at: 0,
                parent_session_id: None,
            },
            Arc::new(SystemClock),
            Arc::new(DefaultIdGenerator::new()),
        )),
        None,
    );
    AgentHarness::create(AgentHarnessOptions {
        model: provider.default_model().clone(),
        models: vec![provider],
        session,
        ..Default::default()
    })
    .await
    .unwrap()
}
fn job(owner: u64) -> Arc<Job> {
    Arc::new(Job {
        owner,
        cancel: CancellationToken::new(),
        invocation_signal: None,
        state: Mutex::new(State::default()),
    })
}
fn delta(text: String) -> AgentEvent {
    let partial = Arc::new(faux_assistant_message("", StopReason::Stop));
    AgentEvent::MessageUpdate {
        message_id: None,
        message: partial.clone(),
        assistant_message_event: AssistantMessageEvent::TextDelta {
            content_index: 0,
            delta: text,
            partial,
        },
    }
}
async fn wait(manager: &ChildAgents, parent: &AgentHarness, id: &str) -> Value {
    tokio::time::timeout(Duration::from_secs(3), async {
        loop {
            let value = manager
                .handle(parent, 7, json!({"op":"poll","childId":id}))
                .await
                .unwrap();
            if value["status"] != "running" {
                return value;
            }
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
    })
    .await
    .expect("child must finish")
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn child_reports_and_owner_scoped_disposal_are_independent_of_parent_transcript() {
    let parent = parent(FauxScript::new().with_text("child report")).await;
    let manager = ChildAgents::default();
    let started = manager
        .handle(&parent, 7, json!({"op":"start","task":"report"}))
        .await
        .unwrap();
    let id = started["childId"].as_str().unwrap();
    assert_ne!(id, parent.session().storage().metadata().id);
    let final_value = wait(&manager, &parent, id).await;
    assert_eq!(final_value["result"]["text"], "child report");
    assert_eq!(final_value["status"], "done");
    assert!(parent.get_tip_id().await.unwrap().is_none());
    for op in ["poll", "cancel", "dispose"] {
        assert!(manager
            .handle(&parent, 8, json!({"op":op,"childId":id}))
            .await
            .is_err());
    }
    manager
        .handle(&parent, 7, json!({"op":"dispose","childId":id}))
        .await
        .unwrap();
    assert!(manager.jobs.lock().unwrap().is_empty());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn cancellation_and_unload_stop_pending_children_and_reject_stale_starts() {
    let parent = parent(
        FauxScript::new()
            .with_text("slow report")
            .with_tokens_per_second(0.1),
    )
    .await;
    let manager = ChildAgents::default();
    let started = manager
        .handle(&parent, 7, json!({"op":"start","task":"report"}))
        .await
        .unwrap();
    let id = started["childId"].as_str().unwrap();
    manager
        .handle(&parent, 7, json!({"op":"cancel","childId":id}))
        .await
        .unwrap();
    assert!(wait(&manager, &parent, id).await["error"]
        .as_str()
        .unwrap()
        .contains("cancelled"));
    manager.cancel_owner(7);
    assert!(manager.jobs.lock().unwrap().is_empty());
    assert!(manager
        .handle(&parent, 7, json!({"op":"start","task":"report"}))
        .await
        .unwrap_err()
        .contains("unloaded"));
    tokio::time::timeout(Duration::from_secs(3), async {
        while manager.active.load(Ordering::Acquire) != 0 {
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
    })
    .await
    .unwrap();
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn explicit_timeout_and_parent_scope_cancel_are_observable() {
    for cancel_parent in [false, true] {
        let parent = parent(
            FauxScript::new()
                .with_text("slow report")
                .with_tokens_per_second(0.1),
        )
        .await;
        let job = job(7);
        let child = parent
            .create_child(
                AgentHarnessOptions::default().session,
                Arc::new(Emitter(job.clone())),
                1,
                None,
            )
            .await
            .unwrap();
        if cancel_parent {
            child.scope.cancel();
        }
        run_child(child, job.clone(), "report".into(), 1, 0).await;
        let state = job.state.lock().unwrap();
        let error = state.error.as_deref().unwrap();
        assert!(
            error.contains(if cancel_parent {
                "parent run ended"
            } else {
                "timed out"
            }),
            "{error}"
        );
    }
}

#[test]
fn text_and_event_budgets_are_enforced_before_queue_growth() {
    let job = job(1);
    job.observe(delta("a".repeat(MAX_OUTPUT)));
    job.observe(delta("b".into()));
    assert!(job.cancel.is_cancelled());
    let state = job.state.lock().unwrap();
    assert_eq!(state.bytes, MAX_OUTPUT);
    assert_eq!(state.deltas.len(), 1);
    drop(state);
    let job = super::tests::job(1);
    job.state.lock().unwrap().events = MAX_EVENTS;
    job.observe(delta(String::new()));
    assert!(job.cancel.is_cancelled());
    assert!(job.state.lock().unwrap().deltas.is_empty());
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn poll_drains_bounded_batches_before_delivering_a_terminal_result() {
    let parent = parent(FauxScript::new()).await;
    let manager = ChildAgents::default();
    let job = job(7);
    for _ in 0..4 {
        job.observe(delta("x".repeat(32 * 1024)));
    }
    job.state.lock().unwrap().result = Some(json!({"status":"completed","text":"report"}));
    manager.jobs.lock().unwrap().insert("c".into(), job);
    let a = manager
        .handle(&parent, 7, json!({"op":"poll","childId":"c"}))
        .await
        .unwrap();
    assert_eq!(a["status"], "running");
    assert!(a["result"].is_null());
    assert_eq!(a["deltas"].as_array().unwrap().len(), 2);
    let b = manager
        .handle(&parent, 7, json!({"op":"poll","childId":"c"}))
        .await
        .unwrap();
    assert_eq!(b["status"], "done");
    assert_eq!(b["result"]["text"], "report");
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn capacity_is_bounded_even_while_disposed_children_are_cleaning_up() {
    let parent = parent(FauxScript::new()).await;
    let manager = ChildAgents::default();
    manager.active.store(MAX_JOBS, Ordering::Release);
    assert!(manager
        .handle(&parent, 7, json!({"op":"start","task":"report"}))
        .await
        .unwrap_err()
        .contains("cleanup"));
    assert!(manager.jobs.lock().unwrap().is_empty());
    manager.active.store(0, Ordering::Release);
    for n in 0..MAX_JOBS {
        manager.jobs.lock().unwrap().insert(format!("c{n}"), job(7));
    }
    assert!(manager
        .handle(&parent, 7, json!({"op":"start","task":"report"}))
        .await
        .unwrap_err()
        .contains("capacity"));
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn roster_reports_live_children_and_stop_terminates_by_id() {
    let parent = parent(
        FauxScript::new()
            .with_text("slow report")
            .with_tokens_per_second(0.1),
    )
    .await;
    let manager = ChildAgents::default();
    let started = manager
        .handle(
            &parent,
            7,
            json!({"op":"start","task":"survey the repo","role":"scout"}),
        )
        .await
        .unwrap();
    let id = started["childId"].as_str().unwrap();

    let roster = manager.roster();
    let row = roster
        .iter()
        .find(|row| row["childId"] == id)
        .expect("the running child must be listed");
    assert_eq!(row["status"], "running");
    assert_eq!(row["role"], "scout");
    assert_eq!(row["taskPreview"], "survey the repo");
    assert!(row["startedMs"].as_u64().unwrap() > 0);
    assert!(row["finishedMs"].is_null());

    let stopped = manager.stop(id).unwrap();
    assert_eq!(stopped["status"], "cancelled");
    let row = manager
        .roster()
        .into_iter()
        .find(|row| row["childId"] == id)
        .expect("the terminated child stays in the roster until dispose");
    assert_eq!(row["status"], "failed");
    assert!(row["finishedMs"].as_u64().unwrap() > 0);
    assert!(manager.stop("nope").is_err());

    manager
        .handle(&parent, 7, json!({"op":"dispose","childId":id}))
        .await
        .unwrap();
    assert!(manager.roster().is_empty());
    tokio::time::timeout(Duration::from_secs(3), async {
        while manager.active.load(Ordering::Acquire) != 0 {
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
    })
    .await
    .unwrap();
}

/// Stopping an already-terminal child reports `already-finished` and must not
/// rewrite its recorded outcome or terminal timestamp: the roster is a history,
/// not a live view a late Stop click can corrupt.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stop_on_a_finished_child_reports_already_finished_without_rewriting_it() {
    let parent = parent(FauxScript::new().with_text("child report")).await;
    let manager = ChildAgents::default();
    let started = manager
        .handle(&parent, 7, json!({"op":"start","task":"report"}))
        .await
        .unwrap();
    let id = started["childId"].as_str().unwrap();
    let finished = wait(&manager, &parent, id).await;
    assert_eq!(finished["status"], "done");

    let before = manager
        .roster()
        .into_iter()
        .find(|row| row["childId"] == id)
        .expect("finished child stays in the roster until dispose");
    assert_eq!(before["status"], "done");
    let finished_ms = before["finishedMs"].clone();
    assert!(finished_ms.as_u64().unwrap() > 0);

    let stopped = manager.stop(id).unwrap();
    assert_eq!(stopped["status"], "already-finished");
    let after = manager
        .roster()
        .into_iter()
        .find(|row| row["childId"] == id)
        .expect("still listed");
    assert_eq!(
        after["status"], "done",
        "stop must not flip a done child to failed"
    );
    assert_eq!(
        after["finishedMs"], finished_ms,
        "the terminal timestamp is history"
    );

    manager
        .handle(&parent, 7, json!({"op":"dispose","childId":id}))
        .await
        .unwrap();
    tokio::time::timeout(Duration::from_secs(3), async {
        while manager.active.load(Ordering::Acquire) != 0 {
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
    })
    .await
    .unwrap();
}

/// `finished_ms` is first-write-wins: a later `fail()` (for example a Stop
/// click) must not move the timestamp of a child that already settled.
#[test]
fn mark_finished_keeps_the_first_terminal_timestamp() {
    let child = job(7);
    {
        let mut state = child.state.lock().unwrap();
        state.finished_ms = Some(1_000);
    }
    child.mark_finished();
    assert_eq!(
        child.state.lock().unwrap().finished_ms,
        Some(1_000),
        "mark_finished must not overwrite an existing terminal timestamp"
    );

    let fresh = job(7);
    fresh.mark_finished();
    let first = fresh.state.lock().unwrap().finished_ms;
    assert!(first.is_some());
    fresh.mark_finished();
    assert_eq!(fresh.state.lock().unwrap().finished_ms, first);
}

/// `stop` is the HOST path: it is intentionally not owner-scoped, unlike the
/// plugin path (`handle_existing` / `cancel`), which rejects a foreign owner.
/// Pin both halves so the missing owner filter stays a documented decision
/// instead of an accident.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn stop_is_host_privileged_while_the_plugin_path_stays_owner_scoped() {
    let parent = parent(
        FauxScript::new()
            .with_text("slow report")
            .with_tokens_per_second(0.1),
    )
    .await;
    let manager = ChildAgents::default();
    let started = manager
        .handle(&parent, 7, json!({"op":"start","task":"report"}))
        .await
        .unwrap();
    let id = started["childId"].as_str().unwrap();

    // Plugin path: a different owner cannot cancel it.
    assert!(
        manager
            .handle(&parent, 8, json!({"op":"cancel","childId":id}))
            .await
            .is_err(),
        "a foreign owner must not cancel another plugin's child"
    );
    assert_eq!(manager.roster()[0]["status"], "running");

    // Host path: the host itself may stop any child it holds.
    assert_eq!(manager.stop(id).unwrap()["status"], "cancelled");
    assert_eq!(manager.roster()[0]["status"], "failed");

    manager
        .handle(&parent, 7, json!({"op":"dispose","childId":id}))
        .await
        .unwrap();
    tokio::time::timeout(Duration::from_secs(3), async {
        while manager.active.load(Ordering::Acquire) != 0 {
            tokio::time::sleep(Duration::from_millis(5)).await;
        }
    })
    .await
    .unwrap();
}

#[test]
fn child_limits_reject_wrong_types_and_out_of_range_values() {
    for value in [json!(-1), json!(0), json!(33), json!("8"), Value::Null] {
        assert!(bounded_number(&json!({"maxTurns":value}), "maxTurns", 8, 1, 32).is_err());
    }
    assert_eq!(bounded_number(&json!({}), "maxTurns", 8, 1, 32).unwrap(), 8);
}

mod invocation;
mod security;