runifold-testkit 0.7.1

Deterministic runtime testing and Agent quality evaluation for Runifold
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
//! Deterministic fault injection shared by model, Tool, and recovery tests.

use std::{
    collections::{BTreeMap, VecDeque},
    sync::{Arc, Mutex},
};

use futures_util::StreamExt;
use runifold_model::{
    ContentPart, Model, ModelCallContext, ModelCapabilities, ModelError, ModelEventStream,
    ModelFuture, ModelRef, ModelRequest, ModelStreamEvent,
};
use runifold_tool::{Tool, ToolContext, ToolDescriptor, ToolError, ToolFuture, ToolOutput};
use serde_json::Value;
use thiserror::Error;

/// Fault applied to one model invocation.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub enum ModelFault {
    /// Executes the invocation without injection.
    Pass,
    /// Fails before a canonical stream is opened.
    FailOpen(ModelError),
    /// Ends the stream after the specified number of canonical events.
    DisconnectAfterEvents(usize),
    /// Ends the stream immediately after the first completed Tool call.
    DisconnectAfterToolCall,
}

/// Fault applied to one named Tool invocation.
#[derive(Clone, Debug)]
pub struct ToolFault {
    tool: String,
    invocation: u64,
    error: ToolError,
}

impl ToolFault {
    /// Fails the selected one-based invocation of a named Tool.
    pub fn on_invocation(tool: impl Into<String>, invocation: u64, error: ToolError) -> Self {
        Self {
            tool: tool.into(),
            invocation,
            error,
        }
    }
}

#[derive(Debug, Default)]
struct FaultState {
    model_faults: VecDeque<ModelFault>,
    tool_faults: Vec<ToolFault>,
    tool_invocations: BTreeMap<String, u64>,
    tool_executions: BTreeMap<String, u64>,
    runtime_restarts: u64,
}

/// Shared deterministic fault plan and execution counters.
#[derive(Clone, Debug, Default)]
pub struct FaultController {
    state: Arc<Mutex<FaultState>>,
}

/// Fluent scenario name for [`FaultController`].
pub type FaultScenario = FaultController;

impl FaultController {
    /// Creates an empty fault plan.
    pub fn new() -> Self {
        Self::default()
    }

    /// Queues a fault for the next model invocation.
    #[must_use]
    pub fn model_fault(self, fault: ModelFault) -> Self {
        self.lock().model_faults.push_back(fault);
        self
    }

    /// Disconnects the next model stream after a completed Tool call.
    #[must_use]
    pub fn disconnect_after_tool_call(self) -> Self {
        self.model_fault(ModelFault::DisconnectAfterToolCall)
    }

    /// Disconnects the next model stream after `events` canonical events.
    #[must_use]
    pub fn disconnect_after_model_events(self, events: usize) -> Self {
        self.model_fault(ModelFault::DisconnectAfterEvents(events))
    }

    /// Fails the next model invocation before opening its stream.
    #[must_use]
    pub fn fail_next_model(self, error: ModelError) -> Self {
        self.model_fault(ModelFault::FailOpen(error))
    }

    /// Queues a named Tool fault.
    #[must_use]
    pub fn tool_fault(self, fault: ToolFault) -> Self {
        self.lock().tool_faults.push(fault);
        self
    }

    /// Fails the selected one-based invocation of a named Tool.
    #[must_use]
    pub fn fail_tool_on_invocation(
        self,
        tool: impl Into<String>,
        invocation: u64,
        error: ToolError,
    ) -> Self {
        self.tool_fault(ToolFault::on_invocation(tool, invocation, error))
    }

    /// Wraps a model with this plan.
    pub fn model<M>(&self, model: M) -> FaultInjectingModel<M>
    where
        M: Model,
    {
        FaultInjectingModel {
            inner: model,
            controller: self.clone(),
        }
    }

    /// Wraps a Tool with this plan.
    pub fn tool(&self, tool: Arc<dyn Tool>) -> FaultInjectingTool {
        FaultInjectingTool {
            inner: tool,
            controller: self.clone(),
        }
    }

    /// Returns the number of observed invocations for one Tool.
    pub fn tool_invocations(&self, tool: &str) -> u64 {
        self.lock().tool_invocations.get(tool).copied().unwrap_or(0)
    }

    /// Returns the number of calls forwarded to the underlying Tool.
    pub fn tool_executions(&self, tool: &str) -> u64 {
        self.lock().tool_executions.get(tool).copied().unwrap_or(0)
    }

