causa-runtime 0.0.1

Causa runtime — optional components: execution stack (turn loop, dispatch, streaming, pause/resume, session)
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
//! Streaming driver tests: `run_streaming` /
//! `run_in_conversation_streaming` through scripted streaming gateways,
//! plus the shared retry-backoff behavior.

mod common;

use causa_kernel::{
    CancellationToken, ConversationId, ModelGateway, ModelInvokeError, ModelInvokeErrorKind,
    ModelRequest, ModelStream, RoundId, StreamDelta, TextPayload, TurnContext, TurnId,
};
use causa_runtime::{
    ConversationState, NoopInteraction, RetryPolicy, RunControl, StreamEventCollector, TurnOutcome,
    TurnPolicy, TurnResult, TurnRunOptions, TurnRunner, project_streaming_turn,
};
use common::{
    EchoTool, RecordingStreamingGateway, StreamScript, ctrl, text_script, tooluse_script,
};
use futures_util::StreamExt;
use std::sync::Arc;
use std::time::{Duration, Instant};

fn streaming_options(max_retries: u32, backoff_base_ms: u64) -> TurnRunOptions {
    TurnRunOptions {
        policy: TurnPolicy {
            retry: RetryPolicy {
                max_retries,
                retry_timeouts: false,
                backoff_base_ms,
                backoff_max_ms: backoff_base_ms.max(1),
            },
            ..Default::default()
        },
        ..Default::default()
    }
}

fn collector_for(ctx: &TurnContext) -> Arc<StreamEventCollector> {
    Arc::new(StreamEventCollector::new(ctx.turn_id(), None))
}

// ---- driver streaming ---------------------------------------------------------

#[tokio::test]
async fn streaming_run_completes_and_observes_text_deltas() {
    let ctx = ctx();
    let collector = collector_for(&ctx);
    let mut options = streaming_options(0, 0);
    options.interaction = collector.clone();
    let gateway = RecordingStreamingGateway::scripted(vec![text_script("hello world")]);
    let runner = TurnRunner::new(
        gateway.clone(),
        Arc::new(causa_runtime::ToolExecutor::from_vec(vec![])),
    );

    let out: TurnOutcome = runner.run_streaming(ctx, options, ctrl()).await;

    assert!(matches!(out.result, TurnResult::Completed { .. }));
    assert_eq!(out.trace.rounds.len(), 1);
    // The Done output (not the deltas) is the fact source: input block
    // plus one assistant text block carrying the assembled text.
    let blocks = out.context.blocks();
    assert_eq!(blocks.len(), 2);
    assert!(matches!(
        &blocks[1].content,
        causa_kernel::BlockContent::Parts(parts)
            if matches!(
                parts.as_slice(),
                [causa_kernel::ContentPart::Text(causa_kernel::TextPayload(t))] if t == "hello world"
            )
    ));
    // The interaction observed the advisory deltas, never the terminal ones.
    let events = collector.events();
    assert_eq!(events.len(), 1);
    match &events[0].kind {
        causa_runtime::ContextEventKind::TextDelta { round_id, delta } => {
            assert_eq!(*round_id, RoundId(0));
            assert_eq!(delta, "hello world");
        }
        other => panic!("expected TextDelta event, got {other:?}"),
    }
}

#[tokio::test]
async fn streaming_tool_round_drives_next_round() {
    let ctx = ctx();
    let collector = collector_for(&ctx);
    let mut options = streaming_options(0, 0);
    options.interaction = collector.clone();
    options.invocation = Default::default();
    let gateway = RecordingStreamingGateway::scripted(vec![
        tooluse_script("calling echo", "echo", serde_json::json!({"a": 1})),
        text_script("all done"),
    ]);
    let runner = TurnRunner::new(
        gateway.clone(),
        Arc::new(causa_runtime::ToolExecutor::from_vec(vec![Arc::new(
            EchoTool,
        )])),
    );

    let out: TurnOutcome = runner.run_streaming(ctx, options, ctrl()).await;

    assert!(matches!(out.result, TurnResult::Completed { .. }));
    assert_eq!(out.trace.rounds.len(), 2);
    assert_eq!(out.trace.tool_calls_total, 1);
    assert_eq!(gateway.attempts(), 2);
    // Deltas from both rounds observed, in round order.
    let events = collector.events();
    let rounds: Vec<RoundId> = events
        .iter()
        .filter_map(|e| match &e.kind {
            causa_runtime::ContextEventKind::TextDelta { round_id, .. } => Some(*round_id),
            _ => None,
        })
        .collect();
    assert_eq!(rounds, vec![RoundId(0), RoundId(1)]);
}

