kruxiaflow-worker 0.12.4

Worker SDK for Kruxia Flow — run Rust activities in budgeted workflows with engine-enforced cost limits
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
//! Protocol conformance tests against a mock server: auth, 401 refresh,
//! 409 idempotency, usage reporting, and the full poll → execute → report
//! loop including panic/timeout containment and graceful drain.

use kruxiaflow_worker::{
    ActivityError, ActivityResult, ClientError, UsageEntry, Worker, WorkerApiClient, WorkerConfig,
};
use rust_decimal_macros::dec;
use serde_json::{Value, json};
use std::time::Duration;
use uuid::Uuid;
use wiremock::matchers::{body_partial_json, header, header_exists, method, path};
use wiremock::{Mock, MockServer, Request, ResponseTemplate};

fn token_mock() -> Mock {
    Mock::given(method("POST"))
        .and(path("/api/v1/oauth/token"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "access_token": "test_token_1",
            "token_type": "Bearer",
            "expires_in": 3600
        })))
}

fn empty_poll_response() -> ResponseTemplate {
    ResponseTemplate::new(200).set_body_json(json!({"activities": [], "count": 0}))
}

fn one_activity_response(activity_id: Uuid, name: &str, parameters: Value) -> ResponseTemplate {
    ResponseTemplate::new(200).set_body_json(json!({
        "activities": [{
            "activity_id": activity_id,
            "workflow_id": Uuid::now_v7(),
            "activity_key": "step_one",
            "worker": "demo",
            "activity_name": name,
            "parameters": parameters,
            "settings": null,
            "timeout_seconds": null,
            "output_definitions": null,
            "signal_data": null
        }],
        "count": 1
    }))
}

fn test_config(server: &MockServer) -> WorkerConfig {
    WorkerConfig {
        api_url: server.uri(),
        worker: "demo".to_string(),
        worker_id: "test_worker".to_string(),
        poll_interval: Duration::from_millis(10),
        shutdown_timeout: Duration::from_secs(5),
        ..WorkerConfig::default()
    }
}

/// Wait until the mock server has seen `count` requests matching the
/// predicate, or panic after ~2s.
async fn wait_for_requests(
    server: &MockServer,
    count: usize,
    predicate: impl Fn(&Request) -> bool,
) {
    for _ in 0..200 {
        let requests = server.received_requests().await.unwrap_or_default();
        if requests.iter().filter(|r| predicate(r)).count() >= count {
            return;
        }
        tokio::time::sleep(Duration::from_millis(10)).await;
    }
    panic!("mock server never received {count} matching request(s)");
}

// =========================================================================
// Client: auth
// =========================================================================

#[tokio::test]
async fn client_sends_bearer_token_from_credentials() {
    let server = MockServer::start().await;
    token_mock().expect(1).mount(&server).await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .and(header("authorization", "Bearer test_token_1"))
        .respond_with(empty_poll_response())
        .expect(1)
        .mount(&server)
        .await;

    let client = WorkerApiClient::with_credentials(server.uri(), "id", "secret");
    let activities = client.poll_activities("demo", "w1", 5).await.unwrap();
    assert!(activities.is_empty());
}

#[tokio::test]
async fn client_refreshes_token_on_401_and_retries_once() {
    let server = MockServer::start().await;
    token_mock().mount(&server).await;
    // First poll: 401. Retry with refreshed token: 200.
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(ResponseTemplate::new(401))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;

    let client = WorkerApiClient::with_credentials(server.uri(), "id", "secret");
    let activities = client.poll_activities("demo", "w1", 5).await.unwrap();
    assert!(activities.is_empty());

    // Token endpoint hit twice: initial + refresh
    let token_requests = server
        .received_requests()
        .await
        .unwrap()
        .iter()
        .filter(|r| r.url.path() == "/api/v1/oauth/token")
        .count();
    assert_eq!(token_requests, 2);
}

#[tokio::test]
async fn client_without_credentials_sends_no_auth_header() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .expect(1)
        .mount(&server)
        .await;

    let client = WorkerApiClient::new(server.uri());
    client.poll_activities("demo", "w1", 5).await.unwrap();

    let requests = server.received_requests().await.unwrap();
    assert!(
        requests
            .iter()
            .all(|r| !r.headers.contains_key("authorization")),
        "unauthenticated client must not send an Authorization header"
    );
}

