greentic-ext-runtime 1.2.26

Wasmtime-based runtime for Greentic Designer Extensions
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
use std::sync::Arc;
use std::sync::atomic::{AtomicU32, Ordering};

use wasmtime::component::ResourceTable;
use wasmtime_wasi::{WasiCtx, WasiCtxBuilder, WasiCtxView, WasiView};

use crate::host_bindings::greentic::extension_host::{broker, http, i18n, llm, logging, secrets};
use crate::host_ports::{KeyTranslator, LlmPort, SecretsBackend, Translator};
use crate::url_matcher::UrlMatcher;
use greentic_extension_sdk_contract::describe::Permissions;

/// Maximum number of nested `host.broker.call-extension` hops the runtime
/// allows in a single dispatch chain. Mirrors `crate::broker::MAX_DEPTH`.
pub const MAX_BROKER_DEPTH: u32 = 8;

/// Per-Store host context. One `HostState` is built per WIT invocation —
/// the dependencies live in `Arc`s so cloning is cheap.
pub struct HostState {
    pub extension_id: String,
    pub permissions: Permissions,
    pub call_depth: AtomicU32,
    translator: Arc<dyn Translator>,
    secrets_backend: Arc<dyn SecretsBackend>,
    http_client: Option<reqwest::blocking::Client>,
    llm_port: Option<Arc<dyn LlmPort>>,
    /// Per-call caller context for this dispatch (tenant slug + authenticated
    /// user email), threaded from the host's
    /// [`crate::host_ports::HostCallContext`] and forwarded to host ports.
    /// `Default` (all `None`) when the host runs single-tenant/dev.
    call_ctx: crate::host_ports::HostCallContext,
    url_matcher: UrlMatcher,
    runtime_weak: std::sync::Weak<crate::runtime::ExtensionRuntime>,
    // consumed by the oauth broker Host impl below
    oauth_config: Option<crate::oauth::OAuthBrokerConfig>,
    // WASI state — required because cargo-component-built WASM components
    // implicitly import WASI interfaces (wasi:cli/environment etc.).
    wasi: WasiCtx,
    table: ResourceTable,
}

impl HostState {
    /// Builder used by `LoadedExtension::build_store_and_instance` to
    /// produce a `HostState` for a single dispatch.
    #[must_use]
    pub fn builder(extension_id: String, permissions: Permissions) -> HostStateBuilder {
        HostStateBuilder {
            extension_id,
            permissions,
            translator: Arc::new(KeyTranslator),
            secrets_backend: Arc::new(crate::host_ports::InMemorySecrets::new()),
            http_client: None,
            llm_port: None,
            call_ctx: crate::host_ports::HostCallContext::default(),
            url_matcher: UrlMatcher::default(),
            runtime_weak: std::sync::Weak::new(),
            call_depth_start: 0,
            oauth_config: None,
        }
    }

    #[must_use]
    pub fn translator(&self) -> &dyn Translator {
        self.translator.as_ref()
    }

    #[must_use]
    pub fn secrets_backend(&self) -> &dyn SecretsBackend {
        self.secrets_backend.as_ref()
    }

    #[must_use]
    pub fn url_matcher(&self) -> &UrlMatcher {
        &self.url_matcher
    }
}

/// Builder for [`HostState`]. Avoids a 7-positional-arg constructor.
pub struct HostStateBuilder {
    extension_id: String,
    permissions: Permissions,
    translator: Arc<dyn Translator>,
    secrets_backend: Arc<dyn SecretsBackend>,
    http_client: Option<reqwest::blocking::Client>,
    llm_port: Option<Arc<dyn LlmPort>>,
    call_ctx: crate::host_ports::HostCallContext,
    url_matcher: UrlMatcher,
    runtime_weak: std::sync::Weak<crate::runtime::ExtensionRuntime>,
    call_depth_start: u32,
    oauth_config: Option<crate::oauth::OAuthBrokerConfig>,
}

