roder-core 0.4.0

Agentic software development tools and SDKs for Roder.
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use roder_api::extension::{ExtensionRegistryBuilder, ToolProviderId};
use roder_api::tools::{
    ToolCall, ToolContributor, ToolExecutionBarrier as ToolScheduler, ToolExecutionContext,
    ToolExecutor, ToolRegistry, ToolResult, ToolSpec,
};
use roder_ext_jsonl_thread_store::store::JsonlThreadStoreFactory;
use serde_json::json;
use tokio::sync::Barrier;

use super::*;
use crate::runtime::RuntimeConfig;

#[tokio::test]
async fn readers_overlap_and_writer_excludes_readers_and_writers() {
    let scheduler = Arc::new(ToolScheduler::default());
    let rendezvous = Arc::new(Barrier::new(3));
    let release = Arc::new(Barrier::new(3));
    let mut readers = Vec::new();
    for _ in 0..2 {
        let scheduler = scheduler.clone();
        let rendezvous = rendezvous.clone();
        let release = release.clone();
        readers.push(tokio::spawn(async move {
            let _guard = scheduler.acquire(ToolExecutionMode::SharedRead).await;
            rendezvous.wait().await;
            release.wait().await;
        }));
    }
    tokio::time::timeout(Duration::from_secs(2), rendezvous.wait())
        .await
        .expect("both readers must acquire the barrier concurrently");
    let writer = scheduler.acquire(ToolExecutionMode::Exclusive);
    tokio::pin!(writer);
    assert!(futures::poll!(&mut writer).is_pending());
    release.wait().await;
    let guard = tokio::time::timeout(Duration::from_secs(2), writer)
        .await
        .unwrap();
    let mut other_writer = Box::pin(scheduler.acquire(ToolExecutionMode::Exclusive));
    let mut read = Box::pin(scheduler.acquire(ToolExecutionMode::SharedRead));
    assert!(futures::poll!(&mut other_writer).is_pending());
    assert!(futures::poll!(&mut read).is_pending());
    drop(guard);
    // Cancel the queued writer so the read can acquire.
    drop(other_writer);
    tokio::time::timeout(Duration::from_secs(2), read)
        .await
        .expect("dropping a waiting writer must release the queue");
    for reader in readers {
        reader.await.unwrap();
    }
}

#[tokio::test]
async fn cancellation_releases_held_and_waiting_barriers() {
    let scheduler = Arc::new(ToolScheduler::default());
    let entered = Arc::new(tokio::sync::Notify::new());
    let task = {
        let scheduler = scheduler.clone();
        let entered = entered.clone();
        tokio::spawn(async move {
            let _guard = scheduler.acquire(ToolExecutionMode::Exclusive).await;
            entered.notify_one();
            futures::future::pending::<()>().await;
        })
    };
    entered.notified().await;
    let mut waiter = Box::pin(scheduler.acquire(ToolExecutionMode::Exclusive));
    assert!(futures::poll!(&mut waiter).is_pending());
    drop(waiter);
    task.abort();
    let _ = task.await;
    tokio::time::timeout(
        Duration::from_secs(2),
        scheduler.acquire(ToolExecutionMode::SharedRead),
    )
    .await
    .expect("aborted tasks must not retain scheduling guards");
}

#[derive(Default)]
struct ProbeState {
    readers: AtomicUsize,
    writers: AtomicUsize,
    log: Mutex<Vec<String>>,
}

struct ReadProbe {
    state: Arc<ProbeState>,
    pair: Arc<Barrier>,
    hold: Option<(Arc<tokio::sync::Notify>, Arc<tokio::sync::Notify>)>,
}

fn spec(name: &str) -> ToolSpec {
    ToolSpec {
        name: name.into(),
        description: "scheduling test".into(),
        parameters: json!({
            "type": "object",
            "properties": {"pair": {"type": "boolean"}},
            "required": ["pair"],
            "additionalProperties": false
        }),
    }
}

fn result(call: ToolCall) -> ToolResult {
    ToolResult {
        id: call.id,
        name: call.name,
        text: "ok".into(),
        data: json!({}),
        is_error: false,
    }
}

#[async_trait::async_trait]
impl ToolExecutor for ReadProbe {
    fn spec(&self) -> ToolSpec {
        spec("read_probe")
    }

    fn execution_mode(&self) -> ToolExecutionMode {
        ToolExecutionMode::SharedRead
    }

    async fn execute(&self, _: ToolExecutionContext, call: ToolCall) -> anyhow::Result<ToolResult> {
        assert_eq!(self.state.writers.load(Ordering::SeqCst), 0);
        self.state.readers.fetch_add(1, Ordering::SeqCst);
        self.state
            .log
            .lock()
            .unwrap()
            .push(format!("{}:start", call.id));
        if let Some((started, release)) = &self.hold {
            started.notify_one();
            release.notified().await;
        }
        if call.arguments["pair"] == true {
            self.pair.wait().await;
            assert_eq!(self.state.writers.load(Ordering::SeqCst), 0);
        }
        self.state
            .log
            .lock()
            .unwrap()
            .push(format!("{}:end", call.id));
        self.state.readers.fetch_sub(1, Ordering::SeqCst);
        Ok(result(call))
    }
}