#[tokio::test]
async fn streaming_retry_after_error_delta_succeeds() {
    let ctx = ctx();
    let gateway = RecordingStreamingGateway::scripted(vec![
        // Attempt 1: mid-stream provider error (retryable, same frame).
        Ok(vec![
            StreamDelta::TextDelta {
                delta: "partial".into(),
            },
            StreamDelta::Error {
                kind: ModelInvokeErrorKind::Transient,
                message: "provider hiccup".into(),
            },
        ]),
        // Attempt 2: clean completion.
        text_script("recovered"),
    ]);
    let runner = TurnRunner::new(
        gateway.clone(),
        Arc::new(causa_runtime::ToolExecutor::from_vec(vec![])),
    );

    let out: TurnOutcome = runner
        .run_streaming(ctx, streaming_options(1, 0), ctrl())
        .await;

    assert!(matches!(out.result, TurnResult::Completed { .. }));
    assert_eq!(gateway.attempts(), 2);
    // Both attempts share one round's attempt trace.
    let attempts = &out.trace.rounds[0].attempts;
    assert_eq!(attempts.len(), 2);
    assert_eq!(attempts[0].kind, Some(ModelInvokeErrorKind::Transient));
    assert!(attempts[0].is_retryable);
    assert_eq!(attempts[1].kind, None);
}

#[tokio::test]
async fn streaming_transport_error_maps_like_invoke() {
    let ctx = ctx();
    let gateway = RecordingStreamingGateway::scripted(vec![
        Err(ModelInvokeErrorKind::Transient),
        Err(ModelInvokeErrorKind::Transient),
        text_script("third time"),
    ]);
    let runner = TurnRunner::new(
        gateway.clone(),
        Arc::new(causa_runtime::ToolExecutor::from_vec(vec![])),
    );

    let out: TurnOutcome = runner
        .run_streaming(ctx, streaming_options(2, 0), ctrl())
        .await;

    assert!(matches!(out.result, TurnResult::Completed { .. }));
    assert_eq!(gateway.attempts(), 3);
    assert_eq!(out.trace.rounds[0].attempts.len(), 3);
}

/// A stream that yields one delta and then never resolves — the
/// cancellation-race fixture.
struct PendAfterFirstDelta;
#[async_trait::async_trait]
impl ModelGateway for PendAfterFirstDelta {
    async fn invoke(
        &self,
        _req: &ModelRequest,
        _ctrl: &causa_kernel::AttemptControl,
    ) -> Result<causa_kernel::ModelOutput, ModelInvokeError> {
        Err(ModelInvokeError::new(
            ModelInvokeErrorKind::Permanent,
            "fixture",
        ))
    }
    async fn stream(
        &self,
        _req: &ModelRequest,
        _ctrl: &causa_kernel::AttemptControl,
    ) -> Result<ModelStream, ModelInvokeError> {
        let first = futures_util::stream::iter(vec![StreamDelta::TextDelta {
            delta: "started".into(),
        }]);
        Ok(Box::pin(first.chain(futures_util::stream::pending())))
    }
}

#[tokio::test]
async fn cancellation_mid_stream_interrupts_explicitly() {
    let token = CancellationToken::new();
    struct CancelOnFirstDelta {
        token: CancellationToken,
    }
    #[async_trait::async_trait]
    impl causa_runtime::TurnInteraction for CancelOnFirstDelta {
        async fn on_delta(&self, _round_id: RoundId, _delta: &StreamDelta) {
            self.token.cancel();
        }
    }
    let ctx = ctx();
    let mut options = streaming_options(3, 0);
    options.interaction = Arc::new(CancelOnFirstDelta {
        token: token.clone(),
    });
    let runner = TurnRunner::new(
        Arc::new(PendAfterFirstDelta),
        Arc::new(causa_runtime::ToolExecutor::from_vec(vec![])),
    );

    let out = runner
        .run_streaming(ctx, options, RunControl::new(token, None))
        .await;

    match out.result {
        TurnResult::Interrupted {
            cause: causa_runtime::TurnInterruption::ExplicitCancellation,
        } => {}
        other => panic!("expected ExplicitCancellation, got {other:?}"),
    }
}

