runifold-workflow 0.3.1

Durable workflow and Agent orchestration runtime 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
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
use std::{collections::BTreeMap, fmt, num::NonZeroU16, sync::Arc};

use runifold_core::{
    Checkpoint, CheckpointError, CheckpointErrorKind, CheckpointId, CheckpointStore, RunContext,
    RunId, Usage,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;

use crate::{StepId, WorkflowError, WorkflowOutcome, WorkflowWait};
use crate::{WorkflowLease, WorkflowStore};

const CHECKPOINT_KIND: &str = "runifold.workflow";
const CHECKPOINT_SCHEMA_VERSION: u32 = 4;
const MIN_CHECKPOINT_SCHEMA_VERSION: u32 = 3;

/// Recovery behavior for a workflow interrupted inside one node.
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
#[non_exhaustive]
pub enum WorkflowResumePolicy {
    /// Reject recovery that could duplicate model cost or external effects.
    #[default]
    RejectAmbiguous,
    /// Explicitly retry only the interrupted workflow node.
    RetryInterruptedStep,
}

/// Maximum number of immutable checkpoint revisions returned by one query.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct WorkflowCheckpointHistoryLimit(NonZeroU16);

impl WorkflowCheckpointHistoryLimit {
    /// Creates a bounded history page limit.
    ///
    /// # Errors
    ///
    /// Rejects zero and values above 256.
    pub fn new(value: u16) -> Result<Self, CheckpointError> {
        NonZeroU16::new(value)
            .filter(|value| value.get() <= 256)
            .map(Self)
            .ok_or_else(|| {
                CheckpointError::new(
                    CheckpointErrorKind::InvalidPayload,
                    "workflow checkpoint history limit must be in 1..=256",
                )
            })
    }

    /// Returns the validated page size.
    pub const fn get(self) -> u16 {
        self.0.get()
    }
}

/// Immutable, typed view of one historical workflow checkpoint revision.
#[derive(Clone, Debug, PartialEq)]
pub struct WorkflowCheckpointRevision {
    /// Workflow checkpoint whose history owns this revision.
    pub checkpoint_id: CheckpointId,
    /// Monotonic revision within the checkpoint.
    pub revision: u64,
    /// Run that produced the immutable revision.
    pub run_id: RunId,
    /// Store timestamp carried by the checkpoint envelope.
    pub updated_at_ms: u64,
    /// Validated workflow state at this revision.
    pub state: WorkflowCheckpointState,
}

impl WorkflowCheckpointRevision {
    #[doc(hidden)]
    pub fn from_checkpoint(checkpoint: Checkpoint) -> Result<Self, CheckpointError> {
        decode_revision(checkpoint)
    }
}

/// Explicit safety policy for forking a historical checkpoint.
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum WorkflowForkPolicy {
    /// Fork only from a stable boundary that cannot repeat an ambiguous node.
    #[default]
    RejectAmbiguous,
    /// Re-run one serial node whose checkpoint was persisted as in-flight.
    RetryInterruptedStep,
}

/// Idempotent command that creates a new execution from immutable history.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct WorkflowForkCommand {
    /// Caller-stable identity of the new execution branch.
    pub fork_checkpoint_id: CheckpointId,
    /// Existing workflow whose history is being selected.
    pub source_checkpoint_id: CheckpointId,
    /// Exact immutable source revision.
    pub source_revision: u64,
    /// Explicit ambiguous-replay policy.
    pub policy: WorkflowForkPolicy,
}

impl WorkflowForkCommand {
    /// Creates a fork command with a generated target identity.
    pub fn new(
        source_checkpoint_id: CheckpointId,
        source_revision: u64,
        policy: WorkflowForkPolicy,
    ) -> Self {
        Self::with_id(
            CheckpointId::new(),
            source_checkpoint_id,
            source_revision,
            policy,
        )
    }

    /// Creates a retryable fork command with a caller-owned target identity.
    pub const fn with_id(
        fork_checkpoint_id: CheckpointId,
        source_checkpoint_id: CheckpointId,
        source_revision: u64,
        policy: WorkflowForkPolicy,
    ) -> Self {
        Self {
            fork_checkpoint_id,
            source_checkpoint_id,
            source_revision,
            policy,
        }
    }