#[tokio::test]
async fn client_without_credentials_maps_401_to_auth_required() {
    let server = MockServer::start().await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(ResponseTemplate::new(401))
        .mount(&server)
        .await;

    let client = WorkerApiClient::new(server.uri());
    let err = client.poll_activities("demo", "w1", 5).await.unwrap_err();
    assert!(matches!(err, ClientError::AuthRequired));
}

// =========================================================================
// Client: completion / failure payloads
// =========================================================================

#[tokio::test]
async fn complete_sends_usage_entries_and_returns_warnings() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/complete")))
        .and(body_partial_json(json!({
            "worker_id": "w1",
            "output": {"summary": "done"},
            "cost_usd": "0.002",
            "usage": [{
                "provider": "anthropic",
                "model": "claude-sonnet-5",
                "input_tokens": 1000,
                "output_tokens": 50,
                "cache_read_tokens": 800,
                "cache_creation_tokens": 0
            }]
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "acknowledged": true,
            "warnings": ["unknown model recorded at cost 0"]
        })))
        .expect(1)
        .mount(&server)
        .await;

    let client = WorkerApiClient::new(server.uri());
    let ack = client
        .complete_activity(
            activity_id,
            "w1",
            json!({"summary": "done"}),
            Some(dec!(0.002)),
            &[UsageEntry::new("anthropic", "claude-sonnet-5")
                .input_tokens(1000)
                .output_tokens(50)
                .cache_read_tokens(800)],
        )
        .await
        .unwrap();
    assert_eq!(ack.warnings.len(), 1);
}

#[tokio::test]
async fn complete_omits_usage_when_empty() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/complete")))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"acknowledged": true})))
        .expect(1)
        .mount(&server)
        .await;

    let client = WorkerApiClient::new(server.uri());
    client
        .complete_activity(activity_id, "w1", json!({}), None, &[])
        .await
        .unwrap();

    let request = &server.received_requests().await.unwrap()[0];
    let body: Value = serde_json::from_slice(&request.body).unwrap();
    assert!(body.get("usage").is_none(), "empty usage must be omitted");
    assert!(body.get("cost_usd").is_none());
}

#[tokio::test]
async fn fail_sends_error_with_usage_and_cost() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/fail")))
        .and(body_partial_json(json!({
            "worker_id": "w1",
            "error": {
                "code": "RATE_LIMITED",
                "message": "provider rate limit",
                "retryable": true
            },
            "cost_usd": "0.01",
            "usage": [{"provider": "openai", "model": "gpt-5"}]
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "acknowledged": true,
            "will_retry": true
        })))
        .expect(1)
        .mount(&server)
        .await;

    let client = WorkerApiClient::new(server.uri());
    let error = ActivityError::retryable("RATE_LIMITED", "provider rate limit")
        .with_cost(dec!(0.01))
        .with_usage(vec![UsageEntry::new("openai", "gpt-5")]);
    let ack = client
        .fail_activity(activity_id, "w1", &error)
        .await
        .unwrap();
    assert!(ack.will_retry);
}

#[tokio::test]
async fn conflict_is_a_distinct_error() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/heartbeat")))
        .respond_with(
            ResponseTemplate::new(409).set_body_json(json!({"error": "already completed"})),
        )
        .mount(&server)
        .await;

    let client = WorkerApiClient::new(server.uri());
    let err = client.heartbeat(activity_id, "w1").await.unwrap_err();
    assert!(matches!(err, ClientError::Conflict { .. }));
}

// =========================================================================
// Worker loop: poll → execute → report
// =========================================================================

/// Build a worker against the mock server, run it in the background, and
/// return its shutdown handle plus the join handle.
async fn spawn_worker(server: &MockServer, worker: Worker) -> tokio::task::JoinHandle<()> {
    let _ = server;
    tokio::spawn(async move { worker.run().await })
}

