sayiir-core 1.0.0

Core types and traits for the Sayiir durable workflow engine
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
//! Fixed-length 32-byte identifiers backed by SHA-256.
//!
//! [`Hash32`] is a primitive value type — a `[u8; 32]` with cheap `Copy`,
//! constant-time-equivalent comparison (single SIMD-friendly memcmp), and a
//! single hash-map probe instead of the per-character hashing a `String`
//! incurs. It is the building block for the semantic id newtypes
//! [`DefinitionHash`], [`WorkflowId`], and [`TaskId`], each of which is a
//! distinct `Hash32` newtype so the type system prevents mixing identifier
//! kinds at call sites.
//!
//! Serde encodes a `Hash32` as a 64-character lowercase hex string on
//! human-readable formats (JSON, TOML) and as raw 32 bytes on binary formats
//! (bincode, rkyv-derived codecs). This keeps user-facing snapshot blobs and
//! API payloads readable while making over-the-wire transports compact.

use core::fmt;
use core::str::FromStr;

use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha2::{Digest, Sha256};

/// A 32-byte fixed-length identifier, typically the output of SHA-256.
///
/// Stored inline as `[u8; 32]` — no heap allocation, no length prefix, `Copy`,
/// and trivially `Hash`/`Eq` (one memcmp). `Default` returns [`Hash32::ZERO`]
/// (all-zero bytes) so semantic newtypes wrapping it can be `Default` too.
#[derive(Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Hash32([u8; 32]);

impl Hash32 {
    /// Zero hash — useful as a sentinel for "uninitialised". Not a valid
    /// SHA-256 output in practice.
    pub const ZERO: Self = Self([0u8; 32]);

    /// Construct from raw bytes.
    #[must_use]
    pub const fn from_bytes(bytes: [u8; 32]) -> Self {
        Self(bytes)
    }

    /// Construct from a byte slice, validating the length.
    ///
    /// Use this on the wire/storage boundary — e.g. decoding a `BYTEA`
    /// column from sqlx — where you have a `&[u8]` rather than a
    /// fixed-size array.
    ///
    /// # Errors
    ///
    /// Returns [`Hash32ParseError::WrongLength`] if `bytes.len() != 32`.
    pub fn from_slice(bytes: &[u8]) -> Result<Self, Hash32ParseError> {
        let arr: [u8; 32] = bytes
            .try_into()
            .map_err(|_| Hash32ParseError::WrongLength(bytes.len()))?;
        Ok(Self(arr))
    }

    /// Borrow the underlying bytes.
    #[must_use]
    pub const fn as_bytes(&self) -> &[u8; 32] {
        &self.0
    }

    /// Consume into the underlying bytes.
    #[must_use]
    pub const fn into_bytes(self) -> [u8; 32] {
        self.0
    }

    /// Compute SHA-256 of the given input.
    #[must_use]
    pub fn sha256(input: impl AsRef<[u8]>) -> Self {
        let mut hasher = Sha256::new();
        hasher.update(input.as_ref());
        Self::from_digest(hasher)
    }

    /// Finalise a hasher into a `Hash32`.
    #[must_use]
    pub fn from_digest(hasher: Sha256) -> Self {
        let out = hasher.finalize();
        let mut bytes = [0u8; 32];
        bytes.copy_from_slice(&out);
        Self(bytes)
    }

    /// Lowercase hex encoding (64 chars). Allocates.
    #[must_use]
    pub fn to_hex(&self) -> String {
        use fmt::Write as _;
        let mut s = String::with_capacity(64);
        for byte in &self.0 {
            let _ = write!(&mut s, "{byte:02x}");
        }
        s
    }