impl HostStateBuilder {
    #[must_use]
    pub fn translator(mut self, t: Arc<dyn Translator>) -> Self {
        self.translator = t;
        self
    }
    #[must_use]
    pub fn secrets_backend(mut self, s: Arc<dyn SecretsBackend>) -> Self {
        self.secrets_backend = s;
        self
    }
    #[must_use]
    pub fn http_client(mut self, c: Option<reqwest::blocking::Client>) -> Self {
        self.http_client = c;
        self
    }
    #[must_use]
    pub fn llm_port(mut self, p: Option<Arc<dyn LlmPort>>) -> Self {
        self.llm_port = p;
        self
    }
    /// Set the per-call caller context (tenant slug + authenticated user
    /// email) for this dispatch. Threaded into host ports (today the LLM port)
    /// so the host can resolve per-tenant and validate per-user identity.
    #[must_use]
    pub fn call_ctx(mut self, ctx: crate::host_ports::HostCallContext) -> Self {
        self.call_ctx = ctx;
        self
    }
    #[must_use]
    pub fn url_matcher(mut self, m: UrlMatcher) -> Self {
        self.url_matcher = m;
        self
    }
    #[must_use]
    pub fn runtime_weak(mut self, w: std::sync::Weak<crate::runtime::ExtensionRuntime>) -> Self {
        self.runtime_weak = w;
        self
    }
    #[must_use]
    pub fn call_depth_start(mut self, n: u32) -> Self {
        self.call_depth_start = n;
        self
    }
    #[must_use]
    pub fn oauth_config(mut self, c: Option<crate::oauth::OAuthBrokerConfig>) -> Self {
        self.oauth_config = c;
        self
    }

    #[must_use]
    pub fn build(self) -> HostState {
        let wasi = WasiCtxBuilder::new().build();
        let table = ResourceTable::new();
        HostState {
            extension_id: self.extension_id,
            permissions: self.permissions,
            call_depth: AtomicU32::new(self.call_depth_start),
            translator: self.translator,
            secrets_backend: self.secrets_backend,
            http_client: self.http_client,
            llm_port: self.llm_port,
            call_ctx: self.call_ctx,
            url_matcher: self.url_matcher,
            runtime_weak: self.runtime_weak,
            oauth_config: self.oauth_config,
            wasi,
            table,
        }
    }
}

/// Implement [`WasiView`] so that `wasmtime_wasi::p2::add_to_linker_sync` can wire
/// WASI host functions. cargo-component adds WASI imports to every component it
/// builds, even if the Rust source never calls them.
impl WasiView for HostState {
    fn ctx(&mut self) -> WasiCtxView<'_> {
        WasiCtxView {
            ctx: &mut self.wasi,
            table: &mut self.table,
        }
    }
}

impl logging::Host for HostState {
    fn log(&mut self, level: logging::Level, target: String, message: String) {
        let ext = &self.extension_id;
        match level {
            logging::Level::Trace => tracing::trace!(%ext, %target, "{message}"),
            logging::Level::Debug => tracing::debug!(%ext, %target, "{message}"),
            logging::Level::Info => tracing::info!(%ext, %target, "{message}"),
            logging::Level::Warn => tracing::warn!(%ext, %target, "{message}"),
            logging::Level::Error => tracing::error!(%ext, %target, "{message}"),
        }
    }

    fn log_kv(
        &mut self,
        level: logging::Level,
        target: String,
        message: String,
        fields: Vec<(String, String)>,
    ) {
        let pairs: Vec<String> = fields.iter().map(|(k, v)| format!("{k}={v}")).collect();
        let msg = if pairs.is_empty() {
            message
        } else {
            format!("{message} {{{}}}", pairs.join(", "))
        };
        self.log(level, target, msg);
    }
}

// i18n / secrets / broker / http impls are wired in tasks B.2, B.3,
// B.4, B.5. The B.7 grep gate enforces that no impl falls back to the
// historical stub sentinel string.

impl i18n::Host for HostState {
    fn t(&mut self, key: String) -> String {
        self.translator.t(&key)
    }
    fn tf(&mut self, key: String, args: Vec<(String, String)>) -> String {
        let borrowed: Vec<(&str, &str)> =
            args.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect();
        self.translator.tf(&key, &borrowed)
    }
}

