alkhttp 0.5.0

HTTP interface for the alk stack: serves HTTP/1.1 + HTTP/2 on standard ALPNs (with WebSocket upgrade carrying the channels protocol) and hosts the HTTP-backed call-protocol adapters
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
//! `from_jsonschema` adapter: register a single HTTP-backed operation from a
//! caller-supplied [`OperationSpec`], path template, and HTTP method
//! ([ADR-066]).
//!
//! One `HandlerRegistration` per call with a reqwest forwarding handler
//! (the shared [`super::forward`] code path with `from_openapi`).
//! Provenance is `FromJsonSchema` (leaf, `composition_authority: None`,
//! `scoped_env: None`). The registered operation's visibility is forced
//! to `Internal` (ADR-015: adapter-registered ops are composition
//! material) — the caller's spec is not passed through verbatim.
//! Construction validates the method, path template, and base URL
//! eagerly (review 001 OAI-09), and the spec's declared path-template
//! placeholders must be bound by the input schema. `Sub` op type →
//! `HandlerKind::Stream` expecting `text/event-stream` (ADR-049).
//!
//! The spec's `input_schema` is the forwarding allow-list (review 001
//! OAI-02, enforced in `super::forward`); see [`FromJsonSchema::new`]
//! for the `wire: "header"` declaration (OAI-03).
//!
//! [ADR-066]: https://docs.rs/alkhttp (docs/architecture/decisions)

use std::sync::Arc;

use alkcall::client::{AdapterError, OperationAdapter};
use alkcall::core::types::Capabilities;
use alkcall::registry::context::OperationContext;
use alkcall::registry::registration::{
    make_handler, make_streaming_handler, HandlerKind, HandlerRegistration, OperationProvenance,
};
use alkcall::registry::spec::{OperationSpec, OperationType, Visibility};

use crate::adapters::input_validation::CompiledInputSchema;
use async_trait::async_trait;
use reqwest::Method;
use serde_json::Value;

use super::forward::{forward, forward_stream, validate_path_template, HttpServiceConfig};
use crate::client::SharedHttpClient;

/// The HTTP-backed single-endpoint adapter (ADR-066): one caller-built
/// [`OperationSpec`] forwarded to one HTTP endpoint. Eagerly validated
/// at construction; the caller's spec visibility is forced to
/// `Internal` (ADR-015).
pub struct FromJsonSchema {
    spec: OperationSpec,
    config: HttpServiceConfig,
    path_template: String,
    method: String,
    http_client: Arc<SharedHttpClient>,
}

impl FromJsonSchema {
    /// Register a caller-supplied [`OperationSpec`] backed by a single
    /// HTTP endpoint.
    ///
    /// Construction validates eagerly (review 001 OAI-09): the HTTP
    /// method must parse, the path template must be well-formed (balanced
    /// `{}` placeholders, no empty name), and the base URL must parse as
    /// an HTTP(S) origin without userinfo — malformed values fail here
    /// instead of surfacing as a per-call `INTERNAL` error on first
    /// invoke. The registered operation's visibility is forced to
    /// `Internal` (ADR-015: adapter-registered ops are composition
    /// material; the caller's spec is not passed through verbatim).
    ///
    /// The spec's `input_schema` is also the forwarding allow-list
    /// (review 001 OAI-02): input keys not declared in its `properties`
    /// are rejected at call time. Mark a property with
    /// `"wire": "header"` to route it as an upstream HTTPS header
    /// instead of a query parameter (review 001 OAI-03). Input key
    /// `GATEWAY_BODY_KEY` ("body") carries
    /// the request body.
    pub fn new(
        spec: OperationSpec,
        config: HttpServiceConfig,
        path_template: String,
        method: String,
        http_client: Arc<SharedHttpClient>,
    ) -> Result<Self, AdapterError> {
        validate_method(&method)?;
        validate_path_template(&path_template)?;
        Self::validate_base_url(&config.base_url)?;
        if spec_name_references_undeclared(&spec, &path_template) {
            return Err(AdapterError::SchemaParse {
                message: format!(
                    "path template `{path_template}` references a placeholder the \
                     operation's input_schema does not declare; the forwarded call \
                     could never satisfy it"
                ),
            });
        }
        if config.base_url.is_empty() {
            return Err(AdapterError::SchemaParse {
                message: "base_url must not be empty".into(),
            });
        }
        let spec = OperationSpec {
            visibility: Visibility::Internal,
            ..spec
        };
        Ok(Self {
            spec,
            config,
            path_template,
            method,
            http_client,
        })
    }

