trusty-code 0.2.0

Per-project Claude-Code-compatible MPM orchestration harness (bin: tcode) — an original coding harness, tracked under epic #2052.
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
//! OpenAI-compatible inference transport, backed by the shared
//! `trusty_common::inference` adapter layer (#2406, epic #2400).
//!
//! Why: trusty-code used to hand-roll its own OpenRouter `reqwest` client. Epic
//! #2400 centralises the OpenAI-compatible HTTP mechanics (auth, the OpenRouter
//! detailed-usage directive, HTTP→error classification, response parsing) in
//! ONE shared core so every consumer shares it instead of six near-identical
//! copies. This module is tcode's thin consumer of that core: it selects the
//! provider (OpenRouter, Fireworks, Together, or AtlasCloud) by model slug, resolves the
//! credential via the shared 3-tier resolver (process env > `.env.local` >
//! secure store), builds the matching `trusty_common::inference` adapter once
//! (caching it so the underlying `reqwest` connection pool is reused across a
//! run's many turns — a bake-off latency concern), and bridges tcode's
//! request/response types across the seam (`super::convert`). Bedrock stays on
//! its own Converse transport (`super::bedrock`, routed by `super::dispatch`) —
//! its migration into commons is #2407.
//! What: [`OpenAiCompatClient`] implements [`LlmClientTrait`]. `fireworks/*`
//! slugs route to the Fireworks adapter (stripping the `fireworks/` routing
//! prefix to the provider-native model id and requiring `FIREWORKS_API_KEY`);
//! `together/*` slugs route to the Together adapter (#2494 — stripping the
//! `together/` routing prefix and requiring `TOGETHER_API_KEY`); `atlascloud/*`
//! slugs route to the AtlasCloud adapter (#2536 — stripping the `atlascloud/`
//! routing prefix and requiring `ATLASCLOUD_API_KEY`); everything else
//! routes to OpenRouter, sending the slug unchanged (identical to the pre-#2406
//! behaviour). A missing credential surfaces at `chat()` time (not
//! construction), preserving the #2245 deferred-failure contract. The base URLs
//! are overridable (`TCODE_OPENROUTER_BASE_URL` / `TCODE_FIREWORKS_BASE_URL` /
//! `TCODE_TOGETHER_BASE_URL` / `TCODE_ATLASCLOUD_BASE_URL` or
//! [`OpenAiCompatClient::with_config`]) so an offline mock server can be targeted
//! end-to-end.
//! Test: `client::tests::*` (provider selection, prefix stripping, hermetic
//! missing-credential path against an injected empty store) and the black-box
//! HTTP round-trip in `tests/inference_shared_adapter_e2e.rs`; the `#[ignore]`
//! `live_openrouter_call` / `live_fireworks_call` / `live_together_call` smokes
//! hit the real APIs.

use std::collections::HashMap;
use std::sync::Arc;

use async_trait::async_trait;
use tokio::sync::Mutex;
use trusty_common::inference::{
    InferenceAdapter,
    credentials::{KeyStore, default_store, resolve_key_with},
    provider_for,
    providers::{atlascloud, fireworks, openrouter, together},
    registry::ProviderId,
};

use super::client_trait::LlmClientTrait;
use super::convert;
use super::error::LlmError;
use super::request::ChatRequest;
use super::response::ChatResponse;

/// Env var overriding the OpenRouter API base URL (for offline mock testing /
/// self-hosted gateways). Defaults to [`openrouter::OPENROUTER_BASE_URL`].
const OPENROUTER_BASE_URL_ENV: &str = "TCODE_OPENROUTER_BASE_URL";

/// Env var overriding the Fireworks API base URL. Defaults to
/// [`fireworks::FIREWORKS_BASE_URL`].
const FIREWORKS_BASE_URL_ENV: &str = "TCODE_FIREWORKS_BASE_URL";

/// Env var overriding the Together API base URL. Defaults to
/// [`together::TOGETHER_BASE_URL`].
const TOGETHER_BASE_URL_ENV: &str = "TCODE_TOGETHER_BASE_URL";

/// Env var overriding the AtlasCloud API base URL. Defaults to
/// [`atlascloud::ATLASCLOUD_BASE_URL`].
const ATLASCLOUD_BASE_URL_ENV: &str = "TCODE_ATLASCLOUD_BASE_URL";

