Files
archie/cmd/archied/reload_test.go
T
sam 31fdbddd61 fix(config): complete the reloadable allowlist and pin it against ForTask
The allowlist drifted before the chunk shipped. ForTask (config.go:457-465)
is the authoritative per-dispatch snapshot and carries Models, Budgets,
Dispatch, DiffCapLines, Notify and Forge.Host -- I allowlisted only three.
And the poll path reads cfg.Label and cfg.BotUser live per cycle
(daemon.go:359,390 and :368,381) off the same per-poll config as
Dispatch.Trigger. Five fields would have reported 'requires a restart'
while actually taking effect immediately: the safe direction, but exactly
the warning-fatigue failure that kills the feature (an operator who is
told to restart for a label change that already applied learns the
warning is noise).

The fix is enforcement, not a bigger list: TestReloadableFieldsCoverForTaskSnapshot
reflects over TaskConfig and fails if any field ForTask carries is not in
the allowlist (Forge maps to the Forge.Host sub-field entry). A field
added to ForTask later now fails the test instead of silently producing a
wrong warning. Label and BotUser are not carried by ForTask -- they come
off the poll path -- so they are allowlisted by hand with a comment, and
the same test pins them explicitly.

Also documents on webui.Server.Cfg that in production it is the daemon's
OWN Holder (web.Cfg = d.Cfg), so Set publishes to the running daemon: a
config-mutating handler must apply to a copy, validate the materialised
config, persist, then Set -- never Set directly.

task check clean.
2026-08-10 00:47:30 +10:00

241 lines
8.2 KiB
Go

