memable 0.1.4

An embeddable durable execution engine using key-based memoisation
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
use std::fmt;
use std::time::Duration;

/// Errors originating from the engine itself.
///
/// Covers storage failures (redb), serialization failures (postcard), and
/// step execution failures propagated from user closures.
///
/// # Examples
///
/// ```
/// use memable::EngineError;
///
/// let err = EngineError::step_failed("my-step", "connection reset", false);
/// assert!(err.to_string().contains("my-step"));
/// ```
#[derive(Debug, thiserror::Error)]
pub enum EngineError {
    /// A storage operation failed.
    #[error("storage error: {0}")]
    Storage(Box<dyn std::error::Error + Send + Sync>),

    /// Serialization or deserialization of a step result failed.
    #[error("serialization error for step '{key}': {source}")]
    Serialization {
        /// The step key that failed.
        key: String,
        /// The underlying serialization error.
        #[source]
        source: Box<dyn std::error::Error + Send + Sync>,
    },

    /// A step closure returned an error.
    #[error("step '{key}' failed: {source}")]
    StepFailed {
        /// The step key that failed.
        key: String,
        /// The underlying error from the step closure.
        source: Box<dyn std::error::Error + Send + Sync>,
        /// Whether the original error was marked as retryable.
        retryable: bool,
    },

    /// The engine has not been started.
    #[error("engine has not been started")]
    NotStarted,

    /// No workflow is registered under the given name.
    #[error("workflow '{0}' is not registered")]
    WorkflowNotFound(String),

    /// A step exceeded its configured timeout.
    #[error("step '{key}' timed out after {duration:?}")]
    StepTimeout {
        /// The step key that timed out.
        key: String,
        /// The timeout duration that was exceeded.
        duration: Duration,
    },

    /// A key component contains the `/` delimiter.
    #[error("invalid key component ({label}): '{value}' must not contain '/'")]
    InvalidKey {
        /// Which component was invalid (e.g. `"workflow_name"`, `"instance_id"`, `"step_key"`).
        label: &'static str,
        /// The invalid value.
        value: String,
    },

    /// The stored payload type does not match the expected type.
    ///
    /// This occurs when [`Engine::signal`](crate::Engine::signal) delivers a
    /// payload of type `T` but the workflow's suspend point expects type `U`.
    ///
    /// # Examples
    ///
    /// ```
    /// use memable::EngineError;
    ///
    /// let err = EngineError::TypeMismatch {
    ///     key: "approval:v1".into(),
    ///     expected: "i32".into(),
    ///     found: "alloc::string::String".into(),
    /// };
    /// assert!(err.to_string().contains("type mismatch"));
    /// ```
    #[error("type mismatch for step '{key}': expected `{expected}`, found `{found}`")]
    TypeMismatch {
        /// The step key where the mismatch occurred.
        key: String,
        /// The type name expected by the deserializing code.
        expected: String,
        /// The type name found in the stored envelope.
        found: String,
    },

    /// A signal was rejected because the target step is not suspended.
    #[error("signal rejected for step '{key}': {reason}")]
    SignalRejected {
        /// The step key the signal targeted.
        key: String,
        /// Why the signal was rejected.
        reason: String,
    },

    /// A signal was superseded because another caller already claimed the
    /// suspended step.
    ///
    /// This is distinct from [`SignalRejected`](EngineError::SignalRejected),
    /// which means the step was never suspended or does not exist.
    /// `SignalSuperseded` means the step *was* suspended but another signal
    /// or timer already claimed it.
    ///
    /// # Examples
    ///
    /// ```
    /// use memable::EngineError;
    ///
    /// let err = EngineError::SignalSuperseded {
    ///     key: "approval:v1".into(),
    /// };
    /// assert!(err.to_string().contains("superseded"));
    /// ```
    #[error("signal superseded for step '{key}': another caller already claimed it")]
    SignalSuperseded {
        /// The step key that was already claimed.
        key: String,
    },

