rpi-cli 0.33.0

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
//! Real authenticated IPC driving the existing owned/durable native facade.
use super::*;
use crate::host::{
    client::{encode_frame, read_frame, write_frame},
    lifecycle::LifecycleReceipt,
};

fn execute_command(intent: &LifecycleRequest) -> wire::Command {
    let request = serde_json::from_value(json!(intent)).unwrap();
    match intent.action {
        LifecycleAction::CreateProfile { .. } => wire::Command::AdministrativeCreate { request },
        LifecycleAction::RestoreProfile { .. } => wire::Command::AdministrativeRestore { request },
        LifecycleAction::Close { .. } => wire::Command::AdministrativeClose { request },
        LifecycleAction::CancelSubmitted { .. } => wire::Command::AdministrativeCancel { request },
        LifecycleAction::Stop {} => wire::Command::AdministrativeStop { request },
        _ => panic!("not an enabled execution intent"),
    }
}
fn decoded(data: serde_json::Value) -> LifecycleReceipt {
    serde_json::from_value(data["receipt"].clone()).unwrap()
}
async fn revision(client: &mut HostClient, agent: AgentId) -> u64 {
    client
        .call(wire::Command::Inspect {
            agent_id: serde_json::from_value(json!(agent)).unwrap(),
        })
        .await
        .unwrap()["revision"]
        .as_u64()
        .unwrap()
}

#[tokio::test]
async fn ipc_profile_execution_is_guarded_idempotent_and_reconciles_old_versions_before_loading() {
    let root = tempfile::tempdir().unwrap();
    let profile = input(root.path(), "execute");
    let spec = profile.spec.clone();
    let scope = ControlScopeId::new();
    let control = control(root.path());
    let mut daemon = HostDaemon::open_configured(
        root.path().join("host"),
        HostConfig::default(),
        control.clone(),
        vec![],
        Some(policy(scope, BTreeSet::from([spec.id]), vec![profile])),
    )
    .await
    .unwrap();
    let reference = daemon
        .administration()
        .unwrap()
        .profile_references()
        .next()
        .unwrap();
    let create = request(
        &daemon,
        daemon.administration().unwrap(),
        LifecycleAction::CreateProfile { profile: reference },
    );
    let mut client = connect(&control).await;
    let created = decoded(client.call(execute_command(&create)).await.unwrap());
    succeeded(&created);
    assert_eq!(created.request, create);
    assert_eq!(
        decoded(client.call(execute_command(&create)).await.unwrap()),
        created
    );
    assert_eq!(
        client.call(wire::Command::Status {}).await.unwrap()["resident"],
        1
    );
    let mut wrong = create.clone();
    wrong.request_id = RequestId::new();
    wrong.scope = ControlScopeId::new();
    assert_eq!(
        client.call(execute_command(&wrong)).await.unwrap_err(),
        ClientError::Rejected(wire::ErrorCode::Unauthorized)
    );
    wrong = create.clone();
    wrong.request_id = RequestId::new();
    wrong.epoch = crate::host::HostEpoch::new();
    assert_eq!(
        client.call(execute_command(&wrong)).await.unwrap_err(),
        ClientError::Rejected(wire::ErrorCode::StaleEpoch)
    );
    let current = revision(&mut client, spec.id).await;
    let bad_close = request(
        &daemon,
        daemon.administration().unwrap(),
        LifecycleAction::Close {
            agent_id: spec.id,
            revision: current + 1,
        },
    );
    assert_eq!(
        client.call(execute_command(&bad_close)).await.unwrap_err(),
        ClientError::Rejected(wire::ErrorCode::StaleRevision)
    );
    assert_eq!(
        client.call(wire::Command::Status {}).await.unwrap()["resident"],
        1
    );
    let close = request(
        &daemon,
        daemon.administration().unwrap(),
        LifecycleAction::Close {
            agent_id: spec.id,
            revision: current,
        },
    );
    let closed = decoded(client.call(execute_command(&close)).await.unwrap());
    succeeded(&closed);
    assert_eq!(
        decoded(client.call(execute_command(&close)).await.unwrap()),
        closed
    );
    assert_eq!(
        client.call(wire::Command::Status {}).await.unwrap()["resident"],
        0
    );
    let restore = request(
        &daemon,
        daemon.administration().unwrap(),
        LifecycleAction::RestoreProfile {
            profile: reference,
            revision: revision(&mut client, spec.id).await,
        },
    );
    succeeded(&decoded(
        client.call(execute_command(&restore)).await.unwrap(),
    ));
    assert_eq!(
        client.call(wire::Command::Status {}).await.unwrap()["resident"],
        1
    );
    drop(client);
    daemon.shutdown().await.unwrap();
    let before = std::fs::read(spec.session.as_ref().unwrap().path.clone()).unwrap();
    let mut profile = input(root.path(), "execute");
    profile.spec = spec.clone();
    let next_control = super::control(root.path());
    let mut next = HostDaemon::open_configured(
        root.path().join("host"),
        HostConfig::default(),
        next_control.clone(),
        vec![],
        Some(policy(scope, BTreeSet::from([spec.id]), vec![profile])),
    )
    .await
    .unwrap();
    let new_ref = next
        .administration()
        .unwrap()
        .profile_references()
        .next()
        .unwrap();
    assert_ne!(new_ref, reference);
    let mut client = connect(&next_control).await;
    assert_eq!(
        decoded(client.call(execute_command(&create)).await.unwrap()),
        created
    );
    assert_eq!(
        decoded(client.call(execute_command(&restore)).await.unwrap()).request,
        restore
    );
    let mut altered = create.clone();
    altered.action = LifecycleAction::CreateProfile { profile: new_ref };
    assert_eq!(
        client.call(execute_command(&altered)).await.unwrap_err(),
        ClientError::Rejected(wire::ErrorCode::Conflict)
    );
    assert_eq!(
        client.call(wire::Command::Status {}).await.unwrap()["resident"],
        0
    );
    assert_eq!(
        std::fs::read(spec.session.as_ref().unwrap().path.clone()).unwrap(),
        before
    );
    drop(client);
    next.shutdown().await.unwrap();
}

