From 98e7378f2d3530f8f9930d785d8638cf0d9cdff4 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Sat, 17 Oct 2026 17:21:40 +1100 Subject: [PATCH] refactor(servicekit): stop linking the State Store and control plane into archie-messaging --- cmd/archie-messaging/architecture_test.go | 39 ++++++++++++++++++++ internal/app/archiegateway/chat_runtime.go | 3 +- internal/app/servicekit/pool.go | 27 -------------- internal/app/servicekit/runtimeconfig.go | 8 +++- internal/app/statestore/pool.go | 3 +- internal/infrastructure/postgres/postgres.go | 17 +++++++++ 6 files changed, 64 insertions(+), 33 deletions(-) create mode 100644 cmd/archie-messaging/architecture_test.go delete mode 100644 internal/app/servicekit/pool.go diff --git a/cmd/archie-messaging/architecture_test.go b/cmd/archie-messaging/architecture_test.go new file mode 100644 index 000000000..58af6b79b --- /dev/null +++ b/cmd/archie-messaging/architecture_test.go @@ -0,0 +1,39 @@ +// The Messaging Service reaches the State Store and the workflow engine only +// through their contracts. Go imports are atomic, so one reference relinks a +// runtime, and the boundary holds only while something re-measures it. + +package main + +import ( + "context" + "os/exec" + "strings" + "testing" + "time" +) + +func TestMessagingDoesNotLinkStoreOrWorkflowRuntime(t *testing.T) { + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) + defer cancel() + out, err := exec.CommandContext(ctx, "go", "list", "-deps", ".").Output() + if err != nil { + t.Fatalf("go list -deps .: %v", err) + } + linked := map[string]bool{} + for line := range strings.SplitSeq(strings.TrimSpace(string(out)), "\n") { + linked[line] = true + } + for _, pkg := range []string{ + "github.com/jackc/pgx/v5/pgxpool", + "github.com/samcharles93/archie-core/internal/infrastructure/postgres", + "github.com/samcharles93/archie-core/internal/app/controlplane", + "github.com/samcharles93/archie-core/internal/domain/workflow", + "github.com/samcharles93/archie-core/internal/agentexec", + } { + t.Run(pkg, func(t *testing.T) { + if linked[pkg] { + t.Errorf("cmd/archie-messaging links %s: services reach each other only through contracts", pkg) + } + }) + } +} diff --git a/internal/app/archiegateway/chat_runtime.go b/internal/app/archiegateway/chat_runtime.go index b2dc34d42..f81d57970 100644 --- a/internal/app/archiegateway/chat_runtime.go +++ b/internal/app/archiegateway/chat_runtime.go @@ -11,7 +11,6 @@ import ( "github.com/samcharles93/archie-core/internal/agentexec" "github.com/samcharles93/archie-core/internal/agentexec/modelloop" - "github.com/samcharles93/archie-core/internal/app/servicekit" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/agentrun" "github.com/samcharles93/archie-core/internal/gateway" @@ -22,7 +21,7 @@ import ( ) func (b *server) openChatSessions(ctx context.Context) error { - pool, err := servicekit.OpenPool(ctx, b.cfg.DatabaseURL, "the conversation store") + pool, err := postgres.OpenMigrated(ctx, b.cfg.DatabaseURL, "the conversation store") if err != nil { return fmt.Errorf("open conversation store: %w", err) } diff --git a/internal/app/servicekit/pool.go b/internal/app/servicekit/pool.go deleted file mode 100644 index e64d61628..000000000 --- a/internal/app/servicekit/pool.go +++ /dev/null @@ -1,27 +0,0 @@ -package servicekit - -import ( - "context" - "fmt" - - "github.com/jackc/pgx/v5/pgxpool" - - "github.com/samcharles93/archie-core/internal/infrastructure/postgres" -) - -// OpenPool opens and migrates the PostgreSQL pool a service owns its tables in. -// service names the owner in the error an empty database_url produces. -func OpenPool(ctx context.Context, url, service string) (*pgxpool.Pool, error) { - if url == "" { - return nil, fmt.Errorf("database_url is required: %s is Postgres-only", service) - } - pool, err := postgres.Open(ctx, url) - if err != nil { - return nil, err - } - if err := postgres.Migrate(ctx, pool, postgres.Migrations()); err != nil { - pool.Close() - return nil, err - } - return pool, nil -} diff --git a/internal/app/servicekit/runtimeconfig.go b/internal/app/servicekit/runtimeconfig.go index 933bc7e90..8dbb334b8 100644 --- a/internal/app/servicekit/runtimeconfig.go +++ b/internal/app/servicekit/runtimeconfig.go @@ -6,20 +6,24 @@ import ( "log/slog" "slices" - "github.com/samcharles93/archie-core/internal/app/controlplane" "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/domain/applystatus" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/secret" ) +// RuntimeConfigSource reads the control plane's stored settings. +type RuntimeConfigSource interface { + RuntimeConfig(ctx context.Context, base config.Config) (config.Config, map[string]int64, error) +} + // RuntimeConfig layers the control plane's stored settings over base, lets // adjust apply the caller's own live settings, then validates the result and // resolves its provider secrets. The outcome is reported for every layered // kind in reportKinds (every kind when nil), so a refused document keeps the // version still running on the record and a process never reports a kind it // does not re-apply live. -func RuntimeConfig(ctx context.Context, cp *controlplane.Client, status *applystatus.Reporter, secrets *secret.Registry, log *slog.Logger, base config.Config, adjust func(*config.Config), reportKinds []string) (config.Config, map[string]int64, error) { +func RuntimeConfig(ctx context.Context, cp RuntimeConfigSource, status *applystatus.Reporter, secrets *secret.Registry, log *slog.Logger, base config.Config, adjust func(*config.Config), reportKinds []string) (config.Config, map[string]int64, error) { cfg, versions, err := cp.RuntimeConfig(ctx, base) if err != nil { return config.Config{}, nil, err diff --git a/internal/app/statestore/pool.go b/internal/app/statestore/pool.go index b79ca26ee..e83a816a9 100644 --- a/internal/app/statestore/pool.go +++ b/internal/app/statestore/pool.go @@ -3,7 +3,6 @@ package statestore import ( "context" - "github.com/samcharles93/archie-core/internal/app/servicekit" "github.com/samcharles93/archie-core/internal/infrastructure/postgres" ) @@ -16,7 +15,7 @@ import ( // The pool is stored on boot.pg and closed at shutdown; nothing opens a second // pool per subsystem. func (b *server) openStateStorePool(ctx context.Context) error { - pool, err := servicekit.OpenPool(ctx, b.cfg.DatabaseURL, "the State Store") + pool, err := postgres.OpenMigrated(ctx, b.cfg.DatabaseURL, "the State Store") if err != nil { b.log.Error("open state store postgres pool", "err", err) return err diff --git a/internal/infrastructure/postgres/postgres.go b/internal/infrastructure/postgres/postgres.go index b56142e51..f5d442ab2 100644 --- a/internal/infrastructure/postgres/postgres.go +++ b/internal/infrastructure/postgres/postgres.go @@ -59,6 +59,23 @@ func Open(ctx context.Context, url string) (*pgxpool.Pool, error) { return pool, nil } +// OpenMigrated opens and migrates the pool a service owns its tables in. +// service names the owner in the error an empty database_url produces. +func OpenMigrated(ctx context.Context, url, service string) (*pgxpool.Pool, error) { + if url == "" { + return nil, fmt.Errorf("database_url is required: %s is Postgres-only", service) + } + pool, err := Open(ctx, url) + if err != nil { + return nil, err + } + if err := Migrate(ctx, pool, Migrations()); err != nil { + pool.Close() + return nil, err + } + return pool, nil +} + // Migrate applies the goose migrations in fsys. A session-level advisory lock // makes processes that start together apply each migration once. func Migrate(ctx context.Context, pool *pgxpool.Pool, fsys fs.FS) error {