briefcase-core 3.0.0

Open-source decision tracking for AI
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
//! Synchronous replay engine implementation
//!
//! This module provides a synchronous variant of the replay engine
//! for use cases that don't require async functionality.

use super::{
    ChangeType, Comparator, ExecutionConfig, FieldChange, PolicyViolation, ReplayError, ReplayMode,
    ReplayPolicy, ReplayResult, ReplayStats, ReplayStatus, SnapshotDiff, SyncModelExecutor,
};
use crate::models::DecisionSnapshot;
use crate::storage::sync::SyncStorageBackend;
use std::sync::Arc;
use std::time::Instant;

/// Synchronous replay engine
pub struct SyncReplayEngine<S: SyncStorageBackend> {
    storage: S,
    default_mode: ReplayMode,
    executor: Option<Arc<dyn SyncModelExecutor>>,
}

impl<S: SyncStorageBackend> SyncReplayEngine<S> {
    /// Create a new synchronous replay engine
    pub fn new(storage: S) -> Self {
        Self {
            storage,
            default_mode: ReplayMode::Tolerant,
            executor: None,
        }
    }

    /// Create a replay engine with a specific default mode
    pub fn with_mode(storage: S, mode: ReplayMode) -> Self {
        Self {
            storage,
            default_mode: mode,
            executor: None,
        }
    }

    /// Set a model executor for the replay engine
    pub fn with_executor(mut self, executor: Arc<dyn SyncModelExecutor>) -> Self {
        self.executor = Some(executor);
        self
    }

    /// Get a reference to the current executor, if any
    pub fn executor(&self) -> Option<&dyn SyncModelExecutor> {
        self.executor.as_ref().map(|arc| arc.as_ref())
    }

    /// Get the default replay mode
    pub fn default_mode(&self) -> ReplayMode {
        self.default_mode.clone()
    }

    /// Replay a snapshot by ID
    pub fn replay(
        &self,
        snapshot_id: &str,
        mode: Option<ReplayMode>,
        _context_overrides: Option<serde_json::Value>,
    ) -> Result<ReplayResult, ReplayError> {
        let start_time = Instant::now();
        let replay_mode = mode.unwrap_or_else(|| self.default_mode());

        let original_snapshot = self
            .storage
            .load_decision(snapshot_id)
            .map_err(|e| ReplayError::StorageError(e.to_string()))?;

        match replay_mode {
            ReplayMode::ValidationOnly => {
                // Just validate without re-executing
                Ok(ReplayResult {
                    status: ReplayStatus::Success,
                    original_snapshot,
                    replay_output: None,
                    outputs_match: true, // Assume match for validation-only
                    diff: None,
                    policy_violations: Vec::new(),
                    execution_time_ms: start_time.elapsed().as_millis() as f64,
                })
            }
            ReplayMode::Strict | ReplayMode::Tolerant => {
                // Use the executor if available, otherwise fall back to simulation
                if self.executor.is_some() {
                    self.execute_replay(&original_snapshot, replay_mode, start_time)
                } else {
                    // For sync implementation, we'll simulate replay
                    let simulated_output = simulate_execution(&original_snapshot);
                    let outputs_match =
                        compare_outputs(&original_snapshot, &simulated_output, &replay_mode);

                    Ok(ReplayResult {
                        status: if outputs_match {
                            ReplayStatus::Success
                        } else {
                            ReplayStatus::Failed
                        },
                        original_snapshot,
                        replay_output: Some(simulated_output),
                        outputs_match,
                        diff: None, // TODO: Implement diff calculation
                        policy_violations: Vec::new(),
                        execution_time_ms: start_time.elapsed().as_millis() as f64,
                    })
                }
            }
        }
    }

    /// Replay with policy validation
    pub fn replay_with_policy(
        &self,
        snapshot_id: &str,
        policy: &ReplayPolicy,
        mode: Option<ReplayMode>,
    ) -> Result<ReplayResult, ReplayError> {
        let mut result = self.replay(snapshot_id, mode, None)?;

        // Validate against policy
        let violations = self.validate(snapshot_id, policy)?;
        result.policy_violations = violations;

        if !result.policy_violations.is_empty() {
            result.status = ReplayStatus::Failed;
        }

        Ok(result)
    }

