From 0e64573b047ebc39f181f9d3026ca5b82b3e5ea7 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 15 Sep 2026 03:35:45 +1000 Subject: [PATCH] refactor(install): install.sh delegates config generation, and tests stop reading hand-written configs Two things fall out of one principle: a test that asserts against a hand-written file is testing an artifact a human or an agent may legitimately rewrite at any moment, so only generated output is a valid target. install.sh generated config.toml two ways -- a full heredoc for the self-host path and an awk splice of the [forge] block into config.example.toml -- with a forge_block shell helper feeding both. That is the arrangement docs/architecture/configuration.md decided against: it "must stop generating config.toml; archied setup owns it", because the installer hand-rolling TOML already drifted once (choosing Gitea produced a config naming a token the installer never wrote). Both generators and forge_block are gone; install.sh collects its answers as before and passes them to archied setup as parameters, with secrets passed as REFERENCES to the env keys it just wrote rather than as values, which would put them on a command line. The installer also now asks for a cloud model, which archied setup requires and the old path never emitted. deployment_contract_test.go tested the removed generators by parsing install.sh's shell text, reconstructing the heredoc and asserting on its content. It broke the instant install.sh stopped generating config -- "install.sh has no heredoc declaring the daemon config's [nats] table" -- which is the failure mode in miniature: the test was coupled to an artifact, not to behaviour. Deleted. What it actually protected is kept, asserted on the GENERATED config instead: TestRunSetup_GeneratedConfigSatisfiesDaemonRequirements runs archied setup and loads what it wrote, requiring services.state.target and bot_user. A test against generated output fails when generation breaks and passes when a human merely tidies the template. Found by an adversarial package sweep (temp tracker: config-1, repo-root-deployment-1, repo-root-deployment-2). --- deployment_contract_test.go | 81 -------------------- install.sh | 118 ++++++++++------------------- internal/app/archied/setup_test.go | 55 ++++++++++++-- 3 files changed, 90 insertions(+), 164 deletions(-) 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") + } +}