fix(config): keep the fields a file overlay omits, and judge a database-owned setting at its owner

Resolves archie-core-xrux and archie-core-i3qm. The -config-overlay file path
(and the overlay-directory path) now fold a partial map-valued entry instead of
replacing it wholesale, so an overlay that sets one sibling key no longer clears
services.state.target_token. The file load path stops judging settings the
control plane owns, so a stale TOML value in a database-owned field can no
longer stop archie-state-store starting; boot.runtimeConfig remains the one
layering call and still validates the merged document. A kind with no stored
value leaves the file's value in force.
This commit is contained in:
sam committed 2026-09-21 22:15:22 +10:00
commit f38b741be2
17 files changed
+1398 -139

No files matched your search

+64 -16
View File
@@ -27,16 +27,46 @@ func ApplyOverlayValues(cfg *config.Config, overrides map[string]any) error {
if len(overrides) == 0 {
return nil
}
// yaml gives a struct field the field-level treatment promised above, but a
// map VALUE is decoded into a fresh zero value: an entry the overlay only
// partially addresses would lose every field it does not name, so an
// operator changing services.state.target alone would silently drop
// target_token. Fold the fields the overlay omits forward out of the entry
// cfg already holds, leaving the decode a complete entry to read. Doing it
// here, once, is what makes the promise hold for every map of structs
// (services, image.hosted, image.local, providers) instead of at each
// field's reader.
folded, err := foldOverrides(reflect.ValueOf(cfg), overrides)
return applyOverlayMapping(cfg, overrides)
}
// applyOverlayFile layers the overlay file at path over target, with the same
// field-level precedence ApplyOverlayValues gives a map overlay, and reports the
// file's keys that nothing consumes. target is whatever the overlay addresses:
// the whole config for a main file, one sub-struct for a feature file.
//
// Both processed overlay forms go through it -- the single overlay file, and an
// overlay directory's main and feature files -- so neither can lose the fields of
// a map-valued entry it only partly addresses. A conf.d/*.yaml extra in an
// overlay directory does not: decodeExtra stores a whole file under
// cfg.Extra[name] and nothing reads that map, so there is no entry to fold into.
// Decoding an overlay into target directly replaces such an entry wholesale,
// which is how an overlay naming only services.state.target cleared the
// target_token the base file set.
func applyOverlayFile(path string, target any) ([]string, error) {
mapping, err := decodeFileMapping(path)
if err != nil {
return nil, err
}
if err := applyOverlayMapping(target, mapping); err != nil {
return nil, err
}
return overlayFileKeys(path, mapping, structTypeOf(target))
}
// applyOverlayMapping is the decode both overlay paths share: the fields each
// map-valued entry the overlay does not name are folded forward out of the
// entry target already holds, and the completed mapping is then decoded over it.
//
// yaml gives a struct field the field-level treatment ApplyOverlayValues
// promises, but a map VALUE is decoded into a fresh zero value: an entry the
// overlay only partially addresses would lose every field it does not name, so
// an operator changing services.state.target alone would silently drop
// target_token. Folding here, once, is what makes the promise hold for every
// map of structs (services, image.hosted, image.local, providers) instead of
// at each field's reader.
func applyOverlayMapping(target any, doc map[string]any) error {
folded, err := foldOverrides(reflect.ValueOf(target), doc)
if err != nil {
return err
}
@@ -44,12 +74,22 @@ func ApplyOverlayValues(cfg *config.Config, overrides map[string]any) error {
if err != nil {
return fmt.Errorf("%w: encoding config overlay: %w", ErrUnreadable, err)
}
if err := yaml.Unmarshal(data, cfg); err != nil {
if err := yaml.Unmarshal(data, target); err != nil {
return fmt.Errorf("%w: parsing config overlay: %w", ErrUnreadable, err)
}
return nil
}
// structTypeOf returns the struct type target points at, so the fold and the key
// report can inspect it without an instance.
func structTypeOf(target any) reflect.Type {
t := reflect.TypeOf(target)
for t != nil && t.Kind() == reflect.Pointer {
t = t.Elem()
}
return t
}
// foldOverrides returns doc with the fields its map entries omit carried
// forward from the entries cfg already holds. It descends through structs, so
// {"image": {"hosted": {...}}} folds the same way as an override written at the
@@ -162,9 +202,17 @@ func mergeMapping(base, override map[string]any) map[string]any {
// yamlField returns the field of struct v that the decode reads key into. The
// yaml tag is the one consulted because the decode below is yaml's.
func yamlField(v reflect.Value, key string) (reflect.Value, bool) {
t := v.Type()
for i := range t.NumField() {
field := t.Field(i)
field, ok := yamlFieldOf(v.Type(), key)
if !ok {
return reflect.Value{}, false
}
return v.FieldByIndex(field.Index), true
}
// yamlFieldOf is yamlField without a value: the overlay key report walks a raw
// mapping against the target's type before anything is decoded into it.
func yamlFieldOf(t reflect.Type, key string) (reflect.StructField, bool) {
for field := range t.Fields() {
if field.PkgPath != "" {
continue // unexported, so the decoder cannot write it
}
@@ -176,8 +224,8 @@ func yamlField(v reflect.Value, key string) (reflect.Value, bool) {
name = strings.ToLower(field.Name)
}
if name == key {
return v.Field(i), true
return field, true
}
}
return reflect.Value{}, false
return reflect.StructField{}, false
}
@@ -27,6 +27,32 @@ func decodeConfigFileKeys(path string, target any) ([]string, error) {
}
}
// decodeFileMapping parses a configuration file into the nested mapping it
// carries, with no typed target. An overlay needs this shape: a typed decode
// cannot distinguish a key the file omits from a key it sets to the zero
// value, and that distinction is what carrying the fields of a partially
// addressed map entry forward is built on (see applyOverlayFile).
func decodeFileMapping(path string) (map[string]any, error) {
var doc map[string]any
switch filepath.Ext(path) {
case ".yaml", ".yml":
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("%w: reading %s: %w", ErrUnreadable, path, err)
}
if err := yaml.Unmarshal(data, &doc); err != nil {
return nil, fmt.Errorf("%w: parsing %s: %w", ErrUnreadable, path, err)
}
case ".toml":
if _, err := toml.DecodeFile(path, &doc); err != nil {
return nil, fmt.Errorf("%w: parsing %s: %w", ErrUnreadable, path, err)
}
default:
return nil, fmt.Errorf("%w: config file %s must end in .toml, .yaml, or .yml", ErrUnreadable, path)
}
return doc, nil
}
// decodeTOMLKeys decodes a TOML file into target, additionally
// returning the top-level dotted key paths present in the file that
// target did not consume (toml.MetaData.Undecoded()). A single decode
@@ -72,15 +98,25 @@ func decodeYAML(path string, target any) error {
// into cfg directly. Memory and tools are sub-structs whose keys sit at the
// top level of their own file rather than nested under a "memory:" or
// "tools:" key, so they decode into the sub-struct.
func decodeFeature(cfg *config.Config, feature Feature, path string) error {
//
// An overlay layer's feature file is folded over the base layer, for the same
// reason and with the same precedence as a main overlay file. The keys that fold
// reports are dropped here: only a main file reports unknown keys today
// (decodeMain), and a feature file reporting a second, differently-shaped set
// would be a new surface rather than a fix to this one.
func decodeFeature(cfg *config.Config, feature Feature, path string, layer Layer) error {
target := any(cfg)
switch feature {
case FeatureMemory:
return decodeYAML(path, &cfg.Memory)
target = &cfg.Memory
case FeatureTools:
return decodeYAML(path, &cfg.Tools)
default:
return decodeYAML(path, cfg)
target = &cfg.Tools
}
if layer == LayerOverlay {
_, err := applyOverlayFile(path, target)
return err
}
return decodeYAML(path, target)
}
// decodeExtra decodes an unrecognised conf.d/ file into cfg.Extra under name.
+100 -12
View File
@@ -5,6 +5,7 @@ import (
"log/slog"
"os"
"path/filepath"
"reflect"
"sort"
"github.com/samcharles93/archie-core/internal/config"
@@ -146,7 +147,7 @@ func (l *Loader) ApplyOverlay(doc *Document, overrides map[string]any) (*Documen
next.Config = doc.Config.Clone()
next.Provenance.Origins = append([]Origin(nil), doc.Provenance.Origins...)
if len(overrides) == 0 {
return l.finalize(&next)
return l.finalize(&next, Validate)
}
if err := ApplyOverlayValues(&next.Config, overrides); err != nil {
return nil, err
@@ -155,7 +156,7 @@ func (l *Loader) ApplyOverlay(doc *Document, overrides map[string]any) (*Documen
return nil, err
}
next.Provenance.record(Origin{Path: "config_overlay (runtime)", Role: RoleMain, Layer: LayerOverlay})
return l.finalize(&next)
return l.finalize(&next, Validate)
}
func (l *Loader) overlayFile(basePath, overlayPath string) (*Document, error) {
@@ -173,7 +174,10 @@ func (l *Loader) overlayFile(basePath, overlayPath string) (*Document, error) {
doc.Provenance.record(Origin{Path: basePath, Role: RoleMain, Layer: LayerBase})
if overlayPath != "" {
cfgKeys, err = decodeConfigFileKeys(overlayPath, &doc.Config)
// The overlay is folded over the base config rather than decoded into
// it: a decode replaces a map-valued entry wholesale, clearing the
// fields of that entry the overlay does not name (applyOverlayFile).
cfgKeys, err = applyOverlayFile(overlayPath, &doc.Config)
if err != nil {
return nil, err
}
@@ -185,7 +189,72 @@ func (l *Loader) overlayFile(basePath, overlayPath string) (*Document, error) {
doc.Provenance.record(Origin{Path: overlayPath, Role: RoleMain, Layer: LayerOverlay})
}
return l.finalize(doc)
return l.finalize(doc, validateBootstrap)
}
// overlayFileKeys reports the keys of an overlay file that nothing consumes. It
// has two sources, because the file's own decode and the fold's apply decode do
// not match keys the same way: TOML accepts a key that differs only in case,
// while the yaml decode the fold uses does not. A key only the file's decode
// matches would therefore be consumed by the report below and dropped in
// silence by the apply, so the apply's own view of the mapping is unioned in.
//
// The second decode here is deliberate: it is what carries TOML's Undecoded()
// view of the file, which the raw mapping cannot report (it consumes every key).
func overlayFileKeys(path string, mapping map[string]any, target reflect.Type) ([]string, error) {
decoded, err := decodeConfigFileKeys(path, reflect.New(target).Interface())
if err != nil {
return nil, err
}
return append(decoded, unmatchableKeys(mapping, target)...), nil
}
// unmatchableKeys returns the dotted paths of the keys in mapping that the fold's
// yaml decode cannot match to a field of target. It descends the way foldOverrides
// does -- through structs and through string-keyed maps of structs -- so it sees
// the same keys the apply does. A slice of tables is not descended: the yaml
// decode replaces it wholesale, so anything inside it is a value, not a key.
func unmatchableKeys(mapping map[string]any, target reflect.Type) []string {
var out []string
for key, value := range mapping {
field, ok := yamlFieldOf(target, key)
if !ok {
out = append(out, key)
continue
}
sub, ok := value.(map[string]any)
if !ok {
continue
}
switch field.Type.Kind() {
case reflect.Struct:
out = append(out, prefixed(key, unmatchableKeys(sub, field.Type))...)
case reflect.Map:
entry := field.Type.Elem()
for entry.Kind() == reflect.Pointer {
entry = entry.Elem()
}
if entry.Kind() != reflect.Struct || field.Type.Key().Kind() != reflect.String {
continue
}
for entryName, entryValue := range sub {
entryMapping, ok := entryValue.(map[string]any)
if !ok {
continue
}
out = append(out, prefixed(key+"."+entryName, unmatchableKeys(entryMapping, entry))...)
}
}
}
return out
}
// prefixed qualifies a nested key report with the path that reached it.
func prefixed(prefix string, keys []string) []string {
for i, key := range keys {
keys[i] = prefix + "." + key
}
return keys
}
// unknownKeys returns the keys present in both a and b: one config file
@@ -275,7 +344,7 @@ func (l *Loader) Dir(baseDir, overlayDir string) (*Document, error) {
}
}
return l.finalize(doc)
return l.finalize(doc, validateBootstrap)
}
// loadDir discovers and decodes one directory into doc. The base layer must
@@ -288,7 +357,7 @@ func (l *Loader) loadDir(doc *Document, dir string, layer Layer) error {
switch path, isYAMLFile, ok := files.main(); {
case ok:
if err := l.decodeMain(doc, path, isYAMLFile); err != nil {
if err := l.decodeMain(doc, path, isYAMLFile, layer); err != nil {
return err
}
doc.Provenance.record(Origin{Path: path, Role: RoleMain, Layer: layer})
@@ -298,7 +367,7 @@ func (l *Loader) loadDir(doc *Document, dir string, layer Layer) error {
for _, feature := range files.sortedFeatures() {
path := files.features[feature]
if err := decodeFeature(&doc.Config, feature, path); err != nil {
if err := decodeFeature(&doc.Config, feature, path, layer); err != nil {
return err
}
doc.Provenance.record(Origin{Path: path, Role: RoleFeature, Layer: layer, Feature: feature})
@@ -315,8 +384,20 @@ func (l *Loader) loadDir(doc *Document, dir string, layer Layer) error {
return nil
}
// decodeMain decodes the daemon-level file in whichever format it uses.
func (l *Loader) decodeMain(doc *Document, path string, isYAMLFile bool) error {
// decodeMain decodes the daemon-level file in whichever format it uses. An
// overlay layer's file is FOLDED over what the base layer decoded, the same way
// a single overlay file folds over its base -- otherwise a map-valued entry it
// only partly addresses would clear the fields it does not name, which is the
// archie-core-e2e2 failure on the directory form of the same flag.
func (l *Loader) decodeMain(doc *Document, path string, isYAMLFile bool, layer Layer) error {
if layer == LayerOverlay {
cfgKeys, err := applyOverlayFile(path, &doc.Config)
if err != nil {
return err
}
doc.UnknownKeys = append(doc.UnknownKeys, cfgKeys...)
return decodeSchedulingFile(path, &doc.Scheduling)
}
if isYAMLFile {
if err := decodeYAML(path, &doc.Config); err != nil {
return err
@@ -336,12 +417,19 @@ func (l *Loader) decodeMain(doc *Document, path string, isYAMLFile bool) error {
}
// finalize applies defaults, then validates. The order matters: validation
// judges the effective configuration, including values the operator never
// judges the defaulted configuration, including values the operator never
// wrote.
func (l *Loader) finalize(doc *Document) (*Document, error) {
//
// validate is a parameter because the two kinds of caller produce different
// documents. Every file source ([Loader.File], [Loader.Overlay], [Loader.Dir])
// produces a BOOTSTRAP document and passes validateBootstrap: the settings the
// control plane owns are layered over it later, so a stale value in one of them
// must not fail the load. [Loader.ApplyOverlay] layers overrides onto a
// document that is already effective, so it passes [Validate].
func (l *Loader) finalize(doc *Document, validate func(*config.Config) error) (*Document, error) {
doc.UnknownKeys = sortedUnique(append(doc.UnknownKeys, unregisteredServiceKeys(doc.Config.Services)...))
l.applyDefaults(&doc.Config)
if err := Validate(&doc.Config); err != nil {
if err := validate(&doc.Config); err != nil {
return nil, err
}
l.log.Debug("configuration loaded", "sources", doc.Provenance.String())
@@ -236,31 +236,80 @@ func TestLoadTelegramTokenSecretRefAndLegacyFallback(t *testing.T) {
}
}
// TestLoadRejectsInvalidConfigEnumsAndGlobs pins which layer rejects each kind
// of invalid value. Every file source produces a BOOTSTRAP document, and the
// settings the control plane owns are layered over it later; a stale TOML value
// in one of those must not fail the load (docs/prds/runtime-control-plane.md,
// "Bootstrap, migration, and recovery"). So a check over a database-owned
// setting is asserted with Validate instead -- what boot.runtimeConfig runs over
// the document a process will use, and the checks the loader no longer applies.
// Each case asserts both halves it applies to, so a check that quietly
// disappeared fails the case rather than passing it.
func TestLoadRejectsInvalidConfigEnumsAndGlobs(t *testing.T) {
tests := []struct {
name string
extra string
name string
body string
wantLoadErr bool
}{
{name: "dispatch trigger", extra: "\n[dispatch]\ntrigger = \"labels\"\n"},
{name: "provider userinfo", extra: "\n[providers.openai]\nclass = \"openai\"\nbase_url = \"https://token@example.com/v1\"\n"},
{name: "provider query secret", extra: "\n[providers.openai]\nclass = \"openai\"\nbase_url = \"https://example.com/v1?api_key=secret\"\n"},
{name: "test glob", extra: "\n[[repos]]\nowner = \"acme\"\nname = \"app\"\ntest_glob = \"[\"\n"},
{
name: "dispatch trigger",
body: fileConfigPrefix + "[dispatch]\ntrigger = \"labels\"\n",
},
{
name: "provider userinfo",
body: fileConfigPrefix + "[providers.openai]\nclass = \"openai\"\nbase_url = \"https://token@example.com/v1\"\n",
},
{
name: "provider query secret",
body: fileConfigPrefix + "[providers.openai]\nclass = \"openai\"\nbase_url = \"https://example.com/v1?api_key=secret\"\n",
},
{
name: "test glob",
body: fileConfigPrefix + "[[repos]]\nowner = \"acme\"\nname = \"app\"\ntest_glob = \"[\"\n",
},
{
// File-owned settings: no control-plane resource carries them, so
// the load path is the layer that has to reject them.
name: "forge intake",
body: fileConfigPrefix + "[forge]\nintake = \"not-a-real-intake\"\n",
wantLoadErr: true,
},
{
name: "memory engine",
body: fileConfigPrefix + "[memory]\nengine = \"not-a-real-engine\"\n",
wantLoadErr: true,
},
{
name: "negative capture retention",
body: fileConfigPrefix + "[capture]\nretention = \"-1h\"\n",
wantLoadErr: true,
},
{
name: "image default naming no provider",
body: fileConfigPrefix + "[image]\ndefault = \"not-a-real-provider\"\n",
wantLoadErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
contents := "bot_user = \"widget\"\n"
if tt.name != "test glob" {
contents += "\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"
}
contents += tt.extra
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
if err := os.WriteFile(path, []byte(tt.body), 0o600); err != nil {
t.Fatal(err)
}
if _, err := loadFile(path); err == nil {
t.Fatal("loadFile() succeeded, want validation error")
doc, err := New(nil).File(path)
if tt.wantLoadErr {
if err == nil {
t.Fatal("File() = nil error, want the bootstrap document rejected")
}
return
}
if err != nil {
t.Fatalf("File: %v (a database-owned value must not fail the load)", err)
}
if err := Validate(&doc.Config); err == nil {
t.Fatal("Validate = nil, want the value rejected there")
}
})
}
@@ -487,6 +536,11 @@ func TestLoadContainerVolumeTTL(t *testing.T) {
}
}
// TestLoadRejectsNegativeContainerVolumeTTL: containers are a control-plane
// resource (container-runtime-policies), so a negative volume_ttl is judged by
// Validate rather than by the load path -- a stale TOML value in a database-owned
// setting must not fail a process's startup. The second half of the assertion is
// what keeps dropping the check from passing this test.
func TestLoadRejectsNegativeContainerVolumeTTL(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
contents := "bot_user = \"widget\"\n" +
@@ -497,8 +551,12 @@ func TestLoadRejectsNegativeContainerVolumeTTL(t *testing.T) {
t.Fatal(err)
}
if _, err := loadFile(path); err == nil {
t.Fatal("loadFile() accepted a negative containers.volume_ttl")
doc, err := New(nil).File(path)
if err != nil {
t.Fatalf("File: %v (a database-owned value must not fail the load)", err)
}
if err := Validate(&doc.Config); err == nil {
t.Fatal("Validate accepted a negative containers.volume_ttl")
}
}
@@ -571,6 +629,274 @@ func TestOverlay(t *testing.T) {
}
}
// TestResolveFileOverlayPreservesOmittedMapEntryFields characterizes the
// -config-overlay path at the depth the runtime overlay is already guarded at
// (TestApplyOverlayValuesPreservesOmittedNestedFields).
//
// It goes through Loader.Resolve, the production entry point, because the
// hazard was a property of how the overlay file is applied, not of any one
// decode helper: Loader.overlayFile decoded the overlay into the very config
// the base file produced, and a map-valued entry is replaced wholesale, so every
// field of that entry the overlay does not name was cleared.
//
// That is archie-core-e2e2 on a user-visible path. deployments/dev.toml is the
// documented -config-overlay argument and sets [services.state] target/listen,
// while the installed config.toml is where [services.state].target_token lives:
// the overlay silently dropped the token. The directory form of the same flag is
// covered by TestDirOverlayPreservesOmittedMapEntryFields.
func TestResolveFileOverlayPreservesOmittedMapEntryFields(t *testing.T) {
tests := []struct {
name string
base string
overlayName string
overlay string
check func(t *testing.T, cfg config.Config)
}{
{
// The deployment shape quoted above, with the value that is read
// only when a client dials a non-loopback State Store.
name: "services.state entry keeps the token it does not name",
base: "[services.state]\ntarget = \"127.0.0.1:9090\"\ntarget_token = \"secret\"\n",
overlay: "[services.state]\ntarget = \"10.0.0.5:9090\"\n",
check: func(t *testing.T, cfg config.Config) {
t.Helper()
got := cfg.Services[config.ServiceNameState]
if got.Target != "10.0.0.5:9090" {
t.Errorf("services.state.target = %q, want the overlay's 10.0.0.5:9090", got.Target)
}
if got.TargetToken != "secret" {
t.Errorf("services.state.target_token = %q, want the base's secret: a file overlay must not clear a field it does not name", got.TargetToken)
}
},
},
{
// An enabled hosted provider must keep its class and key env: the
// overlay naming only base_url would otherwise clear them and fail
// validation after the load.
name: "image.hosted entry keeps the fields it does not name",
base: "[image.hosted.minimax]\nenabled = true\nclass = \"minimax\"\n" +
"api_key_env = \"MINIMAX_API_KEY\"\n",
overlay: "[image.hosted.minimax]\nbase_url = \"https://api.example\"\n",
check: func(t *testing.T, cfg config.Config) {
t.Helper()
got := cfg.Image.Hosted["minimax"]
want := config.ImageHostedProvider{
Enabled: true,
Class: "minimax",
APIKeyEnv: "MINIMAX_API_KEY",
BaseURL: "https://api.example",
}
if got != want {
t.Errorf("image.hosted.minimax = %+v, want %+v", got, want)
}
},
},
{
name: "providers entry keeps the fields it does not name",
base: "[providers.anthropic]\nclass = \"anthropic\"\n" +
"api_key_env = \"ANTHROPIC_API_KEY\"\n",
overlay: "[providers.anthropic]\nbase_url = \"https://proxy.example\"\n",
check: func(t *testing.T, cfg config.Config) {
t.Helper()
got := cfg.Providers["anthropic"]
want := config.Provider{
Class: "anthropic",
APIKeyEnv: "ANTHROPIC_API_KEY",
BaseURL: "https://proxy.example",
}
if got != want {
t.Errorf("providers.anthropic = %+v, want %+v", got, want)
}
},
},
{
// The overlay file may be YAML (-config-overlay accepts either
// format), and it is folded by the same code.
name: "yaml overlay keeps the token it does not name",
base: "[services.state]\ntarget = \"127.0.0.1:9090\"\ntarget_token = \"secret\"\n",
overlayName: "dev.yaml",
overlay: "services:\n state:\n target: 10.0.0.5:9090\n",
check: func(t *testing.T, cfg config.Config) {
t.Helper()
got := cfg.Services[config.ServiceNameState]
if got.Target != "10.0.0.5:9090" {
t.Errorf("services.state.target = %q, want the overlay's 10.0.0.5:9090", got.Target)
}
if got.TargetToken != "secret" {
t.Errorf("services.state.target_token = %q, want the base's secret", got.TargetToken)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
basePath := filepath.Join(dir, "config.toml")
overlayName := tt.overlayName
if overlayName == "" {
overlayName = "dev.toml"
}
overlayPath := filepath.Join(dir, overlayName)
if err := os.WriteFile(basePath, []byte(minimalValidConfigTOML+tt.base), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(overlayPath, []byte(tt.overlay), 0o600); err != nil {
t.Fatal(err)
}
doc, err := New(nil).Resolve(basePath, overlayPath)
if err != nil {
t.Fatalf("Resolve: %v", err)
}
tt.check(t, doc.Config)
})
}
}
// TestLoadAcceptsStaleDatabaseOwnedValues is the bootstrap half of the layer
// split (archie-core-i3qm). docs/prds/runtime-control-plane.md,
// "Bootstrap, migration, and recovery": after migration, settings in TOML are
// ignored and cannot block State Store startup. The other half is the boot
// itself -- archie-state-store seeds these same settings into the control plane
// on a fresh database, and a seed it cannot validate is skipped rather than fatal
// (TestStateStoreBootsWithStaleDatabaseOwnedValues boots the process for that).
//
// Each case is a setting with a control-plane resource behind it, seeded from
// that field by controlplane.Server.ImportConfig: providers
// (provider-settings), poll_interval and dispatch (scheduling-policy),
// containers (container-runtime-policies), repositories
// (repository-policies). The second assertion is what keeps this from being a
// weaker gate: the value is still rejected by Validate, which is what the daemon
// and the Gateway run over the document a process will actually use. Nothing in
// this package layers that document, so the check here is the file document on
// its own -- the test is that the loader judges no database-owned field, not
// that the value is harmless.
func TestLoadAcceptsStaleDatabaseOwnedValues(t *testing.T) {
tests := []struct {
name string
body string
}{
{
name: "providers base_url carries userinfo",
body: fileConfigPrefix + "[providers.openai]\nclass = \"openai\"\nbase_url = \"https://token@example.com/v1\"\n",
},
{
name: "negative poll_interval",
body: fileConfigPrefix + "poll_interval = \"-5s\"\n",
},
{
name: "unrecognised dispatch.trigger",
body: fileConfigPrefix + "[dispatch]\ntrigger = \"labels\"\n",
},
{
name: "negative containers.volume_ttl",
body: fileConfigPrefix + "[containers]\nvolume_ttl = \"-1m\"\n",
},
{
name: "malformed repos test_glob",
body: fileConfigPrefix + "[[repos]]\nowner = \"acme\"\nname = \"app\"\ntest_glob = \"[\"\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte(tt.body), 0o600); err != nil {
t.Fatal(err)
}
doc, err := New(nil).File(path)
if err != nil {
t.Fatalf("File: %v (a stale database-owned value must not fail the load)", err)
}
if err := Validate(&doc.Config); err == nil {
t.Fatal("Validate = nil, want the control plane's own layer to reject it")
}
})
}
}
// fileConfigPrefix is the smallest document the load path accepts on its own,
// so each case above adds one stale setting and nothing else.
const fileConfigPrefix = "bot_user = \"widget\"\nwork_dir = \"/base/work\"\n"
// TestDirOverlayPreservesOmittedMapEntryFields is the directory form of the
// same guarantee TestResolveFileOverlayPreservesOmittedMapEntryFields pins.
// -config-overlay accepts a directory as well as a file, Loader.Dir documents
// "the same field-level precedence as [Loader.Overlay]", and Loader.Resolve
// routes a directory source to Dir -- so a map-valued entry the overlay only
// partly addresses must keep the fields it does not name on both forms of the
// flag, including when the entry arrives through a feature file.
func TestDirOverlayPreservesOmittedMapEntryFields(t *testing.T) {
tests := []struct {
name string
baseFiles map[string]string
overlayFiles map[string]string
check func(t *testing.T, cfg config.Config)
}{
{
name: "main config keeps the token it does not name",
baseFiles: map[string]string{
"config.yaml": "bot_user: widget\nservices:\n state:\n target: 127.0.0.1:9090\n target_token: secret\n",
},
overlayFiles: map[string]string{
"config.yaml": "services:\n state:\n target: 10.0.0.5:9090\n",
},
check: func(t *testing.T, cfg config.Config) {
t.Helper()
got := cfg.Services[config.ServiceNameState]
if got.Target != "10.0.0.5:9090" {
t.Errorf("services.state.target = %q, want the overlay's 10.0.0.5:9090", got.Target)
}
if got.TargetToken != "secret" {
t.Errorf("services.state.target_token = %q, want the base's secret: a directory overlay must not clear a field it does not name", got.TargetToken)
}
},
},
{
name: "feature file keeps the fields it does not name",
baseFiles: map[string]string{
"config.yaml": "bot_user: widget\n",
"config.models.yaml": "providers:\n anthropic:\n class: anthropic\n api_key_env: ANTHROPIC_API_KEY\n",
},
overlayFiles: map[string]string{
"config.models.yaml": "providers:\n anthropic:\n base_url: https://proxy.example\n",
},
check: func(t *testing.T, cfg config.Config) {
t.Helper()
got := cfg.Providers["anthropic"]
want := config.Provider{
Class: "anthropic",
APIKeyEnv: "ANTHROPIC_API_KEY",
BaseURL: "https://proxy.example",
}
if got != want {
t.Errorf("providers.anthropic = %+v, want %+v", got, want)
}
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
baseDir, overlayDir := tmpConfigDir(t), tmpConfigDir(t)
for name, content := range tt.baseFiles {
writeFile(t, baseDir, name, content)
}
for name, content := range tt.overlayFiles {
writeFile(t, overlayDir, name, content)
}
doc, err := New(nil).Resolve(baseDir, overlayDir)
if err != nil {
t.Fatalf("Resolve: %v", err)
}
tt.check(t, doc.Config)
})
}
}
func TestLoadBytesDoesNotExist(t *testing.T) {
_, err := loadBytes(nil)
if err == nil {
@@ -89,6 +89,60 @@ func TestUnknownSchedulingKeyIsStillReported(t *testing.T) {
}
}
// TestOverlayUnknownKeysAreStillReported pins that the overlay path kept its
// drift detection when the overlay file started being applied through the
// config fold instead of being decoded into the document's config. That matters
// twice over there: the report comes from a separate decode, and a file is
// parsed by TOML while the fold applies it with a yaml decode, which do not
// match keys the same way. A key the APPLY cannot consume has to be reported,
// or a spelling that works in the base config would silently do nothing in the
// overlay.
func TestOverlayUnknownKeysAreStillReported(t *testing.T) {
tests := []struct {
name string
overlay string
wantUnknown string
}{
{
name: "a key no decode target consumes",
overlay: "[containers]\nmax_concurrancy = 4\n", // typo: concurrancy
wantUnknown: "containers.max_concurrancy",
},
{
// TOML matches keys case-insensitively, so the file's own decode
// consumes BOT_USER and reports nothing; the apply folds the file's
// mapping with a yaml decode, which does not match it. Without the
// second source for this report the value would be dropped in
// silence, so the spelling is reported instead.
name: "a key only the file decode can match",
overlay: "BOT_USER = \"upper\"\n",
wantUnknown: "BOT_USER",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dir := t.TempDir()
basePath := filepath.Join(dir, "config.toml")
overlayPath := filepath.Join(dir, "dev.toml")
if err := os.WriteFile(basePath, []byte(minimalValidConfigTOML), 0o600); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(overlayPath, []byte(tt.overlay), 0o600); err != nil {
t.Fatal(err)
}
doc, err := New(nil).Resolve(basePath, overlayPath)
if err != nil {
t.Fatalf("Resolve: %v (an unknown key must not fail the load)", err)
}
if !slices.Contains(doc.UnknownKeys, tt.wantUnknown) {
t.Errorf("UnknownKeys = %v, want it to contain %q", doc.UnknownKeys, tt.wantUnknown)
}
})
}
}
// TestExampleConfigHasNoUnknownKeys is the contract test the design
// promised: the checked-in template must never itself trip the detector.
func TestExampleConfigHasNoUnknownKeys(t *testing.T) {
+134 -28
View File
@@ -37,9 +37,23 @@ var (
memoryEngines = []string{memoryEngineBuiltin}
)
// Validate runs the same checks Loader applies before accepting a config,
// against a config.Config value already built in memory -- e.g. by archied
// setup, before it has written anything to disk.
// Validate judges a configuration a process will run with -- every check that
// can reject one -- against a config.Config value already built in memory, e.g.
// by archied setup before it has written anything to disk. It is a superset of
// what [Loader] applies to a file (validateBootstrap): a check over a setting
// the control plane owns belongs here and not there, so that a stale TOML value
// cannot fail a process's startup.
//
// It judges an EFFECTIVE document: the bootstrap file with every setting the
// control plane owns layered over it. That layering happens at exactly one
// place, boot.runtimeConfig (internal/app/archied/control_plane.go), which runs
// this afterwards; the daemon and the Gateway both go through it, and the
// readiness config probe re-runs it against the running value. A database value
// that will not validate therefore stops the process rather than starting it
// degraded.
//
// A plain file document is judged by validateBootstrap instead: see the split
// there for why the loader must not apply this to it.
//
// Like validate, it does not apply defaults. Several checks (dispatch.trigger,
// forge.type) only pass once a default has been filled
@@ -58,31 +72,37 @@ func Validate(cfg *config.Config) error {
// validate reports the first problem that would stop the daemon running.
// It does not modify cfg -- run applyDefaults first.
func validate(cfg *config.Config) error {
if err := validateDispatch(cfg); err != nil {
if err := validateBootstrap(cfg); err != nil {
return err
}
return validateDatabaseOwned(cfg)
}
// validateBootstrap reports the first problem in the settings a bootstrap
// document still owns: the file config a process reads before it can reach the
// State Store. [Loader] applies this to every file source.
//
// It deliberately omits the settings the control plane stores, because a stale
// TOML value in one of them must not be able to fail a process's startup
// (docs/prds/runtime-control-plane.md, "Bootstrap, migration, and recovery":
// after migration, settings in TOML are ignored and cannot block State Store
// startup). archie-state-store resolves its file config and seeds the
// control-plane resources from that same document on a fresh database, and a
// seed it cannot validate is skipped rather than fatal
// (controlplane.Server.ImportConfig), so a check left here is a check that can
// still block it. Those checks live in validateDatabaseOwned and run on the
// effective document instead -- which is where the process that uses the value
// refuses it, and where the operator's fix is the file again.
func validateBootstrap(cfg *config.Config) error {
if err := validateForgeIntake(cfg); err != nil {
return err
}
if err := validateProviders(cfg.Providers); err != nil {
return err
}
if len(cfg.Identities) > 0 {
if err := validateIdentities(cfg.Identities); err != nil {
return err
}
} else if err := validateSingleIdentity(cfg); err != nil {
return err
}
if err := validatePollInterval(cfg); err != nil {
if err := validateIdentityStructure(cfg); err != nil {
return err
}
if err := validateNATS(cfg); err != nil {
return err
}
if err := validateContainers(cfg); err != nil {
return err
}
if err := validateMemory(cfg); err != nil {
return err
}
@@ -92,6 +112,30 @@ func validate(cfg *config.Config) error {
return validateCapture(cfg)
}
// validateDatabaseOwned reports the first problem in a setting the control
// plane owns. Each check below judges a field that a control-plane resource is
// seeded from and replaces wholesale: providers (provider-settings),
// poll_interval and dispatch (scheduling-policy), containers
// (container-runtime-policies), and the repository lists
// (repository-policies). It runs on the effective document, after that
// replacement, which is the only point at which the value being judged is the
// value a process will use.
func validateDatabaseOwned(cfg *config.Config) error {
if err := validateDispatch(cfg); err != nil {
return err
}
if err := validateProviders(cfg.Providers); err != nil {
return err
}
if err := validatePollInterval(cfg); err != nil {
return err
}
if err := validateContainers(cfg); err != nil {
return err
}
return validateRepositoryContents(cfg)
}
// validateImage rejects an enabled hosted provider with no class or no
// resolvable credential, and a Default naming a provider that is not both
// present and enabled -- either would let /image silently fall back to
@@ -186,7 +230,7 @@ func validatePollInterval(cfg *config.Config) error {
// labels) was configured while the actual trigger-match label was left
// blank.
func validateDispatch(cfg *config.Config) error {
if !oneOf(cfg.Dispatch.Trigger, dispatchTriggers) {
if !DispatchTriggerValid(cfg.Dispatch.Trigger) {
return fmt.Errorf("%w: dispatch.trigger %q (want %s)", ErrInvalidInput, cfg.Dispatch.Trigger, list(dispatchTriggers))
}
if (cfg.Dispatch.Trigger == dispatchTriggerLabel || cfg.Dispatch.Trigger == dispatchTriggerEither) && cfg.Label == "" {
@@ -195,6 +239,16 @@ func validateDispatch(cfg *config.Config) error {
return nil
}
// DispatchTriggerValid reports whether trigger names a discovery rule the daemon
// can poll with. It is exported because the scheduling-policy resource validator
// (internal/app/controlplane) judges the same field: that resource is seeded
// from cfg.Dispatch and replaces it wholesale once the database owns it, so a
// value this package rejects must not be storable, and a value it accepts must
// not be rejected there. One definition, both layers.
func DispatchTriggerValid(trigger string) bool {
return oneOf(trigger, dispatchTriggers)
}
// validateForgeIntake checks the forge intake mode and that webhook intake has
// the secret and listen address it needs. An unset intake resolves to "poll"
// without mutating cfg, so Validate (which does not apply defaults) accepts a
@@ -237,6 +291,18 @@ func validateProviders(providers map[string]config.Provider) error {
return nil
}
// validateIdentityStructure judges the shape of the identity definitions: the
// fields a process needs to know which identities exist and which credentials
// each one commits and calls with. The repository lists inside them are judged
// by validateRepositoryContents, because repositories are a control-plane
// resource that replaces the file's list after the merge.
func validateIdentityStructure(cfg *config.Config) error {
if len(cfg.Identities) == 0 {
return validateSingleIdentity(cfg)
}
return validateIdentities(cfg.Identities)
}
func validateIdentities(identities []config.IdentityConfig) error {
for i, id := range identities {
if id.Name == "" {
@@ -254,9 +320,6 @@ func validateIdentities(identities []config.IdentityConfig) error {
if !ForgeDisabled(id.Forge.Type) && id.Forge.Token == (config.SecretRef{}) {
return fmt.Errorf("%w: identities[%d].forge.token is required (each identity needs its own secret reference; unlike the top-level [forge], there is no default)", ErrInvalidInput, i)
}
if err := validateRepos(id.Repos); err != nil {
return fmt.Errorf("identities[%d]: %w", i, err)
}
}
return nil
}
@@ -278,19 +341,62 @@ func validateSingleIdentity(cfg *config.Config) error {
if !oneOf(cfg.Forge.Type, forgeTypes) {
return fmt.Errorf("%w: forge.type %q (want %s)", ErrInvalidInput, cfg.Forge.Type, list(forgeTypes))
}
return validateRepos(cfg.Repos)
return nil
}
func validateRepos(repos []config.Repo) error {
// validateRepositoryContents judges the repositories themselves, whichever list
// they appear in. The shared list is the decision the database's copy replaces,
// so it is verified on the effective document -- after that replacement, which is
// the only point the value being judged is the one a process will use. The
// per-identity lists are verified there for a different reason: no resource
// carries them, and the daemon is the only process that reads them
// (docs/architecture/configuration.md's IdentityConfig note).
func validateRepositoryContents(cfg *config.Config) error {
if len(cfg.Identities) == 0 {
return ValidateRepositories(cfg.Repos)
}
for i, id := range cfg.Identities {
if err := ValidateRepositories(id.Repos); err != nil {
return fmt.Errorf("identities[%d]: %w", i, err)
}
}
return nil
}
// ValidateRepositories judges a repository list: owner and name present, no
// duplicate entries, and a test glob the gate can compile. It is exported
// because the repository-policies resource validator (internal/app/controlplane)
// judges the same list -- that resource is seeded from these and replaces them
// wholesale, so the two layers must not disagree about which lists are valid, in
// either direction: a list one layer accepts and the other refuses is a config
// that boots file-side and stores nowhere, or a stored value the daemon then
// refuses to start with.
func ValidateRepositories(repos []config.Repo) error {
seen := make(map[string]struct{}, len(repos))
for i, r := range repos {
if r.Owner == "" || r.Name == "" {
return fmt.Errorf("%w: repos[%d] needs owner and name", ErrInvalidInput, i)
}
if glob := r.ResolvedTestGlob(); glob != "" {
if _, err := filepath.Match(glob, ""); err != nil {
return fmt.Errorf("%w: repos[%d] test_glob %q: %w", ErrInvalidInput, i, glob, err)
}
if _, exists := seen[r.FullName()]; exists {
return fmt.Errorf("%w: repos[%d] duplicates %q", ErrInvalidInput, i, r.FullName())
}
seen[r.FullName()] = struct{}{}
if err := validateTestGlob(r.ResolvedTestGlob()); err != nil {
return fmt.Errorf("%w: repos[%d] %w", ErrInvalidInput, i, err)
}
}
return nil
}
// validateTestGlob reports whether glob is a pattern the test-protection gate can
// compile. It has one caller on each side through ValidateRepositories, so the
// rule has one definition rather than a copy per layer.
func validateTestGlob(glob string) error {
if glob == "" {
return nil
}
if _, err := filepath.Match(glob, ""); err != nil {
return fmt.Errorf("test_glob %q: %w", glob, err)
}
return nil
}
@@ -69,6 +69,12 @@ func TestValidate_IsCallableOutsideThePackage(t *testing.T) {
}
}
// TestValidate_RejectsTheSameProblemsAsLoaderLoad pins the direction that still
// holds after archie-core-i3qm split the bootstrap checks from the effective
// ones: every problem the load path rejects, Validate rejects too. Validate
// additionally rejects the settings the control plane owns, which the load path
// must let through (validateBootstrap) so a stale TOML value cannot fail a
// process's startup. The name is kept from when the two check sets were equal.
func TestValidate_RejectsTheSameProblemsAsLoaderLoad(t *testing.T) {
tests := []struct {
name string