ADR-0101: Config write safety — atomic, backed-up, guarded, attributable¶
- Status: Accepted 2026-11-05
- Context: Issue #404 (2026-11-05 incident:
agentsand six other keys wiped from~/.milk/config.json; forensics on the issue) - Supersedes/amends: nothing — complements #405 (the
.bakno-op guard, whose commit message deferred the rest to this design)
Context¶
milk rewrites its own configuration from several runtime paths (update-check stamps, wizard commits, theme/agent/telegram slash commands, CLI config commands, ACP set_config_option) with no atomicity, no lock, no meaningful backup and no record:
Save()truncatedconfig.jsonin place — any concurrent reader (and several milk processes share~/.milk) could see half a file, tripdefaults()on parse, and the next save persisted the agentless defaults over a good config. A failure to load became a write of a wiped config.- The backup (
config.json.bak) was refreshed from loads and saved with the new bytes — a backup of what you just wrote restores nothing, and a wiped-but-valid file replaced the last-known-good copy at the very next start. - The startup update-check write-back saved a 15-second-old in-memory snapshot of the whole config — a lost-update race against any edit made during the network call.
- Nothing logged config mutations: the incident's corrupting writer left no trace (mtimes overwritten, no
obsevent), so attribution was impossible after the fact. - Test guards against the real state root were per-test convention (
t.Setenv("HOME")) — while no test wroteconfig.json, the same runs littered the real~/.milk/sessionswith hundreds of fixtures.
Decision¶
Write model (internal/config/safety.go, lock_*.go):
- Atomic + serialized. Every write goes temp-file →
rename(readers see old or new bytes, never a torn file) under an advisory sidecar lock (flock/LockFileEx, auto-released on process death; no-op fallback on exotic platforms). - Backup = previous content. The writer rotates the bytes it is about to replace into
.bak(old.bak→.bak.1, N=1), only for effective changes (configsEquivalent, #405's rule) and never from an unparseable file. Loads never touch.bak— a refresh there would let a wiped-but-valid file replace the last-known-good copy at the next start. - Persist guard (refuse-persisting-defaults). A load that ends with an unparseable file and no usable backup arms the guard: every ordinary save refuses with
ErrPersistRefused(user-facing text). The guard clears on a successful load/recovery or on the init wizard's forced save (SaveForced/SaveScopeInForced— the one explicit recovery path allowed to write after a failed load). Read errors (absent overlay file = normal) leave the guard untouched in both directions.
Consent model — by construction, not by prompt:
- After migrating
updateCheck/skipUpdateReleasetoPatchBookkeeping(re-read the file, parse it, set only whitelisted stamp keysupdate_last_check/update_skipped_version, write only on change — never a snapshot), every full-config writer in milk is reachable only from an explicit user action (slash command, wizard, CLI, ACP option). - The remaining knob,
config_consent: "strict" (default) | "open", only controls the guard:"open"restores the legacy permissive behavior for whoever wants it. - An interactive ask-gate on the ADR-0052 permission machinery was considered and rejected for now: with no non-explicit full-config writer left it would be unreachable UI with real plumbing cost (config writes happen inside
Updateand in goroutines). The safety net for future writers is the observer below plus the guard; the ask-gate stays the documented option if a non-explicit writer ever needs it. - Attribution. Every save, patch and refusal emits a
SaveEventthroughSetSaveObserver;cmd/milkwires it toobs(config_write/config_write_refusedin the milk log when OTel is on). The next incident gets a log line.
Test hygiene:
internal/testutil.SandboxStateRoot()—TestMain-level redirection ofHOME/USERPROFILE/MILK_CONFIG_ROOTforcmd/milk,internal/config,internal/session,internal/agent/local, plus a canary that fails the run (exit 98) if the realconfig.jsonchanged bytes during it. Convention became a property of the suite. Synthesized homes are rc-complete:EnsureShellRCseeds a comments-only.zshrc(sandbox always; the showcase pane only under$SHELL=zsh), because an rc-less HOME makes zsh run its first-run menu, which swallows harness keystrokes — env-prefixed launch lines included — and silently launches against the real state root (the #404 forensics \"fixture artifact\"). A passing canary also drain-removes the sandbox root, and per-test homes useinternal/testutil.SandboxHomewith the same drain-aware cleanup: a test's machinery can outlive its return (a turn or job completion handler writing state after the test ended), and the drain waits that late writer out instead of failingTempDir RemoveAll cleanup: … directory not empty— the CI flake family that hitTestACPStreaming_BackgroundJobOutputV2AndV1andTestHandleBusyKey_EnterDoesNotSpawn. A writer that persists past the drain timeout fails loudly naming the leftover files, so genuine orphans diagnose themselves. Two addenda from the PR #411 CI failure: toolchain caches are pinned outside the sandbox —forwardToolchainCachescapturesgo env GOMODCACHE GOCACHEbefore any HOME override (both entry points), because a childgo buildderiving them from the sandboxed HOME (CI exports no GOPATH) downloads a read-only 0555 module cache thatRemoveAllcan never unlink — the drain would retry a doomed removal until timeout and TestMain FATALed; reproduced locally withenv -u GOPATH, green after the pin. And the drain now grants rm -rf semantics: onfs.ErrPermissionit chmod-walks the tree owner-writable (makeRemovable) and retries once, instead of timing out on a tree no owner should be unable to delete. Known limit: a completion handler bound to env at write time can still write under another test's HOME; the drain absorbs it into the victim's cleanup, bounding writers to their spawn-time root remains the larger refactor (not done).
Consequences¶
- Greenfield stamping changes shape:
PatchBookkeepingwrites nothing when the file is absent — butLoad()creates the config on first start, so the normal flow is unaffected; a stamp is simply skipped when there is no config to stamp. .baksemantics flip from "last parsed bytes" to "content the previous save replaced";.bak.1appears (single predecessor). #405's no-op suppression is preserved viaconfigsEquivalent.- A session that starts against a corrupt config now runs read-mostly: slash commands that persist settings report the refusal until
/config initfixes the file (orconfig_consent: "open"opts out). - Two new OS-locked files exist next to config (
config.json.lock, tiny, permanent by design — unlinking would race the lock) and occasional.tmpwrites (removed on failure or rename). - Tests in the four sandboxed packages never touch the real state root again;
internal/showcase(gated live test needs the real config presence as its skip signal) andeval(isolated roots by design) deliberately keep the convention model.