ff-core 0.4.0

FlowFabric core types, partition math, key builders, error codes
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
use crate::types::{BudgetId, TimestampMs};
use serde::{Deserialize, Serialize};
use std::collections::BTreeSet;

/// Capability CSV ceilings (RFC-009 §7.5). Shared between `ff-sdk` (worker
/// caps ingress) and `ff-scheduler` (worker caps ingress); mirrored in
/// `lua/helpers.lua` and enforced by `lua/scheduling.lua`/`lua/execution.lua`
/// as defense-in-depth. Inclusive: a CSV exactly CAPS_MAX_BYTES long is
/// accepted; one byte more is rejected.
pub const CAPS_MAX_BYTES: usize = 4096;
pub const CAPS_MAX_TOKENS: usize = 256;

/// Retry configuration for an execution.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct RetryPolicy {
    /// Maximum number of retry attempts (not counting the initial attempt).
    #[serde(default = "default_max_retries")]
    pub max_retries: u32,
    /// Backoff strategy.
    #[serde(default)]
    pub backoff: BackoffStrategy,
    /// Error categories eligible for automatic retry.
    #[serde(default)]
    pub retryable_categories: Vec<String>,
}

fn default_max_retries() -> u32 {
    3
}

impl Default for RetryPolicy {
    fn default() -> Self {
        Self {
            max_retries: default_max_retries(),
            backoff: BackoffStrategy::default(),
            retryable_categories: Vec::new(),
        }
    }
}

/// Backoff strategy for retries.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case", tag = "type")]
pub enum BackoffStrategy {
    /// Fixed delay between retries.
    Fixed { delay_ms: u64 },
    /// Exponential backoff with optional jitter.
    Exponential {
        initial_delay_ms: u64,
        max_delay_ms: u64,
        multiplier: f64,
        #[serde(default)]
        jitter: bool,
    },
}

impl Default for BackoffStrategy {
    fn default() -> Self {
        Self::Exponential {
            initial_delay_ms: 1000,
            max_delay_ms: 60_000,
            multiplier: 2.0,
            jitter: false,
        }
    }
}

/// Timeout configuration for an execution.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct TimeoutPolicy {
    /// Per-attempt timeout in milliseconds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub attempt_timeout_ms: Option<u64>,
    /// Total execution deadline (absolute timestamp or duration from creation).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub execution_deadline_ms: Option<u64>,
    /// Maximum number of lease-expiry reclaims before failing with max_reclaims_exceeded.
    /// Default: 100.
    #[serde(default = "default_max_reclaim_count")]
    pub max_reclaim_count: u32,
}

fn default_max_reclaim_count() -> u32 {
    100
}

/// Suspension behavior configuration.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct SuspensionPolicy {
    /// Default suspension timeout in milliseconds.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub default_timeout_ms: Option<u64>,
    /// What happens when suspension times out: "fail" or "cancel".
    #[serde(default = "default_timeout_behavior")]
    pub timeout_behavior: String,
}

fn default_timeout_behavior() -> String {
    "fail".to_owned()
}

/// Fallback chain configuration (provider/model progression).
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FallbackPolicy {
    /// Ordered list of fallback tiers.
    pub tiers: Vec<FallbackTier>,
}

/// A single tier in the fallback chain.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct FallbackTier {
    /// Provider name (e.g., "anthropic", "openai").
    pub provider: String,
    /// Model identifier.
    pub model: String,
    /// Optional per-tier timeout override in ms.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timeout_ms: Option<u64>,
}

/// Routing requirements for worker matching.
#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)]
pub struct RoutingRequirements {
    /// Required capabilities the worker must advertise. An empty set means
    /// any worker on the lane may claim (backwards-compatible default).
    /// `BTreeSet` for deterministic ordering — critical for the sorted CSV
    /// form that ff_issue_claim_grant receives in ARGV and for reproducible
    /// test output / log correlation.
    #[serde(default)]
    pub required_capabilities: BTreeSet<String>,
    /// Preferred locality/region.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub preferred_locality: Option<String>,
    /// Isolation level.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub isolation_level: Option<String>,
}

/// Stream durability and retention configuration.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct StreamPolicy {
    /// Durability mode: "buffered" (default) or "durable".
    #[serde(default = "default_durability_mode")]
    pub durability_mode: String,
    /// Maximum number of frames to retain per stream.
    #[serde(default = "default_retention_maxlen")]
    pub retention_maxlen: u64,
    /// Stream retention TTL in ms after closure.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub retention_ttl_ms: Option<u64>,
}

fn default_durability_mode() -> String {
    "buffered".to_owned()
}

fn default_retention_maxlen() -> u64 {
    10_000
}

