Files
archie/internal/config/model_providers.go
T
sam ae178f1be0 fix(egress): bind a task's model providers from their api_key_ref
A container task parked when a provider carried an api_key_env but no
credential binding named after it, even though the task's models never
reached that provider. Egress pre-bound every provider the instance
configures and ignored api_key_ref, so the credentials an operator stores in
provider-settings were never used.

A task's config now carries only the providers its model aliases resolve to,
each with the variable its credential is read from: the operator's
api_key_env, or a name derived from the provider when its credential is a
secret reference. modelEgress resolves that reference once for the session
and the proxy substitutes the session's key for the sentinel, so the
container still holds no key. A provider that only declares an env var keeps
resolving through its org credential binding.
2026-10-20 06:56:38 +11:00

53 lines
1.6 KiB
Go

package config
import (
"crypto/sha256"
"fmt"
"strings"
)
// ProviderKeyEnvName is the variable a run reads provider id's credential from
// when the provider names no api_key_env. It is derived from the id alone, so
// the daemon that resolves the credential and the container that holds only its
// sentinel agree on the name without the key travelling.
func ProviderKeyEnvName(id string) string {
sum := sha256.Sum256([]byte("provider\x00" + id))
return fmt.Sprintf("ARCHIE_PROVIDER_%X_API_KEY", sum[:8])
}
// KeyEnv is the variable a run reads provider id's credential from: its
// api_key_env, or the name derived for a provider that carries only a secret
// reference. A provider with neither holds no credential and names no variable.
func (p Provider) KeyEnv(id string) string {
switch {
case p.APIKeyEnv != "":
return p.APIKeyEnv
case p.APIKey == (SecretRef{}):
return ""
default:
return ProviderKeyEnvName(id)
}
}
// ModelProviders returns c's providers that one of its model aliases resolves
// to, each carrying the variable its credential is read from: the operator's
// api_key_env, or a name derived from the provider when its credential is a
// secret reference. A provider no alias names is left out, so nothing requires
// a credential for a provider a run never reaches.
func (c Config) ModelProviders() map[string]Provider {
out := make(map[string]Provider, len(c.Providers))
for _, ref := range c.Models {
id, _, ok := strings.Cut(ref, "/")
if !ok {
continue
}
provider, ok := c.Providers[id]
if !ok {
continue
}
provider.APIKeyEnv = provider.KeyEnv(id)
out[id] = provider
}
return out
}