/// The provider name `crate::provider::provider_for` reports for `fireworks/*`
/// slugs — the single routing condition this client checks (mirroring how
/// `super::dispatch` keys Bedrock routing off the same factory).
const FIREWORKS_PROVIDER_NAME: &str = "fireworks";

/// The provider name `crate::provider::provider_for` reports for `together/*`
/// slugs — the second OpenAI-dialect routing condition this client checks
/// (#2494), keyed off the same factory as Fireworks.
const TOGETHER_PROVIDER_NAME: &str = "together";

/// The provider name `crate::provider::provider_for` reports for `atlascloud/*`
/// slugs — the third OpenAI-dialect routing condition this client checks
/// (#2536), keyed off the same factory as Fireworks and Together.
const ATLASCLOUD_PROVIDER_NAME: &str = "atlascloud";

/// OpenAI-compatible transport over the shared inference adapter, routing
/// OpenRouter and Fireworks by model slug.
///
/// Why: one place that owns "which OpenAI-dialect provider, whose key, which
/// base URL" so the dispatch layer and the agent loop stay backend-agnostic.
/// What: holds the shared credential [`KeyStore`], the (overridable) per-provider
/// base URLs, and a lazily-populated cache of built adapters keyed by
/// [`ProviderId`] so each provider's `reqwest` client (and its connection pool)
/// is constructed at most once per process.
/// Test: `client::tests::*`.
pub struct OpenAiCompatClient {
    store: Box<dyn KeyStore>,
    openrouter_base: String,
    fireworks_base: String,
    together_base: String,
    atlascloud_base: String,
    adapters: Mutex<HashMap<ProviderId, Arc<dyn InferenceAdapter>>>,
}

impl std::fmt::Debug for OpenAiCompatClient {
    /// Redacting `Debug`: the [`KeyStore`] and built adapters hold credential
    /// material, so this prints only the opaque type name.
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OpenAiCompatClient").finish_non_exhaustive()
    }
}

impl OpenAiCompatClient {
    /// Construct with the process-default secure store and base URLs.
    ///
    /// Why: the production entry point — binary code funnels here so credential
    /// resolution uses the same env > `.env.local` > store chain everywhere.
    /// What: uses [`default_store`] and reads the base-URL env overrides once
    /// (falling back to the real provider URLs). Never touches credentials, so
    /// construction cannot fail on a missing key (#2245): that surfaces at
    /// `chat()` time.
    /// Test: `client::tests::selects_openrouter_for_plain_slug`.
    pub fn new() -> Self {
        Self::with_store(default_store())
    }

    /// Construct with an explicit [`KeyStore`], reading base URLs from the env
    /// overrides (or the real provider defaults).
    ///
    /// Why: lets a hermetic test inject a `MemoryKeyStore` so credential
    /// resolution never depends on the machine's real environment or `$HOME`.
    /// What: as [`Self::new`] but with the caller's store.
    /// Test: `client::tests::missing_openrouter_key_errors_at_chat_time`.
    pub fn with_store(store: Box<dyn KeyStore>) -> Self {
        let openrouter_base = std::env::var(OPENROUTER_BASE_URL_ENV)
            .unwrap_or_else(|_| openrouter::OPENROUTER_BASE_URL.to_string());
        let fireworks_base = std::env::var(FIREWORKS_BASE_URL_ENV)
            .unwrap_or_else(|_| fireworks::FIREWORKS_BASE_URL.to_string());
        let together_base = std::env::var(TOGETHER_BASE_URL_ENV)
            .unwrap_or_else(|_| together::TOGETHER_BASE_URL.to_string());
        let atlascloud_base = std::env::var(ATLASCLOUD_BASE_URL_ENV)
            .unwrap_or_else(|_| atlascloud::ATLASCLOUD_BASE_URL.to_string());
        Self::with_config(
            store,
            openrouter_base,
            fireworks_base,
            together_base,
            atlascloud_base,
        )
    }

    /// Construct with an explicit store AND explicit per-provider base URLs.
    ///
    /// Why: the offline black-box e2e points each provider at a
    /// [`trusty_common::inference::test_support::MockInferenceServer`] without
    /// mutating process env (so the test stays parallel-safe).
    /// What: stores all five inputs and an empty adapter cache.
    /// Test: `tests/inference_shared_adapter_e2e.rs`.
    pub fn with_config(
        store: Box<dyn KeyStore>,
        openrouter_base: String,
        fireworks_base: String,
        together_base: String,
        atlascloud_base: String,
    ) -> Self {
        Self {
            store,
            openrouter_base,
            fireworks_base,
            together_base,
            atlascloud_base,
            adapters: Mutex::new(HashMap::new()),
        }
    }

