refactor(store)!: remove the SQLite task store, the PocketBase event store and the SQLite session store

The State Store, event capture and Gateway conversations already serve from
PostgreSQL. This deletes internal/store and internal/infrastructure/edastore,
the SQLite gateway session store and openProductionTaskStore, and the -db
recovery path on a legacy task file; backup, restore, validate and rollback
work on the database database_url names.

- The binding-secret cipher moves to internal/infrastructure/bindingcipher and
  ToolCall to internal/infrastructure/postgres.
- NewSessionStoreMemory is now a pure in-memory store that passes the shared
  SessionStore conformance suite.
- Tests run against PostgreSQL through internal/infrastructure/postgres/pgstore;
  legacyimport/legacyfixture writes the legacy files the import and the boot
  gate read.
- The cron dialect is owned in internal/domain/scheduling, so
  github.com/pocketbase/pocketbase and pocketbase/dbx leave go.mod.
  modernc.org/sqlite stays for the one-time import (legacyread).
- The UI and Messaging architecture gates ban the Postgres and legacy readers
  in place of internal/store.
This commit is contained in:
sam committed 2026-09-24 14:43:50 +10:00
1 parent dda12dcdba
commit ae552db7e0
90 files changed
+1022 -1453

No files matched your search

+32 -26
View File
@@ -1,8 +1,7 @@
// Package postgres opens archie's PostgreSQL database and applies its schema
// migrations. This file is the PostgreSQL implementation of the EDA
// persistence contracts (captures, mappings, bindings and the two dispatch
// ledgers) plus the tool_calls transcript, mirroring the PocketBase-backed
// internal/infrastructure/edastore.Store it will replace in the cutover wave.
// ledgers) plus the tool_calls transcript.
package postgres
import (
@@ -20,14 +19,12 @@ import (
"github.com/samcharles93/archie-core/internal/domain/mapping"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/infrastructure/edastore"
"github.com/samcharles93/archie-core/internal/infrastructure/bindingcipher"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
)
// EDA is the PostgreSQL implementation of every event-capture contract, plus
// the tool_calls transcript the composition projection writes through. It
// satisfies the same surface as *edastore.Store, so the cutover wave can swap
// one for the other behind storecontract without touching the consumers.
// the tool_calls transcript the composition projection writes through.
var (
_ storecontract.CaptureStore = (*EDA)(nil)
_ storecontract.MappingStore = (*EDA)(nil)
@@ -40,26 +37,23 @@ var (
type EDA struct {
pool *pgxpool.Pool
q *postgresdb.Queries
cipher edastore.BindingCipher
cipher bindingcipher.BindingCipher
notify func(events.Event)
}
// NewEDA builds an EDA store over pool. A nil cipher keeps binding secrets in
// plaintext (the behaviour that predates the option); the caller resolves the
// keyring through edastore.NewBindingCipher, exactly as the PocketBase store
// does. The pool is owned by the caller, not this store.
func NewEDA(pool *pgxpool.Pool, cipher edastore.BindingCipher) *EDA {
// plaintext; the caller resolves the keyring through
// bindingcipher.NewBindingCipher. The pool is owned by the caller, not this store.
func NewEDA(pool *pgxpool.Pool, cipher bindingcipher.BindingCipher) *EDA {
return &EDA{pool: pool, q: postgresdb.New(pool), cipher: cipher}
}
// SetNotify wires the change-event callback that the PocketBase store's
// Config.Notify provides. It is the same injection point: nil is a silent
// no-op, which is what every production construction gets today.
// SetNotify wires the change-event callback. nil is a silent no-op, which is
// what every production construction gets today.
func (s *EDA) SetNotify(fn func(events.Event)) { s.notify = fn }
// notifyWrite announces one successful write on the bindings or mappings
// tables. It mirrors edastore.(*Store).notifyWrite byte-for-byte: the data
// carries the record id and action, and consumers refetch the row themselves.
// tables. The data carries the record id and action, and consumers refetch the row themselves.
func (s *EDA) notifyWrite(kind, subject, action, id string) {
if s.notify == nil {
return
@@ -77,8 +71,7 @@ func newRecordID() string { return uuid.NewString() }
// --- captures ---
// InsertCapture stores one inbound event verbatim. retention and maxEvents are
// accepted for contract compatibility and applied as a prune after the insert,
// exactly as the PocketBase store does.
// applied as a prune after the insert.
func (s *EDA) InsertCapture(ctx context.Context, c storecontract.CapturedEvent, retention time.Duration, maxEvents int) (string, error) {
received := c.ReceivedAt
if received.IsZero() {
@@ -208,8 +201,8 @@ func (s *EDA) InsertMapping(ctx context.Context, m mapping.Mapping) (string, err
return id, nil
}
// GetMapping returns (nil, nil) for an absent mapping, matching the
// PocketBase store's found=false convention the gRPC layer translates.
// GetMapping returns (nil, nil) for an absent mapping, the found=false
// convention the gRPC layer translates.
func (s *EDA) GetMapping(ctx context.Context, id string) (*mapping.Mapping, error) {
r, err := s.q.GetMapping(ctx, id)
if errors.Is(err, pgx.ErrNoRows) {
@@ -347,8 +340,8 @@ func (s *EDA) InsertBinding(ctx context.Context, b binding.Binding) (string, err
return id, nil
}
// GetBinding returns (nil, nil) for an absent binding, matching the
// PocketBase store's found=false convention the gRPC layer translates.
// GetBinding returns (nil, nil) for an absent binding, the found=false
// convention the gRPC layer translates.
func (s *EDA) GetBinding(ctx context.Context, id string) (*binding.Binding, error) {
r, err := s.q.GetBinding(ctx, id)
if errors.Is(err, pgx.ErrNoRows) {
@@ -522,8 +515,21 @@ func ledgerWrite(err error, what string) error {
// --- tool calls ---
// ToolCall is one completed tool invocation during an agent stage run, as the
// tool_call event projection stores it: a tool's output summary in Result, a
// failure's detail in Error.
type ToolCall struct {
ID string
TaskID int64
Attempt int
Tool string
Result string
Error string
CalledAt time.Time
}
// InsertToolCall appends one row to the tool_calls transcript.
func (s *EDA) InsertToolCall(ctx context.Context, tc edastore.ToolCall) error {
func (s *EDA) InsertToolCall(ctx context.Context, tc ToolCall) error {
calledAt := tc.CalledAt
if calledAt.IsZero() {
calledAt = time.Now()
@@ -543,14 +549,14 @@ func (s *EDA) InsertToolCall(ctx context.Context, tc edastore.ToolCall) error {
}
// TaskToolCalls returns one task's tool calls in call order.
func (s *EDA) TaskToolCalls(ctx context.Context, taskID int64) ([]edastore.ToolCall, error) {
func (s *EDA) TaskToolCalls(ctx context.Context, taskID int64) ([]ToolCall, error) {
rows, err := s.q.TaskToolCalls(ctx, taskID)
if err != nil {
return nil, fmt.Errorf("edastore: list tool calls: %w", err)
}
out := make([]edastore.ToolCall, 0, len(rows))
out := make([]ToolCall, 0, len(rows))
for _, r := range rows {
out = append(out, edastore.ToolCall{
out = append(out, ToolCall{
ID: r.ID,
TaskID: r.TaskID,
Attempt: int(r.Attempt),
@@ -12,7 +12,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/mapping"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/events"
"github.com/samcharles93/archie-core/internal/infrastructure/edastore"
"github.com/samcharles93/archie-core/internal/infrastructure/bindingcipher"
)
const edaTestKey = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
@@ -24,7 +24,7 @@ func edaFor(t *testing.T) *EDA {
return NewEDA(pool, nil)
}
func edaWithCipher(t *testing.T, cipher edastore.BindingCipher) (*pgxpool.Pool, *EDA) {
func edaWithCipher(t *testing.T, cipher bindingcipher.BindingCipher) (*pgxpool.Pool, *EDA) {
t.Helper()
pool, _ := migrated(t)
return pool, NewEDA(pool, cipher)
@@ -131,7 +131,7 @@ func TestConcurrentInsertBindingSameSource(t *testing.T) {
// untouched, so an edit form that does not echo the secret cannot blank an
// armed binding's HMAC key.
func TestUpdateBindingEmptySecretPreservesStored(t *testing.T) {
cipher, err := edastore.NewBindingCipher(edaTestKey, nil)
cipher, err := bindingcipher.NewBindingCipher(edaTestKey, nil)
if err != nil {
t.Fatalf("NewBindingCipher() error = %v", err)
}
@@ -173,7 +173,7 @@ func readRawSecret(t *testing.T, pool *pgxpool.Pool, id string) string {
// The cipher path must keep decrypt-in-store: what is stored is an envelope,
// what reads back is the plaintext HMAC key the intake verifies with.
func TestBindingSecretEncryptedAtRestAndDecryptedOnRead(t *testing.T) {
cipher, err := edastore.NewBindingCipher(edaTestKey, nil)
cipher, err := bindingcipher.NewBindingCipher(edaTestKey, nil)
if err != nil {
t.Fatalf("NewBindingCipher() error = %v", err)
}
@@ -200,6 +200,50 @@ func TestBindingSecretEncryptedAtRestAndDecryptedOnRead(t *testing.T) {
}
}
// A deployment with no configured key stores plaintext.
func TestNoCipherKeepsPlaintext(t *testing.T) {
pool, s := edaWithCipher(t, nil)
id, err := s.InsertBinding(t.Context(), binding.Binding{
Name: "n", Matcher: binding.Matcher{Source: "src-plain"}, Workflow: "implement",
Secret: "supersecretvalue0123",
})
if err != nil {
t.Fatalf("InsertBinding() error = %v", err)
}
if stored := readRawSecret(t, pool, id); stored != "supersecretvalue0123" {
t.Errorf("stored secret = %q, want the plaintext with no cipher configured", stored)
}
}
// A secret written under the previous key stays readable once a new active
// key is installed; otherwise rotation bricks every armed binding.
func TestRotatedKeyStillReadsOldRows(t *testing.T) {
const oldKey = "fedcba9876543210fedcba9876543210fedcba9876543210fedcba9876543210"
oldCipher, err := bindingcipher.NewBindingCipher(oldKey, nil)
if err != nil {
t.Fatalf("NewBindingCipher(old) error = %v", err)
}
pool, s := edaWithCipher(t, oldCipher)
id, err := s.InsertBinding(t.Context(), binding.Binding{
Name: "n", Matcher: binding.Matcher{Source: "src-rotate"}, Workflow: "implement",
Secret: "supersecretvalue0123",
})
if err != nil {
t.Fatalf("InsertBinding() error = %v", err)
}
rotated, err := bindingcipher.NewBindingCipher(edaTestKey, []string{oldKey})
if err != nil {
t.Fatalf("NewBindingCipher(rotated) error = %v", err)
}
got, err := NewEDA(pool, rotated).GetBinding(t.Context(), id)
if err != nil {
t.Fatalf("GetBinding() after rotation error = %v", err)
}
if got.Secret != "supersecretvalue0123" {
t.Errorf("Secret after rotation = %q: the previous key must still decrypt", got.Secret)
}
}
// The lifecycle gate survives the port: pending_approval -> armed, and only
// from pending_approval.
func TestBindingLifecycle(t *testing.T) {
@@ -308,13 +352,13 @@ func TestToolCallRoundTrip(t *testing.T) {
at := time.Date(2026, 9, 23, 1, 2, 3, 0, time.UTC)
for range 2 {
if err := s.InsertToolCall(t.Context(), edastore.ToolCall{
if err := s.InsertToolCall(t.Context(), ToolCall{
TaskID: 42, Attempt: 2, Tool: "shell", Result: "all checks passed", CalledAt: at,
}); err != nil {
t.Fatalf("InsertToolCall() error = %v", err)
}
}
if err := s.InsertToolCall(t.Context(), edastore.ToolCall{
if err := s.InsertToolCall(t.Context(), ToolCall{
TaskID: 7, Attempt: 1, Tool: "read_file", Error: "no such file", CalledAt: at.Add(time.Second),
}); err != nil {
t.Fatalf("InsertToolCall(other task) error = %v", err)
@@ -0,0 +1,61 @@
// Package pgstore opens migrated PostgreSQL stores on a throwaway pgtest
// database, for tests outside internal/infrastructure/postgres. A package
// using it calls pgtest.Main from TestMain.
package pgstore
import (
"testing"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/samcharles93/archie-core/internal/infrastructure/bindingcipher"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest"
)
// TaskDB is the State Store's task database: the task, event, status and
// identity surface plus the control-plane resources, over one pool.
type TaskDB struct {
*postgres.Store
*postgres.Resources
Pool *pgxpool.Pool
}
// Pool opens a pool on a fresh migrated database, closed when t ends.
func Pool(t testing.TB) *pgxpool.Pool {
t.Helper()
return PoolAt(t, pgtest.URL(t))
}
// PoolAt opens a pool on url and migrates it, closed when t ends. Opening a
// second pool on the same url is how a test models a restart.
func PoolAt(t testing.TB, url string) *pgxpool.Pool {
t.Helper()
pool, err := postgres.Open(t.Context(), url)
if err != nil {
t.Fatalf("pgstore: open: %v", err)
}
t.Cleanup(pool.Close)
if err := postgres.Migrate(t.Context(), pool, postgres.Migrations()); err != nil {
t.Fatalf("pgstore: migrate: %v", err)
}
return pool
}
// On returns the task database over pool.
func On(pool *pgxpool.Pool) *TaskDB {
return &TaskDB{Store: postgres.New(pool), Resources: postgres.NewResources(pool), Pool: pool}
}
// Open returns the task database on a fresh migrated database.
func Open(t testing.TB) *TaskDB {
t.Helper()
return On(Pool(t))
}
// EDA returns the event-capture store on a fresh migrated database. A nil
// cipher keeps binding secrets in plaintext.
func EDA(t testing.TB, cipher bindingcipher.BindingCipher) *postgres.EDA {
t.Helper()
return postgres.NewEDA(Pool(t), cipher)
}
@@ -14,8 +14,7 @@ import (
)
// Resources is the PostgreSQL implementation of the State Store's
// control-plane resource document operations. It is separate while the rest
// of the transitional SQLite Store is still being ported.
// control-plane resource document operations.
type Resources struct {
pool *pgxpool.Pool
}