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.
This commit is contained in:
sam committed 2026-08-10 00:54:23 +10:00
1 parent 31fdbddd61
commit da2d93a830
11 files changed
+796 -14

No files matched your search

+102 -14
View File
@@ -43,6 +43,7 @@ import (
"github.com/samcharles93/archie-core/internal/forgerpc"
"github.com/samcharles93/archie-core/internal/gateway"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/overlay"
"github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats"
"github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog"
"github.com/samcharles93/archie-core/internal/logging"
@@ -293,6 +294,15 @@ func taskDBPath(configuredPath string) string {
return configuredPath + "-tasks.sqlite"
}
// configDBPath resolves the runtime config overlay file. It is a sibling
// of the task and conversation stores (same configured path, own suffix)
// so each file owns its user_version and its migrator with no
// contention; recovery from a broken overlay is rm this file plus the
// --no-config-overlay boot flag.
func configDBPath(configuredPath string) string {
return configuredPath + "-config.sqlite"
}
func manualRequeueTask(ctx context.Context, st store.TaskStore, taskID int64) error {
task, err := st.TaskByID(ctx, taskID)
if err != nil {
@@ -323,7 +333,8 @@ func run() int {
log := slog.New(slog.NewJSONHandler(os.Stderr, nil))
loader := configuration.New(log)
if *noConfigOverlay || configuration.SkipOverlay() {
skipOverlay := *noConfigOverlay || configuration.SkipOverlay()
if skipOverlay {
log.Info("runtime config overlay disabled (recovery hatch); booting on file config alone")
}
doc, err := loader.Resolve(*cfgPath, *overlayPath)
@@ -331,6 +342,32 @@ func run() int {
fmt.Fprintln(os.Stderr, err)
return 1
}
// Runtime config overlay: dashboard-edited overrides layered over the
// file config from their own SQLite file (own user_version and
// migrator, no contention with the task store). Skipped under
// --no-config-overlay / ARCHIE_SKIP_CONFIG_OVERLAY=1, or when the
// store cannot be opened or its values fail validation -- a broken
// overlay must not brick the daemon.
var overlayStore *overlay.Store
if !skipOverlay {
overlayStore, err = overlay.Open(ctx, configDBPath(doc.Config.DBPath))
if err != nil {
log.Error("config overlay unavailable; booting on file config alone", "path", configDBPath(doc.Config.DBPath), "err", err)
} else {
defer overlayStore.Close()
if overrides, err := overlayStore.Snapshot(ctx); err != nil {
log.Error("config overlay unreadable; booting on file config alone", "err", err)
} else if len(overrides) > 0 {
next := *doc
applied, err := loader.ApplyOverlay(&next, overrides)
if err != nil {
log.Error("config overlay rejected by validation; booting on file config alone", "err", err)
} else {
doc = applied
}
}
}
}
cfg := doc.Config
// Re-create the logger now the config is known. Everything above this
@@ -986,11 +1023,28 @@ func run() int {
// published config.
web.Cfg = d.Cfg
// SIGHUP re-loads the config from disk and republishes it. The apply
// closure swaps the daemon's Holder and the dashboard's provenance;
// the reload log warns about changed fields that require a restart, so
// an operator never has to read the source to find out whether their
// edit took effect. web.LastReload exposes the outcome to /api/config.
// publishConfig publishes a config snapshot and its provenance to
// both the daemon and the dashboard (they share one Holder). Reload
// and the PATCH path both go through it, so the two can never
// diverge. currentProvenance is the last published provenance chain;
// the PATCH path appends the runtime-overlay origin to it.
currentProvenance := doc.Provenance
publishConfig := func(cfg config.Config, provenance configuration.Provenance) {
d.Cfg.Set(cfg)
origins := make([]webui.ConfigOrigin, 0, len(provenance.Origins))
for _, origin := range provenance.Origins {
origins = append(origins, webui.ConfigOrigin{
Path: origin.Path, Role: string(origin.Role), Layer: string(origin.Layer), Feature: string(origin.Feature),
})
}
web.SetProvenance(origins)
}
// SIGHUP re-loads the config from disk, layers the runtime overlay,
// and republishes it. The reload log warns about changed fields that
// require a restart, so an operator never has to read the source to
// find out whether their edit took effect. web.LastReload exposes
// the outcome to /api/config.
reloadController := newReloadController(loader, *cfgPath, *overlayPath, func(doc *configuration.Document) {
// Boot merges catalog-discovered providers into cfg before the
// Holders are seeded; publishing the raw reloaded document would
@@ -999,14 +1053,8 @@ func run() int {
// failed to load merges to identity).
applyModelCatalog(&doc.Config, catalog)
old := d.Cfg.Get()
d.Cfg.Set(doc.Config)
provenance := make([]webui.ConfigOrigin, 0, len(doc.Provenance.Origins))
for _, origin := range doc.Provenance.Origins {
provenance = append(provenance, webui.ConfigOrigin{
Path: origin.Path, Role: string(origin.Role), Layer: string(origin.Layer), Feature: string(origin.Feature),
})
}
web.SetProvenance(provenance)
currentProvenance = doc.Provenance
publishConfig(doc.Config, doc.Provenance)
if fields := changedNonReloadableFields(old, doc.Config); len(fields) > 0 {
log.Warn("config reloaded; some changes require a restart",
"fields", fields, "paths", doc.Provenance.Paths())
@@ -1014,8 +1062,48 @@ func run() int {
log.Info("config reloaded", "paths", doc.Provenance.Paths())
}
})
if overlayStore != nil {
reloadController.WithOverlay(func() (map[string]any, error) { return overlayStore.Snapshot(ctx) })
}
web.LastReload = reloadController.Status
// PATCH /api/config goes through the same publish path as reload:
// apply to a copy of the published config, validate the materialised
// result, persist to the overlay, then publish. It must never call
// Set directly from the handler -- web.Cfg is the daemon's own Holder.
web.UpdateConfig = func(ctx context.Context, updates map[string]any) error {
if overlayStore == nil {
return fmt.Errorf("%w: config overlay is disabled (--no-config-overlay or ARCHIE_SKIP_CONFIG_OVERLAY=1)", webui.ErrConfigUpdateUnavailable)
}
for key := range updates {
if reason, denied := overlay.DeniedKeys[key]; denied {
return fmt.Errorf("%w: config key %s is not runtime-tunable: %s", webui.ErrConfigUpdateInvalid, key, reason)
}
}
next := d.Cfg.Get()
if err := configuration.ApplyOverlayValues(&next, updates); err != nil {
return fmt.Errorf("%w: %v", webui.ErrConfigUpdateInvalid, err)
}
if err := configuration.Validate(&next); err != nil {
return fmt.Errorf("%w: %v", webui.ErrConfigUpdateInvalid, err)
}
for key, value := range updates {
data, err := json.Marshal(value)
if err != nil {
return fmt.Errorf("%w: %v", webui.ErrConfigUpdateInvalid, err)
}
if err := overlayStore.Set(ctx, key, string(data), "dashboard"); err != nil {
return err
}
}
prov := configuration.Provenance{Origins: append(append([]configuration.Origin{}, currentProvenance.Origins...),
configuration.Origin{Path: "config_overlay (runtime)", Role: configuration.RoleMain, Layer: configuration.LayerOverlay})}
currentProvenance = prov
publishConfig(next, prov)
log.Info("config updated from dashboard", "keys", len(updates))
return nil
}
reloadCh := make(chan os.Signal, 1)
signal.Notify(reloadCh, syscall.SIGHUP)
defer signal.Stop(reloadCh)
+33
View File
@@ -29,6 +29,12 @@ type ReloadController struct {
// 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]
}
@@ -51,6 +57,26 @@ func (c *ReloadController) Reload() error {
})
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),
@@ -58,6 +84,13 @@ func (c *ReloadController) Reload() error {
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 {
+48
View File
@@ -2,6 +2,7 @@ package main
import (
"context"
"errors"
"log/slog"
"os"
"path/filepath"
@@ -105,6 +106,53 @@ func TestReloadValidationFailureDoesNotApply(t *testing.T) {
}
}
func TestReloadAppliesOverlayBeforePublish(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
writeConfig(t, path, minimalConfigTOML("widget"))
loader := configuration.New(slog.New(slog.DiscardHandler))
var published config.Config
controller := newReloadController(loader, path, "", func(doc *configuration.Document) {
published = doc.Config
}).WithOverlay(func() (map[string]any, error) {
return map[string]any{"poll_interval": "90s"}, nil
})
if err := controller.Reload(); err != nil {
t.Fatal(err)
}
if published.PollInterval != config.Duration(90*time.Second) {
t.Errorf("PollInterval = %v, want 90s (overlay applied before publish)", published.PollInterval)
}
}
// TestReloadOverlayFailureAborts pins that a failed overlay read or
// validation aborts the reload exactly like a bad file: apply is not
// called and the error is recorded.
func TestReloadOverlayFailureAborts(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
writeConfig(t, path, minimalConfigTOML("widget"))
loader := configuration.New(slog.New(slog.DiscardHandler))
applied := 0
controller := newReloadController(loader, path, "", func(*configuration.Document) { applied++ }).
WithOverlay(func() (map[string]any, error) {
return nil, errors.New("overlay unreadable")
})
if err := controller.Reload(); err == nil {
t.Fatal("expected an error from the failed overlay")
}
if applied != 0 {
t.Fatalf("apply called %d times after overlay failure, want 0", applied)
}
if st := controller.Status(); st.LastError == "" {
t.Fatal("expected LastError recorded")
}
}
func TestReloadLoopAppliesOnSignalAndStopsOnCancel(t *testing.T) {
dir := t.TempDir()
path := filepath.Join(dir, "config.toml")
@@ -0,0 +1,33 @@
package configuration
import (
"fmt"
"gopkg.in/yaml.v3"
"github.com/samcharles93/archie-core/internal/config"
)
// ApplyOverlayValues decodes overrides (a nested map of dotted-path
// values, as produced by the overlay store) into cfg using the same
// field-level precedence as a file overlay: only the keys present are
// replaced, every field the overlay omits keeps its existing value.
//
// This is the decode half of the runtime config overlay. The loader's
// ApplyOverlay wraps it with defaulting, validation and provenance; the
// dashboard PATCH path calls it on a copy of the published config and
// validates the materialised result before persisting. Either way the
// caller owns the cfg value -- pass a copy when the base must survive.
func ApplyOverlayValues(cfg *config.Config, overrides map[string]any) error {
if len(overrides) == 0 {
return nil
}
data, err := yaml.Marshal(overrides)
if err != nil {
return fmt.Errorf("%w: encoding config overlay: %v", ErrUnreadable, err)
}
if err := yaml.Unmarshal(data, cfg); err != nil {
return fmt.Errorf("%w: parsing config overlay: %v", ErrUnreadable, err)
}
return nil
}
@@ -0,0 +1,88 @@
package configuration
import (
"log/slog"
"os"
"path/filepath"
"testing"
"time"
"github.com/samcharles93/archie-core/internal/config"
)
func TestApplyOverlayValuesFieldLevelMerge(t *testing.T) {
cfg := config.Config{
BotUser: "a",
Budgets: config.Budgets{MaxSteps: 10},
}
overrides := map[string]any{
"budgets": map[string]any{"max_steps": float64(20)},
}
if err := ApplyOverlayValues(&cfg, overrides); err != nil {
t.Fatal(err)
}
if cfg.Budgets.MaxSteps != 20 {
t.Errorf("MaxSteps = %d, want 20", cfg.Budgets.MaxSteps)
}
// Fields the overlay omits keep their value.
if cfg.BotUser != "a" {
t.Errorf("BotUser = %q, want a (unchanged)", cfg.BotUser)
}
// Unrelated nested fields keep their value too.
if cfg.Budgets.MaxTokens != 0 {
t.Errorf("MaxTokens = %d, want 0 (untouched)", cfg.Budgets.MaxTokens)
}
}
func TestLoaderApplyOverlayLayersDefaultsValidatesAndRecordsProvenance(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte("bot_user = \"widget\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"), 0o600); err != nil {
t.Fatal(err)
}
l := New(slog.New(slog.DiscardHandler))
doc, err := l.Resolve(path, "")
if err != nil {
t.Fatal(err)
}
applied, err := l.ApplyOverlay(doc, map[string]any{"poll_interval": "90s"})
if err != nil {
t.Fatal(err)
}
if applied.Config.PollInterval != config.Duration(90*time.Second) {
t.Errorf("PollInterval = %v, want 90s", applied.Config.PollInterval)
}
if len(applied.Provenance.Origins) != 2 {
t.Errorf("Origins = %+v, want 2 (file + runtime overlay)", applied.Provenance.Origins)
}
// An overlay that fails validation aborts; the base document is
// untouched when a copy is passed.
next := *doc
if _, err := l.ApplyOverlay(&next, map[string]any{"agent": map[string]any{"mode": "bogus"}}); err == nil {
t.Fatal("invalid overlay accepted")
}
if doc.Config.Agent.Mode != "inprocess" {
t.Errorf("base Agent.Mode = %q, want inprocess (defaulted, untouched)", doc.Config.Agent.Mode)
}
}
func TestLoaderApplyOverlayEmptyIsNoOp(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.toml")
if err := os.WriteFile(path, []byte("bot_user = \"widget\"\n[[repos]]\nowner = \"acme\"\nname = \"app\"\n"), 0o600); err != nil {
t.Fatal(err)
}
l := New(slog.New(slog.DiscardHandler))
doc, err := l.Resolve(path, "")
if err != nil {
t.Fatal(err)
}
applied, err := l.ApplyOverlay(doc, nil)
if err != nil {
t.Fatal(err)
}
if len(applied.Provenance.Origins) != 1 {
t.Errorf("Origins = %+v, want 1 (no overlay origin for an empty overlay)", applied.Provenance.Origins)
}
}
@@ -113,6 +113,25 @@ 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{}
@@ -0,0 +1,178 @@
// Package overlay persists runtime-tunable configuration overrides in
// their own SQLite file (cfg.DBPath + "-config.sqlite"), separate from
// the task, conversation and search stores so each file owns its
// user_version and its migrator with no contention.
//
// It backs the dashboard's config editing: a PATCH writes here, the
// daemon layers these values over the file config at boot and on every
// reload, and removing the file (or passing --no-config-overlay /
// ARCHIE_SKIP_CONFIG_OVERLAY=1) is the documented recovery from a write
// that would otherwise break boot.
package overlay
import (
"context"
"database/sql"
"encoding/json"
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"time"
_ "modernc.org/sqlite"
)
// DeniedKeys are the config keys the overlay may never set, mapped to
// the reason shown to the operator. db_path is the daemon's own
// bootstrap path -- the daemon must read it before it can open this
// store -- and work_dir pins the whole working layout. Enforced at
// write time (the API returns 4xx) and again in Set, not silently
// dropped at read time.
var DeniedKeys = map[string]string{
"db_path": "required for bootstrap; cannot be changed at runtime",
"work_dir": "pins the daemon's working layout; cannot be changed at runtime",
}
// Store is the config overlay database.
type Store struct{ db *sql.DB }
const schemaVersion = 1
const schema = `
CREATE TABLE IF NOT EXISTS config_overlay (
key TEXT PRIMARY KEY,
value TEXT NOT NULL,
updated_at TEXT NOT NULL,
updated_by TEXT NOT NULL
);
`
// Open opens (creating if needed) the overlay database and migrates it.
func Open(ctx context.Context, path string) (*Store, error) {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return nil, err
}
db, err := sql.Open("sqlite", sqliteDSN(path))
if err != nil {
return nil, err
}
db.SetMaxOpenConns(1)
if _, err := db.ExecContext(ctx, schema); err != nil {
return nil, errors.Join(fmt.Errorf("overlay: init schema: %w", err), db.Close())
}
if err := migrate(ctx, db); err != nil {
return nil, errors.Join(fmt.Errorf("overlay: migrate: %w", err), db.Close())
}
return &Store{db: db}, nil
}
// Close closes the database.
func (s *Store) Close() error { return s.db.Close() }
// sqliteDSN enables WAL and a busy timeout. Kept local to match the
// task and gateway stores; deduplicating the three copies is a
// follow-up, not part of this change.
func sqliteDSN(path string) string {
separator := "?"
if strings.HasSuffix(path, "?") || strings.HasSuffix(path, "&") {
separator = ""
} else if strings.Contains(path, "?") {
separator = "&"
}
return path + separator + "_pragma=journal_mode(WAL)&_pragma=busy_timeout(5000)"
}
func migrate(ctx context.Context, db *sql.DB) error {
tx, err := db.BeginTx(ctx, nil)
if err != nil {
return err
}
defer func() { _ = tx.Rollback() }()
var version int
if err := tx.QueryRowContext(ctx, `PRAGMA user_version`).Scan(&version); err != nil {
return err
}
if version > schemaVersion {
return fmt.Errorf("config overlay schema version %d is newer than supported version %d", version, schemaVersion)
}
if version == schemaVersion {
return tx.Commit()
}
if _, err := tx.ExecContext(ctx, `PRAGMA user_version = 1`); err != nil {
return err
}
return tx.Commit()
}
// Set persists one override. key is a dotted config path
// ("budgets.max_steps"); value is the JSON encoding of the field value
// ("12", "\"60s\"", "true"). updatedBy names the writer for audit.
// Keys in DeniedKeys are refused here as well as at the API layer.
func (s *Store) Set(ctx context.Context, key, value, updatedBy string) error {
if reason, denied := DeniedKeys[key]; denied {
return fmt.Errorf("config key %s is not runtime-tunable: %s", key, reason)
}
_, err := s.db.ExecContext(ctx,
`INSERT INTO config_overlay (key, value, updated_at, updated_by) VALUES (?, ?, ?, ?)
ON CONFLICT(key) DO UPDATE SET
value = excluded.value, updated_at = excluded.updated_at, updated_by = excluded.updated_by`,
key, value, time.Now().UTC().Format(time.RFC3339), updatedBy)
return err
}
// Delete removes one override, restoring the file's value for that key.
func (s *Store) Delete(ctx context.Context, key string) error {
_, err := s.db.ExecContext(ctx, `DELETE FROM config_overlay WHERE key = ?`, key)
return err
}
// Snapshot returns every stored override as a nested map of typed
// values ready to decode into config.Config. Dotted keys are nested:
// "budgets.max_steps" becomes map["budgets"]["max_steps"]. Values are
// JSON-decoded so the caller never has to know the field's type.
func (s *Store) Snapshot(ctx context.Context) (map[string]any, error) {
rows, err := s.db.QueryContext(ctx, `SELECT key, value FROM config_overlay`)
if err != nil {
return nil, err
}
defer func() { _ = rows.Close() }()
out := make(map[string]any)
for rows.Next() {
var key, raw string
if err := rows.Scan(&key, &raw); err != nil {
return nil, err
}
var value any
if err := json.Unmarshal([]byte(raw), &value); err != nil {
return nil, fmt.Errorf("overlay: key %s holds invalid JSON %q: %w", key, raw, err)
}
if err := Nest(out, key, value); err != nil {
return nil, err
}
}
return out, rows.Err()
}
// Nest inserts value into root at the dotted key path ("a.b.c" ->
// root["a"]["b"]["c"]), creating intermediate maps as needed.
func Nest(root map[string]any, key string, value any) error {
parts := strings.Split(key, ".")
if len(parts) == 0 || parts[0] == "" {
return fmt.Errorf("overlay: empty key %q", key)
}
cur := root
for _, part := range parts[:len(parts)-1] {
next, ok := cur[part].(map[string]any)
if !ok {
next = make(map[string]any)
cur[part] = next
}
cur = next
}
cur[parts[len(parts)-1]] = value
return nil
}
@@ -0,0 +1,122 @@
package overlay
import (
"context"
"path/filepath"
"reflect"
"testing"
)
func openTest(t *testing.T) (*Store, context.Context) {
t.Helper()
s, err := Open(context.Background(), filepath.Join(t.TempDir(), "config.sqlite"))
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { _ = s.Close() })
return s, context.Background()
}
func TestSetGetSnapshotRoundTrip(t *testing.T) {
s, ctx := openTest(t)
for _, tc := range []struct {
key, value string
}{
{"budgets.max_steps", "12"},
{"label", `"archie"`},
{"containers.enabled", "true"},
{"web.listen", `":8643"`},
} {
if err := s.Set(ctx, tc.key, tc.value, "test"); err != nil {
t.Fatalf("Set(%s): %v", tc.key, err)
}
}
snap, err := s.Snapshot(ctx)
if err != nil {
t.Fatal(err)
}
if got := snap["budgets"].(map[string]any)["max_steps"]; !reflect.DeepEqual(got, float64(12)) {
t.Errorf("budgets.max_steps = %#v, want float64(12)", got)
}
if got := snap["label"]; got != "archie" {
t.Errorf("label = %#v, want archie", got)
}
if got := snap["containers"].(map[string]any)["enabled"]; got != true {
t.Errorf("containers.enabled = %#v, want true", got)
}
if got := snap["web"].(map[string]any)["listen"]; got != ":8643" {
t.Errorf("web.listen = %#v, want :8643", got)
}
}
func TestSetUpsertsAndDeleteRestores(t *testing.T) {
s, ctx := openTest(t)
if err := s.Set(ctx, "label", `"first"`, "test"); err != nil {
t.Fatal(err)
}
if err := s.Set(ctx, "label", `"second"`, "test"); err != nil {
t.Fatal(err)
}
snap, err := s.Snapshot(ctx)
if err != nil {
t.Fatal(err)
}
if snap["label"] != "second" {
t.Fatalf("after upsert, label = %#v, want second", snap["label"])
}
if err := s.Delete(ctx, "label"); err != nil {
t.Fatal(err)
}
snap, err = s.Snapshot(ctx)
if err != nil {
t.Fatal(err)
}
if len(snap) != 0 {
t.Fatalf("after delete, snapshot = %#v, want empty", snap)
}
}
func TestSetRefusesDeniedKeys(t *testing.T) {
s, ctx := openTest(t)
for key := range DeniedKeys {
if err := s.Set(ctx, key, `"x"`, "test"); err == nil {
t.Errorf("Set(%s) succeeded, want denial", key)
}
}
}
func TestNestBuildsNestedMaps(t *testing.T) {
root := map[string]any{}
if err := Nest(root, "a.b.c", 1); err != nil {
t.Fatal(err)
}
if err := Nest(root, "a.b.d", "x"); err != nil {
t.Fatal(err)
}
ab := root["a"].(map[string]any)["b"].(map[string]any)
if ab["c"] != 1 || ab["d"] != "x" {
t.Fatalf("nested map = %#v", root)
}
if err := Nest(root, "", 1); err == nil {
t.Fatal("empty key accepted")
}
}
func TestOpenIdempotentAndVersioned(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.sqlite")
s1, err := Open(context.Background(), path)
if err != nil {
t.Fatal(err)
}
_ = s1.Close()
s2, err := Open(context.Background(), path)
if err != nil {
t.Fatalf("reopen: %v", err)
}
_ = s2.Close()
}
+68
View File
@@ -1,12 +1,31 @@
package webui
import (
"encoding/json"
"errors"
"maps"
"net/http"
"strconv"
"strings"
"github.com/samcharles93/archie-core/internal/channels"
"github.com/samcharles93/archie-core/internal/config"
"github.com/samcharles93/archie-core/internal/infrastructure/configuration/overlay"
)
// Sentinel errors for handleConfigUpdate status classification. The
// composition root wraps its UpdateConfig errors with these so the
// handler can answer 400 (invalid input) or 503 (unavailable) without
// reaching into infrastructure internals.
var (
// ErrConfigUpdateUnavailable reports that config editing is not
// wired (no UpdateConfig seam, or the overlay is skipped by the
// recovery flag).
ErrConfigUpdateUnavailable = errors.New("config editing unavailable")
// ErrConfigUpdateInvalid reports a rejected update: a denylisted
// key, a failed validation of the materialised config, or an
// unparseable value.
ErrConfigUpdateInvalid = errors.New("config update invalid")
)
// ChannelView is one conversational front-end as shown on the dashboard's
@@ -198,6 +217,10 @@ type ConfigView struct {
// Reload reports the most recent config reload outcome. Omitted when
// the reload status is unavailable.
Reload *config.ReloadStatus `json:"reload,omitempty"`
// Locked maps dotted config keys that cannot be changed from the
// dashboard to the reason. The UI renders these rows disabled rather
// than silently omitting the edit affordance.
Locked map[string]string `json:"locked,omitempty"`
}
// IdentityView is who Archie is on the forge -- never the token that
@@ -347,6 +370,7 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
},
Web: WebView{Listen: cfg.Web.Listen},
Provenance: provenance,
Locked: lockedConfigKeys(),
}
if s.LastReload != nil {
rs := s.LastReload()
@@ -356,6 +380,50 @@ func (s *Server) handleConfig(w http.ResponseWriter, r *http.Request) {
writeJSON(w, view)
}
// lockedConfigKeys returns the dotted config keys the overlay refuses
// to set, with the reason shown in the UI. These are the daemon's own
// bootstrap inputs; changing them from the dashboard could break the
// next boot.
func lockedConfigKeys() map[string]string {
out := make(map[string]string, len(overlay.DeniedKeys))
maps.Copy(out, overlay.DeniedKeys)
return out
}
// handleConfigUpdate applies a set of dotted-path config updates via
// UpdateConfig, which the composition root wires to the same
// validate-persist-publish path as reload. The dashboard can then
// change runtime-tunable settings without hand-editing TOML.
func (s *Server) handleConfigUpdate(w http.ResponseWriter, r *http.Request) {
if s.UpdateConfig == nil {
http.Error(w, ErrConfigUpdateUnavailable.Error(), http.StatusServiceUnavailable)
return
}
var body struct {
Updates map[string]any `json:"updates"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, "invalid request body: "+err.Error(), http.StatusBadRequest)
return
}
if len(body.Updates) == 0 {
http.Error(w, "no updates provided", http.StatusBadRequest)
return
}
if err := s.UpdateConfig(r.Context(), body.Updates); err != nil {
switch {
case errors.Is(err, ErrConfigUpdateUnavailable):
http.Error(w, err.Error(), http.StatusServiceUnavailable)
case errors.Is(err, ErrConfigUpdateInvalid):
http.Error(w, err.Error(), http.StatusBadRequest)
default:
http.Error(w, "config update failed: "+err.Error(), http.StatusInternalServerError)
}
return
}
writeJSON(w, map[string]any{"ok": true})
}
func reposView(repos []config.Repo) []RepoView {
out := make([]RepoView, 0, len(repos))
for _, r := range repos {
+97
View File
@@ -1,7 +1,9 @@
package webui
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
@@ -223,6 +225,101 @@ func TestSetProvenancePublishesForConfigView(t *testing.T) {
}
}
// TestHandleConfigReportsLockedKeys proves the dashboard is told which
// config keys it cannot edit and why, so it can disable those rows
// instead of silently omitting the edit affordance.
func TestHandleConfigReportsLockedKeys(t *testing.T) {
srv := newTestServer(t)
srv.Cfg = configWithFakeSecrets()
req := httptest.NewRequestWithContext(t.Context(), http.MethodGet, "/api/config", nil)
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
var got ConfigView
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
t.Fatalf("decode: %v", err)
}
for _, key := range []string{"db_path", "work_dir"} {
if got.Locked[key] == "" {
t.Errorf("Locked[%q] is empty, want a reason", key)
}
}
}
// TestHandleConfigUpdateAppliesViaSeam proves the PATCH handler passes
// the decoded updates to the wired UpdateConfig seam and answers ok.
func TestHandleConfigUpdateAppliesViaSeam(t *testing.T) {
srv := newTestServer(t)
var got map[string]any
srv.UpdateConfig = func(_ context.Context, updates map[string]any) error {
got = updates
return nil
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config",
strings.NewReader(`{"updates": {"budgets.max_steps": 12}}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusOK {
t.Fatalf("status = %d, body = %s", w.Code, w.Body)
}
if got["budgets.max_steps"] != float64(12) {
t.Errorf("updates = %#v, want budgets.max_steps=12", got)
}
}
// TestHandleConfigUpdateInvalidMapsTo400 proves a rejected update (the
// seam wraps its error with ErrConfigUpdateInvalid) answers 400.
func TestHandleConfigUpdateInvalidMapsTo400(t *testing.T) {
srv := newTestServer(t)
srv.UpdateConfig = func(context.Context, map[string]any) error {
return fmt.Errorf("%w: label is not runtime-tunable", ErrConfigUpdateInvalid)
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config",
strings.NewReader(`{"updates": {"label": "x"}}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400, body = %s", w.Code, w.Body)
}
}
// TestHandleConfigUpdateUnavailableMapsTo503 proves a disabled overlay
// (ErrConfigUpdateUnavailable) answers 503, as does a server with no
// UpdateConfig seam at all.
func TestHandleConfigUpdateUnavailableMapsTo503(t *testing.T) {
srv := newTestServer(t)
srv.UpdateConfig = func(context.Context, map[string]any) error {
return fmt.Errorf("%w: overlay disabled by recovery flag", ErrConfigUpdateUnavailable)
}
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config",
strings.NewReader(`{"updates": {"label": "x"}}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want 503, body = %s", w.Code, w.Body)
}
}
func TestHandleConfigUpdateWithoutSeamMapsTo503(t *testing.T) {
srv := newTestServer(t)
req := httptest.NewRequestWithContext(t.Context(), http.MethodPatch, "/api/config",
strings.NewReader(`{"updates": {"label": "x"}}`))
w := httptest.NewRecorder()
srv.Handler().ServeHTTP(w, req)
if w.Code != http.StatusServiceUnavailable {
t.Fatalf("status = %d, want 503, body = %s", w.Code, w.Body)
}
}
func TestHandleChannelsReportsConfiguredState(t *testing.T) {
srv := newTestServer(t)
srv.Cfg = configWithFakeSecrets()
+8
View File
@@ -71,6 +71,13 @@ type Server struct {
// reload status.
LastReload func() config.ReloadStatus
// UpdateConfig applies a set of dotted-path config updates through
// the same validate-persist-publish path as reload (wired by the
// composition root). Optional: when nil, PATCH /api/config answers
// 503. The handler never touches the Cfg Holder directly -- see the
// Cfg field doc.
UpdateConfig func(context.Context, map[string]any) error
// Channels reports actual adapter lifecycle, independently of configuration
// presence. Nil preserves the configuration-only fallback for tests and
// minimal embedding.
@@ -169,6 +176,7 @@ func (s *Server) Handler() http.Handler {
mux.HandleFunc("GET /api/channels", s.handleChannels)
mux.HandleFunc("POST /api/channels/{id}/reload", s.handleChannelReload)
mux.HandleFunc("GET /api/config", s.handleConfig)
mux.HandleFunc("PATCH /api/config", s.handleConfigUpdate)
mux.HandleFunc("GET /api/logs", s.handleLogs)
mux.HandleFunc("GET /api/tasks/{id}/logs", s.handleTaskLogs)
mux.HandleFunc("GET /api/logs/stream", s.handleLogStream)