Garry's Opinionated OpenClaw/Hermes Agent Brain
TypeScript
perf(persistence): index receipt compaction's candidate scan (v223)
time-attacka210cc8a0034
diff --git a/src/core/persistence/schema.ts b/src/core/persistence/schema.tsindex 67a9c04cef..20e68fc583 100644--- a/src/core/persistence/schema.ts+++ b/src/core/persistence/schema.ts@@ -43,6 +43,17 @@ export const PERSISTENCE_COMMITTED_WATERMARK_INDEX = { name: 'persistence_requesexport const PERSISTENCE_SYNC_WATERMARK_INDEX_SQL = `CREATE INDEX IF NOT EXISTS persistence_requests_sync_watermarkON persistence_requests(worktree_id,source_incarnation,completed_at DESC) WHERE state='committed' AND COALESCE(intent->>'kind','') LIKE 'managed_sync_%'`;export const PERSISTENCE_SYNC_WATERMARK_INDEX = { name: 'persistence_requests_sync_watermark', table: 'persistence_requests', sql: PERSISTENCE_SYNC_WATERMARK_INDEX_SQL } as const;+/**+ * Receipt compaction's candidate scan (journal.ts `compactWriteReceipts`, on+ * every idle maintenance tick of a resident consumer): terminal receipts not+ * yet compacted whose `completed_at` is past the retention window. Without it+ * the scan read every receipt ever written (82 ms at 50k receipts, growing with+ * each write) to find none. Compaction removes a receipt from the index.+ * Postgres builds it CONCURRENTLY in migration v223; PGLite inline.+ */+export const PERSISTENCE_COMPACTABLE_INDEX_SQL = `CREATE INDEX IF NOT EXISTS persistence_requests_compactable+ ON persistence_requests(completed_at) WHERE recovery IS NULL AND NOT compacted AND state IN ('committed','conflict','failed','cancelled')`;+export const PERSISTENCE_COMPACTABLE_INDEX = { name: 'persistence_requests_compactable', table: 'persistence_requests', sql: PERSISTENCE_COMPACTABLE_INDEX_SQL } as const;/*** #6317: one row per process that runs a full persistence consumer on a host* (consumer-heartbeat.ts). The primary key carries the process nonce, so a@@ -73,7 +84,7 @@ export const PERSISTENCE_CONSUMERS_TABLE_SQL = `CREATE TABLE IF NOT EXISTS persi)`;/** Indexes the Postgres blob omits because a migration builds them CONCURRENTLY. */export const POSTGRES_CONCURRENT_PERSISTENCE_INDEXES: ReadonlySet<string> = new Set([- PERSISTENCE_DATABASE_PENDING_INDEX_SQL, PERSISTENCE_SYNC_RUN_OPEN_INDEX_SQL, PERSISTENCE_SYNC_RUN_COMMITTED_INDEX_SQL, PERSISTENCE_SYNC_WATERMARK_INDEX_SQL,+ PERSISTENCE_DATABASE_PENDING_INDEX_SQL, PERSISTENCE_SYNC_RUN_OPEN_INDEX_SQL, PERSISTENCE_SYNC_RUN_COMMITTED_INDEX_SQL, PERSISTENCE_SYNC_WATERMARK_INDEX_SQL, PERSISTENCE_COMPACTABLE_INDEX_SQL,]);/** Durable infrastructure: never reconstruct or discard these rows during page reindexing. */export const PERSISTENCE_SCHEMA_STATEMENTS = [@@ -165,6 +176,7 @@ export const PERSISTENCE_SCHEMA_STATEMENTS = [PERSISTENCE_SYNC_RUN_OPEN_INDEX_SQL,PERSISTENCE_SYNC_RUN_COMMITTED_INDEX_SQL,PERSISTENCE_SYNC_WATERMARK_INDEX_SQL,+ PERSISTENCE_COMPACTABLE_INDEX_SQL,`CREATE INDEX IF NOT EXISTS persistence_requests_principal ON persistence_requests(principal_kind,principal_id,sequence DESC)`,`CREATE TABLE IF NOT EXISTS persistence_effects (id bigserial PRIMARY KEY,diff --git a/src/core/pglite-schema.generated.ts b/src/core/pglite-schema.generated.tsindex 65c68d88d9..0943cd0e0a 100644--- a/src/core/pglite-schema.generated.ts+++ b/src/core/pglite-schema.generated.ts@@ -1321,6 +1321,8 @@ CREATE INDEX IF NOT EXISTS persistence_requests_sync_run_committedON persistence_requests(source_id,(intent->>'runId'),(intent->>'index')) WHERE state='committed' AND intent ? 'runId';CREATE INDEX IF NOT EXISTS persistence_requests_sync_watermarkON persistence_requests(worktree_id,source_incarnation,completed_at DESC) WHERE state='committed' AND COALESCE(intent->>'kind','') LIKE 'managed_sync_%';+CREATE INDEX IF NOT EXISTS persistence_requests_compactable+ ON persistence_requests(completed_at) WHERE recovery IS NULL AND NOT compacted AND state IN ('committed','conflict','failed','cancelled');CREATE INDEX IF NOT EXISTS persistence_requests_principal ON persistence_requests(principal_kind,principal_id,sequence DESC);CREATE TABLE IF NOT EXISTS persistence_effects (id bigserial PRIMARY KEY,diff --git a/src/core/schema-migrations/registry.generated.ts b/src/core/schema-migrations/registry.generated.tsindex c12d660871..a0fd041e3d 100644--- a/src/core/schema-migrations/registry.generated.ts+++ b/src/core/schema-migrations/registry.generated.ts@@ -223,7 +223,7 @@ import { v219 } from './v219-pages-reconcile-name-indexes.ts';import { v220 } from './v220-persistence-request-preparation-attempts.ts';import { v221 } from './v221-chunk-embedding-pending-since.ts';import { v222 } from './v222-persistence-consumers.ts';-import { v223 } from './v223-persistence-sync-watermark-index.ts';+import { v223 } from './v223-persistence-serve-loop-indexes.ts';import { v224 } from './v224-page-retrievals.ts';// Array order is historical (see HISTORICAL_ARRAY_ORDER in the generator); thediff --git a/src/core/schema-migrations/v223-persistence-serve-loop-indexes.ts b/src/core/schema-migrations/v223-persistence-serve-loop-indexes.tsnew file mode 100644index 0000000000..4757bde530--- /dev/null+++ b/src/core/schema-migrations/v223-persistence-serve-loop-indexes.ts@@ -0,0 +1,38 @@+import type { Migration } from './types.ts';+import { PERSISTENCE_COMPACTABLE_INDEX, PERSISTENCE_SYNC_WATERMARK_INDEX } from '../persistence/schema.ts';+import { buildIndexOnline, migrationNotice } from './helpers.ts';++// Applied by src/core/migrate.ts on the next initSchema(); see docs/ENGINES.md+// ("Canonical schema sources") for when schema.sql or a TS fragment also changes.+//+// Two persistence_requests reads a resident `gbrain serve` repeats walked every+// receipt ever written (receipts are permanent; compaction only drops intent):+//+// - The movement watermark (persistence/sync-movement.ts, every minute per+// managed source) filtered v222's `persistence_requests_committed_watermark`+// by incarnation and intent kind, so a worktree whose committed receipts are+// not managed sync receipts was a full walk that detoasted every intent:+// 25 ms at 5k receipts, 220 ms at 50k. `persistence_requests_sync_watermark`+// keys on (worktree_id, source_incarnation, completed_at DESC) and holds only+// committed receipts of a `managed_sync_*` kind: one index probe.+// - Receipt compaction's candidate scan (journal.ts `compactWriteReceipts`,+// every idle maintenance tick) was a sequential scan: 82 ms at 50k receipts.+// `persistence_requests_compactable` holds the uncompacted terminal receipts+// by `completed_at`.+//+// Postgres builds both CONCURRENTLY, one at a time, then drops v222's index+// CONCURRENTLY (nothing else reads it); PGLite builds and drops inline. The+// watermark build detoasts each receipt's intent once: 2.3 s at 50k receipts.+// Fresh installs replay v222 on an empty table, so its build and drop cost+// nothing.+export const v223: Migration = {+ version: 223,+ name: 'persistence_serve_loop_indexes',+ idempotent: true,+ sql: '',+ handler: async engine => {+ await buildIndexOnline(engine, 223, PERSISTENCE_SYNC_WATERMARK_INDEX, { notice: migrationNotice });+ await buildIndexOnline(engine, 223, PERSISTENCE_COMPACTABLE_INDEX, { notice: migrationNotice });+ await engine.runMigration(223, `DROP INDEX ${engine.kind === 'postgres' ? 'CONCURRENTLY ' : ''}IF EXISTS persistence_requests_committed_watermark;`);+ },+};diff --git a/src/core/schema-migrations/v223-persistence-sync-watermark-index.ts b/src/core/schema-migrations/v223-persistence-sync-watermark-index.tsdeleted file mode 100644index f18cb8a052..0000000000--- a/src/core/schema-migrations/v223-persistence-sync-watermark-index.ts+++ /dev/null@@ -1,28 +0,0 @@-import type { Migration } from './types.ts';-import { PERSISTENCE_SYNC_WATERMARK_INDEX } from '../persistence/schema.ts';-import { buildIndexOnline, migrationNotice } from './helpers.ts';--// Applied by src/core/migrate.ts on the next initSchema(); see docs/ENGINES.md-// ("Canonical schema sources") for when schema.sql or a TS fragment also changes.-//-// The movement watermark read (persistence/sync-movement.ts, every minute per-// managed source in `gbrain serve`) filtered v222's-// `persistence_requests_committed_watermark` by incarnation and intent kind,-// so a worktree whose committed receipts are not managed sync receipts was a-// full walk that detoasted every intent: 115 ms at 5k receipts, 1.1 s at 50k.-// `persistence_requests_sync_watermark` keys on (worktree_id,-// source_incarnation, completed_at DESC) and holds only committed receipts of-// a `managed_sync_*` kind, so the read is one index probe. Postgres builds it-// CONCURRENTLY and then drops v222's index CONCURRENTLY (nothing else reads-// it); PGLite builds and drops inline. Fresh installs replay v222 on an empty-// table, so the build and drop there cost nothing.-export const v223: Migration = {- version: 223,- name: 'persistence_sync_watermark_index',- idempotent: true,- sql: '',- handler: async engine => {- await buildIndexOnline(engine, 223, PERSISTENCE_SYNC_WATERMARK_INDEX, { notice: migrationNotice });- await engine.runMigration(223, `DROP INDEX ${engine.kind === 'postgres' ? 'CONCURRENTLY ' : ''}IF EXISTS persistence_requests_committed_watermark;`);- },-};diff --git a/test/fixtures/goldens/catalog/pglite-engine-init-default.json b/test/fixtures/goldens/catalog/pglite-engine-init-default.jsonindex 673c2c38f2..50ace5fef0 100644--- a/test/fixtures/goldens/catalog/pglite-engine-init-default.json+++ b/test/fixtures/goldens/catalog/pglite-engine-init-default.json@@ -1054,6 +1054,7 @@"persistence_host_bindings_pkey": "CREATE UNIQUE INDEX persistence_host_bindings_pkey ON public.persistence_host_bindings USING btree (worktree_id, host_id)","persistence_local_writers_credential_hash_key": "CREATE UNIQUE INDEX persistence_local_writers_credential_hash_key ON public.persistence_local_writers USING btree (credential_hash)","persistence_local_writers_pkey": "CREATE UNIQUE INDEX persistence_local_writers_pkey ON public.persistence_local_writers USING btree (id)",+ "persistence_requests_compactable": "CREATE INDEX persistence_requests_compactable ON public.persistence_requests USING btree (completed_at) WHERE ((recovery IS NULL) AND (NOT compacted) AND (state = ANY (ARRAY['committed'::text, 'conflict'::text, 'failed'::text, 'cancelled'::text])))","persistence_requests_database_pending": "CREATE INDEX persistence_requests_database_pending ON public.persistence_requests USING btree (source_incarnation, sequence) WHERE ((worktree_id IS NULL) AND (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text])))","persistence_requests_pending": "CREATE INDEX persistence_requests_pending ON public.persistence_requests USING btree (worktree_id, sequence) WHERE (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text]))","persistence_requests_pkey": "CREATE UNIQUE INDEX persistence_requests_pkey ON public.persistence_requests USING btree (id)",diff --git a/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.json b/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.jsonindex aa645d967c..bd4b345f58 100644--- a/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.json+++ b/test/fixtures/goldens/catalog/pglite-engine-init-fts-portuguese.json@@ -1054,6 +1054,7 @@"persistence_host_bindings_pkey": "CREATE UNIQUE INDEX persistence_host_bindings_pkey ON public.persistence_host_bindings USING btree (worktree_id, host_id)","persistence_local_writers_credential_hash_key": "CREATE UNIQUE INDEX persistence_local_writers_credential_hash_key ON public.persistence_local_writers USING btree (credential_hash)","persistence_local_writers_pkey": "CREATE UNIQUE INDEX persistence_local_writers_pkey ON public.persistence_local_writers USING btree (id)",+ "persistence_requests_compactable": "CREATE INDEX persistence_requests_compactable ON public.persistence_requests USING btree (completed_at) WHERE ((recovery IS NULL) AND (NOT compacted) AND (state = ANY (ARRAY['committed'::text, 'conflict'::text, 'failed'::text, 'cancelled'::text])))","persistence_requests_database_pending": "CREATE INDEX persistence_requests_database_pending ON public.persistence_requests USING btree (source_incarnation, sequence) WHERE ((worktree_id IS NULL) AND (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text])))","persistence_requests_pending": "CREATE INDEX persistence_requests_pending ON public.persistence_requests USING btree (worktree_id, sequence) WHERE (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text]))","persistence_requests_pkey": "CREATE UNIQUE INDEX persistence_requests_pkey ON public.persistence_requests USING btree (id)",diff --git a/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.json b/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.jsonindex cd470d3ddd..3d4c62e137 100644--- a/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.json+++ b/test/fixtures/goldens/catalog/pglite-engine-init-high-dims.json@@ -1050,6 +1050,7 @@"persistence_host_bindings_pkey": "CREATE UNIQUE INDEX persistence_host_bindings_pkey ON public.persistence_host_bindings USING btree (worktree_id, host_id)","persistence_local_writers_credential_hash_key": "CREATE UNIQUE INDEX persistence_local_writers_credential_hash_key ON public.persistence_local_writers USING btree (credential_hash)","persistence_local_writers_pkey": "CREATE UNIQUE INDEX persistence_local_writers_pkey ON public.persistence_local_writers USING btree (id)",+ "persistence_requests_compactable": "CREATE INDEX persistence_requests_compactable ON public.persistence_requests USING btree (completed_at) WHERE ((recovery IS NULL) AND (NOT compacted) AND (state = ANY (ARRAY['committed'::text, 'conflict'::text, 'failed'::text, 'cancelled'::text])))","persistence_requests_database_pending": "CREATE INDEX persistence_requests_database_pending ON public.persistence_requests USING btree (source_incarnation, sequence) WHERE ((worktree_id IS NULL) AND (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text])))","persistence_requests_pending": "CREATE INDEX persistence_requests_pending ON public.persistence_requests USING btree (worktree_id, sequence) WHERE (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text]))","persistence_requests_pkey": "CREATE UNIQUE INDEX persistence_requests_pkey ON public.persistence_requests USING btree (id)",diff --git a/test/fixtures/goldens/catalog/pglite-schema-blob-default.json b/test/fixtures/goldens/catalog/pglite-schema-blob-default.jsonindex c4c0cfecde..5fefa7819c 100644--- a/test/fixtures/goldens/catalog/pglite-schema-blob-default.json+++ b/test/fixtures/goldens/catalog/pglite-schema-blob-default.json@@ -771,6 +771,7 @@"persistence_host_bindings_pkey": "CREATE UNIQUE INDEX persistence_host_bindings_pkey ON public.persistence_host_bindings USING btree (worktree_id, host_id)","persistence_local_writers_credential_hash_key": "CREATE UNIQUE INDEX persistence_local_writers_credential_hash_key ON public.persistence_local_writers USING btree (credential_hash)","persistence_local_writers_pkey": "CREATE UNIQUE INDEX persistence_local_writers_pkey ON public.persistence_local_writers USING btree (id)",+ "persistence_requests_compactable": "CREATE INDEX persistence_requests_compactable ON public.persistence_requests USING btree (completed_at) WHERE ((recovery IS NULL) AND (NOT compacted) AND (state = ANY (ARRAY['committed'::text, 'conflict'::text, 'failed'::text, 'cancelled'::text])))","persistence_requests_database_pending": "CREATE INDEX persistence_requests_database_pending ON public.persistence_requests USING btree (source_incarnation, sequence) WHERE ((worktree_id IS NULL) AND (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text])))","persistence_requests_pending": "CREATE INDEX persistence_requests_pending ON public.persistence_requests USING btree (worktree_id, sequence) WHERE (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text]))","persistence_requests_pkey": "CREATE UNIQUE INDEX persistence_requests_pkey ON public.persistence_requests USING btree (id)",diff --git a/test/fixtures/goldens/catalog/pglite-schema-blob-fts-portuguese.json b/test/fixtures/goldens/catalog/pglite-schema-blob-fts-portuguese.jsonindex d4166afe22..bd7cc9b6bb 100644--- a/test/fixtures/goldens/catalog/pglite-schema-blob-fts-portuguese.json+++ b/test/fixtures/goldens/catalog/pglite-schema-blob-fts-portuguese.json@@ -771,6 +771,7 @@"persistence_host_bindings_pkey": "CREATE UNIQUE INDEX persistence_host_bindings_pkey ON public.persistence_host_bindings USING btree (worktree_id, host_id)","persistence_local_writers_credential_hash_key": "CREATE UNIQUE INDEX persistence_local_writers_credential_hash_key ON public.persistence_local_writers USING btree (credential_hash)","persistence_local_writers_pkey": "CREATE UNIQUE INDEX persistence_local_writers_pkey ON public.persistence_local_writers USING btree (id)",+ "persistence_requests_compactable": "CREATE INDEX persistence_requests_compactable ON public.persistence_requests USING btree (completed_at) WHERE ((recovery IS NULL) AND (NOT compacted) AND (state = ANY (ARRAY['committed'::text, 'conflict'::text, 'failed'::text, 'cancelled'::text])))","persistence_requests_database_pending": "CREATE INDEX persistence_requests_database_pending ON public.persistence_requests USING btree (source_incarnation, sequence) WHERE ((worktree_id IS NULL) AND (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text])))","persistence_requests_pending": "CREATE INDEX persistence_requests_pending ON public.persistence_requests USING btree (worktree_id, sequence) WHERE (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text]))","persistence_requests_pkey": "CREATE UNIQUE INDEX persistence_requests_pkey ON public.persistence_requests USING btree (id)",diff --git a/test/fixtures/goldens/catalog/pglite-schema-blob-high-dims.json b/test/fixtures/goldens/catalog/pglite-schema-blob-high-dims.jsonindex e6841bf6fd..b2a4ecbde4 100644--- a/test/fixtures/goldens/catalog/pglite-schema-blob-high-dims.json+++ b/test/fixtures/goldens/catalog/pglite-schema-blob-high-dims.json@@ -770,6 +770,7 @@"persistence_host_bindings_pkey": "CREATE UNIQUE INDEX persistence_host_bindings_pkey ON public.persistence_host_bindings USING btree (worktree_id, host_id)","persistence_local_writers_credential_hash_key": "CREATE UNIQUE INDEX persistence_local_writers_credential_hash_key ON public.persistence_local_writers USING btree (credential_hash)","persistence_local_writers_pkey": "CREATE UNIQUE INDEX persistence_local_writers_pkey ON public.persistence_local_writers USING btree (id)",+ "persistence_requests_compactable": "CREATE INDEX persistence_requests_compactable ON public.persistence_requests USING btree (completed_at) WHERE ((recovery IS NULL) AND (NOT compacted) AND (state = ANY (ARRAY['committed'::text, 'conflict'::text, 'failed'::text, 'cancelled'::text])))","persistence_requests_database_pending": "CREATE INDEX persistence_requests_database_pending ON public.persistence_requests USING btree (source_incarnation, sequence) WHERE ((worktree_id IS NULL) AND (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text])))","persistence_requests_pending": "CREATE INDEX persistence_requests_pending ON public.persistence_requests USING btree (worktree_id, sequence) WHERE (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text]))","persistence_requests_pkey": "CREATE UNIQUE INDEX persistence_requests_pkey ON public.persistence_requests USING btree (id)",diff --git a/test/fixtures/goldens/catalog/postgres-engine-init-default.json b/test/fixtures/goldens/catalog/postgres-engine-init-default.jsonindex c2cc9b65f5..bfeec5741b 100644--- a/test/fixtures/goldens/catalog/postgres-engine-init-default.json+++ b/test/fixtures/goldens/catalog/postgres-engine-init-default.json@@ -1058,6 +1058,7 @@"persistence_host_bindings_pkey": "CREATE UNIQUE INDEX persistence_host_bindings_pkey ON public.persistence_host_bindings USING btree (worktree_id, host_id)","persistence_local_writers_credential_hash_key": "CREATE UNIQUE INDEX persistence_local_writers_credential_hash_key ON public.persistence_local_writers USING btree (credential_hash)","persistence_local_writers_pkey": "CREATE UNIQUE INDEX persistence_local_writers_pkey ON public.persistence_local_writers USING btree (id)",+ "persistence_requests_compactable": "CREATE INDEX persistence_requests_compactable ON public.persistence_requests USING btree (completed_at) WHERE ((recovery IS NULL) AND (NOT compacted) AND (state = ANY (ARRAY['committed'::text, 'conflict'::text, 'failed'::text, 'cancelled'::text])))","persistence_requests_database_pending": "CREATE INDEX persistence_requests_database_pending ON public.persistence_requests USING btree (source_incarnation, sequence) WHERE ((worktree_id IS NULL) AND (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text])))","persistence_requests_pending": "CREATE INDEX persistence_requests_pending ON public.persistence_requests USING btree (worktree_id, sequence) WHERE (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text]))","persistence_requests_pkey": "CREATE UNIQUE INDEX persistence_requests_pkey ON public.persistence_requests USING btree (id)",diff --git a/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.json b/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.jsonindex bb81352794..4da0ae2587 100644--- a/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.json+++ b/test/fixtures/goldens/catalog/postgres-engine-init-fts-portuguese.json@@ -1058,6 +1058,7 @@"persistence_host_bindings_pkey": "CREATE UNIQUE INDEX persistence_host_bindings_pkey ON public.persistence_host_bindings USING btree (worktree_id, host_id)","persistence_local_writers_credential_hash_key": "CREATE UNIQUE INDEX persistence_local_writers_credential_hash_key ON public.persistence_local_writers USING btree (credential_hash)","persistence_local_writers_pkey": "CREATE UNIQUE INDEX persistence_local_writers_pkey ON public.persistence_local_writers USING btree (id)",+ "persistence_requests_compactable": "CREATE INDEX persistence_requests_compactable ON public.persistence_requests USING btree (completed_at) WHERE ((recovery IS NULL) AND (NOT compacted) AND (state = ANY (ARRAY['committed'::text, 'conflict'::text, 'failed'::text, 'cancelled'::text])))","persistence_requests_database_pending": "CREATE INDEX persistence_requests_database_pending ON public.persistence_requests USING btree (source_incarnation, sequence) WHERE ((worktree_id IS NULL) AND (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text])))","persistence_requests_pending": "CREATE INDEX persistence_requests_pending ON public.persistence_requests USING btree (worktree_id, sequence) WHERE (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text]))","persistence_requests_pkey": "CREATE UNIQUE INDEX persistence_requests_pkey ON public.persistence_requests USING btree (id)",diff --git a/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.json b/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.jsonindex 177491044b..112cef59de 100644--- a/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.json+++ b/test/fixtures/goldens/catalog/postgres-engine-init-high-dims.json@@ -1054,6 +1054,7 @@"persistence_host_bindings_pkey": "CREATE UNIQUE INDEX persistence_host_bindings_pkey ON public.persistence_host_bindings USING btree (worktree_id, host_id)","persistence_local_writers_credential_hash_key": "CREATE UNIQUE INDEX persistence_local_writers_credential_hash_key ON public.persistence_local_writers USING btree (credential_hash)","persistence_local_writers_pkey": "CREATE UNIQUE INDEX persistence_local_writers_pkey ON public.persistence_local_writers USING btree (id)",+ "persistence_requests_compactable": "CREATE INDEX persistence_requests_compactable ON public.persistence_requests USING btree (completed_at) WHERE ((recovery IS NULL) AND (NOT compacted) AND (state = ANY (ARRAY['committed'::text, 'conflict'::text, 'failed'::text, 'cancelled'::text])))","persistence_requests_database_pending": "CREATE INDEX persistence_requests_database_pending ON public.persistence_requests USING btree (source_incarnation, sequence) WHERE ((worktree_id IS NULL) AND (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text])))","persistence_requests_pending": "CREATE INDEX persistence_requests_pending ON public.persistence_requests USING btree (worktree_id, sequence) WHERE (state = ANY (ARRAY['queued'::text, 'running'::text, 'recovering'::text]))","persistence_requests_pkey": "CREATE UNIQUE INDEX persistence_requests_pkey ON public.persistence_requests USING btree (id)",diff --git a/test/fixtures/goldens/migrations/records.json b/test/fixtures/goldens/migrations/records.jsonindex 70d018abe9..811af8ecab 100644--- a/test/fixtures/goldens/migrations/records.json+++ b/test/fixtures/goldens/migrations/records.json@@ -14,6 +14,7 @@"ISSUANCE_CLOCK_SLACK_SECONDS": "8f36c80392df6f9b9c754826289b63969d9d41ca8955213291e4413d614dce35","PAGES_RECONCILE_NAME_INDEXES": "8805e35341b22324b932b1cc05030cc8f2bfc95532bf810ec35330783f29c59a","PERSISTENCE_COMMITTED_WATERMARK_INDEX": "f472fa0acb542c1caebaaa3bc69fb2da1647e5e201239429fc02ccef0d669533",+ "PERSISTENCE_COMPACTABLE_INDEX": "25ee657878efb41ba9ea3a7624464ddcf66407d861659456c230366e57f230a0","PERSISTENCE_DATABASE_PENDING_INDEX_SQL": "6e0f618be33d037454933ea4dea11438413d45aef35d3bd4f33e89624d6e199e","PERSISTENCE_EFFECT_PARKED_INDEX_SQL": "0107863883415378d7c0dce7a0cbd5311c2c5a59fc0f81917cc8b767fd235cf2","PERSISTENCE_SYNC_RUN_INDEXES": "e1b12613c5fd2facb850e6857b0bddcf49b2478ec258fa226fe15e71e55df9c1",@@ -2232,8 +2233,8 @@"idempotent": true,"name": "durable_concurrent_persistence","sql": {- "length": 11023,- "sha256": "049756d3b614eb9f3b546693c3e7011ebc7c3f4efdd0a7961a8c4e0ab67ae1c6"+ "length": 11224,+ "sha256": "4c37c43e4946a8582cf8dea1d51764d20bc0bf1d642beb3f4842ffea02df405f"},"sqlFor": "absent","transaction": "absent",@@ -3310,16 +3311,17 @@"handler": {"form": "arrow","helpers": [+ "PERSISTENCE_COMPACTABLE_INDEX","PERSISTENCE_SYNC_WATERMARK_INDEX","buildIndexOnline","dropInvalidConcurrentIndex","migrationNotice","quietMigrationNotices"],- "sha256": "9318bd3cdc60576818636a9ac3683d75da3251820565cf61be9270300b0f560d"+ "sha256": "f7130a17b685dfd2443826ec7959856c9195334273870f1233a8fb1547c1e5b2"},"idempotent": true,- "name": "persistence_sync_watermark_index",+ "name": "persistence_serve_loop_indexes","sql": {"length": 0,"sha256": "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"diff --git a/test/helpers/serve-loop-migrations.ts b/test/helpers/serve-loop-migrations.tsindex 212ff0a26c..f5536dfbb9 100644--- a/test/helpers/serve-loop-migrations.ts+++ b/test/helpers/serve-loop-migrations.ts@@ -9,11 +9,11 @@ import { LATEST_VERSION, runMigrations } from '../../src/core/migrate.ts';import { MIGRATIONS } from '../../src/core/schema-migrations/registry.generated.ts';const INDEXES_SQL = `SELECT indexname AS name FROM pg_indexes WHERE indexname IN- ('persistence_requests_committed_watermark','persistence_requests_sync_watermark','pages_last_retrieved_at_idx') ORDER BY indexname`;+ ('persistence_requests_committed_watermark','persistence_requests_sync_watermark','persistence_requests_compactable','pages_last_retrieved_at_idx') ORDER BY indexname`;-/** A fresh install at head has the sync watermark index and page_retrievals, and neither superseded index. */+/** A fresh install at head has the sync watermark and compactable indexes and page_retrievals, and neither superseded index. */export async function assertFreshServeLoopSchema(engine: BrainEngine): Promise<void> {- expect((await engine.executeRaw<{ name: string }>(INDEXES_SQL)).map(r => r.name)).toEqual(['persistence_requests_sync_watermark']);+ expect((await engine.executeRaw<{ name: string }>(INDEXES_SQL)).map(r => r.name)).toEqual(['persistence_requests_compactable', 'persistence_requests_sync_watermark']);const [table] = await engine.executeRaw<{ present: boolean }>("SELECT to_regclass('page_retrievals') IS NOT NULL AS present");expect(table?.present).toBe(true);const triggers = await engine.executeRaw<{ name: string }>("SELECT tgname AS name FROM pg_trigger WHERE tgrelid='pages'::regclass AND tgname='pages_forget_retrievals'");@@ -27,7 +27,7 @@ export async function assertFreshServeLoopSchema(engine: BrainEngine): Promise<v* column's values alone; a second run changes nothing.*/export async function assertUpgradedServeLoopSchema(engine: BrainEngine): Promise<void> {- const v223 = MIGRATIONS.find(m => m.name === 'persistence_sync_watermark_index')!;+ const v223 = MIGRATIONS.find(m => m.name === 'persistence_serve_loop_indexes')!;expect(MIGRATIONS.find(m => m.name === 'page_retrievals')!.version).toBe(v223.version + 1);await engine.executeRaw('DELETE FROM pages');const ids = (await engine.executeRaw<{ id: number }>(`INSERT INTO pages (slug, source_id, type, title, compiled_truth, timeline, last_retrieved_at)@@ -37,6 +37,7 @@ export async function assertUpgradedServeLoopSchema(engine: BrainEngine): Promisawait engine.executeRaw('DROP FUNCTION IF EXISTS gbrain_forget_page_retrievals()');await engine.executeRaw('DROP TABLE IF EXISTS page_retrievals');await engine.executeRaw('DROP INDEX IF EXISTS persistence_requests_sync_watermark');+ await engine.executeRaw('DROP INDEX IF EXISTS persistence_requests_compactable');await engine.executeRaw(`CREATE INDEX persistence_requests_committed_watermark ON persistence_requests(worktree_id,completed_at DESC) WHERE state='committed'`);await engine.executeRaw('CREATE INDEX pages_last_retrieved_at_idx ON pages (last_retrieved_at)');expect((await engine.executeRaw<{ name: string }>(INDEXES_SQL)).map(r => r.name)).toEqual(['pages_last_retrieved_at_idx', 'persistence_requests_committed_watermark']);