Skip to main content

forge_workspace/
provider_probe.rs

1//! Workspace-side routing of account probes through the
2//! forge-gateway backends, shared by the boot loader and the 60 s
3//! usage poller.
4
5use std::collections::HashMap;
6
7use forge_agent::cloud::AgentHost;
8use forge_gateway::{AccountEnv, ProbeError, ProviderBackend, UsageSnapshot};
9use forge_primitives::account::Provider;
10
11pub(crate) fn backend_for(provider: Provider) -> Result<&'static dyn ProviderBackend, ProbeError> {
12    if let Some(backend) = forge_gateway::backend(provider) {
13        return Ok(backend);
14    }
15    debug_assert!(
16        false,
17        "every provider the probe arms reach must have a backend registered; {provider:?}"
18    );
19    Err(ProbeError::Unmappable(format!("no backend registered for {provider:?}")))
20}
21
22/// One probe round-trip through the account's backend and the shared
23/// host.
24pub(crate) async fn probe_via_backend(
25    provider: Provider,
26    env: &HashMap<String, String>,
27) -> Result<UsageSnapshot, ProbeError> {
28    let backend = backend_for(provider)?;
29    let account_env = AccountEnv { env };
30    backend.probe(&account_env, &AgentHost).await
31}
32
33#[cfg(test)]
34mod tests {
35    use super::*;
36
37    /// The registry holds every token the anthropic-shaped arms can
38    /// reach; a missing registration is the one error this module
39    /// invents, and it must never surface for Anthropic.
40    #[test]
41    fn anthropic_backend_always_resolves() {
42        assert!(backend_for(Provider::Anthropic).is_ok());
43    }
44}