Files
archie/internal/infrastructure/configuration/loader.go
T
sam da2d93a830 feat(config): runtime config overlay store and PATCH /api/config
The second half of #348's storage layer: dashboard-edited overrides
live in their own SQLite file, separate from the task and conversation
stores so each file owns its user_version and migrator with no
contention (configDBPath -> cfg.DBPath + "-config.sqlite", matching
the taskDBPath / conversationDBPath convention).

Store (internal/infrastructure/configuration/overlay):
- config_overlay(key TEXT PRIMARY KEY, value TEXT NOT NULL,
  updated_at, updated_by), own migration, WAL.
- Set refuses DeniedKeys (db_path, work_dir) at write time as well as
  at the API layer; the map is exported so the dashboard can show the
  reason instead of silently omitting the edit affordance.
- Snapshot JSON-decodes each value and nests dotted keys
  ("budgets.max_steps" -> map[b][m]) ready to decode into Config.

Loader (ApplyOverlay / ApplyOverlayValues):
- Decodes a nested overlay map with the same field-level precedence as
  a file overlay, records the runtime-overlay origin, then applies
  defaults and validates. Used at boot, on SIGHUP reload (ReloadController
  gained an optional WithOverlay source) and by the PATCH path.

Boot and reload:
- A broken overlay (unopenable store, unreadable snapshot, values that
  fail validation) degrades to file config alone -- it must never brick
  the daemon. --no-config-overlay / ARCHIE_SKIP_CONFIG_OVERLAY=1 skips
  the store entirely; the flag is now a real gate, not a log-only stub.

PATCH /api/config (webui):
- handleConfigUpdate decodes {"updates": {dotted.key: value}}, calls
  the composition-wired UpdateConfig seam, and classifies errors via
  webui sentinels: ErrConfigUpdateInvalid -> 400 (denylisted key,
  failed validation, unparseable value), ErrConfigUpdateUnavailable ->
  503 (overlay skipped by recovery flag), anything else -> 500.
- The seam (main.go) applies updates to a COPY of the published
  config, validates the materialised result, persists to the overlay,
  then publishes through the same path as reload -- it never calls
  Set directly from the handler, since web.Cfg is the daemon's own
  Holder.
- ConfigView gains Locked (dotted key -> reason) so the UI disables
  denylisted rows explicitly.

Verified: task check clean; new tests cover the store round-trip,
denied keys, loader overlay layering/validation/provenance, reload
with and without overlay, and the PATCH handler's 200/400/503 paths.
2026-08-10 00:54:23 +10:00

241 lines
8.0 KiB
Go

