af-backtest 0.5.0

Deterministic, domain-neutral historical replay chassis with decision traces and bounded report projection.
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
//! `af-backtest` — deterministic historical replay for product adapters.
//!
//! This crate owns the reusable mechanics only: ordered historical ingress,
//! state transitions, decision traces, value curves, metrics, and bounded
//! report projection. Domain event types, accounting, execution semantics,
//! data providers, and concrete strategies stay in product/extension crates.

#![deny(missing_docs)]
#![deny(rustdoc::broken_intra_doc_links)]

use serde::{Deserialize, Serialize};

/// Durable persistence port for definitions, snapshots, runs and bounded reports.
#[async_trait::async_trait]
pub trait BacktestStore: Send + Sync {
    /// Save or replace a tenant-owned definition.
    async fn save_definition(
        &self,
        context: &af_context::RequestContext,
        definition: BacktestDefinition,
    ) -> Result<(), BacktestStoreError>;
    /// Persist an immutable input snapshot and start a run.
    async fn start_run(
        &self,
        context: &af_context::RequestContext,
        definition_id: &af_context::BacktestDefinitionId,
        input_snapshot: serde_json::Value,
    ) -> Result<af_context::BacktestRunId, BacktestStoreError>;
    /// Complete a run with a report below `max_bytes`.
    async fn complete_run(
        &self,
        context: &af_context::RequestContext,
        run_id: &af_context::BacktestRunId,
        report: serde_json::Value,
        max_bytes: usize,
    ) -> Result<(), BacktestStoreError>;
    /// Preserve a terminal failure for recovery diagnostics.
    async fn fail_run(
        &self,
        context: &af_context::RequestContext,
        run_id: &af_context::BacktestRunId,
        message: &str,
    ) -> Result<(), BacktestStoreError>;
}

/// Tenant-owned deterministic replay definition.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BacktestDefinition {
    /// Stable definition identity.
    pub id: af_context::BacktestDefinitionId,
    /// Product-neutral display name.
    pub name: String,
    /// Immutable model implementation version.
    pub model_version: String,
    /// Bounded model configuration.
    pub config: serde_json::Value,
}

/// Failure owned by the durable replay store.
#[derive(Debug, thiserror::Error, PartialEq, Eq)]
pub enum BacktestStoreError {
    /// Store dependency is unavailable.
    #[error("backtest store unavailable: {0}")]
    Unavailable(String),
    /// Requested record does not exist in the tenant.
    #[error("backtest record not found")]
    NotFound,
    /// Input is invalid.
    #[error("invalid backtest state: {0}")]
    Invalid(String),
    /// Report exceeds its configured serialized size cap.
    #[error("report size {actual} exceeds maximum {maximum}")]
    ReportTooLarge {
        /// Serialized report bytes.
        actual: usize,
        /// Configured maximum bytes.
        maximum: usize,
    },
}

/// Product-supplied deterministic replay behavior.
pub trait BacktestModel {
    /// Typed product error returned by decisions and transitions.
    type Error: std::error::Error + Send + Sync + 'static;
    /// Historical input event type.
    type Event;
    /// Replay state carried between events.
    type State: Clone;
    /// Decision produced by the model.
    type Action: Clone;

    /// State before the first event.
    fn initial_state(&self) -> Self::State;
    /// Starting value used for return and drawdown.
    fn initial_value(&self, state: &Self::State) -> f64;
    /// Monotonic timestamp of an event.
    fn timestamp(&self, event: &Self::Event) -> i64;

    /// Produce decisions from the current event and pre-transition state.
    fn decide(
        &mut self,
        event: &Self::Event,
        state: &Self::State,
    ) -> Result<Vec<Self::Action>, Self::Error>;

    /// Apply one decision. The model owns domain-specific transition rules.
    fn apply(
        &mut self,
        event: &Self::Event,
        action: &Self::Action,
        state: &mut Self::State,
    ) -> Result<(), Self::Error>;

    /// Mark the state after all actions for the event have been applied.
    fn value(&self, event: &Self::Event, state: &Self::State) -> f64;
}

