diff --git a/deployment_contract_test.go b/deployment_contract_test.go index 316d209ee..716267cef 100644 --- a/deployment_contract_test.go +++ b/deployment_contract_test.go @@ -2,8 +2,6 @@ package configtemplate import ( "os" - "path/filepath" - "regexp" "strings" "testing" @@ -37,85 +35,6 @@ func TestExternalNATSProfileLoadsWithManagedWorkers(t *testing.T) { } } -// heredocRe matches any `cat < Generating initial config.toml..." - if [ "${SELF_HOST_LLM}" = true ] && [ -n "${OLLAMA_MODEL}" ]; then - cat < "${ARCHIE_CONFIG_DIR}/config.toml" -# archied configuration generated by install.sh -poll_interval = "60s" -label = "archie" -bot_user = "archie-bot" - -$(forge_block) - -[models] -triage = "ollama/${OLLAMA_MODEL}" -planner = "ollama/${OLLAMA_MODEL}" -builder = "ollama/${OLLAMA_MODEL}" - -[providers.ollama] -class = "ollama" - -[nats] -mode = "embedded" - -# The daemon and gateway dial the standalone archie-state-store process and -# the standalone archie-gateway (the dashboard and chat front-ends dial it -# too). Loopback targets with no token are the no-setup default; see -# deployments/README.md for starting archie-state-store and archie-gateway. -[services.gateway] -target = "127.0.0.1:8585" - -[services.state] -target = "127.0.0.1:9090" - -[containers] -image = "ghcr.io/samcharles93/archie-agent:latest" -pull_policy = "missing" - -[budgets] -max_steps = 60 -wall_clock = "30m" -EOF + setup_args=(--defaults -config "${ARCHIE_CONFIG_DIR}/config.toml" -forge-type "${FORGE_TYPE}") + case "${FORGE_TYPE}" in + github) + setup_args+=(-forge-secret-ref env:ARCHIE_GITHUB_TOKEN) + ;; + gitea) + setup_args+=(-forge-host "${GITEA_URL}" -forge-secret-ref env:ARCHIE_GITEA_TOKEN) + ;; + esac + if [ "${SELF_HOST_LLM}" = true ]; then + setup_args+=(-provider ollama -model "${OLLAMA_MODEL}") else - # Replace the example's [forge] block wholesale rather than patching - # individual lines: seddding type and host still left the GitHub token key - # behind, so a Gitea install read a key the installer never wrote. - forge_block > "${ARCHIE_CONFIG_DIR}/.forge.block" - awk ' - /^\[forge\]/ { skip = 1; while ((getline line < "'"${ARCHIE_CONFIG_DIR}"'/.forge.block") > 0) print line; next } - /^\[/ { skip = 0 } - !skip - ' "${SRC_DIR}/config.example.toml" > "${ARCHIE_CONFIG_DIR}/config.toml" - rm -f "${ARCHIE_CONFIG_DIR}/.forge.block" + # Reference the key in the env file, so the config keeps working if the + # operator adds it later. With no key present archied disables that provider + # and says so, rather than leaving the daemon unable to start. + setup_args+=(-provider openai -model "${CLOUD_MODEL}" -provider-secret-ref env:OPENAI_API_KEY) + fi + if ! "${ARCHIE_BIN_DIR}/archied" setup "${setup_args[@]}"; then + echo "ERROR: archied setup could not generate ${ARCHIE_CONFIG_DIR}/config.toml" >&2 + exit 1 fi - chmod 600 "${ARCHIE_CONFIG_DIR}/config.toml" - echo " Created ${ARCHIE_CONFIG_DIR}/config.toml" else echo " [SKIP] ${ARCHIE_CONFIG_DIR}/config.toml already exists (preserving user config)." fi diff --git a/internal/app/archied/setup_test.go b/internal/app/archied/setup_test.go index a9eb6b537..c3348e084 100644 --- a/internal/app/archied/setup_test.go +++ b/internal/app/archied/setup_test.go @@ -153,12 +153,21 @@ func TestInstallValidatedConfig_ConfigWriteFailureLeavesNoSecret(t *testing.T) { } rendered := []byte("bot_user = \"widget\"\nwork_dir = \"/tmp/archie-setup-work\"\n") - err := installValidatedConfig(configuration.New(nil), cfgPath, sink, rendered) - if err == nil { - t.Fatal("installValidatedConfig = nil error, want a write failure") + + // Prove the fixture is loadable BEFORE relying on the write failing. + // Asserted by loading it rather than by matching the error text below: a + // reworded error must not silently disarm this guard and let the test pass + // for the wrong reason. + fixture := filepath.Join(dir, "fixture.toml") + if err := os.WriteFile(fixture, rendered, 0o600); err != nil { + t.Fatal(err) } - if strings.Contains(err.Error(), "does not load") { - t.Fatalf("fixture failed validation rather than the write, so this test never exercised the write path: %v", err) + if _, err := configuration.New(nil).File(fixture); err != nil { + t.Fatalf("fixture config does not load, so this test would not exercise the write path: %v", err) + } + + if err := installValidatedConfig(configuration.New(nil), cfgPath, sink, rendered); err == nil { + t.Fatal("installValidatedConfig = nil error, want a write failure") } if _, statErr := os.Stat(envPath); !os.IsNotExist(statErr) { t.Errorf("secret committed even though the config was never written: %v", statErr) @@ -237,3 +246,39 @@ func TestRunSetup_RejectsReferencesNothingWouldUse(t *testing.T) { }) } } + +// TestRunSetup_GeneratedConfigSatisfiesDaemonRequirements pins the properties +// archied needs of whatever setup emits, asserted on the GENERATED config. +// +// It replaces a test that read config.example.toml and reconstructed install.sh's +// shell heredoc. Testing hand-written files is testing an artifact a human or an +// agent may legitimately rewrite at any time -- and that test in fact broke the +// moment install.sh stopped generating config, because the heredoc it parsed no +// longer existed. The generated output is the contract; the template is an input. +func TestRunSetup_GeneratedConfigSatisfiesDaemonRequirements(t *testing.T) { + dir := t.TempDir() + cfgPath := filepath.Join(dir, "config.toml") + devNull, err := os.Open(os.DevNull) + if err != nil { + t.Fatal(err) + } + defer func() { _ = devNull.Close() }() + + var stdout, stderr strings.Builder + if code := RunSetup([]string{"--defaults", "-config", cfgPath}, devNull, &stdout, &stderr); code != 0 { + t.Fatalf("RunSetup exit = %d, stderr: %s", code, stderr.String()) + } + + doc, err := configuration.New(nil).File(cfgPath) + if err != nil { + t.Fatalf("generated config does not load: %v", err) + } + // openStateStoreAdapter refuses an empty target, so this is the difference + // between a config that boots and one that does not. + if doc.Config.Services.State.Target == "" { + t.Error("generated config leaves services.state.target empty; the daemon would not boot") + } + if doc.Config.BotUser == "" { + t.Error("generated config leaves bot_user empty") + } +}