    /// Select the OpenAI-dialect provider for a model slug.
    ///
    /// Why: single source of truth — reuses `crate::provider::provider_for`, the
    /// same factory `super::dispatch` consults for Bedrock routing and
    /// `AgentLoop::build_request` consults for usage/caching decisions, so
    /// routing can never disagree with normalisation.
    /// What: [`ProviderId::Fireworks`] iff that factory reports `"fireworks"`,
    /// [`ProviderId::Together`] iff it reports `"together"` (#2494),
    /// [`ProviderId::AtlasCloud`] iff it reports `"atlascloud"` (#2536), else
    /// [`ProviderId::OpenRouter`] (the default for every other slug).
    /// Test: `client::tests::selects_fireworks_for_prefixed_slug`,
    /// `client::tests::selects_together_for_prefixed_slug`,
    /// `client::tests::selects_atlascloud_for_prefixed_slug`,
    /// `client::tests::selects_openrouter_for_plain_slug`.
    fn provider_for_slug(model: &str) -> ProviderId {
        match crate::provider::provider_for(model).name() {
            FIREWORKS_PROVIDER_NAME => ProviderId::Fireworks,
            TOGETHER_PROVIDER_NAME => ProviderId::Together,
            ATLASCLOUD_PROVIDER_NAME => ProviderId::AtlasCloud,
            _ => ProviderId::OpenRouter,
        }
    }

    /// The provider-native wire model id for a routing slug.
    ///
    /// Why: Fireworks serves bare `accounts/fireworks/models/*` ids and Together
    /// serves bare `meta-llama/*` (etc.) ids, but tcode routes on a
    /// `fireworks/`- or `together/`-prefixed slug; the prefix is a routing
    /// artefact that must be stripped before the id is sent. OpenRouter takes the
    /// slug verbatim (identical to pre-#2406).
    /// What: strips a leading `fireworks/` for [`ProviderId::Fireworks`], a
    /// leading `together/` for [`ProviderId::Together`] (#2494), and a leading
    /// `atlascloud/` for [`ProviderId::AtlasCloud`] (#2536 — the remainder, e.g.
    /// `openai/gpt-5.6-sol`, is the AtlasCloud-native wire id); returns the slug
    /// unchanged otherwise.
    /// Test: `client::tests::fireworks_wire_model_strips_prefix`,
    /// `client::tests::together_wire_model_strips_prefix`,
    /// `client::tests::atlascloud_wire_model_strips_prefix`.
    fn wire_model(provider: ProviderId, model: &str) -> String {
        match provider {
            ProviderId::Fireworks => model
                .strip_prefix("fireworks/")
                .unwrap_or(model)
                .to_string(),
            ProviderId::Together => model.strip_prefix("together/").unwrap_or(model).to_string(),
            ProviderId::AtlasCloud => model
                .strip_prefix("atlascloud/")
                .unwrap_or(model)
                .to_string(),
            _ => model.to_string(),
        }
    }

    /// Get (or lazily build + cache) the adapter for a provider.
    ///
    /// Why: building an adapter constructs a `reqwest` client; doing that once
    /// per provider and reusing it preserves connection pooling across a run's
    /// turns (a latency/cost baseline concern), mirroring the original client's
    /// build-once-at-startup behaviour.
    /// What: returns the cached `Arc` on a hit; on a miss builds via
    /// [`Self::build_adapter`], inserts, and returns it. A build failure is
    /// returned (not cached), so a later turn can retry once the credential is
    /// fixed.
    /// Test: exercised by every `chat` path.
    async fn adapter_for(
        &self,
        provider: ProviderId,
    ) -> Result<Arc<dyn InferenceAdapter>, LlmError> {
        {
            let cache = self.adapters.lock().await;
            if let Some(adapter) = cache.get(&provider) {
                return Ok(adapter.clone());
            }
        }
        let built: Arc<dyn InferenceAdapter> = Arc::from(self.build_adapter(provider)?);
        let mut cache = self.adapters.lock().await;
        Ok(cache.entry(provider).or_insert(built).clone())
    }

