/** * PgCheckpointStore — PostgreSQL parity for the checkpoint store. * * Structural gap #16: checkpoints were PostgreSQL-only (raw legacy local database inside * CheckpointManager). This store provides PG parity so a Postgres deployment * persists task/INDEX execution state in the same backend as the rest of the * data plane (one-backend rule). * * Mirrors PgSynapseSessionStore / PgScheduledJobStore's discipline: the * ICheckpointStore contract is MOSTLY SYNCHRONOUS (create/list + AutoCheckpointer * call them with no await, matching the PostgreSQL store and legacy local database API); * `restoreCheckpoint` + `countExistingMemoryIds` are async so this store can * run a real `SELECT id FROM memories WHERE id IN (...)` on the restore path * (the single production restore caller already awaits). PG is inherently * async, so this store: * - Writes fire-and-forget (best-effort, logged on failure — matching the * PostgreSQL store's try/catch best-effort semantics). * - Reads are served from an in-memory mirror hydrated from PG on first use * (async) and kept in sync by every create/delete. The mirror is the hot * read path within a process; PG is the durability + cross-process recovery * layer (a new process hydrates its mirror from the persisted rows, so a * checkpoint created before a restart is visible after it once hydration * settles). * * Uses raw SQL ($executeRaw / $queryRaw) via the shared prisma client — the * same pattern as PgSynapseSessionStore and MemoryRepositoryPg — to avoid the * Prisma 7.7.0 + adapter-pg isObjectEnumValue incompatibility. Reuses * getPrismaClient() (no second pool). * * Schema parity: task_checkpoints (see Prisma model Checkpoint and PG migration * 20260710160000_add_task_checkpoints_pg). State is stored compressed with the * SAME algorithm as the PostgreSQL store (Bun.deflateSync on the JSON) so a row * round-trips byte-for-byte; decompression uses Bun.inflateSync. Note: the PG * store keeps a deserialized in-memory mirror (TaskCheckpoint objects), so the * compressed bytes only matter for parity with any future direct-SQL consumer. */ import { TaskCheckpoint, TaskState } from "@massa-th0th/shared"; import type { ICheckpointStore, CheckpointMetadata, ListCheckpointsOptions, CreateCheckpointOptions, CheckpointStats } from "./checkpoint-store.js"; export declare class PgCheckpointStore implements ICheckpointStore { private prisma; /** In-memory mirror: the sync read path. Hydrated from PG on first use. */ private mirror; private hydrated; private hydrating; /** * Epoch (ms) of the last failed hydration attempt. Rate-limits retries so a * persistent PG error does not turn every op into a full `SELECT *` retry * storm (hydrated stays false forever → ensureHydrated re-fires every call). */ private hydrateFailedAt; private static readonly HYDRATE_RETRY_MS; /** Per-id serialized write chain so commits land in call order. */ private inflight; private getClient; /** * Best-effort hydrate the mirror from PG. Resolves (never rejects) — failures * log a warn and leave the mirror empty; the manager can still create * checkpoints in-memory and persist them once PG is reachable. */ private ensureHydrated; private rowToCheckpoint; createCheckpoint(state: TaskState, options?: CreateCheckpointOptions): TaskCheckpoint; getCheckpoint(checkpointId: string): TaskCheckpoint | null; listCheckpoints(options?: ListCheckpointsOptions): TaskCheckpoint[]; listCheckpointsMetadata(options?: ListCheckpointsOptions): CheckpointMetadata[]; getCheckpointState(checkpointId: string): TaskState | null; getLatestCheckpoint(taskId: string): TaskCheckpoint | null; deleteCheckpoint(checkpointId: string): boolean; purgeExpired(): number; /** * Real PG check: `SELECT id FROM memories WHERE id IN (...)` via prisma, so * the restore integrity check reports genuinely missing referenced memories * (was a no-op that returned the input unchanged — silently permissive). The * id list is chunked (BATCH_SIZE per query) to stay under PG's parameter / * packet limits for large checkpoints. On any failure the method falls back to * returning the full input (best-effort, mirroring the PostgreSQL store's * try/catch) so a restore is never blocked by an unrelated query error. * * Now async: `restoreCheckpoint` was made `Promise` * (pre-mortem SF4 confirmed exactly one production caller, already-async tool * handler, Promise-based MCP/tools-api contract), so the PG store can finally * await prisma here. */ countExistingMemoryIds(memoryIds: string[]): Promise; getStats(): CheckpointStats; /** * Await mirror hydration before a read (hydration race fix, #16). * The very first read after a process restart returns empty until hydration * settles (typically <100ms). Callers that must observe persisted rows * immediately after restart await this before reading. */ ensureReady(): Promise; close(): void; /** Apply the list filters + ordering + limit/offset to a checkpoint iterable. */ private applyFilters; private checkpointToMetadata; /** * Chain a write onto any in-flight write for the same key so commits land in * call order. Different keys stay concurrent. Mirrors PgSynapseSessionStore. */ private chainWrite; /** Test helper: await in-flight writes. Not for production use. */ __drain(): Promise; /** Test helper: force hydration to complete before reading the mirror. */ __hydrate(): Promise; } //# sourceMappingURL=checkpoint-store-pg.d.ts.map