    /// Validate a snapshot against a policy
    pub fn validate(
        &self,
        snapshot_id: &str,
        policy: &ReplayPolicy,
    ) -> Result<Vec<PolicyViolation>, ReplayError> {
        let snapshot = self
            .storage
            .load_decision(snapshot_id)
            .map_err(|e| ReplayError::StorageError(e.to_string()))?;

        let mut violations = Vec::new();

        // Apply policy rules
        for rule in &policy.rules {
            match rule.comparator {
                Comparator::ExactMatch => {
                    // For exact match, we'd need to compare with expected values
                    // This is a simplified implementation
                    if rule.field == "function_name" {
                        // Example validation logic
                        if snapshot.function_name.is_empty() {
                            violations.push(PolicyViolation {
                                rule_name: format!("exact_match_{}", rule.field),
                                field: rule.field.clone(),
                                expected: "non-empty function name".to_string(),
                                actual: "empty".to_string(),
                                message: "Function name cannot be empty".to_string(),
                            });
                        }
                    }
                }
                Comparator::SemanticSimilarity => {
                    // For similarity threshold, we'd need reference values
                    // This is a simplified implementation
                    if rule.field == "output" && snapshot.outputs.is_empty() {
                        violations.push(PolicyViolation {
                            rule_name: format!("similarity_{}", rule.field),
                            field: rule.field.clone(),
                            expected: "at least one output".to_string(),
                            actual: "no outputs".to_string(),
                            message: "At least one output is required".to_string(),
                        });
                    }
                }
                Comparator::MaxIncreasePercent => {
                    // Placeholder for cost/performance checks
                    // Would need historical data for comparison
                }
                Comparator::MaxDecreasePercent => {
                    // Placeholder for cost/performance checks
                    // Would need historical data for comparison
                }
                Comparator::WithinRange => {
                    // Placeholder for range checks
                    // Would need expected ranges for comparison
                }
            }
        }

        Ok(violations)
    }

    /// Execute replay with a model executor, if available
    fn execute_replay(
        &self,
        original: &DecisionSnapshot,
        mode: ReplayMode,
        start_time: Instant,
    ) -> Result<ReplayResult, ReplayError> {
        if let Some(ref executor) = self.executor {
            // Check model support
            if let Some(ref params) = original.model_parameters {
                if !executor.supports_model(&params.model_name) {
                    return Err(ReplayError::ExecutionError(format!(
                        "Executor '{}' does not support model '{}'",
                        executor.executor_name(),
                        params.model_name
                    )));
                }
            }

            // Execute with the configured default config
            let config = ExecutionConfig::default();
            let exec_result = executor.execute(
                &original.inputs,
                original.model_parameters.as_ref(),
                &original.context,
                &config,
            )?;

            let execution_time = start_time.elapsed().as_millis() as f64;

            // Compare outputs based on mode
            let tolerance = match mode {
                ReplayMode::Strict => 1.0,         // Exact match
                ReplayMode::Tolerant => 0.8,       // 80% similarity
                ReplayMode::ValidationOnly => 0.0, // Don't care about outputs
            };

            let comparison =
                executor.compare_outputs(&original.outputs, &exec_result.outputs, tolerance);

            let replay_output = serde_json::to_value(&exec_result.outputs).ok();

            Ok(ReplayResult {
                status: if comparison.is_match {
                    ReplayStatus::Success
                } else {
                    ReplayStatus::Failed
                },
                original_snapshot: original.clone(),
                replay_output,
                outputs_match: comparison.is_match,
                diff: Some(SnapshotDiff {
                    inputs_changed: false,
                    outputs_changed: !comparison.is_match,
                    model_params_changed: false,
                    execution_time_delta_ms: execution_time
                        - original.execution_time_ms.unwrap_or(0.0),
                    changes: comparison
                        .field_comparisons
                        .iter()
                        .filter(|c| !c.is_match)
                        .map(|c| FieldChange {
                            field_path: format!("output.{}", c.field_name),
                            old_value: c.original_value.clone(),
                            new_value: c.replayed_value.clone(),
                            change_type: ChangeType::Modified,
                        })
                        .collect(),
                }),
                policy_violations: Vec::new(),
                execution_time_ms: execution_time,
            })
        } else {
            // Fall back to simulation if no executor is set
            self.simulate_replay(original, mode, start_time)
        }
    }

    /// Get replay statistics for a list of snapshots
    pub fn get_replay_stats(&self, snapshot_ids: &[String]) -> Result<ReplayStats, ReplayError> {
        let mut total_replays = 0;
        let mut successful_replays = 0;
        let mut failed_replays = 0;
        let mut exact_matches = 0;
        let mut mismatches = 0;
        let mut total_execution_time_ms = 0.0;

        for snapshot_id in snapshot_ids {
            match self.replay(snapshot_id, None, None) {
                Ok(result) => {
                    total_replays += 1;
                    total_execution_time_ms += result.execution_time_ms;

                    match result.status {
                        ReplayStatus::Success => {
                            successful_replays += 1;
                            if result.outputs_match {
                                exact_matches += 1;
                            } else {
                                mismatches += 1;
                            }
                        }
                        _ => {
                            failed_replays += 1;
                            mismatches += 1;
                        }
                    }
                }
                Err(_) => {
                    total_replays += 1;
                    failed_replays += 1;
                    mismatches += 1;
                }
            }
        }

        let average_execution_time_ms = if total_replays > 0 {
            total_execution_time_ms / total_replays as f64
        } else {
            0.0
        };

        Ok(ReplayStats {
            total_replays,
            successful_replays,
            failed_replays,
            exact_matches,
            mismatches,
            average_execution_time_ms,
            total_execution_time_ms,
        })
    }

