feat(postgres): wire the State Store to Postgres — DB URL, fail-closed pool, composition seam

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.
This commit is contained in:
sam committed 2026-09-24 02:18:56 +10:00
1 parent cebbcb5690
commit 39967f0ad9
25 files changed
+294 -62

No files matched your search

+8
View File
@@ -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
@@ -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)
+14
View File
@@ -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)) }
+10 -46
View File
@@ -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
}
+33
View File
@@ -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
}
+38
View File
@@ -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
}
@@ -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")
}
}
@@ -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)
}
+39
View File
@@ -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
}
+5 -1
View File
@@ -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
+10 -3
View File
@@ -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"`
@@ -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")
}
@@ -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",
}
+2
View File
@@ -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,
+1 -1
View File
@@ -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)
}
+2
View File
@@ -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,
+3 -2
View File
@@ -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 {