Files
archie/internal/infrastructure/configuration/apply_test.go
T
sam 479ed95a91 fix(config): close review blocker on the PATCH path; deep-copy snapshots; atomics for shared state
Second adversarial pass findings, fixed:

BLOCKER -- PATCH silently dropped every dotted key. The UI sends
dotted keys ('budgets.max_steps') but ApplyOverlayValues fed them
straight to yaml.Unmarshal, which cannot decode a dotted key as a
struct field and silently ignored it: 200 OK, zero effect, and the row
still persisted (poisoning later reloads with an unvalidated value).
The reload path worked only because overlay.Snapshot nests first. Fix:
applyDottedOverlay nests dotted keys via overlay.Nest before decode --
the same nesting Snapshot performs -- and is pinned by
TestApplyDottedOverlayNestsBeforeDecode, which fails against the old
behaviour.

MAJOR -- shallow copies let overlay decode mutate the published
snapshot. Holder.Get and *doc are shallow; yaml decodes maps in place,
so a crafted nested update (or a failed boot overlay apply) mutated
shared maps of the published config before validation. Fix: config.Clone
deep-copies every reference field (Models, Providers, Repos, Identities,
Dispatch.Labels, Agent.Env, Chat/Telegram slices, Tools.MCPServers,
Memory.ProviderConfig, WebFetch.Enabled pointer, Extra), used by the
PATCH path (next := d.Cfg.Get().Clone()) and by Loader.ApplyOverlay,
which now never mutates its input (clones Config and the provenance
chain internally), so boot and reload pass the base document directly
and a failed apply leaves it exactly as it was. Pinned by
TestConfigCloneDeepCopiesReferenceFields.

MAJOR -- data races on captured main.go variables. bootOverlayErr and
currentProvenance were plain values written by the SIGHUP goroutine and
the PATCH/Reset handlers and read by every GET /api/config; -race passed
only because no test exercised the composition root concurrently. Both
are now atomic.Pointer; the LastReload closure and the provenance
dedup read them atomically.

MINOR -- MaxConcurrency removed from the reloadable allowlist. The
dispatchers re-read it but the startup-built container pool captures it
(container/pool.go:94,163), so 'every consumer re-reads it' is false and
a change only partially applies. It now warns requires-restart; the
criterion comment on reloadableFields documents the pool capture.

MINOR -- ResetConfig now resolves and validates the target state BEFORE
deleting the row, so a broken file cannot leave the store and the
running config disagreeing; the provenance chain no longer grows
unboundedly (the runtime-overlay origin replaces, not appends).

Also: holder.go no longer references a file watcher that does not exist.

Verified: task check clean; -race clean on cmd/archied, webui, daemon,
config; new findings from the second adversarial pass are closed.
2026-08-10 01:23:34 +10:00

88 lines
2.6 KiB
Go

package configuration
import (
"log/slog"
"os"
"path/filepath"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
)
func TestApplyOverlayValuesFieldLevelMerge(t *testing.T) {
cfg := config.Config{
BotUser: "a",
Budgets: config.Budgets{MaxSteps: 10},
}
overrides := map[string]any{
"budgets": map[string]any{"max_steps": float64(20)},
}
if err := ApplyOverlayValues(&cfg, overrides); err != nil {
t.Fatal(err)
}
if cfg.Budgets.MaxSteps != 20 {
t.Errorf("MaxSteps = %d, want 20", cfg.Budgets.MaxSteps)
}
// Fields the overlay omits keep their value.
if cfg.BotUser != "a" {
t.Errorf("BotUser = %q, want a (unchanged)", cfg.BotUser)
}
// Unrelated nested fields keep their value too.
if cfg.Budgets.MaxTokens != 0 {
t.Errorf("MaxTokens = %d, want 0 (untouched)", cfg.Budgets.MaxTokens)
}
}
func TestLoaderApplyOverlayLayersDefaultsValidatesAndRecordsProvenance(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte("bot_user = \"widget\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"), 0o600); err != nil {
t.Fatal(err)
}
l := New(slog.New(slog.DiscardHandler))
doc, err := l.Resolve(path, "")
if err != nil {
t.Fatal(err)
}
applied, err := l.ApplyOverlay(doc, map[string]any{"poll_interval": "90s"})
if err != nil {
t.Fatal(err)
}
if applied.Config.PollInterval != config.Duration(90*time.Second) {
t.Errorf("PollInterval = %v, want 90s", applied.Config.PollInterval)
}
if len(applied.Provenance.Origins) != 2 {
t.Errorf("Origins = %+v, want 2 (file + runtime overlay)", applied.Provenance.Origins)
}
// An overlay that fails validation aborts; ApplyOverlay never mutates
// its input, so the base document is untouched even without a copy.
if _, err := l.ApplyOverlay(doc, map[string]any{"agent": map[string]any{"mode": "bogus"}}); err == nil {
t.Fatal("invalid overlay accepted")
}
if doc.Config.Agent.Mode != "inprocess" {
t.Errorf("base Agent.Mode = %q, want inprocess (defaulted, untouched)", doc.Config.Agent.Mode)
}
}
func TestLoaderApplyOverlayEmptyIsNoOp(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte("bot_user = \"widget\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"), 0o600); err != nil {
t.Fatal(err)
}
l := New(slog.New(slog.DiscardHandler))
doc, err := l.Resolve(path, "")
if err != nil {
t.Fatal(err)
}
applied, err := l.ApplyOverlay(doc, nil)
if err != nil {
t.Fatal(err)
}
if len(applied.Provenance.Origins) != 1 {
t.Errorf("Origins = %+v, want 1 (no overlay origin for an empty overlay)", applied.Provenance.Origins)
}
}