impl secrets::Host for HostState {
    fn get(&mut self, uri: String) -> Result<String, String> {
        // Permission check: every secret URI the extension reads must be
        // declared verbatim or as a prefix in `permissions.secrets`. Strict
        // prefix match (no glob today — that's a follow-up if needed).
        let permitted = self
            .permissions
            .secrets
            .iter()
            .any(|allowed| uri == *allowed || uri.starts_with(&format!("{allowed}/")));
        if !permitted {
            tracing::warn!(
                ext = %self.extension_id,
                requested = %uri,
                "secrets::get permission denied"
            );
            return Err(format!("permission denied for secret: {uri}"));
        }
        match self.secrets_backend.get(&uri) {
            Ok(value) => Ok(value),
            Err(crate::host_ports::SecretsError::NotFound(k)) => {
                Err(format!("secret not found: {k}"))
            }
            Err(crate::host_ports::SecretsError::Backend(msg)) => {
                tracing::error!(ext = %self.extension_id, %msg, "secrets backend error");
                Err(format!("secrets backend error: {msg}"))
            }
        }
    }
}

impl broker::Host for HostState {
    fn call_extension(
        &mut self,
        kind: String,
        target_id: String,
        function: String,
        _args_json: String,
    ) -> Result<String, String> {
        // 1. Permission check — declared kinds only.
        if !self
            .permissions
            .call_extension_kinds
            .iter()
            .any(|k| k == &kind)
        {
            tracing::warn!(
                ext = %self.extension_id,
                requested_kind = %kind,
                "broker::call_extension permission denied"
            );
            return Err(format!(
                "{} may not call {kind} extensions",
                self.extension_id
            ));
        }
        // 2. Depth check — prevent unbounded recursion across host boundaries.
        let depth = self.call_depth.load(Ordering::Relaxed);
        if depth >= MAX_BROKER_DEPTH {
            tracing::warn!(
                ext = %self.extension_id,
                depth,
                "broker::call_extension max depth exceeded"
            );
            return Err(format!(
                "max broker call depth exceeded ({depth} >= {MAX_BROKER_DEPTH})"
            ));
        }
        // 3. Resolve runtime — `runtime_weak` is `Weak::new()` in unit tests
        //    that use `HostState::builder(...).build()` without supplying a
        //    runtime. Surface a clear error so test code distinguishes
        //    "no runtime context" from "permission denied".
        let Some(_rt) = self.runtime_weak.upgrade() else {
            return Err("broker: no runtime context available".into());
        };
        // 4. Cross-extension dispatch — full implementation requires
        //    `ExtensionRuntime::invoke_tool_with_depth(self: &Arc<Self>,
        //    ...)` which cascades the runtime API to `&Arc<Self>` and
        //    updates every caller. Deferred to a follow-up alongside the
        //    WASM broker fixtures. Today's permission + depth check is
        //    the security-load-bearing portion of B.5.
        Err(format!(
            "broker: dispatch to {target_id}.{function} pending (B.5 WASM round-trip)"
        ))
    }
}

