causa-runtime 0.0.2

Causa runtime — optional components: material execution, bound tool processing, streaming
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
//! Controlled returns preserve known/unknown/pending state and error payloads.
mod tool_fixtures;
use async_trait::async_trait;
use causa_kernel::*;
use causa_runtime::{ToolExecutorOptions, ToolProcessingError, ToolProcessorPhase, new_block_id};
use serde_json::json;
use std::sync::{
    Arc,
    atomic::{AtomicUsize, Ordering},
};
use std::time::{Duration, Instant};
use tokio::sync::{Notify, Semaphore};
use tool_fixtures::*;

struct CancelProcessor;
#[async_trait]
impl ToolBatchProcessor for CancelProcessor {
    async fn process(
        &self,
        batch: &mut ToolBatch,
        context: &ProcessorContext<'_>,
    ) -> Result<(), ProcessorError> {
        for entry in batch.calls_mut() {
            entry.push_note(TextPayload::new("pre note"));
        }
        for entry in batch.results_mut() {
            entry.output_mut().unwrap().content = json!("post edit");
        }
        context.control.cancellation_token().cancel();
        Ok(())
    }
}
struct Count(Arc<AtomicUsize>);
#[async_trait]
impl ToolBatchProcessor for Count {
    async fn process(
        &self,
        _batch: &mut ToolBatch,
        _context: &ProcessorContext<'_>,
    ) -> Result<(), ProcessorError> {
        self.0.fetch_add(1, Ordering::SeqCst);
        Ok(())
    }
}
#[tokio::test]
async fn synchronous_before_cancel_preserves_mutations_and_stops_remaining_stages() {
    let count = Arc::new(AtomicUsize::new(0));
    let tool = NamedTool::new("work");
    let executor = executor(
        vec![tool.clone()],
        ToolExecutorOptions {
            before: vec![Arc::new(CancelProcessor), Arc::new(Count(count.clone()))],
            ..Default::default()
        },
    );
    let mut batch = batch(&["work", "work"]);
    assert!(matches!(
        executor
            .bind(invocation(), control())
            .await
            .unwrap()
            .process(&mut batch)
            .await,
        Err(ToolProcessingError::Control(ControlError::Cancelled))
    ));
    assert_eq!(batch.completed_len(), 0);
    assert_eq!(
        batch.calls()[0].call().result_notes,
        [TextPayload::new("pre note")]
    );
    assert_eq!(tool.count.load(Ordering::SeqCst), 0);
    assert_eq!(count.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn synchronous_after_cancel_returns_error_even_with_all_results_and_preserves_edits() {
    let count = Arc::new(AtomicUsize::new(0));
    let tool = NamedTool::new("work");
    let executor = executor(
        vec![tool.clone()],
        ToolExecutorOptions {
            after: vec![Arc::new(CancelProcessor), Arc::new(Count(count.clone()))],
            ..Default::default()
        },
    );
    let mut batch = batch(&["work"]);
    assert!(matches!(
        executor
            .bind(invocation(), control())
            .await
            .unwrap()
            .process(&mut batch)
            .await,
        Err(ToolProcessingError::Control(ControlError::Cancelled))
    ));
    assert_eq!(statuses(&batch), vec![ToolResultStatus::Succeeded]);
    assert_eq!(
        batch.results()[0].result().unwrap().1.output.content,
        json!("post edit")
    );
    assert_eq!(
        batch.results()[0].result().unwrap().1.notes,
        [TextPayload::new("tool note")]
    );
    assert_eq!(count.load(Ordering::SeqCst), 0);
}
struct CancelsOnStart(Arc<AtomicUsize>);
#[async_trait]
impl Tool for CancelsOnStart {
    fn definition(&self) -> ToolDefinition {
        definition("cancel")
    }
    async fn execute(&self, _call: &ToolCallContext, control: &CallControl) -> ToolResultPayload {
        self.0.fetch_add(1, Ordering::SeqCst);
        control.cancellation_token().cancel();
        std::future::pending().await
    }
}
#[tokio::test]
async fn first_dispatch_cancel_marks_started_unknown_and_unstarted_pending() {
    let count = Arc::new(AtomicUsize::new(0));
    let executor = executor(
        vec![Arc::new(CancelsOnStart(count.clone()))],
        ToolExecutorOptions::default(),
    );
    let mut batch = batch(&["cancel", "cancel", "cancel", "cancel"]);
    let returned = tokio::time::timeout(
        Duration::from_secs(2),
        executor
            .bind(invocation(), control())
            .await
            .unwrap()
            .process(&mut batch),
    )
    .await
    .unwrap();
    assert!(matches!(
        returned,
        Err(ToolProcessingError::Control(ControlError::Cancelled))
    ));
    assert_eq!(count.load(Ordering::SeqCst), 1);
    assert_eq!(statuses(&batch), vec![ToolResultStatus::UnknownOutcome]);
    assert_eq!(batch.calls().len(), 3);
}
#[tokio::test]
async fn already_accepted_result_survives_later_sibling_cancel() {
    let count = Arc::new(AtomicUsize::new(0));
    let known = NamedTool::new("known");
    let executor = executor(
        vec![known.clone(), Arc::new(CancelsOnStart(count))],
        ToolExecutorOptions::default(),
    );
    let mut batch = batch(&["known", "cancel", "known"]);
    let known_id = batch.declaration_ids()[0];
    assert!(matches!(
        executor
            .bind(invocation(), control())
            .await
            .unwrap()
            .process(&mut batch)
            .await,
        Err(ToolProcessingError::Control(ControlError::Cancelled))
    ));
    assert_eq!(known.count.load(Ordering::SeqCst), 1);
    assert_eq!(
        batch
            .results()
            .iter()
            .find(|entry| entry.call().call_block_id == known_id)
            .unwrap()
            .result()
            .unwrap()
            .1
            .status,
        ToolResultStatus::Succeeded
    );
    assert_eq!(batch.completed_len(), 2);
    assert_eq!(batch.calls().len(), 1);
}
struct WaitTool {
    name: &'static str,
    entered: Arc<Notify>,
    count: Arc<AtomicUsize>,
}
#[async_trait]
impl Tool for WaitTool {
    fn definition(&self) -> ToolDefinition {
        definition(self.name)
    }
    async fn execute(&self, _call: &ToolCallContext, _control: &CallControl) -> ToolResultPayload {
        self.count.fetch_add(1, Ordering::SeqCst);
        self.entered.notify_one();
        std::future::pending().await
    }
}
#[tokio::test]
async fn external_cancel_settles_every_started_future_without_waiting_for_tool_cooperation() {
    let entered = Arc::new(Notify::new());
    let count = Arc::new(AtomicUsize::new(0));
    let parent = control();
    let caller = parent.clone();
    let executor = Arc::new(executor(
        vec![Arc::new(WaitTool {
            name: "wait",
            entered: entered.clone(),
            count: count.clone(),
        })],
        ToolExecutorOptions::default(),
    ));
    let task = tokio::spawn(async move {
        let mut batch = batch(&["wait", "wait"]);
        let result = executor
            .bind(invocation(), parent)
            .await
            .unwrap()
            .process(&mut batch)
            .await;
        (result, batch)
    });
    while count.load(Ordering::SeqCst) != 2 {
        tokio::time::timeout(Duration::from_secs(2), entered.notified())
            .await
            .unwrap();
    }
    caller.cancellation_token().cancel();
    let (result, batch) = tokio::time::timeout(Duration::from_secs(2), task)
        .await
        .unwrap()
        .unwrap();
    assert!(matches!(
        result,
        Err(ToolProcessingError::Control(ControlError::Cancelled))
    ));
    assert_eq!(statuses(&batch), vec![ToolResultStatus::UnknownOutcome; 2]);
}
struct WaitingProcessor {
    entered: Arc<Notify>,
}
#[async_trait]
impl ToolBatchProcessor for WaitingProcessor {
    async fn process(
        &self,
        batch: &mut ToolBatch,
        _context: &ProcessorContext<'_>,
    ) -> Result<(), ProcessorError> {
        for entry in batch.calls_mut() {
            entry.push_note(TextPayload::new("before wait"));
        }
        for entry in batch.results_mut() {
            entry.output_mut().unwrap().content = json!("before wait");
        }
        self.entered.notify_one();
        std::future::pending().await
    }
}
#[tokio::test]
async fn cancel_during_before_and_after_wait_keeps_current_materials() {
    for phase in [ToolProcessorPhase::Before, ToolProcessorPhase::After] {
        let entered = Arc::new(Notify::new());
        let tool = NamedTool::new("work");
        let parent = control();
        let caller = parent.clone();
        let processors: Vec<Arc<dyn ToolBatchProcessor>> = vec![Arc::new(WaitingProcessor {
            entered: entered.clone(),
        })];
        let options = match phase {
            ToolProcessorPhase::Before => ToolExecutorOptions {
                before: processors,
                ..Default::default()
            },
            ToolProcessorPhase::After => ToolExecutorOptions {
                after: processors,
                ..Default::default()
            },
        };
        let executor = Arc::new(executor(vec![tool.clone()], options));
        let task = tokio::spawn(async move {
            let mut batch = batch(&["work"]);
            let result = executor
                .bind(invocation(), parent)
                .await
                .unwrap()
                .process(&mut batch)
                .await;
            (result, batch)
        });
        tokio::time::timeout(Duration::from_secs(2), entered.notified())
            .await
            .unwrap();
        caller.cancellation_token().cancel();
        let (result, batch) = tokio::time::timeout(Duration::from_secs(2), task)
            .await
            .unwrap()
            .unwrap();
        assert!(matches!(
            result,
            Err(ToolProcessingError::Control(ControlError::Cancelled))
        ));
        if phase == ToolProcessorPhase::Before {
            assert_eq!(
                batch.calls()[0].call().result_notes,
                [TextPayload::new("before wait")]
            );
            assert_eq!(tool.count.load(Ordering::SeqCst), 0);
        } else {
            assert_eq!(
                batch.results()[0].result().unwrap().1.output.content,
                json!("before wait")
            );
            assert_eq!(statuses(&batch), vec![ToolResultStatus::Succeeded]);
        }
    }
}
#[tokio::test]
async fn binding_does_not_restart_parent_deadline_when_processing_later() {
    let parent = CallControl::new(
        CancellationToken::new(),
        Some(Instant::now() + Duration::from_millis(15)),
    );
    let tool = NamedTool::new("work");
    let executor = executor(vec![tool.clone()], ToolExecutorOptions::default());
    let binding = executor.bind(invocation(), parent).await.unwrap();
    tokio::time::sleep(Duration::from_millis(25)).await;
    let mut batch = batch(&["work"]);
    assert!(matches!(
        binding.process(&mut batch).await,
        Err(ToolProcessingError::Control(ControlError::TimedOut))
    ));
    assert_eq!(batch.completed_len(), 0);
    assert_eq!(tool.count.load(Ordering::SeqCst), 0);
}
#[tokio::test]
async fn explicit_cancel_wins_when_deadline_is_also_expired() {
    let parent = CallControl::new(
        CancellationToken::new(),
        Some(Instant::now() - Duration::from_secs(1)),
    );
    parent.cancellation_token().cancel();
    let executor = executor(vec![], ToolExecutorOptions::default());
    assert!(matches!(
        executor.bind(invocation(), parent).await,
        Err(causa_runtime::ToolCatalogError::Control(
            ControlError::Cancelled
        ))
    ));
}
struct WrongResult {
    entered: Arc<Notify>,
    release: Arc<Semaphore>,
    foreign_id: BlockId,
}
#[async_trait]
impl Tool for WrongResult {
    fn definition(&self) -> ToolDefinition {
        definition("wrong")
    }
    async fn execute(&self, call: &ToolCallContext, _control: &CallControl) -> ToolResultPayload {
        self.entered.notify_one();
        self.release.acquire().await.unwrap().forget();
        let mut result = result(
            call,
            ToolResultStatus::Succeeded,
            json!({"diagnostic": "actual payload"}),
        );
        result.call_block_id = self.foreign_id;
        result
            .media
            .push(MediaRef::new("image/png", "foreign-media"));
        result.notes.push(TextPayload::new("foreign note"));
        result
    }
}
#[tokio::test]
async fn rejected_tool_payload_is_retained_and_module_error_never_cancels_shared_parent() {
    let entered = Arc::new(Notify::new());
    let release = Arc::new(Semaphore::new(0));
    let waiting = Arc::new(Notify::new());
    let count = Arc::new(AtomicUsize::new(0));
    let foreign_id = new_block_id();
    let parent = control();
    let caller = parent.clone();
    let known = NamedTool::new("known");
    let executor = Arc::new(executor(
        vec![
            known.clone(),
            Arc::new(WrongResult {
                entered: entered.clone(),
                release: release.clone(),
                foreign_id,
            }),
            Arc::new(WaitTool {
                name: "wait",
                entered: waiting.clone(),
                count: count.clone(),
            }),
        ],
        ToolExecutorOptions::default(),
    ));
    let executing = executor.clone();
    let task = tokio::spawn(async move {
        let mut batch = batch(&["known", "wrong", "wait"]);
        let wrong_id = batch.declaration_ids()[1];
        let result = executing
            .bind(invocation(), parent)
            .await
            .unwrap()
            .process(&mut batch)
            .await;
        (result, batch, wrong_id)
    });
    tokio::time::timeout(Duration::from_secs(2), entered.notified())
        .await
        .unwrap();
    tokio::time::timeout(Duration::from_secs(2), waiting.notified())
        .await
        .unwrap();
    release.add_permits(1);
    let (error, returned, wrong_id) = tokio::time::timeout(Duration::from_secs(2), task)
        .await
        .unwrap()
        .unwrap();
    match error.unwrap_err() {
        ToolProcessingError::ResultRejected {
            call_block_id,
            error: BatchError::MismatchedCall { expected, actual },
            result,
        } => {
            assert_eq!(call_block_id, wrong_id);
            assert_eq!(expected, wrong_id);
            assert_eq!(actual, foreign_id);
            assert_eq!(
                result.output.content,
                json!({"diagnostic": "actual payload"})
            );
            assert_eq!(
                result.media,
                vec![MediaRef::new("image/png", "foreign-media")]
            );
            assert_eq!(result.notes, [TextPayload::new("foreign note")]);
        }
        error => panic!("unexpected error {error:?}"),
    }
    assert!(!caller.is_cancelled());
    assert_eq!(returned.completed_len(), 3);
    assert_eq!(
        statuses(&returned)
            .iter()
            .filter(|status| **status == ToolResultStatus::UnknownOutcome)
            .count(),
        2
    );
    let mut separate = batch(&["known"]);
    executor
        .bind(invocation(), caller)
        .await
        .unwrap()
        .process(&mut separate)
        .await
        .unwrap();
    assert_eq!(statuses(&separate), vec![ToolResultStatus::Succeeded]);
}

struct CancelOnDrop(CancellationToken);
impl Drop for CancelOnDrop {
    fn drop(&mut self) {
        self.0.cancel();
    }
}
struct CancelWhenDropped(Arc<Notify>);
#[async_trait]
impl Tool for CancelWhenDropped {
    fn definition(&self) -> ToolDefinition {
        definition("drop")
    }
    async fn execute(&self, _call: &ToolCallContext, control: &CallControl) -> ToolResultPayload {
        let _guard = CancelOnDrop(control.cancellation_token().clone());
        self.0.notify_one();
        std::future::pending().await
    }
}
#[tokio::test]
async fn selected_parent_deadline_survives_later_cancel_while_call_future_is_dropped() {
    let entered = Arc::new(Notify::new());
    let parent = CallControl::new(
        CancellationToken::new(),
        Some(Instant::now() + Duration::from_millis(25)),
    );
    let caller = parent.clone();
    let executor = executor(
        vec![Arc::new(CancelWhenDropped(entered))],
        ToolExecutorOptions::default(),
    );
    let mut batch = batch(&["drop"]);
    let result = executor
        .bind(invocation(), parent)
        .await
        .unwrap()
        .process(&mut batch)
        .await;
    assert!(matches!(
        result,
        Err(ToolProcessingError::Control(ControlError::TimedOut))
    ));
    assert!(
        caller.is_cancelled(),
        "the tool's Drop runs after the cause was selected"
    );
    assert_eq!(statuses(&batch), vec![ToolResultStatus::UnknownOutcome]);
}