    /// A regular step found a `Suspended` entry in the step table.
    ///
    /// This means the step key was previously used as a suspend or timer
    /// point, and the workflow code has since changed to use the same key
    /// for a regular step. The engine refuses to silently re-execute the
    /// step because it would bypass the original suspend gate.
    ///
    /// To resolve this, either:
    /// - Use a new step key (e.g. `"review:v2"`) in the updated workflow
    /// - Drain or signal suspended instances before deploying the change
    ///
    /// # Examples
    ///
    /// ```
    /// use memable::EngineError;
    ///
    /// let err = EngineError::SuspendedStepConflict {
    ///     key: "approval:v1".into(),
    /// };
    /// assert!(err.to_string().contains("suspended entry"));
    /// ```
    #[error(
        "step '{key}' has a suspended entry but was called as a regular step \
         — the workflow code likely changed while an instance was suspended"
    )]
    SuspendedStepConflict {
        /// The step key with the conflicting entry.
        key: String,
    },

    /// All retry attempts for a step were exhausted.
    ///
    /// The step's last error is preserved as the [`source`](std::error::Error::source).
    /// The failed result is persisted as a dead-letter entry. Calling
    /// [`Engine::resume`](crate::Engine::resume) re-attempts the step with
    /// a fresh retry budget.
    ///
    /// # Examples
    ///
    /// ```
    /// use memable::EngineError;
    ///
    /// let err = EngineError::retries_exhausted("fetch:v1", 3, "connection refused");
    /// assert!(err.to_string().contains("3 attempts"));
    /// ```
    #[error("step '{key}' failed after {attempts} attempts: {source}")]
    RetriesExhausted {
        /// The step key that exhausted its retries.
        key: String,
        /// Total number of attempts (initial + retries).
        attempts: u32,
        /// The last error from the step closure.
        source: Box<dyn std::error::Error + Send + Sync>,
    },

    /// The workflow suspended at a step, awaiting an external signal.
    #[error("workflow suspended at step '{key}'")]
    Suspended {
        /// The step key where the workflow suspended.
        key: String,
    },
}

impl EngineError {
    /// Creates a [`StepFailed`](EngineError::StepFailed) error.
    ///
    /// # Examples
    ///
    /// ```
    /// use memable::EngineError;
    ///
    /// let err = EngineError::step_failed("my-step", "boom", false);
    /// assert!(matches!(err, EngineError::StepFailed { .. }));
    /// ```
    pub fn step_failed(
        key: impl Into<String>,
        source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
        retryable: bool,
    ) -> Self {
        Self::StepFailed {
            key: key.into(),
            source: source.into(),
            retryable,
        }
    }

    /// Creates a [`RetriesExhausted`](EngineError::RetriesExhausted) error.
    ///
    /// # Examples
    ///
    /// ```
    /// use memable::EngineError;
    ///
    /// let err = EngineError::retries_exhausted("fetch:v1", 3, "timeout");
    /// assert!(matches!(err, EngineError::RetriesExhausted { .. }));
    /// ```
    pub fn retries_exhausted(
        key: impl Into<String>,
        attempts: u32,
        source: impl Into<Box<dyn std::error::Error + Send + Sync>>,
    ) -> Self {
        Self::RetriesExhausted {
            key: key.into(),
            attempts,
            source: source.into(),
        }
    }
}

impl From<redb::DatabaseError> for EngineError {
    fn from(err: redb::DatabaseError) -> Self {
        Self::Storage(Box::new(err))
    }
}

impl From<redb::TransactionError> for EngineError {
    fn from(err: redb::TransactionError) -> Self {
        Self::Storage(Box::new(err))
    }
}

impl From<redb::TableError> for EngineError {
    fn from(err: redb::TableError) -> Self {
        Self::Storage(Box::new(err))
    }
}

impl From<redb::StorageError> for EngineError {
    fn from(err: redb::StorageError) -> Self {
        Self::Storage(Box::new(err))
    }
}

impl From<redb::CommitError> for EngineError {
    fn from(err: redb::CommitError) -> Self {
        Self::Storage(Box::new(err))
    }
}