    /// Returns the number of explicit runtime reconstructions.
    pub fn runtime_restarts(&self) -> u64 {
        self.lock().runtime_restarts
    }

    /// Asserts exact Tool execution count with a typed failure.
    ///
    /// # Errors
    ///
    /// Returns [`ScenarioAssertionError`] when the observed count differs.
    pub fn assert_tool_executed_exactly(
        &self,
        tool: &str,
        expected: u64,
    ) -> Result<(), ScenarioAssertionError> {
        let actual = self.tool_executions(tool);
        if actual == expected {
            Ok(())
        } else {
            Err(ScenarioAssertionError::ToolInvocationCount {
                tool: tool.into(),
                expected,
                actual,
            })
        }
    }

    fn lock(&self) -> std::sync::MutexGuard<'_, FaultState> {
        self.state
            .lock()
            .unwrap_or_else(std::sync::PoisonError::into_inner)
    }

    fn next_model_fault(&self) -> ModelFault {
        self.lock()
            .model_faults
            .pop_front()
            .unwrap_or(ModelFault::Pass)
    }

    fn tool_result(&self, tool: &str) -> Option<ToolError> {
        let mut state = self.lock();
        let invocation = {
            let count = state.tool_invocations.entry(tool.into()).or_default();
            *count = count.saturating_add(1);
            *count
        };
        let error = state
            .tool_faults
            .iter()
            .find(|fault| fault.tool == tool && fault.invocation == invocation)
            .map(|fault| fault.error.clone());
        if error.is_none() {
            let count = state.tool_executions.entry(tool.into()).or_default();
            *count = count.saturating_add(1);
        }
        error
    }

    fn record_restart(&self) {
        let mut state = self.lock();
        state.runtime_restarts = state.runtime_restarts.saturating_add(1);
    }
}

/// Model wrapper applying one queued fault per invocation.
#[derive(Clone, Debug)]
pub struct FaultInjectingModel<M> {
    inner: M,
    controller: FaultController,
}

impl<M> Model for FaultInjectingModel<M>
where
    M: Model,
{
    fn capabilities<'a>(
        &'a self,
        model: &'a ModelRef,
    ) -> ModelFuture<'a, Result<ModelCapabilities, ModelError>> {
        self.inner.capabilities(model)
    }

    fn stream(
        &self,
        request: ModelRequest,
        context: ModelCallContext,
    ) -> ModelFuture<'_, Result<ModelEventStream, ModelError>> {
        let fault = self.controller.next_model_fault();
        Box::pin(async move {
            if let ModelFault::FailOpen(error) = fault {
                return Err(error);
            }
            let stream = self.inner.stream(request, context).await?;
            match fault {
                ModelFault::Pass => Ok(stream),
                ModelFault::DisconnectAfterEvents(limit) => Ok(Box::pin(stream.take(limit))),
                ModelFault::DisconnectAfterToolCall => {
                    let truncated = futures_util::stream::unfold(
                        (stream, false),
                        |(mut stream, stopped)| async move {
                            if stopped {
                                return None;
                            }
                            let item = stream.next().await?;
                            let stop = matches!(
                                &item,
                                Ok(ModelStreamEvent::ContentPartCompleted {
                                    part: ContentPart::ToolCall(_),
                                    ..
                                })
                            );
                            Some((item, (stream, stop)))
                        },
                    );
                    Ok(Box::pin(truncated))
                }
                ModelFault::FailOpen(_) => unreachable!("handled before opening the stream"),
            }
        })
    }
}

/// Tool wrapper applying named, one-based invocation faults.
#[derive(Clone)]
pub struct FaultInjectingTool {
    inner: Arc<dyn Tool>,
    controller: FaultController,
}

impl Tool for FaultInjectingTool {
    fn descriptor(&self) -> &ToolDescriptor {
        self.inner.descriptor()
    }

    fn invoke(
        &self,
        input: Value,
        context: ToolContext,
    ) -> ToolFuture<'_, Result<ToolOutput, ToolError>> {
        let injected = self.controller.tool_result(&self.inner.descriptor().name);
        if let Some(error) = injected {
            return Box::pin(async move { Err(error) });
        }
        self.inner.invoke(input, context)
    }
}

impl std::fmt::Debug for FaultInjectingTool {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("FaultInjectingTool")
            .field("tool", &self.inner.descriptor().name)
            .finish_non_exhaustive()
    }
}