impl http::Host for HostState {
    fn fetch(&mut self, req: http::Request) -> Result<http::Response, String> {
        // 1. Permission check via strict UrlMatcher.
        if !self.url_matcher.is_allowed(&req.url) {
            tracing::warn!(
                ext = %self.extension_id,
                url = %req.url,
                "http::fetch permission denied"
            );
            return Err(format!("network not allowed for url: {}", req.url));
        }

        // 2. Build the reqwest request. `http_client` is `None` when the
        //    host wasn't given one (typical for unit tests). Surface a
        //    clean error rather than panic, and don't lazy-construct a
        //    client here — see `HostOverrides` doc comment.
        let client = self
            .http_client
            .as_ref()
            .ok_or_else(|| "http client not configured for this runtime".to_string())?;
        let method = match req.method.to_uppercase().as_str() {
            "GET" => reqwest::Method::GET,
            "POST" => reqwest::Method::POST,
            "PUT" => reqwest::Method::PUT,
            "DELETE" => reqwest::Method::DELETE,
            "PATCH" => reqwest::Method::PATCH,
            "HEAD" => reqwest::Method::HEAD,
            other => return Err(format!("unsupported http method: {other}")),
        };
        let mut builder = client.request(method, &req.url);
        for (k, v) in &req.headers {
            builder = builder.header(k.as_str(), v.as_str());
        }
        if let Some(body) = req.body {
            builder = builder.body(body);
        }
        // 3. Execute (blocking — wasmtime sync wiring expects sync host fns).
        let resp = builder.send().map_err(|e| {
            tracing::error!(ext = %self.extension_id, error = %e, "http::fetch transport error");
            format!("http transport error: {e}")
        })?;
        let status = resp.status().as_u16();
        let headers = resp
            .headers()
            .iter()
            .filter_map(|(k, v)| {
                v.to_str()
                    .ok()
                    .map(|s| (k.as_str().to_string(), s.to_string()))
            })
            .collect();
        let body = resp
            .bytes()
            .map_err(|e| format!("http body error: {e}"))?
            .to_vec();
        Ok(http::Response {
            status,
            headers,
            body,
        })
    }
}

impl llm::Host for HostState {
    fn complete(&mut self, request: llm::LlmRequest) -> Result<llm::LlmResponse, String> {
        // 1. Resolve the effective role from describe permissions. A `role_hint`
        //    must be one the extension declared; with no hint we allow the sole
        //    declared role and otherwise require disambiguation.
        let declared = &self.permissions.llm_roles;
        let role = match (&request.role_hint, declared.as_slice()) {
            (Some(hint), roles) if roles.iter().any(|r| r == hint) => hint.clone(),
            (Some(hint), _) => {
                tracing::warn!(ext = %self.extension_id, requested = %hint, "llm role not permitted");
                return Err(format!("llm role not permitted: {hint}"));
            }
            (None, [sole]) => sole.clone(),
            (None, []) => {
                return Err("llm role not permitted: extension declares no llm_roles".to_string());
            }
            (None, _many) => {
                return Err(
                    "llm role-hint required: extension declares multiple llm_roles".to_string(),
                );
            }
        };

        // 2. Resolve the port. Absent in unit tests and runtimes the host did
        //    not wire for LLM use — surface a clean error rather than panic.
        let Some(port) = self.llm_port.as_ref() else {
            return Err("llm not configured for this runtime".to_string());
        };

        // 3. Map the WIT request onto the host port, call, map the response.
        let port_req = crate::host_ports::LlmPortRequest {
            system_prompt: request.system_prompt,
            messages: request
                .messages
                .into_iter()
                .map(|m| (m.role, m.content))
                .collect(),
            response_format: match request.response_format {
                None | Some(llm::ResponseFormat::Text) => {
                    crate::host_ports::LlmPortResponseFormat::Text
                }
                Some(llm::ResponseFormat::Json) => crate::host_ports::LlmPortResponseFormat::Json,
                Some(llm::ResponseFormat::JsonSchema(s)) => {
                    crate::host_ports::LlmPortResponseFormat::JsonSchema(s)
                }
            },
        };
        match port.complete(&self.extension_id, &self.call_ctx, &role, port_req) {
            Ok(r) => Ok(llm::LlmResponse {
                content: r.content,
                total_tokens: r.total_tokens,
            }),
            Err(e) => {
                tracing::warn!(ext = %self.extension_id, %role, error = %e, "llm port error");
                Err(e.to_string())
            }
        }
    }
}

