From 757b75de940ebd3d1a7d0ffc40cb926fb289fca5 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Thu, 24 Sep 2026 02:18:56 +1000 Subject: [PATCH] =?UTF-8?q?feat(postgres):=20wire=20the=20State=20Store=20?= =?UTF-8?q?to=20Postgres=20=E2=80=94=20DB=20URL,=20fail-closed=20pool,=20c?= =?UTF-8?q?omposition=20seam?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Wave 1 of the Postgres migration (archie-core-7r0x.9). This is the shared shape every port lane builds on; nothing here switches a live path to Postgres yet. - config: add `database_url` beside `db_path` (config.go), mark it bootstrap-only in denied_keys, expose it in the webui StorageView/config schema, and add it to config.example.toml plus all deployments/*.toml. - docker-compose: add a postgres:18 service; `task dev` (dev:postgres) brings it up and waits for it before the State Store boots. - archied: `openStateStore` now opens and migrates the process-scoped Postgres pool via `openStateStorePool`. The State Store fails closed (D1.2) when database_url is absent or invalid rather than fall back to SQLite; serving against Postgres later requires an import-completion record (D1.3), which is a later lane's gate, not this one. - composition seam: task-store construction (state_store_task.go) and EDA-store construction (state_store_eda.go) are extracted from openStateStore, with the pool open in state_store_pool.go. This is a behaviour-preserving refactor only — no store, interface, error, ordering or logging change — so L1 and L2 build against separate files instead of sharing state_store.go. Tests: fail-closed pool behaviour is pinned (absent/invalid URL); the State Store boot/restart tests now run against a throwaway Postgres 18 container (pgtest TestMain). Scoped test command and module-wide build pass; see lane report. --- Taskfile.yml | 22 ++++++++ config.example.toml | 6 ++ deployments/dev.toml | 6 ++ deployments/docker-nats-stack.toml | 4 ++ deployments/local-ollama-standalone.toml | 4 ++ deployments/multi-forge-github-gitea.toml | 4 ++ deployments/single-forge-github.toml | 4 ++ docker-compose.yml | 27 +++++++++ internal/app/archied/bootstrap.go | 8 +++ .../archied/control_plane_validation_test.go | 16 ++++-- internal/app/archied/pgtest_test.go | 14 +++++ internal/app/archied/state_store.go | 56 ++++--------------- internal/app/archied/state_store_eda.go | 33 +++++++++++ internal/app/archied/state_store_pool.go | 38 +++++++++++++ internal/app/archied/state_store_pool_test.go | 32 +++++++++++ .../archied/state_store_regression_test.go | 3 +- internal/app/archied/state_store_task.go | 39 +++++++++++++ internal/app/archied/state_store_test.go | 6 +- internal/config/config.go | 13 ++++- .../infrastructure/configuration/defaults.go | 5 ++ .../configuration/denied_keys.go | 5 +- internal/webui/api_config.go | 2 + internal/webui/api_config_test.go | 2 +- internal/webui/config_schema.go | 2 + internal/webui/config_schema_test.go | 5 +- 25 files changed, 294 insertions(+), 62 deletions(-) create mode 100644 internal/app/archied/pgtest_test.go create mode 100644 internal/app/archied/state_store_eda.go create mode 100644 internal/app/archied/state_store_pool.go create mode 100644 internal/app/archied/state_store_pool_test.go create mode 100644 internal/app/archied/state_store_task.go diff --git a/Taskfile.yml b/Taskfile.yml index 83afa17a2..6e42568f2 100644 --- a/Taskfile.yml +++ b/Taskfile.yml @@ -387,6 +387,25 @@ tasks: Ctrl-C stops all five. deps: [dev:daemon, dev:gateway, dev:state-store, dev:dashboard, dev:ui] + dev:postgres: + desc: "Start the dev PostgreSQL 18 service and wait until it accepts connections (prefer `task dev`)" + cmds: + - docker compose up -d postgres + # `up -d` returns before the container is ready; wait for its + # healthcheck so the State Store (which fails closed on a bad URL) does + # not race the boot. + - | + set -eu + for _ in $(seq 1 60); do + status=$(docker inspect --format '{{.State.Health.Status}}' "$(docker compose ps -q postgres)" 2>/dev/null || true) + if [ "$status" = "healthy" ]; then + exit 0 + fi + sleep 1 + done + echo "postgres did not become healthy within 60s" >&2 + exit 1 + dev:daemon: desc: archied only, live-reloading via air (prefer `task dev`) cmds: @@ -399,6 +418,9 @@ tasks: dev:state-store: desc: archie-state-store only, no live-reload (prefer `task dev`) + # The State Store fails closed without a PostgreSQL URL (Postgres-only + # epic), so bring the Compose service up and wait for it before booting. + deps: [dev:postgres] cmds: - go run ./cmd/archie-state-store -config-overlay deployments/dev.toml diff --git a/config.example.toml b/config.example.toml index f20a4fbfd..58af92add 100644 --- a/config.example.toml +++ b/config.example.toml @@ -15,6 +15,12 @@ # work_dir = "~/.local/share/archie/work" # default # db_path = "~/.local/share/archie/archie.db" # prefix for independent task and conversation SQLite files +# The standalone State Store's PostgreSQL connection. Required: the store +# fails closed rather than fall back to SQLite, so leave this unset only if +# you do not run archie-state-store. `docker compose up -d postgres` starts +# a matching local postgres:18 with this default URL. +# database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" + # skills_dir = "~/.local/share/archie/skills" # SKILL.md search path; defaults under work_dir when unset # plugin_dir = "~/.local/share/archie/plugins" # daemon plugin manifests; unset means no plugins are loaded # module_dir = "~/.local/share/archie/modules" # EDA playbook Module action kinds (log.go etc.); unset means no modules loaded diff --git a/deployments/dev.toml b/deployments/dev.toml index 058e11fbc..1ae68ae35 100644 --- a/deployments/dev.toml +++ b/deployments/dev.toml @@ -8,6 +8,12 @@ # [web], [services.*] and [capture] land in archie-ui; the daemon ignores # [web] entirely. +# `task dev` runs archie-state-store against this overlay, and the State Store +# fails closed without a PostgreSQL URL. This must reach the `postgres` +# service in docker-compose.yml, which `task dev` brings up first via +# dev:postgres -- keep the URL and that service in agreement. +database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" + [web] listen = "127.0.0.1:8484" diff --git a/deployments/docker-nats-stack.toml b/deployments/docker-nats-stack.toml index 4f286540c..fa22f1614 100644 --- a/deployments/docker-nats-stack.toml +++ b/deployments/docker-nats-stack.toml @@ -16,6 +16,10 @@ bot_user = "archie" diff_cap_lines = 0 work_dir = "~/.local/share/archie/work" db_path = "~/.local/share/archie/archie.db" +# The State Store's PostgreSQL connection (required; Postgres-only epic). +# `docker compose up -d postgres` provides this local default; point it at +# your own Postgres 18 server otherwise. +database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" [services.gateway] target = "127.0.0.1:8585" diff --git a/deployments/local-ollama-standalone.toml b/deployments/local-ollama-standalone.toml index 37d576ddc..3c202d039 100644 --- a/deployments/local-ollama-standalone.toml +++ b/deployments/local-ollama-standalone.toml @@ -7,6 +7,10 @@ work_dir = "~/.local/share/archie/work" db_path = "~/.local/share/archie/archie.db" +# The State Store's PostgreSQL connection (required; Postgres-only epic). +# `docker compose up -d postgres` provides this local default; point it at +# your own Postgres 18 server otherwise. +database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" [services.gateway] target = "127.0.0.1:8585" diff --git a/deployments/multi-forge-github-gitea.toml b/deployments/multi-forge-github-gitea.toml index 3872bca45..f5727333b 100644 --- a/deployments/multi-forge-github-gitea.toml +++ b/deployments/multi-forge-github-gitea.toml @@ -8,6 +8,10 @@ work_dir = "~/.local/share/archie/work" db_path = "~/.local/share/archie/archie.db" +# The State Store's PostgreSQL connection (required; Postgres-only epic). +# `docker compose up -d postgres` provides this local default; point it at +# your own Postgres 18 server otherwise. +database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" [services.gateway] target = "127.0.0.1:8585" diff --git a/deployments/single-forge-github.toml b/deployments/single-forge-github.toml index ce12a1ab9..88ea68fb5 100644 --- a/deployments/single-forge-github.toml +++ b/deployments/single-forge-github.toml @@ -7,6 +7,10 @@ work_dir = "~/.local/share/archie/work" db_path = "~/.local/share/archie/archie.db" +# The State Store's PostgreSQL connection (required; Postgres-only epic). +# `docker compose up -d postgres` provides this local default; point it at +# your own Postgres 18 server otherwise. +database_url = "postgres://archie:archie@127.0.0.1:5432/archie?sslmode=disable" poll_interval = "60s" label = "archie" bot_user = "archie-bot" diff --git a/docker-compose.yml b/docker-compose.yml index 283f19041..2d1a7b224 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -12,6 +12,32 @@ # docker compose logs -f nats (the bus) services: + # ── PostgreSQL (State Store) ─────────────────────────────────── + # The standalone archie-state-store process's database. PostgreSQL 18 is + # the required minimum and target; `postgres.Open` refuses anything older. + # `task dev` brings this up before the State Store via dev:postgres. The + # credentials and database name below are the dev default mirrored by + # deployments/dev.toml's database_url -- change both together. + postgres: + image: postgres:18 + environment: + POSTGRES_USER: archie + POSTGRES_PASSWORD: archie + POSTGRES_DB: archie + # postgres:18 moved its default PGDATA under a versioned path; pin the + # classic location so the named volume keeps working across image updates. + PGDATA: /var/lib/postgresql/data/pgdata + ports: + - "5432:5432" + volumes: + - pg_data:/var/lib/postgresql/data + restart: unless-stopped + healthcheck: + test: ["CMD-SHELL", "pg_isready -U archie -d archie"] + interval: 10s + timeout: 5s + retries: 5 + # ── NATS JetStream ──────────────────────────────────────────────── nats: image: nats:2.14-alpine3.22 @@ -56,6 +82,7 @@ services: volumes: nats_data: + pg_data: # The subnet is pinned for the optional external-broker profile because its # configured URL must be reachable from both the native daemon and workers. diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 733a17be1..5066d1a3e 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -21,6 +21,7 @@ import ( "syscall" "time" + "github.com/jackc/pgx/v5/pgxpool" natsio "github.com/nats-io/nats.go" "github.com/samcharles93/ai-sdk/runtime" @@ -108,6 +109,13 @@ type boot struct { st storecontract.TaskStore eda *edastore.Store + // pg is the State Store process's process-scoped PostgreSQL pool, opened + // and migrated by openStateStorePool. It is the one connection the + // standalone archie-state-store binary holds: do not open a second pool + // per subsystem. Nil for every process that is not the State Store (the + // daemon and gateway dial the State Store over gRPC and never open + // Postgres themselves). + pg *pgxpool.Pool // stateStore is the State Store contract adapter every daemon and gateway // store consumer depends on. It is ALWAYS the remote *staterpc.Client // dialed to the standalone archie-state-store gRPC service at diff --git a/internal/app/archied/control_plane_validation_test.go b/internal/app/archied/control_plane_validation_test.go index f0ee5fae1..8e96ef2a0 100644 --- a/internal/app/archied/control_plane_validation_test.go +++ b/internal/app/archied/control_plane_validation_test.go @@ -14,6 +14,7 @@ import ( "github.com/samcharles93/archie-core/internal/app/controlplane" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" ) // staleDatabaseOwnedSettings are TOML fragments, each carrying one value a @@ -70,9 +71,12 @@ func TestLoadConfigAcceptsStaleDatabaseOwnedValues(t *testing.T) { // staleBootGrace is how long one boot in // TestStateStoreBootsWithStaleDatabaseOwnedValues may run before the test cancels -// it: short, because the assertion does not depend on it -- a refusal arrives as -// an error either before the cancel (first select) or after it (second select, -// where a non-Canceled error fails the test). +// it. A refusal arrives as an error before the cancel (first select); a boot +// that serves returns only after the cancel (second select). The bound must be +// long enough for a full boot -- now Postgres pool + migration, then SQLite and +// the control-plane seed -- to finish, because cancelling mid-seed surfaces a +// transaction error that reads as a refusal rather than as the interrupt it is. +// 2s matches startupGrace, the sibling single-boot bound. // // staleShutdownGrace bounds the shutdown, and is deliberately not the 5s the // single-boot regression test uses: this table runs five boots back to back on a @@ -80,7 +84,7 @@ func TestLoadConfigAcceptsStaleDatabaseOwnedValues(t *testing.T) { // the bound costs nothing when shutdown is quick and only decides how long a // failure takes to report. const ( - staleBootGrace = 250 * time.Millisecond + staleBootGrace = 2 * time.Second staleShutdownGrace = 30 * time.Second ) @@ -99,8 +103,8 @@ func TestStateStoreBootsWithStaleDatabaseOwnedValues(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "config.toml") body := fmt.Sprintf( - "bot_user = 'archie'\ndb_path = %q\n%s[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n", - filepath.Join(dir, "state.db"), tt.body, + "bot_user = 'archie'\ndb_path = %q\ndatabase_url = %q\n%s[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n", + filepath.Join(dir, "state.db"), pgtest.URL(t), tt.body, ) if err := os.WriteFile(path, []byte(body), 0o600); err != nil { t.Fatal(err) diff --git a/internal/app/archied/pgtest_test.go b/internal/app/archied/pgtest_test.go new file mode 100644 index 000000000..695c9f9c9 --- /dev/null +++ b/internal/app/archied/pgtest_test.go @@ -0,0 +1,14 @@ +package archied + +import ( + "os" + "testing" + + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" +) + +// TestMain runs the archied test binary against a throwaway PostgreSQL 18 +// container, mirroring internal/infrastructure/postgres. The State Store's +// openStateStore fails closed without a working database_url, so the tests +// that exercise it (e.g. TestStateStoreDataSurvivesRestart) need a real pool. +func TestMain(m *testing.M) { os.Exit(pgtest.Main(m)) } diff --git a/internal/app/archied/state_store.go b/internal/app/archied/state_store.go index 60a5af36f..750c027ee 100644 --- a/internal/app/archied/state_store.go +++ b/internal/app/archied/state_store.go @@ -15,7 +15,6 @@ import ( "fmt" "net" "net/http" - "path/filepath" "time" "google.golang.org/grpc" @@ -25,10 +24,8 @@ import ( "github.com/samcharles93/archie-core/internal/domain/health" "github.com/samcharles93/archie-core/internal/domain/identity" "github.com/samcharles93/archie-core/internal/domain/storecontract" - "github.com/samcharles93/archie-core/internal/infrastructure/edastore" "github.com/samcharles93/archie-core/internal/infrastructure/readiness" "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" - "github.com/samcharles93/archie-core/internal/store" ) func configuredIdentityNames(cfg config.Config) []string { @@ -187,10 +184,11 @@ func openStateStoreControlPlane(resources controlplane.ResourceStore) (*controlp return server, nil } -// openStateStore opens the single task-store SQLite file exactly once for -// service ownership. It resolves secrets (for the binding cipher) but does -// NOT open gateway chat sessions -- those live in the separate archie-gateway -// process on their own SQLite file and are out of state-store scope. +// openStateStore composes the State Store process's persistence: it resolves +// secrets (for the binding cipher), opens and migrates the process-scoped +// PostgreSQL pool (fail closed), and opens the task and event-capture stores. +// It does NOT open gateway chat sessions -- those live in the separate +// archie-gateway process on their own store and are out of state-store scope. func (b *boot) openStateStore(ctx context.Context) error { cfg, log := b.cfg, b.log secrets, err := configuredSecretRegistry(&cfg, log) @@ -204,51 +202,17 @@ func (b *boot) openStateStore(ctx context.Context) error { log.Error("configure bindings cipher", "err", err) return err } - // Claim the database before opening it. This process is the file's owner - // for as long as it lives, and the claim is what an offline recovery - // command checks before it rewrites the file: without it, "the State Store - // is stopped" is a rule the operator is trusted to have followed rather - // than a fact the command can verify. A second State Store on the same - // database fails here, which is the single-owner invariant stated instead - // of merely documented. - path := taskDBPath(cfg.DBPath) - ownership, err := store.AcquireOwnership(path) - if err != nil { - log.Error("claim state store ownership", "err", err) + // Open the PostgreSQL pool and apply the schema before anything else: the + // State Store fails closed without a working database_url. + if err := b.openStateStorePool(ctx); err != nil { return err } - b.addCleanup(func() { - if err := ownership.Release(); err != nil { - log.Error("release state store ownership", "err", err) - } - }) - eda, err := edastore.Open(edastore.Config{ - DBPath: edaDBPath(cfg.DBPath), - DataDir: filepath.Join(filepath.Dir(path), "eda"), - Cipher: bindingCipher, - }) - if err != nil { - log.Error("open event-capture store", "err", err) + if err := b.openTaskStore(ctx, taskDBPath(cfg.DBPath)); err != nil { return err } - b.eda = eda - b.addCleanup(func() { - if err := eda.Close(); err != nil { - log.Error("close event-capture store", "err", err) - } - }) - - st, err := openProductionTaskStore(ctx, path) - if err != nil { - log.Error("open state store", "err", err) + if err := b.openEDAStore(cfg, bindingCipher); err != nil { return err } - b.st = st - b.addCleanup(func() { - if err := st.Close(); err != nil { - log.Error("close state store", "err", err) - } - }) return nil } diff --git a/internal/app/archied/state_store_eda.go b/internal/app/archied/state_store_eda.go new file mode 100644 index 000000000..213651ae4 --- /dev/null +++ b/internal/app/archied/state_store_eda.go @@ -0,0 +1,33 @@ +package archied + +import ( + "path/filepath" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/infrastructure/edastore" +) + +// openEDAStore opens the event-capture store (PocketBase today, Postgres when +// L2 ports it) beside the task database. It is a separate composition file +// from openTaskStore so L2 can replace edastore.Open here without colliding +// with L1's replacement of openProductionTaskStore -- the two lanes write +// different files. +func (b *boot) openEDAStore(cfg config.Config, cipher edastore.BindingCipher) error { + path := taskDBPath(cfg.DBPath) + eda, err := edastore.Open(edastore.Config{ + DBPath: edaDBPath(cfg.DBPath), + DataDir: filepath.Join(filepath.Dir(path), "eda"), + Cipher: cipher, + }) + if err != nil { + b.log.Error("open event-capture store", "err", err) + return err + } + b.eda = eda + b.addCleanup(func() { + if err := eda.Close(); err != nil { + b.log.Error("close event-capture store", "err", err) + } + }) + return nil +} diff --git a/internal/app/archied/state_store_pool.go b/internal/app/archied/state_store_pool.go new file mode 100644 index 000000000..402b62c85 --- /dev/null +++ b/internal/app/archied/state_store_pool.go @@ -0,0 +1,38 @@ +package archied + +import ( + "context" + "errors" + + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" +) + +// openStateStorePool opens the State Store process's one process-scoped +// PostgreSQL pool and applies the schema. It is the fail-closed seam for the +// Postgres-only epic: a missing or invalid database_url stops the State Store +// from starting rather than falling back to SQLite, and a migration failure is +// a startup failure, not a warning. +// +// The pool is stored on boot.pg and closed at shutdown; nothing opens a second +// pool per subsystem. +func (b *boot) openStateStorePool(ctx context.Context) error { + url := b.cfg.DatabaseURL + if url == "" { + err := errors.New("database_url is required: the State Store is Postgres-only and refuses to fall back to SQLite") + b.log.Error("open state store postgres pool", "err", err) + return err + } + pool, err := postgres.Open(ctx, url) + if err != nil { + b.log.Error("open state store postgres pool", "err", err) + return err + } + if err := postgres.Migrate(ctx, pool, postgres.Migrations()); err != nil { + pool.Close() + b.log.Error("migrate state store postgres schema", "err", err) + return err + } + b.pg = pool + b.addCleanup(pool.Close) + return nil +} diff --git a/internal/app/archied/state_store_pool_test.go b/internal/app/archied/state_store_pool_test.go new file mode 100644 index 000000000..84835468d --- /dev/null +++ b/internal/app/archied/state_store_pool_test.go @@ -0,0 +1,32 @@ +package archied + +import ( + "strings" + "testing" + + "github.com/samcharles93/archie-core/internal/config" +) + +// TestOpenStateStorePoolFailsClosedWithoutURL pins D1.2: the State Store is +// Postgres-only, so a missing database_url must stop it from starting rather +// than fall back to SQLite. +func TestOpenStateStorePoolFailsClosedWithoutURL(t *testing.T) { + b := newBootstrap() + b.cfg = config.Config{} + if err := b.openStateStorePool(t.Context()); err == nil { + t.Fatal("openStateStorePool without database_url succeeded; want fail closed") + } else if !strings.Contains(err.Error(), "database_url is required") { + t.Fatalf("openStateStorePool error = %v, want it to name the missing database_url", err) + } +} + +// TestOpenStateStorePoolFailsClosedOnInvalidURL pins the invalid half of D1.2: +// a URL that will not connect (here, one that does not parse) is the same +// failure as an absent one, surfaced at boot rather than at first query. +func TestOpenStateStorePoolFailsClosedOnInvalidURL(t *testing.T) { + b := newBootstrap() + b.cfg = config.Config{DatabaseURL: "://not-a-url"} + if err := b.openStateStorePool(t.Context()); err == nil { + t.Fatal("openStateStorePool with an unparseable database_url succeeded; want fail closed") + } +} diff --git a/internal/app/archied/state_store_regression_test.go b/internal/app/archied/state_store_regression_test.go index 7410709fc..e188b838c 100644 --- a/internal/app/archied/state_store_regression_test.go +++ b/internal/app/archied/state_store_regression_test.go @@ -14,6 +14,7 @@ import ( "time" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" ) // startupGrace is how long RunStateStore is given to either refuse the config @@ -56,7 +57,7 @@ func TestRunStateStoreAcceptsEnvironmentToken(t *testing.T) { t.Setenv("STATE_STORE_TOKEN", "environment-token") dir := t.TempDir() path := filepath.Join(dir, "config.toml") - data := fmt.Sprintf("bot_user = 'archie'\ndb_path = %q\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n[[repos]]\nowner = 'acme'\nname = 'widget'\n", filepath.Join(dir, "state.db")) + data := fmt.Sprintf("bot_user = 'archie'\ndb_path = %q\ndatabase_url = %q\n[forge]\ntype = 'github'\nhost = 'https://github.example.com'\n[[repos]]\nowner = 'acme'\nname = 'widget'\n", filepath.Join(dir, "state.db"), pgtest.URL(t)) if err := os.WriteFile(path, []byte(data), 0o600); err != nil { t.Fatal(err) } diff --git a/internal/app/archied/state_store_task.go b/internal/app/archied/state_store_task.go new file mode 100644 index 000000000..b8444ef92 --- /dev/null +++ b/internal/app/archied/state_store_task.go @@ -0,0 +1,39 @@ +package archied + +import ( + "context" + + "github.com/samcharles93/archie-core/internal/store" +) + +// openTaskStore claims ownership of the task database and opens the production +// task store. The ownership claim is what makes "the State Store is stopped" a +// fact an offline recovery command can check, and a second State Store on the +// same database fails here. It is a separate composition file from openEDAStore +// so L1 can replace openProductionTaskStore (and the ownership claim, per the +// serve-ownership decision) here without colliding with L2's edastore port. +func (b *boot) openTaskStore(ctx context.Context, path string) error { + ownership, err := store.AcquireOwnership(path) + if err != nil { + b.log.Error("claim state store ownership", "err", err) + return err + } + b.addCleanup(func() { + if err := ownership.Release(); err != nil { + b.log.Error("release state store ownership", "err", err) + } + }) + + st, err := openProductionTaskStore(ctx, path) + if err != nil { + b.log.Error("open state store", "err", err) + return err + } + b.st = st + b.addCleanup(func() { + if err := st.Close(); err != nil { + b.log.Error("close state store", "err", err) + } + }) + return nil +} diff --git a/internal/app/archied/state_store_test.go b/internal/app/archied/state_store_test.go index 363a2b9f4..0d199e82d 100644 --- a/internal/app/archied/state_store_test.go +++ b/internal/app/archied/state_store_test.go @@ -13,6 +13,7 @@ import ( pb "github.com/samcharles93/archie-core/internal/contracts/state/v1" "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/infrastructure/edastore" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" "github.com/samcharles93/archie-core/internal/logging" "github.com/samcharles93/archie-core/internal/store" @@ -170,7 +171,10 @@ func TestStateStoreDepsServePlaybookDispatcher(t *testing.T) { // (RunStateStore's own opener, not a test-only shortcut) round-trips through // the file rather than an in-memory or per-process store. func TestStateStoreDataSurvivesRestart(t *testing.T) { - cfg := config.Config{DBPath: filepath.Join(t.TempDir(), "archie")} + cfg := config.Config{ + DBPath: filepath.Join(t.TempDir(), "archie"), + DatabaseURL: pgtest.URL(t), + } first := newBootstrap() first.cfg = cfg diff --git a/internal/config/config.go b/internal/config/config.go index df758e437..d5211d967 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -485,9 +485,16 @@ type Config struct { ModuleDir string `toml:"module_dir" yaml:"module_dir"` // SecretEngineDir contains Yaegi secret-engine plugins. Built-in env and // bws engines remain available when this is empty. - SecretEngineDir string `toml:"secret_engine_dir" yaml:"secret_engine_dir"` - DBPath string `toml:"db_path" yaml:"db_path"` - PollInterval Duration `toml:"poll_interval" yaml:"poll_interval"` + SecretEngineDir string `toml:"secret_engine_dir" yaml:"secret_engine_dir"` + DBPath string `toml:"db_path" yaml:"db_path"` + // DatabaseURL is the PostgreSQL connection URL the standalone State Store + // process opens at boot. It is bootstrap-only (the same class as db_path + // and work_dir): the pool is opened once, so a runtime change cannot take + // effect, and it is refused by the runtime overlay. It has no default -- + // an empty value is a startup error for the State Store, which fails + // closed rather than fall back to SQLite (the epic is Postgres-only). + DatabaseURL string `toml:"database_url" yaml:"database_url"` + PollInterval Duration `toml:"poll_interval" yaml:"poll_interval"` // Label marks issues archie should pick up. Label string `toml:"label" yaml:"label"` BotUser string `toml:"bot_user" yaml:"bot_user"` diff --git a/internal/infrastructure/configuration/defaults.go b/internal/infrastructure/configuration/defaults.go index 18344a82f..7226360f2 100644 --- a/internal/infrastructure/configuration/defaults.go +++ b/internal/infrastructure/configuration/defaults.go @@ -226,6 +226,11 @@ func (l *Loader) applyGeneralDefaults(cfg *config.Config) { cfg.WorkDir = expandHomePath(cfg.WorkDir) cfg.DBPath = expandHomePath(cfg.DBPath) cfg.Chat.Workspace = expandHomePath(cfg.Chat.Workspace) + // DatabaseURL has no default and is not path-expanded: it is a connection + // URL, not a file path. Empty means "not configured", which the State + // Store refuses at boot (fail closed) rather than a value this loader can + // invent -- a wrong default would point a production store at a database + // that does not exist. if cfg.WorkDir == "" { cfg.WorkDir = filepath.Join(l.dataHome(), "archie", "work") } diff --git a/internal/infrastructure/configuration/denied_keys.go b/internal/infrastructure/configuration/denied_keys.go index 935989947..158c4fad8 100644 --- a/internal/infrastructure/configuration/denied_keys.go +++ b/internal/infrastructure/configuration/denied_keys.go @@ -9,6 +9,7 @@ package configuration // config renderer can reference the policy without linking the overlay's // SQLite store (archie-core-8cda.5.6). 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", + "db_path": "required for bootstrap; cannot be changed at runtime", + "database_url": "required for bootstrap; cannot be changed at runtime", + "work_dir": "pins the daemon's working layout; cannot be changed at runtime", } diff --git a/internal/webui/api_config.go b/internal/webui/api_config.go index 25e64115e..c7bcfbeaa 100644 --- a/internal/webui/api_config.go +++ b/internal/webui/api_config.go @@ -243,6 +243,7 @@ type BudgetsView struct { type StorageView struct { WorkDir string `json:"work_dir"` DBPath string `json:"db_path"` + DatabaseURL string `json:"database_url"` SkillsDir string `json:"skills_dir,omitempty"` PluginDir string `json:"plugin_dir,omitempty"` SecretEngineDir string `json:"secret_engine_dir,omitempty"` @@ -375,6 +376,7 @@ func BuildConfigView(in ConfigViewInput) ConfigView { Storage: StorageView{ WorkDir: cfg.WorkDir, DBPath: cfg.DBPath, + DatabaseURL: cfg.DatabaseURL, SkillsDir: cfg.SkillsDir, PluginDir: cfg.PluginDir, SecretEngineDir: cfg.SecretEngineDir, diff --git a/internal/webui/api_config_test.go b/internal/webui/api_config_test.go index 384558cb2..7733b2d83 100644 --- a/internal/webui/api_config_test.go +++ b/internal/webui/api_config_test.go @@ -345,7 +345,7 @@ func TestHandleConfigReportsLockedKeys(t *testing.T) { if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil { t.Fatalf("decode: %v", err) } - for _, key := range []string{"db_path", "work_dir"} { + for _, key := range []string{"db_path", "database_url", "work_dir"} { if got.Locked[key] == "" { t.Errorf("Locked[%q] is empty, want a reason", key) } diff --git a/internal/webui/config_schema.go b/internal/webui/config_schema.go index 90a46e7b7..72c442786 100644 --- a/internal/webui/config_schema.go +++ b/internal/webui/config_schema.go @@ -148,6 +148,7 @@ func configFieldDescriptors() []ConfigSection { Fields: []ConfigField{ {Key: "work_dir", Label: "Work directory", Type: FieldString, Editable: false}, {Key: "db_path", Label: "State path prefix", Type: FieldString, Editable: false}, + {Key: "database_url", Label: "PostgreSQL URL", Description: "The State Store's database connection; set at boot, restart required.", Type: FieldString, Editable: false}, {Key: "skills_dir", Label: "Shared skills directory", Description: "Empty uses the work directory.", Type: FieldString, Editable: true, RestartRequired: true}, {Key: "plugin_dir", Label: "Daemon plugin directory", Description: "Empty means no daemon plugins.", Type: FieldString, Editable: true, RestartRequired: true}, {Key: "secret_engine_dir", Label: "Secret engine plugin directory", Description: "Empty means built-in secret engines only.", Type: FieldString, Editable: true, RestartRequired: true}, @@ -192,6 +193,7 @@ func configFieldValues(view ConfigView) map[string]any { "budgets.gate_max_failures": view.Budgets.GateMaxFailures, "work_dir": view.Storage.WorkDir, "db_path": view.Storage.DBPath, + "database_url": view.Storage.DatabaseURL, "skills_dir": view.Storage.SkillsDir, "plugin_dir": view.Storage.PluginDir, "secret_engine_dir": view.Storage.SecretEngineDir, diff --git a/internal/webui/config_schema_test.go b/internal/webui/config_schema_test.go index 4b8d62646..5b3110ae4 100644 --- a/internal/webui/config_schema_test.go +++ b/internal/webui/config_schema_test.go @@ -64,7 +64,7 @@ func TestConfigFieldDescriptorsAreComplete(t *testing.T) { "bot_user", "bot_email", "label", "forge.type", "forge.host", "diff_cap_lines", "repos", "models", "providers", "budgets.max_steps", "budgets.wall_clock", "budgets.gate_max_failures", - "work_dir", "db_path", "skills_dir", "plugin_dir", "secret_engine_dir", + "work_dir", "db_path", "database_url", "skills_dir", "plugin_dir", "secret_engine_dir", "containers.image", "containers.max_concurrency", "containers.max_uptime", "containers.volume_ttl", "containers.pull_policy", "containers.network", "web.listen", @@ -101,6 +101,7 @@ func TestConfigFieldDescriptorsRestartRequiredIsDeliberate(t *testing.T) { "budgets.gate_max_failures": false, "work_dir": false, // locked, not merely restart-required "db_path": false, // locked, not merely restart-required + "database_url": false, // locked, not merely restart-required "skills_dir": true, // absent from reloadableFields "plugin_dir": true, "secret_engine_dir": true, @@ -148,7 +149,7 @@ func TestConfigFieldDescriptorsStructuredFieldsAreNotEditable(t *testing.T) { // renderer does not need runtime state to know not to offer an edit // affordance for a key that can never succeed. func TestConfigFieldDescriptorsLockedStorageFieldsAreNotEditable(t *testing.T) { - locked := map[string]bool{"work_dir": true, "db_path": true} + locked := map[string]bool{"work_dir": true, "db_path": true, "database_url": true} for _, section := range configFieldDescriptors() { for _, f := range section.Fields { if locked[f.Key] && f.Editable {