#[tokio::test]
async fn conversation_streaming_entry_completes_and_stamps() {
    let mut state = ConversationState::new(ConversationId("conv-stream".into()));
    state.begin_turn(TurnId::new("t1")).unwrap();
    state
        .active_turn_mut()
        .unwrap()
        .append_input(TextPayload::new("hi"), "user")
        .unwrap();
    let turn_id = state.active_turn().unwrap().turn_id();
    let collector = Arc::new(StreamEventCollector::new(
        turn_id,
        Some(ConversationId("conv-stream".into())),
    ));
    let mut options = streaming_options(0, 0);
    options.interaction = collector.clone();
    let gateway = RecordingStreamingGateway::scripted(vec![text_script("done")]);
    let runner = TurnRunner::new(
        gateway.clone(),
        Arc::new(causa_runtime::ToolExecutor::from_vec(vec![])),
    );

    let mut out = runner
        .run_in_conversation_streaming(state, options, ctrl())
        .await
        .unwrap();

    assert!(matches!(out.result, TurnResult::Completed { .. }));
    assert!(out.state.active_turn().unwrap().is_sealed());
    // The interaction observed the delta with the conversation envelope.
    let events = collector.events();
    assert_eq!(events.len(), 1);
    assert_eq!(
        events[0].conversation_id,
        Some(ConversationId("conv-stream".into()))
    );
    // Host loop: commit receives the sealed turn.
    let entry = out.state.commit(TurnId::new("t1")).unwrap();
    assert_eq!(entry.sequence.0, 0);
}

#[tokio::test]
async fn project_streaming_turn_interleaves_deltas_with_dispatches() {
    // Drive a real two-round streaming turn, then project it.
    let ctx = ctx();
    let collector = collector_for(&ctx);
    let mut options = streaming_options(0, 0);
    options.interaction = collector.clone();
    let gateway = RecordingStreamingGateway::scripted(vec![
        tooluse_script("calling echo", "echo", serde_json::json!({"a": 1})),
        text_script("all done"),
    ]);
    let runner = TurnRunner::new(
        gateway,
        Arc::new(causa_runtime::ToolExecutor::from_vec(vec![Arc::new(
            EchoTool,
        )])),
    );
    let out: TurnOutcome = runner.run_streaming(ctx, options, ctrl()).await;
    let deltas = collector.events();

    let events = project_streaming_turn(&out.context, &out.result, &out.trace, None, deltas);
    // Canonical order: TurnStarted, round-0 deltas (text plus the two
    // tool-call increments — name, then arguments) then
    // ToolBatchDispatched, round-1 delta, TurnOutcome.
    let kinds: Vec<&str> = events
        .iter()
        .map(|e| match &e.kind {
            causa_runtime::ContextEventKind::TurnStarted => "started",
            causa_runtime::ContextEventKind::TextDelta { .. } => "text",
            causa_runtime::ContextEventKind::ToolCallDelta { .. } => "call_delta",
            causa_runtime::ContextEventKind::ToolBatchDispatched { .. } => "dispatch",
            causa_runtime::ContextEventKind::TurnOutcome { .. } => "outcome",
            causa_runtime::ContextEventKind::ReasoningDelta { .. } => "reasoning",
        })
        .collect();
    assert_eq!(
        kinds,
        vec![
            "started",
            "text",
            "call_delta",
            "call_delta",
            "dispatch",
            "text",
            "outcome",
        ]
    );
    // The dispatch event carries the committed (pre-execution) payloads.
    match &events[4].kind {
        causa_runtime::ContextEventKind::ToolBatchDispatched {
            round_id, calls, ..
        } => {
            assert_eq!(*round_id, RoundId(0));
            assert_eq!(calls.len(), 1);
            assert_eq!(calls[0].tool_name, "echo");
        }
        other => panic!("expected dispatch, got {other:?}"),
    }
}

// ---- backoff --------------------------------------------------------------------

#[tokio::test]
async fn backoff_delays_increase_and_respect_the_cap() {
    let policy = RetryPolicy {
        max_retries: 4,
        retry_timeouts: false,
        backoff_base_ms: 10,
        backoff_max_ms: 1000,
    };
    // Jitter is ±20%, so these adjacent steps cannot overlap.
    let d2 = policy.backoff_delay(2);
    let d3 = policy.backoff_delay(3);
    let d4 = policy.backoff_delay(4);
    assert!(
        d2 >= Duration::from_millis(8) && d2 <= Duration::from_millis(12),
        "d2={d2:?}"
    );
    assert!(d3 > d2, "d3={d3:?} must exceed d2={d2:?}");
    assert!(d4 > d3, "d4={d4:?} must exceed d3={d3:?}");
    // The cap applies before jitter: 10ms base, attempt 5 → 10*2^3 = 80ms,
    // under the 120ms cap; jitter keeps it within 64..96ms.
    let capped_policy = RetryPolicy {
        max_retries: 4,
        retry_timeouts: false,
        backoff_base_ms: 10,
        backoff_max_ms: 120,
    };
    let d5 = capped_policy.backoff_delay(5);
    assert!(
        d5 >= Duration::from_millis(64) && d5 <= Duration::from_millis(96),
        "d5={d5:?}"
    );
    // A cap below the raw value: 100ms base, attempt 4 → 400ms → capped at
    // 150ms; jitter keeps it within 120..180ms.
    let hard_cap = RetryPolicy {
        max_retries: 4,
        retry_timeouts: false,
        backoff_base_ms: 100,
        backoff_max_ms: 150,
    };
    let d4 = hard_cap.backoff_delay(4);
    assert!(
        d4 >= Duration::from_millis(120) && d4 <= Duration::from_millis(180),
        "d4={d4:?}"
    );
}