/// Replay limits.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BacktestConfig {
    /// Hard resource bound; inputs above it fail before executing any model code.
    pub max_events: usize,
    /// Capture one detailed decision step every N events. Curve points remain full.
    pub capture_every: usize,
}

impl Default for BacktestConfig {
    fn default() -> Self {
        Self {
            max_events: 1_000_000,
            capture_every: 1,
        }
    }
}

/// Value after one event.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ValuePoint {
    /// Event timestamp.
    pub timestamp: i64,
    /// Value at this point.
    pub value: f64,
}

/// Captured decision trace for one event.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ReplayStep<A> {
    /// Index of the event in the input.
    pub event_index: usize,
    /// Event timestamp.
    pub timestamp: i64,
    /// Decisions applied for this event.
    pub actions: Vec<A>,
    /// Value at this point.
    pub value: f64,
}

/// Aggregate metrics for a replay.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BacktestSummary {
    /// Events replayed.
    pub events_processed: usize,
    /// Decisions applied.
    pub actions_applied: usize,
    /// Value before the first event.
    pub initial_value: f64,
    /// Value after the last event.
    pub final_value: f64,
    /// `final_value / initial_value - 1`.
    pub total_return: f64,
    /// Largest peak-to-trough decline as a fraction of the peak.
    pub max_drawdown: f64,
}

/// Full replay output.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BacktestReport<S, A> {
    /// Short human-readable summary.
    pub summary: BacktestSummary,
    /// State after the last event.
    pub final_state: S,
    /// Value after every event.
    pub value_curve: Vec<ValuePoint>,
    /// Captured decision traces.
    pub steps: Vec<ReplayStep<A>>,
}

/// Caps for projecting a report to chat or API consumers.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ProjectionPolicy {
    /// Maximum curve points kept; evenly down-sampled beyond this.
    pub max_curve_points: usize,
    /// Upper bound on model steps per Run.
    pub max_steps: usize,
}

impl Default for ProjectionPolicy {
    fn default() -> Self {
        Self {
            max_curve_points: 80,
            max_steps: 24,
        }
    }
}

/// A transport/chat-safe projection of a potentially large full report.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BacktestProjection<A> {
    /// Short human-readable summary.
    pub summary: BacktestSummary,
    /// Down-sampled value curve.
    pub value_curve: Vec<ValuePoint>,
    /// Capped decision traces.
    pub steps: Vec<ReplayStep<A>>,
    /// Curve points before capping.
    pub total_curve_points: usize,
    /// Steps before capping.
    pub total_steps: usize,
}

impl<S, A: Clone> BacktestReport<S, A> {
    /// Project a report under `policy` without losing the summary.
    pub fn project(
        &self,
        policy: ProjectionPolicy,
    ) -> Result<BacktestProjection<A>, BacktestError> {
        if policy.max_curve_points < 2 {
            return Err(BacktestError::InvalidProjection(
                "max_curve_points must be at least 2".into(),
            ));
        }
        if policy.max_steps == 0 {
            return Err(BacktestError::InvalidProjection(
                "max_steps must be greater than 0".into(),
            ));
        }
        Ok(BacktestProjection {
            summary: self.summary.clone(),
            value_curve: sample_evenly(&self.value_curve, policy.max_curve_points),
            steps: sample_evenly(&self.steps, policy.max_steps),
            total_curve_points: self.value_curve.len(),
            total_steps: self.steps.len(),
        })
    }
}

