ff-core 0.3.4

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
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
use serde::{Deserialize, Serialize};

// ── Dimension A — Lifecycle Phase ──

/// What major phase of existence is this execution in?
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LifecyclePhase {
    /// Accepted by engine, not yet resolved to runnable/delayed. Transient.
    Submitted,
    /// Eligible or potentially eligible for claiming.
    Runnable,
    /// Currently owned by a worker lease and in progress.
    Active,
    /// Intentionally paused, waiting for signal/approval/callback.
    Suspended,
    /// Execution is finished. No further state transitions except replay.
    Terminal,
}

// ── Dimension B — Ownership State ──

/// Who, if anyone, is currently allowed to mutate active execution state?
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum OwnershipState {
    /// No current lease.
    Unowned,
    /// A worker holds a valid lease.
    Leased,
    /// Lease TTL passed without renewal. Execution awaits reclaim.
    LeaseExpiredReclaimable,
    /// Lease was explicitly revoked by operator or engine.
    LeaseRevoked,
}

// ── Dimension C — Eligibility State ──

/// Can the execution be claimed for work right now?
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EligibilityState {
    /// Ready for claiming.
    EligibleNow,
    /// Delayed until a future timestamp.
    NotEligibleUntilTime,
    /// Waiting on upstream executions in a flow/DAG.
    BlockedByDependencies,
    /// Budget limit reached.
    BlockedByBudget,
    /// Quota or rate-limit reached.
    BlockedByQuota,
    /// No capable/available worker matches requirements.
    BlockedByRoute,
    /// Lane is paused or draining.
    BlockedByLaneState,
    /// Operator hold.
    BlockedByOperator,
    /// Used when lifecycle_phase is active, suspended, or terminal.
    NotApplicable,
}

// ── Dimension D — Blocking Reason ──

/// What is the most specific explanation for lack of forward progress?
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BlockingReason {
    /// Not blocked.
    None,
    /// Eligible but no worker has claimed yet.
    WaitingForWorker,
    /// Delayed for retry backoff.
    WaitingForRetryBackoff,
    /// Delayed after suspension resume.
    WaitingForResumeDelay,
    /// Worker-initiated explicit delay.
    WaitingForDelay,
    /// Suspended, waiting for a generic signal.
    WaitingForSignal,
    /// Suspended, waiting for human approval.
    WaitingForApproval,
    /// Suspended, waiting for external callback.
    WaitingForCallback,
    /// Suspended, waiting for tool completion.
    WaitingForToolResult,
    /// Blocked on child/dependency executions.
    WaitingForChildren,
    /// Budget exhausted.
    WaitingForBudget,
    /// Quota/rate-limit window full.
    WaitingForQuota,
    /// No worker with required capabilities available.
    WaitingForCapableWorker,
    /// No worker in required region/locality.
    WaitingForLocalityMatch,
    /// Operator placed a hold.
    PausedByOperator,
    /// Policy rule prevents progress (e.g. lane pause).
    PausedByPolicy,
    /// Flow cancellation with let_active_finish blocked this unclaimed member.
    PausedByFlowCancel,
}

// ── Dimension E — Terminal Outcome ──

/// If the execution is terminal, how did it end?
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum TerminalOutcome {
    /// Not terminal.
    None,
    /// Completed successfully with result.
    Success,
    /// Failed after exhausting retries or by explicit failure.
    Failed,
    /// Intentionally terminated by user, operator, or policy.
    Cancelled,
    /// Deadline, TTL, or suspension timeout elapsed.
    Expired,
    /// Required dependency failed, making this execution impossible.
    Skipped,
}

// ── Dimension F — Attempt State ──

/// What is happening at the concrete run-attempt layer?
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AttemptState {
    /// No attempt context (e.g. freshly submitted, or skipped before any attempt).
    None,
    /// Awaiting initial claim.
    PendingFirstAttempt,
    /// An attempt is actively executing.
    RunningAttempt,
    /// Current attempt was interrupted (crash, reclaim, suspension, delay, waiting children).
    AttemptInterrupted,
    /// Awaiting retry after failure.
    PendingRetryAttempt,
    /// Awaiting replay after terminal state.
    PendingReplayAttempt,
    /// The final attempt has concluded.
    AttemptTerminal,
}

