rpi-extensions 0.35.0

Rust-native (cdylib) plugin loader + AgentTool adapter for rpi — libloading + spawn_blocking ABI bridge
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
436
437
438
439
440
441
442
443
444
445
446
447
448
use super::*;
use serde_json::{json, Value};
use std::sync::atomic::AtomicUsize;
use tokio_util::sync::CancellationToken;

#[derive(Default)]
struct Host {
    calls: AtomicUsize,
    revoked: AtomicUsize,
    aborts: AtomicUsize,
    command_lists: AtomicUsize,
    stop_rounds: AtomicUsize,
    side_sinks: std::sync::Mutex<Vec<SideQuestionSink>>,
}
#[async_trait::async_trait]
impl RuntimeActionHost for Host {
    fn stop_round(&self, args: Value) -> Result<Value, String> {
        assert!(args.get("__rpiRuntimeKey").is_none());
        self.stop_rounds.fetch_add(1, Ordering::SeqCst);
        Ok(json!({"requested":true}))
    }

    async fn list_commands(&self, args: Value) -> Result<Value, String> {
        assert!(args.get("__rpiRuntimeKey").is_none());
        self.command_lists.fetch_add(1, Ordering::SeqCst);
        Ok(
            json!({"commands":[{"name":"integrated","description":"both actions coexist","source":"extension"}]}),
        )
    }