/// Reconstructs runtime state from one factory while preserving external
/// fixtures such as stores, fault counters, and cassette servers.
pub struct RecoveryHarness<T, F>
where
    F: FnMut() -> T,
{
    current: T,
    factory: F,
    controller: FaultController,
}

impl<T, F> RecoveryHarness<T, F>
where
    F: FnMut() -> T,
{
    /// Constructs the first runtime instance.
    pub fn new(mut factory: F, controller: FaultController) -> Self {
        let current = factory();
        Self {
            current,
            factory,
            controller,
        }
    }

    /// Returns the active runtime fixture.
    pub const fn current(&self) -> &T {
        &self.current
    }

    /// Returns the active runtime fixture mutably.
    pub fn current_mut(&mut self) -> &mut T {
        &mut self.current
    }

    /// Drops and reconstructs runtime-owned state while retaining the shared
    /// external fixture and fault controller.
    pub fn restart(&mut self) -> &mut T {
        self.current = (self.factory)();
        self.controller.record_restart();
        &mut self.current
    }
}

/// Typed failure from a scenario assertion.
#[derive(Clone, Debug, Error, Eq, PartialEq)]
#[non_exhaustive]
pub enum ScenarioAssertionError {
    /// A Tool did not execute the expected number of times.
    #[error("tool `{tool}` executed {actual} times; expected {expected}")]
    ToolInvocationCount {
        /// Tool name.
        tool: String,
        /// Expected count.
        expected: u64,
        /// Observed count.
        actual: u64,
    },
}

#[cfg(test)]
mod tests {
    use std::collections::BTreeMap;

    use runifold_core::RetrySafety;
    use runifold_model::{
        ContentPart, FinishReason, Message, Model, ModelCallContext, ModelErrorKind, ModelRef,
        ModelRequest, ModelStreamEvent, ToolCall,
    };
    use runifold_tool::{ToolError, ToolErrorKind};

    use crate::ScriptedModel;

    use super::{FaultController, ModelFault, RecoveryHarness, ToolFault};

    #[test]
    fn disconnect_after_tool_call_never_reaches_terminal_success() {
        let model = ScriptedModel::new();
        let model_ref = ModelRef::new("test", "model");
        model.enqueue([
            ModelStreamEvent::ResponseStarted {
                id: Some("response".into()),
                model: model_ref.clone(),
            },
            ModelStreamEvent::ContentPartCompleted {
                index: 0,
                part: ContentPart::ToolCall(ToolCall {
                    id: "call".into(),
                    name: "charge".into(),
                    arguments: serde_json::json!({}),
                    raw_arguments: Some("{}".into()),
                    metadata: BTreeMap::new(),
                }),
            },
            ModelStreamEvent::ResponseCompleted {
                finish_reason: FinishReason::ToolCalls,
                provider_metadata: BTreeMap::new(),
            },
        ]);
        let controller = FaultController::new().model_fault(ModelFault::DisconnectAfterToolCall);
        let model = controller.model(model);
        let request = ModelRequest::new(model_ref, Message::user("run"));

        let error =
            futures_executor::block_on(model.invoke(request, ModelCallContext::new())).unwrap_err();

        assert_eq!(error.kind, ModelErrorKind::Protocol);
    }

    #[test]
    fn tool_faults_are_one_based_and_distinguish_attempts_from_execution() {
        let mut error = ToolError::local(ToolErrorKind::Execution, "injected");
        error.retry_safety = RetrySafety::Safe;
        let controller =
            FaultController::new().tool_fault(ToolFault::on_invocation("charge", 1, error.clone()));

        assert_eq!(controller.tool_result("charge"), Some(error));
        assert_eq!(controller.tool_result("charge"), None);
        assert_eq!(controller.tool_invocations("charge"), 2);
        assert_eq!(controller.tool_executions("charge"), 1);
        controller
            .assert_tool_executed_exactly("charge", 1)
            .unwrap();
    }

    #[test]
    fn recovery_harness_reconstructs_runtime_and_counts_restarts() {
        let controller = FaultController::new();
        let mut next = 0_u64;
        let mut harness = RecoveryHarness::new(
            || {
                next += 1;
                next
            },
            controller.clone(),
        );

        assert_eq!(*harness.current(), 1);
        assert_eq!(*harness.restart(), 2);
        assert_eq!(controller.runtime_restarts(), 1);
    }
}