    /// Parse from a 64-character lowercase or uppercase hex string.
    ///
    /// # Errors
    ///
    /// Returns [`Hash32ParseError`] if the input is not 64 hex digits.
    pub fn from_hex(s: &str) -> Result<Self, Hash32ParseError> {
        let bytes_in = s.as_bytes();
        if bytes_in.len() != 64 {
            return Err(Hash32ParseError::WrongLength(bytes_in.len()));
        }
        let mut bytes = [0u8; 32];
        for (i, byte) in bytes.iter_mut().enumerate() {
            let lo = bytes_in.get(i * 2).copied().unwrap_or(0);
            let hi = bytes_in.get(i * 2 + 1).copied().unwrap_or(0);
            *byte = (decode_nibble(lo)? << 4) | decode_nibble(hi)?;
        }
        Ok(Self(bytes))
    }
}

#[inline]
fn decode_nibble(c: u8) -> Result<u8, Hash32ParseError> {
    match c {
        b'0'..=b'9' => Ok(c - b'0'),
        b'a'..=b'f' => Ok(c - b'a' + 10),
        b'A'..=b'F' => Ok(c - b'A' + 10),
        _ => Err(Hash32ParseError::InvalidChar(c)),
    }
}

/// Errors from parsing a hex-encoded [`Hash32`].
#[derive(Debug, thiserror::Error)]
pub enum Hash32ParseError {
    /// Input length was not exactly 64 hex characters.
    #[error("expected 64 hex characters, got {0}")]
    WrongLength(usize),
    /// Non-hex byte encountered.
    #[error("invalid hex character: {:?}", *.0 as char)]
    InvalidChar(u8),
}

impl fmt::Display for Hash32 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for byte in &self.0 {
            write!(f, "{byte:02x}")?;
        }
        Ok(())
    }
}

impl fmt::Debug for Hash32 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl FromStr for Hash32 {
    type Err = Hash32ParseError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_hex(s)
    }
}

impl AsRef<[u8]> for Hash32 {
    fn as_ref(&self) -> &[u8] {
        &self.0
    }
}

impl Serialize for Hash32 {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        if serializer.is_human_readable() {
            serializer.collect_str(self)
        } else {
            serializer.serialize_bytes(&self.0)
        }
    }
}

impl<'de> Deserialize<'de> for Hash32 {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        if deserializer.is_human_readable() {
            let s = <&str>::deserialize(deserializer)?;
            Self::from_hex(s).map_err(serde::de::Error::custom)
        } else {
            struct V;
            impl<'de> serde::de::Visitor<'de> for V {
                type Value = Hash32;
                fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                    f.write_str("32 raw bytes")
                }
                fn visit_bytes<E: serde::de::Error>(self, v: &[u8]) -> Result<Hash32, E> {
                    if v.len() != 32 {
                        return Err(E::invalid_length(v.len(), &self));
                    }
                    let mut bytes = [0u8; 32];
                    bytes.copy_from_slice(v);
                    Ok(Hash32(bytes))
                }
                fn visit_borrowed_bytes<E: serde::de::Error>(
                    self,
                    v: &'de [u8],
                ) -> Result<Hash32, E> {
                    self.visit_bytes(v)
                }
                fn visit_byte_buf<E: serde::de::Error>(self, v: Vec<u8>) -> Result<Hash32, E> {
                    self.visit_bytes(&v)
                }
                fn visit_seq<A: serde::de::SeqAccess<'de>>(
                    self,
                    mut seq: A,
                ) -> Result<Hash32, A::Error> {
                    let mut bytes = [0u8; 32];
                    for (i, byte) in bytes.iter_mut().enumerate() {
                        *byte = seq
                            .next_element()?
                            .ok_or_else(|| serde::de::Error::invalid_length(i, &"32 bytes"))?;
                    }
                    Ok(Hash32(bytes))
                }
            }
            deserializer.deserialize_bytes(V)
        }
    }
}

// ============================================================================
// Semantic newtypes — `DefinitionHash` and `TaskId`.
//
// Both are `Hash32`-backed but distinct at the type level so the compiler
// catches mixups. The macro keeps their surface in lock-step.
// ============================================================================

