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).
This commit is contained in:
sam committed 2026-09-15 03:35:45 +10:00
1 parent a856e5ac80
commit 0e64573b04
3 files changed
+90 -164

No files matched your search

-81
View File
@@ -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 <<DELIM` heredoc opener in install.sh, tolerating
// <<- and a quoted delimiter. The redirect target is deliberately NOT matched:
// the config writer may name it inline, put the redirect before the delimiter,
// or factor the path into a variable, and none of those change what is written.
var heredocRe = regexp.MustCompile("(?m)^[^\\n]*\\bcat\\b[^\\n]*?<<-?[ \\t]*['\"]?([A-Za-z_][A-Za-z0-9_]*)['\"]?[ \\t]*")
// configHeredocs returns the body of every install.sh heredoc that declares the
// daemon config's [nats] table, which the systemd unit heredoc does not. The
// selector is a table the assertions below never touch, so it stays structural
// rather than becoming a search for the field under test.
//
// Command substitutions are dropped rather than expanded. Substituting a
// hand-written forge block here would duplicate install.sh's own forge_block
// output, so the test would assert against a copy that can silently diverge from
// what the installer writes; the loader defaults what their absence omits. Plain
// ${VAR} references are left in place: they are ordinary string contents once
// parsed, and removing them would empty the [models] table for no benefit.
func configHeredocs(t *testing.T) []string {
t.Helper()
source := readDeploymentFile(t, "install.sh")
var out []string
for _, m := range heredocRe.FindAllStringSubmatchIndex(source, -1) {
delim := source[m[2]:m[3]]
rest := source[m[1]:]
_, after, ok := strings.Cut(rest, "\n")
if !ok {
continue
}
var body strings.Builder
for line := range strings.SplitSeq(after, "\n") {
if strings.TrimRight(strings.TrimLeft(line, "\t"), "\r") == delim {
break
}
if strings.Contains(line, "$(") {
continue // a command substitution install.sh resolves at run time
}
body.WriteString(line)
body.WriteByte('\n')
}
if b := body.String(); strings.Contains(b, "\n[nats]") {
out = append(out, b)
}
}
if len(out) == 0 {
t.Fatal("install.sh has no heredoc declaring the daemon config's [nats] table")
}
return out
}
func TestInstallerGeneratedConfigHasServiceTargets(t *testing.T) {
// Non-self-host branch: install.sh replaces only the [forge] block in
// config.example.toml, so every other section -- including the required
// [services.state] target -- must already be active there.
example, err := configuration.New(nil).File("config.example.toml")
if err != nil {
t.Fatalf("load config.example.toml: %v", err)
}
if example.Config.Services.State.Target == "" {
t.Error("config.example.toml leaves services.state.target empty; the install.sh awk branch would emit an unbootable config")
}
// Self-host branch: install.sh writes the config through its own heredoc.
// Reconstruct every such heredoc and require each to parse with the State
// Store target the daemon's openStateStoreAdapter enforces.
for _, body := range configHeredocs(t) {
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
generated, err := configuration.New(nil).File(path)
if err != nil {
t.Fatalf("load reconstructed self-host install config: %v", err)
}
if generated.Config.Services.State.Target == "" {
t.Error("install.sh config heredoc leaves services.state.target empty; generated config cannot boot")
}
}
}
func TestInstallerUsesNativeDaemonAndManagedWorkerImage(t *testing.T) {
source := readDeploymentFile(t, "install.sh")
for _, forbidden := range []string{
+40 -78
View File
@@ -238,6 +238,7 @@ FORGE_TYPE="github"
GITEA_URL="https://gitea.example.com"
SELF_HOST_LLM=false
OLLAMA_MODEL=""
CLOUD_MODEL=""
if [ "${INTERACTIVE}" = true ]; then
echo ""
@@ -328,6 +329,13 @@ if [ "${INTERACTIVE}" = true ]; then
fi
fi
# A cloud provider needs a model name. archied setup requires one, and the
# installer previously emitted no [models] at all on this path.
if [ "${SELF_HOST_LLM}" = false ]; then
read -rp "Model name for OpenAI (e.g. gpt-5.4): " CLOUD_MODEL || CLOUD_MODEL=""
CLOUD_MODEL="${CLOUD_MODEL:-gpt-5.4}"
fi
# Prompt for cloud provider key if not self-hosting and no keys detected
if [ "${SELF_HOST_LLM}" = false ] && [ ${#FOUND_ENV_KEYS[@]} -eq 0 ]; then
echo "No existing cloud provider API keys were detected."
@@ -339,88 +347,42 @@ if [ "${INTERACTIVE}" = true ]; then
fi
fi
# Render the [forge] block from the answers actually given. Both config paths
# below share this: the generated-config branch previously hardcoded the GitHub
# token key and the Gitea host, so choosing Gitea produced a config pointing at
# a token the installer never wrote, and choosing GitHub pointed host at
# gitea.example.com.
forge_block() {
printf '[forge]\ntype = "%s"\n' "${FORGE_TYPE}"
case "${FORGE_TYPE}" in
github)
printf 'host = "https://github.com"\n'
printf 'token = { engine = "env", key = "ARCHIE_GITHUB_TOKEN" }\n'
;;
gitea)
printf 'host = "%s"\n' "${GITEA_URL}"
printf 'token = { engine = "env", key = "ARCHIE_GITEA_TOKEN" }\n'
;;
none)
# Forge disabled: archied takes the NoopForge path and never resolves a
# token, so emitting one would only invite a stale key.
;;
esac
# Trailing blank line keeps the section separated from whatever follows when
# this block is spliced into the example config. Command substitution strips
# it in the generated-config path, so it is harmless there.
printf '\n'
}
# Write config.toml with selected options if config.toml doesn't exist
# Generate config.toml through archied setup rather than here.
#
# The installer hand-rolling TOML already drifted from the schema once: the
# generated branch hardcoded the GitHub token key and the Gitea host, so choosing
# Gitea produced a config naming a token the installer never wrote. The durable
# fix, decided in docs/architecture/configuration.md, is that the code writing
# the config is the code that reads it -- archied setup renders it, and archied
# loads it.
#
# The answers collected above are passed as parameters. Secrets are passed as
# REFERENCES to the env keys set_env_key just wrote, never as values: a value on a
# command line lands in shell history and every process listing, and would be a
# second way to set a secret that no secret engine knows about.
if [ ! -f "${ARCHIE_CONFIG_DIR}/config.toml" ]; then
echo "==> Generating initial config.toml..."
if [ "${SELF_HOST_LLM}" = true ] && [ -n "${OLLAMA_MODEL}" ]; then
cat <<EOF > "${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
+50 -5
View File
@@ -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")
}
}