diff --git a/cmd/archied/main.go b/cmd/archied/main.go index bba00c40e..ce454ee78 100644 --- a/cmd/archied/main.go +++ b/cmd/archied/main.go @@ -162,6 +162,50 @@ func configuredMCPProvider(server config.MCPServer) (toolprovider.Engine, error) } } +// resolveForge builds the forge client for one forge configuration, returning +// the resolved token alongside it for the worktree manager. +// +// A missing or unusable credential disables the forge; it does not stop the +// daemon. The forge is one feature among many, and killing the process denies +// the operator chat, the gateway and every other subsystem over a capability +// they may not use at all -- under a systemd unit with Restart=on-failure that +// becomes a crash loop where the error scrolls past unread. A malformed +// configuration is a different matter and still fails fast in validation, well +// before this point. +func resolveForge(cfg config.Forge, secrets *secret.Registry, log *slog.Logger) (forge.Forge, string) { + if isForgeDisabled(cfg.Type) { + return forge.NewNoop(log), "" + } + token, err := cfg.Token.Resolve(secrets) + if err != nil || token == "" { + log.Warn("forge disabled: token unavailable", + "forge_type", cfg.Type, + "engine", cfg.Token.Engine, + "key", cfg.Token.Key, + "err", err) + return forge.NewNoop(log), "" + } + client, err := forge.New(cfg.Type, token, cfg.Host, log) + if err != nil { + log.Warn("forge disabled: client construction failed", + "forge_type", cfg.Type, "err", err) + return forge.NewNoop(log), "" + } + return client, token +} + +// isForgeDisabled reports whether a forge type explicitly opts out of forge +// integration. It mirrors the identically named check in the configuration +// package, which is unexported there. +func isForgeDisabled(forgeType string) bool { + switch forgeType { + case "none", "off", "disabled": + return true + default: + return false + } +} + func run() int { defaultCfg := filepath.Join(configHome(), "archie", "config.toml") cfgPath := flag.String("config", defaultCfg, "path to config.toml") @@ -180,24 +224,7 @@ func run() int { cfg := doc.Config secrets := secret.NewRegistry() - var forgeClient forge.Forge - var token string - if cfg.Forge.Type == "none" || cfg.Forge.Type == "off" || cfg.Forge.Type == "disabled" { - forgeClient = forge.NewNoop(log) - } else { - var errToken error - token, errToken = cfg.Forge.Token.Resolve(secrets) - if errToken != nil || token == "" { - fmt.Fprintf(os.Stderr, "forge token (engine %q, key %q) is required: %v\n", cfg.Forge.Token.Engine, cfg.Forge.Token.Key, errToken) - return 1 - } - var errForge error - forgeClient, errForge = forge.New(cfg.Forge.Type, token, cfg.Forge.Host, log) - if errForge != nil { - fmt.Fprintln(os.Stderr, errForge) - return 1 - } - } + forgeClient, token := resolveForge(cfg.Forge, secrets, log) st, err := nell.OpenStore(cfg.DBPath, cfg.BotUser) if err != nil { @@ -466,24 +493,10 @@ func run() int { // and Run() takes the legacy single-identity path unchanged. var identityRunners []*daemon.IdentityRunner for _, idCfg := range cfg.Identities { - var idForge forge.Forge - var idToken string - if idCfg.Forge.Type == "none" || idCfg.Forge.Type == "off" || idCfg.Forge.Type == "disabled" { - idForge = forge.NewNoop(log) - } else { - var errToken error - idToken, errToken = idCfg.Forge.Token.Resolve(secrets) - if errToken != nil || idToken == "" { - log.Error("identity forge token unresolved", "identity", idCfg.Name, "err", errToken) - return 1 - } - var errForge error - idForge, errForge = forge.New(idCfg.Forge.Type, idToken, idCfg.Forge.Host, log) - if errForge != nil { - log.Error("identity forge client failed", "identity", idCfg.Name, "err", errForge) - return 1 - } - } + // Same reasoning as the primary forge: one identity whose credential is + // missing must not deny every other identity, and every other + // subsystem, the ability to run. + idForge, idToken := resolveForge(idCfg.Forge, secrets, log.With("identity", idCfg.Name)) idTrees := &worktree.Manager{ WorkDir: filepath.Join(cfg.WorkDir, "identity-"+idCfg.Name), Token: idToken, diff --git a/cmd/archied/main_test.go b/cmd/archied/main_test.go index 900ee1201..0d57bc5fa 100644 --- a/cmd/archied/main_test.go +++ b/cmd/archied/main_test.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "io" "log/slog" "os" "path/filepath" @@ -21,6 +22,7 @@ import ( "github.com/samcharles93/archie-core/internal/forgerpc" "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/nell" + "github.com/samcharles93/archie-core/internal/secret" "github.com/samcharles93/archie-core/internal/store" "github.com/samcharles93/archie-core/internal/storerpc" "github.com/samcharles93/archie-core/internal/tools" @@ -28,6 +30,82 @@ import ( "github.com/samcharles93/archie-core/internal/worktreerpc" ) +// TestResolveForgeDegradesInsteadOfFailing guards a startup behaviour change: +// a missing forge credential used to abort archied with exit 1. The forge is +// one feature among many, so that denied the operator chat, the gateway and +// every other subsystem -- and under the installed systemd unit +// (Restart=on-failure, RestartSec=5s) it crash-looped where the error was +// never read. A missing credential must disable the forge, not the daemon. +func TestResolveForgeDegradesInsteadOfFailing(t *testing.T) { + t.Parallel() + + log := slog.New(slog.NewTextHandler(io.Discard, nil)) + + tests := []struct { + name string + cfg config.Forge + }{ + { + name: "explicitly disabled", + cfg: config.Forge{Type: "none"}, + }, + { + name: "token refers to an unset environment variable", + cfg: config.Forge{ + Type: "github", + Host: "https://github.com", + Token: secret.SecretRef{Engine: "env", Key: "ARCHIE_TEST_TOKEN_DEFINITELY_UNSET"}, + }, + }, + { + name: "token reference is empty", + cfg: config.Forge{Type: "github", Host: "https://github.com"}, + }, + } + + for _, tc := range tests { + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + client, token := resolveForge(tc.cfg, secret.NewRegistry(), log) + if client == nil { + t.Fatal("resolveForge returned no forge client; startup would nil-panic") + } + if token != "" { + t.Errorf("token = %q, want empty when the forge is disabled", token) + } + // The no-op forge answers rather than erroring, which is what lets + // the rest of the daemon run against it unchanged. + issues, err := client.IssuesWithLabel(t.Context(), "owner", "repo", "archie") + if err != nil { + t.Errorf("disabled forge returned an error instead of no work: %v", err) + } + if len(issues) != 0 { + t.Errorf("disabled forge returned %d issues, want 0", len(issues)) + } + }) + } +} + +// Not parallel: t.Setenv mutates process state and panics under t.Parallel. +func TestResolveForgeBuildsClientWhenTokenResolves(t *testing.T) { + t.Setenv("ARCHIE_TEST_FORGE_TOKEN", "ghp_exampletoken") + log := slog.New(slog.NewTextHandler(io.Discard, nil)) + + client, token := resolveForge(config.Forge{ + Type: "github", + Host: "https://github.com", + Token: secret.SecretRef{Engine: "env", Key: "ARCHIE_TEST_FORGE_TOKEN"}, + }, secret.NewRegistry(), log) + + if client == nil { + t.Fatal("resolveForge returned no forge client") + } + if token != "ghp_exampletoken" { + t.Errorf("token = %q, want the resolved value passed to the worktree manager", token) + } +} + func TestChatGenerateOptionsIncludesToolsAndMultipleSteps(t *testing.T) { registry := tools.NewRegistry() if err := registry.Register(tools.ToolEntry{ diff --git a/install.sh b/install.sh index c30c8119b..061690f04 100755 --- a/install.sh +++ b/install.sh @@ -424,6 +424,11 @@ if [ "${INSTALL_SYSTEMD}" = true ] && command -v systemctl &>/dev/null && [ -d " [Unit] Description=Archie Core Orchestrator Daemon After=network.target +# Give up after 5 failures in 5 minutes rather than restarting forever. A +# genuinely broken install otherwise loops every RestartSec indefinitely, +# burying the real error in the journal instead of surfacing a failed unit. +StartLimitIntervalSec=300 +StartLimitBurst=5 [Service] Type=simple