/// Define a `Hash32`-backed semantic newtype with the standard surface:
/// `Copy`/`Eq`/`Hash`, hex `Display`, hex `FromStr`, transparent serde,
/// and `From<&str>`/`From<String>` that **hash** the input via SHA-256
/// (not hex-parse). The hex round-trip path is `from_hex` / `to_hex`.
macro_rules! hash32_newtype {
    (
        $(#[$attr:meta])*
        $name:ident
    ) => {
        $(#[$attr])*
        #[derive(
            Clone, Copy, Default, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize,
        )]
        #[serde(transparent)]
        pub struct $name(Hash32);

        impl $name {
            #[doc = concat!("Construct a [`", stringify!($name), "`] from a [`Hash32`].")]
            #[must_use]
            pub const fn from_hash(hash: Hash32) -> Self { Self(hash) }

            #[doc = concat!("Construct a [`", stringify!($name), "`] from raw 32 bytes.")]
            #[must_use]
            pub const fn from_bytes(bytes: [u8; 32]) -> Self {
                Self(Hash32::from_bytes(bytes))
            }

            #[doc = concat!(
                "Construct a [`", stringify!($name),
                "`] from a length-checked byte slice. See [`Hash32::from_slice`]."
            )]
            ///
            /// # Errors
            ///
            /// Returns [`Hash32ParseError::WrongLength`] if `bytes.len() != 32`.
            pub fn from_slice(bytes: &[u8]) -> Result<Self, Hash32ParseError> {
                Hash32::from_slice(bytes).map(Self)
            }

            /// SHA-256-hash the given input and wrap the result.
            ///
            /// This is the canonical way to mint a fresh id from a
            /// human-readable name at construction time.
            #[must_use]
            pub fn sha256(input: impl AsRef<[u8]>) -> Self {
                Self(Hash32::sha256(input))
            }

            /// Borrow the underlying [`Hash32`].
            #[must_use]
            pub const fn as_hash(&self) -> &Hash32 { &self.0 }

            /// Borrow the raw 32 bytes.
            #[must_use]
            pub const fn as_bytes(&self) -> &[u8; 32] { self.0.as_bytes() }

            /// Lowercase hex encoding (64 chars).
            #[must_use]
            pub fn to_hex(&self) -> String { self.0.to_hex() }

            /// Parse from a 64-character hex string.
            ///
            /// # Errors
            ///
            /// See [`Hash32::from_hex`].
            pub fn from_hex(s: &str) -> Result<Self, Hash32ParseError> {
                Hash32::from_hex(s).map(Self)
            }
        }

        impl fmt::Display for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                fmt::Display::fmt(&self.0, f)
            }
        }

        impl fmt::Debug for $name {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                write!(f, concat!(stringify!($name), "({})"), self.0)
            }
        }

        impl FromStr for $name {
            type Err = Hash32ParseError;
            fn from_str(s: &str) -> Result<Self, Self::Err> { Self::from_hex(s) }
        }

        impl From<Hash32> for $name {
            fn from(h: Hash32) -> Self { Self(h) }
        }

        impl From<$name> for Hash32 {
            fn from(h: $name) -> Self { h.0 }
        }

        // NOTE: `From<&str>` / `From<String>` SHA-256-hash the input. They do
        // **not** parse a hex hash — use `from_hex` for that. These impls keep
        // builder APIs and test fixtures (`"task-1".into()`) ergonomic.
        impl From<&str> for $name {
            fn from(s: &str) -> Self { Self::sha256(s.as_bytes()) }
        }

        impl From<&String> for $name {
            fn from(s: &String) -> Self { Self::sha256(s.as_bytes()) }
        }

        impl From<String> for $name {
            fn from(s: String) -> Self { Self::sha256(s.as_bytes()) }
        }
    };
}

hash32_newtype! {
    /// SHA-256 fingerprint of a workflow's structural definition.
    ///
    /// Computed from the workflow's continuation tree (task IDs, retry
    /// policies, fork shapes, delays, signals, loops, child workflows). Used
    /// by the runtime to detect when a serialised snapshot was written
    /// against a different workflow definition than the one currently in
    /// memory.
    ///
    /// Compares in a single 32-byte memcmp instead of a 64-character string
    /// equality, and hashes to one `u64` instead of per-character siphash.
    DefinitionHash
}