/// Complete execution policy snapshot, frozen at creation time.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ExecutionPolicy {
    /// Higher value = higher priority. Default: 0.
    #[serde(default)]
    pub priority: i32,
    /// Earliest eligible time.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub delay_until: Option<TimestampMs>,
    /// Retry configuration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub retry_policy: Option<RetryPolicy>,
    /// Timeout configuration.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub timeout_policy: Option<TimeoutPolicy>,
    /// Maximum lease-expiry reclaims. Default: 100.
    #[serde(default = "default_max_reclaim_count")]
    pub max_reclaim_count: u32,
    /// Suspension behavior.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub suspension_policy: Option<SuspensionPolicy>,
    /// Fallback chain.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub fallback_policy: Option<FallbackPolicy>,
    /// Maximum number of replays. Default: 10.
    #[serde(default = "default_max_replay_count")]
    pub max_replay_count: u32,
    /// Attached budget references.
    #[serde(default)]
    pub budget_ids: Vec<BudgetId>,
    /// Routing requirements.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub routing_requirements: Option<RoutingRequirements>,
    /// Idempotency dedup window in ms. V1 default: 24h.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub dedup_window_ms: Option<u64>,
    /// Stream policy.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub stream_policy: Option<StreamPolicy>,
    /// Maximum signal records accepted. Default: 10000.
    #[serde(default = "default_max_signals")]
    pub max_signals_per_execution: u32,
}

impl Default for ExecutionPolicy {
    fn default() -> Self {
        Self {
            priority: 0,
            delay_until: None,
            retry_policy: None,
            timeout_policy: None,
            max_reclaim_count: default_max_reclaim_count(),
            suspension_policy: None,
            fallback_policy: None,
            max_replay_count: default_max_replay_count(),
            budget_ids: Vec::new(),
            routing_requirements: None,
            dedup_window_ms: None,
            stream_policy: None,
            max_signals_per_execution: default_max_signals(),
        }
    }
}

fn default_max_replay_count() -> u32 {
    10
}

