fix(statestore): read a kind the store does not hold as its seed, and stop validate touching the daemon's log

Four defects from the re-run contract review, all in the recovery path.

A store missing a control-plane kind was refused while a store with no
resources table at all was accepted -- two stances inside validateStore for
states the daemon cannot tell apart. The State Store seeds every kind from
its own config before it serves (RunStateStore -> ImportConfig), so a kind
the file does not hold yet is one archied reads the seed for: an upgrade that
adds a kind, and a validate before the State Store has started again, exited
1 with "read provider-settings: resource not found" for a store the daemon
boots on fine. StoredRuntimeConfig now reads a missing kind as the seed the
State Store would write for it (Definition.seededValue, shared with
ImportConfig so the two cannot drift), and the missing-table branch is the
same stance taken to its extreme.

defaultConfigPath() derived the daemon's config with os.UserConfigDir, which
disagrees with archied's configHome on darwin and rejects a relative
XDG_CONFIG_HOME outright -- leaving validate with an empty path and no
verdict, on a host where archied boots from relative-dir/archie/config.toml.
It is now the daemon's own rule, exported as archied.DefaultConfigPath and
used by the daemon's own -config default too, with a test pinning the
equality.

-h exited 2, the code documented as "the command line was wrong", while the
serve path in the same binary exits 0 and main.go advertises -h for these
subcommands. ErrHelp now returns 0.

validate resolved the boot config through loadConfig -> setupLogging, which
created cfg.Log.File's directory, opened the log, and wrote a line stamped
component="daemon" -- a diagnostic editing the deployment it inspects, and
rotating a log the daemon is writing. boot.stderrLog keeps setupLogging on
the stderr logger for the recovery path: no feed, no task-log registry, no
durable copy.

Tests: TestRecoveryValidateRunsTheGateBootRuns now asserts the unseeded and
the missing-kind stores are accepted (both exited 1 before), while the
bogus-policy case still refuses; TestRecoveryHelpExitsZero,
TestRecoveryValidateLeavesTheDaemonLogAlone and
TestRecoveryDefaultConfigFollowsTheDaemonsConfigHome are new. Each was run
red first, and the seed fallback and stderrLog were mutation-checked.
This commit is contained in:
sam committed 2026-09-21 22:04:42 +10:00
1 parent e7e5ddfba5
commit 08df95684c
8 files changed
+215 -37

No files matched your search

