alien-commands 3.0.1

Alien Commands protocol implementation
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
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
use std::any::Any;
use std::fmt::Debug;

use alien_error::{Context, ContextError, IntoAlienError, IntoAlienErrorDirect};
use async_trait::async_trait;

use crate::{error::Result, types::Envelope};

/// Trait for dispatching command envelopes to agents via platform-specific transport
#[async_trait]
pub trait CommandDispatcher: Send + Sync + Debug {
    /// Dispatch an envelope to the target agent
    async fn dispatch(&self, envelope: &Envelope) -> Result<()>;

    /// Helper method for downcasting to concrete types in tests
    fn as_any(&self) -> &dyn Any;
}

/// No-op command dispatcher that succeeds without doing anything
#[derive(Debug)]
pub struct NullCommandDispatcher;

#[async_trait]
impl CommandDispatcher for NullCommandDispatcher {
    async fn dispatch(&self, envelope: &Envelope) -> Result<()> {
        tracing::debug!(
            command_id = %envelope.command_id,
            command = %envelope.command,
            "NullCommandDispatcher: no-op dispatch"
        );
        Ok(())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

#[cfg(any(feature = "server", feature = "dispatchers"))]
mod platform_dispatchers {
    use super::*;
    use alien_aws_clients::aws::{
        lambda::{InvocationType, InvokeRequest, LambdaApi, LambdaClient},
        AwsClientConfig,
    };
    use alien_aws_clients::AwsCredentialProvider;
    use alien_azure_clients::azure::{
        service_bus::{
            AzureServiceBusDataPlaneClient, SendMessageParameters, ServiceBusDataPlaneApi,
        },
        token_cache::AzureTokenCache,
        AzureClientConfig,
    };
    use alien_client_core::{
        redact_request_body, Error as CloudClientError, ErrorData as CloudClientErrorData,
    };
    use alien_gcp_clients::gcp::{
        pubsub::{PubSubApi, PubSubClient, PublishRequest, PubsubMessage},
        GcpClientConfig,
    };
    use base64::prelude::*;
    use reqwest::Client;
    use std::collections::HashMap;
    use std::time::Duration;

    const HTTP_COMMAND_REQUEST_TIMEOUT: Duration = Duration::from_secs(10);

    /// Provider responses that unambiguously reject the current request.
    ///
    /// Timeouts and server errors are deliberately excluded: the provider may
    /// have accepted the command before the acknowledgement was lost. Keep the
    /// allowlist narrow instead of treating every 4xx as proof of non-delivery.
    fn is_definite_cloud_rejection_status(status: u16) -> bool {
        matches!(status, 400 | 401 | 403 | 404 | 409 | 413 | 415 | 422 | 429)
    }

    /// Service Bus obtains credentials and builds the request before sending
    /// it. Those local failures, plus an explicit allowlisted HTTP response,
    /// prove that the queue did not accept this command.
    fn is_definite_service_bus_rejection(error: &CloudClientError) -> bool {
        match error.error.as_ref() {
            Some(
                CloudClientErrorData::AuthenticationError { .. }
                | CloudClientErrorData::InvalidClientConfig { .. }
                | CloudClientErrorData::SerializationError { .. },
            ) => true,
            Some(CloudClientErrorData::HttpResponseError { http_status, .. }) => {
                is_definite_cloud_rejection_status(*http_status)
            }
            _ => false,
        }
    }

    fn http_status_from_context(context: Option<&serde_json::Value>) -> Option<u16> {
        context
            .and_then(serde_json::Value::as_object)
            .and_then(|fields| {
                fields
                    .get("http_status")
                    .or_else(|| fields.get("httpStatus"))
            })
            .and_then(serde_json::Value::as_u64)
            .and_then(|status| u16::try_from(status).ok())
    }

    /// Find the provider HTTP status before erasing response-derived context.
    fn command_provider_http_status(error: &CloudClientError) -> Option<u16> {
        if let Some(CloudClientErrorData::HttpResponseError { http_status, .. }) =
            error.error.as_ref()
        {
            return Some(*http_status);
        }
        if let Some(status) = http_status_from_context(error.context.as_ref()) {
            return Some(status);
        }

        let mut layer = error.source.as_deref();
        while let Some(source) = layer {
            if let Some(status) = http_status_from_context(source.context.as_ref()) {
                return Some(status);
            }
            layer = source.source.as_deref();
        }
        None
    }

    /// Remove all provider request/response-derived details from a command
    /// dispatch error while retaining machine-readable error codes and the
    /// provider HTTP status. Provider and proxy response bodies can reflect the
    /// submitted envelope, including inline params and signed URLs, so keeping
    /// only the category and numeric status is the safe command boundary.
    fn scrub_command_provider_error(mut error: CloudClientError) -> CloudClientError {
        let provider_status = command_provider_http_status(&error);
        error.message = provider_status.map_or_else(
            || format!("Cloud provider request failed ({})", error.code),
            |status| format!("Cloud provider request returned HTTP {status}"),
        );
        error.context = provider_status.map(|status| {
            serde_json::json!({
                "http_status": status,
            })
        });
        error.hint = None;
        error.error = None;

        let mut layer = error.source.as_deref_mut();
        while let Some(source) = layer {
            source.message = format!("Cloud provider error ({})", source.code);
            source.context = None;
            source.hint = None;
            source.error = None;
            layer = source.source.as_deref_mut();
        }

        error
    }

    /// HTTP command dispatcher used by Local and Kubernetes Workers.
    ///
    /// The target is the runtime-owned command endpoint, not an application
    /// route. The deployment token authenticates the operator/manager relay to
    /// the Worker runtime.
    pub struct HttpCommandDispatcher {
        client: Client,
        target_url: String,
        token: String,
        request_timeout: Duration,
    }

    impl Debug for HttpCommandDispatcher {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_struct("HttpCommandDispatcher")
                .field("target_url", &self.target_url)
                .field("token", &"[REDACTED]")
                .finish()
        }
    }

    impl HttpCommandDispatcher {
        pub fn new(client: Client, target_url: String, token: String) -> Self {
            Self {
                client,
                target_url,
                token,
                request_timeout: HTTP_COMMAND_REQUEST_TIMEOUT,
            }
        }

        #[cfg(test)]
        pub(crate) fn with_request_timeout(mut self, request_timeout: Duration) -> Self {
            self.request_timeout = request_timeout;
            self
        }
    }

    #[async_trait]
    impl CommandDispatcher for HttpCommandDispatcher {
        async fn dispatch(&self, envelope: &Envelope) -> Result<()> {
            let response = match self
                .client
                .post(&self.target_url)
                .bearer_auth(&self.token)
                .json(envelope)
                .timeout(self.request_timeout)
                .send()
                .await
            {
                Ok(response) => response,
                Err(error) => {
                    // Builder/connect failures happen before the runtime can
                    // accept the envelope. Timeouts and other request errors
                    // are ambiguous: the runtime may have returned 202 after
                    // the acknowledgement path was lost.
                    let definite_non_delivery = error.is_builder() || error.is_connect();
                    let error = error.without_url();
                    let context = if definite_non_delivery {
                        crate::ErrorData::TransportDispatchRejected {
                            message: "Worker runtime was unreachable before command delivery"
                                .to_string(),
                            transport_type: Some("http".to_string()),
                            target: Some(envelope.command_id.clone()),
                        }
                    } else {
                        crate::ErrorData::TransportDispatchFailed {
                            message: "Worker runtime acknowledgement was not received".to_string(),
                            transport_type: Some("http".to_string()),
                            target: Some(envelope.command_id.clone()),
                        }
                    };
                    return Err(error.into_alien_error().context(context));
                }
            };

            // The runtime contract returns 202 only after validation,
            // duplicate suppression, and tracked-task acceptance. A legacy
            // application route returning another 2xx is not delivery.
            if response.status() != reqwest::StatusCode::ACCEPTED {
                return Err(alien_error::AlienError::new(
                    crate::ErrorData::TransportDispatchRejected {
                        message: format!(
                            "Worker runtime rejected command push with HTTP {}",
                            response.status()
                        ),
                        transport_type: Some("http".to_string()),
                        target: Some(envelope.command_id.clone()),
                    },
                ));
            }

            tracing::debug!(
                command_id = %envelope.command_id,
                command = %envelope.command,
                target_url = %self.target_url,
                "Successfully pushed command envelope to Worker runtime"
            );

            Ok(())
        }

        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    /// AWS Lambda command dispatcher using InvokeFunction API
    #[derive(Debug)]
    pub struct LambdaCommandDispatcher {
        lambda_client: LambdaClient,
        function_name: String,
    }

    impl LambdaCommandDispatcher {
        pub async fn new(
            client: Client,
            config: AwsClientConfig,
            function_name: String,
        ) -> Result<Self> {
            let credentials = AwsCredentialProvider::from_config(config)
                .await
                .into_alien_error()
                .context(crate::ErrorData::TransportDispatchFailed {
                    message: "Failed to create AWS credential provider".to_string(),
                    transport_type: Some("lambda".to_string()),
                    target: None,
                })?;
            Ok(Self {
                lambda_client: LambdaClient::new(client, credentials),
                function_name,
            })
        }
    }

    #[async_trait]
    impl CommandDispatcher for LambdaCommandDispatcher {
        async fn dispatch(&self, envelope: &Envelope) -> Result<()> {
            // Serialize the command envelope as JSON payload
            let payload = serde_json::to_vec(envelope).into_alien_error().context(
                crate::ErrorData::TransportDispatchRejected {
                    message: "Failed to serialize command envelope before Lambda dispatch"
                        .to_string(),
                    transport_type: Some("lambda".to_string()),
                    target: Some(envelope.command_id.clone()),
                },
            )?;

            let function_name = self.function_name.clone();

            // Use async invocation to send the envelope to the Lambda function
            // The Lambda function should have alien-worker-runtime configured to handle command envelopes
            let invoke_request = InvokeRequest::builder()
                .function_name(function_name.clone())
                .invocation_type(InvocationType::Event) // Async invocation
                .payload(payload)
                .build();

            let invoke_response = self.lambda_client.invoke(invoke_request).await.context(
                crate::ErrorData::TransportDispatchFailed {
                    message: format!("Failed to invoke Lambda function {}", function_name),
                    transport_type: Some("lambda".to_string()),
                    target: Some(envelope.command_id.clone()),
                },
            )?;

            // AWS Lambda's Event invocation contract acknowledges queueing
            // with exactly 202. The client intentionally exposes other HTTP
            // statuses in InvokeResponse, so classify them here without
            // changing the general-purpose Lambda client API.
            if invoke_response.status_code != reqwest::StatusCode::ACCEPTED.as_u16() {
                let context = if is_definite_cloud_rejection_status(invoke_response.status_code) {
                    crate::ErrorData::TransportDispatchRejected {
                        message: format!(
                            "Lambda rejected asynchronous invocation with HTTP {}",
                            invoke_response.status_code
                        ),
                        transport_type: Some("lambda".to_string()),
                        target: Some(envelope.command_id.clone()),
                    }
                } else {
                    crate::ErrorData::TransportDispatchFailed {
                        message: format!(
                            "Lambda asynchronous invocation acknowledgement was HTTP {} instead of 202",
                            invoke_response.status_code
                        ),
                        transport_type: Some("lambda".to_string()),
                        target: Some(envelope.command_id.clone()),
                    }
                };
                return Err(alien_error::AlienError::new(context));
            }

            tracing::debug!(
                command_id = %envelope.command_id,
                command = %envelope.command,
                function_name = %function_name,
                "Successfully dispatched command envelope to Lambda function"
            );

            Ok(())
        }

        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    /// GCP Pub/Sub command dispatcher
    #[derive(Debug)]
    pub struct PubSubCommandDispatcher {
        pubsub_client: PubSubClient,
        #[allow(dead_code)]
        project_id: String,
        topic_id: String,
    }

    impl PubSubCommandDispatcher {
        pub fn new(client: Client, config: GcpClientConfig, topic_id: String) -> Self {
            let project_id = config.project_id.clone();
            Self {
                pubsub_client: PubSubClient::new(client, config),
                project_id,
                topic_id,
            }
        }
    }

    #[async_trait]
    impl CommandDispatcher for PubSubCommandDispatcher {
        async fn dispatch(&self, envelope: &Envelope) -> Result<()> {
            // Serialize the command envelope as JSON
            let envelope_json = serde_json::to_string(envelope).into_alien_error().context(
                crate::ErrorData::TransportDispatchRejected {
                    message: "Failed to serialize command envelope before Pub/Sub dispatch"
                        .to_string(),
                    transport_type: Some("pubsub".to_string()),
                    target: Some(envelope.command_id.clone()),
                },
            )?;

            // Base64 encode the JSON payload as required by Pub/Sub
            let data = BASE64_STANDARD.encode(envelope_json.as_bytes());

            let topic_id = self.topic_id.clone();

            // Create the Pub/Sub message with command envelope metadata
            let mut attributes = HashMap::new();
            attributes.insert("cmd-protocol".to_string(), envelope.protocol.clone());
            attributes.insert("cmd-command-id".to_string(), envelope.command_id.clone());
            attributes.insert("cmd-command".to_string(), envelope.command.clone());

            let message = PubsubMessage::builder()
                .data(data)
                .attributes(attributes)
                .build();

            let publish_request = PublishRequest::builder().messages(vec![message]).build();

            // Pub/Sub's generic client retries internally. A final 4xx does
            // not prove an earlier timed-out attempt was not accepted, so all
            // provider-call errors remain ambiguous. The canonical scrub must
            // run before adding a non-internal command error layer because the
            // captured request contains the base64 command envelope.
            let publish_result = redact_request_body(
                self.pubsub_client
                    .publish(topic_id.clone(), publish_request)
                    .await,
            );
            if let Err(error) = publish_result {
                return Err(scrub_command_provider_error(error).context(
                    crate::ErrorData::TransportDispatchFailed {
                        message: format!("Failed to publish to Pub/Sub topic {}", topic_id),
                        transport_type: Some("pubsub".to_string()),
                        target: Some(envelope.command_id.clone()),
                    },
                ));
            }

            tracing::debug!(
                command_id = %envelope.command_id,
                command = %envelope.command,
                topic_id = %topic_id,
                "Successfully dispatched command envelope to Pub/Sub topic"
            );

            Ok(())
        }

        fn as_any(&self) -> &dyn Any {
            self
        }
    }

    /// Azure Service Bus command dispatcher
    #[derive(Debug)]
    pub struct ServiceBusCommandDispatcher {
        servicebus_client: AzureServiceBusDataPlaneClient,
        namespace_name: String,
        queue_name: String,
    }

    impl ServiceBusCommandDispatcher {
        pub fn new(
            client: Client,
            config: AzureClientConfig,
            namespace_name: String,
            queue_name: String,
        ) -> Self {
            Self {
                servicebus_client: AzureServiceBusDataPlaneClient::new(
                    client,
                    AzureTokenCache::new(config),
                ),
                namespace_name,
                queue_name,
            }
        }
    }

    #[async_trait]
    impl CommandDispatcher for ServiceBusCommandDispatcher {
        async fn dispatch(&self, envelope: &Envelope) -> Result<()> {
            // Serialize the command envelope as JSON
            let envelope_json = serde_json::to_string(envelope).into_alien_error().context(
                crate::ErrorData::TransportDispatchRejected {
                    message: "Failed to serialize command envelope before Service Bus dispatch"
                        .to_string(),
                    transport_type: Some("servicebus".to_string()),
                    target: Some(envelope.command_id.clone()),
                },
            )?;

            let namespace_name = self.namespace_name.clone();
            let queue_name = self.queue_name.clone();

            // Create custom properties for command metadata
            let mut custom_properties = HashMap::new();
            custom_properties.insert("cmd-protocol".to_string(), envelope.protocol.clone());
            custom_properties.insert("cmd-command-id".to_string(), envelope.command_id.clone());
            custom_properties.insert("cmd-command".to_string(), envelope.command.clone());

            let message = SendMessageParameters {
                body: envelope_json,
                broker_properties: None,
                custom_properties,
            };

            // Service Bus does not retry SendMessage internally, so an
            // allowlisted 4xx is a reliable rejection. Network failures,
            // request timeouts, and 5xx responses are ambiguous. Scrub the
            // command body before wrapping either category.
            let send_result = redact_request_body(
                self.servicebus_client
                    .send_message(namespace_name.clone(), queue_name.clone(), message)
                    .await,
            );
            if let Err(error) = send_result {
                let context = if is_definite_service_bus_rejection(&error) {
                    crate::ErrorData::TransportDispatchRejected {
                        message: format!(
                            "Service Bus rejected message for queue {}/{}",
                            namespace_name, queue_name
                        ),
                        transport_type: Some("servicebus".to_string()),
                        target: Some(envelope.command_id.clone()),
                    }
                } else {
                    crate::ErrorData::TransportDispatchFailed {
                        message: format!(
                            "Service Bus acknowledgement failed for queue {}/{}",
                            namespace_name, queue_name
                        ),
                        transport_type: Some("servicebus".to_string()),
                        target: Some(envelope.command_id.clone()),
                    }
                };
                return Err(scrub_command_provider_error(error).context(context));
            }

            tracing::debug!(
                command_id = %envelope.command_id,
                command = %envelope.command,
                namespace = %namespace_name,
                queue = %queue_name,
                "Successfully dispatched command envelope to Service Bus queue"
            );

            Ok(())
        }

        fn as_any(&self) -> &dyn Any {
            self
        }
    }
}

#[cfg(any(feature = "server", feature = "dispatchers"))]
pub use platform_dispatchers::*;

#[cfg(all(test, feature = "test-utils"))]
mod tests {
    use std::collections::HashMap;
    use std::time::Duration;

    use alien_core::{
        AwsClientConfig, AwsCredentials, AwsServiceOverrides, AzureClientConfig, AzureCredentials,
        AzureServiceOverrides, BodySpec, GcpClientConfig, GcpCredentials, GcpServiceOverrides,
    };
    use axum::{
        extract::Json,
        http::{header::AUTHORIZATION, HeaderMap, StatusCode},
        routing::post,
        Router,
    };
    use base64::prelude::*;

    use super::{
        CommandDispatcher, HttpCommandDispatcher, LambdaCommandDispatcher, PubSubCommandDispatcher,
        ServiceBusCommandDispatcher,
    };

    const COMMAND_BODY_SENTINEL: &str = "COMMAND_ERROR_BODY_MUST_BE_REDACTED";
    const SIGNED_URL_SENTINEL: &str =
        "https://storage.example.test/result?signature=MUST_NOT_ESCAPE";

    async fn spawn_static_response(
        status: StatusCode,
        body: &'static str,
    ) -> (String, tokio::task::JoinHandle<()>) {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let server = tokio::spawn(async move {
            axum::serve(
                listener,
                Router::new().fallback(move || async move { (status, body) }),
            )
            .await
            .unwrap();
        });
        (format!("http://{address}"), server)
    }

    fn sensitive_envelope(command_id: &str) -> crate::Envelope {
        let mut envelope = crate::test_utils::test_envelope(
            command_id,
            COMMAND_BODY_SENTINEL,
            BodySpec::inline(COMMAND_BODY_SENTINEL.as_bytes()),
        );
        envelope.response_handling.submit_response_url = SIGNED_URL_SENTINEL.to_string();
        envelope
    }

    fn aws_config(lambda_endpoint: String) -> AwsClientConfig {
        AwsClientConfig {
            account_id: "123456789012".to_string(),
            region: "us-east-1".to_string(),
            credentials: AwsCredentials::AccessKeys {
                access_key_id: "test-access-key".to_string(),
                secret_access_key: "test-secret-key".to_string(),
                session_token: None,
            },
            service_overrides: Some(AwsServiceOverrides {
                endpoints: HashMap::from([("lambda".to_string(), lambda_endpoint)]),
            }),
        }
    }

    fn azure_config(service_bus_endpoint: String) -> AzureClientConfig {
        AzureClientConfig {
            subscription_id: "test-subscription".to_string(),
            tenant_id: "test-tenant".to_string(),
            region: Some("eastus".to_string()),
            credentials: AzureCredentials::AccessToken {
                token: "test-token".to_string(),
            },
            service_overrides: Some(AzureServiceOverrides {
                endpoints: HashMap::from([("servicebus".to_string(), service_bus_endpoint)]),
            }),
        }
    }

    fn gcp_config(pubsub_endpoint: String) -> GcpClientConfig {
        GcpClientConfig {
            project_id: "test-project".to_string(),
            region: "us-central1".to_string(),
            credentials: GcpCredentials::AccessToken {
                token: "test-token".to_string(),
            },
            service_overrides: Some(GcpServiceOverrides {
                endpoints: HashMap::from([("pubsub".to_string(), pubsub_endpoint)]),
            }),
            project_number: Some("123456789012".to_string()),
        }
    }

    async fn accept_command(
        headers: HeaderMap,
        Json(envelope): Json<crate::Envelope>,
    ) -> StatusCode {
        if headers
            .get(AUTHORIZATION)
            .and_then(|value| value.to_str().ok())
            == Some("Bearer secret")
            && envelope.command_id == "cmd-http"
        {
            StatusCode::ACCEPTED
        } else {
            StatusCode::UNAUTHORIZED
        }
    }

    async fn legacy_success_route() -> StatusCode {
        StatusCode::OK
    }

    #[tokio::test]
    async fn http_dispatcher_posts_authenticated_envelope_and_checks_status() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let server = tokio::spawn(async move {
            axum::serve(
                listener,
                Router::new().route(crate::WORKER_COMMAND_PUSH_PATH, post(accept_command)),
            )
            .await
            .unwrap();
        });
        let target_url = format!("http://{address}{}", crate::WORKER_COMMAND_PUSH_PATH);
        let envelope = crate::test_utils::test_simple_envelope("cmd-http", "sync");

        HttpCommandDispatcher::new(
            reqwest::Client::new(),
            target_url.clone(),
            "secret".to_string(),
        )
        .dispatch(&envelope)
        .await
        .unwrap();

        let error =
            HttpCommandDispatcher::new(reqwest::Client::new(), target_url, "wrong".to_string())
                .dispatch(&envelope)
                .await
                .unwrap_err();
        assert_eq!(error.code, "TRANSPORT_DISPATCH_REJECTED");

        server.abort();
    }

    #[tokio::test]
    async fn http_dispatcher_rejects_legacy_200_route() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let server = tokio::spawn(async move {
            axum::serve(
                listener,
                Router::new().route(crate::WORKER_COMMAND_PUSH_PATH, post(legacy_success_route)),
            )
            .await
            .unwrap();
        });
        let envelope = crate::test_utils::test_simple_envelope("cmd-http", "sync");
        let error = HttpCommandDispatcher::new(
            reqwest::Client::new(),
            format!("http://{address}{}", crate::WORKER_COMMAND_PUSH_PATH),
            "secret".to_string(),
        )
        .dispatch(&envelope)
        .await
        .expect_err("only the runtime's 202 acceptance is delivery");

        assert_eq!(error.code, "TRANSPORT_DISPATCH_REJECTED");
        server.abort();
    }

    #[tokio::test]
    async fn http_dispatcher_classifies_connection_refusal_as_definite_rejection() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        drop(listener);
        let envelope = crate::test_utils::test_simple_envelope("cmd-http", "sync");

        let error = HttpCommandDispatcher::new(
            reqwest::Client::new(),
            format!("http://{address}{}", crate::WORKER_COMMAND_PUSH_PATH),
            "secret".to_string(),
        )
        .dispatch(&envelope)
        .await
        .expect_err("connection refusal happens before delivery");

        assert_eq!(error.code, "TRANSPORT_DISPATCH_REJECTED");
    }