fn default_max_signals() -> u32 {
    10_000
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn execution_policy_defaults() {
        let policy = ExecutionPolicy::default();
        assert_eq!(policy.priority, 0);
        assert_eq!(policy.max_reclaim_count, 100);
        assert_eq!(policy.max_replay_count, 10);
        assert_eq!(policy.max_signals_per_execution, 10_000);
        assert!(policy.retry_policy.is_none());
        assert!(policy.timeout_policy.is_none());
    }

    #[test]
    fn retry_policy_serde() {
        let policy = RetryPolicy {
            max_retries: 3,
            backoff: BackoffStrategy::Exponential {
                initial_delay_ms: 100,
                max_delay_ms: 30_000,
                multiplier: 2.0,
                jitter: true,
            },
            retryable_categories: vec!["timeout".into(), "provider_error".into()],
        };
        let json = serde_json::to_string(&policy).unwrap();
        let parsed: RetryPolicy = serde_json::from_str(&json).unwrap();
        assert_eq!(policy, parsed);
    }

    #[test]
    fn timeout_policy_defaults() {
        let json = r#"{"attempt_timeout_ms": 30000}"#;
        let policy: TimeoutPolicy = serde_json::from_str(json).unwrap();
        assert_eq!(policy.attempt_timeout_ms, Some(30_000));
        assert_eq!(policy.max_reclaim_count, 100);
    }

    #[test]
    fn retry_policy_defaults() {
        let policy = RetryPolicy::default();
        assert_eq!(policy.max_retries, 3);
        assert_eq!(
            policy.backoff,
            BackoffStrategy::Exponential {
                initial_delay_ms: 1000,
                max_delay_ms: 60_000,
                multiplier: 2.0,
                jitter: false,
            }
        );
        assert!(policy.retryable_categories.is_empty());
    }

    #[test]
    fn retry_policy_lua_compatible_json() {
        let policy = RetryPolicy::default();
        let json = serde_json::to_value(&policy).unwrap();
        assert_eq!(json["max_retries"], 3);
        let backoff = &json["backoff"];
        assert_eq!(backoff["type"], "exponential");
        assert_eq!(backoff["initial_delay_ms"], 1000);
        assert_eq!(backoff["max_delay_ms"], 60_000);
        assert_eq!(backoff["multiplier"], 2.0);

        let fixed = RetryPolicy {
            max_retries: 1,
            backoff: BackoffStrategy::Fixed { delay_ms: 5000 },
            retryable_categories: vec![],
        };
        let json = serde_json::to_value(&fixed).unwrap();
        assert_eq!(json["backoff"]["type"], "fixed");
        assert_eq!(json["backoff"]["delay_ms"], 5000);
    }

    #[test]
    fn retry_policy_deserialize_minimal() {
        let json = r#"{"max_retries": 5}"#;
        let policy: RetryPolicy = serde_json::from_str(json).unwrap();
        assert_eq!(policy.max_retries, 5);
        assert_eq!(policy.backoff, BackoffStrategy::default());
    }

    /// Regression: `ExecutionPolicy::default()` must not serialize any
    /// `null` fields. Lua's `cjson.decode` maps JSON `null` to a
    /// `cjson.null` sentinel (userdata), which fails the
    /// `type(field) == "table"` checks in `lua/policy.lua` and produces
    /// `invalid_policy_json:<field>:not_object` errors on every HTTP
    /// consumer that posts a default-constructed policy.
    #[test]
    fn default_execution_policy_has_no_nulls() {
        let policy = ExecutionPolicy::default();
        let json = serde_json::to_value(&policy).unwrap();
        let obj = json.as_object().expect("top-level object");
        for (key, value) in obj {
            assert!(
                !value.is_null(),
                "default ExecutionPolicy must not emit null field `{key}` — \
                 Lua policy validation rejects null for optional table fields"
            );
        }
        // Sanity: the scalar fields with defaults are still present.
        assert_eq!(obj.get("priority"), Some(&serde_json::json!(0)));
        assert_eq!(obj.get("max_reclaim_count"), Some(&serde_json::json!(100)));
    }

    /// Regression: setting a single optional field must not surface
    /// `null` for the other seven. Reproduces the original failure mode:
    /// a consumer sets `retry_policy` only, and the server rejects the
    /// request because `routing_requirements: null` is interpreted as
    /// a non-table by cjson.
    #[test]
    fn partial_execution_policy_omits_unset_options() {
        let policy = ExecutionPolicy {
            retry_policy: Some(RetryPolicy::default()),
            ..Default::default()
        };
        let json = serde_json::to_value(&policy).unwrap();
        let obj = json.as_object().expect("top-level object");
        for field in [
            "delay_until",
            "timeout_policy",
            "suspension_policy",
            "fallback_policy",
            "routing_requirements",
            "dedup_window_ms",
            "stream_policy",
        ] {
            assert!(
                !obj.contains_key(field),
                "field `{field}` must be absent when unset, not `null`"
            );
        }
        assert!(obj.contains_key("retry_policy"));
    }

    /// Round-trip a fully-populated policy to prove `skip_serializing_if`
    /// does not drop set values.
    #[test]
    fn populated_execution_policy_round_trip() {
        let policy = ExecutionPolicy {
            priority: 7,
            delay_until: Some(TimestampMs(123_456)),
            retry_policy: Some(RetryPolicy::default()),
            timeout_policy: Some(TimeoutPolicy {
                attempt_timeout_ms: Some(30_000),
                execution_deadline_ms: Some(300_000),
                max_reclaim_count: 5,
            }),
            suspension_policy: Some(SuspensionPolicy {
                default_timeout_ms: Some(60_000),
                timeout_behavior: "cancel".into(),
            }),
            fallback_policy: Some(FallbackPolicy {
                tiers: vec![FallbackTier {
                    provider: "anthropic".into(),
                    model: "claude-opus".into(),
                    timeout_ms: Some(45_000),
                }],
            }),
            routing_requirements: Some(RoutingRequirements {
                required_capabilities: BTreeSet::from(["gpu".to_owned()]),
                preferred_locality: Some("us-west-2".into()),
                isolation_level: Some("strict".into()),
            }),
            dedup_window_ms: Some(86_400_000),
            stream_policy: Some(StreamPolicy {
                durability_mode: "durable".into(),
                retention_maxlen: 5000,
                retention_ttl_ms: Some(3_600_000),
            }),
            ..Default::default()
        };
        let json = serde_json::to_string(&policy).unwrap();
        assert!(
            !json.contains(":null"),
            "populated policy must not contain null fields: {json}"
        );
        let parsed: ExecutionPolicy = serde_json::from_str(&json).unwrap();
        assert_eq!(policy, parsed);
    }

    #[test]
    fn full_execution_policy_serde() {
        let policy = ExecutionPolicy {
            priority: 10,
            retry_policy: Some(RetryPolicy {
                max_retries: 5,
                backoff: BackoffStrategy::Fixed { delay_ms: 1000 },
                retryable_categories: vec![],
            }),
            ..Default::default()
        };
        let json = serde_json::to_string(&policy).unwrap();
        let parsed: ExecutionPolicy = serde_json::from_str(&json).unwrap();
        assert_eq!(policy, parsed);
    }
}