    fn validate_base_url(base_url: &str) -> Result<(), AdapterError> {
        let parsed = url::Url::parse(base_url).map_err(|e| AdapterError::SchemaParse {
            message: format!("invalid base_url `{base_url}`: {e}"),
        })?;
        let scheme = parsed.scheme();
        if scheme != "https" && scheme != "http" {
            return Err(AdapterError::SchemaParse {
                message: format!("base_url `{base_url}` must be an http(s) URL; `{scheme}` is not"),
            });
        }
        if !parsed.username().is_empty() || parsed.password().is_some() {
            return Err(AdapterError::SchemaParse {
                message: format!(
                    "base_url `{base_url}` must not embed userinfo; credentials are \
                     injected per-call from Capabilities"
                ),
            });
        }
        Ok(())
    }
}

fn validate_method(method: &str) -> Result<(), AdapterError> {
    if method.is_empty() {
        return Err(AdapterError::SchemaParse {
            message: "HTTP method must not be empty".into(),
        });
    }
    Method::from_bytes(method.to_ascii_uppercase().as_bytes()).map_err(|_| {
        AdapterError::SchemaParse {
            message: format!("invalid HTTP method `{method}`"),
        }
    })?;
    Ok(())
}
fn spec_name_references_undeclared(spec: &OperationSpec, path_template: &str) -> bool {
    let properties = spec
        .input_schema
        .get("properties")
        .and_then(|p| p.as_object())
        .map(|p| p.keys().cloned().collect::<Vec<_>>())
        .unwrap_or_default();
    let mut rest = path_template;
    while let Some(start) = rest.find('{') {
        let Some(end_rel) = rest[start..].find('}') else {
            return false;
        };
        let name = &rest[start + 1..start + end_rel];
        if !properties.iter().any(|k| k == name) {
            return true;
        }
        rest = &rest[start + end_rel + 1..];
    }
    false
}

