durable-workflow 4.10.1

Rust client and worker SDK for Durable Workflow Cloud and self-hosted Server
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
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
use super::*;

const MAX_ATTEMPTS: u64 = 100;
const MAX_HEARTBEATS: usize = 1000;
const LEASE_RENEWAL_INTERVAL: Duration = Duration::from_secs(1);

/// Retry and timeout settings for an activity executed by the workflow worker.
///
/// Local activities bypass the activity queue. They do not accept routing,
/// schedule-to-start timeouts, or remote activity cancellation policies.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct LocalActivityOptions {
    pub retry_policy: Option<ActivityRetryPolicy>,
    pub start_to_close_timeout: Option<Duration>,
    pub schedule_to_close_timeout: Option<Duration>,
    pub heartbeat_timeout: Option<Duration>,
}

impl LocalActivityOptions {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn retry_policy(mut self, policy: ActivityRetryPolicy) -> Self {
        self.retry_policy = Some(policy);
        self
    }
    pub fn start_to_close_timeout(mut self, timeout: Duration) -> Self {
        self.start_to_close_timeout = Some(timeout);
        self
    }
    pub fn schedule_to_close_timeout(mut self, timeout: Duration) -> Self {
        self.schedule_to_close_timeout = Some(timeout);
        self
    }
    pub fn heartbeat_timeout(mut self, timeout: Duration) -> Self {
        self.heartbeat_timeout = Some(timeout);
        self
    }
    fn into_activity_options(self) -> ActivityOptions {
        ActivityOptions {
            retry_policy: self.retry_policy,
            start_to_close_timeout: self.start_to_close_timeout,
            schedule_to_close_timeout: self.schedule_to_close_timeout,
            heartbeat_timeout: self.heartbeat_timeout,
            ..ActivityOptions::default()
        }
    }
}

pub(super) fn validate(options: &ValidatedActivityOptions) -> Result<()> {
    let max_attempts = options
        .retry_policy
        .as_ref()
        .and_then(|policy| policy["max_attempts"].as_u64())
        .unwrap_or(1);
    if max_attempts > MAX_ATTEMPTS {
        return Err(Error::InvalidActivityOptions(ActivityOptionsError::new(
            ActivityOptionsErrorKind::InvalidMaxAttempts,
            Some("max_attempts"),
            "local activities support at most 100 attempts",
        )));
    }
    for seconds in [
        options.start_to_close_timeout,
        options.schedule_to_close_timeout,
        options.heartbeat_timeout,
    ]
    .into_iter()
    .flatten()
    .chain(options.retry_policy.iter().flat_map(|p| {
        p["backoff_seconds"]
            .as_array()
            .into_iter()
            .flatten()
            .filter_map(Value::as_u64)
    })) {
        if Instant::now()
            .checked_add(Duration::from_secs(seconds))
            .is_none()
        {
            return Err(Error::InvalidActivityOptions(ActivityOptionsError::new(
                ActivityOptionsErrorKind::TimeoutOverflow,
                Some("local_activity"),
                "local timing exceeds the monotonic clock range",
            )));
        }
    }
    Ok(())
}

impl WorkflowContext {
    /// Execute a registered activity in this workflow worker and record its outcome.
    ///
    /// Committed results are replayed without executing the callback. A worker
    /// lost before acknowledgment can execute the callback again, so side effects
    /// must be idempotent. Callbacks must yield to Tokio and avoid blocking work.
    pub fn local_activity<T: Serialize>(
        &self,
        activity_type: impl Into<String>,
        args: T,
    ) -> ActivityCall {
        self.local_activity_with_options(activity_type, LocalActivityOptions::new(), args)
    }

    pub fn local_activity_with_options<T: Serialize>(
        &self,
        activity_type: impl Into<String>,
        options: LocalActivityOptions,
        args: T,
    ) -> ActivityCall {
        let mut call =
            self.activity_with_options(activity_type, options.into_activity_options(), args);
        call.local = true;
        call
    }