    #[doc(hidden)]
    pub fn prepare_checkpoint(&self, source: Checkpoint) -> Result<Checkpoint, CheckpointError> {
        fork_checkpoint(source, self.fork_checkpoint_id, self.policy)
    }
}

/// Immutable parent relationship of one forked workflow execution.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct WorkflowLineage {
    /// Source workflow checkpoint.
    pub parent_checkpoint_id: CheckpointId,
    /// Exact parent revision selected for the fork.
    pub parent_revision: u64,
    /// Safety policy used to create the child.
    pub policy: WorkflowForkPolicy,
}

/// Result of an idempotent workflow fork command.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum WorkflowForkOutcome {
    /// A new workflow branch was atomically created.
    Created {
        /// New branch identity.
        checkpoint_id: CheckpointId,
    },
    /// The same target identity was already bound to the same source.
    Duplicate {
        /// Existing branch identity.
        checkpoint_id: CheckpointId,
    },
}

/// Persisted workflow execution phase.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "state", rename_all = "snake_case")]
#[non_exhaustive]
pub enum WorkflowCheckpointPhase {
    /// Stable output is ready for the next node.
    Ready,
    /// One node may have partially executed.
    StepInFlight {
        /// Stable interrupted node identity.
        step: StepId,
    },
    /// The worker lease was released while this node awaits a durable wake.
    Waiting {
        /// Stable waiting node identity.
        step: StepId,
        /// Durable wake condition.
        wait: WorkflowWait,
    },
    /// A parallel node has one or more incomplete branches.
    ParallelInFlight {
        /// Stable parallel node identity.
        step: StepId,
        /// Durable branch progress keyed independently of completion order.
        branches: BTreeMap<StepId, ParallelBranchCheckpoint>,
    },
    /// A first-success race has no durable winner yet, or awaits commit.
    RaceInFlight {
        /// Stable race node identity.
        step: StepId,
        /// Durable branch progress keyed independently of completion order.
        branches: BTreeMap<StepId, ParallelBranchCheckpoint>,
    },
    /// The workflow reached a terminal output.
    Completed {
        /// Complete canonical workflow result.
        outcome: WorkflowOutcome,
    },
}

/// Persisted state of one branch inside an in-flight parallel node.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "state", rename_all = "snake_case")]
#[non_exhaustive]
pub enum ParallelBranchCheckpoint {
    /// The branch may have partially executed.
    InFlight,
    /// The branch returned a stable canonical output.
    Completed {
        /// Canonical branch output.
        output: Value,
    },
    /// The branch returned a known failure.
    Failed {
        /// Safe persisted failure explanation.
        message: String,
    },
    /// The branch was abandoned after another branch won.
    Cancelled,
}

/// Versioned workflow state stored in a domain-neutral checkpoint envelope.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct WorkflowCheckpointState {
    /// Stable workflow definition name.
    pub workflow: String,
    /// Caller-managed definition version.
    pub workflow_version: u32,
    /// Ordered node layout used to reject incompatible definitions.
    pub layout: Vec<StepId>,
    /// Index of the next node to execute.
    pub next_index: usize,
    /// Canonical value presented to the next node.
    pub value: Value,
    /// Stable outputs of all completed nodes.
    pub outputs: BTreeMap<StepId, Value>,
    /// Shared usage snapshot at persistence time.
    pub usage: Usage,
    /// Current recovery phase.
    pub phase: WorkflowCheckpointPhase,
}

impl WorkflowCheckpointState {
    pub(crate) fn outcome(&self) -> Option<WorkflowOutcome> {
        match &self.phase {
            WorkflowCheckpointPhase::Completed { outcome } => Some(outcome.clone()),
            _ => None,
        }
    }
}

/// Stable handle binding one workflow checkpoint identity to a store.
#[derive(Clone)]
pub struct WorkflowCheckpoint {
    id: CheckpointId,
    backend: WorkflowCheckpointBackend,
}

#[derive(Clone)]
enum WorkflowCheckpointBackend {
    Local(Arc<dyn CheckpointStore>),
    Distributed {
        store: Arc<dyn WorkflowStore>,
        lease: WorkflowLease,
    },
}