/// Deliberately does not opt in: an unknown executor must remain exclusive.
struct UnknownProbe(Arc<ProbeState>);

#[async_trait::async_trait]
impl ToolExecutor for UnknownProbe {
    fn spec(&self) -> ToolSpec {
        spec("unknown_probe")
    }

    async fn execute(&self, _: ToolExecutionContext, call: ToolCall) -> anyhow::Result<ToolResult> {
        assert_eq!(self.0.readers.load(Ordering::SeqCst), 0);
        assert_eq!(self.0.writers.fetch_add(1, Ordering::SeqCst), 0);
        self.0
            .log
            .lock()
            .unwrap()
            .push(format!("{}:start", call.id));
        tokio::task::yield_now().await;
        assert_eq!(self.0.readers.load(Ordering::SeqCst), 0);
        self.0.log.lock().unwrap().push(format!("{}:end", call.id));
        self.0.writers.fetch_sub(1, Ordering::SeqCst);
        Ok(result(call))
    }
}

struct CoordinatingProbe;

#[async_trait::async_trait]
impl ToolExecutor for CoordinatingProbe {
    fn spec(&self) -> ToolSpec {
        spec("coordinator_probe")
    }

    fn execution_mode(&self) -> ToolExecutionMode {
        ToolExecutionMode::Coordinator
    }

    async fn execute(
        &self,
        ctx: ToolExecutionContext,
        call: ToolCall,
    ) -> anyhow::Result<ToolResult> {
        let barrier = ctx.handles.execution_barrier.as_ref().unwrap();
        let _child_guard = barrier.acquire(ToolExecutionMode::Exclusive).await;
        Ok(result(call))
    }
}

struct Probes(Vec<Arc<dyn ToolExecutor>>);

impl ToolContributor for Probes {
    fn id(&self) -> ToolProviderId {
        "scheduling-probes".into()
    }

    fn contribute(&self, registry: &mut ToolRegistry) -> anyhow::Result<()> {
        for tool in &self.0 {
            registry.register(tool.clone())?;
        }
        Ok(())
    }
}

fn call(id: &str, name: &str, pair: bool) -> ToolCallCompleted {
    ToolCallCompleted {
        id: id.into(),
        name: name.into(),
        arguments: json!({"pair": pair}).to_string(),
    }
}

struct DenyRead(Arc<AtomicUsize>);

#[async_trait::async_trait]
impl roder_api::context::PolicyContributor for DenyRead {
    fn id(&self) -> roder_api::extension::PolicyContributorId {
        "deny-scheduling-probe".into()
    }

    async fn review_tool(
        &self,
        _: roder_api::context::PolicyReview,
    ) -> anyhow::Result<roder_api::context::PolicyContribution> {
        self.0.fetch_add(1, Ordering::SeqCst);
        Ok(roder_api::context::PolicyContribution::Deny {
            reason: "a scheduling hint does not grant permission".into(),
        })
    }
}

#[tokio::test]
async fn shared_read_metadata_does_not_bypass_policy_review() {
    let directory = tempfile::tempdir().unwrap();
    let state = Arc::new(ProbeState::default());
    let reviews = Arc::new(AtomicUsize::new(0));
    let mut builder = ExtensionRegistryBuilder::new();
    builder.inference_engine(Arc::new(crate::fake_provider::FakeInferenceEngine));
    builder.thread_store_factory(Arc::new(JsonlThreadStoreFactory {
        base_path: directory.path().to_path_buf(),
    }));
    builder.tool_contributor(Arc::new(Probes(vec![Arc::new(ReadProbe {
        state: state.clone(),
        pair: Arc::new(Barrier::new(1)),
        hold: None,
    })])));
    builder.policy_contributor(Arc::new(DenyRead(reviews.clone())));
    let config = RuntimeConfig {
        policy_mode: roder_api::policy_mode::PolicyMode::Bypass,
        tool_allowlist: vec!["read_probe".into()],
        ..RuntimeConfig::default()
    };
    let runtime = Arc::new(Runtime::new(builder.build().unwrap(), config).unwrap());
    let thread = runtime.create_thread(None).await.unwrap().thread_id;
    let result = runtime
        .route_tool_call(
            &thread,
            &"policy-turn".into(),
            call("read", "read_probe", false),
            None,
            None,
            None,
        )
        .await
        .unwrap();
    assert!(result.is_error);
    assert!(
        result
            .result
            .contains("scheduling hint does not grant permission")
    );
    assert_eq!(reviews.load(Ordering::SeqCst), 1);
    assert!(state.log.lock().unwrap().is_empty());
}