/// Failure while validating input or replaying.
#[derive(Debug, thiserror::Error, PartialEq)]
pub enum BacktestError {
    /// Backtest requires at least one historical event.
    #[error("backtest requires at least one historical event")]
    EmptyEvents,
    /// Max_events must be greater than 0.
    #[error("max_events must be greater than 0")]
    InvalidEventLimit,
    /// Capture_every must be greater than 0.
    #[error("capture_every must be greater than 0")]
    InvalidCaptureRate,
    /// More events than `max_events`.
    #[error("event count {actual} exceeds configured maximum {maximum}")]
    EventLimitExceeded {
        /// Events supplied.
        actual: usize,
        /// Configured `max_events`.
        maximum: usize,
    },
    /// Initial value must be finite and greater than zero, got.
    #[error("initial value must be finite and greater than zero, got {0}")]
    InvalidInitialValue(f64),
    /// Event timestamps are out of order at index `index`: `previous` > `current`.
    #[error("event timestamps are out of order at index {index}: {previous} > {current}")]
    NonMonotonicTimestamp {
        /// Index of the event being applied.
        index: usize,
        /// Timestamp of the previous event.
        previous: i64,
        /// Timestamp of the offending event.
        current: i64,
    },
    /// The model failed to decide on an event.
    #[error("decision failed at event {index}: {message}")]
    Decision {
        /// Index of the event being decided.
        index: usize,
        /// Model-reported failure.
        message: String,
    },
    /// State transition failed at event `index`, action `action_index`: `message`.
    #[error("state transition failed at event {index}, action {action_index}: {message}")]
    Transition {
        /// Index of the event being applied.
        index: usize,
        /// Index of the action within that event.
        action_index: usize,
        /// Model-reported failure.
        message: String,
    },
    /// The model produced NaN or infinity.
    #[error("model returned a non-finite value at event {index}: {value}")]
    NonFiniteValue {
        /// Index of the offending event.
        index: usize,
        /// The non-finite value.
        value: f64,
    },
    /// Invalid projection policy.
    #[error("invalid projection policy: {0}")]
    InvalidProjection(String),
    /// Replay observed cancellation between events.
    #[error("backtest cancelled")]
    Cancelled,
    /// Replay exceeded its caller-provided deadline.
    #[error("backtest deadline exceeded")]
    DeadlineExceeded,
}

/// Execute a deterministic replay. Events with the same timestamp are allowed
/// and preserve input order; decreasing timestamps fail closed.
pub fn run_backtest<M: BacktestModel>(
    model: &mut M,
    events: &[M::Event],
    config: BacktestConfig,
) -> Result<BacktestReport<M::State, M::Action>, BacktestError> {
    if events.is_empty() {
        return Err(BacktestError::EmptyEvents);
    }
    if config.max_events == 0 {
        return Err(BacktestError::InvalidEventLimit);
    }
    if config.capture_every == 0 {
        return Err(BacktestError::InvalidCaptureRate);
    }
    if events.len() > config.max_events {
        return Err(BacktestError::EventLimitExceeded {
            actual: events.len(),
            maximum: config.max_events,
        });
    }

    let timestamps = events
        .iter()
        .map(|event| model.timestamp(event))
        .collect::<Vec<_>>();
    for (index, pair) in timestamps.windows(2).enumerate() {
        if pair[1] < pair[0] {
            return Err(BacktestError::NonMonotonicTimestamp {
                index: index + 1,
                previous: pair[0],
                current: pair[1],
            });
        }
    }

    let mut state = model.initial_state();
    let initial_value = model.initial_value(&state);
    if !initial_value.is_finite() || initial_value <= 0.0 {
        return Err(BacktestError::InvalidInitialValue(initial_value));
    }

    let mut value_curve = Vec::with_capacity(events.len());
    let mut steps = Vec::with_capacity(events.len().div_ceil(config.capture_every));
    let mut actions_applied = 0;

    for (index, event) in events.iter().enumerate() {
        let timestamp = timestamps[index];

        let actions = model
            .decide(event, &state)
            .map_err(|error| BacktestError::Decision {
                index,
                message: error.to_string(),
            })?;
        for (action_index, action) in actions.iter().enumerate() {
            model
                .apply(event, action, &mut state)
                .map_err(|error| BacktestError::Transition {
                    index,
                    action_index,
                    message: error.to_string(),
                })?;
            actions_applied += 1;
        }

        let value = model.value(event, &state);
        if !value.is_finite() {
            return Err(BacktestError::NonFiniteValue { index, value });
        }
        value_curve.push(ValuePoint { timestamp, value });
        if index % config.capture_every == 0 || index + 1 == events.len() {
            steps.push(ReplayStep {
                event_index: index,
                timestamp,
                actions,
                value,
            });
        }
    }

    let final_value = value_curve
        .last()
        .map_or(initial_value, |point| point.value);
    let summary = BacktestSummary {
        events_processed: events.len(),
        actions_applied,
        initial_value,
        final_value,
        total_return: (final_value / initial_value) - 1.0,
        max_drawdown: max_drawdown(initial_value, &value_curve),
    };
    Ok(BacktestReport {
        summary,
        final_state: state,
        value_curve,
        steps,
    })
}

