taquba-workflow 0.13.0

Durable, at-least-once workflow runtime on top of the Taquba task queue. Particularly well-suited for AI agent runs.
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
//! Stored forms of the runtime's public types. Each `Durable*` type
//! mirrors a public type and is what actually serializes, so the public
//! type can evolve without changing the stored layout. Records are
//! MessagePack maps with named fields, written through [`encode`] and
//! read through [`decode`] or, for a record at one key of the queue's
//! KV namespace, [`kv_record`].
//!
//! A record that fails to decode is treated in one of two ways, chosen
//! by what its absence means to the reader, and each treatment has a
//! function of its own. Where absence means that the work runs again, the reader
//! calls [`decode_or_absent`], which logs the failure and reports the
//! record as absent: a memo entry is recomputed, a step-output replay
//! record re-executes the step, a run result record is reported as
//! missing, and a member record skipped by the group listing is
//! submitted again. Where absence is read as a fresh entity, the reader
//! calls [`decode`] or [`kv_record`], and the failure propagates as
//! [`Error::Deserialization`](crate::Error::Deserialization): the run
//! record, the current-step pointer, the terminal record, a member
//! record read on its own and the group manifest, whose absence starts
//! a new run or group. A new reader calls one of these and does not
//! deserialize a record directly.

use std::collections::HashMap;
use std::fmt::Display;
use std::time::Duration;

use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use taquba::QueueView;
use tracing::warn;

use crate::error::Result;

/// Encode one of the crate's own records as MessagePack with named
/// fields. The record types here hold strings, bytes, integers and
/// enumerations, whose encoding cannot fail.
pub(crate) fn encode<T: Serialize>(record: &T) -> Vec<u8> {
    rmp_serde::to_vec_named(record).expect("a durable record encodes")
}

/// Decode one of the crate's own records. A record that does not decode
/// is [`Error::Deserialization`](crate::Error::Deserialization).
pub(crate) fn decode<T: DeserializeOwned>(bytes: &[u8]) -> Result<T> {
    Ok(rmp_serde::from_slice(bytes)?)
}

/// Decode a record whose absence means that the work runs again. A
/// record that fails to decode is logged, as the `record` kind of the
/// entity `id`, and reported as absent.
pub(crate) fn decode_or_absent<T: DeserializeOwned>(
    bytes: &[u8],
    record: &'static str,
    id: &dyn Display,
) -> Option<T> {
    match rmp_serde::from_slice(bytes) {
        Ok(value) => Some(value),
        Err(err) => {
            warn!(record, %id, error = %err, "{record} failed to decode; treated as absent");
            None
        }
    }
}

/// The record at `key` of the queue's KV namespace, `None` when no
/// value is stored there.
pub(crate) async fn kv_record<T: DeserializeOwned>(
    view: &QueueView,
    key: &[u8],
) -> Result<Option<T>> {
    match view.kv_get(key).await? {
        Some(bytes) => decode(&bytes).map(Some),
        None => Ok(None),
    }
}

use crate::effects::StagedEffects;
use crate::keys::RunId;
use crate::runner::{StepErrorKind, StepOutcome, Trigger};
use crate::terminal::{RunOutcome, TerminalStatus};

/// Durable per-run record written atomically with the step-0 enqueue in
/// [`WorkflowRuntime::submit`] via [`Queue::enqueue_with_effects`]. It contains
/// just enough state to detect duplicate submissions across runtime restarts,
/// to reject re-submissions that change the input and to record a cancellation
/// request for the run's next step. Deleted with the settlement that terminates
/// the run, staged in `terminate_collecting_effects`.
///
/// `run_id` keeps the record self-describing for ad hoc operator
/// inspection; `submitted_at_ms` is useful for ordering and stale-record
/// auditing; `input_hash` is the SHA-256 of the original `spec.input` and
/// powers the `Error::InputMismatch` check on duplicate submissions.
/// `cancel_requested` is set by `WorkflowRuntime::cancel` on this key
/// so that the write conflicts with the termination's delete of the
/// record and a request can never outlive the run.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct DurableRunRecord {
    pub(crate) run_id: RunId,
    pub(crate) submitted_at_ms: u64,
    pub(crate) input_hash: [u8; 32],
    pub(crate) cancel_requested: bool,
}