package configuration
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"github.com/samcharles93/archie-core/internal/config"
)
// Document is the decoded external configuration together with a record of
// where it came from.
type Document struct {
// Config is the translated settings.
//
// TEMPORARY. docs/architecture/configuration.md requires this to become
// typed per-domain sections that never expose a whole-application model.
// It stays for now so this package can own loading without every
// consumer changing at once. Delete when internal/config is dissolved.
Config config.Config
// Provenance lists the files that produced Config, in precedence order.
Provenance Provenance
}
// Loader reads configuration from files. The zero value is not usable; call
// [New].
type Loader struct {
log *slog.Logger
// dataHomeFn resolves the base directory for derived paths. Injected so
// tests can exercise defaulting without depending on the environment --
// the previous package-level dataHome() read XDG_DATA_HOME directly and
// discarded the error from os.UserHomeDir.
dataHomeFn func() string
}
// New returns a Loader. A nil logger discards output.
func New(log *slog.Logger) *Loader {
if log == nil {
log = slog.New(slog.DiscardHandler)
}
return &Loader{log: log, dataHomeFn: xdgDataHome}
}
// dataHome resolves the base directory for derived paths.
func (l *Loader) dataHome() string {
if l.dataHomeFn == nil {
return xdgDataHome()
}
return l.dataHomeFn()
}
// xdgDataHome returns $XDG_DATA_HOME, falling back to ~/.local/share, and
// finally to a relative path when the home directory is unknowable.
func xdgDataHome() string {
if dir := os.Getenv("XDG_DATA_HOME"); dir != "" {
return dir
}
home, err := os.UserHomeDir()
if err != nil {
return filepath.Join(".local", "share")
}
return filepath.Join(home, ".local", "share")
}
// File loads a single configuration file. TOML remains supported for existing
// deployments; YAML files use the same defaults and validation path.
func (l *Loader) File(path string) (*Document, error) {
return l.Resolve(path, "")
}
// Resolve selects the configuration source from its filesystem type: a TOML
// or YAML file, or a feature-config directory. This is the production entry
// point. Keeping selection here ensures command-line loading and operator
// tooling share the exact same precedence and validation rules.
func (l *Loader) Resolve(basePath, overlayPath string) (*Document, error) {
info, err := os.Stat(basePath)
if err != nil {
return nil, fmt.Errorf("%w: config source %s: %w", ErrUnreadable, basePath, err)
}
if info.IsDir() {
if overlayPath != "" {
overlayInfo, statErr := os.Stat(overlayPath)
if statErr != nil {
return nil, fmt.Errorf("%w: config overlay %s: %w", ErrUnreadable, overlayPath, statErr)
}
if !overlayInfo.IsDir() {
return nil, fmt.Errorf("config overlay %s must be a directory when config source %s is a directory", overlayPath, basePath)
}
}
return l.Dir(basePath, overlayPath)
}
if overlayPath != "" {
overlayInfo, statErr := os.Stat(overlayPath)
if statErr != nil {
return nil, fmt.Errorf("%w: config overlay %s: %w", ErrUnreadable, overlayPath, statErr)
}
if overlayInfo.IsDir() {
return nil, fmt.Errorf("config overlay %s must be a file when config source %s is a file", overlayPath, basePath)
}
}
return l.overlayFile(basePath, overlayPath)
}
// Overlay loads basePath, then decodes overlayPath into the same value so
// only the fields the overlay sets are replaced -- every field it omits keeps
// the base value. This lets a deployment-specific file (config.docker.toml)
// declare only what differs instead of restating the whole schema. An empty
// overlayPath is equivalent to [Loader.File].
func (l *Loader) Overlay(basePath, overlayPath string) (*Document, error) {
return l.overlayFile(basePath, overlayPath)
}
// ApplyOverlay decodes the runtime config overlay (a nested map of
// dotted-path values from the overlay store) into doc with the same
// field-level precedence as a file overlay, records the overlay origin,
// then applies defaults and validates. It is used at boot and on reload
// to layer dashboard-edited overrides over the file config.
//
// doc is decoded in place; pass a copy when the base document must
// survive a decode or validation failure (the boot path does).
func (l *Loader) ApplyOverlay(doc *Document, overrides map[string]any) (*Document, error) {
if len(overrides) == 0 {
return l.finalize(doc)
}
if err := ApplyOverlayValues(&doc.Config, overrides); err != nil {
return nil, err
}
doc.Provenance.record(Origin{Path: "config_overlay (runtime)", Role: RoleMain, Layer: LayerOverlay})
return l.finalize(doc)
}
func (l *Loader) overlayFile(basePath, overlayPath string) (*Document, error) {
doc := &Document{}
if err := decodeConfigFile(basePath, &doc.Config); err != nil {
return nil, err
}
doc.Provenance.record(Origin{Path: basePath, Role: RoleMain, Layer: LayerBase})
if overlayPath != "" {
if err := decodeConfigFile(overlayPath, &doc.Config); err != nil {
return nil, err
}
doc.Provenance.record(Origin{Path: overlayPath, Role: RoleMain, Layer: LayerOverlay})
}
return l.finalize(doc)
}
// Dir loads configuration from a directory tree:
//
// config.yaml daemon-level settings
// config.gateway.yaml chat channels and platforms
// config.tools.yaml MCP servers and tool policy
// config.memory.yaml memory provider settings
// config.models.yaml LLM providers and models
// config.identities.yaml identity settings
// conf.d/*.yaml additional feature files
//
// A missing feature file means the feature is absent, not an error. Legacy
// config.toml is the fallback when config.yaml is absent; YAML wins when
// both exist.
//
// When overlayDir is non-empty its files are applied on top of baseDir, with
// the same field-level precedence as [Loader.Overlay]. The overlay directory
// need not contain a main config.
func (l *Loader) Dir(baseDir, overlayDir string) (*Document, error) {
doc := &Document{}
if err := l.loadDir(doc, baseDir, LayerBase); err != nil {
return nil, err
}
if overlayDir != "" {
if err := l.loadDir(doc, overlayDir, LayerOverlay); err != nil {
return nil, err
}
}
return l.finalize(doc)
}
// loadDir discovers and decodes one directory into doc. The base layer must
// supply a main config; an overlay may contribute feature files alone.
func (l *Loader) loadDir(doc *Document, dir string, layer Layer) error {
files, err := discover(dir)
if err != nil {
return err
}
switch path, isYAMLFile, ok := files.main(); {
case ok:
if err := l.decodeMain(&doc.Config, path, isYAMLFile); err != nil {
return err
}
doc.Provenance.record(Origin{Path: path, Role: RoleMain, Layer: layer})
case layer == LayerBase:
return fmt.Errorf("%w: no config.yaml or config.toml in %s", ErrNoConfigFile, dir)
}
for _, feature := range files.sortedFeatures() {
path := files.features[feature]
if err := decodeFeature(&doc.Config, feature, path); err != nil {
return err
}
doc.Provenance.record(Origin{Path: path, Role: RoleFeature, Layer: layer, Feature: feature})
}
for _, name := range files.sortedExtras() {
path := files.extras[name]
if err := decodeExtra(&doc.Config, name, path); err != nil {
return err
}
doc.Provenance.record(Origin{Path: path, Role: RoleExtra, Layer: layer, Feature: Feature(name)})
}
return nil
}
// decodeMain decodes the daemon-level file in whichever format it uses.
func (l *Loader) decodeMain(cfg *config.Config, path string, isYAMLFile bool) error {
if isYAMLFile {
return decodeYAML(path, cfg)
}
return decodeTOML(path, cfg)
}
// finalize applies defaults, then validates. The order matters: validation
// judges the effective configuration, including values the operator never
// wrote.
func (l *Loader) finalize(doc *Document) (*Document, error) {
l.applyDefaults(&doc.Config)
if err := Validate(&doc.Config); err != nil {
return nil, err
}
l.log.Debug("configuration loaded", "sources", doc.Provenance.String())
return doc, nil
}