    /// Lossless Avro result, including bytes and large integers.
    pub async fn local_activity_avro_value<T: Serialize>(
        &self,
        activity_type: impl Into<String>,
        args: T,
    ) -> Result<AvroValue> {
        self.local_activity_avro_value_with_options(
            activity_type,
            LocalActivityOptions::new(),
            args,
        )
        .await
    }

    pub async fn local_activity_avro_value_with_options<T: Serialize>(
        &self,
        activity_type: impl Into<String>,
        options: LocalActivityOptions,
        args: T,
    ) -> Result<AvroValue> {
        let mut call = self.local_activity_with_options(activity_type, options, args);
        std::future::poll_fn(|cx| Pin::new(&mut call).poll_avro_value(cx)).await
    }

    pub async fn local_activity_typed<I: Serialize, O: DeserializeOwned>(
        &self,
        activity_type: impl Into<String>,
        args: I,
    ) -> Result<O> {
        self.local_activity_typed_with_options(activity_type, LocalActivityOptions::new(), args)
            .await
    }

    pub async fn local_activity_typed_with_options<I: Serialize, O: DeserializeOwned>(
        &self,
        activity_type: impl Into<String>,
        options: LocalActivityOptions,
        args: I,
    ) -> Result<O> {
        let activity_type = activity_type.into();
        let result = self
            .local_activity_avro_value_with_options(activity_type.clone(), options, args)
            .await?;
        decode_handler_result(result, HandlerKind::Activity, &activity_type)
    }
}

#[derive(Debug)]
pub(super) struct Request {
    pub command_index: usize,
    pub options: ValidatedActivityOptions,
    pub arguments: AvroValue,
    pub result: Arc<Mutex<Option<ActivityOutcome>>>,
}

#[derive(Debug)]
pub(super) struct Heartbeats {
    active: bool,
    started: Instant,
    last: Instant,
    reports: Vec<Value>,
    capacity: usize,
    overflowed: bool,
}

impl Heartbeats {
    pub(super) fn captured_attempt() -> (Arc<Mutex<Self>>, impl Drop) {
        let now = Instant::now();
        let reports = Arc::new(Mutex::new(Self {
            active: true,
            started: now,
            last: now,
            reports: Vec::new(),
            capacity: MAX_HEARTBEATS,
            overflowed: false,
        }));
        let close = CloseAttempt(reports.clone());
        (reports, close)
    }

    pub fn record<T: Serialize>(&mut self, details: T) -> Result<ActivityHeartbeatResponse> {
        if !self.active {
            return Ok(heartbeat_response(false));
        }
        if self.reports.len() >= self.capacity {
            self.overflowed = true;
            return Err(Error::WorkerLoop(
                "local_activity_heartbeat_limit_exceeded: at most 1000 reports per local command"
                    .into(),
            ));
        }
        let details = encode_typed_envelope(&AvroValue::from_serialize(&details)?, DEFAULT_CODEC)?;
        let now = Instant::now();
        self.reports.push(
            json!({"elapsed_ms": millis(now.duration_since(self.started)), "details": details}),
        );
        self.last = now;
        Ok(heartbeat_response(true))
    }
}

fn heartbeat_response(active: bool) -> ActivityHeartbeatResponse {
    ActivityHeartbeatResponse {
        cancel_requested: false,
        heartbeat_recorded: active,
        can_continue: Some(active),
        reason: (!active).then(|| "local_activity_attempt_closed".into()),
        run_closed_reason: None,
        run_closed_at: None,
        lease_expires_at: None,
        last_heartbeat_at: None,
    }
}

struct CloseAttempt(Arc<Mutex<Heartbeats>>);
impl Drop for CloseAttempt {
    fn drop(&mut self) {
        if let Ok(mut heartbeats) = self.0.lock() {
            heartbeats.active = false;
        }
    }
}

fn millis(duration: Duration) -> u64 {
    u64::try_from(duration.as_millis()).unwrap_or(u64::MAX)
}