+12 -12
View File
@@ -2,25 +2,20 @@ package main
import (
"context"
"errors"
"flag"
"fmt"
"io"
"os"
"path/filepath"
"github.com/samcharles93/archie-core/internal/app/archied"
)
// defaultConfigPath is the configuration the serve path reads by default; the
// offline validate asks what the daemon would boot with, so it reads the same
// one.
func defaultConfigPath() string {
base, err := os.UserConfigDir()
if err != nil {
return ""
}
return filepath.Join(base, "archie", "config.toml")
}
// defaultConfigPath is the configuration the serve path reads by default, and
// the one the daemon boots with. It is the daemon's own rule rather than a
// second derivation of it: os.UserConfigDir (what the sibling processes use)
// disagrees with archied on darwin and rejects a relative XDG_CONFIG_HOME
// outright, which would leave validate answering about a file nobody boots.
func defaultConfigPath() string { return archied.DefaultConfigPath() }
// recoveryUsage is the whole flag surface of the offline recovery commands.
// They are subcommands of this binary rather than a recovery binary of their
@@ -77,6 +72,11 @@ func runRecovery(args []string, stdout, stderr io.Writer) int {
flags.StringVar(&options.Overlay, "config-overlay", "", "configuration overlay file or directory")
}
if err := flags.Parse(args[1:]); err != nil {
// A requested help is not a wrong command line: the usage above is the
// answer, and the serve path exits 0 for the same request.
if errors.Is(err, flag.ErrHelp) {
return 0
}
return 2
}
if flags.NArg() > 0 {
+105 -6
View File
@@ -317,16 +317,94 @@ func TestRecoveryValidateRunsTheGateBootRuns(t *testing.T) {
t.Errorf("refusal must name what boot rejects; stderr = %q", stderr)
}
// A store the State Store never seeded: the daemon cannot layer its
// settings from it either, so the offline check must not call it valid.
// A store the State Store never seeded holds no kinds at all. Its next start
// creates the resources table and seeds every kind from this same config
// (internal/app/archied/state_store.go), so archied layers those seeds and
// starts: refusing the file here would send an operator to restore a
// snapshot for a store the daemon boots on perfectly well.
unseeded := filepath.Join(dir, "unseeded.db-tasks.sqlite")
openTaskStore(t, unseeded)
code, stdout, stderr = runRecoveryCmd(t, "validate", "-db", unseeded, "-config", configPath)
if code == 0 {
t.Fatalf("validate accepted a store with no stored settings; stdout = %q", stdout)
if code != 0 {
t.Fatalf("validate refused a store the State Store would seed on its next start; stderr = %q", stderr)
}
if !strings.Contains(stderr, "provider-settings") {
t.Errorf("refusal must name the resource kind boot cannot read; stderr = %q", stderr)
if !strings.Contains(stdout, "0 stored resources validate") {
t.Errorf("validate must report that there was nothing stored to check; stdout = %q", stdout)
}
// The upgrade case: a release defines a kind the store does not hold yet,
// and validate runs before the State Store has started again. The State
// Store seeds the kind it is missing, so this is the same stance as the
// store with nothing stored at all -- the file is not the reason the daemon
// would refuse to start, and saying otherwise costs the operator a snapshot.
partial := filepath.Join(dir, "partial.db-tasks.sqlite")
seedStoreResources(t, openTaskStore(t, partial), configPath)
if err := execRaw(t, partial, `DELETE FROM resources WHERE kind='provider-settings'`); err != nil {
t.Fatal(err)
}
code, stdout, stderr = runRecoveryCmd(t, "validate", "-db", partial, "-config", configPath)
if code != 0 {
t.Fatalf("validate refused a store missing a kind the State Store seeds; stderr = %q", stderr)
}
if count := storedResourceCount(t, stdout); count == 0 {
t.Errorf("validate must still check the kinds the store does hold; stdout = %q", stdout)
}
}
// The subcommands advertise -h as the way to read their flags, and the serve
// path in the same binary exits 0 for the same request, so a script probing the
// recovery surface must not read a requested help as a failure.
func TestRecoveryHelpExitsZero(t *testing.T) {
commands := []string{archied.RecoveryBackup, archied.RecoveryRestore, archied.RecoveryValidate, archied.RecoveryRollback}
for _, command := range commands {
code, _, stderr := runRecoveryCmd(t, command, "-h")
if code != 0 {
t.Errorf("%s -h exited %d, want 0; stderr = %q", command, code, stderr)
}
}
}
// validate diagnoses a deployment; it is not the deployment. Resolving the boot
// config must not create, append to, or rotate the daemon's log file or the
// directory it lives in: a line stamped component="daemon" in archied's log is
// indistinguishable from the daemon having written it.
func TestRecoveryValidateLeavesTheDaemonLogAlone(t *testing.T) {
dir := t.TempDir()
configPath, logFile := writeConfigWithLogFile(t, dir)
db := filepath.Join(dir, "archie.db-tasks.sqlite")
seedStoreResources(t, openTaskStore(t, db), configPath)
code, _, stderr := runRecoveryCmd(t, "validate", "-db", db, "-config", configPath)
if code != 0 {
t.Fatalf("validate exited %d: %s", code, stderr)
}
if _, err := os.Stat(logFile); !os.IsNotExist(err) {
t.Fatalf("validate touched the daemon's log file %s (stat err = %v)", logFile, err)
}
if _, err := os.Stat(filepath.Dir(logFile)); !os.IsNotExist(err) {
t.Errorf("validate created the log directory %s (stat err = %v)", filepath.Dir(logFile), err)
}
}
// validate's verdict is the daemon's verdict only if it reads the configuration
// the daemon reads. archied resolves that default from its own configHome, which
// honours a relative XDG_CONFIG_HOME; os.UserConfigDir rejects a relative one
// outright, which left the command with no config and no verdict at all.
func TestRecoveryDefaultConfigFollowsTheDaemonsConfigHome(t *testing.T) {
relative := filepath.Join("relative", "config")
t.Setenv("XDG_CONFIG_HOME", relative)
if got, want := defaultConfigPath(), filepath.Join(relative, "archie", "config.toml"); got != want {
t.Errorf("defaultConfigPath() with a relative XDG_CONFIG_HOME = %q, want %q", got, want)
}
absolute := t.TempDir()
t.Setenv("XDG_CONFIG_HOME", absolute)
if got, want := defaultConfigPath(), filepath.Join(absolute, "archie", "config.toml"); got != want {
t.Errorf("defaultConfigPath() = %q, want %q", got, want)
}
// The equality itself: one helper, so the two cannot drift apart again.
if got, want := defaultConfigPath(), archied.DefaultConfigPath(); got != want {
t.Errorf("defaultConfigPath() = %q, but the daemon resolves %q", got, want)
}
}
@@ -582,6 +660,27 @@ func TestRecoveryRejectsAnUnknownSubcommand(t *testing.T) {
}
}
// writeConfigWithLogFile is writeMinimalConfig plus the daemon's durable log
// destination, which only the daemon may bring into existence.
func writeConfigWithLogFile(t *testing.T, dir string) (string, string) {
t.Helper()
configPath := writeMinimalConfig(t, dir)
logFile := filepath.Join(dir, "logs", "archied.log")
f, err := os.OpenFile(configPath, os.O_APPEND|os.O_WRONLY, 0o600)
if err != nil {
t.Fatalf("open config to append: %v", err)
}
defer func() {
if err := f.Close(); err != nil {
t.Fatalf("close config: %v", err)
}
}()
if _, err := fmt.Fprintf(f, "\n[log]\nfile = %q\n", logFile); err != nil {
t.Fatalf("append [log]: %v", err)
}
return configPath, logFile
}
// seedStoreResources seeds the store the way the State Store seeds it at boot,
// from the same config file the operator hands validate: the command's verdict
// is about the pair, and a store whose settings never matched the config is a