impl crate::host_bindings::design_v04::greentic::oauth_broker::broker_v1::Host for HostState {
    /// Retrieve a token for the given OAuth provider.
    ///
    /// Permission gate: the provider must be declared in
    /// `permissions.oauth_providers`. If not, returns a JSON error string with
    /// `"error": "permission_denied"` — fails closed.
    ///
    /// When permitted but no `oauth_config` or `http_client` is present (e.g.
    /// the runtime was not configured with an OAuth broker), returns
    /// `"error": "oauth_broker_unconfigured"`.
    ///
    /// The `shared_secret` is NEVER logged.
    fn get_token(&mut self, provider_id: String, _subject: String, scopes: Vec<String>) -> String {
        // Permission gate — mirror the secrets/network allowlist checks.
        if !self
            .permissions
            .oauth_providers
            .iter()
            .any(|p| p == &provider_id)
        {
            tracing::warn!(
                ext = %self.extension_id,
                provider = %provider_id,
                "oauth get-token permission denied"
            );
            return serde_json::json!({"error": "permission_denied", "provider": provider_id})
                .to_string();
        }

        let Some(cfg) = self.oauth_config.clone() else {
            return "{\"error\":\"oauth_broker_unconfigured\"}".to_string();
        };
        let Some(client) = self.http_client.clone() else {
            return "{\"error\":\"oauth_broker_unconfigured\"}".to_string();
        };

        let req = crate::oauth::ResourceTokenRequest {
            http_base_url: cfg.http_base_url,
            env: cfg.env,
            tenant: cfg.tenant,
            team: cfg.team,
            resource_id: provider_id.clone(),
            scopes,
        };

        match crate::oauth::request_resource_token_blocking(
            &client,
            &req,
            cfg.shared_secret.as_deref(),
        ) {
            Ok(resp) => serde_json::to_string(&resp)
                .unwrap_or_else(|_| "{\"error\":\"encode_failed\"}".to_string()),
            Err(e) => {
                tracing::warn!(provider = %provider_id, error = %e, "oauth get-token failed");
                "{\"error\":\"broker_request_failed\"}".to_string()
            }
        }
    }

    /// Build a consent URL for the given OAuth provider.
    ///
    /// Not yet implemented in the design-extension runtime — returns an empty
    /// string. Consent flows are handled by the OAuth broker service directly;
    /// this stub satisfies the WIT interface contract.
    fn get_consent_url(
        &mut self,
        _provider_id: String,
        _subject: String,
        _scopes: Vec<String>,
        _redirect_path: String,
        _extra_json: String,
    ) -> String {
        String::new()
    }