#[async_trait]
impl OperationAdapter for FromJsonSchema {
    async fn import(&self) -> Result<Vec<HandlerRegistration>, AdapterError> {
        let input_validator =
            CompiledInputSchema::compile(&self.spec.input_schema).map_err(|message| {
                AdapterError::SchemaParse {
                    message: format!(
                        "operation `{}`: {message} (review 002 OAI-18: an un-validatable input \
                     contract must fail import, not register unenforced)",
                        self.spec.name
                    ),
                }
            })?;
        let path_template = self.path_template.clone();
        let method_upper = self.method.to_ascii_uppercase();
        let auth_scheme = self.config.auth.clone();
        let default_headers = self.config.default_headers.clone();
        let base_url = self.config.base_url.clone();
        let namespace = self.config.namespace.clone();
        let http_client = Arc::clone(&self.http_client);
        let op_type = self.spec.op_type;
        let input_schema = self.spec.input_schema.clone();

        let error_status_codes: Vec<(u16, String)> = self
            .spec
            .error_schemas
            .iter()
            .map(|e| (e.http_status.unwrap_or(0), e.code.clone()))
            .collect();

        let handler = if op_type == OperationType::Sub {
            let stream_handler =
                make_streaming_handler(move |input: Value, context: OperationContext| {
                    let path_template = path_template.clone();
                    let method_upper = method_upper.clone();
                    let auth_scheme = auth_scheme.clone();
                    let default_headers = default_headers.clone();
                    let base_url = base_url.clone();
                    let namespace = namespace.clone();
                    let http_client = Arc::clone(&http_client);
                    let error_status_codes = error_status_codes.clone();
                    let input_schema = input_schema.clone();
                    let input_validator = input_validator.clone();
                    forward_stream(
                        &http_client,
                        &base_url,
                        &path_template,
                        &method_upper,
                        &auth_scheme,
                        &default_headers,
                        &namespace,
                        &input_schema,
                        &error_status_codes,
                        Some(&input_validator),
                        input,
                        context,
                    )
                });
            HandlerKind::Stream(stream_handler)
        } else {
            let once_handler = make_handler(move |input: Value, context: OperationContext| {
                let path_template = path_template.clone();
                let method_upper = method_upper.clone();
                let auth_scheme = auth_scheme.clone();
                let default_headers = default_headers.clone();
                let base_url = base_url.clone();
                let namespace = namespace.clone();
                let http_client = Arc::clone(&http_client);
                let error_status_codes = error_status_codes.clone();
                let input_schema = input_schema.clone();
                let input_validator = input_validator.clone();
                async move {
                    forward(
                        &http_client,
                        &base_url,
                        &path_template,
                        &method_upper,
                        &auth_scheme,
                        &default_headers,
                        &namespace,
                        &input_schema,
                        &error_status_codes,
                        Some(&input_validator),
                        input,
                        context,
                    )
                    .await
                }
            });
            HandlerKind::Once(once_handler)
        };

        let capabilities = Capabilities::new();
        Ok(vec![HandlerRegistration::new(
            self.spec.clone(),
            handler,
            OperationProvenance::FromJsonSchema,
            None,
            None,
            capabilities,
        )])
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::adapters::forward::{build_request, HttpAuthScheme};
    use crate::client::HttpClientConfig;
    use alkcall::protocol::wire::ResponseEnvelope;
    use alkcall::registry::context::AbortPolicy;
    use alkcall::registry::env::OperationEnv;
    use alkcall::registry::spec::{AccessControl, ErrorDefinition, Visibility};
    use futures::StreamExt;
    use reqwest::header::AUTHORIZATION;
    use reqwest::Method;
    use std::collections::HashMap;
    use std::time::Duration;
    use tokio::io::{AsyncReadExt, AsyncWriteExt};
    use tokio::net::TcpListener;

    fn noop_context(request_id: &str, capabilities: Capabilities) -> OperationContext {
        struct NoopEnv;
        #[async_trait]
        impl OperationEnv for NoopEnv {
            async fn invoke_with_policy(
                &self,
                _ns: &str,
                _op: &str,
                _input: Value,
                parent: &OperationContext,
                _policy: AbortPolicy,
            ) -> ResponseEnvelope {
                ResponseEnvelope::ok(parent.request_id.clone(), Value::Null)
            }
            fn contains(&self, _name: &str) -> bool {
                false
            }
        }
        OperationContext {
            request_id: request_id.to_string(),
            parent_request_id: None,
            identity: None,
            handler_identity: None,
            forwarded_for: None,
            capabilities,
            metadata: HashMap::new(),
            scoped_env: alkcall::registry::context::ScopedPeerEnv::empty(),
            env: Arc::new(NoopEnv),
            abort_policy: AbortPolicy::default(),
            deadline: Some(std::time::Instant::now() + Duration::from_secs(30)),
            internal: true,
            ownership: None,
        }
    }

    fn test_spec(name: &str, op_type: OperationType) -> OperationSpec {
        OperationSpec::new(
            name,
            op_type,
            Visibility::Internal,
            serde_json::json!({"type":"object","properties":{"id":{"type":"string"}}}),
            serde_json::json!({"type":"object"}),
            vec![],
            AccessControl::default(),
            None,
        )
    }

    fn test_spec_with_errors(
        name: &str,
        op_type: OperationType,
        errors: Vec<ErrorDefinition>,
    ) -> OperationSpec {
        OperationSpec::new(
            name,
            op_type,
            Visibility::Internal,
            serde_json::json!({"type":"object"}),
            serde_json::json!({"type":"object"}),
            errors,
            AccessControl::default(),
            None,
        )
    }

    fn test_config(namespace: &str, base_url: &str) -> HttpServiceConfig {
        HttpServiceConfig {
            namespace: namespace.to_string(),
            base_url: base_url.to_string(),
            auth: None,
            default_headers: HashMap::new(),
        }
    }

    fn test_http_client() -> Arc<SharedHttpClient> {
        Arc::new(SharedHttpClient::new(HttpClientConfig::default()).unwrap())
    }

    #[tokio::test]
    async fn import_produces_one_handler_registration() {
        let adapter = FromJsonSchema::new(
            test_spec("svc/getWidget", OperationType::Query),
            test_config("svc", "https://api.example.com"),
            "/widgets".to_string(),
            "GET".to_string(),
            test_http_client(),
        )
        .unwrap();
        let bundles = adapter.import().await.unwrap();
        assert_eq!(bundles.len(), 1);
        assert_eq!(bundles[0].spec.name, "svc/getWidget");
        assert_eq!(bundles[0].provenance, OperationProvenance::FromJsonSchema);
        assert!(bundles[0].composition_authority.is_none());
        assert!(bundles[0].scoped_env.is_none());
    }

    #[tokio::test]
    async fn query_op_registration_is_handler_kind_once() {
        let adapter = FromJsonSchema::new(
            test_spec("svc/getWidget", OperationType::Query),
            test_config("svc", "https://api.example.com"),
            "/widgets".to_string(),
            "GET".to_string(),
            test_http_client(),
        )
        .unwrap();
        let bundles = adapter.import().await.unwrap();
        assert!(matches!(bundles[0].handler, HandlerKind::Once(_)));
    }

    #[tokio::test]
    async fn sub_op_registration_is_handler_kind_stream() {
        let adapter = FromJsonSchema::new(
            test_spec("svc/stream", OperationType::Sub),
            test_config("svc", "https://api.example.com"),
            "/stream".to_string(),
            "POST".to_string(),
            test_http_client(),
        )
        .unwrap();
        let bundles = adapter.import().await.unwrap();
        assert!(matches!(bundles[0].handler, HandlerKind::Stream(_)));
    }

    #[tokio::test]
    async fn mutation_op_registration_is_handler_kind_once() {
        let adapter = FromJsonSchema::new(
            test_spec("svc/createWidget", OperationType::Mutation),
            test_config("svc", "https://api.example.com"),
            "/widgets".to_string(),
            "POST".to_string(),
            test_http_client(),
        )
        .unwrap();
        let bundles = adapter.import().await.unwrap();
        assert!(matches!(bundles[0].handler, HandlerKind::Once(_)));
    }

    #[tokio::test]
    async fn build_request_injects_bearer_from_capabilities() {
        let caps = Capabilities::new().with_http_token("github", "tok-123".to_string());
        let ctx = noop_context("req-1", caps);
        let (method, url, _body, headers) = build_request(
            "https://api.github.com",
            "/repos/{owner}/{repo}/issues",
            "GET",
            &Some(HttpAuthScheme::Bearer),
            &HashMap::new(),
            "github",
            &serde_json::json!({"type": "object", "additionalProperties": true}),
            None,
            &serde_json::json!({"owner":"a","repo":"b"}),
            &ctx,
        )
        .unwrap();
        assert_eq!(method, Method::GET);
        assert_eq!(url.path(), "/repos/a/b/issues");
        assert_eq!(url.host_str(), Some("api.github.com"));
        let auth = headers.get(AUTHORIZATION).unwrap();
        assert_eq!(auth.to_str().unwrap(), "Bearer tok-123");
    }

    #[tokio::test]
    async fn build_request_path_and_query_split() {
        let ctx = noop_context("req-2", Capabilities::new());
        let (_, url, _, _) = build_request(
            "https://api.example.com",
            "/widgets/{id}",
            "GET",
            &None,
            &HashMap::new(),
            "svc",
            &serde_json::json!({"type": "object", "additionalProperties": true}),
            None,
            &serde_json::json!({"id":42,"filter":"active"}),
            &ctx,
        )
        .unwrap();
        assert_eq!(url.path(), "/widgets/42");
        assert_eq!(url.query().unwrap(), "filter=active");
    }

    async fn spawn_echo_server(
        status: u16,
        body: &'static str,
        content_type: &'static str,
    ) -> String {
        let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
        let addr = listener.local_addr().unwrap();
        tokio::spawn(async move {
            loop {
                let (mut sock, _) = match listener.accept().await {
                    Ok(pair) => pair,
                    Err(_) => break,
                };
                let status_line = match status {
                    200 => "200 OK",
                    201 => "201 Created",
                    404 => "404 Not Found",
                    500 => "500 Internal Server Error",
                    _ => "200 OK",
                };
                let body_bytes = body.as_bytes();
                let response = format!(
                    "HTTP/1.1 {status_line}\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{}",
                    body_bytes.len(),
                    body
                );
                let mut buf = [0u8; 4096];
                let _ = sock.read(&mut buf).await;
                sock.write_all(response.as_bytes()).await.unwrap();
                sock.flush().await.unwrap();
            }
        });
        format!("http://{addr}")
    }

    #[tokio::test]
    async fn integration_forwarding_handler_calls_external_endpoint() {
        let base = spawn_echo_server(200, r#"{"ok":true}"#, "application/json").await;
        let adapter = FromJsonSchema::new(
            test_spec("svc/data", OperationType::Query),
            test_config("svc", &base),
            "/data".to_string(),
            "GET".to_string(),
            test_http_client(),
        )
        .unwrap();
        let bundles = adapter.import().await.unwrap();
        let registration = &bundles[0];
        let ctx = noop_context("req-10", Capabilities::new());
        let response = match &registration.handler {
            HandlerKind::Once(h) => h(serde_json::json!({}), ctx).await,
            _ => panic!("expected Once handler"),
        };
        assert_eq!(response.request_id, "req-10");
        match response.result {
            Ok(v) => assert_eq!(v, serde_json::json!({"ok":true})),
            Err(e) => panic!("expected Ok, got {e:?}"),
        }
    }

    #[tokio::test]
    async fn integration_non_2xx_returns_declared_error() {
        let base = spawn_echo_server(404, r#"{"error":"missing"}"#, "application/json").await;
        let errors = vec![ErrorDefinition {
            code: "HTTP_404".to_string(),
            description: "Not found".to_string(),
            schema: serde_json::json!({"type":"object"}),
            http_status: Some(404),
        }];
        let adapter = FromJsonSchema::new(
            test_spec_with_errors("svc/missing", OperationType::Query, errors),
            test_config("svc", &base),
            "/missing".to_string(),
            "GET".to_string(),
            test_http_client(),
        )
        .unwrap();
        let bundles = adapter.import().await.unwrap();
        let registration = &bundles[0];
        let ctx = noop_context("req-11", Capabilities::new());
        let response = match &registration.handler {
            HandlerKind::Once(h) => h(serde_json::json!({}), ctx).await,
            _ => panic!("expected Once handler"),
        };
        match response.result {
            Err(e) => {
                assert_eq!(e.code, "HTTP_404");
                assert!(!e.retryable);
            }
            other => panic!("expected HTTP_404 error, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn integration_undeclared_error_status_returns_http_status_code() {
        let base = spawn_echo_server(500, "boom", "text/plain").await;
        let adapter = FromJsonSchema::new(
            test_spec("svc/x", OperationType::Query),
            test_config("svc", &base),
            "/x".to_string(),
            "GET".to_string(),
            test_http_client(),
        )
        .unwrap();
        let bundles = adapter.import().await.unwrap();
        let registration = &bundles[0];
        let ctx = noop_context("req-12", Capabilities::new());
        let response = match &registration.handler {
            HandlerKind::Once(h) => h(serde_json::json!({}), ctx).await,
            _ => panic!("expected Once handler"),
        };
        match response.result {
            Err(e) => assert_eq!(e.code, "HTTP_500"),
            other => panic!("expected HTTP_500, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn integration_sse_subscription_streams_responded_events() {
        let sse_body = "data: {\"n\":1}\n\ndata: {\"n\":2}\n\n";
        let base = spawn_echo_server(200, sse_body, "text/event-stream").await;
        let adapter = FromJsonSchema::new(
            test_spec("svc/stream", OperationType::Sub),
            test_config("svc", &base),
            "/stream".to_string(),
            "POST".to_string(),
            test_http_client(),
        )
        .unwrap();
        let bundles = adapter.import().await.unwrap();
        let registration = &bundles[0];
        let ctx = noop_context("req-13", Capabilities::new());
        let stream = match &registration.handler {
            HandlerKind::Stream(h) => h(serde_json::json!({}), ctx),
            _ => panic!("expected Stream handler"),
        };
        let collected: Vec<ResponseEnvelope> = stream.collect().await;
        assert_eq!(collected.len(), 2);
        assert_eq!(collected[0].result, Ok(serde_json::json!({"n":1})));
        assert_eq!(collected[1].result, Ok(serde_json::json!({"n":2})));
        assert_eq!(collected[0].request_id, "req-13");
        assert_eq!(collected[1].request_id, "req-13");
    }

    /// The streaming analog of the Once-path build-error envelope: a
    /// Sub op invoked with an invalid input (undeclared key, or a
    /// non-scalar value bound to a path placeholder) yields exactly one
    /// `INVALID_INPUT` envelope, then the stream ends — pre-send, so
    /// the upstream receives zero requests. An empty stream (a silently
    /// swallowed error) would be the worst failure shape for a
    /// subscriptions consumer; the non-empty, single-error shape is the
    /// assertion.
    #[tokio::test]
    async fn integration_sse_subscription_invalid_input_yields_one_error_envelope_and_ends() {
        for (input, expected_fragment) in [
            (
                serde_json::json!({"debug": true}),
                "not declared by the operation's input schema",
            ),
            (
                serde_json::json!({"id": {"nested": "object"}}),
                "[keyword: type",
            ),
        ] {
            let base = spawn_echo_server(200, "data: {\"n\":1}\n\n", "text/event-stream").await;
            let adapter = FromJsonSchema::new(
                test_spec("svc/stream", OperationType::Sub),
                test_config("svc", &base),
                "/stream/{id}".to_string(),
                "POST".to_string(),
                test_http_client(),
            )
            .unwrap();
            let bundles = adapter.import().await.unwrap();
            let registration = &bundles[0];
            let ctx = noop_context("req-13b", Capabilities::new());
            let stream = match &registration.handler {
                HandlerKind::Stream(h) => h(input.clone(), ctx),
                _ => panic!("expected Stream handler"),
            };
            let collected: Vec<ResponseEnvelope> = stream.collect().await;
            assert_eq!(
                collected.len(),
                1,
                "exactly one error envelope, then the stream ends (never an empty-200-subscribe)"
            );
            match &collected[0].result {
                Err(e) => {
                    assert_eq!(e.code, "INVALID_INPUT", "message was: {}", e.message);
                    assert!(
                        e.message.contains(expected_fragment),
                        "message `{}` lacks `{expected_fragment}`",
                        e.message
                    );
                }
                other => panic!("expected INVALID_INPUT, got {other:?}"),
            }
            assert_eq!(collected[0].request_id, "req-13b");
        }
    }

    #[test]
    fn no_env_vars_read_in_build_request() {
        std::env::set_var("OPENAI_API_KEY", "should-not-be-used");
        let ctx = noop_context("req-14", Capabilities::new());
        let err = build_request(
            "https://api.openai.com",
            "/v1/chat",
            "POST",
            &Some(HttpAuthScheme::Bearer),
            &HashMap::new(),
            "openai",
            &serde_json::json!({"type": "object", "additionalProperties": true}),
            None,
            &serde_json::json!({"body":{"prompt":"hi"}}),
            &ctx,
        )
        .expect_err("absent capability is a loud error (FWD-16), never an unauthenticated send");
        assert_eq!(err.code, "INTERNAL");
        assert!(
            err.message
                .contains("capability for namespace `openai` is absent"),
            "message was: {}",
            err.message
        );
        assert!(
            !err.message.contains("should-not-be-used"),
            "error must not echo env material"
        );
        std::env::remove_var("OPENAI_API_KEY");
    }

    /// OAI-18 fail-closed precedent: an input schema that cannot
    /// compile fails *import* loudly — never a registration whose
    /// call-time enforcement silently degrades.
    #[tokio::test]
    async fn uncompilable_input_schema_fails_import_loudly() {
        let mut spec = test_spec("svc/broken", OperationType::Query);
        spec.input_schema = serde_json::json!({"type": "object", "required": "n"});
        let adapter = FromJsonSchema::new(
            spec,
            test_config("svc", "https://api.example.com"),
            "/x".to_string(),
            "GET".to_string(),
            test_http_client(),
        )
        .unwrap();
        let result = adapter.import().await;
        match result {
            Err(AdapterError::SchemaParse { message }) => {
                assert!(
                    message.contains("failed to compile"),
                    "message was: {message}"
                );
                assert!(message.contains("svc/broken"), "message was: {message}");
            }
            Ok(_) => panic!("an un-validatable input contract must fail import (OAI-18)"),
            Err(e) => panic!("expected SchemaParse, got {e:?}"),
        }
    }

    #[test]
    fn malformed_method_path_template_and_base_url_fail_at_construction() {
        for (method, path_template, base_url, expected_fragment) in [
            ("NOT/*A METHOD", "/x", "https://x", "invalid HTTP method"),
            ("GET", "/x/{open", "https://x", "unterminated placeholder"),
            ("GET", "/x/}", "https://x", "without a matching"),
            ("GET", "/x", "https://u:p@x", "userinfo"),
            ("GET", "/x", "ftp://x", "must be an http(s) URL"),
            ("GET", "/x", "not a url", "invalid base_url"),
        ] {
            let result = FromJsonSchema::new(
                test_spec("svc/x", OperationType::Query),
                test_config("svc", base_url),
                path_template.to_string(),
                method.to_string(),
                test_http_client(),
            );
            match result {
                Err(AdapterError::SchemaParse { message }) => {
                    assert!(
                        message.contains(expected_fragment),
                        "message `{message}` lacks `{expected_fragment}`"
                    );
                }
                Ok(_) => panic!(
                    "malformed adapter config ({method}, {path_template}, {base_url}) must fail at construction"
                ),
                Err(e) => panic!("expected SchemaParse, got {e}"),
            }
        }
    }

    #[test]
    fn construction_forces_visibility_internal_and_validates_placeholder_binding() {
        let spec = OperationSpec::new(
            "svc/getWidget",
            OperationType::Query,
            Visibility::External,
            serde_json::json!({"type":"object","properties":{"id":{"type":"string"}}}),
            serde_json::json!({"type":"object"}),
            vec![],
            AccessControl::default(),
            None,
        );
        let adapter = FromJsonSchema::new(
            spec,
            test_config("svc", "https://api.example.com"),
            "/widgets/{id}".to_string(),
            "GET".to_string(),
            test_http_client(),
        )
        .expect("adapter builds");
        let bundles = futures::executor::block_on(adapter.import()).unwrap();
        assert_eq!(bundles[0].spec.visibility, Visibility::Internal);

        let spec = OperationSpec::new(
            "svc/getWidget",
            OperationType::Query,
            Visibility::Internal,
            serde_json::json!({"type":"object","properties":{"widget_id":{"type":"string"}}}),
            serde_json::json!({"type":"object"}),
            vec![],
            AccessControl::default(),
            None,
        );
        let result = FromJsonSchema::new(
            spec,
            test_config("svc", "https://api.example.com"),
            "/widgets/{id}".to_string(),
            "GET".to_string(),
            test_http_client(),
        );
        match result {
            Err(AdapterError::SchemaParse { message }) => {
                assert!(message.contains("placeholder"), "message was: {message}");
                assert!(message.contains("{id}"), "message was: {message}");
            }
            Ok(_) => panic!("unbound placeholder must fail at construction"),
            Err(e) => panic!("expected SchemaParse, got {e}"),
        }
    }

    #[test]
    fn construction_rejects_placeholder_when_input_schema_has_no_properties() {
        let spec = OperationSpec::new(
            "svc/getWidget",
            OperationType::Query,
            Visibility::Internal,
            serde_json::json!({"type":"object"}),
            serde_json::json!({"type":"object"}),
            vec![],
            AccessControl::default(),
            None,
        );
        let result = FromJsonSchema::new(
            spec,
            test_config("svc", "https://api.example.com"),
            "/widgets/{id}".to_string(),
            "GET".to_string(),
            test_http_client(),
        );
        match result {
            Err(AdapterError::SchemaParse { message }) => {
                assert!(message.contains("placeholder"), "message was: {message}");
                assert!(message.contains("{id}"), "message was: {message}");
            }
            Ok(_) => {
                panic!("no-properties schema with a placeholder template must fail at construction")
            }
            Err(e) => panic!("expected SchemaParse, got {e}"),
        }

        let spec = OperationSpec::new(
            "svc/listWidgets",
            OperationType::Query,
            Visibility::Internal,
            serde_json::json!({"type":"object"}),
            serde_json::json!({"type":"object"}),
            vec![],
            AccessControl::default(),
            None,
        );
        let adapter = FromJsonSchema::new(
            spec,
            test_config("svc", "https://api.example.com"),
            "/widgets".to_string(),
            "GET".to_string(),
            test_http_client(),
        )
        .expect("placeholder-free template with a no-properties schema builds");
        let bundles = futures::executor::block_on(adapter.import()).unwrap();
        assert_eq!(bundles[0].spec.visibility, Visibility::Internal);
    }
}