#[tokio::test]
async fn runtime_batches_adjacent_reads_without_crossing_mutations() {
    let directory = tempfile::tempdir().unwrap();
    let state = Arc::new(ProbeState::default());
    let unknown = Arc::new(UnknownProbe(state.clone()));
    assert_eq!(unknown.execution_mode(), ToolExecutionMode::Exclusive);
    let mut builder = ExtensionRegistryBuilder::new();
    builder.inference_engine(Arc::new(crate::fake_provider::FakeInferenceEngine));
    builder.thread_store_factory(Arc::new(JsonlThreadStoreFactory {
        base_path: directory.path().to_path_buf(),
    }));
    builder.tool_contributor(Arc::new(Probes(vec![
        Arc::new(ReadProbe {
            state: state.clone(),
            pair: Arc::new(Barrier::new(2)),
            hold: None,
        }),
        unknown,
        Arc::new(CoordinatingProbe),
    ])));
    let config = RuntimeConfig {
        policy_mode: roder_api::policy_mode::PolicyMode::Bypass,
        tool_allowlist: vec![
            "read_probe".into(),
            "unknown_probe".into(),
            "coordinator_probe".into(),
        ],
        ..RuntimeConfig::default()
    };
    let runtime = Arc::new(Runtime::new(builder.build().unwrap(), config).unwrap());
    let thread = runtime.create_thread(None).await.unwrap().thread_id;
    assert_eq!(
        runtime.tool_execution_mode("unregistered"),
        ToolExecutionMode::Exclusive
    );
    let results = tokio::time::timeout(
        Duration::from_secs(5),
        runtime.execute_scheduled_tools(
            &thread,
            &"scheduling-turn".into(),
            vec![
                call("a", "read_probe", true),
                call("b", "read_probe", true),
                call("w", "unknown_probe", false),
                call("w2", "unknown_probe", false),
                call("c", "read_probe", false),
            ],
            true,
            None,
            None,
        ),
    )
    .await
    .expect("shared reads must overlap, not deadlock")
    .unwrap();
    assert_eq!(
        results
            .iter()
            .map(|result| result.id.as_str())
            .collect::<Vec<_>>(),
        ["a", "b", "w", "w2", "c"]
    );
    assert!(results.iter().all(|result| !result.is_error), "{results:?}");
    let log = state.log.lock().unwrap();
    let index = |entry| log.iter().position(|value| value == entry).unwrap();
    assert!(index("a:end") < index("w:start"));
    assert!(index("b:end") < index("w:start"));
    assert!(index("w:end") < index("w2:start"));
    assert!(index("w2:end") < index("c:start"));
    drop(log);
    let result = tokio::time::timeout(
        Duration::from_secs(2),
        runtime.route_tool_call(
            &thread,
            &"scheduling-turn".into(),
            call("nested", "coordinator_probe", false),
            None,
            None,
            None,
        ),
    )
    .await
    .expect("a coordinator must not hold the barrier while awaiting a child")
    .unwrap();
    assert!(!result.is_error, "{result:?}");
}

#[tokio::test]
async fn independent_runtime_dispatches_share_the_same_resource_barrier() {
    let directory = tempfile::tempdir().unwrap();
    let state = Arc::new(ProbeState::default());
    let started = Arc::new(tokio::sync::Notify::new());
    let release = Arc::new(tokio::sync::Notify::new());
    let mut builder = ExtensionRegistryBuilder::new();
    builder.inference_engine(Arc::new(crate::fake_provider::FakeInferenceEngine));
    builder.thread_store_factory(Arc::new(JsonlThreadStoreFactory {
        base_path: directory.path().to_path_buf(),
    }));
    builder.tool_contributor(Arc::new(Probes(vec![
        Arc::new(ReadProbe {
            state: state.clone(),
            pair: Arc::new(Barrier::new(1)),
            hold: Some((started.clone(), release.clone())),
        }),
        Arc::new(UnknownProbe(state)),
    ])));
    let runtime = Arc::new(
        Runtime::new(
            builder.build().unwrap(),
            RuntimeConfig {
                policy_mode: roder_api::policy_mode::PolicyMode::Bypass,
                tool_allowlist: vec!["read_probe".into(), "unknown_probe".into()],
                ..RuntimeConfig::default()
            },
        )
        .unwrap(),
    );
    let thread = runtime.create_thread(None).await.unwrap().thread_id;
    let read = tokio::spawn({
        let runtime = runtime.clone();
        let thread = thread.clone();
        async move {
            runtime
                .route_tool_call(
                    &thread,
                    &"read-turn".into(),
                    call("read", "read_probe", false),
                    None,
                    None,
                    None,
                )
                .await
        }
    });
    tokio::time::timeout(Duration::from_secs(2), started.notified())
        .await
        .unwrap();
    let mut write = tokio::spawn({
        let runtime = runtime.clone();
        async move {
            runtime
                .route_tool_call(
                    &thread,
                    &"write-turn".into(),
                    call("write", "unknown_probe", false),
                    None,
                    None,
                    None,
                )
                .await
        }
    });
    assert!(
        tokio::time::timeout(Duration::from_millis(100), &mut write)
            .await
            .is_err(),
        "a separately dispatched writer must wait for the active read"
    );
    release.notify_one();
    assert!(
        !tokio::time::timeout(Duration::from_secs(2), read)
            .await
            .unwrap()
            .unwrap()
            .unwrap()
            .is_error
    );
    assert!(
        !tokio::time::timeout(Duration::from_secs(2), write)
            .await
            .unwrap()
            .unwrap()
            .unwrap()
            .is_error
    );
}