/// Error returned by [`Engine::subscribe`](crate::Engine::subscribe).
///
/// Distinguishes between an instance that never existed and one that has
/// stale `Running` metadata from a crash (no live task backing it).
///
/// # Examples
///
/// ```
/// use memable::SubscribeError;
///
/// let err = SubscribeError::NotFound {
///     workflow_name: "etl".into(),
///     instance_id: "run-42".into(),
/// };
/// assert!(err.to_string().contains("etl"));
/// ```
#[derive(Debug, thiserror::Error)]
pub enum SubscribeError {
    /// No instance with this workflow name and ID exists.
    #[error("no instance found for workflow '{workflow_name}' with id '{instance_id}'")]
    NotFound {
        /// The workflow name that was queried.
        workflow_name: String,
        /// The instance ID that was queried.
        instance_id: String,
    },

    /// The instance has persisted `Running` metadata but no live task.
    ///
    /// This typically indicates the process crashed while the workflow was
    /// executing. The instance may be recoverable via
    /// [`Engine::resume`](crate::Engine::resume).
    #[error(
        "instance '{instance_id}' of workflow '{workflow_name}' has stale Running metadata \
         (likely crashed) — consider calling resume()"
    )]
    StaleRunning {
        /// The workflow name.
        workflow_name: String,
        /// The instance ID with stale state.
        instance_id: String,
    },

    /// A storage operation failed while reading metadata.
    #[error("storage error: {0}")]
    Storage(Box<dyn std::error::Error + Send + Sync>),
}

/// Error returned by [`Engine::state`](crate::Engine::state).
///
/// # Examples
///
/// ```
/// use memable::StateError;
///
/// let err = StateError::NotFound {
///     workflow_name: "etl".into(),
///     instance_id: "run-42".into(),
/// };
/// assert!(err.to_string().contains("etl"));
/// ```
#[derive(Debug, thiserror::Error)]
pub enum StateError {
    /// No instance with this workflow name and ID exists.
    #[error("no instance found for workflow '{workflow_name}' with id '{instance_id}'")]
    NotFound {
        /// The workflow name that was queried.
        workflow_name: String,
        /// The instance ID that was queried.
        instance_id: String,
    },

    /// A storage operation failed while reading metadata.
    #[error("storage error: {0}")]
    Storage(Box<dyn std::error::Error + Send + Sync>),
}

/// Error returned by step closures.
///
/// The variant communicates retry intent to the engine:
/// - [`Retryable`](StepError::Retryable) — transient failure that the engine
///   will retry according to the step's [`RetryPolicy`](crate::RetryPolicy).
/// - [`Permanent`](StepError::Permanent) — unrecoverable failure, propagated
///   immediately without retrying.
///
/// There is intentionally no blanket [`From`] implementation. Callers must
/// choose explicitly whether a given error is retryable or permanent.
///
/// # Examples
///
/// ```
/// use memable::StepError;
///
/// let err = StepError::permanent("invalid input");
/// assert!(err.to_string().contains("permanent"));
/// ```
#[derive(Debug)]
pub enum StepError {
    /// A transient failure that may succeed on retry.
    Retryable(Box<dyn std::error::Error + Send + Sync>),
    /// An unrecoverable failure.
    Permanent(Box<dyn std::error::Error + Send + Sync>),
}

impl StepError {
    /// Creates a [`Retryable`](StepError::Retryable) error.
    ///
    /// # Examples
    ///
    /// ```
    /// use memable::StepError;
    ///
    /// let err = StepError::retryable("timeout");
    /// assert!(matches!(err, StepError::Retryable(_)));
    /// ```
    pub fn retryable(source: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self {
        Self::Retryable(source.into())
    }

    /// Creates a [`Permanent`](StepError::Permanent) error.
    ///
    /// # Examples
    ///
    /// ```
    /// use memable::StepError;
    ///
    /// let err = StepError::permanent("bad input");
    /// assert!(matches!(err, StepError::Permanent(_)));
    /// ```
    pub fn permanent(source: impl Into<Box<dyn std::error::Error + Send + Sync>>) -> Self {
        Self::Permanent(source.into())
    }
}

impl fmt::Display for StepError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Retryable(e) => write!(f, "retryable: {e}"),
            Self::Permanent(e) => write!(f, "permanent: {e}"),
        }
    }
}

impl std::error::Error for StepError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::Retryable(e) | Self::Permanent(e) => Some(e.as_ref()),
        }
    }
}