#[tokio::test]
async fn backoff_sleep_cancels_immediately() {
    let token = CancellationToken::new();
    let gateway = RecordingStreamingGateway::scripted(vec![Err(ModelInvokeErrorKind::Transient)]);
    let runner = TurnRunner::new(
        gateway,
        Arc::new(causa_runtime::ToolExecutor::from_vec(vec![])),
    );
    // 60s backoff; the test cancels 50ms in.
    let mut options = streaming_options(1, 60_000);
    options.interaction = Arc::new(NoopInteraction);
    let started = Instant::now();
    tokio::spawn({
        let token = token.clone();
        async move {
            tokio::time::sleep(Duration::from_millis(50)).await;
            token.cancel();
        }
    });
    let out = runner
        .run_streaming(ctx(), options, RunControl::new(token, None))
        .await;
    assert!(
        started.elapsed() < Duration::from_secs(5),
        "backoff must be cancellation-aware, took {:?}",
        started.elapsed()
    );
    match out.result {
        TurnResult::Interrupted {
            cause: causa_runtime::TurnInterruption::ExplicitCancellation,
        } => {}
        other => panic!("expected ExplicitCancellation, got {other:?}"),
    }
}

#[tokio::test]
async fn backoff_sleep_respects_the_turn_deadline() {
    let gateway = RecordingStreamingGateway::scripted(vec![Err(ModelInvokeErrorKind::Transient)]);
    let runner = TurnRunner::new(
        gateway.clone(),
        Arc::new(causa_runtime::ToolExecutor::from_vec(vec![])),
    );
    // 60s backoff; the turn deadline fires 50ms in. The driver must cap
    // its backoff wait at the remaining deadline and end the turn instead
    // of sitting out a wait it owns.
    let mut options = streaming_options(1, 60_000);
    options.interaction = Arc::new(NoopInteraction);
    let started = Instant::now();
    let out: TurnOutcome = runner
        .run_streaming(
            ctx(),
            options,
            RunControl::new(
                CancellationToken::new(),
                Some(Instant::now() + Duration::from_millis(50)),
            ),
        )
        .await;
    assert!(
        started.elapsed() < Duration::from_secs(5),
        "backoff must not outlive the turn deadline, took {:?}",
        started.elapsed()
    );
    match out.result {
        TurnResult::Interrupted {
            cause: causa_runtime::TurnInterruption::TurnDeadlineExceeded,
        } => {}
        other => panic!("expected TurnDeadlineExceeded, got {other:?}"),
    }
    // The deadline landed during backoff: no second attempt was spent.
    assert_eq!(gateway.attempts(), 1);
}

#[tokio::test]
async fn backoff_base_zero_keeps_immediate_retry_behavior() {
    let gateway = RecordingStreamingGateway::scripted(vec![
        Err(ModelInvokeErrorKind::Transient),
        text_script("fast"),
    ]);
    let runner = TurnRunner::new(
        gateway.clone(),
        Arc::new(causa_runtime::ToolExecutor::from_vec(vec![])),
    );
    let started = Instant::now();
    let out: TurnOutcome = runner
        .run_streaming(ctx(), streaming_options(1, 0), ctrl())
        .await;
    assert!(matches!(out.result, TurnResult::Completed { .. }));
    assert_eq!(gateway.attempts(), 2);
    assert!(
        started.elapsed() < Duration::from_millis(200),
        "base 0 must not sleep, took {:?}",
        started.elapsed()
    );
}

fn ctx() -> TurnContext {
    let mut ctx = TurnContext::new(TurnId::new("t-stream"));
    ctx.append_input(TextPayload::new("hi"), "user").unwrap();
    ctx
}

// Silence the unused import when StreamScript is only used via aliases.
#[allow(unused)]
fn _assert_script_type(_: &StreamScript) {}