export type DatabaseFailureCode = "db_deadlock" | "db_serialization_failure" | "db_failure"; export type PersistenceRetryOutcome = "not_retryable" | "exhausted"; export type SafeDatabaseErrorFacts = { severity?: string; schema?: string; table?: string; column?: string; dataType?: string; constraint?: string; routine?: string; /** * Our own PL/pgSQL functions on the error's call chain, innermost first, as * `name:line` (for example `fence_turn:42`). This names the exact check that * raised a refusal; the raised message and SQL text are never copied. */ plpgsql?: string[]; }; export type PersistenceFailureDetails = { code: DatabaseFailureCode; sqlState: string | null; stage: string; eventTypes: string[]; correlationId: string; attempts: number; retryOutcome: PersistenceRetryOutcome; database: SafeDatabaseErrorFacts; }; /** * The objects in a possibly wrapped driver error, breadth first: the error, * then everything under its `cause`, `original`, `driverError`, `error` and * `errors` keys. Stops after 64 distinct objects. */ export declare function nestedErrorRecords(error: unknown): Generator>; /** Find the driver SQLSTATE even when Drizzle wrapped it under nested causes. */ export declare function nestedPostgresSqlState(error: unknown): string | null; /** * The message PostgreSQL raised with the SQLSTATE, even under nested causes. * Fences raise fixed, data-free reasons; callers use this to say which one * refused instead of collapsing every refusal into one generic answer. */ export declare function nestedPostgresMessage(error: unknown): string | null; /** The DETAIL PostgreSQL raised with `sqlState`, even under nested causes. */ export declare function nestedPostgresDetail(error: unknown, sqlState: string): string | null; export declare function databaseFailureCode(sqlState: string | null): DatabaseFailureCode; export declare function isRetryablePersistenceSqlState(sqlState: string | null): boolean; export declare function isSchemaChangePersistenceSqlState(sqlState: string | null): boolean; /** * Recognize only explicit postgres.js/Node transport codes, including nested * driver causes. These failures can arrive as plain Errors with no SQLSTATE or * database diagnostic fields when the connection dies before PostgreSQL can * answer. Messages are intentionally ignored: they are unstable and may * contain connection detail. */ export declare function isRetryableDatabaseTransportFailure(error: unknown): boolean; /** * Explicit marker for a dependency that hid its driver error behind its own * generic failure but is known to have failed reading the database. */ export declare class DatabaseUnavailableError extends Error { readonly code = "DATABASE_UNAVAILABLE"; constructor(message?: string, options?: { cause?: unknown; }); } /** * True when a failure means the database connection itself went away (an * operator drain, failover, restart, or socket loss), not that a statement was * rejected. Such a failure is transient: a fresh pooled connection succeeds once * the server accepts connections again. It says nothing about whether a write * in flight committed. * * Socket and postgres.js transport codes (`ECONNRESET`, `CONNECTION_CLOSED`, * ...) count only when the same failure proves it came from the database: a * query-bearing driver or ORM error, a PostgresError, or postgres.js's own * connection error. The same codes from NATS, a provider fetch, or a browser * transport are not database loss. */ export declare function isDatabaseConnectionLoss(error: unknown): boolean; /** * Distinguish database/ORM failures from expected domain exceptions when a * driver omitted SQLSTATE. This checks shape only; callers retain the original * failure independently as canonical error evidence. */ export declare function isDatabasePersistenceFailure(error: unknown): boolean; export declare function safeDatabaseErrorFacts(error: unknown): SafeDatabaseErrorFacts; /** Own-driver transaction boundary; callback errors never establish provenance. */ export declare class DatabaseTransactionError extends Error { readonly stage: "admission" | "settlement"; readonly original?: unknown; readonly name = "DatabaseTransactionError"; constructor(stage: "admission" | "settlement", cause: unknown, original?: unknown); } /** Typed persistence classification retaining the original cause internally. * Its ordinary message is stable and contains no SQL or driver parameters. */ export declare class SessionEventPersistenceError extends Error { readonly details: PersistenceFailureDetails; readonly name = "SessionEventPersistenceError"; constructor(details: PersistenceFailureDetails, cause?: unknown); get code(): DatabaseFailureCode; } export declare function isSessionEventPersistenceError(error: unknown): error is SessionEventPersistenceError; /** * A database guard refused the request on purpose, and the application has * already said what that means (for example a stale access version or a * missing grant). It keeps the database error as its cause for diagnostics, * but it is a decision, not a database failure: the persistence retry helper * rethrows it unchanged and never retries it. */ export declare class ClassifiedDatabaseRefusal extends Error { } export type IdempotentPersistenceTransactionOptions = { stage: string; eventTypes?: string[]; maxAttempts?: number; correlationId?: string; onRetry?: (input: { attempt: number; sqlState: "40P01" | "40001"; }) => void | Promise; /** Waits before retrying a schema-change SQLSTATE; tests shorten it. */ sleep?: (ms: number) => Promise; }; /** * Retry only the supplied idempotent database transaction/savepoint. Provider * inference, tools, NATS, and all other external effects must remain outside * this function. A single correlation ID follows every persistence attempt. */ export declare function runIdempotentPersistenceTransaction(options: IdempotentPersistenceTransactionOptions, transaction: (attempt: number) => Promise): Promise;