#[tokio::test]
async fn ipc_default_grant_denies_all_execution_and_raw_mismatched_stop_never_executes() {
    for all_permissions in [false, true] {
        let root = tempfile::tempdir().unwrap();
        let profile = input(root.path(), "deny");
        let spec = profile.spec.clone();
        let control = control(root.path());
        let mut config = policy(
            ControlScopeId::new(),
            BTreeSet::from([spec.id]),
            vec![profile],
        );
        if !all_permissions {
            config.permissions = AdministrativePermissions::default();
        }
        let mut daemon = HostDaemon::open_configured(
            root.path().join("host"),
            HostConfig::default(),
            control.clone(),
            vec![],
            Some(config),
        )
        .await
        .unwrap();
        let admin = daemon.administration().unwrap();
        let reference = admin.profile_references().next().unwrap();
        let mut client = connect(&control).await;
        if !all_permissions {
            for action in [
                LifecycleAction::CreateProfile { profile: reference },
                LifecycleAction::RestoreProfile {
                    profile: reference,
                    revision: 1,
                },
                LifecycleAction::Close {
                    agent_id: spec.id,
                    revision: 1,
                },
                LifecycleAction::CancelSubmitted {
                    agent_id: spec.id,
                    revision: 1,
                    submitted_request_id: RequestId::new(),
                },
                LifecycleAction::Stop {},
            ] {
                let intent = request(&daemon, admin, action);
                assert_eq!(
                    client.call(execute_command(&intent)).await.unwrap_err(),
                    ClientError::Rejected(wire::ErrorCode::Unauthorized)
                );
            }
        }
        // Bypass client.valid: even a policy granting Stop must not execute a
        // Stop smuggled through the administrative_create transport operation.
        let mut raw = tokio::net::UnixStream::connect(&control.socket_path)
            .await
            .unwrap();
        let hello = wire::Hello {
            protocol: wire::PROTOCOL.into(),
            version: wire::Version::default(),
            host_id: control.host_id,
            token: control.token.clone(),
        };
        write_frame(
            &mut raw,
            &encode_frame(&hello, wire::MAX_FRAME_BYTES).unwrap(),
        )
        .await
        .unwrap();
        let welcome: wire::Welcome = read_frame(&mut raw, wire::MAX_FRAME_BYTES).await.unwrap();
        let intent = request(&daemon, admin, LifecycleAction::Stop {});
        let malformed = wire::Request {
            call_id: wire::CallId::new(),
            epoch: welcome.epoch,
            command: wire::Command::AdministrativeCreate {
                request: serde_json::from_value(json!(intent)).unwrap(),
            },
        };
        write_frame(
            &mut raw,
            &encode_frame(&malformed, wire::MAX_FRAME_BYTES).unwrap(),
        )
        .await
        .unwrap();
        let response: wire::Response = read_frame(&mut raw, wire::MAX_FRAME_BYTES).await.unwrap();
        assert!(matches!(
            response.reply,
            wire::Reply::Error {
                code: wire::ErrorCode::InvalidRequest
            }
        ));
        assert_eq!(
            client.call(wire::Command::Status {}).await.unwrap()["registered"],
            0
        );
        assert!(!daemon.service().is_stopping());
        assert_eq!(admin.active_jobs(), 0);
        assert!(!spec.session.unwrap().path.exists());
        for capability in [
            wire::Capability::Create,
            wire::Capability::Restore,
            wire::Capability::Close,
            wire::Capability::Cancel,
            wire::Capability::Stop,
        ] {
            assert!(!welcome.supports(capability));
        }
        drop(raw);
        drop(client);
        daemon.shutdown().await.unwrap();
    }
}