#[tokio::test]
async fn poll_execute_complete_roundtrip() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();

    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(one_activity_response(
            activity_id,
            "echo",
            json!({"message": "hello"}),
        ))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/complete")))
        .and(body_partial_json(json!({
            "worker_id": "test_worker",
            "output": {"echoed": "hello"}
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"acknowledged": true})))
        .expect(1)
        .mount(&server)
        .await;

    #[derive(serde::Deserialize)]
    struct EchoParams {
        message: String,
    }

    let worker = Worker::builder()
        .config(test_config(&server))
        .register_fn("echo", |params: EchoParams, _ctx| async move {
            Ok(ActivityResult::value("echoed", json!(params.message)))
        })
        .build()
        .unwrap();
    let handle = worker.handle();
    let join = spawn_worker(&server, worker).await;

    wait_for_requests(&server, 1, |r| r.url.path().ends_with("/complete")).await;
    handle.shutdown();
    tokio::time::timeout(Duration::from_secs(5), join)
        .await
        .expect("worker should shut down")
        .unwrap();
}

#[tokio::test]
async fn panicking_handler_reports_retryable_failure() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();

    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(one_activity_response(activity_id, "boom", json!({})))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/fail")))
        .and(body_partial_json(json!({
            "error": {"code": "PANIC", "retryable": true}
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"acknowledged": true})))
        .expect(1)
        .mount(&server)
        .await;

    let worker = Worker::builder()
        .config(test_config(&server))
        .worker("demo")
        .register_fn("boom", |_params: Value, _ctx| async move {
            panic!("intentional test panic");
            #[allow(unreachable_code)]
            Ok(ActivityResult::default())
        })
        .build()
        .unwrap();
    let handle = worker.handle();
    let join = spawn_worker(&server, worker).await;

    // The worker survives the panic and reports the failure.
    wait_for_requests(&server, 1, |r| r.url.path().ends_with("/fail")).await;
    handle.shutdown();
    tokio::time::timeout(Duration::from_secs(5), join)
        .await
        .expect("worker should survive a panicking handler")
        .unwrap();
}

#[tokio::test]
async fn terminal_error_reports_retryable_false() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();

    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(one_activity_response(activity_id, "reject", json!({})))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/fail")))
        .and(body_partial_json(json!({
            "error": {"code": "BAD_INPUT", "retryable": false}
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"acknowledged": true})))
        .expect(1)
        .mount(&server)
        .await;

    let worker = Worker::builder()
        .config(test_config(&server))
        .worker("demo")
        .register_fn("reject", |_params: Value, _ctx| async move {
            Err::<ActivityResult, _>(ActivityError::terminal("BAD_INPUT", "no"))
        })
        .build()
        .unwrap();
    let handle = worker.handle();
    let join = spawn_worker(&server, worker).await;

    wait_for_requests(&server, 1, |r| r.url.path().ends_with("/fail")).await;
    handle.shutdown();
    tokio::time::timeout(Duration::from_secs(5), join)
        .await
        .unwrap()
        .unwrap();
}

#[tokio::test]
async fn invalid_typed_params_report_invalid_parameters() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();

    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(one_activity_response(
            activity_id,
            "echo",
            json!({"unexpected": true}),
        ))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/fail")))
        .and(body_partial_json(json!({
            "error": {"code": "INVALID_PARAMETERS", "retryable": false}
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"acknowledged": true})))
        .expect(1)
        .mount(&server)
        .await;

    #[derive(serde::Deserialize)]
    struct EchoParams {
        #[allow(dead_code)]
        message: String,
    }

    let worker = Worker::builder()
        .config(test_config(&server))
        .worker("demo")
        .register_fn("echo", |params: EchoParams, _ctx| async move {
            Ok(ActivityResult::value("echoed", json!(params.message)))
        })
        .build()
        .unwrap();
    let handle = worker.handle();
    let join = spawn_worker(&server, worker).await;

    wait_for_requests(&server, 1, |r| r.url.path().ends_with("/fail")).await;
    handle.shutdown();
    tokio::time::timeout(Duration::from_secs(5), join)
        .await
        .unwrap()
        .unwrap();
}