    /// Exchange an authorization code for a token set.
    ///
    /// Not yet implemented in the design-extension runtime — returns an empty
    /// string. Code exchange is handled by the OAuth broker service directly;
    /// this stub satisfies the WIT interface contract.
    fn exchange_code(
        &mut self,
        _provider_id: String,
        _subject: String,
        _code: String,
        _redirect_path: String,
    ) -> String {
        String::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::host_bindings::greentic::extension_host::i18n::Host as I18nHost;
    use crate::host_ports::Translator;
    use std::sync::Arc;

    struct EnglishToIndonesian;
    impl Translator for EnglishToIndonesian {
        fn t(&self, key: &str) -> String {
            match key {
                "greentic.test.hello" => "Halo dunia".to_string(),
                _ => key.to_string(),
            }
        }
        fn tf(&self, key: &str, args: &[(&str, &str)]) -> String {
            let template = self.t(key);
            args.iter()
                .fold(template, |acc, (k, v)| acc.replace(&format!("{{{k}}}"), v))
        }
    }

    fn host_with_translator(t: Arc<dyn Translator>) -> HostState {
        HostState::builder("test-ext".to_string(), Permissions::default())
            .translator(t)
            .build()
    }

    #[test]
    fn i18n_t_resolves_translated_value() {
        let mut h = host_with_translator(Arc::new(EnglishToIndonesian));
        let got = h.t("greentic.test.hello".to_string());
        assert_eq!(got, "Halo dunia");
    }

    #[test]
    fn i18n_t_falls_back_to_key_for_unknown() {
        let mut h = host_with_translator(Arc::new(EnglishToIndonesian));
        let got = h.t("missing.key".to_string());
        assert_eq!(got, "missing.key");
    }

    #[test]
    fn secrets_get_returns_value_when_permitted() {
        use crate::host_bindings::greentic::extension_host::secrets::Host as SecretsHost;
        use crate::host_ports::InMemorySecrets;

        let mut backend = InMemorySecrets::default();
        backend.insert("api.openai.com/api_key", "sk-real");
        let mut perms = Permissions::default();
        perms.secrets.push("api.openai.com/api_key".to_string());

        let mut h = HostState::builder("test-ext".to_string(), perms)
            .secrets_backend(Arc::new(backend))
            .build();
        let v = h.get("api.openai.com/api_key".to_string()).unwrap();
        assert_eq!(v, "sk-real");
    }

    #[test]
    fn secrets_get_denies_when_uri_not_in_permissions() {
        use crate::host_bindings::greentic::extension_host::secrets::Host as SecretsHost;
        use crate::host_ports::InMemorySecrets;

        let mut backend = InMemorySecrets::default();
        backend.insert("api.openai.com/api_key", "sk-real");
        let perms = Permissions::default();

        let mut h = HostState::builder("test-ext".to_string(), perms)
            .secrets_backend(Arc::new(backend))
            .build();
        let err = h.get("api.openai.com/api_key".to_string()).unwrap_err();
        assert!(
            err.contains("permission denied"),
            "expected permission denied, got: {err}"
        );
    }

    #[test]
    fn broker_denies_when_kind_not_in_permissions() {
        use crate::host_bindings::greentic::extension_host::broker::Host as BrokerHost;

        let mut perms = Permissions::default();
        perms.call_extension_kinds.push("provider".to_string());
        let mut h = HostState::builder("caller".into(), perms).build();

        let err = h
            .call_extension(
                "design".into(),
                "greentic.target".into(),
                "do_something".into(),
                "{}".into(),
            )
            .unwrap_err();
        assert!(err.contains("may not call"), "got: {err}");
    }

    #[test]
    fn broker_rejects_when_depth_exceeded() {
        use crate::host_bindings::greentic::extension_host::broker::Host as BrokerHost;

        let mut perms = Permissions::default();
        perms.call_extension_kinds.push("design".to_string());
        let mut h = HostState::builder("caller".into(), perms)
            .call_depth_start(MAX_BROKER_DEPTH)
            .build();
        let err = h
            .call_extension(
                "design".into(),
                "greentic.target".into(),
                "do".into(),
                "{}".into(),
            )
            .unwrap_err();
        assert!(err.contains("max"), "expected depth error, got: {err}");
    }

    #[test]
    fn http_fetch_denied_when_url_not_in_matcher() {
        use crate::host_bindings::greentic::extension_host::http::{Host as HttpHost, Request};

        let mut h = HostState::builder("test-ext".into(), Permissions::default())
            .url_matcher(crate::url_matcher::UrlMatcher::from_patterns(vec![
                "https://allowed.com/*".into(),
            ]))
            .build();
        let req = Request {
            method: "GET".into(),
            url: "https://evil.com/".into(),
            headers: vec![],
            body: None,
        };
        let err = h.fetch(req).unwrap_err();
        assert!(
            err.contains("not allowed") || err.contains("permission denied"),
            "got: {err}"
        );
    }

    #[test]
    fn secrets_get_surfaces_backend_not_found() {
        use crate::host_bindings::greentic::extension_host::secrets::Host as SecretsHost;
        use crate::host_ports::InMemorySecrets;

        let backend = InMemorySecrets::default();
        let mut perms = Permissions::default();
        perms.secrets.push("api.openai.com/api_key".to_string());

        let mut h = HostState::builder("test-ext".to_string(), perms)
            .secrets_backend(Arc::new(backend))
            .build();
        let err = h.get("api.openai.com/api_key".to_string()).unwrap_err();
        assert!(err.contains("not found"), "got: {err}");
    }

    /// In-test [`LlmPort`] that records the `extension_id` / `ctx` / `role`
    /// it was called with and echoes the system prompt back. Asserts the host
    /// resolved the expected role and threaded the expected tenant + user email
    /// before forwarding.
    ///
    /// `expected_*` prefix is deliberate (these are the values the port asserts
    /// against, not generic data), so the shared-prefix lint is silenced here.
    #[allow(clippy::struct_field_names)]
    struct FakeLlm {
        expected_extension_id: String,
        expected_tenant: Option<String>,
        expected_user_email: Option<String>,
        expected_role: String,
    }

    impl crate::host_ports::LlmPort for FakeLlm {
        fn complete(
            &self,
            extension_id: &str,
            ctx: &crate::host_ports::HostCallContext,
            role: &str,
            request: crate::host_ports::LlmPortRequest,
        ) -> Result<crate::host_ports::LlmPortResponse, crate::host_ports::LlmPortError> {
            assert_eq!(extension_id, self.expected_extension_id, "extension_id");
            assert_eq!(ctx.tenant, self.expected_tenant, "tenant");
            assert_eq!(ctx.user_email, self.expected_user_email, "user_email");
            assert_eq!(role, self.expected_role, "role");
            Ok(crate::host_ports::LlmPortResponse {
                content: format!("echo:{}", request.system_prompt),
                total_tokens: Some(7),
            })
        }
    }

    fn llm_request(role_hint: Option<&str>) -> llm::LlmRequest {
        llm::LlmRequest {
            role_hint: role_hint.map(str::to_string),
            system_prompt: "you are a composer".to_string(),
            messages: vec![],
            response_format: None,
        }
    }

    #[test]
    fn llm_complete_resolves_sole_declared_role() {
        use crate::host_bindings::greentic::extension_host::llm::Host as LlmHost;

        let mut perms = Permissions::default();
        perms.llm_roles.push("sorla_composer".to_string());
        let port = Arc::new(FakeLlm {
            expected_extension_id: "test-ext".to_string(),
            expected_tenant: Some("acme".to_string()),
            expected_user_email: None,
            expected_role: "sorla_composer".to_string(),
        });
        let mut h = HostState::builder("test-ext".to_string(), perms)
            .llm_port(Some(port))
            .call_ctx(crate::host_ports::HostCallContext {
                tenant: Some("acme".into()),
                user_email: None,
            })
            .build();

        let resp = h
            .complete(llm_request(None))
            .expect("complete should succeed");
        assert_eq!(resp.content, "echo:you are a composer");
        assert_eq!(resp.total_tokens, Some(7));
    }

    #[test]
    fn llm_complete_passes_none_tenant_by_default() {
        use crate::host_bindings::greentic::extension_host::llm::Host as LlmHost;

        let mut perms = Permissions::default();
        perms.llm_roles.push("sorla_composer".to_string());
        let port = Arc::new(FakeLlm {
            expected_extension_id: "test-ext".to_string(),
            expected_tenant: None,
            expected_user_email: None,
            expected_role: "sorla_composer".to_string(),
        });
        // No `.call_ctx(...)` in the builder chain — the host runs
        // single-tenant/dev, so the port must observe a default (all-`None`)
        // context.
        let mut h = HostState::builder("test-ext".to_string(), perms)
            .llm_port(Some(port))
            .build();

        let resp = h
            .complete(llm_request(None))
            .expect("complete should succeed");
        assert_eq!(resp.content, "echo:you are a composer");
    }

    #[test]
    fn llm_complete_passes_user_email() {
        use crate::host_bindings::greentic::extension_host::llm::Host as LlmHost;

        let mut perms = Permissions::default();
        perms.llm_roles.push("sorla_composer".to_string());
        let port = Arc::new(FakeLlm {
            expected_extension_id: "test-ext".to_string(),
            expected_tenant: Some("acme".to_string()),
            expected_user_email: Some("alice@acme.com".to_string()),
            expected_role: "sorla_composer".to_string(),
        });
        let mut h = HostState::builder("test-ext".to_string(), perms)
            .llm_port(Some(port))
            .call_ctx(crate::host_ports::HostCallContext {
                tenant: Some("acme".into()),
                user_email: Some("alice@acme.com".into()),
            })
            .build();

        let resp = h
            .complete(llm_request(None))
            .expect("complete should succeed");
        assert_eq!(resp.content, "echo:you are a composer");
    }

    #[test]
    fn llm_complete_rejects_undeclared_role() {
        use crate::host_bindings::greentic::extension_host::llm::Host as LlmHost;

        let perms = Permissions::default(); // no llm_roles declared
        let port = Arc::new(FakeLlm {
            expected_extension_id: "test-ext".to_string(),
            expected_tenant: None,
            expected_user_email: None,
            expected_role: "sorla_composer".to_string(),
        });
        let mut h = HostState::builder("test-ext".to_string(), perms)
            .llm_port(Some(port))
            .build();

        let err = h.complete(llm_request(Some("sorla_composer"))).unwrap_err();
        assert!(err.contains("llm role not permitted"), "got: {err}");
    }

    #[test]
    fn llm_complete_without_port_errors() {
        use crate::host_bindings::greentic::extension_host::llm::Host as LlmHost;

        let mut perms = Permissions::default();
        perms.llm_roles.push("sorla_composer".to_string());
        let mut h = HostState::builder("test-ext".to_string(), perms).build();

        let err = h.complete(llm_request(None)).unwrap_err();
        assert!(err.contains("llm not configured"), "got: {err}");
    }

    #[test]
    fn llm_complete_requires_hint_when_multiple_roles() {
        use crate::host_bindings::greentic::extension_host::llm::Host as LlmHost;

        let mut perms = Permissions::default();
        perms.llm_roles.push("a".to_string());
        perms.llm_roles.push("b".to_string());
        let port = Arc::new(FakeLlm {
            expected_extension_id: "test-ext".to_string(),
            expected_tenant: None,
            expected_user_email: None,
            expected_role: "unused".to_string(),
        });
        let mut h = HostState::builder("test-ext".to_string(), perms)
            .llm_port(Some(port))
            .build();

        let err = h.complete(llm_request(None)).unwrap_err();
        assert!(err.contains("role-hint required"), "got: {err}");
    }

    #[test]
    fn i18n_tf_substitutes_named_args() {
        struct GreetTranslator;
        impl Translator for GreetTranslator {
            fn t(&self, key: &str) -> String {
                if key == "greentic.test.greet" {
                    "Halo {name}!".to_string()
                } else {
                    key.to_string()
                }
            }
            fn tf(&self, key: &str, args: &[(&str, &str)]) -> String {
                let template = self.t(key);
                args.iter()
                    .fold(template, |acc, (k, v)| acc.replace(&format!("{{{k}}}"), v))
            }
        }
        let mut h = host_with_translator(Arc::new(GreetTranslator));
        let got = h.tf(
            "greentic.test.greet".to_string(),
            vec![("name".to_string(), "Bima".to_string())],
        );
        assert_eq!(got, "Halo Bima!");
    }

    #[test]
    fn oauth_get_token_denied_when_provider_not_declared() {
        use crate::host_bindings::design_v04::greentic::oauth_broker::broker_v1::Host as OAuthHost;
        let mut h = HostState::builder("ext".into(), Permissions::default()).build();
        let out = h.get_token("hubspot".into(), String::new(), vec![]);
        assert!(out.contains("permission_denied"), "got: {out}");
    }

    #[test]
    fn oauth_get_token_errors_when_unconfigured() {
        use crate::host_bindings::design_v04::greentic::oauth_broker::broker_v1::Host as OAuthHost;
        let mut perms = Permissions::default();
        perms.oauth_providers.push("hubspot".into());
        let mut h = HostState::builder("ext".into(), perms).build();
        let out = h.get_token("hubspot".into(), String::new(), vec![]);
        assert!(out.contains("oauth_broker_unconfigured"), "got: {out}");
    }

    #[test]
    fn host_state_carries_oauth_config() {
        use crate::oauth::OAuthBrokerConfig;
        let cfg = OAuthBrokerConfig {
            http_base_url: "https://oauth.example/".into(),
            env: "dev".into(),
            tenant: "acme".into(),
            team: None,
            shared_secret: Some("s".into()),
        };
        let h = HostState::builder("test-ext".to_string(), Permissions::default())
            .oauth_config(Some(cfg.clone()))
            .build();
        assert_eq!(h.oauth_config.as_ref().unwrap().tenant, "acme");
    }
}