// ── Derived: Public State ──

/// Engine-computed user-facing label. Derived from the state vector.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum PublicState {
    /// Eligible and waiting for a worker to claim.
    Waiting,
    /// Not yet eligible due to time-based delay.
    Delayed,
    /// Blocked by budget, quota, or rate-limit policy.
    RateLimited,
    /// Blocked on child/dependency executions.
    WaitingChildren,
    /// Currently being processed by a worker.
    Active,
    /// Intentionally paused, waiting for signal/approval/callback.
    Suspended,
    /// Terminal: finished successfully.
    Completed,
    /// Terminal: finished unsuccessfully.
    Failed,
    /// Terminal: intentionally terminated.
    Cancelled,
    /// Terminal: deadline/TTL elapsed.
    Expired,
    /// Terminal: impossible to run because required dependency failed.
    Skipped,
}

// ── State Vector ──

/// The full 6+1 dimension execution state vector.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct StateVector {
    pub lifecycle_phase: LifecyclePhase,
    pub ownership_state: OwnershipState,
    pub eligibility_state: EligibilityState,
    pub blocking_reason: BlockingReason,
    pub terminal_outcome: TerminalOutcome,
    pub attempt_state: AttemptState,
    /// Engine-derived user-facing label.
    pub public_state: PublicState,
}

impl StateVector {
    /// Derive public_state from the other 6 dimensions per RFC-001 §2.4.
    ///
    /// Never panics. In distributed systems, constraint violations can occur
    /// via partial writes or reconciler drift. Impossible combinations log a
    /// warning and return a safe fallback instead of crashing.
    pub fn derive_public_state(&self) -> PublicState {
        match self.lifecycle_phase {
            LifecyclePhase::Terminal => match self.terminal_outcome {
                TerminalOutcome::Success => PublicState::Completed,
                TerminalOutcome::Failed => PublicState::Failed,
                TerminalOutcome::Cancelled => PublicState::Cancelled,
                TerminalOutcome::Expired => PublicState::Expired,
                TerminalOutcome::Skipped => PublicState::Skipped,
                TerminalOutcome::None => {
                    // V4 violation: terminal without outcome. Corrupt state —
                    // surface as Failed so operators notice and investigate.
                    // No logging here (ff-core has no tracing dep); callers
                    // detect this via is_consistent() returning false.
                    PublicState::Failed
                }
            },
            LifecyclePhase::Suspended => PublicState::Suspended,
            LifecyclePhase::Active => PublicState::Active,
            LifecyclePhase::Runnable => match self.eligibility_state {
                EligibilityState::EligibleNow => PublicState::Waiting,
                EligibilityState::NotEligibleUntilTime => PublicState::Delayed,
                EligibilityState::BlockedByDependencies => PublicState::WaitingChildren,
                EligibilityState::BlockedByBudget | EligibilityState::BlockedByQuota => {
                    PublicState::RateLimited
                }
                EligibilityState::BlockedByRoute
                | EligibilityState::BlockedByLaneState
                | EligibilityState::BlockedByOperator => PublicState::Waiting,
                EligibilityState::NotApplicable => {
                    // Constraint violation: runnable should not have not_applicable.
                    // Surface as Waiting — the index reconciler will correct this.
                    PublicState::Waiting
                }
            },
            LifecyclePhase::Submitted => PublicState::Waiting,
        }
    }

    /// Check if the stored public_state matches the derived value.
    pub fn is_consistent(&self) -> bool {
        self.public_state == self.derive_public_state()
    }
}

// ── Attempt Lifecycle (RFC-002) ──

/// Per-attempt lifecycle states.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AttemptLifecycle {
    /// Attempt record exists; worker has not yet acquired a lease.
    Created,
    /// Worker has acquired a lease and is actively executing.
    Started,
    /// Attempt is paused because the execution intentionally suspended.
    Suspended,
    /// Attempt completed successfully.
    EndedSuccess,
    /// Attempt failed.
    EndedFailure,
    /// Attempt was cancelled.
    EndedCancelled,
    /// Attempt was interrupted by lease expiry/revocation and the execution was reclaimed.
    InterruptedReclaimed,
}