    fn simulate_replay(
        &self,
        original: &DecisionSnapshot,
        mode: ReplayMode,
        start_time: Instant,
    ) -> Result<ReplayResult, ReplayError> {
        let simulated_output = simulate_execution(original);
        let outputs_match = compare_outputs(original, &simulated_output, &mode);

        Ok(ReplayResult {
            status: if outputs_match {
                ReplayStatus::Success
            } else {
                ReplayStatus::Failed
            },
            original_snapshot: original.clone(),
            replay_output: Some(simulated_output),
            outputs_match,
            diff: None,
            policy_violations: Vec::new(),
            execution_time_ms: start_time.elapsed().as_millis() as f64,
        })
    }
}

impl<S: SyncStorageBackend> Clone for SyncReplayEngine<S>
where
    S: Clone,
{
    fn clone(&self) -> Self {
        Self {
            storage: self.storage.clone(),
            default_mode: self.default_mode.clone(),
            executor: self.executor.clone(),
        }
    }
}

/// Simulate execution for demo purposes
/// In a real implementation, this would re-execute the decision
fn simulate_execution(decision: &DecisionSnapshot) -> serde_json::Value {
    // For now, just return the first output if available
    if let Some(output) = decision.outputs.first() {
        output.value.clone()
    } else {
        serde_json::Value::Null
    }
}

/// Compare outputs based on replay mode
fn compare_outputs(
    decision: &DecisionSnapshot,
    simulated_output: &serde_json::Value,
    mode: &ReplayMode,
) -> bool {
    if let Some(original_output) = decision.outputs.first() {
        match mode {
            ReplayMode::Strict => {
                // Exact match required
                original_output.value == *simulated_output
            }
            ReplayMode::Tolerant => {
                // Allow some differences (simplified implementation)
                if original_output.value == *simulated_output {
                    true
                } else {
                    // Could implement fuzzy matching here
                    false
                }
            }
            ReplayMode::ValidationOnly => {
                // Always true for validation-only mode
                true
            }
        }
    } else {
        simulated_output.is_null()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::models::*;
    use crate::storage::sync::MemoryStorageBackend;
    use serde_json::json;

    fn create_test_decision() -> DecisionSnapshot {
        let input = Input::new("test_input", json!("value"), "string");
        let output = Output::new("test_output", json!("result"), "string");
        let model_params = ModelParameters::new("gpt-4");

        DecisionSnapshot::new("test_function")
            .with_module("test_module")
            .add_input(input)
            .add_output(output)
            .with_model_parameters(model_params)
            .add_tag("env", "test")
    }

    #[test]
    fn test_sync_replay_validation_only() {
        let storage = MemoryStorageBackend::new();
        let engine = SyncReplayEngine::new(storage);

        let decision = create_test_decision();
        let decision_id = engine.storage.save_decision(&decision).unwrap();

        let result = engine
            .replay(&decision_id, Some(ReplayMode::ValidationOnly), None)
            .unwrap();

        assert_eq!(result.status, ReplayStatus::Success);
        assert!(result.outputs_match);
        assert!(result.replay_output.is_none());
    }

    #[test]
    fn test_sync_replay_tolerant_mode() {
        let storage = MemoryStorageBackend::new();
        let engine = SyncReplayEngine::new(storage);

        let decision = create_test_decision();
        let decision_id = engine.storage.save_decision(&decision).unwrap();

        let result = engine
            .replay(&decision_id, Some(ReplayMode::Tolerant), None)
            .unwrap();

        assert_eq!(result.status, ReplayStatus::Success);
        assert!(result.replay_output.is_some());
    }

    #[test]
    fn test_sync_replay_stats() {
        let storage = MemoryStorageBackend::new();
        let engine = SyncReplayEngine::new(storage);

        let decision1 = create_test_decision();
        let decision2 = create_test_decision();

        let id1 = engine.storage.save_decision(&decision1).unwrap();
        let id2 = engine.storage.save_decision(&decision2).unwrap();

        let stats = engine.get_replay_stats(&[id1, id2]).unwrap();

        assert_eq!(stats.total_replays, 2);
        assert!(stats.total_execution_time_ms >= 0.0);
        assert!(stats.average_execution_time_ms >= 0.0);
    }

    #[test]
    fn test_sync_replay_policy_validation() {
        let storage = MemoryStorageBackend::new();
        let engine = SyncReplayEngine::new(storage);

        let policy = ReplayPolicy::new("test_policy".to_string())
            .with_exact_match("function_name".to_string());

        let decision = create_test_decision();
        let decision_id = engine.storage.save_decision(&decision).unwrap();

        let violations = engine.validate(&decision_id, &policy).unwrap();

        // Should pass validation since function name is not empty
        assert!(violations.is_empty());
    }
}