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.
191 lines
6.7 KiB
Go
191 lines
6.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"os"
|
|
"reflect"
|
|
"sync/atomic"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/config"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
|
)
|
|
|
|
// ReloadController re-runs the configuration load on demand and
|
|
// publishes the result through apply. A failed reload never publishes:
|
|
// apply is called only with a Document that passed validation, and the
|
|
// running config is left untouched on error. That "Set is not called on
|
|
// the error path" property is the safety core of the reload feature --
|
|
// a bad file must leave the daemon running on the previous config.
|
|
type ReloadController struct {
|
|
loader *configuration.Loader
|
|
base string
|
|
overlay string
|
|
|
|
// apply receives a freshly loaded, validated Document. It is the
|
|
// composition root's job to wire this to whatever owns the running
|
|
// config (the daemon's Holder, the dashboard's provenance). Must be
|
|
// non-nil; newReloadController requires it.
|
|
apply func(*configuration.Document)
|
|
|
|
// overlayValues, when non-nil, returns the runtime config overlay
|
|
// (dotted keys -> typed values) to layer over the file config before
|
|
// publishing. A failed overlay read or validation aborts the reload
|
|
// exactly like a bad file.
|
|
overlayValues func() (map[string]any, error)
|
|
|
|
status atomic.Pointer[config.ReloadStatus]
|
|
}
|
|
|
|
func newReloadController(loader *configuration.Loader, base, overlay string, apply func(*configuration.Document)) *ReloadController {
|
|
c := &ReloadController{loader: loader, base: base, overlay: overlay, apply: apply}
|
|
c.status.Store(&config.ReloadStatus{})
|
|
return c
|
|
}
|
|
|
|
// Reload re-runs the configuration load. On validation failure it
|
|
// records the error in Status and returns it; apply is not called and
|
|
// the running configuration is unchanged. On success it calls apply
|
|
// with the fresh Document and records the reload time.
|
|
func (c *ReloadController) Reload() error {
|
|
doc, err := c.loader.Resolve(c.base, c.overlay)
|
|
if err != nil {
|
|
c.status.Store(&config.ReloadStatus{
|
|
LastError: err.Error(),
|
|
LastErrorAt: time.Now().UTC().Format(time.RFC3339),
|
|
})
|
|
return err
|
|
}
|
|
if c.overlayValues != nil {
|
|
values, err := c.overlayValues()
|
|
if err != nil {
|
|
c.status.Store(&config.ReloadStatus{
|
|
LastError: err.Error(),
|
|
LastErrorAt: time.Now().UTC().Format(time.RFC3339),
|
|
})
|
|
return err
|
|
}
|
|
if len(values) > 0 {
|
|
next := *doc
|
|
if doc, err = c.loader.ApplyOverlay(&next, values); err != nil {
|
|
c.status.Store(&config.ReloadStatus{
|
|
LastError: err.Error(),
|
|
LastErrorAt: time.Now().UTC().Format(time.RFC3339),
|
|
})
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
c.apply(doc)
|
|
c.status.Store(&config.ReloadStatus{
|
|
LastReloadAt: time.Now().UTC().Format(time.RFC3339),
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// WithOverlay attaches the runtime config overlay source. Optional: a
|
|
// controller without it publishes the file config alone.
|
|
func (c *ReloadController) WithOverlay(fn func() (map[string]any, error)) *ReloadController {
|
|
c.overlayValues = fn
|
|
return c
|
|
}
|
|
|
|
// Status returns the most recent reload outcome.
|
|
func (c *ReloadController) Status() config.ReloadStatus {
|
|
if p := c.status.Load(); p != nil {
|
|
return *p
|
|
}
|
|
return config.ReloadStatus{}
|
|
}
|
|
|
|
// reloadLoop waits for a reload signal on ch and re-loads the config
|
|
// each time, until ctx is cancelled. A failed reload is logged and the
|
|
// running config is left untouched (see ReloadController.Reload).
|
|
func reloadLoop(ctx context.Context, ch <-chan os.Signal, c *ReloadController, log *slog.Logger) {
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return
|
|
case <-ch:
|
|
if err := c.Reload(); err != nil {
|
|
log.Error("config reload failed; keeping running config", "err", err)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// reloadableFields are the Config fields the daemon re-reads per cycle
|
|
// or per dispatched task, so a reload takes effect without a restart.
|
|
// This is deliberately an allowlist: a field added to Config later
|
|
// defaults to requires-restart, which is the safe direction and forces
|
|
// whoever adds it to decide deliberately.
|
|
var reloadableFields = map[string]bool{
|
|
// Tickers reset on change (daemon.go Run / runIdentities).
|
|
"PollInterval": true,
|
|
// Poll loops re-read per cycle (daemon.go poll / pollForIdentity).
|
|
"Repos": true,
|
|
// Per-task snapshot: ForTask carries these into every dispatched
|
|
// TaskContext (config.go ForTask), built fresh from d.Cfg.Get() at
|
|
// dispatch. Pinned by TestReloadableFieldsCoverForTaskSnapshot so a
|
|
// field added to ForTask cannot silently drift out of this list.
|
|
"DiffCapLines": true,
|
|
"Budgets": true,
|
|
"Dispatch": true,
|
|
"Models": true,
|
|
"Notify": true,
|
|
// Per-poll reads off the live config in pollIssuesWithConfig:
|
|
// cfg.Label feeds IssuesWithLabel (daemon.go:359,390), cfg.BotUser
|
|
// feeds AssignedIssues (:368,381). Not carried by ForTask, so
|
|
// pinned by hand in TestReloadableFieldsCoverForTaskSnapshot.
|
|
"Label": true,
|
|
"BotUser": true,
|
|
}
|
|
|
|
// reloadableSubFields are sub-fields of structs that are otherwise
|
|
// startup-built. VolumeTTL and MaxConcurrency are re-read per cycle
|
|
// (cleanupExpiredStorage, drainSQLite/drainNATS); everything else in
|
|
// Containers (Image, Enabled, MaxUptime, PullPolicy, Network) is frozen
|
|
// in the startup-built container pool. Forge.Host is carried into
|
|
// TaskContext by ForTask (display/link building only); the forge client
|
|
// itself is startup-built, so Type/Token/TokenEnv stay requires-restart.
|
|
var reloadableSubFields = map[string]map[string]bool{
|
|
"Containers": {"VolumeTTL": true, "MaxConcurrency": true},
|
|
"Forge": {"Host": true},
|
|
}
|
|
|
|
// changedNonReloadableFields compares old and new and returns the
|
|
// dotted names of top-level Config fields whose values differ and are
|
|
// not re-read per cycle. The reload log warns about these so the
|
|
// operator knows their edit did not take effect until a restart --
|
|
// the alternative is an operator who believes a reloaded poll interval
|
|
// changed the forge token, and burns an hour discovering it did not.
|
|
func changedNonReloadableFields(old, next config.Config) []string {
|
|
var changed []string
|
|
ov, nv := reflect.ValueOf(old), reflect.ValueOf(next)
|
|
ot := reflect.TypeFor[config.Config]()
|
|
for i := 0; i < ot.NumField(); i++ {
|
|
f := ot.Field(i)
|
|
name := f.Name
|
|
if reloadableFields[name] {
|
|
continue
|
|
}
|
|
if subs, ok := reloadableSubFields[name]; ok && f.Type.Kind() == reflect.Struct {
|
|
for j := 0; j < f.Type.NumField(); j++ {
|
|
sub := f.Type.Field(j).Name
|
|
if subs[sub] {
|
|
continue
|
|
}
|
|
if !reflect.DeepEqual(ov.Field(i).Field(j).Interface(), nv.Field(i).Field(j).Interface()) {
|
|
changed = append(changed, name+"."+sub)
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
if !reflect.DeepEqual(ov.Field(i).Interface(), nv.Field(i).Interface()) {
|
|
changed = append(changed, name)
|
|
}
|
|
}
|
|
return changed
|
|
}
|