    /// Resolve the credential and build the shared adapter for a provider.
    ///
    /// Why: this is the seam that reuses the shared resolver + provider factory
    /// while keeping tcode's routing decision authoritative — we build ONLY the
    /// provider tcode selected, never letting the shared resolver's own
    /// prefix-based fallbacks silently re-home a request to OpenAI-direct or
    /// Anthropic-direct.
    /// What: for Fireworks, first resolves the `FIREWORKS_API_KEY` explicitly via
    /// the shared resolver so a missing key is ALWAYS a clear, Fireworks-specific
    /// [`LlmError::MissingConfig`] — never the shared two-stage resolver's silent
    /// fall-back to OpenRouter (which would be a wrong-provider misroute), and
    /// never an OpenRouter-flavoured error when both keys happen to be absent;
    /// only once the key is confirmed does it resolve + build the Fireworks
    /// adapter. Together (#2494) and AtlasCloud (#2536) follow the identical
    /// contract against `TOGETHER_API_KEY` / `ATLASCLOUD_API_KEY` respectively.
    /// For OpenRouter, resolves on an `openrouter/` routing
    /// slug (a missing key surfaces as the resolver's own `MissingCredential`,
    /// mapped to `MissingConfig`). The resolved model slug is irrelevant to the
    /// built adapter (it sends the per-request wire model), so a fixed routing
    /// slug is used.
    /// Test: `client::tests::missing_openrouter_key_errors_at_chat_time`,
    /// `client::tests::missing_fireworks_key_errors_not_falls_back`,
    /// `client::tests::missing_together_key_errors_not_falls_back`,
    /// `client::tests::missing_atlascloud_key_errors_not_falls_back`.
    fn build_adapter(&self, provider: ProviderId) -> Result<Box<dyn InferenceAdapter>, LlmError> {
        match provider {
            ProviderId::Fireworks => {
                if resolve_key_with("fireworks", self.store.as_ref()).is_none() {
                    return Err(LlmError::MissingConfig(
                        "FIREWORKS_API_KEY is required to reach a fireworks/* model. Export it \
                         (e.g. `export FIREWORKS_API_KEY=fw_...`), add it to .env.local, or set it \
                         via `tcode config keys set fireworks`."
                            .to_string(),
                    ));
                }
                // The key is present, so stage-1 of the resolver returns Fireworks.
                let resolved = provider_for("fireworks/route", self.store.as_ref())
                    .map_err(convert::map_error)?;
                fireworks::build(&resolved, &self.fireworks_base).map_err(convert::map_error)
            }
            ProviderId::Together => {
                if resolve_key_with("together", self.store.as_ref()).is_none() {
                    return Err(LlmError::MissingConfig(
                        "TOGETHER_API_KEY is required to reach a together/* model. Export it \
                         (e.g. `export TOGETHER_API_KEY=tgp_...`), add it to .env.local, or set \
                         it via `tcode config keys set together`."
                            .to_string(),
                    ));
                }
                // The key is present, so stage-1 of the resolver returns Together.
                let resolved = provider_for("together/route", self.store.as_ref())
                    .map_err(convert::map_error)?;
                together::build(&resolved, &self.together_base).map_err(convert::map_error)
            }
            ProviderId::AtlasCloud => {
                if resolve_key_with("atlascloud", self.store.as_ref()).is_none() {
                    return Err(LlmError::MissingConfig(
                        "ATLASCLOUD_API_KEY is required to reach an atlascloud/* model. Export it \
                         (e.g. `export ATLASCLOUD_API_KEY=ac_...`), add it to .env.local, or set \
                         it via `tcode config keys set atlascloud`."
                            .to_string(),
                    ));
                }
                // The key is present, so stage-1 of the resolver returns AtlasCloud.
                let resolved = provider_for("atlascloud/route", self.store.as_ref())
                    .map_err(convert::map_error)?;
                atlascloud::build(&resolved, &self.atlascloud_base).map_err(convert::map_error)
            }
            _ => {
                let resolved = provider_for("openrouter/route", self.store.as_ref())
                    .map_err(convert::map_error)?;
                openrouter::build(&resolved, &self.openrouter_base).map_err(convert::map_error)
            }
        }
    }

