Files
archie/internal/infrastructure/configuration/validate_test.go
T
sam f544dd0978 feat(config): SIGHUP-triggered reload with requires-restart warnings
The daemon can now re-read its configuration on SIGHUP (or on a
future in-process trigger) and republish it through the Holder,
closing the first half of issue #348: 'config can change at runtime,
safely, and can't brick the daemon.'

Reload path:
- cmd/archied/reload.go: ReloadController re-runs loader.Resolve on
  the same paths main.go passed at startup (Resolve is pure -- no
  reload state added to the loader). apply is called only with a
  Document that passed validation; a failed reload records the error
  and its timestamp and leaves the running config untouched. That
  Set-not-called-on-error property is the safety core and is pinned
  by TestReloadValidationFailureDoesNotApply.
- SIGHUP listener in main.go runs reloadLoop(ctx, ch, controller, log);
  the loop is extracted as a testable seam (syscall.SIGHUP is Unix-only,
  fine given the Linux target).

PollInterval is genuinely reloadable now, not just re-read:
the three ticker sites (Run, per-identity goroutine, shared
maintenance loop) re-read the interval each tick and Reset the ticker
on change. A reloaded poll_interval takes effect on the next cycle.

Diff-and-warn so an operator never has to read the source to know
whether an edit took effect: changedNonReloadableFields compares the
old and new snapshots against an allowlist of re-read fields
(PollInterval, Repos, DiffCapLines, Budgets, Dispatch, and the
Containers sub-fields VolumeTTL + MaxConcurrency). Anything else that
changed logs Warn 'requires a restart'; a fully-applied reload logs
Info. The allowlist is deliberate: a field added to Config later
defaults to requires-restart, the safe direction.

NATS split-brain closed: containerEnv now hands containers the
endpoint the daemon's own client connected with at startup
(Daemon.ConnectedNATS, captured at construction) instead of the live
config, so a reloaded nats.url cannot point new containers at a
server the daemon is not publishing on. A nats change is therefore
classified requires-restart, and CLAUDE.md's 'hands its own nats.url
verbatim' note is updated to the meaning it now has (same today,
deliberately different after a reload).

Validation guard: poll_interval <= 0 is rejected in validate.go
beside the VolumeTTL guard. applyDefaults fills zero with 60s, so
this fires on an explicit negative -- which would panic
time.NewTicker/Reset at boot and, after the per-iteration reset,
on a SIGHUP mid-run.

Observability: config.ReloadStatus (last_error, last_error_at,
last_reload_at) is recorded by the controller and surfaced in
/api/config via Server.LastReload, so a stale running config is
visible in the dashboard, not just the logs. Rendering waits for the
UI step.

Recovery hatch: --no-config-overlay flag plus ARCHIE_SKIP_CONFIG_OVERLAY=1
(configuration.SkipOverlay), logged at startup. No overlay source
exists yet; the flag is the documented escape hatch the DB overlay
(step 3 of #348) will consult.

webui.Server.ConfigProvenance becomes atomic.Pointer so a reload can
swap it via SetProvenance independently of Cfg; the accepted skew
(new config alongside old provenance) is documented on the field.

Verified: task check clean; -race on daemon, webui, cmd/archied,
config, infrastructure/configuration.
2026-08-10 00:19:46 +10:00

137 lines
3.4 KiB
Go

package configuration
import (
"errors"
"os"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
)
// A minimal config that satisfies every check validate applies without
// relying on applyDefaults -- Validate documents that it does not apply
// defaults, so this fixture fills in every field a default would otherwise
// have supplied.
func minimalValidConfig() config.Config {
return config.Config{
BotUser: "archie-bot",
PollInterval: config.Duration(60 * time.Second),
Agent: config.Agent{Mode: "inprocess"},
Dispatch: config.Dispatch{
Trigger: "assignee",
},
Forge: config.Forge{Type: "none"},
}
}
func TestValidate_IsCallableOutsideThePackage(t *testing.T) {
cfg := minimalValidConfig()
if err := Validate(&cfg); err != nil {
t.Fatalf("Validate(minimal valid config) = %v, want nil", err)
}
}
func TestValidate_RejectsTheSameProblemsAsLoaderLoad(t *testing.T) {
tests := []struct {
name string
mutate func(cfg *config.Config)
wantErr bool
}{
{
name: "valid config",
mutate: func(cfg *config.Config) {},
wantErr: false,
},
{
name: "missing bot_user with no identities",
mutate: func(cfg *config.Config) { cfg.BotUser = "" },
wantErr: true,
},
{
name: "unrecognised agent.mode",
mutate: func(cfg *config.Config) { cfg.Agent.Mode = "not-a-real-mode" },
wantErr: true,
},
{
name: "unrecognised dispatch.trigger",
mutate: func(cfg *config.Config) { cfg.Dispatch.Trigger = "not-a-real-trigger" },
wantErr: true,
},
{
name: "unrecognised forge.type",
mutate: func(cfg *config.Config) { cfg.Forge.Type = "not-a-real-forge" },
wantErr: true,
},
{
name: "negative poll_interval",
mutate: func(cfg *config.Config) { cfg.PollInterval = config.Duration(-5 * time.Second) },
wantErr: true,
},
{
name: "zero poll_interval",
mutate: func(cfg *config.Config) { cfg.PollInterval = 0 },
wantErr: true,
},
{
name: "provider base_url carries userinfo",
mutate: func(cfg *config.Config) {
cfg.Providers = map[string]config.Provider{
"acme": {BaseURL: "https://user:pass@example.com"},
}
},
wantErr: true,
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
cfg := minimalValidConfig()
tc.mutate(&cfg)
err := Validate(&cfg)
if tc.wantErr && err == nil {
t.Fatal("Validate() = nil, want an error")
}
if tc.wantErr && !errors.Is(err, ErrInvalidInput) {
t.Errorf("Validate() = %v, want it to wrap ErrInvalidInput", err)
}
if !tc.wantErr && err != nil {
t.Fatalf("Validate() = %v, want nil", err)
}
})
}
}
// Validate must run the exact same checks Loader.File applies internally --
// this pins that a config Validate accepts on its own, once the same
// defaults Loader.File would apply are filled in by hand, is also accepted
// when it goes through the real file-loading path.
func TestValidate_AgreesWithLoaderLoad(t *testing.T) {
dir := t.TempDir()
path := dir + "/config.toml"
const doc = `
bot_user = "archie-bot"
[agent]
mode = "inprocess"
[dispatch]
trigger = "assignee"
[forge]
type = "none"
`
if err := os.WriteFile(path, []byte(doc), 0o600); err != nil {
t.Fatal(err)
}
cfg := minimalValidConfig()
if err := Validate(&cfg); err != nil {
t.Fatalf("Validate(in-memory) = %v, want nil", err)
}
if _, err := New(nil).File(path); err != nil {
t.Fatalf("Loader.File(equivalent config on disk) = %v, want nil", err)
}
}