impl AttemptLifecycle {
    /// Whether this attempt lifecycle state is terminal.
    pub fn is_terminal(self) -> bool {
        matches!(
            self,
            Self::EndedSuccess
                | Self::EndedFailure
                | Self::EndedCancelled
                | Self::InterruptedReclaimed
        )
    }
}

// ── Attempt Type (RFC-002) ──

/// Why this attempt was created.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum AttemptType {
    /// First attempt for this execution.
    Initial,
    /// Retry after a failed attempt.
    Retry,
    /// Reclaim after lease expiry or revocation.
    Reclaim,
    /// Replay of a terminal execution.
    Replay,
    /// Fallback progression to next provider/model.
    Fallback,
}

// ── Lane State (RFC-009) ──

/// Lane operational state.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum LaneState {
    /// Lane is accepting and processing work.
    Active,
    /// Lane is accepting submissions but not claiming (paused).
    Paused,
    /// Lane is not accepting new submissions but processing existing.
    Draining,
    /// Lane is disabled.
    Disabled,
}

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

    #[test]
    fn derive_public_state_terminal_success() {
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Terminal,
            ownership_state: OwnershipState::Unowned,
            eligibility_state: EligibilityState::NotApplicable,
            blocking_reason: BlockingReason::None,
            terminal_outcome: TerminalOutcome::Success,
            attempt_state: AttemptState::AttemptTerminal,
            public_state: PublicState::Completed,
        };
        assert_eq!(sv.derive_public_state(), PublicState::Completed);
        assert!(sv.is_consistent());
    }

    #[test]
    fn derive_public_state_active() {
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Active,
            ownership_state: OwnershipState::Leased,
            eligibility_state: EligibilityState::NotApplicable,
            blocking_reason: BlockingReason::None,
            terminal_outcome: TerminalOutcome::None,
            attempt_state: AttemptState::RunningAttempt,
            public_state: PublicState::Active,
        };
        assert_eq!(sv.derive_public_state(), PublicState::Active);
        assert!(sv.is_consistent());
    }

    #[test]
    fn derive_public_state_active_lease_expired_still_active() {
        // D3: Active dominates ownership nuance
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Active,
            ownership_state: OwnershipState::LeaseExpiredReclaimable,
            eligibility_state: EligibilityState::NotApplicable,
            blocking_reason: BlockingReason::None,
            terminal_outcome: TerminalOutcome::None,
            attempt_state: AttemptState::AttemptInterrupted,
            public_state: PublicState::Active,
        };
        assert_eq!(sv.derive_public_state(), PublicState::Active);
    }

    #[test]
    fn derive_public_state_runnable_eligible() {
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Runnable,
            ownership_state: OwnershipState::Unowned,
            eligibility_state: EligibilityState::EligibleNow,
            blocking_reason: BlockingReason::WaitingForWorker,
            terminal_outcome: TerminalOutcome::None,
            attempt_state: AttemptState::PendingFirstAttempt,
            public_state: PublicState::Waiting,
        };
        assert_eq!(sv.derive_public_state(), PublicState::Waiting);
    }

    #[test]
    fn derive_public_state_delayed() {
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Runnable,
            ownership_state: OwnershipState::Unowned,
            eligibility_state: EligibilityState::NotEligibleUntilTime,
            blocking_reason: BlockingReason::WaitingForRetryBackoff,
            terminal_outcome: TerminalOutcome::None,
            attempt_state: AttemptState::PendingRetryAttempt,
            public_state: PublicState::Delayed,
        };
        assert_eq!(sv.derive_public_state(), PublicState::Delayed);
    }

    #[test]
    fn derive_public_state_waiting_children() {
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Runnable,
            ownership_state: OwnershipState::Unowned,
            eligibility_state: EligibilityState::BlockedByDependencies,
            blocking_reason: BlockingReason::WaitingForChildren,
            terminal_outcome: TerminalOutcome::None,
            attempt_state: AttemptState::PendingFirstAttempt,
            public_state: PublicState::WaitingChildren,
        };
        assert_eq!(sv.derive_public_state(), PublicState::WaitingChildren);
    }

    #[test]
    fn derive_public_state_rate_limited() {
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Runnable,
            ownership_state: OwnershipState::Unowned,
            eligibility_state: EligibilityState::BlockedByBudget,
            blocking_reason: BlockingReason::WaitingForBudget,
            terminal_outcome: TerminalOutcome::None,
            attempt_state: AttemptState::PendingFirstAttempt,
            public_state: PublicState::RateLimited,
        };
        assert_eq!(sv.derive_public_state(), PublicState::RateLimited);
    }

    #[test]
    fn derive_public_state_suspended() {
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Suspended,
            ownership_state: OwnershipState::Unowned,
            eligibility_state: EligibilityState::NotApplicable,
            blocking_reason: BlockingReason::WaitingForApproval,
            terminal_outcome: TerminalOutcome::None,
            attempt_state: AttemptState::AttemptInterrupted,
            public_state: PublicState::Suspended,
        };
        assert_eq!(sv.derive_public_state(), PublicState::Suspended);
    }

    #[test]
    fn derive_public_state_submitted_collapses_to_waiting() {
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Submitted,
            ownership_state: OwnershipState::Unowned,
            eligibility_state: EligibilityState::NotApplicable,
            blocking_reason: BlockingReason::None,
            terminal_outcome: TerminalOutcome::None,
            attempt_state: AttemptState::None,
            public_state: PublicState::Waiting,
        };
        assert_eq!(sv.derive_public_state(), PublicState::Waiting);
    }

    #[test]
    fn derive_public_state_skipped() {
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Terminal,
            ownership_state: OwnershipState::Unowned,
            eligibility_state: EligibilityState::NotApplicable,
            blocking_reason: BlockingReason::None,
            terminal_outcome: TerminalOutcome::Skipped,
            attempt_state: AttemptState::None,
            public_state: PublicState::Skipped,
        };
        assert_eq!(sv.derive_public_state(), PublicState::Skipped);
    }

    #[test]
    fn attempt_lifecycle_terminal_check() {
        assert!(AttemptLifecycle::EndedSuccess.is_terminal());
        assert!(AttemptLifecycle::EndedFailure.is_terminal());
        assert!(AttemptLifecycle::EndedCancelled.is_terminal());
        assert!(AttemptLifecycle::InterruptedReclaimed.is_terminal());
        assert!(!AttemptLifecycle::Created.is_terminal());
        assert!(!AttemptLifecycle::Started.is_terminal());
        assert!(!AttemptLifecycle::Suspended.is_terminal());
    }

    #[test]
    fn serde_roundtrip_lifecycle_phase() {
        let phase = LifecyclePhase::Active;
        let json = serde_json::to_string(&phase).unwrap();
        assert_eq!(json, "\"active\"");
        let parsed: LifecyclePhase = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, phase);
    }

    #[test]
    fn serde_roundtrip_blocking_reason() {
        let reason = BlockingReason::PausedByFlowCancel;
        let json = serde_json::to_string(&reason).unwrap();
        assert_eq!(json, "\"paused_by_flow_cancel\"");
        let parsed: BlockingReason = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, reason);
    }

    #[test]
    fn serde_roundtrip_state_vector() {
        let sv = StateVector {
            lifecycle_phase: LifecyclePhase::Active,
            ownership_state: OwnershipState::Leased,
            eligibility_state: EligibilityState::NotApplicable,
            blocking_reason: BlockingReason::None,
            terminal_outcome: TerminalOutcome::None,
            attempt_state: AttemptState::RunningAttempt,
            public_state: PublicState::Active,
        };
        let json = serde_json::to_string(&sv).unwrap();
        let parsed: StateVector = serde_json::from_str(&json).unwrap();
        assert_eq!(sv, parsed);
    }
}