impl WorkflowCheckpoint {
    /// Creates a new workflow checkpoint handle.
    pub fn new(store: Arc<dyn CheckpointStore>) -> Self {
        Self {
            id: CheckpointId::new(),
            backend: WorkflowCheckpointBackend::Local(store),
        }
    }

    /// Reconnects to an existing workflow checkpoint.
    pub fn existing(id: CheckpointId, store: Arc<dyn CheckpointStore>) -> Self {
        Self {
            id,
            backend: WorkflowCheckpointBackend::Local(store),
        }
    }

    /// Binds a distributed checkpoint to the current fenced workflow lease.
    pub fn distributed(store: Arc<dyn WorkflowStore>, lease: WorkflowLease) -> Self {
        Self {
            id: lease.checkpoint_id,
            backend: WorkflowCheckpointBackend::Distributed { store, lease },
        }
    }

    /// Returns the stable checkpoint identity.
    pub const fn id(&self) -> CheckpointId {
        self.id
    }

    /// Loads and validates the latest typed workflow state.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointError`] for storage or payload failures.
    pub fn load(&self) -> Result<(Checkpoint, WorkflowCheckpointState), CheckpointError> {
        let WorkflowCheckpointBackend::Local(store) = &self.backend else {
            return Err(CheckpointError::new(
                CheckpointErrorKind::Storage,
                "distributed workflow checkpoints must be loaded asynchronously",
            ));
        };
        let checkpoint = store.load(self.id)?;
        decode(checkpoint)
    }

    /// Loads and validates either a local or distributed checkpoint.
    ///
    /// # Errors
    ///
    /// Returns [`CheckpointError`] for ownership, storage, or payload failures.
    pub async fn load_async(
        &self,
    ) -> Result<(Checkpoint, WorkflowCheckpointState), CheckpointError> {
        let checkpoint = match &self.backend {
            WorkflowCheckpointBackend::Local(store) => store.load(self.id)?,
            WorkflowCheckpointBackend::Distributed { store, lease } => {
                store.load_checkpoint(lease.clone()).await?
            }
        };
        decode(checkpoint)
    }

    async fn compare_and_swap(
        &self,
        checkpoint: &Checkpoint,
        expected_revision: Option<u64>,
    ) -> Result<(), CheckpointError> {
        match &self.backend {
            WorkflowCheckpointBackend::Local(store) => {
                store.compare_and_swap(checkpoint, expected_revision)
            }
            WorkflowCheckpointBackend::Distributed { store, lease } => {
                store
                    .compare_and_swap_checkpoint(
                        lease.clone(),
                        checkpoint.clone(),
                        expected_revision,
                    )
                    .await
            }
        }
    }
}

fn decode(
    checkpoint: Checkpoint,
) -> Result<(Checkpoint, WorkflowCheckpointState), CheckpointError> {
    if checkpoint.kind != CHECKPOINT_KIND
        || !(MIN_CHECKPOINT_SCHEMA_VERSION..=CHECKPOINT_SCHEMA_VERSION)
            .contains(&checkpoint.schema_version)
    {
        return Err(CheckpointError::new(
            CheckpointErrorKind::InvalidPayload,
            "checkpoint kind or schema version is not supported",
        ));
    }
    let state = serde_json::from_value(checkpoint.payload.clone()).map_err(|error| {
        CheckpointError::new(CheckpointErrorKind::InvalidPayload, error.to_string())
    })?;
    Ok((checkpoint, state))
}

pub(crate) fn decode_revision(
    checkpoint: Checkpoint,
) -> Result<WorkflowCheckpointRevision, CheckpointError> {
    let (checkpoint, state) = decode(checkpoint)?;
    Ok(WorkflowCheckpointRevision {
        checkpoint_id: checkpoint.id,
        revision: checkpoint.revision,
        run_id: checkpoint.run_id,
        updated_at_ms: checkpoint.updated_at_ms,
        state,
    })
}