/// Durable pointer from a run to the queue job currently representing
/// it, kept beside the run record: written with the step-0 enqueue,
/// rewritten in the settlement that enqueues each next step and deleted
/// with the termination. It is what a duplicate submission known only
/// from the durable record, or a reader outside the process, resolves a
/// run's live job from.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct DurableCurrentStep {
    pub(crate) step_number: u32,
    pub(crate) job_id: String,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub(crate) struct DurableDuration {
    secs: u64,
    nanos: u32,
}

impl From<Duration> for DurableDuration {
    fn from(duration: Duration) -> Self {
        Self {
            secs: duration.as_secs(),
            nanos: duration.subsec_nanos(),
        }
    }
}

impl From<DurableDuration> for Duration {
    fn from(duration: DurableDuration) -> Self {
        Duration::new(duration.secs, duration.nanos)
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) enum DurableTrigger {
    Immediate,
    After(DurableDuration),
    OnSignal {
        correlation_key: String,
        timeout: DurableDuration,
    },
}

impl From<&Trigger> for DurableTrigger {
    fn from(trigger: &Trigger) -> Self {
        match trigger {
            Trigger::Immediate => Self::Immediate,
            Trigger::After(delay) => Self::After((*delay).into()),
            Trigger::OnSignal {
                correlation_key,
                timeout,
            } => Self::OnSignal {
                correlation_key: correlation_key.clone(),
                timeout: (*timeout).into(),
            },
        }
    }
}

impl From<DurableTrigger> for Trigger {
    fn from(trigger: DurableTrigger) -> Self {
        match trigger {
            DurableTrigger::Immediate => Self::Immediate,
            DurableTrigger::After(delay) => Self::After(delay.into()),
            DurableTrigger::OnSignal {
                correlation_key,
                timeout,
            } => Self::OnSignal {
                correlation_key,
                timeout: timeout.into(),
            },
        }
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) enum DurableStepOutcome {
    Continue {
        #[serde(with = "serde_bytes")]
        payload: Vec<u8>,
        when: DurableTrigger,
    },
    Succeed {
        #[serde(with = "serde_bytes")]
        result: Vec<u8>,
    },
    Fail {
        reason: String,
    },
    Cancel {
        reason: String,
    },
}

impl From<&StepOutcome> for DurableStepOutcome {
    fn from(outcome: &StepOutcome) -> Self {
        match outcome {
            StepOutcome::Continue { payload, when } => Self::Continue {
                payload: payload.clone(),
                when: when.into(),
            },
            StepOutcome::Succeed { result } => Self::Succeed {
                result: result.clone(),
            },
            StepOutcome::Fail { reason } => Self::Fail {
                reason: reason.clone(),
            },
            StepOutcome::Cancel { reason } => Self::Cancel {
                reason: reason.clone(),
            },
        }
    }
}

impl From<DurableStepOutcome> for StepOutcome {
    fn from(outcome: DurableStepOutcome) -> Self {
        match outcome {
            DurableStepOutcome::Continue { payload, when } => Self::Continue {
                payload,
                when: when.into(),
            },
            DurableStepOutcome::Succeed { result } => Self::Succeed { result },
            DurableStepOutcome::Fail { reason } => Self::Fail { reason },
            DurableStepOutcome::Cancel { reason } => Self::Cancel { reason },
        }
    }
}

/// Storage envelope for a step-output replay entry. `stored_at_ms`
/// records when the outcome was persisted so a replayed delayed `Continue`
/// can schedule the next step relative to the original settlement
/// rather than the replay.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct DurableStepOutcomeRecord {
    pub(crate) stored_at_ms: u64,
    pub(crate) outcome: DurableStepOutcome,
    /// Effects staged through [`crate::EffectsHandle`] during the
    /// recorded delivery, restored into the settlement when the outcome
    /// is replayed.
    pub(crate) effects: StagedEffects,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum DurableTerminalStatus {
    Succeeded,
    Failed,
    Cancelled,
}

impl From<TerminalStatus> for DurableTerminalStatus {
    fn from(status: TerminalStatus) -> Self {
        match status {
            TerminalStatus::Succeeded => Self::Succeeded,
            TerminalStatus::Failed => Self::Failed,
            TerminalStatus::Cancelled => Self::Cancelled,
        }
    }
}

impl From<DurableTerminalStatus> for TerminalStatus {
    fn from(status: DurableTerminalStatus) -> Self {
        match status {
            DurableTerminalStatus::Succeeded => Self::Succeeded,
            DurableTerminalStatus::Failed => Self::Failed,
            DurableTerminalStatus::Cancelled => Self::Cancelled,
        }
    }
}

/// The stored form of a [`RunTermination`](crate::RunTermination) with the
/// final step and the SHA-256 of the run's input: the terminal record under
/// `workflow/outcomes/{run_id}`, written in the settlement that terminates the
/// run and read by [`WorkflowView::status`](crate::WorkflowView::status) once
/// the run record is gone and by a typed re-submission after completion, the
/// termination half of a member record and the termination a run result record
/// belongs to.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) struct DurableTermination {
    pub(crate) status: DurableTerminalStatus,
    pub(crate) error: Option<String>,
    pub(crate) error_kind: Option<DurableErrorKind>,
    pub(crate) final_step: u32,
    pub(crate) terminated_at_ms: u64,
    pub(crate) input_hash: [u8; 32],
}

/// The durable member record of a grouped run, written under
/// `workflow/groups/{group_id}/{key}` with the member's submission and
/// rewritten in the settlement that terminates it.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct DurableMember {
    pub(crate) run_id: RunId,
    /// The member's termination; `None` while it is active.
    pub(crate) terminated: Option<DurableTermination>,
}

