From a856e5ac8097ea077eca2043dbadb9e4c8e2e6a7 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 03:28:35 +1000 Subject: [PATCH] fix(archied): disable a provider whose credential is missing instead of refusing to boot configuration.md's startup policy -- "a missing credential disables a capability. Only an invalid config stops the daemon" -- names this exact case: "Apply the same reasoning to any future capability credential, LLM keys especially: start, report the capability as unavailable, let the operator see it." The code did the opposite. resolveProviderMap returned an error when a provider's api_key could not be resolved, and again when it resolved empty, so configuredSecretRegistry failed and the daemon would not start. A provider nobody referenced still blocked boot, because the walk covers every entry in cfg.Providers rather than the ones [models] uses. The forge path was fixed to degrade for precisely this reason (resolveForge, guarded by TestResolveForgeDegradesInsteadOfFailing); this is its provider counterpart. A credential that cannot be resolved now disables that provider with a warning naming it and why, and the daemon starts. Disabling clears the reference rather than leaving it. webui's ProviderView derives Configured from api_key or APIKeyEnv, so an unresolvable reference left in place would advertise a provider nothing can use -- the "looks configured and is not" shape this codebase keeps having to remove. Consumers already treat an empty APIKeyEnv as no credential (agentexec/runtime skips such a provider, the daemon exports nothing to the agent container), so clearing it is the whole disable. The other half of the policy is kept: a reference naming only one of engine and key is a config error, not a missing credential, and stays fatal. applyForgeDefaults only ever builds a ref with both halves, so a half-named one can only come from a hand-edited config. This also defuses the trap 027dc99 introduced. archied setup can now write a provider reference to any engine (-provider-secret-ref bws:OPENAI_API_KEY), and on a machine without that engine the config used to load clean and then brick the daemon. It now starts with that provider disabled and a warning. The comment on templateDefaultActiveProvider described the old fatal behaviour and is corrected rather than left to mislead the next reader. All three behaviours are mutation-verified: returning an error again, accepting a half-named reference, and disabling without clearing the reference each fail the corresponding test. The red was an assertion failure, not a compile error -- the logger parameter was threaded through first so the new test could be run against unchanged behaviour. --- internal/app/archied/main_test.go | 100 +++++++++++++++++- internal/app/archied/provider_secrets.go | 58 ++++++++-- .../configuration/setup/step_provider.go | 15 ++- 3 files changed, 152 insertions(+), 21 deletions(-) diff --git a/internal/app/archied/main_test.go b/internal/app/archied/main_test.go index 114f72d28..84520bbc9 100644 --- a/internal/app/archied/main_test.go +++ b/internal/app/archied/main_test.go @@ -181,7 +181,7 @@ func TestResolveProviderSecretSetsPrivateRuntimeEnvironment(t *testing.T) { }, }} - if err := resolveProviderSecrets(&cfg, registry); err != nil { + if err := resolveProviderSecrets(&cfg, registry, slog.New(slog.DiscardHandler)); err != nil { t.Fatal(err) } provider := cfg.Providers["openai"] @@ -216,7 +216,7 @@ func TestResolveProviderSecretsIsolatesIdentityCredentials(t *testing.T) { }, } - if err := resolveProviderSecrets(&cfg, registry); err != nil { + if err := resolveProviderSecrets(&cfg, registry, slog.New(slog.DiscardHandler)); err != nil { t.Fatal(err) } names := []string{ @@ -1249,3 +1249,99 @@ func TestSubscribeAgentEventsFlushesBeforeReturning(t *testing.T) { t.Fatal("subscribeAgentEvents does not flush its NATS subscription before returning") } } + +// TestResolveProviderSecretsDegradesInsteadOfFailing pins the startup policy for +// LLM credentials. configuration.md's "missing credential degrades, invalid +// config is fatal" names LLM keys as the case that must behave this way, and +// holds up the forge path as the precedent: a token that cannot resolve disables +// the capability and the daemon still starts. This is its provider counterpart. +// +// A provider whose key cannot be resolved is disabled rather than optional: +// webui's ProviderView reports Configured from the reference, so leaving an +// unresolvable one in place would advertise a provider nothing can use. +func TestResolveProviderSecretsDegradesInsteadOfFailing(t *testing.T) { + tests := []struct { + name string + ref secret.SecretRef + registry *secret.Registry // nil means a default registry + wantLog string + }{ + { + name: "the engine is not registered", + ref: secret.SecretRef{Engine: "not-an-engine", Key: "OPENAI_API_KEY"}, + wantLog: "api_key unavailable", + }, + { + name: "the key is unset in the env engine", + ref: secret.SecretRef{Engine: "env", Key: "ARCHIE_TEST_PROVIDER_KEY_DEFINITELY_UNSET"}, + wantLog: "api_key unavailable", + }, + { + // A registered engine that answers nothing, which is how a + // Yaegi-loaded engine whose exported value omits Resolve behaves + // (see secret.Engine's doc): present, registered, empty. + name: "the engine resolves every key to empty", + ref: secret.SecretRef{Engine: "empty", Key: "OPENAI_API_KEY"}, + registry: registryWithEmptyEngine(), + wantLog: "api_key resolved empty", + }, + } + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + cfg := config.Config{Providers: map[string]config.Provider{ + "acme": {Class: "openai", APIKey: tc.ref}, + }} + registry := tc.registry + if registry == nil { + registry = secret.NewRegistry() + } + rec := &recordingHandler{} + if err := resolveProviderSecrets(&cfg, registry, slog.New(rec)); err != nil { + t.Fatalf("resolveProviderSecrets = %v, want nil: a missing credential must disable the provider, not stop the daemon", err) + } + got := cfg.Providers["acme"] + if got.APIKey != (secret.SecretRef{}) { + t.Errorf("provider api_key = %+v, want the unresolvable reference cleared", got.APIKey) + } + if got.APIKeyEnv != "" { + t.Errorf("provider api_key_env = %q, want empty: nothing was exported", got.APIKeyEnv) + } + warns := rec.Warnings() + if len(warns) != 1 || !strings.Contains(warns[0], tc.wantLog) { + t.Errorf("warnings = %v, want exactly one containing %q", warns, tc.wantLog) + } + }) + } +} + +// TestResolveProviderSecretsRejectsAHalfNamedRef pins the other half of the +// policy: a reference naming only one of engine and key is a config error rather +// than a missing credential, and an invalid config still stops the daemon. +func TestResolveProviderSecretsRejectsAHalfNamedRef(t *testing.T) { + for _, ref := range []secret.SecretRef{ + {Engine: "env"}, + {Key: "OPENAI_API_KEY"}, + } { + cfg := config.Config{Providers: map[string]config.Provider{ + "acme": {Class: "openai", APIKey: ref}, + }} + if err := resolveProviderSecrets(&cfg, secret.NewRegistry(), slog.New(slog.DiscardHandler)); err == nil { + t.Errorf("resolveProviderSecrets with api_key %+v = nil, want an error", ref) + } + } +} + +// emptyEngine resolves every key to an empty value with no error, standing in for +// a Yaegi-loaded engine whose exported value omits Resolve (see secret.Engine's +// doc): registered, present, and answering nothing. +type emptyEngine struct{} + +func (emptyEngine) Name() string { return "empty" } +func (emptyEngine) Version() string { return "0.0.0" } +func (emptyEngine) Resolve(string) (string, error) { return "", nil } + +func registryWithEmptyEngine() *secret.Registry { + r := secret.NewRegistry() + r.Register(emptyEngine{}) + return r +} diff --git a/internal/app/archied/provider_secrets.go b/internal/app/archied/provider_secrets.go index 90e643a22..a2207553c 100644 --- a/internal/app/archied/provider_secrets.go +++ b/internal/app/archied/provider_secrets.go @@ -30,40 +30,61 @@ func configuredSecretRegistry(cfg *config.Config, log *slog.Logger) (*secret.Reg } log.Info("secret engines loaded", "count", loaded) } - if err := resolveProviderSecrets(cfg, registry); err != nil { + if err := resolveProviderSecrets(cfg, registry, log); err != nil { return nil, fmt.Errorf("resolve provider secrets: %w", err) } return registry, nil } -func resolveProviderSecrets(cfg *config.Config, registry *secret.Registry) error { - if err := resolveProviderMap("root", cfg.Providers, registry); err != nil { +func resolveProviderSecrets(cfg *config.Config, registry *secret.Registry, log *slog.Logger) error { + if err := resolveProviderMap("root", cfg.Providers, registry, log); err != nil { return err } for i := range cfg.Identities { - if err := resolveProviderMap(cfg.Identities[i].Name, cfg.Identities[i].Providers, registry); err != nil { + if err := resolveProviderMap(cfg.Identities[i].Name, cfg.Identities[i].Providers, registry, log); err != nil { return fmt.Errorf("identity %q: %w", cfg.Identities[i].Name, err) } } return nil } -func resolveProviderMap(scope string, providers map[string]config.Provider, registry *secret.Registry) error { +// resolveProviderMap exports each provider's resolved api_key into a private +// environment variable and rewrites the provider to reference that variable, so +// a key never travels as config data. +// +// A credential that cannot be resolved disables that provider and is reported, +// rather than failing the boot. configuration.md's startup policy is that a +// missing credential disables a capability and only an invalid config stops the +// daemon, and it names LLM keys as the case that must behave that way; the forge +// path was fixed to degrade for the same reason (resolveForge). +// +// A reference naming only one of engine and key is a config error rather than a +// missing credential, so that stays fatal. applyForgeDefaults only ever builds a +// ref with both halves, so a half-named one can only come from a hand-edited +// config. +func resolveProviderMap(scope string, providers map[string]config.Provider, registry *secret.Registry, log *slog.Logger) error { for id, provider := range providers { - if provider.APIKey == (secret.SecretRef{}) { + switch { + case provider.APIKey == (secret.SecretRef{}): continue + case provider.APIKey.Engine == "" || provider.APIKey.Key == "": + return fmt.Errorf("provider %q: api_key must name both an engine and a key, got {engine: %q, key: %q}", + id, provider.APIKey.Engine, provider.APIKey.Key) } + value, err := registry.Resolve(provider.APIKey) if err != nil { - return fmt.Errorf("provider %q: %w", id, err) + disableProvider(providers, id, provider, log, "api_key unavailable", err) + continue } - value = strings.TrimSpace(value) - if value == "" { - return fmt.Errorf("provider %q: resolved api_key is empty", id) + if value = strings.TrimSpace(value); value == "" { + disableProvider(providers, id, provider, log, "api_key resolved empty", nil) + continue } envName := providerSecretEnvName(scope, id) if err := os.Setenv(envName, value); err != nil { - return fmt.Errorf("provider %q: export resolved api_key: %w", id, err) + disableProvider(providers, id, provider, log, "api_key could not be exported", err) + continue } provider.APIKey = secret.SecretRef{} provider.APIKeyEnv = envName @@ -72,6 +93,21 @@ func resolveProviderMap(scope string, providers map[string]config.Provider, regi return nil } +// disableProvider clears a provider's credential so every consumer sees it as +// unconfigured, and says why. The reference is cleared rather than left in +// place: webui's ProviderView derives Configured from it, so keeping an +// unresolvable reference would advertise a provider that cannot be used. +func disableProvider(providers map[string]config.Provider, id string, provider config.Provider, log *slog.Logger, reason string, cause error) { + provider.APIKey = secret.SecretRef{} + provider.APIKeyEnv = "" + providers[id] = provider + if cause != nil { + log.Warn("provider disabled: "+reason, "provider", id, "err", cause) + return + } + log.Warn("provider disabled: "+reason, "provider", id) +} + func providerSecretEnvName(scope, providerID string) string { sum := sha256.Sum256([]byte(scope + "\x00" + providerID)) return fmt.Sprintf("ARCHIE_PROVIDER_%X_API_KEY", sum[:8]) diff --git a/internal/infrastructure/configuration/setup/step_provider.go b/internal/infrastructure/configuration/setup/step_provider.go index 378268cfc..408495976 100644 --- a/internal/infrastructure/configuration/setup/step_provider.go +++ b/internal/infrastructure/configuration/setup/step_provider.go @@ -35,14 +35,13 @@ var cloudProviders = []cloudProvider{ // templateDefaultActiveProvider is the one provider table config.example. // toml ships active by default: [providers.openai], with // api_key = {engine="bws", key="OPENAI_API_KEY"}. bws is compiled in but -// requires the bws CLI on PATH; on a machine without it (the common case), -// resolving that key errors, and cmd/archied/provider_secrets.go's -// resolveProviderSecrets walks every entry in cfg.Providers -- not just -// the ones [models] actually references -- and refuses to start the -// daemon at all if any of them fails to resolve. Choosing any provider -// other than OpenAI, or choosing OpenAI but leaving its key blank, must -// neutralise this table rather than leave it as a silent boot-time -// landmine nothing in setup's own output would explain. +// requires the bws CLI on PATH; on a machine without it (the common case) +// resolving that key fails, and resolveProviderSecrets disables the provider +// with a warning rather than stopping the daemon. Neutralising the table is +// still setup's business: a provider the operator never chose should not be +// left advertised and unusable, with a warning they did not cause. Choosing any +// provider other than OpenAI, or choosing OpenAI but leaving its key blank, +// therefore replaces this table. const templateDefaultActiveProvider = "openai" func stepProvider(ctx context.Context, p Prompter, discovery ModelDiscovery, secrets SecretSink, params Params) (tableEdits, string, error) {