refactor(config): delete the runtime config overlay
The control plane replaces this, rather than extending it (docs/prds/runtime-control-plane.md, "First implementation"). Until now both paths existed: overlay kept dashboard edits in its own SQLite file beside archie.db and layered them over the file config at boot and on every reload, which is the duplicate configuration authority the PRD set out to remove. Gone with it: the --no-config-overlay flag and ARCHIE_SKIP_CONFIG_OVERLAY recovery hatch, which existed because a bad overlay row could brick the boot; ReloadStatus.OverlayUnavailable; and the reload controller's overlay source. The -config-overlay file overlay is a different thing and stays. The dashboard's three write seams went too. UpdateConfig, ResetConfig and UpdateRepoField were assigned only in internal/webui's own tests, so in production they answered 503 and ConfigView.Editable was permanently false (archie-core-j28m). Removing Editable would otherwise have flipped the SPA's controls on, since configEditable read a missing field as true, so the config pages become what they already were in practice: a read of what each process is running with. Configuration is written through the control plane. apply.go survives this deliberately; archie-core-xrux carries the question of whether its map merge is still guarding the file overlay path.
This commit is contained in:
1 parent
d8dd9ac6d6
commit
2311529def
35 files changed
+58
-1519
No files matched your search
@@ -1,18 +0,0 @@
|
||||
package configuration
|
||||
|
||||
import "os"
|
||||
|
||||
// envSkipOverlay names the environment variable that disables runtime
|
||||
// config overlays. It is the recovery hatch for an overlay write that
|
||||
// leaves the daemon unable to boot: set ARCHIE_SKIP_CONFIG_OVERLAY=1
|
||||
// and the daemon boots from file config alone. The same switch exists
|
||||
// as the --no-config-overlay flag on archied.
|
||||
const envSkipOverlay = "ARCHIE_SKIP_CONFIG_OVERLAY"
|
||||
|
||||
// SkipOverlay reports whether runtime config overlays are disabled via
|
||||
// ARCHIE_SKIP_CONFIG_OVERLAY=1. The overlay source (a later step of the
|
||||
// config-editing work) consults this before loading; cmd/archied
|
||||
// OR-s its --no-config-overlay flag with this at startup.
|
||||
func SkipOverlay() bool {
|
||||
return os.Getenv(envSkipOverlay) == "1"
|
||||
}
|
||||
@@ -1,17 +0,0 @@
|
||||
package configuration
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSkipOverlayEnvSet(t *testing.T) {
|
||||
t.Setenv(envSkipOverlay, "1")
|
||||
if !SkipOverlay() {
|
||||
t.Fatal("SkipOverlay() = false with env set to 1")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSkipOverlayEnvUnset(t *testing.T) {
|
||||
t.Setenv(envSkipOverlay, "")
|
||||
if SkipOverlay() {
|
||||
t.Fatal("SkipOverlay() = true with env empty")
|
||||
}
|
||||
}
|
||||
@@ -1,197 +0,0 @@
|
||||
// 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"
|
||||
|
||||
"github.com/samcharles93/archie-core/internal/infrastructure/configuration"
|
||||
)
|
||||
|
||||
// DeniedKeys moved to internal/infrastructure/configuration ( DeniedKeys):
|
||||
// webui references the constant and must not link this package's SQLite
|
||||
// store (archie-core-8cda.5.6).
|
||||
var DeniedKeys = configuration.DeniedKeys
|
||||
|
||||
// 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
|
||||
}
|
||||
|
||||
// Keys lists the dotted keys the overlay sets, in sorted order. Snapshot
|
||||
// nests them into a config-shaped map for decoding, which loses the paths --
|
||||
// "budgets.max_steps" becomes out["budgets"]["max_steps"] and ranging over
|
||||
// that map yields "budgets". The dashboard marks overridden rows and offers a
|
||||
// per-row reset by dotted key, so it needs the keys as stored.
|
||||
func (s *Store) Keys(ctx context.Context) ([]string, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `SELECT key FROM config_overlay ORDER BY key`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer func() { _ = rows.Close() }()
|
||||
|
||||
var keys []string
|
||||
for rows.Next() {
|
||||
var key string
|
||||
if err := rows.Scan(&key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
keys = append(keys, key)
|
||||
}
|
||||
return keys, rows.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
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
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.image", `"archie-agent:test"`},
|
||||
{"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)
|
||||
}
|
||||
|
||||
budgets, ok := snap["budgets"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("budgets = %#v, want map", snap["budgets"])
|
||||
}
|
||||
if got := budgets["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)
|
||||
}
|
||||
containers, ok := snap["containers"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("containers = %#v, want map", snap["containers"])
|
||||
}
|
||||
if got := containers["image"]; got != "archie-agent:test" {
|
||||
t.Errorf("containers.image = %#v, want archie-agent:test", got)
|
||||
}
|
||||
web, ok := snap["web"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("web = %#v, want map", snap["web"])
|
||||
}
|
||||
if got := web["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)
|
||||
}
|
||||
a, ok := root["a"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("root[a] = %#v, want map", root["a"])
|
||||
}
|
||||
ab, ok := a["b"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("root[a][b] = %#v, want map", a["b"])
|
||||
}
|
||||
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()
|
||||
}
|
||||
|
||||
// TestKeysReportsDottedPaths: Snapshot nests keys for decoding, so it cannot
|
||||
// answer "which keys are overridden" -- ranging over its result yields the
|
||||
// top-level section ("budgets"), not the stored key ("budgets.max_steps").
|
||||
// The dashboard marks rows and resets them by stored key, so Keys reports
|
||||
// those.
|
||||
func TestKeysReportsDottedPaths(t *testing.T) {
|
||||
store, ctx := openTest(t)
|
||||
for key, value := range map[string]string{
|
||||
"budgets.max_steps": "40",
|
||||
"budgets.wall_clock": `"30m"`,
|
||||
"label": `"archie"`,
|
||||
} {
|
||||
if err := store.Set(ctx, key, value, "test"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
keys, err := store.Keys(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := []string{"budgets.max_steps", "budgets.wall_clock", "label"}
|
||||
if len(keys) != len(want) {
|
||||
t.Fatalf("Keys = %v, want %v", keys, want)
|
||||
}
|
||||
for i, key := range want {
|
||||
if keys[i] != key {
|
||||
t.Errorf("Keys[%d] = %q, want %q (sorted, dotted)", i, keys[i], key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestKeysOnAnEmptyOverlay: nothing overridden is an empty list, not an error.
|
||||
func TestKeysOnAnEmptyOverlay(t *testing.T) {
|
||||
store, ctx := openTest(t)
|
||||
keys, err := store.Keys(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(keys) != 0 {
|
||||
t.Fatalf("Keys = %v, want none", keys)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user