/// Execute a deterministic replay while observing cancellation and deadline
/// between input events.
pub fn run_backtest_with_control<M: BacktestModel>(
    model: &mut M,
    events: &[M::Event],
    config: BacktestConfig,
    cancellation: &tokio_util::sync::CancellationToken,
    deadline: std::time::Instant,
) -> Result<BacktestReport<M::State, M::Action>, BacktestError> {
    if cancellation.is_cancelled() {
        return Err(BacktestError::Cancelled);
    }
    if std::time::Instant::now() >= deadline {
        return Err(BacktestError::DeadlineExceeded);
    }
    let mut checked = CheckedModel {
        inner: model,
        cancellation,
        deadline,
    };
    run_backtest(&mut checked, events, config).map_err(|error| match &error {
        BacktestError::Decision { message, .. } | BacktestError::Transition { message, .. }
            if message == "backtest cancelled" =>
        {
            BacktestError::Cancelled
        }
        BacktestError::Decision { message, .. } | BacktestError::Transition { message, .. }
            if message == "backtest deadline exceeded" =>
        {
            BacktestError::DeadlineExceeded
        }
        _ => error,
    })
}

struct CheckedModel<'a, M> {
    inner: &'a mut M,
    cancellation: &'a tokio_util::sync::CancellationToken,
    deadline: std::time::Instant,
}

impl<M: BacktestModel> BacktestModel for CheckedModel<'_, M> {
    type Error = ControlledModelError<M::Error>;
    type Event = M::Event;
    type State = M::State;
    type Action = M::Action;

    fn initial_state(&self) -> Self::State {
        self.inner.initial_state()
    }
    fn initial_value(&self, state: &Self::State) -> f64 {
        self.inner.initial_value(state)
    }
    fn timestamp(&self, event: &Self::Event) -> i64 {
        self.inner.timestamp(event)
    }
    fn decide(
        &mut self,
        event: &Self::Event,
        state: &Self::State,
    ) -> Result<Vec<Self::Action>, Self::Error> {
        if self.cancellation.is_cancelled() {
            return Err(ControlledModelError::Cancelled);
        }
        if std::time::Instant::now() >= self.deadline {
            return Err(ControlledModelError::DeadlineExceeded);
        }
        self.inner
            .decide(event, state)
            .map_err(ControlledModelError::Model)
    }
    fn apply(
        &mut self,
        event: &Self::Event,
        action: &Self::Action,
        state: &mut Self::State,
    ) -> Result<(), Self::Error> {
        self.inner
            .apply(event, action, state)
            .map_err(ControlledModelError::Model)
    }
    fn value(&self, event: &Self::Event, state: &Self::State) -> f64 {
        self.inner.value(event, state)
    }
}

#[derive(Debug, thiserror::Error)]
enum ControlledModelError<E: std::error::Error> {
    #[error("backtest cancelled")]
    Cancelled,
    #[error("backtest deadline exceeded")]
    DeadlineExceeded,
    #[error(transparent)]
    Model(E),
}

fn max_drawdown(initial_value: f64, curve: &[ValuePoint]) -> f64 {
    let mut peak = initial_value;
    let mut maximum = 0.0_f64;
    for point in curve {
        peak = peak.max(point.value);
        if peak > 0.0 {
            maximum = maximum.max((peak - point.value) / peak);
        }
    }
    maximum
}

fn sample_evenly<T: Clone>(items: &[T], maximum: usize) -> Vec<T> {
    if items.len() <= maximum {
        return items.to_vec();
    }
    if maximum == 1 {
        return vec![items[items.len() - 1].clone()];
    }
    (0..maximum)
        .map(|slot| {
            let index = slot * (items.len() - 1) / (maximum - 1);
            items[index].clone()
        })
        .collect()
}