    /// Route + issue one chat call through the shared adapter.
    ///
    /// Why: the single method [`LlmClientTrait`] exposes; all provider selection,
    /// prefix stripping, credential resolution, and type bridging live behind it.
    /// What: selects the provider from `req.model`, gets its cached adapter,
    /// converts the request (with the provider-native wire model), calls the
    /// shared adapter, maps any error, and converts the response back.
    /// Test: `client::tests::*` and `tests/inference_shared_adapter_e2e.rs`.
    pub async fn chat(&self, req: &ChatRequest) -> Result<ChatResponse, LlmError> {
        let provider = Self::provider_for_slug(&req.model);
        let adapter = self.adapter_for(provider).await?;
        let wire_model = Self::wire_model(provider, &req.model);
        let shared_req = convert::to_shared_request(req, wire_model);
        let shared_resp = adapter
            .chat(&shared_req)
            .await
            .map_err(convert::map_error)?;
        Ok(convert::from_shared_response(shared_resp))
    }
}

impl Default for OpenAiCompatClient {
    /// Delegates to [`OpenAiCompatClient::new`].
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl LlmClientTrait for OpenAiCompatClient {
    /// Forward to the inherent [`OpenAiCompatClient::chat`].
    async fn chat(&self, req: &ChatRequest) -> Result<ChatResponse, LlmError> {
        OpenAiCompatClient::chat(self, req).await
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use trusty_common::inference::credentials::MemoryKeyStore;

    /// A plain (non-fireworks) slug selects OpenRouter.
    ///
    /// Why: preserves the pre-#2406 default — every non-fireworks, non-bedrock
    /// slug goes to OpenRouter unchanged.
    /// What: assert the provider selection for representative OpenRouter slugs.
    /// Test: this test.
    #[test]
    fn selects_openrouter_for_plain_slug() {
        for slug in [
            "openai/gpt-4o-mini",
            "anthropic/claude-sonnet-4-5",
            "qwen/qwen-2.5-coder-32b-instruct",
        ] {
            assert_eq!(
                OpenAiCompatClient::provider_for_slug(slug),
                ProviderId::OpenRouter,
                "{slug} must select OpenRouter"
            );
        }
    }

    /// A `fireworks/*` slug selects Fireworks.
    ///
    /// Why: this is the routing decision that makes Fireworks reachable (#2406).
    /// What: assert the provider selection for a fireworks slug.
    /// Test: this test.
    #[test]
    fn selects_fireworks_for_prefixed_slug() {
        assert_eq!(
            OpenAiCompatClient::provider_for_slug(
                "fireworks/accounts/fireworks/models/llama-v3p1-70b-instruct"
            ),
            ProviderId::Fireworks
        );
    }

    /// A `together/*` slug selects Together.
    ///
    /// Why: this is the routing decision that makes Together reachable (#2494).
    /// What: assert the provider selection for a together slug.
    /// Test: this test.
    #[test]
    fn selects_together_for_prefixed_slug() {
        assert_eq!(
            OpenAiCompatClient::provider_for_slug(
                "together/meta-llama/Llama-3.3-70B-Instruct-Turbo"
            ),
            ProviderId::Together
        );
    }

    /// An `atlascloud/*` slug selects AtlasCloud, including the nested
    /// `atlascloud/openai/gpt-5.6-sol` form.
    ///
    /// Why: this is the routing decision that makes AtlasCloud reachable (#2536),
    /// and the nested `openai/`-shaped model id must not re-home to OpenRouter.
    /// What: assert the provider selection for the nested and a bare atlascloud slug.
    /// Test: this test.
    #[test]
    fn selects_atlascloud_for_prefixed_slug() {
        assert_eq!(
            OpenAiCompatClient::provider_for_slug("atlascloud/openai/gpt-5.6-sol"),
            ProviderId::AtlasCloud
        );
        assert_eq!(
            OpenAiCompatClient::provider_for_slug("atlascloud/deepseek-v3"),
            ProviderId::AtlasCloud
        );
    }

    /// The Fireworks wire model strips the `fireworks/` routing prefix; the
    /// OpenRouter wire model is the slug verbatim.
    ///
    /// Why: Fireworks 404s on the prefixed id; OpenRouter needs the slug as-is.
    /// What: assert both directions.
    /// Test: this test.
    #[test]
    fn fireworks_wire_model_strips_prefix() {
        assert_eq!(
            OpenAiCompatClient::wire_model(
                ProviderId::Fireworks,
                "fireworks/accounts/fireworks/models/llama-v3p1-70b-instruct"
            ),
            "accounts/fireworks/models/llama-v3p1-70b-instruct"
        );
        assert_eq!(
            OpenAiCompatClient::wire_model(ProviderId::OpenRouter, "openai/gpt-4o-mini"),
            "openai/gpt-4o-mini"
        );
    }

    /// The Together wire model strips the `together/` routing prefix down to the
    /// bare Together catalog slug.
    ///
    /// Why: Together 404s on the prefixed id; the prefix is a tcode routing
    /// artefact only (#2494).
    /// What: assert the stripped slug.
    /// Test: this test.
    #[test]
    fn together_wire_model_strips_prefix() {
        assert_eq!(
            OpenAiCompatClient::wire_model(
                ProviderId::Together,
                "together/meta-llama/Llama-3.3-70B-Instruct-Turbo"
            ),
            "meta-llama/Llama-3.3-70B-Instruct-Turbo"
        );
    }

    /// The AtlasCloud wire model strips the `atlascloud/` routing prefix down to
    /// the AtlasCloud-native slug — including the nested `openai/gpt-5.6-sol`
    /// remainder (the `atlascloud/` segment is the ONLY routing artefact stripped)
    /// and the bare `deepseek-v3` remainder.
    ///
    /// Why: AtlasCloud 404s on the `atlascloud/`-prefixed id; the prefix is a
    /// tcode routing artefact only, but the model id may itself be `vendor/model`
    /// shaped, so only the leading `atlascloud/` segment must be removed (#2536).
    /// What: assert the stripped slug for both the nested and bare forms.
    /// Test: this test.
    #[test]
    fn atlascloud_wire_model_strips_prefix() {
        assert_eq!(
            OpenAiCompatClient::wire_model(ProviderId::AtlasCloud, "atlascloud/openai/gpt-5.6-sol"),
            "openai/gpt-5.6-sol"
        );
        assert_eq!(
            OpenAiCompatClient::wire_model(ProviderId::AtlasCloud, "atlascloud/deepseek-v3"),
            "deepseek-v3"
        );
    }

    /// With an empty store and (assumed) no `OPENROUTER_API_KEY` env, an
    /// OpenRouter chat fails at `chat()` time with a `MissingConfig` — never at
    /// construction (#2245 deferred-failure contract).
    ///
    /// Why: pins that construction is credential-free and that the missing-key
    /// error is actionable and surfaces only when a request needs it.
    /// What: build with an empty `MemoryKeyStore`; if the ambient env happens to
    /// carry a real key (dev machines), skip the assertion rather than make a
    /// live call — the hermetic case (no env key) is what CI exercises.
    /// Test: this test.
    #[tokio::test]
    async fn missing_openrouter_key_errors_at_chat_time() {
        if std::env::var("OPENROUTER_API_KEY").is_ok_and(|v| !v.is_empty()) {
            return; // real key present locally — don't issue a live call
        }
        let client = OpenAiCompatClient::with_store(Box::new(MemoryKeyStore::new())); // empty store
        let req = ChatRequest {
            model: "openai/gpt-4o-mini".into(),
            messages: vec![],
            temperature: None,
            max_tokens: None,
            tools: None,
            tool_choice: None,
            usage: None,
        };
        let err = client
            .chat(&req)
            .await
            .expect_err("must fail without a key");
        assert!(
            matches!(err, LlmError::MissingConfig(_)),
            "expected MissingConfig, got: {err:?}"
        );
    }

    /// A `fireworks/*` request with no `FIREWORKS_API_KEY` fails with an
    /// explicit `MissingConfig` — it must NOT silently fall back to OpenRouter.
    ///
    /// Why: the shared resolver's stage-1 falls through to OpenRouter when a
    /// family key is absent; for an explicit tcode fireworks route that would be
    /// a wrong-provider misroute, so `build_adapter` turns it into a clear error.
    /// What: with an empty store (and, when present locally, a real
    /// `FIREWORKS_API_KEY` shadowing it, in which case skip), assert
    /// `MissingConfig`.
    /// Test: this test.
    #[tokio::test]
    async fn missing_fireworks_key_errors_not_falls_back() {
        if std::env::var("FIREWORKS_API_KEY").is_ok_and(|v| !v.is_empty()) {
            return; // real key present locally — the fall-back guard isn't exercised
        }
        let client = OpenAiCompatClient::with_store(Box::new(MemoryKeyStore::new()));
        let req = ChatRequest {
            model: "fireworks/accounts/fireworks/models/llama-v3p1-8b-instruct".into(),
            messages: vec![],
            temperature: None,
            max_tokens: None,
            tools: None,
            tool_choice: None,
            usage: None,
        };
        let err = client
            .chat(&req)
            .await
            .expect_err("must fail without a key");
        match err {
            LlmError::MissingConfig(msg) => {
                assert!(
                    msg.contains("FIREWORKS_API_KEY"),
                    "unhelpful message: {msg}"
                )
            }
            other => panic!("expected MissingConfig naming FIREWORKS_API_KEY, got: {other:?}"),
        }
    }

    /// A `together/*` request with no `TOGETHER_API_KEY` fails with an explicit
    /// `MissingConfig` — it must NOT silently fall back to OpenRouter (#2494).
    ///
    /// Why: the shared resolver's stage-1 falls through to OpenRouter when a
    /// family key is absent; for an explicit tcode together route that would be a
    /// wrong-provider misroute, so `build_adapter` turns it into a clear error —
    /// the same contract as Fireworks.
    /// What: with an empty store (and, when present locally, a real
    /// `TOGETHER_API_KEY` shadowing it, in which case skip), assert
    /// `MissingConfig` naming `TOGETHER_API_KEY`.
    /// Test: this test.
    #[tokio::test]
    async fn missing_together_key_errors_not_falls_back() {
        if std::env::var("TOGETHER_API_KEY").is_ok_and(|v| !v.is_empty()) {
            return; // real key present locally — the fall-back guard isn't exercised
        }
        let client = OpenAiCompatClient::with_store(Box::new(MemoryKeyStore::new()));
        let req = ChatRequest {
            model: "together/meta-llama/Llama-3.3-70B-Instruct-Turbo".into(),
            messages: vec![],
            temperature: None,
            max_tokens: None,
            tools: None,
            tool_choice: None,
            usage: None,
        };
        let err = client
            .chat(&req)
            .await
            .expect_err("must fail without a key");
        match err {
            LlmError::MissingConfig(msg) => {
                assert!(msg.contains("TOGETHER_API_KEY"), "unhelpful message: {msg}")
            }
            other => panic!("expected MissingConfig naming TOGETHER_API_KEY, got: {other:?}"),
        }
    }

    /// An `atlascloud/*` request with no `ATLASCLOUD_API_KEY` fails with an
    /// explicit `MissingConfig` — it must NOT silently fall back to OpenRouter
    /// (#2536).
    ///
    /// Why: the shared resolver's stage-1 falls through to OpenRouter when a
    /// family key is absent; for an explicit tcode atlascloud route that would be
    /// a wrong-provider misroute, so `build_adapter` turns it into a clear error —
    /// the same contract as Fireworks and Together.
    /// What: with an empty store (and, when present locally, a real
    /// `ATLASCLOUD_API_KEY` shadowing it, in which case skip), assert
    /// `MissingConfig` naming `ATLASCLOUD_API_KEY`.
    /// Test: this test.
    #[tokio::test]
    async fn missing_atlascloud_key_errors_not_falls_back() {
        if std::env::var("ATLASCLOUD_API_KEY").is_ok_and(|v| !v.is_empty()) {
            return; // real key present locally — the fall-back guard isn't exercised
        }
        let client = OpenAiCompatClient::with_store(Box::new(MemoryKeyStore::new()));
        let req = ChatRequest {
            model: "atlascloud/openai/gpt-5.6-sol".into(),
            messages: vec![],
            temperature: None,
            max_tokens: None,
            tools: None,
            tool_choice: None,
            usage: None,
        };
        let err = client
            .chat(&req)
            .await
            .expect_err("must fail without a key");
        match err {
            LlmError::MissingConfig(msg) => {
                assert!(
                    msg.contains("ATLASCLOUD_API_KEY"),
                    "unhelpful message: {msg}"
                )
            }
            other => panic!("expected MissingConfig naming ATLASCLOUD_API_KEY, got: {other:?}"),
        }
    }

    /// Live integration: a real OpenRouter round-trip via the shared adapter.
    ///
    /// Why: end-to-end validation (ported from the pre-#2406 `live_openrouter_call`)
    /// that tcode's transport, now on the shared adapter, still produces a
    /// non-empty reply and non-zero usage.
    /// What: reads `OPENROUTER_API_KEY`; SKIPS (does not fail) when absent.
    /// Test: `cargo test -p trusty-code -- --include-ignored live_openrouter`.
    #[tokio::test]
    #[ignore = "requires OPENROUTER_API_KEY; skipped in CI"]
    async fn live_openrouter_call() {
        let Ok(key) = std::env::var("OPENROUTER_API_KEY") else {
            eprintln!("OPENROUTER_API_KEY not set — skipping live test");
            return;
        };
        if key.is_empty() {
            eprintln!("OPENROUTER_API_KEY is empty — skipping live test");
            return;
        }
        let client = OpenAiCompatClient::new();
        let req = ChatRequest {
            model: "openai/gpt-4o-mini".into(),
            messages: vec![
                super::super::ChatMessage::system("You are a concise assistant."),
                super::super::ChatMessage::user("Reply with exactly the word: pong"),
            ],
            temperature: Some(0.0),
            max_tokens: Some(16),
            tools: None,
            tool_choice: None,
            usage: None,
        };
        let resp = client.chat(&req).await.expect("chat call succeeded");
        let text = resp.first_text().expect("assistant produced text");
        assert!(!text.is_empty(), "assistant text was empty");
        assert!(
            resp.token_usage().prompt_tokens > 0,
            "prompt_tokens should be > 0"
        );
        eprintln!("live openrouter ok — text: {text:?}");
    }

    /// Live integration: a real Fireworks round-trip via the shared adapter
    /// (the concrete payoff of #2406).
    ///
    /// Why: proves `fireworks/*` routing + prefix stripping + `FIREWORKS_API_KEY`
    /// resolution work against the real API.
    /// What: reads `FIREWORKS_API_KEY`; SKIPS when absent.
    /// Test: `cargo test -p trusty-code -- --include-ignored live_fireworks`.
    #[tokio::test]
    #[ignore = "requires FIREWORKS_API_KEY; skipped in CI"]
    async fn live_fireworks_call() {
        let Ok(key) = std::env::var("FIREWORKS_API_KEY") else {
            eprintln!("FIREWORKS_API_KEY not set — skipping live test");
            return;
        };
        if key.trim().is_empty() {
            eprintln!("FIREWORKS_API_KEY is empty — skipping live test");
            return;
        }
        let client = OpenAiCompatClient::new();
        let req = ChatRequest {
            model: "fireworks/accounts/fireworks/models/llama-v3p1-8b-instruct".into(),
            messages: vec![
                super::super::ChatMessage::system("You are a concise assistant."),
                super::super::ChatMessage::user("Reply with exactly the word: pong"),
            ],
            temperature: Some(0.0),
            max_tokens: Some(16),
            tools: None,
            tool_choice: None,
            usage: None,
        };
        let resp = client.chat(&req).await.expect("chat call succeeded");
        let text = resp.first_text().expect("assistant produced text");
        assert!(!text.is_empty(), "assistant text was empty");
        assert!(
            resp.token_usage().prompt_tokens > 0,
            "prompt_tokens should be > 0"
        );
        eprintln!("live fireworks ok — text: {text:?}");
    }

    /// Live integration: a real Together round-trip via the shared adapter
    /// (the concrete payoff of #2494).
    ///
    /// Why: proves `together/*` routing + prefix stripping + `TOGETHER_API_KEY`
    /// resolution work against the real API.
    /// What: reads `TOGETHER_API_KEY`; SKIPS when absent.
    /// Test: `cargo test -p trusty-code -- --include-ignored live_together`.
    #[tokio::test]
    #[ignore = "requires TOGETHER_API_KEY; skipped in CI"]
    async fn live_together_call() {
        let Ok(key) = std::env::var("TOGETHER_API_KEY") else {
            eprintln!("TOGETHER_API_KEY not set — skipping live test");
            return;
        };
        if key.trim().is_empty() {
            eprintln!("TOGETHER_API_KEY is empty — skipping live test");
            return;
        }
        let client = OpenAiCompatClient::new();
        let req = ChatRequest {
            model: "together/meta-llama/Llama-3.3-70B-Instruct-Turbo".into(),
            messages: vec![
                super::super::ChatMessage::system("You are a concise assistant."),
                super::super::ChatMessage::user("Reply with exactly the word: pong"),
            ],
            temperature: Some(0.0),
            max_tokens: Some(16),
            tools: None,
            tool_choice: None,
            usage: None,
        };
        let resp = client.chat(&req).await.expect("chat call succeeded");
        let text = resp.first_text().expect("assistant produced text");
        assert!(!text.is_empty(), "assistant text was empty");
        assert!(
            resp.token_usage().prompt_tokens > 0,
            "prompt_tokens should be > 0"
        );
        eprintln!("live together ok — text: {text:?}");
    }
}