import { AsyncLocalStorage } from "node:async_hooks" import type { Pool, PoolClient, QueryResultRow } from "pg" import { assertRuntimeDatabaseActivationCurrent, pool } from "../db/index.js" import { bindSql } from "../db/sql-bind.js" import { assertDatabaseRestoreQuarantineOpen, databaseRestoreQuarantineEnabled } from "../db/database-restore-quarantine.js" import { PARTITION_WRITER_ADVISORY_KEY } from "./partition-write-transaction.js" // Separate from admission 8723104: final-flush drains precede exclusive access. export const FLOW_WRITE_LOCK_KEY = 8_723_105 export type FlowWriteKind = "ingest" | "facts" | "rebuild" type Context = { client: PoolClient; active: boolean; partitions: Set;mode:"shared"|"exclusive";authorize:()=>Promise } const context = new AsyncLocalStorage() export class FlowCommitUnknown extends Error { readonly code = "FLOW_COMMIT_UNKNOWN" } export function flowWriteContext(): Context | undefined { const current = context.getStore() if (current && !current.active) throw new Error("Flow write transaction context is closed") return current } // Only this module resolves the owned query client; the global pool is never swapped. export const flowWritePool = new Proxy(pool, { get(target, property, receiver) { if (property === "query") return (async(...args:unknown[])=>{ const current=flowWriteContext() await current?.authorize() if(current&&!current.active)throw Error("Flow write transaction context is closed") const query=(current?.client??target).query.bind(current?.client??target) as (...args:unknown[])=>Promise return query(...args) }) as Pool["query"] return Reflect.get(target,property,receiver) } }) export async function flowWriteQuery(sql:string,params?:unknown[]|Record) { return flowWritePool.query(bindSql(sql,params)) } export async function flowWriteAll(sql:string,params?:unknown[]|Record):Promise { return (await flowWriteQuery(sql,params)).rows } export async function withFlowWriteClient(client:PoolClient,work:()=>Promise,mode:"shared"|"exclusive"="shared",authorize:()=>Promise=async()=>{}):Promise { const current:Context={client,active:true,partitions:new Set(),mode,authorize} try { return await context.run(current,work) } finally { current.active=false } } /** Shared xact lease covers every SQL request, including partition DDL and final * flush. Rebuild/purge take the exclusive session lease on the same key. */ export async function commitFlowWrite>(input:{id:string;kind:FlowWriteKind;sourceProof:string;exclusive?:boolean},work:()=>Promise,authorize:()=>Promise=async()=>{},connect:()=>Promise=()=>pool.connect()):Promise { if(flowWriteContext())throw new Error("A flow write batch must own its transaction") const client=await connect();let commitSent=false,destroy=false try { await authorize();await client.query("BEGIN") await client.query("SET LOCAL lock_timeout='10s'");await client.query("SET LOCAL statement_timeout='120s'") // Enter partition admission on THIS transaction/backend before taking the // flow key or issuing any partition DML/DDL. A row trigger is too late. await client.query("SELECT pg_advisory_xact_lock_shared($1::bigint)",[PARTITION_WRITER_ADVISORY_KEY]) if (databaseRestoreQuarantineEnabled()) await assertDatabaseRestoreQuarantineOpen(client) if(input.exclusive){if((await client.query("SELECT pg_try_advisory_xact_lock($1) AS acquired",[FLOW_WRITE_LOCK_KEY])).rows[0]?.acquired!==true)throw new Error("Flow writers are busy; rebuild was not started")} else await client.query("SELECT pg_advisory_xact_lock_shared($1)",[FLOW_WRITE_LOCK_KEY]) await client.query("SELECT pg_advisory_xact_lock(hashtextextended($1,0))",["flow-write-batch:"+input.id]) await authorize() assertRuntimeDatabaseActivationCurrent() const existing=(await client.query("SELECT kind,source_proof,result FROM flow_write_receipts WHERE batch_id=$1",[input.id])).rows[0] if(existing) { if(existing.kind!==input.kind||existing.source_proof!==input.sourceProof)throw new Error("Flow receipt source does not match owned batch") await client.query("ROLLBACK");await authorize();return existing.result as T } const result=await withFlowWriteClient(client,async()=>{await authorize();return work()},input.exclusive?"exclusive":"shared",authorize) if(Buffer.byteLength(JSON.stringify(result))>60000)throw new Error("Flow write receipt exceeds bound") await authorize() await client.query("INSERT INTO flow_write_receipts(batch_id,kind,source_proof,result) VALUES($1,$2,$3,$4::jsonb)",[input.id,input.kind,input.sourceProof,JSON.stringify(result)]) await authorize() commitSent=true const committed=await client.query("COMMIT") if(committed.command!=="COMMIT")throw new FlowCommitUnknown("PostgreSQL did not confirm flow COMMIT") return result }catch(error) { if(commitSent){destroy=true;throw new FlowCommitUnknown("Flow COMMIT was dispatched without a confirmed response; do not replay")} try{await client.query("ROLLBACK")}catch{destroy=true} throw error }finally{client.release(destroy)} } /** Acquiring the SAME batch lock waits for an old COMMIT/rollback, even after * quarantining its connection. Absence before this barrier is never evidence. */ export async function reconcileFlowWrite(input:{id:string;kind:FlowWriteKind;sourceProof:string},authorize:()=>Promise=async()=>{},connect:()=>Promise=()=>pool.connect()):Promise<{committed:boolean;result:Record|null}> { const client=await connect();let locked=false,destroy=false try { await authorize();await client.query("SET lock_timeout='10s'") destroy=true;await client.query("SELECT pg_advisory_lock(hashtextextended($1,0))",["flow-write-batch:"+input.id]);locked=true;destroy=false await authorize() const row=(await client.query("SELECT kind,source_proof,result FROM flow_write_receipts WHERE batch_id=$1",[input.id])).rows[0] if(row&&(row.kind!==input.kind||row.source_proof!==input.sourceProof))throw new Error("Flow receipt proof mismatch") await authorize();return {committed:Boolean(row),result:row?.result??null} }finally { if(locked)try{const result=await client.query("SELECT pg_advisory_unlock(hashtextextended($1,0)) AS released",["flow-write-batch:"+input.id]);destroy=result.rows[0]?.released!==true}catch{destroy=true} try{await client.query("RESET lock_timeout")}catch{destroy=true} client.release(destroy) } }