Files
archie/internal/infrastructure/configuration/apply.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

34 lines
1.1 KiB
Go

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
}