package main
import (
"context"
"log/slog"
"os"
"path/filepath"
"reflect"
"sync"
"syscall"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
)
// writeConfig writes a TOML config file. The minimal shape (bot_user +
// one repo) is the same fixture loader_test.go uses to satisfy
// validation.
func writeConfig(t *testing.T, path, body string) {
t.Helper()
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
t.Fatal(err)
}
}
func minimalConfigTOML(botUser string) string {
return "bot_user = \"" + botUser + "\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"
}
func invalidConfigTOML() string {
return "bot_user = \"widget\"\nagent = { mode = \"bogus-mode\" }\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"
}
func TestReloadAppliesAndPublishes(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
writeConfig(t, path, minimalConfigTOML("first"))
loader := configuration.New(slog.New(slog.DiscardHandler))
var published config.Config
controller := newReloadController(loader, path, "", func(doc *configuration.Document) {
published = doc.Config
})
if err := controller.Reload(); err != nil {
t.Fatalf("first reload: %v", err)
}
if published.BotUser != "first" {
t.Fatalf("published BotUser = %q, want first", published.BotUser)
}
writeConfig(t, path, minimalConfigTOML("second"))
if err := controller.Reload(); err != nil {
t.Fatalf("second reload: %v", err)
}
if published.BotUser != "second" {
t.Fatalf("published BotUser = %q, want second", published.BotUser)
}
if st := controller.Status(); st.LastReloadAt == "" || st.LastError != "" {
t.Fatalf("Status after success = %+v, want LastReloadAt set and no error", st)
}
}
// TestReloadValidationFailureDoesNotApply pins the safety property of
// the whole reload feature: a reload that fails validation must not
// publish. If apply were called on the error path, a bad file would
// half-apply or corrupt the running config.
func TestReloadValidationFailureDoesNotApply(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
writeConfig(t, path, minimalConfigTOML("widget"))
loader := configuration.New(slog.New(slog.DiscardHandler))
applied := 0
controller := newReloadController(loader, path, "", func(*configuration.Document) { applied++ })
if err := controller.Reload(); err != nil {
t.Fatalf("first reload: %v", err)
}
if applied != 1 {
t.Fatalf("apply called %d times after successful reload, want 1", applied)
}
writeConfig(t, path, invalidConfigTOML())
err := controller.Reload()
if err == nil {
t.Fatal("reload with invalid config: expected an error")
}
if applied != 1 {
t.Fatalf("apply called %d times after failed reload, want still 1", applied)
}
if st := controller.Status(); st.LastError == "" || st.LastErrorAt == "" {
t.Fatalf("Status after failed reload = %+v, want LastError and LastErrorAt set", st)
}
// A subsequent valid reload clears the error.
writeConfig(t, path, minimalConfigTOML("widget"))
if err := controller.Reload(); err != nil {
t.Fatalf("recovery reload: %v", err)
}
if st := controller.Status(); st.LastError != "" || st.LastReloadAt == "" {
t.Fatalf("Status after recovery = %+v, want no error and a reload time", st)
}
}
func TestReloadLoopAppliesOnSignalAndStopsOnCancel(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
writeConfig(t, path, minimalConfigTOML("widget"))
loader := configuration.New(slog.New(slog.DiscardHandler))
applied := make(chan struct{}, 1)
controller := newReloadController(loader, path, "", func(*configuration.Document) {
applied <- struct{}{}
})
ctx, cancel := context.WithCancel(context.Background())
ch := make(chan os.Signal, 1)
var wg sync.WaitGroup
wg.Go(func() {
reloadLoop(ctx, ch, controller, slog.New(slog.DiscardHandler))
})
ch <- syscall.SIGHUP
select {
case <-applied:
case <-time.After(5 * time.Second):
t.Fatal("reload not applied after signal")
}
cancel()
wg.Wait()
}
func TestChangedNonReloadableFields(t *testing.T) {
base := config.Config{
BotUser: "a",
MaxRetries: 3,
PollInterval: config.Duration(30 * time.Second),
Repos: []config.Repo{{Owner: "x", Name: "y"}},
}
// Unchanged -> empty.
if got := changedNonReloadableFields(base, base); len(got) != 0 {
t.Fatalf("unchanged: got %v, want []", got)
}
// Reloadable-only changes -> empty.
reloadableOnly := base
reloadableOnly.PollInterval = config.Duration(60 * time.Second)
reloadableOnly.Repos = []config.Repo{{Owner: "x", Name: "z"}}
reloadableOnly.Budgets = config.Budgets{MaxSteps: 99}
reloadableOnly.Dispatch = config.Dispatch{Trigger: "label"}
reloadableOnly.DiffCapLines = 1234
reloadableOnly.Models = map[string]string{"builder": "provider/model"}
reloadableOnly.Notify = config.Notify{Webhook: "https://notify.example/hook"}
reloadableOnly.Label = "archie"
reloadableOnly.BotUser = "new-bot"
reloadableOnly.Forge = config.Forge{Host: "https://new-forge.example"}
if got := changedNonReloadableFields(base, reloadableOnly); len(got) != 0 {
t.Fatalf("reloadable-only: got %v, want []", got)
}
// A non-reloadable change is named (Agent builds the startup runner).
nonReloadable := base
nonReloadable.Agent = config.Agent{Mode: "subprocess"}
if got := changedNonReloadableFields(base, nonReloadable); len(got) != 1 || got[0] != "Agent" {
t.Fatalf("Agent change: got %v, want [Agent]", got)
}
// Containers sub-field granularity: Image change warns, MaxConcurrency
// and VolumeTTL changes do not.
c1 := base
c1.Containers = config.ContainerConfig{Image: "img1", MaxConcurrency: 2, VolumeTTL: config.Duration(24 * time.Hour)}
c2 := c1
c2.Containers.Image = "img2"
if got := changedNonReloadableFields(c1, c2); len(got) != 1 || got[0] != "Containers.Image" {
t.Fatalf("Image change: got %v, want [Containers.Image]", got)
}
c3 := c1
c3.Containers.MaxConcurrency = 4
if got := changedNonReloadableFields(c1, c3); len(got) != 0 {
t.Fatalf("MaxConcurrency change: got %v, want []", got)
}
c4 := c1
c4.Containers.VolumeTTL = config.Duration(48 * time.Hour)
if got := changedNonReloadableFields(c1, c4); len(got) != 0 {
t.Fatalf("VolumeTTL change: got %v, want []", got)
}
// NATS is not reloadable: a URL change warns (the daemon's client is
// startup-built; containers get the frozen ConnectedNATS).
natsChanged := base
natsChanged.NATS = config.NATSConfig{URL: "nats://new:4222"}
if got := changedNonReloadableFields(base, natsChanged); len(got) != 1 || got[0] != "NATS" {
t.Fatalf("NATS change: got %v, want [NATS]", got)
}
// Forge.Host is reloadable but Forge.Type is not: only the host is
// carried into TaskContext by ForTask.
forgeTypeChanged := base
forgeTypeChanged.Forge = config.Forge{Type: "gitea"}
if got := changedNonReloadableFields(base, forgeTypeChanged); len(got) != 1 || got[0] != "Forge.Type" {
t.Fatalf("Forge.Type change: got %v, want [Forge.Type]", got)
}
}
// TestReloadableFieldsCoverForTaskSnapshot pins the allowlist against
// the per-dispatch snapshot: every field ForTask carries into a
// TaskContext is re-read at dispatch, so a reload applies to new tasks.
// If a field is added to ForTask later, this test fails until the
// allowlist is updated -- a hand-maintained list that mirrors ForTask
// otherwise drifts silently into a wrong 'requires a restart' warning.
func TestReloadableFieldsCoverForTaskSnapshot(t *testing.T) {
tt := reflect.TypeFor[config.TaskConfig]()
for field := range tt.Fields() {
name := field.Name
if name == "Forge" {
// TaskConfig.Forge is TaskForge{Host}; it maps to the
// Config.Forge.Host sub-field allowlist entry.
if !reloadableSubFields["Forge"]["Host"] {
t.Errorf("ForTask field Forge (Host) is not allowlisted as reloadable")
}
continue
}
if !reloadableFields[name] {
t.Errorf("ForTask field %s is not allowlisted as reloadable", name)
}
}
// The poll-path fields (Label, BotUser) are not carried by ForTask;
// pin them explicitly so a refactor that moves their read site does
// not silently drop the allowlist entry.
for _, name := range []string{"Label", "BotUser"} {
if !reloadableFields[name] {
t.Errorf("poll-path field %s is not allowlisted as reloadable", name)
}
}
}