#[tokio::test]
async fn timeout_reports_retryable_timeout_failure() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();

    // timeout_seconds: 1 → slow handler must be failed with TIMEOUT
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "activities": [{
                "activity_id": activity_id,
                "workflow_id": Uuid::now_v7(),
                "activity_key": "slow_step",
                "worker": "demo",
                "activity_name": "slow",
                "parameters": {},
                "settings": null,
                "timeout_seconds": 1,
                "output_definitions": null,
                "signal_data": null
            }],
            "count": 1
        })))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/fail")))
        .and(body_partial_json(json!({
            "error": {"code": "TIMEOUT", "retryable": true}
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"acknowledged": true})))
        .expect(1)
        .mount(&server)
        .await;

    let worker = Worker::builder()
        .config(test_config(&server))
        .worker("demo")
        .register_fn("slow", |_params: Value, _ctx| async move {
            tokio::time::sleep(Duration::from_secs(30)).await;
            Ok(ActivityResult::default())
        })
        .build()
        .unwrap();
    let handle = worker.handle();
    let join = spawn_worker(&server, worker).await;

    wait_for_requests(&server, 1, |r| r.url.path().ends_with("/fail")).await;
    handle.shutdown();
    tokio::time::timeout(Duration::from_secs(5), join)
        .await
        .unwrap()
        .unwrap();
}

#[tokio::test]
async fn conflict_on_complete_is_swallowed() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();

    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(one_activity_response(activity_id, "echo", json!({})))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/complete")))
        .respond_with(ResponseTemplate::new(409).set_body_json(json!({"error": "already done"})))
        .expect(1)
        .mount(&server)
        .await;

    let worker = Worker::builder()
        .config(test_config(&server))
        .worker("demo")
        .register_fn("echo", |params: Value, _ctx| async move {
            Ok(ActivityResult::value("echoed", params))
        })
        .build()
        .unwrap();
    let handle = worker.handle();
    let join = spawn_worker(&server, worker).await;

    wait_for_requests(&server, 1, |r| r.url.path().ends_with("/complete")).await;
    // Worker keeps running (conflict swallowed) and shuts down cleanly.
    handle.shutdown();
    tokio::time::timeout(Duration::from_secs(5), join)
        .await
        .expect("conflict on complete must not break the worker")
        .unwrap();
}

// =========================================================================
// Graceful shutdown
// =========================================================================

#[tokio::test]
async fn shutdown_drains_in_flight_activity() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();

    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(one_activity_response(activity_id, "slowish", json!({})))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/complete")))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"acknowledged": true})))
        .expect(1)
        .mount(&server)
        .await;

    let worker = Worker::builder()
        .config(test_config(&server))
        .worker("demo")
        .register_fn("slowish", |_params: Value, _ctx| async move {
            tokio::time::sleep(Duration::from_millis(500)).await;
            Ok(ActivityResult::value("done", json!(true)))
        })
        .build()
        .unwrap();
    let handle = worker.handle();
    let join = spawn_worker(&server, worker).await;

    // Wait until the activity is in flight, then shut down mid-execution.
    wait_for_requests(&server, 1, |r| r.url.path() == "/api/v1/workers/poll").await;
    tokio::time::sleep(Duration::from_millis(100)).await;
    handle.shutdown();

    tokio::time::timeout(Duration::from_secs(5), join)
        .await
        .expect("worker should drain and stop")
        .unwrap();

    // The in-flight activity completed during the drain.
    let completes = server
        .received_requests()
        .await
        .unwrap()
        .iter()
        .filter(|r| r.url.path().ends_with("/complete"))
        .count();
    assert_eq!(
        completes, 1,
        "in-flight activity must complete during drain"
    );
}

#[tokio::test]
async fn drain_deadline_fails_stuck_activity_as_retryable() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();

    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(one_activity_response(activity_id, "stuck", json!({})))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/fail")))
        .and(body_partial_json(json!({
            "error": {"code": "WORKER_SHUTDOWN", "retryable": true}
        })))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"acknowledged": true})))
        .expect(1)
        .mount(&server)
        .await;

    let config = WorkerConfig {
        shutdown_timeout: Duration::from_millis(200),
        ..test_config(&server)
    };
    let worker = Worker::builder()
        .config(config)
        .worker("demo")
        .register_fn("stuck", |_params: Value, _ctx| async move {
            tokio::time::sleep(Duration::from_secs(60)).await;
            Ok(ActivityResult::default())
        })
        .build()
        .unwrap();
    let handle = worker.handle();
    let join = spawn_worker(&server, worker).await;

    wait_for_requests(&server, 1, |r| r.url.path() == "/api/v1/workers/poll").await;
    tokio::time::sleep(Duration::from_millis(100)).await;
    handle.shutdown();

    tokio::time::timeout(Duration::from_secs(5), join)
        .await
        .expect("worker must not hang on a stuck activity")
        .unwrap();

    wait_for_requests(&server, 1, |r| r.url.path().ends_with("/fail")).await;
}