hash32_newtype! {
    /// SHA-256 hash of a task's user-facing name (e.g. `"validate"`).
    ///
    /// Stored on every runtime data structure that previously held a task
    /// id as a `String` — [`ExecutionPosition`](crate::snapshot::ExecutionPosition)
    /// variants, [`TaskResult`](crate::snapshot::TaskResult), the
    /// `completed_tasks`/`task_retries`/`loop_iterations` `HashMap` keys on
    /// [`WorkflowSnapshot`](crate::snapshot::WorkflowSnapshot),
    /// [`AvailableTask`](crate::task_claim::AvailableTask), and so on. The
    /// human-readable name only lives once per workflow definition on the
    /// continuation tree.
    ///
    /// 32-byte memcmp + single-`u64` hash, same wins as [`DefinitionHash`].
    TaskId
}

hash32_newtype! {
    /// SHA-256 hash of a workflow's user-facing identifier (e.g.
    /// `"order-pipeline"`).
    ///
    /// Used in runtime contexts and dispatch maps where the workflow
    /// identifier needs cheap comparison without keeping the string alive.
    /// The human-readable name remains on the
    /// [`Workflow`](crate::workflow::Workflow) /
    /// [`WorkflowContext`](crate::context::WorkflowContext) for log/error
    /// display.
    WorkflowId
}

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

    #[test]
    fn hash32_round_trips_via_hex() {
        let h = Hash32::sha256(b"hello world");
        let hex = h.to_hex();
        assert_eq!(hex.len(), 64);
        assert_eq!(Hash32::from_hex(&hex).unwrap(), h);
    }

    #[test]
    fn from_hex_rejects_wrong_length() {
        assert!(matches!(
            Hash32::from_hex("abc"),
            Err(Hash32ParseError::WrongLength(3))
        ));
    }

    #[test]
    fn from_hex_rejects_non_hex_char() {
        let bad = "z".repeat(64);
        assert!(matches!(
            Hash32::from_hex(&bad),
            Err(Hash32ParseError::InvalidChar(b'z'))
        ));
    }

    #[test]
    fn from_hex_accepts_uppercase() {
        let lower = "abcd".repeat(16);
        let upper = "ABCD".repeat(16);
        assert_eq!(
            Hash32::from_hex(&lower).unwrap(),
            Hash32::from_hex(&upper).unwrap()
        );
    }

    #[test]
    fn json_round_trip_is_hex_string() {
        let h = Hash32::sha256(b"payload");
        let json = serde_json::to_string(&h).unwrap();
        assert_eq!(json.len(), 66);
        assert!(json.starts_with('"') && json.ends_with('"'));
        let parsed: Hash32 = serde_json::from_str(&json).unwrap();
        assert_eq!(parsed, h);
    }

    #[test]
    fn definition_hash_displays_as_hex() {
        let d = DefinitionHash::from_bytes([0xab; 32]);
        assert_eq!(format!("{d}"), "ab".repeat(32));
    }

    #[test]
    fn definition_hash_serde_transparent() {
        let d = DefinitionHash::from_hash(Hash32::sha256(b"wf"));
        let as_hash_json = serde_json::to_string(d.as_hash()).unwrap();
        let as_def_json = serde_json::to_string(&d).unwrap();
        assert_eq!(as_hash_json, as_def_json);
    }

    #[test]
    fn definition_hash_round_trips_via_hex() {
        let d = DefinitionHash::from_hash(Hash32::sha256(b"abc"));
        let parsed: DefinitionHash = d.to_hex().parse().unwrap();
        assert_eq!(parsed, d);
    }

    #[test]
    fn definition_hash_from_str_hashes_input() {
        let by_str: DefinitionHash = "wf-1".into();
        let by_sha = DefinitionHash::sha256(b"wf-1");
        assert_eq!(by_str, by_sha);
    }
}