impl Worker {
    pub(super) async fn execute_workflow_with_local_activities(
        &self,
        task: WorkflowTask,
    ) -> Result<Option<WorkflowTaskDecision>> {
        #[cfg(feature = "telemetry")]
        if let Some(telemetry) = &self.telemetry {
            return telemetry
                .run_observed(
                    "durable.workflow.task",
                    self.workflow_telemetry_fields(&task),
                    false,
                    self.workflow_observations(&task),
                    async {
                        let mut decision = self
                            .execute_workflow_with_local_activities_inner(task)
                            .await?;
                        if let Some(decision) = &mut decision {
                            self.stamp_observability_commands(&mut decision.commands);
                        }
                        Ok(decision)
                    },
                )
                .await;
        }
        self.execute_workflow_with_local_activities_inner(task)
            .await
    }

    async fn execute_workflow_with_local_activities_inner(
        &self,
        task: WorkflowTask,
    ) -> Result<Option<WorkflowTaskDecision>> {
        let (task, context, mut future) = match self.prepare_workflow_task_execution(task, None)? {
            PreparedWorkflowTask::Decision(decision) => return Ok(Some(decision)),
            PreparedWorkflowTask::Execution {
                task,
                context,
                future,
            } => (task, context, future),
        };
        loop {
            let outcome = Self::poll_workflow_future(&context, &mut future)?;
            let requests = {
                let mut state = context
                    .state
                    .lock()
                    .map_err(|_| Error::WorkflowStatePoisoned)?;
                std::mem::take(&mut state.local_activity_requests)
            };
            if requests.is_empty() {
                return self
                    .finish_workflow_task_execution(&task, &context, outcome)
                    .map(Some);
            }
            if !self.client.local_activities_enabled {
                return Err(Error::WorkerLoop("local_activities_not_enabled: opt in with Worker::local_activities(true) before executing inline local work".into()));
            }
            // A custom future cannot publish a terminal decision while ignoring
            // an unresolved local call. Resolve only genuinely suspended work.
            if !outcome.is_pending() {
                return Err(Error::WorkflowYieldedWithoutCommand);
            }
            for request in requests {
                let Some((wire, result)) = self
                    .execute_local_activity(&task, &context, &request)
                    .await?
                else {
                    // Do not fail, complete, or continue the workflow after a
                    // refused or uncertain lease. Reclaim owns subsequent work.
                    return Ok(None);
                };
                let mut state = context
                    .state
                    .lock()
                    .map_err(|_| Error::WorkflowStatePoisoned)?;
                state.commands[request.command_index] = wire;
                *request
                    .result
                    .lock()
                    .map_err(|_| Error::WorkflowStatePoisoned)? = Some(result);
            }
        }
    }

    async fn local_claim_is_active(&self, task: &WorkflowTask) -> bool {
        tokio::select! {
            biased;
            _ = self.local_worker_stopped() => false,
            receipt = self.client.heartbeat_workflow_task_with_protocol(task, None, MINIMUM_WORKER_PROTOCOL_VERSION) => {
                receipt.is_ok_and(|receipt| receipt.cancellation_request.is_none())
            }
        }
    }

    async fn local_worker_stopped(&self) {
        match &self.client.worker_storage_admission {
            Some(admission) => wait_for_worker_stop(&admission.stop).await,
            None => std::future::pending::<()>().await,
        }
    }

    async fn externalize_local_command(
        &self,
        task: &WorkflowTask,
        context: &WorkflowContext,
        index: usize,
        wire: &mut Value,
        messages: &[(usize, crate::failure_reporting::LocalText)],
    ) -> Result<()> {
        let mut commands = context
            .state
            .lock()
            .map_err(|_| Error::WorkflowStatePoisoned)?
            .commands
            .clone();
        commands[index] = wire.clone();
        let mut body = json!({"lease_owner": task.lease_owner, "workflow_task_attempt": task.workflow_task_attempt,
            "commands": commands});
        let path = format!("/worker/workflow-tasks/{}/complete", task.task_id);
        let limit = self
            .client
            .known_failure_request_limit(RequestProtocol::DefaultWorker)?;
        crate::failure_reporting::fit_local(&mut body, index, messages, limit)?;
        self.client
            .externalize_runtime_payloads(&mut body, &path, RequestProtocol::DefaultWorker)
            .await?;
        let limit = self
            .client
            .known_failure_request_limit(RequestProtocol::DefaultWorker)?;
        crate::failure_reporting::fit_local(&mut body, index, messages, limit)?;
        *wire = body["commands"][index].take();
        Ok(())
    }