    async fn side_question(
        &self,
        owner: u64,
        args: Value,
        sink: SideQuestionSink,
    ) -> Result<Value, String> {
        assert!(args.get("__rpiRuntimeKey").is_none());
        sink(json!({"op":"register","id":"btw-test","type":"text","slot":"overlay"}))?;
        self.side_sinks.lock().unwrap().push(sink);
        Ok(json!({"owner":owner}))
    }
    async fn abort(&self, args: Value) -> Result<Value, String> {
        self.aborts.fetch_add(1, Ordering::SeqCst);
        assert!(
            args.get("__rpiRuntimeKey").is_none(),
            "capabilities must not reach action payloads"
        );
        // No run is active in this mock: mirror the idle-lane no-op contract so
        // the test also pins "idle abort is not an error".
        Ok(json!({"aborted": false}))
    }
    async fn send_message(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn send_user_message(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn append_entry(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn set_session_name(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn get_active_tools(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn set_active_tools(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn set_model(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn get_thinking_level(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn set_thinking_level(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn compact(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn get_system_prompt(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn new_session(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn fork(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn navigate_tree(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn switch_session(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn reload(&self, _: Value) -> Result<Value, String> {
        unreachable!()
    }
    async fn independent_run(&self, owner: u64, args: Value) -> Result<Value, String> {
        self.calls.fetch_add(1, Ordering::SeqCst);
        assert!(
            rpi_agent::tool_execution_scope::invocation_signal().is_some(),
            "dispatch must retain the originating invocation's cancellation"
        );
        assert!(
            args.get("__rpiRuntimeKey").is_none(),
            "capabilities must not reach action payloads"
        );
        Ok(json!({"owner":owner}))
    }
    async fn get_execution_context(&self, _: Value) -> Result<Value, String> {
        Ok(json!({"tools":["goal"]}))
    }
    fn cancel_child_agents(&self, _: u64) {
        self.revoked.fetch_add(1, Ordering::SeqCst);
    }
}
fn call(id: usize, args: Value) -> (i32, Value) {
    call_action(RuntimeActionId::IndependentRun, id, args)
}

fn call_action(action: RuntimeActionId, id: usize, args: Value) -> (i32, Value) {
    call_raw_action(action.into(), id, args)
}

fn call_raw_action(action: u32, id: usize, args: Value) -> (i32, Value) {
    let mut out = StbString::empty();
    let text = args.to_string();
    let rc = trampoline_runtime_action(
        action,
        StbStringRef::from_str(&text),
        &mut out,
        id as *mut c_void,
    );
    // SAFETY: the trampoline returned a live host allocation to its caller.
    let owned = unsafe {
        rpi_plugin_sdk::callbacks::OwnedString::from_raw(out, Some(crate::host_free_string))
    };
    (rc, serde_json::from_str(&owned.to_string_lossy()).unwrap())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn legacy_goal_observes_child_restriction() {
    let root = ActionBridge::new(Handle::current(), Arc::new(Host::default()));
    for (name, child, expected) in [
        (
            "librpi_goal",
            true,
            json!({"tools":["goal"],"childAgent":false}),
        ),
        (
            "librpi_goal-v0.3.0-staged",
            true,
            json!({"tools":["goal"],"childAgent":false}),
        ),
        ("librpi_goal", false, json!({"tools":["goal"]})),
        ("librpi_other", true, json!({"tools":["goal"]})),
    ] {
        let plugin = root.for_plugin(name);
        let invocation = plugin.for_invocation(child, CancellationToken::new());
        assert_eq!(
            call_action(
                RuntimeActionId::GetExecutionContext,
                invocation.user_data() as usize,
                json!({}),
            ),
            (0, expected)
        );
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn independent_runs_require_a_live_top_level_invocation_and_its_capability() {
    let host = Arc::new(Host::default());
    let root = ActionBridge::new(Handle::current(), host.clone());
    let plugin = root.for_plugin("research");
    let child = plugin.for_invocation(true, CancellationToken::new());
    let parent = plugin.for_invocation(false, CancellationToken::new());
    assert_eq!(
        call(plugin.user_data() as usize, json!({"op":"start"})).0,
        1
    );
    assert_eq!(
        call(parent.user_data() as usize, json!({"op":"start"})).0,
        1
    );
    assert_eq!(
        call(
            parent.user_data() as usize,
            json!({"op":"start","__rpiRuntimeKey":"guessed"})
        )
        .0,
        1
    );
    assert_eq!(
        call(
            child.user_data() as usize,
            json!({"op":"start","__rpiRuntimeKey":child.invocation_key()})
        )
        .0,
        1
    );
    // A child synchronous callback cannot spoof another invocation's ID/key.
    {
        let _guard = child.enter_invocation();
        assert_eq!(
            call(
                parent.user_data() as usize,
                json!({"op":"start","__rpiRuntimeKey":parent.invocation_key()})
            )
            .0,
            1
        );
    }
    let accepted = call(
        parent.user_data() as usize,
        json!({"op":"start","__rpiRuntimeKey":parent.invocation_key()}),
    );
    assert_eq!(accepted.0, 0);
    assert_eq!(
        accepted.1["owner"],
        plugin.plugin_identity.as_ref().unwrap().0
    );
    assert_eq!(host.calls.load(Ordering::SeqCst), 1);
    parent.invocation.as_ref().unwrap().signal.cancel();
    assert_eq!(
        call(
            parent.user_data() as usize,
            json!({"op":"start","__rpiRuntimeKey":parent.invocation_key()})
        )
        .0,
        1
    );
}

#[test]
fn retired_action_id_is_rejected_at_the_abi_boundary() {
    assert_eq!(
        call_raw_action(24, 0, json!({"op":"start"})),
        (2, json!({"error":"unknown runtime action id 24"})),
    );
}

#[tokio::test]
async fn dropped_candidate_and_unknown_handles_are_rejected_without_pointer_dereference() {
    let host = Arc::new(Host::default());
    let old = ActionBridge::new(Handle::current(), host.clone());
    let old_id = old.user_data() as usize;
    let candidate = ActionBridge::new(Handle::current(), host);
    let candidate_id = candidate.user_data() as usize;
    drop(candidate); // Failed reload: old bridge is still active.
    assert!(ActionBridge::resolve_handle(old_id).is_some());
    assert!(ActionBridge::resolve_handle(candidate_id).is_none());
    assert_eq!(
        call(candidate_id, json!({})).1["error"],
        "expired runtime handle"
    );
    assert_eq!(
        call(usize::MAX, json!({})).1["error"],
        "expired runtime handle"
    );
    let replacement = ActionBridge::new(Handle::current(), Arc::new(Host::default()));
    assert_ne!(replacement.user_data() as usize, candidate_id);
}

#[tokio::test]
async fn owner_scan_does_not_upgrade_or_pin_unrelated_bridges() {
    let root = ActionBridge::new(Handle::current(), Arc::new(Host::default()));
    let plugin = root.for_plugin("retiring");
    let invocation = plugin.for_invocation(false, CancellationToken::new());
    let own = [&root, &plugin, &invocation];
    for bridge in own {
        bridge.user_data();
        assert_eq!(Arc::strong_count(bridge), 1);
    }

    let unrelated = ActionBridge::new(Handle::current(), Arc::new(Host::default()));
    let unrelated_plugin = unrelated.for_plugin("unrelated");
    let unrelated_invocation = unrelated_plugin.for_invocation(false, CancellationToken::new());
    let unrelated_ids: Vec<_> = [&unrelated, &unrelated_plugin, &unrelated_invocation]
        .into_iter()
        .map(|bridge| bridge.user_data() as usize)
        .collect();

    // Hold the exact scan used by invalidate at the former race window. The
    // global upgrade-before-owner-filter pinned other sessions' invocations
    // until this vector was dropped, delaying immediate handle retirement.
    let live = root.bridges_for_owner();
    assert_eq!(live.len(), own.len());
    assert!(live
        .iter()
        .all(|bridge| Arc::ptr_eq(&bridge.active, &root.active)));
    for bridge in own {
        assert_eq!(Arc::strong_count(bridge), 2);
    }
    for bridge in [&unrelated, &unrelated_plugin, &unrelated_invocation] {
        assert_eq!(Arc::strong_count(bridge), 1);
    }
    drop(unrelated_invocation);
    drop(unrelated_plugin);
    drop(unrelated);
    for id in unrelated_ids {
        assert!(ActionBridge::resolve_handle(id).is_none());
    }
    drop(live);
    for bridge in own {
        assert_eq!(Arc::strong_count(bridge), 1);
    }
}

#[tokio::test]
async fn finishing_an_invocation_does_not_unload_its_plugin_owner() {
    let host = Arc::new(Host::default());
    let root = ActionBridge::new(Handle::current(), host.clone());
    let plugin = root.for_plugin("research");
    let invocation = plugin.for_invocation(false, CancellationToken::new());
    let id = invocation.user_data() as usize;
    drop(invocation);
    assert!(ActionBridge::resolve_handle(id).is_none());
    assert_eq!(host.revoked.load(Ordering::SeqCst), 0);
    root.invalidate();
    assert_eq!(host.revoked.load(Ordering::SeqCst), 1);
}

mod cancel_wait;
mod cleanup;
mod driver;
mod pacing;
mod streaming;

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn side_questions_use_plugin_owner_and_stale_sinks_cannot_resurrect_panels() {
    let host = Arc::new(Host::default());
    let root = ActionBridge::new(Handle::current(), host.clone());
    let plugin = root.for_plugin("btw");
    let invoke = |bridge: &Arc<ActionBridge>| {
        let mut out = StbString::empty();
        let rc = trampoline_runtime_action(
            RuntimeActionId::SideQuestion.into(),
            StbStringRef::from_str(r#"{"op":"start","question":"q","owner":999}"#),
            &mut out,
            bridge.user_data(),
        );
        // SAFETY: trampoline transfers its output allocation to this caller.
        let output = unsafe {
            rpi_plugin_sdk::callbacks::OwnedString::from_raw(out, Some(crate::host_free_string))
        };
        (
            rc,
            serde_json::from_str::<Value>(&output.to_string_lossy()).unwrap(),
        )
    };
    assert_eq!(invoke(&root).0, 1);
    let invocation = plugin.for_invocation(false, CancellationToken::new());
    assert_eq!(invoke(&invocation).0, 1);
    let value = invoke(&plugin);
    assert_eq!(value.0, 0);
    assert_eq!(value.1["owner"], plugin.plugin_identity.as_ref().unwrap().0);
    assert!(!root.extension_status_mailbox().panels().is_empty());
    root.invalidate();
    assert!(root.extension_status_mailbox().panels().is_empty());
    let sink = host.side_sinks.lock().unwrap()[0].clone();
    assert!(sink(json!({"op":"register","id":"btw-test","type":"text"})).is_err());
    assert!(root.extension_status_mailbox().panels().is_empty());
}

/// A newly appended action id must also bump the trampoline's `max_action_id`,
/// or it dispatches to `2` (unknown id) instead of reaching the host.
///
/// This pins the highest id end-to-end through the FFI entrypoint, which is the
/// one place the enum value, the `TryFrom` table and the ceiling have to agree.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn abort_reaches_the_host_and_is_not_blocked_by_the_action_id_ceiling() {
    let host = Arc::new(Host::default());
    let root = ActionBridge::new(Handle::current(), host.clone());
    let plugin = root.for_plugin("stopper");
    let mut out = StbString::empty();
    let rc = trampoline_runtime_action(
        RuntimeActionId::Abort.into(),
        StbStringRef::from_str("{}"),
        &mut out,
        plugin.user_data(),
    );
    // SAFETY: the trampoline returned a live host allocation to its caller.
    let owned = unsafe {
        rpi_plugin_sdk::callbacks::OwnedString::from_raw(out, Some(crate::host_free_string))
    };
    let value: Value = serde_json::from_str(&owned.to_string_lossy()).unwrap();
    assert_eq!(rc, 0, "Abort must dispatch, not fail as an unknown id");
    assert_eq!(value, json!({"aborted": false}));
    assert_eq!(host.aborts.load(Ordering::SeqCst), 1);
}

/// Independently developed wire actions must survive integration together.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn integrated_commands_abort_dialog_and_stop_round_dispatch_without_renumbering() {
    let host = Arc::new(Host::default());
    let root = ActionBridge::new(Handle::current(), host.clone());
    let plugin = root.for_plugin("integrated-actions");
    for (id, expected) in [
        (
            26,
            json!({"commands":[{"name":"integrated","description":"both actions coexist","source":"extension"}]}),
        ),
        (27, json!({"aborted":false})),
        (28, json!({"requests":[]})),
        (29, json!({"requested":true})),
    ] {
        let mut out = StbString::empty();
        let rc = trampoline_runtime_action(
            id,
            StbStringRef::from_str("{}"),
            &mut out,
            plugin.user_data(),
        );
        assert_eq!(rc, 0, "action {id} must reach its own host method");
        // SAFETY: a successful trampoline call returns a live host allocation.
        let owned = unsafe {
            rpi_plugin_sdk::callbacks::OwnedString::from_raw(out, Some(crate::host_free_string))
        };
        let value: Value = serde_json::from_str(&owned.to_string_lossy()).unwrap();
        assert_eq!(value, expected);
    }
    assert!(root.ui_dialog_mailbox().external_takeover());
    root.ui_dialog_mailbox().release_takeover();
    assert!(!root.ui_dialog_mailbox().external_takeover());
    assert_eq!(host.command_lists.load(Ordering::SeqCst), 1);
    assert_eq!(host.aborts.load(Ordering::SeqCst), 1);
    assert_eq!(host.stop_rounds.load(Ordering::SeqCst), 1);
    let mut out = StbString::empty();
    assert_eq!(
        trampoline_runtime_action(
            RuntimeActionId::MAX + 1,
            StbStringRef::from_str("{}"),
            &mut out,
            plugin.user_data()
        ),
        2
    );
    assert_eq!(host.command_lists.load(Ordering::SeqCst), 1);
    assert_eq!(host.aborts.load(Ordering::SeqCst), 1);
    assert_eq!(host.stop_rounds.load(Ordering::SeqCst), 1);
}

#[path = "../../../rpi-agent/tests/common/mod.rs"]
mod agent_common;
mod tool_program;