Files
archie/internal/config/clone_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

75 lines
2.6 KiB
Go

package config
import "testing"
// TestConfigCloneDeepCopiesReferenceFields pins that Clone returns a
// value sharing no memory with the original: mutating the clone's maps
// and slices must not touch the original. A shallow copy here would
// silently let a failed overlay decode mutate the published snapshot.
func TestConfigCloneDeepCopiesReferenceFields(t *testing.T) {
enabled := true
orig := Config{
Models: map[string]string{"builder": "m"},
Repos: []Repo{{Gate: [][]string{{"task", "check"}}, Protect: []string{"p"}}},
Identities: []IdentityConfig{{
Models: map[string]string{"planner": "m2"},
Repos: []Repo{{Gate: [][]string{{"go", "vet"}}}},
}},
Dispatch: Dispatch{Labels: map[string]string{"q": "archie:q"}},
Tools: ToolsConfig{
MCPServers: []MCPServer{{Headers: map[string]string{"A": "b"}, Args: []string{"x"}}},
WebFetch: WebFetchConfig{Enabled: &enabled},
},
Memory: MemoryConfig{ProviderConfig: map[string]string{"k": "v"}},
Chat: ChatConfig{Telegram: TelegramConfig{AllowedUserIDs: []int64{1}}},
Agent: Agent{Env: []string{"HOME"}},
Extra: map[string]any{"custom": 1},
}
got := orig.Clone()
got.Models["builder"] = "changed"
got.Repos[0].Gate[0][0] = "changed"
got.Repos[0].Protect[0] = "changed"
got.Identities[0].Models["planner"] = "changed"
got.Identities[0].Repos[0].Gate[0][0] = "changed"
got.Dispatch.Labels["q"] = "changed"
got.Tools.MCPServers[0].Headers["A"] = "changed"
got.Tools.MCPServers[0].Args[0] = "changed"
got.Memory.ProviderConfig["k"] = "changed"
got.Chat.Telegram.AllowedUserIDs[0] = 99
got.Agent.Env[0] = "changed"
got.Extra["custom"] = 2
*got.Tools.WebFetch.Enabled = false
if orig.Models["builder"] != "m" {
t.Error("Models map is shared")
}
if orig.Repos[0].Gate[0][0] != "task" || orig.Repos[0].Protect[0] != "p" {
t.Error("Repo nested slices are shared")
}
if orig.Identities[0].Models["planner"] != "m2" || orig.Identities[0].Repos[0].Gate[0][0] != "go" {
t.Error("Identity nested fields are shared")
}
if orig.Dispatch.Labels["q"] != "archie:q" {
t.Error("Dispatch.Labels map is shared")
}
if orig.Tools.MCPServers[0].Headers["A"] != "b" || orig.Tools.MCPServers[0].Args[0] != "x" {
t.Error("MCP server fields are shared")
}
if orig.Memory.ProviderConfig["k"] != "v" {
t.Error("Memory.ProviderConfig is shared")
}
if orig.Chat.Telegram.AllowedUserIDs[0] != 1 {
t.Error("Telegram.AllowedUserIDs is shared")
}
if orig.Agent.Env[0] != "HOME" {
t.Error("Agent.Env is shared")
}
if orig.Extra["custom"] != 1 {
t.Error("Extra map is shared")
}
if !*orig.Tools.WebFetch.Enabled {
t.Error("WebFetch.Enabled pointer is shared")
}
}