#[tokio::test]
async fn ipc_deadline_drops_only_waiter_owned_create_keeps_capacity_and_finishes_after_fence() {
    let root = tempfile::tempdir().unwrap();
    let first = input(root.path(), "late-first");
    let first_spec = first.spec.clone();
    let second = input(root.path(), "late-second");
    let second_spec = second.spec.clone();
    let mut control = control(root.path());
    control.request_timeout = Duration::from_millis(500);
    let mut config = policy(
        ControlScopeId::new(),
        BTreeSet::from([first_spec.id, second_spec.id]),
        vec![first, second],
    );
    config.max_jobs = NonZeroUsize::new(1).unwrap();
    let mut daemon = HostDaemon::open_configured(
        root.path().join("host"),
        HostConfig::default(),
        control.clone(),
        vec![],
        Some(config),
    )
    .await
    .unwrap();
    let service = daemon.service().clone();
    let (entered, held) = tokio::sync::oneshot::channel();
    let (release, released) = tokio::sync::oneshot::channel();
    let fence = tokio::spawn(async move {
        service
            .with_stop_reservation(false, move |reservation| {
                Box::pin(async move {
                    let _ = entered.send(());
                    let _ = released.await;
                    drop(reservation);
                    Ok(())
                })
            })
            .await
            .unwrap();
    });
    held.await.unwrap();
    let admin = daemon.administration().unwrap();
    let refs: BTreeMap<_, _> = admin
        .profile_references()
        .map(|r| (r.agent_id, r))
        .collect();
    let create = request(
        &daemon,
        admin,
        LifecycleAction::CreateProfile {
            profile: refs[&first_spec.id],
        },
    );
    let mut client = connect(&control).await;
    let failure = client.call(execute_command(&create)).await.unwrap_err();
    assert!(
        matches!(failure, ClientError::OutcomeUnknown { request_id, .. } if request_id.to_string() == create.request_id.to_string())
    );
    assert_eq!(admin.active_jobs(), 1);
    assert!(!first_spec.session.as_ref().unwrap().path.exists());
    let mut observer = connect(&control).await;
    let other = request(
        &daemon,
        admin,
        LifecycleAction::CreateProfile {
            profile: refs[&second_spec.id],
        },
    );
    assert_eq!(
        observer.call(execute_command(&other)).await.unwrap_err(),
        ClientError::Rejected(wire::ErrorCode::Capacity)
    );
    assert_eq!(
        observer.call(receipt_command(&create)).await.unwrap()["found"],
        false
    );
    assert!(!second_spec.session.as_ref().unwrap().path.exists());
    release.send(()).unwrap();
    fence.await.unwrap();
    let completed = tokio::time::timeout(Duration::from_secs(15), async {
        loop {
            let data = observer.call(receipt_command(&create)).await.unwrap();
            if data["found"] == true {
                let receipt = decoded(data);
                if matches!(receipt.status, LifecycleState::Succeeded { .. }) {
                    break receipt;
                }
                assert!(matches!(receipt.status, LifecycleState::Prepared {}));
            }
            tokio::task::yield_now().await;
        }
    })
    .await
    .unwrap();
    assert_eq!(
        decoded(observer.call(execute_command(&create)).await.unwrap()),
        completed
    );
    assert_eq!(
        observer.call(wire::Command::Status {}).await.unwrap()["resident"],
        1
    );
    assert_eq!(admin.active_jobs(), 0);
    drop(client);
    drop(observer);
    daemon.shutdown().await.unwrap();
}

#[path = "daemon_stop_tests.rs"]
mod stop;