Skip to content
Durable Workflowrust

durable_workflow/
activity_retry.rs

1//! Immutable retry metadata for an activity invocation.
2
3use serde_json::Value;
4
5/// The configured attempt budget in an activity's retry snapshot.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7#[non_exhaustive]
8pub enum ActivityAttemptLimit {
9    /// The snapshot or a valid limit is unavailable. This does not mean unlimited.
10    Unknown,
11    /// The snapshot explicitly contains `"max_attempts": null`.
12    Unlimited,
13    /// The snapshot contains a positive maximum count, including the first attempt.
14    Bounded(u64),
15}
16
17/// Original attempt identity and retry policy, captured before invoking a handler.
18///
19/// Service workers retain the Server claim's JSON without filling defaults or
20/// discarding unknown fields. Direct activity tests and local activities have no
21/// received Server snapshot and report an unknown policy. Fresh workflow tests
22/// expose the same modeled snapshot they record in their generated history.
23#[derive(Clone, Debug, PartialEq)]
24pub struct ActivityRetryInfo {
25    attempt_number: u64,
26    policy: Option<Value>,
27}
28
29impl ActivityRetryInfo {
30    pub(crate) fn new(attempt_number: u64, policy: Option<Value>) -> Self {
31        Self {
32            attempt_number,
33            policy,
34        }
35    }
36
37    /// The original attempt number. Mutating the context's public compatibility
38    /// field does not change this captured identity or its budget checks.
39    pub fn attempt_number(&self) -> u64 {
40        self.attempt_number
41    }
42
43    /// The original policy JSON. `None` means omitted, while `Some(Value::Null)`
44    /// preserves an explicitly null legacy policy. Neither implies unlimited.
45    pub fn policy(&self) -> Option<&Value> {
46        self.policy.as_ref()
47    }
48
49    /// Interpret only a positive integer or explicit null `max_attempts` field.
50    /// Missing, malformed and legacy values remain unknown and are preserved.
51    pub fn attempt_limit(&self) -> ActivityAttemptLimit {
52        match self
53            .policy
54            .as_ref()
55            .and_then(|policy| policy.get("max_attempts"))
56        {
57            Some(Value::Null) => ActivityAttemptLimit::Unlimited,
58            Some(value) => match value.as_u64().filter(|limit| *limit > 0) {
59                Some(limit) => ActivityAttemptLimit::Bounded(limit),
60                None => ActivityAttemptLimit::Unknown,
61            },
62            None => ActivityAttemptLimit::Unknown,
63        }
64    }
65
66    /// Whether this is the final configured attempt (or already exceeds its budget).
67    ///
68    /// Returns `None` for an unknown policy or invalid zero attempt number, and
69    /// `Some(false)` for explicit unlimited retries. A remaining budget does not
70    /// promise another execution: non-retryable failures, cancellation, workflow
71    /// closure and elapsed deadlines may end execution earlier.
72    pub fn is_last_configured_attempt(&self) -> Option<bool> {
73        if self.attempt_number == 0 {
74            return None;
75        }
76        match self.attempt_limit() {
77            ActivityAttemptLimit::Unknown => None,
78            ActivityAttemptLimit::Unlimited => Some(false),
79            ActivityAttemptLimit::Bounded(limit) => Some(self.attempt_number >= limit),
80        }
81    }
82}
83
84pub(super) fn deserialize_policy<'de, D>(deserializer: D) -> Result<Option<Value>, D::Error>
85where
86    D: serde::Deserializer<'de>,
87{
88    // Option<Value>'s usual deserializer collapses explicit null into absence.
89    <Value as serde::Deserialize>::deserialize(deserializer).map(Some)
90}