    async fn admit_local_command(
        &self,
        task: &WorkflowTask,
        context: &WorkflowContext,
        index: usize,
        wire: &mut Value,
        messages: &[(usize, crate::failure_reporting::LocalText)],
    ) -> Result<bool> {
        match self
            .externalize_local_command(task, context, index, wire, messages)
            .await
        {
            Ok(()) => Ok(true),
            Err(error @ Error::FailureReporting(_)) => Err(error),
            Err(_) => Ok(false),
        }
    }

    async fn execute_local_activity(
        &self,
        task: &WorkflowTask,
        context: &WorkflowContext,
        request: &Request,
    ) -> Result<Option<(Value, ActivityOutcome)>> {
        let mut wire = context
            .state
            .lock()
            .map_err(|_| Error::WorkflowStatePoisoned)?
            .commands[request.command_index]
            .clone();
        // Serialize and admit input before the callback can produce side effects.
        if !self
            .admit_local_command(task, context, request.command_index, &mut wire, &[])
            .await?
        {
            return Ok(None);
        }
        let activity_type = wire["activity_type"]
            .as_str()
            .unwrap_or_default()
            .to_owned();
        let policy = request.options.retry_policy.as_ref();
        let max_attempts = policy
            .and_then(|policy| policy["max_attempts"].as_u64())
            .unwrap_or(1);
        let started = Instant::now();
        let total_deadline = request
            .options
            .schedule_to_close_timeout
            .map(|seconds| started + Duration::from_secs(seconds));
        let mut attempts = Vec::new();
        let mut messages = Vec::new();
        let mut heartbeat_count = 0;
        for number in 1..=max_attempts {
            if !self.local_claim_is_active(task).await {
                return Ok(None);
            }
            let attempt_started = Instant::now();
            let reports = Arc::new(Mutex::new(Heartbeats {
                active: true,
                started: attempt_started,
                last: attempt_started,
                reports: Vec::new(),
                capacity: MAX_HEARTBEATS - heartbeat_count,
                overflowed: false,
            }));
            let close = CloseAttempt(reports.clone());
            let attempt_id = format!(
                "{:x}",
                Sha256::digest(
                    format!(
                        "{}\0{}\0{}\0{}",
                        task.task_id, task.workflow_task_attempt, request.command_index, number
                    )
                    .as_bytes()
                )
            );
            let ctx = ActivityContext {
                client: self.client.clone(),
                task_id: task.task_id.clone(),
                activity_attempt_id: attempt_id.clone(),
                lease_owner: task.lease_owner.clone().unwrap_or_default(),
                activity_type: activity_type.clone(),
                attempt_number: number,
                retry_info: crate::ActivityRetryInfo::new(number, None),
                activity_execution_id: None,
                workflow_id: task.workflow_id.clone(),
                run_id: task.run_id.clone(),
                task_queue: self.task_queue.clone(),
                worker_id: self.worker_id.clone(),
                claim_guard: None,
                local_heartbeats: Some(reports.clone()),
                worker_session: None,
            };
            let callback = self
                .activities
                .get(&activity_type)
                .filter(|_| !total_deadline.is_some_and(|deadline| Instant::now() >= deadline));
            let mut future: ActivityFuture = match callback {
                Some(callback) => {
                    self.activity_callback(callback, ctx, request.arguments.clone(), true)
                }
                None if total_deadline.is_some_and(|deadline| Instant::now() >= deadline) => {
                    Box::pin(std::future::pending())
                }
                None => {
                    let unknown = activity_type.clone();
                    Box::pin(async move { Err(Error::ActivityNotRegistered(unknown)) })
                }
            };
            let attempt_deadline = request
                .options
                .start_to_close_timeout
                .map(|seconds| attempt_started + Duration::from_secs(seconds));
            let outcome = loop {
                let heartbeat_deadline = request
                    .options
                    .heartbeat_timeout
                    .map(|seconds| {
                        reports
                            .lock()
                            .map(|reports| reports.last + Duration::from_secs(seconds))
                    })
                    .transpose()
                    .map_err(|_| Error::WorkflowStatePoisoned)?;
                let next_timeout = [
                    (total_deadline, "schedule_to_close"),
                    (attempt_deadline, "start_to_close"),
                    (heartbeat_deadline, "heartbeat"),
                ]
                .into_iter()
                .filter_map(|(deadline, kind)| deadline.map(|d| (d, kind)))
                .min_by_key(|(deadline, _)| *deadline);
                let timeout_kind = next_timeout
                    .map(|(_, kind)| kind)
                    .unwrap_or("start_to_close");
                let timeout = async {
                    match next_timeout {
                        Some((deadline, _)) => tokio::time::sleep_until(deadline.into()).await,
                        None => std::future::pending::<()>().await,
                    }
                };
                tokio::pin!(timeout);
                tokio::select! {
                    biased;
                    _ = self.local_worker_stopped() => return Ok(None),
                    _ = &mut timeout => {
                        if timeout_kind == "heartbeat" && heartbeat_extended(&reports, request.options.heartbeat_timeout)? { continue; }
                        break Err((format!("local activity exceeded {timeout_kind} timeout"), "LocalActivityTimeout".to_owned(), false, Some(timeout_kind)));
                    },
                    result = &mut future => break result.map_err(local_error),
                    _ = tokio::time::sleep(LEASE_RENEWAL_INTERVAL) => {
                        tokio::select! {
                            biased;
                            _ = &mut timeout => {
                                if timeout_kind == "heartbeat" && heartbeat_extended(&reports, request.options.heartbeat_timeout)? { continue; }
                                break Err((format!("local activity exceeded {timeout_kind} timeout"), "LocalActivityTimeout".to_owned(), false, Some(timeout_kind)));
                            },
                            active = self.local_claim_is_active(task) => { if !active { return Ok(None); } }
                        }
                    }
                }
            };
            drop(future);
            drop(close);
            let (heartbeats, overflowed) = {
                let mut reports = reports.lock().map_err(|_| Error::WorkflowStatePoisoned)?;
                (std::mem::take(&mut reports.reports), reports.overflowed)
            };
            heartbeat_count += heartbeats.len();
            let outcome = if overflowed {
                Err((
                    "local activity exceeded 1000 heartbeat reports".into(),
                    "LocalActivityHeartbeatLimit".into(),
                    true,
                    None,
                ))
            } else {
                outcome
            };
            let mut report = json!({"attempt_number": number, "attempt_id": attempt_id,
                "duration_ms": millis(attempt_started.elapsed()), "heartbeats": heartbeats});
            let (result, failure) = match outcome {
                Ok(result) => match encode_typed_envelope(&result, &task.payload_codec) {
                    Ok(envelope) => {
                        wire["result"] = envelope;
                        (Some(result), None)
                    }
                    Err(error) => (
                        None,
                        Some((
                            error.to_string(),
                            "LocalActivityResultCodecError".into(),
                            true,
                            None,
                        )),
                    ),
                },
                Err(failure) => (None, Some(failure)),
            };
            if let Some((message, exception_type, mut non_retryable, timeout_kind)) = failure {
                non_retryable |= policy
                    .and_then(|policy| policy["non_retryable_error_types"].as_array())
                    .is_some_and(|types| {
                        types
                            .iter()
                            .any(|t| t.as_str() == Some(exception_type.as_str()))
                    });
                let status = if timeout_kind.is_some() {
                    "timed_out"
                } else {
                    "failed"
                };
                report["outcome"] = json!(status);
                let original = crate::failure_reporting::LocalText::new(&message);
                report["message"] = json!(original.render(crate::failure_reporting::MESSAGE_BYTES));
                messages.push((attempts.len(), original));
                report["exception_type"] = json!(exception_type);
                report["non_retryable"] = json!(non_retryable);
                if let Some(kind) = timeout_kind {
                    report["timeout_kind"] = json!(kind);
                }
                let retry = number < max_attempts
                    && !non_retryable
                    && timeout_kind != Some("schedule_to_close");
                if retry {
                    let backoff = policy
                        .and_then(|p| p["backoff_seconds"].get((number - 1) as usize))
                        .and_then(Value::as_u64)
                        .unwrap_or(0);
                    report["retry_reason"] = json!(if timeout_kind.is_some() {
                        "timeout"
                    } else {
                        "failure"
                    });
                    report["backoff_seconds"] = json!(backoff);
                    attempts.push(report);
                    let wake = Instant::now() + Duration::from_secs(backoff);
                    while Instant::now() < wake {
                        // An elapsed total deadline is reported as the next
                        // terminal attempt, with no further callback invocation.
                        if total_deadline.is_some_and(|deadline| Instant::now() >= deadline) {
                            break;
                        }
                        if !self.local_claim_is_active(task).await {
                            return Ok(None);
                        }
                        let deadline = total_deadline.map_or(wake, |d| d.min(wake));
                        tokio::time::sleep(
                            deadline
                                .saturating_duration_since(Instant::now())
                                .min(LEASE_RENEWAL_INTERVAL),
                        )
                        .await;
                    }
                    continue;
                }
                attempts.push(report);
                for field in [
                    "outcome",
                    "message",
                    "exception_type",
                    "non_retryable",
                    "timeout_kind",
                ] {
                    if let Some(value) = attempts.last().unwrap().get(field) {
                        wire[field] = value.clone();
                    }
                }
                wire["attempts"] = json!(attempts);
                if !self
                    .admit_local_command(task, context, request.command_index, &mut wire, &messages)
                    .await?
                    || !self.local_claim_is_active(task).await
                {
                    return Ok(None);
                }
                // Match the terminal fields Server persists. Database-generated
                // execution/failure IDs are only available on committed replay.
                let mut payload = wire.clone();
                if let Some(message) = payload["message"].as_str() {
                    payload["message"] =
                        json!(crate::failure_reporting::committed_message(message));
                }
                payload["attempt_number"] = json!(number);
                payload["failure_category"] = json!(if timeout_kind.is_some() {
                    "timeout"
                } else {
                    "application"
                });
                if timeout_kind.is_some() {
                    payload["exception_class"] = payload["exception_type"].take();
                    payload.as_object_mut().unwrap().remove("exception_type");
                    payload.as_object_mut().unwrap().remove("non_retryable");
                }
                let event = HistoryEvent {
                    event_type: if timeout_kind.is_some() {
                        "ActivityTimedOut"
                    } else {
                        "ActivityFailed"
                    }
                    .into(),
                    payload,
                    raw: HashMap::new(),
                };
                let result =
                    activity_outcome(&event, &task.payload_codec, Some(activity_type.clone()))?;
                return Ok(Some((wire, result)));
            }
            report["outcome"] = json!("completed");
            attempts.push(report);
            wire["outcome"] = json!("completed");
            wire["attempts"] = json!(attempts);
            if !self
                .admit_local_command(task, context, request.command_index, &mut wire, &messages)
                .await?
                || !self.local_claim_is_active(task).await
            {
                return Ok(None);
            }
            return Ok(Some((wire, Ok(result.unwrap()))));
        }
        unreachable!("validated local retry budget includes at least one attempt")
    }
}

fn local_error(error: Error) -> (String, String, bool, Option<&'static str>) {
    let failure = crate::ActivityError::from_error(&error, "RustActivityError");
    (
        failure.message,
        failure.exception_type,
        failure.non_retryable,
        None,
    )
}

fn heartbeat_extended(reports: &Arc<Mutex<Heartbeats>>, timeout: Option<u64>) -> Result<bool> {
    let reports = reports.lock().map_err(|_| Error::WorkflowStatePoisoned)?;
    Ok(timeout.is_some_and(|seconds| reports.last + Duration::from_secs(seconds) > Instant::now()))
}