pub(crate) fn fork_checkpoint(
    source: Checkpoint,
    target: CheckpointId,
    policy: WorkflowForkPolicy,
) -> Result<Checkpoint, CheckpointError> {
    let (_, mut state) = decode(source)?;
    match &state.phase {
        WorkflowCheckpointPhase::StepInFlight { .. }
            if policy == WorkflowForkPolicy::RetryInterruptedStep =>
        {
            state.phase = WorkflowCheckpointPhase::Ready;
        }
        WorkflowCheckpointPhase::StepInFlight { .. }
        | WorkflowCheckpointPhase::ParallelInFlight { .. }
        | WorkflowCheckpointPhase::RaceInFlight { .. } => {
            return Err(CheckpointError::new(
                CheckpointErrorKind::Conflict,
                "workflow checkpoint is ambiguous and cannot be forked safely",
            ));
        }
        _ => {}
    }
    Ok(Checkpoint::initial(
        target,
        RunId::new(),
        CHECKPOINT_KIND,
        CHECKPOINT_SCHEMA_VERSION,
        serde_json::to_value(state).map_err(|error| {
            CheckpointError::new(CheckpointErrorKind::InvalidPayload, error.to_string())
        })?,
    ))
}

impl fmt::Debug for WorkflowCheckpoint {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("WorkflowCheckpoint")
            .field("id", &self.id)
            .finish_non_exhaustive()
    }
}

pub(crate) struct WorkflowCheckpointCursor {
    handle: WorkflowCheckpoint,
    envelope: Checkpoint,
}

impl WorkflowCheckpointCursor {
    pub(crate) async fn create(
        handle: &WorkflowCheckpoint,
        run: &RunContext,
        state: &WorkflowCheckpointState,
    ) -> Result<Self, WorkflowError> {
        let envelope = Checkpoint::initial(
            handle.id,
            run.run_id(),
            CHECKPOINT_KIND,
            CHECKPOINT_SCHEMA_VERSION,
            serialize(state)?,
        );
        handle.compare_and_swap(&envelope, None).await?;
        Ok(Self {
            handle: handle.clone(),
            envelope,
        })
    }

    pub(crate) fn loaded(handle: &WorkflowCheckpoint, envelope: Checkpoint) -> Self {
        Self {
            handle: handle.clone(),
            envelope,
        }
    }

    pub(crate) async fn save(
        &mut self,
        state: &WorkflowCheckpointState,
    ) -> Result<(), WorkflowError> {
        let next = self.envelope.next(serialize(state)?)?;
        self.handle
            .compare_and_swap(&next, Some(self.envelope.revision))
            .await?;
        self.envelope = next;
        Ok(())
    }
}

fn serialize(state: &WorkflowCheckpointState) -> Result<Value, WorkflowError> {
    serde_json::to_value(state).map_err(|error| {
        CheckpointError::new(CheckpointErrorKind::InvalidPayload, error.to_string()).into()
    })
}

#[cfg(test)]
mod tests {
    use runifold_core::{CheckpointId, RunId, Usage};
    use serde_json::json;

    use super::*;

    fn in_flight_checkpoint() -> Checkpoint {
        let state = WorkflowCheckpointState {
            workflow: "fork-test".into(),
            workflow_version: 1,
            layout: vec![StepId::parse("charge").unwrap()],
            next_index: 0,
            value: json!({"amount": 42}),
            outputs: BTreeMap::new(),
            usage: Usage {
                tokens: 7,
                ..Usage::default()
            },
            phase: WorkflowCheckpointPhase::StepInFlight {
                step: StepId::parse("charge").unwrap(),
            },
        };
        Checkpoint::initial(
            CheckpointId::new(),
            RunId::new(),
            CHECKPOINT_KIND,
            CHECKPOINT_SCHEMA_VERSION,
            serde_json::to_value(state).unwrap(),
        )
    }

    #[test]
    fn fork_rejects_ambiguous_replay_unless_explicitly_authorized() {
        let source = in_flight_checkpoint();
        let error = fork_checkpoint(
            source.clone(),
            CheckpointId::new(),
            WorkflowForkPolicy::RejectAmbiguous,
        )
        .unwrap_err();
        assert_eq!(error.kind, CheckpointErrorKind::Conflict);

        let forked = fork_checkpoint(
            source,
            CheckpointId::new(),
            WorkflowForkPolicy::RetryInterruptedStep,
        )
        .unwrap();
        let revision = decode_revision(forked).unwrap();
        assert!(matches!(
            revision.state.phase,
            WorkflowCheckpointPhase::Ready
        ));
        assert_eq!(revision.state.usage.tokens, 7);
        assert_eq!(revision.state.next_index, 0);
    }
}