// =========================================================================
// Heartbeats
// =========================================================================

#[tokio::test]
async fn heartbeat_conflict_cancels_execution_without_reporting() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();

    // Long timeout (> 60s) so the heartbeat task spawns; short heartbeat
    // interval so the first beat arrives immediately.
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "activities": [{
                "activity_id": activity_id,
                "workflow_id": Uuid::now_v7(),
                "activity_key": "long_step",
                "worker": "demo",
                "activity_name": "long",
                "parameters": {},
                "settings": null,
                "timeout_seconds": 300,
                "output_definitions": null,
                "signal_data": null
            }],
            "count": 1
        })))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/heartbeat")))
        .respond_with(ResponseTemplate::new(409).set_body_json(json!({"error": "reassigned"})))
        .mount(&server)
        .await;

    let config = WorkerConfig {
        heartbeat_interval: Duration::from_millis(50),
        ..test_config(&server)
    };
    let worker = Worker::builder()
        .config(config)
        .worker("demo")
        .register_fn("long", |_params: Value, _ctx| async move {
            tokio::time::sleep(Duration::from_secs(60)).await;
            Ok(ActivityResult::default())
        })
        .build()
        .unwrap();
    let handle = worker.handle();
    let join = spawn_worker(&server, worker).await;

    wait_for_requests(&server, 1, |r| r.url.path().ends_with("/heartbeat")).await;
    // Give the cancellation a moment, then shut down; the drain must be
    // immediate because the reassigned activity was cancelled locally.
    tokio::time::sleep(Duration::from_millis(200)).await;
    handle.shutdown();
    tokio::time::timeout(Duration::from_secs(5), join)
        .await
        .expect("reassigned activity must not block shutdown")
        .unwrap();

    // Neither complete nor fail was reported for the reassigned activity.
    let requests = server.received_requests().await.unwrap();
    assert!(
        requests
            .iter()
            .all(|r| !r.url.path().ends_with("/complete") && !r.url.path().ends_with("/fail")),
        "reassigned activity must not be reported"
    );
}

#[tokio::test]
async fn heartbeats_sent_for_long_activities() {
    let server = MockServer::start().await;
    let activity_id = Uuid::now_v7();

    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "activities": [{
                "activity_id": activity_id,
                "workflow_id": Uuid::now_v7(),
                "activity_key": "long_step",
                "worker": "demo",
                "activity_name": "long",
                "parameters": {},
                "settings": null,
                "timeout_seconds": 300,
                "output_definitions": null,
                "signal_data": null
            }],
            "count": 1
        })))
        .up_to_n_times(1)
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path("/api/v1/workers/poll"))
        .respond_with(empty_poll_response())
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/heartbeat")))
        .and(header_exists("content-type"))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({
            "acknowledged": true,
            "next_heartbeat_seconds": 30
        })))
        .mount(&server)
        .await;
    Mock::given(method("POST"))
        .and(path(format!("/api/v1/activities/{activity_id}/complete")))
        .respond_with(ResponseTemplate::new(200).set_body_json(json!({"acknowledged": true})))
        .expect(1)
        .mount(&server)
        .await;

    let config = WorkerConfig {
        heartbeat_interval: Duration::from_millis(50),
        ..test_config(&server)
    };
    let worker = Worker::builder()
        .config(config)
        .worker("demo")
        .register_fn("long", |_params: Value, _ctx| async move {
            tokio::time::sleep(Duration::from_millis(300)).await;
            Ok(ActivityResult::value("done", json!(true)))
        })
        .build()
        .unwrap();
    let handle = worker.handle();
    let join = spawn_worker(&server, worker).await;

    wait_for_requests(&server, 1, |r| r.url.path().ends_with("/complete")).await;
    handle.shutdown();
    tokio::time::timeout(Duration::from_secs(5), join)
        .await
        .unwrap()
        .unwrap();

    let heartbeats = server
        .received_requests()
        .await
        .unwrap()
        .iter()
        .filter(|r| r.url.path().ends_with("/heartbeat"))
        .count();
    assert!(
        heartbeats >= 2,
        "expected multiple heartbeats, got {heartbeats}"
    );
}