diff --git a/cmd/archie-state-store/main_test.go b/cmd/archie-state-store/main_test.go index 881ddd820..bf2c5f6a5 100644 --- a/cmd/archie-state-store/main_test.go +++ b/cmd/archie-state-store/main_test.go @@ -429,17 +429,13 @@ func TestStateStoreRealProcessSmoke(t *testing.T) { } }) - t.Run("single_owner_sqlite", func(t *testing.T) { - // The binary owns one task SQLite file (db_path + "-tasks.sqlite"); - // the consumer called only gRPC methods and never opened a store file. - // The config anchor path itself must not be opened directly (no dual - // store ownership, §12 step 8). - taskFile := filepath.Join(dir, "archie.db-tasks.sqlite") - if _, err := os.Stat(taskFile); err != nil { - t.Fatalf("state store did not own %s: %v", taskFile, err) - } - if _, err := os.Stat(filepath.Join(dir, "archie.db")); err == nil { - t.Fatalf("config anchor path was opened directly; dual store ownership violated §12 step 8") + t.Run("serves_from_postgres_only", func(t *testing.T) { + // The binary serves every store from database_url: no SQLite file + // appears beside the configured db_path. + for _, name := range []string{"archie.db", "archie.db-tasks.sqlite", "archie.db-eda.sqlite"} { + if _, err := os.Stat(filepath.Join(dir, name)); err == nil { + t.Fatalf("state store created %s; it serves from Postgres only", name) + } } }) } diff --git a/config.example.toml b/config.example.toml index 58af92add..3267ec6b8 100644 --- a/config.example.toml +++ b/config.example.toml @@ -15,9 +15,10 @@ # 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 +# The PostgreSQL connection archie-state-store, archied and archie-gateway +# serve from. Required: each fails closed rather than fall back to SQLite. +# An install with SQLite stores under db_path runs `archie-state-store import` +# once, with archie stopped, before it starts. `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" diff --git a/docs/architecture/safe-change-and-recovery.md b/docs/architecture/safe-change-and-recovery.md index a1bb55e4e..4f7625131 100644 --- a/docs/architecture/safe-change-and-recovery.md +++ b/docs/architecture/safe-change-and-recovery.md @@ -120,6 +120,30 @@ file values would restore the second source of truth the control plane exists to remove. The cost is that a bad stored value stops the daemon starting, and the value can only be corrected through the daemon's own API. +### Upgrading an install from SQLite + +The State Store, the daemon and the Gateway serve from `database_url` only. An +install that still holds the SQLite stores (`-tasks.sqlite`, +`-eda.sqlite`, `-conversations.sqlite`) upgrades in three +steps: + +1. Stop every archie process. +2. Run `archie-state-store import`. It moves all three stores into Postgres in + one transaction, verifies the rows, and records completion. The SQLite files + are read, never written. +3. Start archie. + +Each serving process checks this at boot, after migrating the schema. With no +completion record and a non-empty legacy file it refuses to start and names the +files and the import command, rather than serve empty tables over existing +data. There is no override. With no legacy data it is a fresh install: it writes +the completion record itself, so a legacy file that appears later is ignored. +A database that recorded a fresh start refuses a later import. + +The Gateway holds a Postgres serve-ownership claim for its whole life, so a +second Gateway against the same database refuses to start (its turn recovery +would fail the first one's in-flight turns). The State Store holds its own. + ### Recovery: offline commands on the State Store Nothing skips the control plane at boot, and the only writer for a resource is diff --git a/docs/data/generated/dev-docs.json b/docs/data/generated/dev-docs.json index 8ea3079f8..a3a3fe198 100644 --- a/docs/data/generated/dev-docs.json +++ b/docs/data/generated/dev-docs.json @@ -100,7 +100,7 @@ "url": "/dev/architecture/safe-change-and-recovery/", "title": "Safe Change and Recovery", "section": "Architecture", - "body": "# Safe Change and Recovery\n\n**Status:** Requirements approved; mechanics are under design. Database-backed settings are implemented through the control plane (2026-09): see the degrade/recovery section below, which records a gap rather than a solution. The full runtime-supervision protocol (bounded observation, versioned audit trail) remains under design.\n**Date:** 2026-07-28 \n**Tracking issue:** [#73](https://github.com/samcharles93/archie-core/issues/73)\n\n## Purpose\n\nArchie is built and improved by agents as well as humans. The architecture MUST\nmake a change easy to locate, understand, test, audit, extend, and reverse\nwithout requiring its author to understand unrelated parts of the application.\n\nSeparation of concerns is therefore an operational safety boundary, not only a\ncode-organisation preference.\n\n## Required outcomes\n\n- An agent can improve a focused feature without receiving authority over\n unrelated domains.\n- Every event, function, action, setting, and policy has an identifiable owner\n and provenance.\n- Source and runtime changes pass deterministic checks outside the actor making\n the change.\n- A rejected or unhealthy candidate cannot replace the last known working\n version.\n- Invalid user configuration does not terminate an otherwise healthy\n application.\n- Configuration and service failures are observable, attributed, and eligible\n for bounded remediation or rollback.\n- The application continues serving unaffected capabilities when one\n capability is misconfigured or unhealthy.\n- Recovery failure is reported and contained rather than hidden in an\n unbounded retry loop.\n\n## Source changes\n\nAgent-authored source changes occur through the task-execution boundary in an\nisolated workspace. The agent does not directly mutate the running\ninstallation.\n\nPromotion is controlled by deterministic repository-owned policy, including\ntests, generated-reference drift checks, protected paths, change scope, and\nother required gates. The actor proposing a change cannot waive these checks.\n\nRepository and domain separation SHOULD allow a task to load and modify only\nthe governing requirements and code for its declared scope.\n\n## Runtime changes\n\nConfiguration and other runtime changes are proposed as candidates rather than\nmutating the active state in place.\n\nThe affected domain owns:\n\n- the command describing the semantic change;\n- validation of its typed settings;\n- health signals that demonstrate correct operation;\n- domain-specific remediation and rollback consequences.\n\nShared policy supplies consistent evaluation, evidence, and audit mechanics.\nIdentity supplies actor attribution. Infrastructure supplies external\nconfiguration storage, service control, and other operational adapters.\n\n## Runtime change protocol\n\nLast-known-good preservation and candidate promotion are the universal runtime\nchange protocol.\n\n1. An identity proposes an immutable candidate version.\n2. Infrastructure parses it without changing active state.\n3. Each affected domain validates its typed settings.\n4. Shared policy evaluates cross-cutting constraints.\n5. Each affected capability prepares the candidate without destroying its\n active instance.\n6. Capability-specific health checks prove the candidate works.\n7. Runtime supervision atomically promotes the candidate.\n8. A bounded observation period watches for delayed failure.\n9. Failure restores the last-known-good version and records the reason,\n evidence, actor, and involved versions.\n10. Repeated remediation failure stops retrying, isolates the capability, and\n reports the fault.\n\nThe affected domain owns the semantic change command, typed validation, health\nsignals, and remediation consequences. Shared runtime supervision owns the\npromotion transaction. Infrastructure performs storage and service-control\noperations.\n\nThe protocol's exact Go contracts, storage model, atomicity boundary, and\nobservation rules require a focused runtime-supervision design pass.\n\n## Failure isolation\n\nInvalid input MUST leave the active valid configuration unchanged and produce a\ndiagnostic record.\n\nA capability that cannot start or reload SHOULD fail independently where its\nabsence does not violate a declared application invariant. Unaffected\ncapabilities continue operating.\n\nRemediation MUST be bounded, observable, and policy-driven. Recovery MUST NOT\nsilently oscillate between versions or retry forever.\n\n## Database settings: degrade paths and recovery (2026-09)\n\nThe control plane owns runtime-tunable settings and stores them in `archie.db`.\nThis replaced a dashboard overlay store that degraded to file config on any\nfailure and had three documented ways back. The replacement has none, and the\nposture is the opposite: **the daemon fails closed.**\n\n### Degrade paths\n\n| Failure | Boot behaviour | Where the operator sees it |\n| --- | --- | --- |\n| State Store unreachable | `boot.loadRuntimeConfig` returns the dial error and `archied` exits non-zero | stderr: `runtime settings unavailable` |\n| A stored resource fails validation | Same: `validate database settings` wraps the error and the daemon exits | Same |\n| SIGHUP reload with a bad file, or a control plane that cannot be read | Running config is kept; the reload records `last_error` / `last_error_at`. The reload re-applies the database layer, so it never publishes file values over the running database-owned ones | `/api/config` → `reload.last_error` → dashboard banner |\n\nFailing closed is deliberate for settings the database owns: falling back to the\nfile values would restore the second source of truth the control plane exists to\nremove. The cost is that a bad stored value stops the daemon starting, and the\nvalue can only be corrected through the daemon's own API.\n\n### Recovery: offline commands on the State Store\n\nNothing skips the control plane at boot, and the only writer for a resource is\n`ControlPlaneService.Command` on a running State Store -- which is exactly what\nthe daemon fails closed without. `archie-state-store` therefore carries the\noffline recovery commands (`archie-core-6zw0`). They work on the task database\nfile directly -- the configured `db_path` with `-tasks.sqlite` appended, not\n`db_path` itself -- and need neither `archied` nor the Web UI running: `validate`\nreads the configuration file the daemon boots with, which is what makes its\nverdict the daemon's verdict.\n\n| Command | What it does | State Store |\n| --- | --- | --- |\n| `backup -db F -out F` | writes a transactionally consistent snapshot (`VACUUM INTO`), verified as a usable store before it is published | may be running |\n| `restore -db F -from F` | replaces the database with a snapshot and drops the replaced file's `-wal`/`-shm` | must be stopped |\n| `validate -db F [-config C]` | runs the checks that stop archied starting: the file is readable, uncorrupted and no newer than the binary; every stored resource decodes under the definition that owns it; and `configuration.Validate` accepts the settings the daemon would read -- the stored ones, or the seed the State Store writes for a kind the file does not hold yet -- layered onto `C`, the check the daemon reports as `validate database settings` | may be running |\n| `rollback -db F -kind K [-revision N]` | replays an earlier revision of a stored resource through the ordinary replace, recording the rollback as a new revision | must be stopped |\n\nThe two validation layers `validate` reports are not the same check, and it\nreports both rather than merging them: the write path's own decode is stricter\nabout shape (it rejects unknown fields boot's decode ignores), while boot is\nstricter about meaning (only boot checks `dispatch.trigger`, and a positive\n`poll_interval`). Either one refusing is a store somebody has to look at.\n\nNo rollback RPC exists and none is needed: a revision carries its own value, so\nrestoring one is the same replace the dashboard and the channels already use.\nThe offline command is that operation with the store stopped -- the one state in\nwhich the daemon's own API cannot serve it.\n\n\"Must be stopped\" is checked rather than assumed. The serving process takes an\nexclusive flock on `\u003cdb\u003e.lock` for its whole life, and `restore` and `rollback`\nrefuse with `task database is owned by another process` while another process\nholds it. The read-only commands -- `backup` and `validate` -- take no lock and\nare meant to run against a serving store: `backup` because the update installer\nruns inside the process it is replacing and cannot stop the State Store, and\n`validate` because an operator checks a running deployment with it. That\n`backup` exemption is only sound while backup never replaces the database, so\nnaming the database as its own `-out` is refused. `scripts/archie-update-install`\ntakes its pre-flight snapshot through this subcommand rather than through its\nown `sqlite3` shell, so the installer and an operator run one implementation.\n\n`rollback` is the surgical route back from a bad stored value; `restore` is the\nblunt one, and is also the way back from a schema migration a release cannot\ngrow out of.\n\n## Gateway restart: two constraints learned the hard way\n\nTelegram `/restart` (`internal/channels/telegram/restart.go` plus the `Start`\nsupervisor loop). Recovered 2026-08-09 from the pre-migration issue tracker.\nBoth constraints must survive any refactor.\n\n**1. Deadlock.** The `/restart` handler runs *on* the bot instance the supervisor\nis about to stop. Tearing down inline from the handler deadlocks the restart it\njust asked for. So the handler sends on a **buffered** channel (`restartCh`,\ncapacity 1) and returns immediately; the supervisor loop in `Start` does the\nteardown. Duplicate requests hit the default branch and are dropped, not queued.\n\n**2. Lockout.** A failed config reload must **not** be fatal. It logs and\nrelaunches with the previous in-memory settings. If a bad config edit killed the\ngateway, it would also destroy the only means of fixing it remotely — which is\nthe entire reason `/restart` exists.\n\n**Scope is gateway-only by design.** The daemon keeps running so in-flight agent\ntasks survive. Exiting the process would also restart under a `unless-stopped`\npolicy, but would kill running work. `Reload` (supplied by `cmd/archied/main.go`)\nre-reads config from disk and refreshes **only** the token and `AllowedUserIDs`;\neverything else is wired into the daemon at construction and still needs a full\nrestart.\n\n**Authorisation is inherited:** `authorizedMessage` rejects non-allowlisted\nsenders before any handler runs, so all gateway commands are admin-only by\nconstruction. Do not add per-command auth without checking that invariant still\nholds.\n" + "body": "# Safe Change and Recovery\n\n**Status:** Requirements approved; mechanics are under design. Database-backed settings are implemented through the control plane (2026-09): see the degrade/recovery section below, which records a gap rather than a solution. The full runtime-supervision protocol (bounded observation, versioned audit trail) remains under design.\n**Date:** 2026-07-28 \n**Tracking issue:** [#73](https://github.com/samcharles93/archie-core/issues/73)\n\n## Purpose\n\nArchie is built and improved by agents as well as humans. The architecture MUST\nmake a change easy to locate, understand, test, audit, extend, and reverse\nwithout requiring its author to understand unrelated parts of the application.\n\nSeparation of concerns is therefore an operational safety boundary, not only a\ncode-organisation preference.\n\n## Required outcomes\n\n- An agent can improve a focused feature without receiving authority over\n unrelated domains.\n- Every event, function, action, setting, and policy has an identifiable owner\n and provenance.\n- Source and runtime changes pass deterministic checks outside the actor making\n the change.\n- A rejected or unhealthy candidate cannot replace the last known working\n version.\n- Invalid user configuration does not terminate an otherwise healthy\n application.\n- Configuration and service failures are observable, attributed, and eligible\n for bounded remediation or rollback.\n- The application continues serving unaffected capabilities when one\n capability is misconfigured or unhealthy.\n- Recovery failure is reported and contained rather than hidden in an\n unbounded retry loop.\n\n## Source changes\n\nAgent-authored source changes occur through the task-execution boundary in an\nisolated workspace. The agent does not directly mutate the running\ninstallation.\n\nPromotion is controlled by deterministic repository-owned policy, including\ntests, generated-reference drift checks, protected paths, change scope, and\nother required gates. The actor proposing a change cannot waive these checks.\n\nRepository and domain separation SHOULD allow a task to load and modify only\nthe governing requirements and code for its declared scope.\n\n## Runtime changes\n\nConfiguration and other runtime changes are proposed as candidates rather than\nmutating the active state in place.\n\nThe affected domain owns:\n\n- the command describing the semantic change;\n- validation of its typed settings;\n- health signals that demonstrate correct operation;\n- domain-specific remediation and rollback consequences.\n\nShared policy supplies consistent evaluation, evidence, and audit mechanics.\nIdentity supplies actor attribution. Infrastructure supplies external\nconfiguration storage, service control, and other operational adapters.\n\n## Runtime change protocol\n\nLast-known-good preservation and candidate promotion are the universal runtime\nchange protocol.\n\n1. An identity proposes an immutable candidate version.\n2. Infrastructure parses it without changing active state.\n3. Each affected domain validates its typed settings.\n4. Shared policy evaluates cross-cutting constraints.\n5. Each affected capability prepares the candidate without destroying its\n active instance.\n6. Capability-specific health checks prove the candidate works.\n7. Runtime supervision atomically promotes the candidate.\n8. A bounded observation period watches for delayed failure.\n9. Failure restores the last-known-good version and records the reason,\n evidence, actor, and involved versions.\n10. Repeated remediation failure stops retrying, isolates the capability, and\n reports the fault.\n\nThe affected domain owns the semantic change command, typed validation, health\nsignals, and remediation consequences. Shared runtime supervision owns the\npromotion transaction. Infrastructure performs storage and service-control\noperations.\n\nThe protocol's exact Go contracts, storage model, atomicity boundary, and\nobservation rules require a focused runtime-supervision design pass.\n\n## Failure isolation\n\nInvalid input MUST leave the active valid configuration unchanged and produce a\ndiagnostic record.\n\nA capability that cannot start or reload SHOULD fail independently where its\nabsence does not violate a declared application invariant. Unaffected\ncapabilities continue operating.\n\nRemediation MUST be bounded, observable, and policy-driven. Recovery MUST NOT\nsilently oscillate between versions or retry forever.\n\n## Database settings: degrade paths and recovery (2026-09)\n\nThe control plane owns runtime-tunable settings and stores them in `archie.db`.\nThis replaced a dashboard overlay store that degraded to file config on any\nfailure and had three documented ways back. The replacement has none, and the\nposture is the opposite: **the daemon fails closed.**\n\n### Degrade paths\n\n| Failure | Boot behaviour | Where the operator sees it |\n| --- | --- | --- |\n| State Store unreachable | `boot.loadRuntimeConfig` returns the dial error and `archied` exits non-zero | stderr: `runtime settings unavailable` |\n| A stored resource fails validation | Same: `validate database settings` wraps the error and the daemon exits | Same |\n| SIGHUP reload with a bad file, or a control plane that cannot be read | Running config is kept; the reload records `last_error` / `last_error_at`. The reload re-applies the database layer, so it never publishes file values over the running database-owned ones | `/api/config` → `reload.last_error` → dashboard banner |\n\nFailing closed is deliberate for settings the database owns: falling back to the\nfile values would restore the second source of truth the control plane exists to\nremove. The cost is that a bad stored value stops the daemon starting, and the\nvalue can only be corrected through the daemon's own API.\n\n### Upgrading an install from SQLite\n\nThe State Store, the daemon and the Gateway serve from `database_url` only. An\ninstall that still holds the SQLite stores (`\u003cdb_path\u003e-tasks.sqlite`,\n`\u003cdb_path\u003e-eda.sqlite`, `\u003cdb_path\u003e-conversations.sqlite`) upgrades in three\nsteps:\n\n1. Stop every archie process.\n2. Run `archie-state-store import`. It moves all three stores into Postgres in\n one transaction, verifies the rows, and records completion. The SQLite files\n are read, never written.\n3. Start archie.\n\nEach serving process checks this at boot, after migrating the schema. With no\ncompletion record and a non-empty legacy file it refuses to start and names the\nfiles and the import command, rather than serve empty tables over existing\ndata. There is no override. With no legacy data it is a fresh install: it writes\nthe completion record itself, so a legacy file that appears later is ignored.\nA database that recorded a fresh start refuses a later import.\n\nThe Gateway holds a Postgres serve-ownership claim for its whole life, so a\nsecond Gateway against the same database refuses to start (its turn recovery\nwould fail the first one's in-flight turns). The State Store holds its own.\n\n### Recovery: offline commands on the State Store\n\nNothing skips the control plane at boot, and the only writer for a resource is\n`ControlPlaneService.Command` on a running State Store -- which is exactly what\nthe daemon fails closed without. `archie-state-store` therefore carries the\noffline recovery commands (`archie-core-6zw0`). They work on the task database\nfile directly -- the configured `db_path` with `-tasks.sqlite` appended, not\n`db_path` itself -- and need neither `archied` nor the Web UI running: `validate`\nreads the configuration file the daemon boots with, which is what makes its\nverdict the daemon's verdict.\n\n| Command | What it does | State Store |\n| --- | --- | --- |\n| `backup -db F -out F` | writes a transactionally consistent snapshot (`VACUUM INTO`), verified as a usable store before it is published | may be running |\n| `restore -db F -from F` | replaces the database with a snapshot and drops the replaced file's `-wal`/`-shm` | must be stopped |\n| `validate -db F [-config C]` | runs the checks that stop archied starting: the file is readable, uncorrupted and no newer than the binary; every stored resource decodes under the definition that owns it; and `configuration.Validate` accepts the settings the daemon would read -- the stored ones, or the seed the State Store writes for a kind the file does not hold yet -- layered onto `C`, the check the daemon reports as `validate database settings` | may be running |\n| `rollback -db F -kind K [-revision N]` | replays an earlier revision of a stored resource through the ordinary replace, recording the rollback as a new revision | must be stopped |\n\nThe two validation layers `validate` reports are not the same check, and it\nreports both rather than merging them: the write path's own decode is stricter\nabout shape (it rejects unknown fields boot's decode ignores), while boot is\nstricter about meaning (only boot checks `dispatch.trigger`, and a positive\n`poll_interval`). Either one refusing is a store somebody has to look at.\n\nNo rollback RPC exists and none is needed: a revision carries its own value, so\nrestoring one is the same replace the dashboard and the channels already use.\nThe offline command is that operation with the store stopped -- the one state in\nwhich the daemon's own API cannot serve it.\n\n\"Must be stopped\" is checked rather than assumed. The serving process takes an\nexclusive flock on `\u003cdb\u003e.lock` for its whole life, and `restore` and `rollback`\nrefuse with `task database is owned by another process` while another process\nholds it. The read-only commands -- `backup` and `validate` -- take no lock and\nare meant to run against a serving store: `backup` because the update installer\nruns inside the process it is replacing and cannot stop the State Store, and\n`validate` because an operator checks a running deployment with it. That\n`backup` exemption is only sound while backup never replaces the database, so\nnaming the database as its own `-out` is refused. `scripts/archie-update-install`\ntakes its pre-flight snapshot through this subcommand rather than through its\nown `sqlite3` shell, so the installer and an operator run one implementation.\n\n`rollback` is the surgical route back from a bad stored value; `restore` is the\nblunt one, and is also the way back from a schema migration a release cannot\ngrow out of.\n\n## Gateway restart: two constraints learned the hard way\n\nTelegram `/restart` (`internal/channels/telegram/restart.go` plus the `Start`\nsupervisor loop). Recovered 2026-08-09 from the pre-migration issue tracker.\nBoth constraints must survive any refactor.\n\n**1. Deadlock.** The `/restart` handler runs *on* the bot instance the supervisor\nis about to stop. Tearing down inline from the handler deadlocks the restart it\njust asked for. So the handler sends on a **buffered** channel (`restartCh`,\ncapacity 1) and returns immediately; the supervisor loop in `Start` does the\nteardown. Duplicate requests hit the default branch and are dropped, not queued.\n\n**2. Lockout.** A failed config reload must **not** be fatal. It logs and\nrelaunches with the previous in-memory settings. If a bad config edit killed the\ngateway, it would also destroy the only means of fixing it remotely — which is\nthe entire reason `/restart` exists.\n\n**Scope is gateway-only by design.** The daemon keeps running so in-flight agent\ntasks survive. Exiting the process would also restart under a `unless-stopped`\npolicy, but would kill running work. `Reload` (supplied by `cmd/archied/main.go`)\nre-reads config from disk and refreshes **only** the token and `AllowedUserIDs`;\neverything else is wired into the daemon at construction and still needs a full\nrestart.\n\n**Authorisation is inherited:** `authorizedMessage` rejects non-allowlisted\nsenders before any handler runs, so all gateway commands are admin-only by\nconstruction. Do not add per-command auth without checking that invariant still\nholds.\n" }, { "url": "/dev/prds/01-project-management/", diff --git a/internal/app/archied/bootstrap.go b/internal/app/archied/bootstrap.go index 5066d1a3e..5281d21dd 100644 --- a/internal/app/archied/bootstrap.go +++ b/internal/app/archied/bootstrap.go @@ -47,11 +47,11 @@ import ( forgewebhook "github.com/samcharles93/archie-core/internal/forge/webhook" "github.com/samcharles93/archie-core/internal/gateway" "github.com/samcharles93/archie-core/internal/infrastructure/configuration" - "github.com/samcharles93/archie-core/internal/infrastructure/edastore" infraembedding "github.com/samcharles93/archie-core/internal/infrastructure/embedding" "github.com/samcharles93/archie-core/internal/infrastructure/eventbus/nats" infraMemory "github.com/samcharles93/archie-core/internal/infrastructure/memory" "github.com/samcharles93/archie-core/internal/infrastructure/modelcatalog" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" "github.com/samcharles93/archie-core/internal/infrastructure/sessioncurator" "github.com/samcharles93/archie-core/internal/infrastructure/skillcurator" "github.com/samcharles93/archie-core/internal/infrastructure/staterpc" @@ -108,13 +108,12 @@ type boot struct { playbooks *playbook.Store st storecontract.TaskStore - eda *edastore.Store + eda eventCaptureStore // 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). + // per subsystem. Nil for every other process: the daemon and Gateway reach + // the State Store over gRPC and hold only the conversation store's pool. 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 @@ -123,7 +122,7 @@ type boot struct { // in-process, per docs/prds/state-store-contract.md §12 step 7). It is set // by openStateStoreAdapter, which requires [services.state].target to be // set. The b.st field remains solely for the standalone archie-state-store - // binary, which owns the single SQLite file. + // binary, which serves the task store from Postgres. stateStore storecontract.TaskStore // stateStoreGrants issues per-task, scoped State Store credentials for // agent containers (daemon.StateStoreGrantIssuer), wrapping the same @@ -155,6 +154,9 @@ type boot struct { // boot builds both archied and archie-gateway. processName string chatSessionStore gateway.SessionStore + // chatPool is the conversation store's pool, which the store owns and + // closes; the Gateway also holds its serve claim on it. + chatPool *pgxpool.Pool catalog modelcatalog.Snapshot catalogModels []string @@ -362,9 +364,8 @@ func (b *boot) openStores(ctx context.Context) error { b.secrets = secrets b.forgeClient, b.token = resolveForge(cfg.Forge, secrets, log) - // The daemon and gateway no longer open archie.db directly: the single - // SQLite file is owned by the standalone archie-state-store process, and - // both consumers dial its gRPC State Store contract. openStateStoreAdapter + // The daemon and gateway do not serve the State Store's tables: the + // standalone archie-state-store process does, and both consumers dial its gRPC State Store contract. openStateStoreAdapter // (called by Run and RunGateway after openStores) resolves b.stateStore as // the remote *staterpc.Client. There is no local store to open here // (docs/prds/state-store-contract.md §12 step 7, no dual-store ownership). @@ -409,11 +410,16 @@ func (b *boot) openStateStoreAdapter() error { return nil } -func (b *boot) openChatSessions(_ context.Context) error { //nolint:unparam // context keeps the composition phase contract aligned with other openers - chatSessionStore, err := makeTelegramSessionStore(b.cfg) //nolint:contextcheck // SQLite schema initialization is synchronous and has no context-aware API +// openChatSessions opens the conversation store on its own pool from +// database_url. The daemon reads it (session curator) and the Gateway serves +// it; only the Gateway claims serve ownership (claimGatewayOwnership). +func (b *boot) openChatSessions(ctx context.Context) error { + pool, err := openServicePool(ctx, b.cfg.DatabaseURL, b.cfg.DBPath, "the conversation store") if err != nil { return fmt.Errorf("open conversation store: %w", err) } + chatSessionStore := gateway.NewPostgresSessionStore(pool) + b.chatPool = pool b.chatSessionStore = chatSessionStore b.addCleanup(func() { if err := chatSessionStore.Close(); err != nil { @@ -423,6 +429,24 @@ func (b *boot) openChatSessions(_ context.Context) error { //nolint:unparam // c return nil } +// claimGatewayOwnership holds the Gateway's serve claim for the process's +// life, so a second Gateway (whose turn recovery would fail this one's +// in-flight turns) refuses to start. It runs after openChatSessions, so its +// cleanup releases the claim before the conversation store closes the pool. +func (b *boot) claimGatewayOwnership(ctx context.Context) error { + ownership, err := postgres.AcquireOwnership(ctx, b.chatPool, postgres.OwnerGateway) + if err != nil { + return fmt.Errorf("claim gateway ownership: %w", err) + } + releaseCtx := context.WithoutCancel(ctx) + b.addCleanup(func() { + if err := ownership.Release(releaseCtx); err != nil { + b.log.Error("release gateway ownership", "err", err) + } + }) + return nil +} + // handleRequeue replays a parked/waiting task by id, then signals an // early success exit unless -once keeps the daemon running. func (b *boot) handleRequeue(ctx context.Context, requeue int64, once bool) (bool, error) { @@ -640,7 +664,7 @@ func (b *boot) setupLLMAndChat(ctx context.Context) error { return err } b.addCleanup(cleanup) - // Channel routers execute turns locally and need the SQLite TurnLedger. + // Channel routers execute turns locally and need the conversation store's TurnLedger. // The remote contract serves web chat; it must not replace their store. b.chat = &webui.ChatService{Contract: contract, Updates: b.updateService} b.setupReadinessProbes() diff --git a/internal/app/archied/chat_setup.go b/internal/app/archied/chat_setup.go index 6d3f86c8b..22512790d 100644 --- a/internal/app/archied/chat_setup.go +++ b/internal/app/archied/chat_setup.go @@ -145,11 +145,8 @@ func makeUpdateService(s chatSetup) *releaseupdate.Service { return updates } -func makeTelegramSessionStore(cfg config.Config) (gateway.SessionStore, error) { - return gateway.OpenSQLiteSessionStore(conversationDBPath(cfg.DBPath)) -} - -// conversationDBPath keeps conversation state in its own SQLite database. +// conversationDBPath is the legacy SQLite conversation database the one-time +// import reads. func conversationDBPath(taskDBPath string) string { return taskDBPath + "-conversations.sqlite" } diff --git a/internal/app/archied/chat_store_composition_test.go b/internal/app/archied/chat_store_composition_test.go index c3c4f48a9..b71be6779 100644 --- a/internal/app/archied/chat_store_composition_test.go +++ b/internal/app/archied/chat_store_composition_test.go @@ -7,13 +7,15 @@ import ( "github.com/samcharles93/archie-core/internal/config" "github.com/samcharles93/archie-core/internal/gateway" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" "github.com/samcharles93/archie-core/internal/store" ) func TestRemoteChatCompositionPreservesChannelTurnLedger(t *testing.T) { cfg := config.Config{ - DBPath: filepath.Join(t.TempDir(), "tasks.db"), - Services: config.Services{config.ServiceNameGateway: {Target: "127.0.0.1:1"}}, + DBPath: filepath.Join(t.TempDir(), "tasks.db"), + DatabaseURL: pgtest.URL(t), + Services: config.Services{config.ServiceNameGateway: {Target: "127.0.0.1:1"}}, } st, err := store.Open(t.Context(), filepath.Join(t.TempDir(), "state-store.db")) if err != nil { diff --git a/internal/app/archied/cutover_test.go b/internal/app/archied/cutover_test.go new file mode 100644 index 000000000..8ecab7ddf --- /dev/null +++ b/internal/app/archied/cutover_test.go @@ -0,0 +1,180 @@ +package archied + +import ( + "context" + "errors" + "os" + "path/filepath" + "strings" + "testing" + "time" + + "github.com/jackc/pgx/v5/pgxpool" + + "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/messaging" + "github.com/samcharles93/archie-core/internal/gateway" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgtest" +) + +// seedLegacyConversation writes one session into the legacy SQLite +// conversation file beside dbPath, which is enough for the cutover gate to +// see legacy data. +func seedLegacyConversation(t *testing.T, dbPath string) { + t.Helper() + legacy, err := gateway.OpenSQLiteSessionStore(conversationDBPath(dbPath)) + if err != nil { + t.Fatalf("open legacy conversation store: %v", err) + } + defer legacy.Close() + if err := legacy.Save(t.Context(), messaging.SessionContext{ + SessionID: "legacy", + Source: messaging.SessionSource{Platform: "telegram", BotUser: "archie"}, + CreatedAt: time.Now(), + }); err != nil { + t.Fatalf("seed legacy session: %v", err) + } +} + +func testPool(t *testing.T, url string) *pgxpool.Pool { + t.Helper() + pool, err := postgres.Open(t.Context(), url) + if err != nil { + t.Fatalf("open pool: %v", err) + } + t.Cleanup(pool.Close) + if err := postgres.Migrate(t.Context(), pool, postgres.Migrations()); err != nil { + t.Fatalf("migrate: %v", err) + } + return pool +} + +// TestCutoverGate pins D1.3 in both serving processes: a fresh install boots +// and records completion, legacy data without a completed import refuses with +// the operator's instruction, and a completed import boots over legacy files. +func TestCutoverGate(t *testing.T) { + processes := []struct { + name string + open func(*boot, context.Context) error + }{ + {"state store", func(b *boot, ctx context.Context) error { return b.openStateStorePool(ctx) }}, + {"gateway", func(b *boot, ctx context.Context) error { return b.openStores(ctx) }}, + } + tests := []struct { + name string + legacy bool + complete bool + wantErr string + }{ + {name: "fresh install"}, + {name: "legacy data without import", legacy: true, wantErr: "run `archie-state-store import`"}, + {name: "legacy data with completed import", legacy: true, complete: true}, + } + for _, p := range processes { + for _, tt := range tests { + t.Run(p.name+"/"+tt.name, func(t *testing.T) { + url := pgtest.URL(t) + dbPath := filepath.Join(t.TempDir(), "archie.db") + if tt.legacy { + seedLegacyConversation(t, dbPath) + } + pool := testPool(t, url) + if tt.complete { + if _, err := pool.Exec(t.Context(), + "INSERT INTO import_completion (id, sources, report) VALUES (1, 's', 'r')"); err != nil { + t.Fatalf("seed completion: %v", err) + } + } + b := newBootstrap() + b.cfg = config.Config{DatabaseURL: url, DBPath: dbPath} + err := p.open(b, t.Context()) + b.cleanup() + if tt.wantErr != "" { + if err == nil || !strings.Contains(err.Error(), tt.wantErr) { + t.Fatalf("open = %v, want refusal containing %q", err, tt.wantErr) + } + return + } + if err != nil { + t.Fatalf("open: %v", err) + } + done, err := postgres.ImportComplete(t.Context(), pool) + if err != nil { + t.Fatalf("ImportComplete: %v", err) + } + if !done { + t.Fatal("serving database has no completion record; a later legacy file would block it") + } + }) + } + } +} + +// TestFreshInstallIgnoresLaterLegacyFile pins the fresh-install record: once a +// database started with no legacy data, a stray legacy file does not block it. +func TestFreshInstallIgnoresLaterLegacyFile(t *testing.T) { + url := pgtest.URL(t) + dbPath := filepath.Join(t.TempDir(), "archie.db") + first := newBootstrap() + first.cfg = config.Config{DatabaseURL: url, DBPath: dbPath} + if err := first.openStateStorePool(t.Context()); err != nil { + t.Fatalf("fresh start: %v", err) + } + first.cleanup() + seedLegacyConversation(t, dbPath) + if _, err := os.Stat(conversationDBPath(dbPath)); err != nil { + t.Fatalf("legacy file not written: %v", err) + } + second := newBootstrap() + second.cfg = first.cfg + if err := second.openStateStorePool(t.Context()); err != nil { + t.Fatalf("restart with a stray legacy file: %v", err) + } + second.cleanup() +} + +// TestGatewayRefusesASecondOwner pins D4.1: one Gateway serves a database, a +// second refuses while the first holds its claim, and one starts again once +// the first has shut down. +func TestGatewayRefusesASecondOwner(t *testing.T) { + cfg := config.Config{DatabaseURL: pgtest.URL(t), DBPath: filepath.Join(t.TempDir(), "archie.db")} + start := func() (*boot, error) { + b := newBootstrap() + b.cfg = cfg + if err := b.openChatSessions(t.Context()); err != nil { + b.cleanup() + return nil, err + } + if err := b.claimGatewayOwnership(t.Context()); err != nil { + b.cleanup() + return nil, err + } + return b, nil + } + first, err := start() + if err != nil { + t.Fatalf("first gateway: %v", err) + } + if _, err := start(); !errors.Is(err, postgres.ErrOwned) { + first.cleanup() + t.Fatalf("second gateway = %v, want ErrOwned", err) + } + first.cleanup() + third, err := start() + if err != nil { + t.Fatalf("gateway after the owner shut down: %v", err) + } + third.cleanup() +} + +// TestConversationStoreFailsClosedWithoutURL: the conversation store is +// Postgres-only, so a missing database_url stops the process. +func TestConversationStoreFailsClosedWithoutURL(t *testing.T) { + b := newBootstrap() + defer b.cleanup() + err := b.openChatSessions(t.Context()) + if err == nil || !strings.Contains(err.Error(), "database_url is required") { + t.Fatalf("openChatSessions = %v, want it to name the missing database_url", err) + } +} diff --git a/internal/app/archied/gateway.go b/internal/app/archied/gateway.go index 7e74fb14c..d2898457c 100644 --- a/internal/app/archied/gateway.go +++ b/internal/app/archied/gateway.go @@ -50,10 +50,9 @@ func gatewayListenAndToken(b *boot, options GatewayOptions) (listen, token strin // RunGateway owns conversation persistence, model runtime and tool-provider // lifecycles. Task-store adapters access the State Store contract (remote -// *staterpc.Client via [services.state].target), not archie.db directly: the -// single SQLite file is owned by archie-state-store, and the Gateway keeps its -// own already-separate session SQLite (docs/prds/state-store-contract.md §12 -// step 8), which is out of state-store scope. +// *staterpc.Client via [services.state].target). The Gateway serves the +// conversation store from database_url and holds the Gateway serve claim for +// its whole life. func RunGateway(ctx context.Context, options GatewayOptions) error { b := newBootstrap() b.processName = applystatus.Gateway @@ -68,7 +67,6 @@ func RunGateway(ctx context.Context, options GatewayOptions) error { // The Gateway also no longer opens archie.db directly: it consumes the // same remote State Store contract the daemon does via // [services.state].target (docs/prds/state-store-contract.md §12 step 7). - // Its own session SQLite is untouched and stays Gateway-owned. if err := b.openGatewayState(ctx); err != nil { return err } @@ -127,6 +125,9 @@ func RunGateway(ctx context.Context, options GatewayOptions) error { } func (b *boot) openGatewayState(ctx context.Context) error { + if err := b.claimGatewayOwnership(ctx); err != nil { + return err + } if err := b.openStateStoreAdapter(); err != nil { return err } diff --git a/internal/app/archied/state_store.go b/internal/app/archied/state_store.go index 750c027ee..7f90bc146 100644 --- a/internal/app/archied/state_store.go +++ b/internal/app/archied/state_store.go @@ -1,7 +1,7 @@ // state_store.go composes the standalone archie-state-store process. It mirrors // gateway.go / cmd/archie-gateway: the binary is a thin main that passes -// process inputs into RunStateStore, which owns the single archie.db SQLite -// file, registers every StateStore gRPC handler (the same service .4.2 serves +// process inputs into RunStateStore, which serves the task and event-capture +// stores from Postgres, registers every StateStore gRPC handler (the same service .4.2 serves // in-process on the daemon), and shuts down cleanly on ctx cancellation. // // See docs/prds/state-store-contract.md (rev. 2c) -- the single authoritative @@ -66,11 +66,10 @@ type StateStoreOptions struct { ReadyAddr string } -// RunStateStore owns the single archie.db SQLite file, registers every -// StateStore gRPC handler, and serves until ctx is cancelled. It opens only -// the task store -- the gateway's session SQLite is owned by the separate -// archie-gateway process and is deliberately untouched (state-store-contract -// rev. 2c §12 step 8). The store service owns its own DB lifecycle, so +// RunStateStore serves the task and event-capture stores from Postgres, +// registers every StateStore gRPC handler, and serves until ctx is cancelled. +// The conversation store belongs to the separate archie-gateway process. The +// store service owns its own DB lifecycle, so // b.cleanup() is the sole owner closing b.st here (in-process owner). func RunStateStore(ctx context.Context, options StateStoreOptions) error { b := newBootstrap() @@ -186,7 +185,8 @@ func openStateStoreControlPlane(resources controlplane.ResourceStore) (*controlp // 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. +// PostgreSQL pool (fail closed), and serves the task and event-capture stores +// from it. // 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 { @@ -207,12 +207,8 @@ func (b *boot) openStateStore(ctx context.Context) error { if err := b.openStateStorePool(ctx); err != nil { return err } - if err := b.openTaskStore(ctx, taskDBPath(cfg.DBPath)); err != nil { - return err - } - if err := b.openEDAStore(cfg, bindingCipher); err != nil { - return err - } + b.openTaskStore() + b.openEDAStore(bindingCipher) return nil } @@ -249,8 +245,8 @@ func (b *boot) stateStoreDeps(grants *staterpc.TaskGrants) staterpc.Deps { if b.taskLogs != nil { deps.TaskLogs = b.taskLogs } - // The event-capture contracts are served by the PocketBase store, not the - // task store: those tables moved. BindingTaskCreator stays below on the + // The event-capture contracts are served by the event-capture store, not + // the task store. BindingTaskCreator stays below on the // task store, because creating a task is the one thing it still does. if b.eda != nil { deps.Captures = b.eda diff --git a/internal/app/archied/state_store_eda.go b/internal/app/archied/state_store_eda.go index 213651ae4..c35c51f4a 100644 --- a/internal/app/archied/state_store_eda.go +++ b/internal/app/archied/state_store_eda.go @@ -1,33 +1,24 @@ package archied import ( - "path/filepath" - - "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/domain/storecontract" "github.com/samcharles93/archie-core/internal/infrastructure/edastore" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" ) -// 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 +// eventCaptureStore is every surface the State Store serves from the +// event-capture tables, plus the tool_call projection it writes through. +type eventCaptureStore interface { + storecontract.CaptureStore + storecontract.MappingStore + storecontract.BindingStore + storecontract.BindingDispatcher + storecontract.PlaybookDispatcher + toolCallWriter +} + +// openEDAStore serves the event-capture store from the State Store's Postgres +// pool, sealing binding secrets with cipher. +func (b *boot) openEDAStore(cipher edastore.BindingCipher) { + b.eda = postgres.NewEDA(b.pg, cipher) } diff --git a/internal/app/archied/state_store_pool.go b/internal/app/archied/state_store_pool.go index e54959173..b4dc81dc4 100644 --- a/internal/app/archied/state_store_pool.go +++ b/internal/app/archied/state_store_pool.go @@ -2,40 +2,30 @@ package archied import ( "context" - "errors" + "fmt" + "strings" + "github.com/jackc/pgx/v5/pgxpool" + + "github.com/samcharles93/archie-core/internal/infrastructure/legacyimport" "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. -// -// It then claims serve ownership of the database for the process's life, so a -// second State Store against the same database refuses to start. +// PostgreSQL pool, applies the schema and claims serve ownership of the +// database for the process's life, so a second State Store against the same +// database refuses to start. A missing or invalid database_url, a migration +// failure or unimported legacy data is a startup failure. // // 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) + pool, err := openServicePool(ctx, b.cfg.DatabaseURL, b.cfg.DBPath, "the State Store") 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 - } - ownership, err := postgres.AcquireOwnership(ctx, pool, "state-store") + ownership, err := postgres.AcquireOwnership(ctx, pool, postgres.OwnerStateStore) if err != nil { pool.Close() b.log.Error("claim state store ownership", "err", err) @@ -49,3 +39,48 @@ func (b *boot) openStateStorePool(ctx context.Context) error { b.addCleanup(func() { _ = ownership.Release(releaseCtx) }) return nil } + +// openServicePool opens and migrates a serving process's pool and refuses to +// serve over legacy data the one-time import has not moved. service names the +// process in the error an operator reads. +func openServicePool(ctx context.Context, url, dbPath, service string) (*pgxpool.Pool, error) { + if url == "" { + return nil, fmt.Errorf("database_url is required: %s is Postgres-only and refuses to fall back to SQLite", 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 + } + if err := refuseUnimportedLegacy(ctx, pool, dbPath); err != nil { + pool.Close() + return nil, err + } + return pool, nil +} + +// refuseUnimportedLegacy is the cutover gate. A database with a completion +// record serves. Otherwise non-empty legacy SQLite files mean an upgrade whose +// import has not run, and serving would hide that data behind empty tables, +// so it refuses. With no legacy data it is a fresh install: the completion +// record is written so a legacy file that appears later cannot block it. +func refuseUnimportedLegacy(ctx context.Context, pool *pgxpool.Pool, dbPath string) error { + done, err := postgres.ImportComplete(ctx, pool) + if err != nil || done { + return err + } + sources := legacySources(dbPath) + has, err := legacyimport.HasLegacyData(ctx, sources) + if err != nil { + return fmt.Errorf("check for legacy SQLite data: %w", err) + } + if has { + return fmt.Errorf("legacy SQLite data found (%s) but this Postgres database has no completed import: "+ + "stop every archie process, run `archie-state-store import`, then start archie again", + strings.Join([]string{sources.StateStore, sources.EDA, sources.Gateway}, ", ")) + } + return postgres.RecordFreshInstall(ctx, pool) +} diff --git a/internal/app/archied/state_store_task.go b/internal/app/archied/state_store_task.go index b8444ef92..abc406920 100644 --- a/internal/app/archied/state_store_task.go +++ b/internal/app/archied/state_store_task.go @@ -1,39 +1,13 @@ package archied import ( - "context" - - "github.com/samcharles93/archie-core/internal/store" + "github.com/samcharles93/archie-core/internal/infrastructure/postgres" ) -// 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 +// openTaskStore serves the task store from the State Store's Postgres pool. +// Serve ownership is the pool's claim (openStateStorePool), so a second State +// Store on the same database already failed before this runs, and the pool's +// cleanup closes what this store reads through. +func (b *boot) openTaskStore() { + b.st = postgres.New(b.pg) } diff --git a/internal/app/archied/state_store_test.go b/internal/app/archied/state_store_test.go index 0d199e82d..a2bec8385 100644 --- a/internal/app/archied/state_store_test.go +++ b/internal/app/archied/state_store_test.go @@ -213,7 +213,7 @@ func TestStateStoreDataSurvivesRestart(t *testing.T) { // this would mean archie.db is opened by two processes at once. func TestOpenStoresNeverOwnsTaskDB(t *testing.T) { b := newBootstrap() - b.cfg = config.Config{DBPath: filepath.Join(t.TempDir(), "archie")} + b.cfg = config.Config{DBPath: filepath.Join(t.TempDir(), "archie"), DatabaseURL: pgtest.URL(t)} if err := b.openStores(t.Context()); err != nil { t.Fatalf("openStores: %v", err) } diff --git a/internal/app/archied/telegram_session_store_test.go b/internal/app/archied/telegram_session_store_test.go deleted file mode 100644 index 9e5d1bc92..000000000 --- a/internal/app/archied/telegram_session_store_test.go +++ /dev/null @@ -1,41 +0,0 @@ -package archied - -import ( - "bytes" - "fmt" - "os" - "path/filepath" - "testing" - - "github.com/samcharles93/archie-core/internal/config" -) - -func TestTelegramConversationStoreUsesFreshSQLiteWithoutReadingTaskLog(t *testing.T) { - taskLogPath := filepath.Join(t.TempDir(), "tasks.db") - taskLog := []byte("not a SQLite database") - if err := os.WriteFile(taskLogPath, taskLog, 0o600); err != nil { - t.Fatalf("write task log fixture: %v", err) - } - - sessions, err := makeTelegramSessionStore(config.Config{ - DBPath: taskLogPath, - }) - if err != nil { - t.Fatalf("makeTelegramSessionStore: %v", err) - } - t.Cleanup(func() { _ = sessions.Close() }) - - if got, want := fmt.Sprintf("%T", sessions), "*gateway.sqliteSessionStore"; got != want { - t.Fatalf("conversation store type = %q, want %q", got, want) - } - if _, err := os.Stat(conversationDBPath(taskLogPath)); err != nil { - t.Fatalf("stat conversation database: %v", err) - } - gotTaskLog, err := os.ReadFile(taskLogPath) - if err != nil { - t.Fatalf("read task log fixture: %v", err) - } - if !bytes.Equal(gotTaskLog, taskLog) { - t.Fatalf("task log changed during conversation-store cutover: got %q, want %q", gotTaskLog, taskLog) - } -} diff --git a/internal/infrastructure/postgres/import_completion.go b/internal/infrastructure/postgres/import_completion.go index 32071fd71..1014c796b 100644 --- a/internal/infrastructure/postgres/import_completion.go +++ b/internal/infrastructure/postgres/import_completion.go @@ -27,3 +27,13 @@ func ImportComplete(ctx context.Context, pool *pgxpool.Pool) (bool, error) { } return done, nil } + +// RecordFreshInstall writes the completion record for a database that starts +// with no legacy data to import, so a legacy file that appears later cannot +// block it from serving. It leaves an existing record untouched. +func RecordFreshInstall(ctx context.Context, pool *pgxpool.Pool) error { + if err := postgresdb.New(pool).InsertFreshInstallCompletion(ctx); err != nil { + return fmt.Errorf("postgres: fresh install completion: %w", err) + } + return nil +} diff --git a/internal/infrastructure/postgres/postgresdb/import.sql.go b/internal/infrastructure/postgres/postgresdb/import.sql.go index c36c97386..250ca5658 100644 --- a/internal/infrastructure/postgres/postgresdb/import.sql.go +++ b/internal/infrastructure/postgres/postgresdb/import.sql.go @@ -20,6 +20,16 @@ func (q *Queries) ImportCompleted(ctx context.Context) (bool, error) { return exists, err } +const insertFreshInstallCompletion = `-- name: InsertFreshInstallCompletion :exec +INSERT INTO import_completion (id, sources, report) VALUES (1, '', 'fresh install: no legacy data') +ON CONFLICT (id) DO NOTHING +` + +func (q *Queries) InsertFreshInstallCompletion(ctx context.Context) error { + _, err := q.db.Exec(ctx, insertFreshInstallCompletion) + return err +} + const insertImportCompletion = `-- name: InsertImportCompletion :exec INSERT INTO import_completion (id, sources, report) VALUES (1, $1, $2) diff --git a/internal/infrastructure/postgres/queries/import.sql b/internal/infrastructure/postgres/queries/import.sql index 0a741eaf2..e912c9106 100644 --- a/internal/infrastructure/postgres/queries/import.sql +++ b/internal/infrastructure/postgres/queries/import.sql @@ -5,3 +5,7 @@ INSERT INTO import_completion (id, sources, report) VALUES (1, $1, $2); -- name: ImportCompleted :one SELECT EXISTS (SELECT 1 FROM import_completion); + +-- name: InsertFreshInstallCompletion :exec +INSERT INTO import_completion (id, sources, report) VALUES (1, '', 'fresh install: no legacy data') +ON CONFLICT (id) DO NOTHING;