    #[tokio::test]
    async fn http_dispatcher_classifies_request_builder_failure_as_definite_rejection() {
        let envelope = crate::test_utils::test_simple_envelope("cmd-http", "sync");

        let error = HttpCommandDispatcher::new(
            reqwest::Client::new(),
            "http://[invalid-address".to_string(),
            "secret".to_string(),
        )
        .dispatch(&envelope)
        .await
        .expect_err("invalid URL fails before delivery");

        assert_eq!(error.code, "TRANSPORT_DISPATCH_REJECTED");
    }

    #[tokio::test]
    async fn http_dispatcher_bounds_ambiguous_acknowledgement_wait() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let server = tokio::spawn(async move {
            let (_socket, _) = listener.accept().await.unwrap();
            std::future::pending::<()>().await;
        });
        let envelope = crate::test_utils::test_simple_envelope("cmd-http", "sync");

        let error = HttpCommandDispatcher::new(
            reqwest::Client::new(),
            format!("http://{address}{}", crate::WORKER_COMMAND_PUSH_PATH),
            "secret".to_string(),
        )
        .with_request_timeout(Duration::from_millis(50))
        .dispatch(&envelope)
        .await
        .expect_err("blackholed acknowledgement must time out");

        assert_eq!(error.code, "TRANSPORT_DISPATCH_FAILED");
        server.abort();
    }

    #[test]
    fn http_dispatcher_debug_redacts_token() {
        let debug = format!(
            "{:?}",
            HttpCommandDispatcher::new(
                reqwest::Client::new(),
                "http://worker/_alien/commands".to_string(),
                "super-secret".to_string(),
            )
        );
        assert!(debug.contains("[REDACTED]"));
        assert!(!debug.contains("super-secret"));
    }

    #[tokio::test]
    async fn lambda_event_dispatch_accepts_exact_202() {
        let (endpoint, server) = spawn_static_response(StatusCode::ACCEPTED, "").await;
        let dispatcher = LambdaCommandDispatcher::new(
            reqwest::Client::new(),
            aws_config(endpoint),
            "test-function".to_string(),
        )
        .await
        .unwrap();

        dispatcher
            .dispatch(&sensitive_envelope("cmd-lambda-202"))
            .await
            .expect("Lambda Event invocation must accept exact HTTP 202");

        server.abort();
    }

    #[tokio::test]
    async fn lambda_event_dispatch_classifies_404_as_definite_rejection() {
        let (endpoint, server) = spawn_static_response(
            StatusCode::NOT_FOUND,
            r#"{"__type":"ResourceNotFoundException","message":"missing"}"#,
        )
        .await;
        let dispatcher = LambdaCommandDispatcher::new(
            reqwest::Client::new(),
            aws_config(endpoint),
            "missing-function".to_string(),
        )
        .await
        .unwrap();

        let error = dispatcher
            .dispatch(&sensitive_envelope("cmd-lambda-404"))
            .await
            .expect_err("an explicit Lambda 404 cannot have accepted the invocation");

        assert_eq!(error.code, "TRANSPORT_DISPATCH_REJECTED");
        server.abort();
    }

    #[tokio::test]
    async fn lambda_event_dispatch_keeps_500_ambiguous() {
        let (endpoint, server) = spawn_static_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            r#"{"__type":"ServiceException","message":"unknown outcome"}"#,
        )
        .await;
        let dispatcher = LambdaCommandDispatcher::new(
            reqwest::Client::new(),
            aws_config(endpoint),
            "test-function".to_string(),
        )
        .await
        .unwrap();

        let error = dispatcher
            .dispatch(&sensitive_envelope("cmd-lambda-500"))
            .await
            .expect_err("Lambda 5xx does not prove non-delivery");

        assert_eq!(error.code, "TRANSPORT_DISPATCH_FAILED");
        server.abort();
    }

    #[tokio::test]
    async fn service_bus_dispatch_accepts_success_status() {
        let (endpoint, server) = spawn_static_response(StatusCode::CREATED, "").await;
        let dispatcher = ServiceBusCommandDispatcher::new(
            reqwest::Client::new(),
            azure_config(endpoint),
            "test-namespace".to_string(),
            "test-queue".to_string(),
        );

        dispatcher
            .dispatch(&sensitive_envelope("cmd-servicebus-201"))
            .await
            .expect("Service Bus success status must acknowledge message acceptance");

        server.abort();
    }

    #[tokio::test]
    async fn service_bus_dispatch_rejects_404_and_redacts_command_body() {
        let (endpoint, server) = spawn_static_response(
            StatusCode::NOT_FOUND,
            "COMMAND_ERROR_BODY_MUST_BE_REDACTED https://storage.example.test/result?signature=MUST_NOT_ESCAPE",
        )
        .await;
        let dispatcher = ServiceBusCommandDispatcher::new(
            reqwest::Client::new(),
            azure_config(endpoint),
            "test-namespace".to_string(),
            "missing-queue".to_string(),
        );

        let error = dispatcher
            .dispatch(&sensitive_envelope("cmd-servicebus-404"))
            .await
            .expect_err("an explicit Service Bus 404 cannot have accepted the message");
        let serialized = serde_json::to_string(&error).unwrap();

        assert_eq!(error.code, "TRANSPORT_DISPATCH_REJECTED");
        assert!(serialized.contains(r#""http_status":404"#));
        assert!(!serialized.contains(COMMAND_BODY_SENTINEL));
        assert!(!serialized.contains(SIGNED_URL_SENTINEL));
        assert!(!serialized.contains("http_request_text"));
        server.abort();
    }

    #[tokio::test]
    async fn service_bus_dispatch_keeps_500_ambiguous_and_redacts_command_body() {
        let (endpoint, server) = spawn_static_response(
            StatusCode::INTERNAL_SERVER_ERROR,
            "COMMAND_ERROR_BODY_MUST_BE_REDACTED https://storage.example.test/result?signature=MUST_NOT_ESCAPE",
        )
        .await;
        let dispatcher = ServiceBusCommandDispatcher::new(
            reqwest::Client::new(),
            azure_config(endpoint),
            "test-namespace".to_string(),
            "test-queue".to_string(),
        );

        let error = dispatcher
            .dispatch(&sensitive_envelope("cmd-servicebus-500"))
            .await
            .expect_err("Service Bus 5xx does not prove non-delivery");
        let serialized = serde_json::to_string(&error).unwrap();

        assert_eq!(error.code, "TRANSPORT_DISPATCH_FAILED");
        assert!(serialized.contains(r#""http_status":500"#));
        assert!(!serialized.contains(COMMAND_BODY_SENTINEL));
        assert!(!serialized.contains(SIGNED_URL_SENTINEL));
        assert!(!serialized.contains("http_request_text"));
        server.abort();
    }

    #[tokio::test]
    async fn service_bus_dispatch_keeps_request_timeout_ambiguous() {
        let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
        let address = listener.local_addr().unwrap();
        let server = tokio::spawn(async move {
            let (_socket, _) = listener.accept().await.unwrap();
            std::future::pending::<()>().await;
        });
        let client = reqwest::Client::builder()
            .timeout(Duration::from_millis(50))
            .build()
            .unwrap();
        let dispatcher = ServiceBusCommandDispatcher::new(
            client,
            azure_config(format!("http://{address}")),
            "test-namespace".to_string(),
            "test-queue".to_string(),
        );

        let error = dispatcher
            .dispatch(&sensitive_envelope("cmd-servicebus-timeout"))
            .await
            .expect_err("lost Service Bus acknowledgement is ambiguous");

        assert_eq!(error.code, "TRANSPORT_DISPATCH_FAILED");
        server.abort();
    }

    #[tokio::test]
    async fn pubsub_403_remains_ambiguous_and_redacts_command_body() {
        let (endpoint, server) = spawn_static_response(
            StatusCode::FORBIDDEN,
            r#"{"error":{"code":403,"message":"COMMAND_ERROR_BODY_MUST_BE_REDACTED https://storage.example.test/result?signature=MUST_NOT_ESCAPE","status":"PERMISSION_DENIED"}}"#,
        )
        .await;
        let dispatcher = PubSubCommandDispatcher::new(
            reqwest::Client::new(),
            gcp_config(format!("{endpoint}/v1")),
            "test-topic".to_string(),
        );
        let envelope = sensitive_envelope("cmd-pubsub-403");
        let encoded_envelope = BASE64_STANDARD.encode(serde_json::to_vec(&envelope).unwrap());

        let error = dispatcher
            .dispatch(&envelope)
            .await
            .expect_err("Pub/Sub attempt history is insufficient for a definite rejection");
        let serialized = serde_json::to_string(&error).unwrap();

        assert_eq!(error.code, "TRANSPORT_DISPATCH_FAILED");
        assert!(serialized.contains(r#""http_status":403"#));
        assert!(!serialized.contains(COMMAND_BODY_SENTINEL));
        assert!(!serialized.contains(SIGNED_URL_SENTINEL));
        assert!(!serialized.contains(&encoded_envelope));
        assert!(!serialized.contains("http_request_text"));
        server.abort();
    }
}