/// Stored form of a [`RunOutcome`]: the payload of a terminal-notification
/// job and the outcome half of the run result record, self-contained so
/// both survive restarts and redeliveries.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct DurableRunOutcome {
    run_id: RunId,
    status: DurableTerminalStatus,
    #[serde(with = "serde_bytes")]
    result: Option<Vec<u8>>,
    error: Option<String>,
    headers: HashMap<String, String>,
    final_step: u32,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub(crate) enum DurableErrorKind {
    Transient,
    Permanent,
}

impl From<StepErrorKind> for DurableErrorKind {
    fn from(kind: StepErrorKind) -> Self {
        match kind {
            StepErrorKind::Transient => Self::Transient,
            StepErrorKind::Permanent => Self::Permanent,
        }
    }
}

impl From<DurableErrorKind> for StepErrorKind {
    fn from(kind: DurableErrorKind) -> Self {
        match kind {
            DurableErrorKind::Transient => Self::Transient,
            DurableErrorKind::Permanent => Self::Permanent,
        }
    }
}

/// The run result record, stored in the run memo under
/// [`RUN_RESULT_MEMO_KEY`](crate::memo::RUN_RESULT_MEMO_KEY) by the
/// worker before the settlement that terminates the run: the committed
/// outcome and the termination the record belongs to, equal to the
/// terminal record written by the same settlement.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub(crate) struct DurableRunResult {
    pub(crate) termination: DurableTermination,
    pub(crate) outcome: DurableRunOutcome,
}

impl From<&RunOutcome> for DurableRunOutcome {
    fn from(outcome: &RunOutcome) -> Self {
        Self {
            run_id: outcome.run_id.clone(),
            status: outcome.status.into(),
            result: outcome.result.clone(),
            error: outcome.error.clone(),
            headers: outcome.headers.clone(),
            final_step: outcome.final_step,
        }
    }
}

impl From<DurableRunOutcome> for RunOutcome {
    fn from(outcome: DurableRunOutcome) -> Self {
        Self {
            run_id: outcome.run_id,
            status: outcome.status.into(),
            result: outcome.result,
            error: outcome.error,
            headers: outcome.headers,
            final_step: outcome.final_step,
        }
    }
}

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

    #[test]
    fn payload_bytes_are_stored_as_binary_strings() {
        // A binary string stores the bytes as they are, so the encoded
        // record contains the payload as one contiguous window. An
        // integer array prefixes every byte at or above `0x80`.
        let payload: Vec<u8> = (0..=255).collect();
        let is_contiguous = |bytes: &[u8]| bytes.windows(payload.len()).any(|w| w == payload);
        assert!(is_contiguous(&encode(&DurableStepOutcome::Continue {
            payload: payload.clone(),
            when: DurableTrigger::Immediate,
        })));
        assert!(is_contiguous(&encode(&DurableStepOutcome::Succeed {
            result: payload.clone(),
        })));
        let outcome = DurableRunOutcome {
            run_id: rid("run"),
            status: DurableTerminalStatus::Succeeded,
            result: Some(payload.clone()),
            error: None,
            headers: HashMap::new(),
            final_step: 0,
        };
        let stored = encode(&outcome);
        assert!(is_contiguous(&stored));
        let decoded: DurableRunOutcome = decode(&stored).unwrap();
        assert_eq!(decoded.result, Some(payload));
    }
}