@opengeni/db 1.1.0 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (77) hide show
  1. package/dist/canonical-human-identities.js +3 -3
  2. package/dist/capability-reconciliation.js +2 -2
  3. package/dist/{chunk-J4WFOVBV.js → chunk-3GAR5UN3.js} +2 -2
  4. package/dist/{chunk-Z5HJXH3X.js → chunk-3TGYFFZR.js} +2 -2
  5. package/dist/{chunk-GLA5YCQS.js → chunk-6EIEGHPK.js} +4 -4
  6. package/dist/{chunk-BLUQG54M.js → chunk-CONWNUSL.js} +2 -2
  7. package/dist/{chunk-EKXCF3FH.js → chunk-CYP72JTQ.js} +11 -4
  8. package/dist/{chunk-EKXCF3FH.js.map → chunk-CYP72JTQ.js.map} +1 -1
  9. package/dist/{chunk-VOB6TXSJ.js → chunk-FWQNHNOU.js} +39 -2
  10. package/dist/chunk-FWQNHNOU.js.map +1 -0
  11. package/dist/{chunk-7Z7ALFRE.js → chunk-GDN5TSZP.js} +2 -2
  12. package/dist/{chunk-M3SWH4MU.js → chunk-JICUP7ZR.js} +2 -2
  13. package/dist/{chunk-KJVOBLFC.js → chunk-JLVWBIEM.js} +3 -3
  14. package/dist/{chunk-BZV5ZDRY.js → chunk-LXPFNG4L.js} +2 -2
  15. package/dist/{chunk-SKYRM7R7.js → chunk-O2Y5E5M3.js} +68 -32
  16. package/dist/chunk-O2Y5E5M3.js.map +1 -0
  17. package/dist/{chunk-M2YGXCCQ.js → chunk-QFJNEXFL.js} +2 -2
  18. package/dist/{chunk-CIGHXFPD.js → chunk-UCIMHNII.js} +101 -21
  19. package/dist/chunk-UCIMHNII.js.map +1 -0
  20. package/dist/{chunk-ODKYBA3C.js → chunk-VDPCIFKL.js} +5 -5
  21. package/dist/{chunk-T5NHDEZW.js → chunk-YOYILDLY.js} +26 -1
  22. package/dist/chunk-YOYILDLY.js.map +1 -0
  23. package/dist/connect-attempts.js +3 -3
  24. package/dist/credit-balances.d.ts +18 -0
  25. package/dist/credit-promotion-policy.d.ts +10 -0
  26. package/dist/editable-artifact-durable-export.js +2 -2
  27. package/dist/editable-artifacts.js +3 -3
  28. package/dist/index.d.ts +21 -5
  29. package/dist/index.js +2369 -2247
  30. package/dist/index.js.map +1 -1
  31. package/dist/managed-auth-session-sets.js +3 -3
  32. package/dist/managed-user-setup.d.ts +1 -0
  33. package/dist/mcp-operations.js +2 -2
  34. package/dist/organization-integration-policy.js +3 -3
  35. package/dist/organization-membership-lifecycle.d.ts +1 -0
  36. package/dist/persistence-errors.d.ts +9 -6
  37. package/dist/provision-roles.js +1 -1
  38. package/dist/retained-provider-commands.js +6 -6
  39. package/dist/runtime-posture.d.ts +4 -4
  40. package/dist/schema.d.ts +121 -2
  41. package/dist/schema.js +3 -1
  42. package/dist/session-background-commands.js +4 -4
  43. package/dist/session-command-output.js +5 -5
  44. package/dist/session-event-slices.js +3 -3
  45. package/dist/session-mcp-credential-rotation.js +5 -5
  46. package/dist/session-tenancy.js +3 -3
  47. package/dist/video-generation.js +3 -3
  48. package/dist/workspace-tool-defaults.js +4 -4
  49. package/drizzle/0613_model_scoped_promotional_credits.sql +238 -0
  50. package/drizzle/0614_quiescence_command_containment.sql +7 -0
  51. package/drizzle/0615_credit_promotion_policy_validation.sql +63 -0
  52. package/package.json +7 -7
  53. package/src/credit-balances.ts +109 -0
  54. package/src/credit-promotion-policy.ts +12 -0
  55. package/src/database.ts +81 -42
  56. package/src/index.ts +201 -60
  57. package/src/managed-user-setup.ts +8 -0
  58. package/src/organization-membership-lifecycle.ts +8 -0
  59. package/src/persistence-errors.ts +17 -6
  60. package/src/provision-roles.ts +9 -0
  61. package/src/runtime-posture.ts +22 -0
  62. package/src/schema.ts +40 -1
  63. package/src/video-generation.ts +2 -7
  64. package/dist/chunk-CIGHXFPD.js.map +0 -1
  65. package/dist/chunk-SKYRM7R7.js.map +0 -1
  66. package/dist/chunk-T5NHDEZW.js.map +0 -1
  67. package/dist/chunk-VOB6TXSJ.js.map +0 -1
  68. /package/dist/{chunk-J4WFOVBV.js.map → chunk-3GAR5UN3.js.map} +0 -0
  69. /package/dist/{chunk-Z5HJXH3X.js.map → chunk-3TGYFFZR.js.map} +0 -0
  70. /package/dist/{chunk-GLA5YCQS.js.map → chunk-6EIEGHPK.js.map} +0 -0
  71. /package/dist/{chunk-BLUQG54M.js.map → chunk-CONWNUSL.js.map} +0 -0
  72. /package/dist/{chunk-7Z7ALFRE.js.map → chunk-GDN5TSZP.js.map} +0 -0
  73. /package/dist/{chunk-M3SWH4MU.js.map → chunk-JICUP7ZR.js.map} +0 -0
  74. /package/dist/{chunk-KJVOBLFC.js.map → chunk-JLVWBIEM.js.map} +0 -0
  75. /package/dist/{chunk-BZV5ZDRY.js.map → chunk-LXPFNG4L.js.map} +0 -0
  76. /package/dist/{chunk-M2YGXCCQ.js.map → chunk-QFJNEXFL.js.map} +0 -0
  77. /package/dist/{chunk-ODKYBA3C.js.map → chunk-VDPCIFKL.js.map} +0 -0
@@ -17,6 +17,7 @@ import { rawRows, withRlsContext, withWorkspaceRls } from "./database";
17
17
  import * as schema from "./schema";
18
18
  import { checkWorkspaceAllowance } from "./usage-allowances";
19
19
  import { creditDebitAttributionForTurn } from "./credit-debit-attribution";
20
+ import { getSpendableCreditBalance } from "./credit-balances";
20
21
 
21
22
  export const ACTIVE_VIDEO_GENERATION_STATUSES = [
22
23
  "preparing",
@@ -1381,13 +1382,7 @@ async function debitVideoGenerationCredits(
1381
1382
  subjectId: attribution.kind === "turn" ? attribution.initiatingHumanSubjectId : null,
1382
1383
  });
1383
1384
  if (refusal) throw Object.assign(new VideoGenerationCreditError(refusal.message), refusal);
1384
- const [balanceRow] = await tx
1385
- .select({
1386
- balanceMicros: sql<number>`coalesce(sum(${schema.creditLedgerEntries.amountMicros}), 0)`,
1387
- })
1388
- .from(schema.creditLedgerEntries)
1389
- .where(eq(schema.creditLedgerEntries.accountId, operation.accountId));
1390
- const balanceMicros = Number(balanceRow?.balanceMicros ?? 0);
1385
+ const { balanceMicros } = await getSpendableCreditBalance(tx, operation.accountId);
1391
1386
  if (!Number.isSafeInteger(balanceMicros) || balanceMicros < operation.pricedCostMicros) {
1392
1387
  throw new VideoGenerationCreditError();
1393
1388
  }
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/video-generation.ts","../src/usage-allowances.ts","../src/credit-debit-attribution.ts"],"sourcesContent":["import {\n GENERATED_VIDEO_MAX_BYTES,\n GeneratedVideoReceipt,\n GROK_IMAGINE_VIDEO_1_5_MODEL_ID,\n SEEDANCE_2_5_MODEL_ID,\n VideoGenerationPolicy,\n VideoGenerationOperationSummary,\n retainedGeneratedVideoReferenceFromFile,\n type GeneratedVideoFacts,\n type MediaGenerationResult,\n type VideoGenerationFundingSource,\n type VideoGenerationPolicy as VideoGenerationPolicyValue,\n} from \"@opengeni/contracts\";\nimport { and, eq, inArray, sql } from \"drizzle-orm\";\nimport type { Database } from \"./database\";\nimport { rawRows, withRlsContext, withWorkspaceRls } from \"./database\";\nimport * as schema from \"./schema\";\nimport { checkWorkspaceAllowance } from \"./usage-allowances\";\nimport { creditDebitAttributionForTurn } from \"./credit-debit-attribution\";\n\nexport const ACTIVE_VIDEO_GENERATION_STATUSES = [\n \"preparing\",\n \"prepared\",\n \"accepted\",\n \"submission_uncertain\",\n \"provider_started\",\n \"retaining\",\n] as const;\n\nexport type VideoGenerationOperation = typeof schema.videoGenerationOperations.$inferSelect;\nexport type VideoGenerationReference = typeof schema.videoGenerationReferences.$inferSelect;\nexport type GeneratedVideoArtifact = typeof schema.generatedVideoArtifacts.$inferSelect;\n\nexport type VideoGenerationOperationWithReferences = VideoGenerationOperation & {\n references: VideoGenerationReference[];\n};\n\nexport class VideoGenerationConflictError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"VideoGenerationConflictError\";\n }\n}\n\nexport class VideoGenerationCapacityError extends Error {\n constructor(message: string) {\n super(message);\n this.name = \"VideoGenerationCapacityError\";\n }\n}\n\nexport class VideoGenerationCreditError extends Error {\n constructor(message = \"insufficient Opengeni credits\") {\n super(message);\n this.name = \"VideoGenerationCreditError\";\n }\n}\n\nexport async function getWorkspaceVideoGenerationPolicy(\n db: Database,\n workspaceId: string,\n): Promise<VideoGenerationPolicyValue> {\n return await withWorkspaceRls(db, workspaceId, async (scopedDb) => {\n const [row] = await scopedDb\n .select()\n .from(schema.workspaceVideoGenerationPolicies)\n .where(eq(schema.workspaceVideoGenerationPolicies.workspaceId, workspaceId))\n .limit(1);\n return policyFromRow(row);\n });\n}\n\nexport async function updateWorkspaceVideoGenerationPolicy(\n db: Database,\n input: {\n accountId: string;\n workspaceId: string;\n subjectId: string;\n expectedRevision: number;\n fundingSource: VideoGenerationFundingSource;\n enabledModelIds: string[];\n defaultModelId: string | null;\n },\n): Promise<VideoGenerationPolicyValue> {\n const requested = VideoGenerationPolicy.parse({\n schemaVersion: 1,\n revision: input.expectedRevision + 1,\n fundingSource: input.fundingSource,\n enabledModelIds: input.enabledModelIds,\n defaultModelId: input.defaultModelId,\n });\n assertSupportedModels(requested.enabledModelIds);\n return await withRlsContext(\n db,\n { accountId: input.accountId, workspaceId: input.workspaceId },\n async (scopedDb) =>\n await scopedDb.transaction(async (tx) => {\n const [workspace] = await tx\n .select({ id: schema.workspaces.id })\n .from(schema.workspaces)\n .where(\n and(\n eq(schema.workspaces.id, input.workspaceId),\n eq(schema.workspaces.accountId, input.accountId),\n ),\n )\n .for(\"update\")\n .limit(1);\n if (!workspace) throw new Error(\"Workspace not found\");\n const [current] = await tx\n .select()\n .from(schema.workspaceVideoGenerationPolicies)\n .where(eq(schema.workspaceVideoGenerationPolicies.workspaceId, input.workspaceId))\n .for(\"update\")\n .limit(1);\n const currentRevision = current?.revision ?? 0;\n if (currentRevision !== input.expectedRevision) {\n throw new VideoGenerationConflictError(\"Video generation policy changed\");\n }\n const now = new Date();\n const values = {\n workspaceId: input.workspaceId,\n accountId: input.accountId,\n revision: currentRevision + 1,\n fundingSource: requested.fundingSource,\n enabledModelIds: [...requested.enabledModelIds],\n defaultModelId: requested.defaultModelId,\n updatedBySubjectId: input.subjectId.slice(0, 1_024),\n updatedAt: now,\n };\n const [row] = current\n ? await tx\n .update(schema.workspaceVideoGenerationPolicies)\n .set(values)\n .where(eq(schema.workspaceVideoGenerationPolicies.workspaceId, input.workspaceId))\n .returning()\n : await tx.insert(schema.workspaceVideoGenerationPolicies).values(values).returning();\n if (!row) throw new Error(\"Video generation policy update was not persisted\");\n return policyFromRow(row);\n }),\n );\n}\n\nexport type AdmitVideoGenerationOperationInput = {\n id: string;\n accountId: string;\n workspaceId: string;\n sessionId: string;\n turnId: string;\n attemptId: string;\n toolCallId: string;\n admissionKey: string;\n requestDigest: string;\n promptDigest: string;\n requestEncrypted: string;\n modelId: string;\n sourceMode: string;\n capabilityRevision: string;\n policyRevision: number;\n fundingSource: VideoGenerationFundingSource;\n pricedCostMicros: number;\n connectionId: string | null;\n credentialVersion: number;\n credentialEncrypted: string;\n providerIdempotencyKey: string;\n expectedArtifactId: string;\n expectedFileId: string;\n reservedBytes?: number;\n workspaceQuotaBytes: number;\n maxConcurrentPerWorkspace: number;\n recoveryDeadlineAt: Date;\n references: Array<{\n ordinal: number;\n role: string;\n contentType: string;\n sizeBytes: number;\n sha256: string;\n stagingObjectKey: string;\n grantExpiresAt?: Date | null;\n cleanupAfter: Date;\n }>;\n};\n\nexport async function admitVideoGenerationOperation(\n db: Database,\n input: AdmitVideoGenerationOperationInput,\n): Promise<{ operation: VideoGenerationOperation; created: boolean }> {\n const reservedBytes = input.reservedBytes ?? GENERATED_VIDEO_MAX_BYTES;\n return await withRlsContext(\n db,\n { accountId: input.accountId, workspaceId: input.workspaceId },\n async (scopedDb) =>\n await scopedDb.transaction(async (tx) => {\n await tx.execute(\n sql`select pg_advisory_xact_lock(hashtextextended(${`video-generation:${input.admissionKey}`}, 0))`,\n );\n const [existing] = await tx\n .select()\n .from(schema.videoGenerationOperations)\n .where(\n and(\n eq(schema.videoGenerationOperations.workspaceId, input.workspaceId),\n eq(schema.videoGenerationOperations.admissionKey, input.admissionKey),\n ),\n )\n .limit(1);\n if (existing) {\n assertAdmissionMatches(existing, input);\n return { operation: existing, created: false };\n }\n const [policy] = await tx\n .select()\n .from(schema.workspaceVideoGenerationPolicies)\n .where(eq(schema.workspaceVideoGenerationPolicies.workspaceId, input.workspaceId))\n .for(\"update\")\n .limit(1);\n if (\n !policy ||\n policy.revision !== input.policyRevision ||\n policy.fundingSource !== input.fundingSource ||\n !policy.enabledModelIds.includes(input.modelId)\n ) {\n throw new VideoGenerationConflictError(\"Video generation capability changed\");\n }\n const active = await tx\n .select({ id: schema.videoGenerationOperations.id })\n .from(schema.videoGenerationOperations)\n .where(\n and(\n eq(schema.videoGenerationOperations.workspaceId, input.workspaceId),\n inArray(schema.videoGenerationOperations.status, [\n ...ACTIVE_VIDEO_GENERATION_STATUSES,\n ]),\n ),\n )\n .for(\"update\");\n if (active.length >= input.maxConcurrentPerWorkspace) {\n throw new VideoGenerationCapacityError(\"Workspace video generation limit reached\");\n }\n await tx\n .insert(schema.workspaceVideoGenerationQuotas)\n .values({\n workspaceId: input.workspaceId,\n accountId: input.accountId,\n })\n .onConflictDoNothing({\n target: schema.workspaceVideoGenerationQuotas.workspaceId,\n });\n const [quota] = await tx\n .select()\n .from(schema.workspaceVideoGenerationQuotas)\n .where(eq(schema.workspaceVideoGenerationQuotas.workspaceId, input.workspaceId))\n .for(\"update\")\n .limit(1);\n if (!quota) throw new Error(\"Video generation quota row disappeared\");\n if (quota.reservedBytes + quota.readyBytes + reservedBytes > input.workspaceQuotaBytes) {\n throw new VideoGenerationCapacityError(\"Workspace video storage quota reached\");\n }\n const [operation] = await tx\n .insert(schema.videoGenerationOperations)\n .values({\n id: input.id,\n accountId: input.accountId,\n workspaceId: input.workspaceId,\n sessionId: input.sessionId,\n turnId: input.turnId,\n attemptId: input.attemptId,\n toolCallId: input.toolCallId,\n admissionKey: input.admissionKey,\n requestDigest: input.requestDigest,\n promptDigest: input.promptDigest,\n requestEncrypted: input.requestEncrypted,\n modelId: input.modelId,\n sourceMode: input.sourceMode,\n capabilityRevision: input.capabilityRevision,\n fundingSource: input.fundingSource,\n pricedCostMicros: input.pricedCostMicros,\n creditState: input.pricedCostMicros > 0 ? \"debited\" : \"not_applicable\",\n connectionId: input.connectionId,\n credentialVersion: input.credentialVersion,\n credentialEncrypted: input.credentialEncrypted,\n providerIdempotencyKey: input.providerIdempotencyKey,\n expectedArtifactId: input.expectedArtifactId,\n expectedFileId: input.expectedFileId,\n reservedBytes,\n recoveryDeadlineAt: input.recoveryDeadlineAt,\n status: input.references.length === 0 ? \"prepared\" : \"preparing\",\n admissionOutputState: \"pending\",\n terminalUpdateState: \"ineligible\",\n })\n .returning();\n if (!operation) throw new Error(\"Video generation operation was not admitted\");\n if (input.pricedCostMicros > 0) {\n if (input.fundingSource !== \"opengeni_credits\" || input.connectionId !== null) {\n throw new Error(\"Only Opengeni-funded video operations may debit credits\");\n }\n await debitVideoGenerationCredits(tx, operation);\n }\n if (input.references.length > 0) {\n await tx.insert(schema.videoGenerationReferences).values(\n input.references.map((reference) => ({\n operationId: operation.id,\n workspaceId: input.workspaceId,\n accountId: input.accountId,\n grantExpiresAt: reference.grantExpiresAt ?? null,\n ...reference,\n })),\n );\n }\n await tx\n .update(schema.workspaceVideoGenerationQuotas)\n .set({\n reservedBytes: sql`${schema.workspaceVideoGenerationQuotas.reservedBytes} + ${reservedBytes}`,\n updatedAt: new Date(),\n })\n .where(eq(schema.workspaceVideoGenerationQuotas.workspaceId, input.workspaceId));\n return { operation, created: true };\n }),\n );\n}\n\n/** Persist a rotated provider token before it is used for another remote call. */\nexport async function rotateVideoGenerationCredential(\n db: Database,\n input: {\n accountId: string;\n workspaceId: string;\n operationId: string;\n requestDigest: string;\n expectedCredentialEncrypted: string;\n credentialEncrypted: string;\n },\n): Promise<VideoGenerationOperation> {\n const [row] = await db\n .update(schema.videoGenerationOperations)\n .set({\n credentialEncrypted: input.credentialEncrypted,\n updatedAt: new Date(),\n })\n .where(\n and(\n eq(schema.videoGenerationOperations.accountId, input.accountId),\n eq(schema.videoGenerationOperations.workspaceId, input.workspaceId),\n eq(schema.videoGenerationOperations.id, input.operationId),\n eq(schema.videoGenerationOperations.requestDigest, input.requestDigest),\n eq(schema.videoGenerationOperations.credentialEncrypted, input.expectedCredentialEncrypted),\n inArray(schema.videoGenerationOperations.status, [\n \"accepted\",\n \"submission_uncertain\",\n \"provider_started\",\n \"retaining\",\n ]),\n ),\n )\n .returning();\n if (!row) throw new VideoGenerationConflictError(\"Video provider credential changed\");\n return row;\n}\n\n/**\n * Seal a reference-bearing operation only after every immutable staging object\n * has been uploaded and independently verified by the worker. The source\n * sandbox path is intentionally not stored, so a failed preparation can only\n * be retried by the same logical tool call while its sandbox still exists.\n */\nexport async function markVideoGenerationPrepared(\n db: Database,\n input: OperationMutationInput,\n): Promise<VideoGenerationOperation> {\n return await mutateOperation(db, input, async (tx, operation) => {\n if (operation.status === \"prepared\") return operation;\n if (operation.status !== \"preparing\") throw invalidTransition(operation, \"prepared\");\n const references = await tx\n .select({\n stagingObjectKey: schema.videoGenerationReferences.stagingObjectKey,\n cleanedAt: schema.videoGenerationReferences.cleanedAt,\n })\n .from(schema.videoGenerationReferences)\n .where(eq(schema.videoGenerationReferences.operationId, operation.id));\n if (\n references.length === 0 ||\n references.some((reference) => !reference.stagingObjectKey || reference.cleanedAt)\n ) {\n throw new VideoGenerationConflictError(\"Video references are not sealed\");\n }\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({ status: \"prepared\", updatedAt: new Date() })\n .where(eq(schema.videoGenerationOperations.id, operation.id))\n .returning();\n return requireRow(updated, \"seal video generation references\");\n });\n}\n\n/** Final authorization fence. Call only after the accepted function output is durable. */\nexport async function markVideoGenerationAccepted(\n db: Database,\n input: {\n accountId: string;\n workspaceId: string;\n operationId: string;\n requestDigest: string;\n },\n): Promise<VideoGenerationOperation> {\n return await mutateOperation(\n db,\n input,\n async (tx, operation) =>\n await markVideoGenerationAcceptedInTransaction(tx, {\n workspaceId: input.workspaceId,\n operation,\n }),\n );\n}\n\n/**\n * Transactional half of acceptance. The pending function result and this\n * transition must commit together: the provider reconciler never observes an\n * accepted operation whose model-visible `accepted` receipt was lost.\n */\nexport async function markVideoGenerationAcceptedInTransaction(\n tx: Database,\n input: {\n workspaceId: string;\n operation: VideoGenerationOperation;\n toolCallId?: string;\n },\n): Promise<VideoGenerationOperation> {\n const operation = input.operation;\n if (input.toolCallId !== undefined && operation.toolCallId !== input.toolCallId) {\n throw new VideoGenerationConflictError(\"Video generation tool-call identity changed\");\n }\n if (operation.admissionOutputState === \"recorded\") return operation;\n if (operation.status !== \"prepared\") {\n throw new VideoGenerationConflictError(\"Video generation is no longer awaiting acceptance\");\n }\n const [policy] = await tx\n .select()\n .from(schema.workspaceVideoGenerationPolicies)\n .where(eq(schema.workspaceVideoGenerationPolicies.workspaceId, input.workspaceId))\n .for(\"share\")\n .limit(1);\n const [connection] = operation.connectionId\n ? await tx\n .select({\n id: schema.connections.id,\n version: schema.connections.version,\n status: schema.connections.status,\n metadata: schema.connections.metadata,\n })\n .from(schema.connections)\n .where(\n and(\n eq(schema.connections.workspaceId, input.workspaceId),\n eq(schema.connections.id, operation.connectionId),\n ),\n )\n .for(\"share\")\n .limit(1)\n : [];\n const workspaceGatewayAuthorized =\n operation.fundingSource === \"workspace_gateway\" &&\n connection?.status === \"active\" &&\n connection.version === operation.credentialVersion &&\n connection.metadata.credentialRole === \"vercel_ai_gateway\";\n const managedAuthorized =\n operation.fundingSource === \"opengeni_credits\" &&\n operation.connectionId === null &&\n (operation.creditState === \"debited\" ||\n (operation.creditState === \"not_applicable\" && operation.pricedCostMicros === 0));\n const supergrokAuthorized =\n operation.fundingSource === \"supergrok_subscription\" &&\n operation.connectionId === null &&\n operation.credentialEncrypted !== null &&\n operation.pricedCostMicros === 0 &&\n operation.creditState === \"not_applicable\";\n if (\n !policy ||\n policy.fundingSource !== operation.fundingSource ||\n !policy.enabledModelIds.includes(operation.modelId) ||\n (!workspaceGatewayAuthorized && !managedAuthorized && !supergrokAuthorized)\n ) {\n throw new VideoGenerationConflictError(\"Video generation authorization changed\");\n }\n const now = new Date();\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({\n status: \"accepted\",\n admissionOutputState: \"recorded\",\n terminalUpdateState: \"pending\",\n nextReconcileAt: now,\n updatedAt: now,\n })\n .where(eq(schema.videoGenerationOperations.id, operation.id))\n .returning();\n return requireRow(updated, \"record video generation acceptance\");\n}\n\nexport async function cancelVideoGenerationBeforeSubmit(\n db: Database,\n input: {\n accountId: string;\n workspaceId: string;\n operationId: string;\n requestDigest: string;\n reason: string;\n },\n): Promise<VideoGenerationOperation> {\n return await mutateOperation(db, input, async (tx, operation) => {\n if (operation.status === \"cancelled_before_submit\") return operation;\n if (!inPreSubmitState(operation.status) || operation.providerRequestSentAt) {\n throw new VideoGenerationConflictError(\n \"Video generation may already have reached the provider\",\n );\n }\n const now = new Date();\n await releaseReservedQuota(tx, operation, false);\n const creditState = await refundVideoGenerationCredits(tx, operation, now);\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({\n status: \"cancelled_before_submit\",\n creditState,\n quotaState: \"released\",\n terminalAt: now,\n terminalUpdateState:\n operation.admissionOutputState === \"recorded\" ? \"pending\" : \"suppressed\",\n credentialEncrypted: null,\n requestEncrypted: null,\n boundedPublicReason: boundPublicReason(input.reason),\n updatedAt: now,\n })\n .where(eq(schema.videoGenerationOperations.id, operation.id))\n .returning();\n if (!updated) throw new Error(\"Video generation cancellation was not recorded\");\n return updated;\n });\n}\n\n/**\n * Turn-interruption half of pre-submit cancellation. It runs under the same\n * session/turn transaction that closes unresolved tool calls, so an accepted\n * receipt can never race a cancellation of its operation.\n */\nexport async function cancelUnacceptedVideoGenerationsForToolCallsInTransaction(\n tx: Database,\n input: {\n workspaceId: string;\n sessionId: string;\n turnId: string;\n toolCallIds: readonly string[];\n reason: string;\n now: Date;\n },\n): Promise<string[]> {\n const toolCallIds = [...new Set(input.toolCallIds)];\n if (toolCallIds.length === 0) return [];\n const operations = await tx\n .select()\n .from(schema.videoGenerationOperations)\n .where(\n and(\n eq(schema.videoGenerationOperations.workspaceId, input.workspaceId),\n eq(schema.videoGenerationOperations.sessionId, input.sessionId),\n eq(schema.videoGenerationOperations.turnId, input.turnId),\n inArray(schema.videoGenerationOperations.toolCallId, toolCallIds),\n eq(schema.videoGenerationOperations.admissionOutputState, \"pending\"),\n inArray(schema.videoGenerationOperations.status, [\"preparing\", \"prepared\"]),\n sql`${schema.videoGenerationOperations.providerRequestSentAt} is null`,\n ),\n )\n .orderBy(schema.videoGenerationOperations.id)\n .for(\"update\");\n const cancelled: string[] = [];\n for (const operation of operations) {\n await releaseReservedQuota(tx, operation, false);\n const creditState = await refundVideoGenerationCredits(tx, operation, input.now);\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({\n status: \"cancelled_before_submit\",\n creditState,\n quotaState: \"released\",\n terminalAt: input.now,\n terminalUpdateState: \"suppressed\",\n nextReconcileAt: input.now,\n providerRequestEncrypted: null,\n providerRequestExpiresAt: null,\n credentialEncrypted: null,\n requestEncrypted: null,\n privateDataEraseAfter: input.now,\n boundedPublicReason: boundPublicReason(input.reason),\n reconcileLeaseOwner: null,\n reconcileLeaseExpiresAt: null,\n updatedAt: input.now,\n })\n .where(eq(schema.videoGenerationOperations.id, operation.id))\n .returning({ id: schema.videoGenerationOperations.id });\n if (!updated) throw new Error(\"Interrupted video generation was not cancelled\");\n await tx\n .update(schema.videoGenerationReferences)\n .set({ cleanupAfter: input.now })\n .where(eq(schema.videoGenerationReferences.operationId, operation.id));\n cancelled.push(updated.id);\n }\n return cancelled;\n}\n\nexport async function claimVideoGenerationOperations(\n db: Database,\n input: { owner: string; leaseSeconds: number; limit: number },\n): Promise<Array<{ operationId: string; accountId: string; workspaceId: string }>> {\n const rows = await rawRows<{\n operation_id: string;\n account_id: string;\n workspace_id: string;\n }>(\n db,\n sql`select * from opengeni_private.claim_video_generation_operations(\n ${input.owner}, ${input.leaseSeconds}, ${input.limit}\n )`,\n );\n return rows.map((row) => ({\n operationId: row.operation_id,\n accountId: row.account_id,\n workspaceId: row.workspace_id,\n }));\n}\n\nexport async function getVideoGenerationOperation(\n db: Database,\n workspaceId: string,\n operationId: string,\n): Promise<VideoGenerationOperationWithReferences | null> {\n return await withWorkspaceRls(db, workspaceId, async (scopedDb) => {\n const [operation] = await scopedDb\n .select()\n .from(schema.videoGenerationOperations)\n .where(\n and(\n eq(schema.videoGenerationOperations.workspaceId, workspaceId),\n eq(schema.videoGenerationOperations.id, operationId),\n ),\n )\n .limit(1);\n if (!operation) return null;\n const references = await scopedDb\n .select()\n .from(schema.videoGenerationReferences)\n .where(eq(schema.videoGenerationReferences.operationId, operationId))\n .orderBy(schema.videoGenerationReferences.ordinal);\n return { ...operation, references };\n });\n}\n\nexport async function markVideoGenerationSubmissionUncertain(\n db: Database,\n input: OperationMutationInput & { nextReconcileAt: Date; error: string },\n): Promise<VideoGenerationOperation> {\n return await mutateOperation(db, input, async (tx, operation) => {\n if (operation.status !== \"submission_uncertain\" || !operation.providerRequestEncrypted) {\n throw invalidTransition(operation, \"submission_uncertain\");\n }\n const now = new Date();\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({\n status: \"submission_uncertain\",\n providerRequestSentAt: operation.providerRequestSentAt ?? now,\n nextReconcileAt: input.nextReconcileAt,\n lastError: boundPrivateError(input.error),\n reconcileLeaseOwner: null,\n reconcileLeaseExpiresAt: null,\n updatedAt: now,\n })\n .where(eq(schema.videoGenerationOperations.id, operation.id))\n .returning();\n return requireRow(updated, \"record uncertain video submission\");\n });\n}\n\n/**\n * Freeze the exact provider start body before issuing the request. If the\n * worker disappears after this transaction, recovery replays the same body\n * and idempotency key rather than reminting URLs or changing request bytes.\n */\nexport async function markVideoGenerationSubmissionIntent(\n db: Database,\n input: OperationMutationInput & {\n encryptedProviderRequest: string;\n providerRequestExpiresAt: Date;\n nextReconcileAt: Date;\n },\n): Promise<VideoGenerationOperation> {\n return await mutateOperation(db, input, async (tx, operation) => {\n if (operation.status === \"submission_uncertain\") {\n if (!operation.providerRequestEncrypted || !operation.providerRequestExpiresAt) {\n throw new VideoGenerationConflictError(\"Video provider request is incomplete\");\n }\n return operation;\n }\n if (operation.status !== \"accepted\") {\n throw invalidTransition(operation, \"submission_uncertain\");\n }\n const now = new Date();\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({\n status: \"submission_uncertain\",\n providerRequestEncrypted: input.encryptedProviderRequest,\n providerRequestExpiresAt: input.providerRequestExpiresAt,\n nextReconcileAt: input.nextReconcileAt,\n lastError: null,\n reconcileLeaseOwner: null,\n reconcileLeaseExpiresAt: null,\n updatedAt: now,\n })\n .where(eq(schema.videoGenerationOperations.id, operation.id))\n .returning();\n await tx\n .update(schema.videoGenerationReferences)\n .set({\n grantExpiresAt: input.providerRequestExpiresAt,\n cleanupAfter: new Date(\n Math.max(\n operation.recoveryDeadlineAt.getTime(),\n input.providerRequestExpiresAt.getTime(),\n ),\n ),\n })\n .where(eq(schema.videoGenerationReferences.operationId, operation.id));\n return requireRow(updated, \"record video provider submission intent\");\n });\n}\n\nexport async function markVideoGenerationProviderStarted(\n db: Database,\n input: OperationMutationInput & {\n providerJobId: string;\n nextReconcileAt: Date;\n },\n): Promise<VideoGenerationOperation> {\n return await mutateOperation(db, input, async (tx, operation) => {\n if (\n operation.providerJobId === input.providerJobId &&\n operation.status === \"provider_started\"\n ) {\n return operation;\n }\n if (!inArrayValue(operation.status, [\"accepted\", \"submission_uncertain\"])) {\n throw invalidTransition(operation, \"provider_started\");\n }\n const now = new Date();\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({\n status: \"provider_started\",\n providerJobId: input.providerJobId.slice(0, 1_024),\n providerRequestEncrypted: null,\n providerRequestExpiresAt: null,\n providerRequestSentAt: operation.providerRequestSentAt ?? now,\n providerStartedAt: operation.providerStartedAt ?? now,\n nextReconcileAt: input.nextReconcileAt,\n lastError: null,\n reconcileLeaseOwner: null,\n reconcileLeaseExpiresAt: null,\n updatedAt: now,\n })\n .where(eq(schema.videoGenerationOperations.id, operation.id))\n .returning();\n return requireRow(updated, \"record video provider job\");\n });\n}\n\nexport async function rescheduleVideoGenerationOperation(\n db: Database,\n input: OperationMutationInput & {\n owner?: string;\n nextReconcileAt: Date;\n error?: string;\n },\n): Promise<boolean> {\n return await withRlsContext(\n db,\n { accountId: input.accountId, workspaceId: input.workspaceId },\n async (scopedDb) => {\n const [updated] = await scopedDb\n .update(schema.videoGenerationOperations)\n .set({\n nextReconcileAt: input.nextReconcileAt,\n ...(input.error ? { lastError: boundPrivateError(input.error) } : {}),\n reconcileLeaseOwner: null,\n reconcileLeaseExpiresAt: null,\n updatedAt: new Date(),\n })\n .where(\n and(\n eq(schema.videoGenerationOperations.workspaceId, input.workspaceId),\n eq(schema.videoGenerationOperations.id, input.operationId),\n eq(schema.videoGenerationOperations.requestDigest, input.requestDigest),\n ...(input.owner\n ? [eq(schema.videoGenerationOperations.reconcileLeaseOwner, input.owner)]\n : []),\n ),\n )\n .returning({ id: schema.videoGenerationOperations.id });\n return updated !== undefined;\n },\n );\n}\n\nexport async function markVideoGenerationRetaining(\n db: Database,\n input: OperationMutationInput,\n): Promise<VideoGenerationOperation> {\n return await mutateOperation(db, input, async (tx, operation) => {\n if (operation.status === \"retaining\") return operation;\n if (operation.status !== \"provider_started\") throw invalidTransition(operation, \"retaining\");\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({\n status: \"retaining\",\n nextReconcileAt: new Date(),\n reconcileLeaseOwner: null,\n reconcileLeaseExpiresAt: null,\n updatedAt: new Date(),\n })\n .where(eq(schema.videoGenerationOperations.id, operation.id))\n .returning();\n return requireRow(updated, \"begin video retention\");\n });\n}\n\nexport async function settleVideoGenerationFailure(\n db: Database,\n input: OperationMutationInput & {\n status: \"provider_failed\" | \"retention_failed\" | \"outcome_unknown\";\n publicReason: string;\n privateError?: string;\n },\n): Promise<VideoGenerationOperation> {\n return await mutateOperation(db, input, async (tx, operation) => {\n if (operation.status === input.status) return operation;\n const allowed =\n input.status === \"provider_failed\"\n ? [\"accepted\", \"submission_uncertain\", \"provider_started\"]\n : input.status === \"retention_failed\"\n ? [\"retaining\"]\n : [\"submission_uncertain\", \"provider_started\"];\n if (!inArrayValue(operation.status, allowed)) throw invalidTransition(operation, input.status);\n await releaseReservedQuota(tx, operation, false);\n const now = new Date();\n const creditState = await refundVideoGenerationCredits(tx, operation, now);\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({\n status: input.status,\n creditState,\n quotaState: \"released\",\n terminalAt: now,\n nextReconcileAt: null,\n terminalUpdateState:\n operation.admissionOutputState === \"recorded\" ? \"pending\" : \"suppressed\",\n providerRequestEncrypted: null,\n providerRequestExpiresAt: null,\n credentialEncrypted: null,\n requestEncrypted: null,\n boundedPublicReason: boundPublicReason(input.publicReason),\n lastError: input.privateError ? boundPrivateError(input.privateError) : null,\n reconcileLeaseOwner: null,\n reconcileLeaseExpiresAt: null,\n privateDataEraseAfter: now,\n updatedAt: now,\n })\n .where(eq(schema.videoGenerationOperations.id, operation.id))\n .returning();\n return requireRow(updated, \"settle failed video generation\");\n });\n}\n\nexport type SettleVideoGenerationReadyInput = OperationMutationInput & {\n fileId: string;\n bucket: string;\n objectKey: string;\n sizeBytes: number;\n sha256: string;\n facts: GeneratedVideoFacts;\n};\n\nexport async function settleVideoGenerationReady(\n db: Database,\n input: SettleVideoGenerationReadyInput,\n): Promise<{\n operation: VideoGenerationOperation;\n artifact: GeneratedVideoArtifact;\n}> {\n return await withRlsContext(\n db,\n { accountId: input.accountId, workspaceId: input.workspaceId },\n async (scopedDb) =>\n await scopedDb.transaction(async (tx) => {\n const [operation] = await tx\n .select()\n .from(schema.videoGenerationOperations)\n .where(\n and(\n eq(schema.videoGenerationOperations.workspaceId, input.workspaceId),\n eq(schema.videoGenerationOperations.id, input.operationId),\n eq(schema.videoGenerationOperations.requestDigest, input.requestDigest),\n ),\n )\n .for(\"update\")\n .limit(1);\n if (!operation) throw new VideoGenerationConflictError(\"Video operation not found\");\n const [existingArtifact] = await tx\n .select()\n .from(schema.generatedVideoArtifacts)\n .where(eq(schema.generatedVideoArtifacts.operationId, operation.id))\n .limit(1);\n if (operation.status === \"completed\" && existingArtifact) {\n return { operation, artifact: existingArtifact };\n }\n if (operation.status !== \"retaining\") throw invalidTransition(operation, \"completed\");\n if (operation.expectedFileId !== input.fileId) {\n throw new VideoGenerationConflictError(\"Video File identity changed during settlement\");\n }\n if (operation.expectedArtifactId === operation.expectedFileId) {\n throw new Error(\"Video artifact and File identities must be distinct\");\n }\n // The durable provider operation owns this write, including when its\n // original session has since disappeared. Ordinary file reads stay scoped.\n await tx.execute(\n sql`SELECT set_config('opengeni.private_file_owner',${operation.privateFileOwnerSubjectId ?? \"\"},true)`,\n );\n const now = new Date();\n await tx.insert(schema.files).values({\n id: input.fileId,\n accountId: input.accountId,\n workspaceId: input.workspaceId,\n status: \"ready\",\n filename: `generated-video-${operation.expectedArtifactId}.mp4`,\n safeFilename: `generated-video-${operation.expectedArtifactId}.mp4`,\n contentType: \"video/mp4\",\n sizeBytes: input.sizeBytes,\n sha256: input.sha256,\n bucket: input.bucket,\n objectKey: input.objectKey,\n updatedAt: now,\n });\n const [artifact] = await tx\n .insert(schema.generatedVideoArtifacts)\n .values({\n id: operation.expectedArtifactId,\n accountId: input.accountId,\n workspaceId: input.workspaceId,\n primaryFileId: input.fileId,\n operationId: operation.id,\n sessionId: operation.sessionId,\n turnId: operation.turnId,\n attemptId: operation.attemptId,\n modelId: operation.modelId,\n sourceMode: operation.sourceMode,\n promptDigest: operation.promptDigest,\n contentType: \"video/mp4\",\n sizeBytes: input.sizeBytes,\n sha256: input.sha256,\n durationMillis: Math.round(input.facts.durationSeconds * 1_000),\n width: input.facts.width,\n height: input.facts.height,\n fpsMilli: Math.round(input.facts.fps * 1_000),\n videoCodec: input.facts.videoCodec,\n audioCodec: input.facts.audioCodec,\n hasAudio: input.facts.hasAudio,\n sandboxFilename: `generated-video-${operation.expectedArtifactId}.mp4`,\n readyAt: now,\n })\n .returning();\n if (!artifact) throw new Error(\"Generated video artifact was not persisted\");\n await tx\n .update(schema.workspaceVideoGenerationQuotas)\n .set({\n reservedBytes: sql`${schema.workspaceVideoGenerationQuotas.reservedBytes} - ${operation.reservedBytes}`,\n readyBytes: sql`${schema.workspaceVideoGenerationQuotas.readyBytes} + ${input.sizeBytes}`,\n updatedAt: now,\n })\n .where(eq(schema.workspaceVideoGenerationQuotas.workspaceId, input.workspaceId));\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({\n status: \"completed\",\n quotaState: \"ready\",\n terminalAt: now,\n nextReconcileAt: null,\n terminalUpdateState:\n operation.admissionOutputState === \"recorded\" ? \"pending\" : \"suppressed\",\n providerRequestEncrypted: null,\n providerRequestExpiresAt: null,\n credentialEncrypted: null,\n requestEncrypted: null,\n boundedPublicReason: null,\n lastError: null,\n privateDataEraseAfter: now,\n reconcileLeaseOwner: null,\n reconcileLeaseExpiresAt: null,\n updatedAt: now,\n })\n .where(eq(schema.videoGenerationOperations.id, operation.id))\n .returning();\n return {\n operation: requireRow(updated, \"settle ready video generation\"),\n artifact,\n };\n }),\n );\n}\n\nexport async function getGeneratedVideoArtifact(\n db: Database,\n workspaceId: string,\n artifactId: string,\n): Promise<{\n artifact: GeneratedVideoArtifact;\n file: typeof schema.files.$inferSelect;\n} | null> {\n return await withWorkspaceRls(db, workspaceId, async (scopedDb) => {\n const [row] = await scopedDb\n .select({ artifact: schema.generatedVideoArtifacts, file: schema.files })\n .from(schema.generatedVideoArtifacts)\n .innerJoin(schema.files, eq(schema.files.id, schema.generatedVideoArtifacts.primaryFileId))\n .where(\n and(\n eq(schema.generatedVideoArtifacts.workspaceId, workspaceId),\n eq(schema.generatedVideoArtifacts.id, artifactId),\n ),\n )\n .limit(1);\n return row ?? null;\n });\n}\n\nexport async function listSessionGeneratedVideoArtifacts(\n db: Database,\n workspaceId: string,\n sessionId: string,\n): Promise<\n Array<{\n artifact: GeneratedVideoArtifact;\n file: typeof schema.files.$inferSelect;\n }>\n> {\n return await withWorkspaceRls(\n db,\n workspaceId,\n async (scopedDb) =>\n await scopedDb\n .select({\n artifact: schema.generatedVideoArtifacts,\n file: schema.files,\n })\n .from(schema.generatedVideoArtifacts)\n .innerJoin(schema.files, eq(schema.files.id, schema.generatedVideoArtifacts.primaryFileId))\n .where(\n and(\n eq(schema.generatedVideoArtifacts.workspaceId, workspaceId),\n eq(schema.generatedVideoArtifacts.sessionId, sessionId),\n sql`${schema.generatedVideoArtifacts.deletedAt} is null`,\n ),\n )\n .orderBy(schema.generatedVideoArtifacts.createdAt, schema.generatedVideoArtifacts.id),\n );\n}\n\n/**\n * Record one staging object as erased only after the object-store delete has\n * succeeded. Deleting first is safe because object deletion is idempotent; a\n * crash before this CAS simply repeats the same delete on recovery.\n */\nexport async function markVideoGenerationReferenceCleaned(\n db: Database,\n input: {\n accountId: string;\n workspaceId: string;\n operationId: string;\n ordinal: number;\n stagingObjectKey: string;\n },\n): Promise<boolean> {\n return await withRlsContext(\n db,\n { accountId: input.accountId, workspaceId: input.workspaceId },\n async (scopedDb) => {\n const [updated] = await scopedDb\n .update(schema.videoGenerationReferences)\n .set({\n stagingObjectKey: null,\n grantExpiresAt: null,\n cleanupAfter: null,\n cleanedAt: new Date(),\n })\n .where(\n and(\n eq(schema.videoGenerationReferences.workspaceId, input.workspaceId),\n eq(schema.videoGenerationReferences.operationId, input.operationId),\n eq(schema.videoGenerationReferences.ordinal, input.ordinal),\n eq(schema.videoGenerationReferences.stagingObjectKey, input.stagingObjectKey),\n ),\n )\n .returning({\n operationId: schema.videoGenerationReferences.operationId,\n });\n if (updated) return true;\n const [existing] = await scopedDb\n .select({ cleanedAt: schema.videoGenerationReferences.cleanedAt })\n .from(schema.videoGenerationReferences)\n .where(\n and(\n eq(schema.videoGenerationReferences.workspaceId, input.workspaceId),\n eq(schema.videoGenerationReferences.operationId, input.operationId),\n eq(schema.videoGenerationReferences.ordinal, input.ordinal),\n ),\n )\n .limit(1);\n return existing?.cleanedAt !== null && existing?.cleanedAt !== undefined;\n },\n );\n}\n\nexport async function markVideoGenerationTerminalUpdate(\n db: Database,\n input: {\n accountId: string;\n workspaceId: string;\n operationId: string;\n state: \"leased\" | \"delivered\" | \"suppressed\";\n updateId?: string;\n },\n): Promise<boolean> {\n return await withRlsContext(\n db,\n { accountId: input.accountId, workspaceId: input.workspaceId },\n async (scopedDb) => {\n return await markVideoGenerationTerminalUpdateInTransaction(scopedDb, input);\n },\n );\n}\n\nexport async function markVideoGenerationTerminalUpdateInTransaction(\n tx: Database,\n input: {\n workspaceId: string;\n operationId: string;\n state: \"leased\" | \"delivered\" | \"suppressed\";\n updateId?: string;\n },\n): Promise<boolean> {\n const allowed =\n input.state === \"leased\"\n ? [\"pending\", \"leased\"]\n : input.state === \"delivered\"\n ? [\"leased\"]\n : [\"pending\", \"leased\"];\n const [updated] = await tx\n .update(schema.videoGenerationOperations)\n .set({\n terminalUpdateState: input.state,\n terminalUpdateId: input.state === \"suppressed\" ? null : (input.updateId ?? null),\n updatedAt: new Date(),\n })\n .where(\n and(\n eq(schema.videoGenerationOperations.workspaceId, input.workspaceId),\n eq(schema.videoGenerationOperations.id, input.operationId),\n inArray(schema.videoGenerationOperations.terminalUpdateState, allowed),\n ...(input.state === \"delivered\" && input.updateId\n ? [eq(schema.videoGenerationOperations.terminalUpdateId, input.updateId)]\n : []),\n ),\n )\n .returning({ id: schema.videoGenerationOperations.id });\n return updated !== undefined;\n}\n\nexport function mediaGenerationResultForOperation(input: {\n operation: VideoGenerationOperation;\n readyReceipt?: Extract<MediaGenerationResult, { status: \"ready\" }>[\"receipt\"];\n}): MediaGenerationResult {\n if (input.operation.status === \"completed\" && input.readyReceipt) {\n return {\n type: \"media_generation_result\",\n schemaVersion: 1,\n status: \"ready\",\n operationId: input.operation.id,\n receipt: input.readyReceipt,\n };\n }\n const failureStatus = input.operation.status;\n if (\n !inArrayValue(failureStatus, [\n \"provider_failed\",\n \"retention_failed\",\n \"cancelled_before_submit\",\n \"outcome_unknown\",\n ])\n ) {\n throw new Error(\"Video generation operation is not terminal\");\n }\n return {\n type: \"media_generation_result\",\n schemaVersion: 1,\n status: failureStatus,\n operationId: input.operation.id,\n boundedPublicReason:\n input.operation.boundedPublicReason ?? \"Video generation did not complete.\",\n } as MediaGenerationResult;\n}\n\n/** Build the compact public/model terminal envelope from durable canonical rows. */\nexport async function mediaGenerationResultForStoredOperation(\n db: Database,\n operation: VideoGenerationOperation,\n): Promise<MediaGenerationResult> {\n if (operation.status !== \"completed\") {\n return mediaGenerationResultForOperation({ operation });\n }\n const retained = await getGeneratedVideoArtifact(\n db,\n operation.workspaceId,\n operation.expectedArtifactId,\n );\n if (!retained || retained.artifact.deletedAt) {\n throw new Error(\"Completed video generation lost its retained artifact\");\n }\n const artifact = retainedGeneratedVideoReferenceFromFile({\n id: retained.artifact.id,\n workspaceId: retained.artifact.workspaceId,\n status: retained.file.status,\n contentType: retained.file.contentType,\n sizeBytes: retained.file.sizeBytes,\n sha256: retained.file.sha256,\n updatedAt: retained.artifact.readyAt.toISOString(),\n width: retained.artifact.width,\n height: retained.artifact.height,\n });\n if (!artifact) throw new Error(\"Generated video receipt could not be constructed\");\n const receipt = GeneratedVideoReceipt.parse({\n type: \"generated_video\",\n schemaVersion: 1,\n operationId: operation.id,\n artifact,\n video: {\n durationSeconds: retained.artifact.durationMillis / 1_000,\n width: retained.artifact.width,\n height: retained.artifact.height,\n fps: retained.artifact.fpsMilli / 1_000,\n hasAudio: retained.artifact.hasAudio,\n videoCodec: retained.artifact.videoCodec,\n audioCodec: retained.artifact.audioCodec,\n },\n sandboxPath: `/workspace/generated-videos/${retained.artifact.sandboxFilename}`,\n });\n return mediaGenerationResultForOperation({\n operation,\n readyReceipt: receipt,\n });\n}\n\nexport async function getVideoGenerationOperationSummary(\n db: Database,\n workspaceId: string,\n operationId: string,\n): Promise<import(\"@opengeni/contracts\").VideoGenerationOperationSummary | null> {\n const operation = await getVideoGenerationOperation(db, workspaceId, operationId);\n if (!operation) return null;\n if (operation.privateFileOwnerSubjectId) {\n const [context] = await withWorkspaceRls(db, workspaceId, (tx) =>\n rawRows<{ owner: string | null }>(\n tx,\n sql`SELECT nullif(current_setting('opengeni.private_file_owner',true),'') AS owner`,\n ),\n );\n if (context?.owner !== operation.privateFileOwnerSubjectId) return null;\n }\n const terminal = inArrayValue(operation.status, [\n \"completed\",\n \"provider_failed\",\n \"cancelled_before_submit\",\n \"outcome_unknown\",\n \"retention_failed\",\n ])\n ? await mediaGenerationResultForStoredOperation(db, operation)\n : null;\n return VideoGenerationOperationSummary.parse({\n schemaVersion: 1,\n operationId: operation.id,\n modelId: operation.modelId,\n status: operation.status === \"submission_uncertain\" ? \"accepted\" : operation.status,\n createdAt: operation.createdAt.toISOString(),\n updatedAt: operation.updatedAt.toISOString(),\n terminal,\n });\n}\n\ntype OperationMutationInput = {\n accountId: string;\n workspaceId: string;\n operationId: string;\n requestDigest: string;\n};\n\nasync function mutateOperation<T>(\n db: Database,\n input: OperationMutationInput,\n mutate: (tx: Database, operation: VideoGenerationOperation) => Promise<T>,\n): Promise<T> {\n return await withRlsContext(\n db,\n { accountId: input.accountId, workspaceId: input.workspaceId },\n async (scopedDb) =>\n await scopedDb.transaction(async (txRaw) => {\n const tx = txRaw as unknown as Database;\n const [operation] = await tx\n .select()\n .from(schema.videoGenerationOperations)\n .where(\n and(\n eq(schema.videoGenerationOperations.workspaceId, input.workspaceId),\n eq(schema.videoGenerationOperations.id, input.operationId),\n eq(schema.videoGenerationOperations.requestDigest, input.requestDigest),\n ),\n )\n .for(\"update\")\n .limit(1);\n if (!operation) throw new VideoGenerationConflictError(\"Video operation not found\");\n return await mutate(tx, operation);\n }),\n );\n}\n\nasync function releaseReservedQuota(\n tx: Database,\n operation: VideoGenerationOperation,\n ready: boolean,\n): Promise<void> {\n if (operation.quotaState !== \"reserved\") return;\n await tx\n .update(schema.workspaceVideoGenerationQuotas)\n .set({\n reservedBytes: sql`${schema.workspaceVideoGenerationQuotas.reservedBytes} - ${operation.reservedBytes}`,\n ...(ready\n ? {\n readyBytes: sql`${schema.workspaceVideoGenerationQuotas.readyBytes} + ${operation.reservedBytes}`,\n }\n : {}),\n updatedAt: new Date(),\n })\n .where(eq(schema.workspaceVideoGenerationQuotas.workspaceId, operation.workspaceId));\n}\n\nasync function debitVideoGenerationCredits(\n tx: Database,\n operation: VideoGenerationOperation,\n): Promise<void> {\n if (operation.pricedCostMicros <= 0 || operation.fundingSource !== \"opengeni_credits\") {\n throw new Error(\"Managed video credit debit has an invalid funding binding\");\n }\n if (!operation.turnId) {\n throw new VideoGenerationCreditError(\"Paid video initiating turn is unavailable\");\n }\n // Model settlement locks the account before the ledger trigger's workspace\n // allowance fence. Preserve that order during prepaid media admission.\n await tx.execute(sql`select pg_advisory_xact_lock(hashtext(${operation.accountId}))`);\n const attribution = await creditDebitAttributionForTurn(tx, {\n accountId: operation.accountId,\n workspaceId: operation.workspaceId,\n turnId: operation.turnId,\n });\n const refusal = await checkWorkspaceAllowance(tx, {\n accountId: operation.accountId,\n workspaceId: operation.workspaceId,\n subjectId: attribution.kind === \"turn\" ? attribution.initiatingHumanSubjectId : null,\n });\n if (refusal) throw Object.assign(new VideoGenerationCreditError(refusal.message), refusal);\n const [balanceRow] = await tx\n .select({\n balanceMicros: sql<number>`coalesce(sum(${schema.creditLedgerEntries.amountMicros}), 0)`,\n })\n .from(schema.creditLedgerEntries)\n .where(eq(schema.creditLedgerEntries.accountId, operation.accountId));\n const balanceMicros = Number(balanceRow?.balanceMicros ?? 0);\n if (!Number.isSafeInteger(balanceMicros) || balanceMicros < operation.pricedCostMicros) {\n throw new VideoGenerationCreditError();\n }\n const now = new Date();\n const inserted = await tx\n .insert(schema.creditLedgerEntries)\n .values({\n accountId: operation.accountId,\n workspaceId: operation.workspaceId,\n type: \"video_generation_debit\",\n amountMicros: -operation.pricedCostMicros,\n sourceType: \"video_generation_operation\",\n sourceId: operation.id,\n idempotencyKey: `credit:video_generation_debit:${operation.id}`,\n metadata: {\n turnId: operation.turnId,\n modelId: operation.modelId,\n sourceMode: operation.sourceMode,\n pricedCostMicros: operation.pricedCostMicros,\n },\n occurredAt: now,\n })\n .onConflictDoNothing({ target: schema.creditLedgerEntries.idempotencyKey })\n .returning({ id: schema.creditLedgerEntries.id });\n if (inserted.length !== 1) {\n throw new VideoGenerationConflictError(\"Video generation credit debit already exists\");\n }\n await tx\n .insert(schema.usageEvents)\n .values({\n accountId: operation.accountId,\n workspaceId: operation.workspaceId,\n eventType: \"video_generation.cost\",\n quantity: operation.pricedCostMicros,\n unit: \"usd_micros\",\n sourceResourceType: \"video_generation_operation\",\n sourceResourceId: operation.id,\n sessionId: operation.sessionId,\n turnId: operation.turnId,\n turnAttemptId: operation.attemptId,\n idempotencyKey: `usage:video_generation.cost:${operation.id}`,\n occurredAt: now,\n })\n .onConflictDoNothing({ target: schema.usageEvents.idempotencyKey });\n}\n\nasync function refundVideoGenerationCredits(\n tx: Database,\n operation: VideoGenerationOperation,\n now: Date,\n): Promise<VideoGenerationOperation[\"creditState\"]> {\n if (operation.creditState !== \"debited\") return operation.creditState;\n if (operation.fundingSource !== \"opengeni_credits\" || operation.pricedCostMicros <= 0) {\n throw new Error(\"Video generation credit refund has an invalid funding binding\");\n }\n await tx.execute(sql`select pg_advisory_xact_lock(hashtext(${operation.accountId}))`);\n const inserted = await tx\n .insert(schema.creditLedgerEntries)\n .values({\n accountId: operation.accountId,\n workspaceId: operation.workspaceId,\n type: \"video_generation_refund\",\n amountMicros: operation.pricedCostMicros,\n sourceType: \"video_generation_operation\",\n sourceId: operation.id,\n idempotencyKey: `credit:video_generation_refund:${operation.id}`,\n metadata: {\n turnId: operation.turnId,\n modelId: operation.modelId,\n sourceMode: operation.sourceMode,\n pricedCostMicros: operation.pricedCostMicros,\n },\n occurredAt: now,\n })\n .onConflictDoNothing({ target: schema.creditLedgerEntries.idempotencyKey })\n .returning({ id: schema.creditLedgerEntries.id });\n if (inserted.length !== 1) {\n throw new VideoGenerationConflictError(\"Video generation credit refund already exists\");\n }\n await tx\n .insert(schema.usageEvents)\n .values({\n accountId: operation.accountId,\n workspaceId: operation.workspaceId,\n eventType: \"video_generation.refund\",\n quantity: operation.pricedCostMicros,\n unit: \"usd_micros\",\n sourceResourceType: \"video_generation_operation\",\n sourceResourceId: operation.id,\n sessionId: operation.sessionId,\n turnId: operation.turnId,\n turnAttemptId: operation.attemptId,\n idempotencyKey: `usage:video_generation.refund:${operation.id}`,\n occurredAt: now,\n })\n .onConflictDoNothing({ target: schema.usageEvents.idempotencyKey });\n return \"refunded\";\n}\n\nfunction policyFromRow(\n row: typeof schema.workspaceVideoGenerationPolicies.$inferSelect | undefined,\n): VideoGenerationPolicyValue {\n return VideoGenerationPolicy.parse(\n row\n ? {\n schemaVersion: 1,\n revision: row.revision,\n fundingSource: row.fundingSource,\n enabledModelIds: row.enabledModelIds,\n defaultModelId: row.defaultModelId,\n }\n : {\n schemaVersion: 1,\n revision: 0,\n fundingSource: \"workspace_gateway\",\n enabledModelIds: [],\n defaultModelId: null,\n },\n );\n}\n\nfunction assertSupportedModels(modelIds: readonly string[]): void {\n for (const modelId of modelIds) {\n if (modelId !== SEEDANCE_2_5_MODEL_ID && modelId !== GROK_IMAGINE_VIDEO_1_5_MODEL_ID) {\n throw new Error(`Unknown video generation model: ${modelId}`);\n }\n }\n}\n\nfunction assertAdmissionMatches(\n operation: VideoGenerationOperation,\n input: AdmitVideoGenerationOperationInput,\n): void {\n if (\n operation.requestDigest !== input.requestDigest ||\n operation.toolCallId !== input.toolCallId ||\n operation.sessionId !== input.sessionId ||\n operation.turnId !== input.turnId ||\n operation.fundingSource !== input.fundingSource ||\n operation.pricedCostMicros !== input.pricedCostMicros\n ) {\n throw new VideoGenerationConflictError(\n \"Video generation admission key was reused with different arguments\",\n );\n }\n}\n\nfunction inPreSubmitState(status: string): boolean {\n return inArrayValue(status, [\"preparing\", \"prepared\", \"accepted\"]);\n}\n\nfunction inArrayValue<T extends string>(value: string, candidates: readonly T[]): value is T {\n return (candidates as readonly string[]).includes(value);\n}\n\nfunction invalidTransition(operation: VideoGenerationOperation, target: string): Error {\n return new VideoGenerationConflictError(\n `Invalid video generation transition ${operation.status} -> ${target}`,\n );\n}\n\nfunction requireRow<T>(value: T | undefined, action: string): T {\n if (!value) throw new Error(`Failed to ${action}`);\n return value;\n}\n\nfunction boundPublicReason(value: string): string {\n const normalized = value.replace(/\\s+/gu, \" \").trim();\n return (normalized || \"Video generation did not complete.\").slice(0, 1_000);\n}\n\nfunction boundPrivateError(value: string): string {\n return value.replace(/\\s+/gu, \" \").trim().slice(0, 4_096);\n}\n","import { sql } from \"drizzle-orm\";\nimport { rawRows, setSubjectRlsContext, withRlsContext, type Database } from \"./database\";\nimport { currentCreditDebitAttribution } from \"./credit-debit-attribution\";\n\n/** All amounts are integer USD micros, never dollars or floating-point credits. */\nexport type MemberAllowanceRule = { share: number } | { credits: number } | null;\nexport type WorkspaceAllowanceConfig = {\n includedCredits: number;\n period: \"monthly\" | \"none\";\n anchorDay?: number;\n memberDefault?: \"none\" | \"equal_share\" | Exclude<MemberAllowanceRule, null>;\n thresholds?: { workspace?: number[]; member?: number[] };\n};\nexport type WorkspaceAllowance = WorkspaceAllowanceConfig & { version: number };\nexport type WorkspaceAllowanceState = { version: number; config: WorkspaceAllowance | null };\nexport type AllowanceStatus = \"ok\" | \"warning\" | \"exhausted\";\nexport type AllowanceUsage = {\n limit: number | null;\n used: number;\n remaining: number | null;\n fraction: number | null;\n status: AllowanceStatus;\n resetsAt: string | null;\n};\nexport type MemberAllowance = {\n subjectId: string;\n rule: MemberAllowanceRule;\n version: number;\n};\nexport type WorkspaceUsage = {\n period: { start: string | null; end: string | null };\n workspace: AllowanceUsage & { includedCredits: number; grantsRemaining: number };\n members: (AllowanceUsage &\n MemberAllowance & {\n externalIdentity: { source: string; externalId: string } | null;\n })[];\n nextCursor: string | null;\n};\nexport type AllowanceExhausted = {\n code: \"allowance_exhausted\";\n scope: \"workspace\" | \"member\";\n resetsAt: string | null;\n subjectId?: string;\n message: string;\n};\ntype Scope = { accountId: string; workspaceId: string };\ntype Actor = { actorSubjectId: string; actorType?: string | undefined };\n\nexport class UsageAllowanceVersionConflictError extends Error {\n constructor() {\n super(\"Usage allowance version conflict\");\n this.name = \"UsageAllowanceVersionConflictError\";\n }\n}\n\nfunction amount(value: number, name: string, positive = false): void {\n if (!Number.isSafeInteger(value) || value < (positive ? 1 : 0)) {\n throw new Error(\n `${name} must be ${positive ? \"a positive\" : \"a non-negative\"} safe integer in USD micros`,\n );\n }\n}\n\nexport function validateMemberAllowanceRule(rule: MemberAllowanceRule): void {\n if (rule === null) return;\n if (\"credits\" in rule) {\n if (Object.keys(rule).length !== 1)\n throw new Error(\"Member rule must have only credits or share\");\n amount(rule.credits, \"credits\");\n } else if (\"share\" in rule) {\n if (Object.keys(rule).length !== 1 || !Number.isFinite(rule.share) || rule.share < 0) {\n throw new Error(\"share must be a finite non-negative number\");\n }\n } else {\n throw new Error(\"Member rule must have credits or share\");\n }\n}\n\nexport function validateWorkspaceAllowanceConfig(config: WorkspaceAllowanceConfig): void {\n amount(config.includedCredits, \"includedCredits\");\n if (config.period !== \"monthly\" && config.period !== \"none\")\n throw new Error(\"Invalid allowance period\");\n if (\n config.anchorDay !== undefined &&\n (!Number.isInteger(config.anchorDay) || config.anchorDay < 1 || config.anchorDay > 31)\n ) {\n throw new Error(\"anchorDay must be an integer from 1 to 31\");\n }\n if (\n config.memberDefault !== undefined &&\n config.memberDefault !== \"none\" &&\n config.memberDefault !== \"equal_share\"\n ) {\n validateMemberAllowanceRule(config.memberDefault);\n }\n for (const list of [config.thresholds?.workspace, config.thresholds?.member]) {\n if (\n list !== undefined &&\n (list.length > 16 ||\n list.some((threshold) => !Number.isFinite(threshold) || threshold <= 0 || threshold > 1))\n ) {\n throw new Error(\n \"thresholds must contain at most 16 fractions greater than zero and at most one\",\n );\n }\n }\n}\n\nasync function command<T>(db: Database, input: Scope & Record<string, unknown>): Promise<T> {\n if (\n [\"set\", \"clear\", \"member\", \"grant\"].includes(String(input.action)) &&\n currentCreditDebitAttribution().kind === \"turn\"\n ) {\n // This context is installed by the authenticated request boundary, not\n // supplied in the allowance command. Agent attempts cannot substitute\n // human-looking actor labels to author allowance policy.\n throw new Error(\"Agent attempts cannot change usage allowances\");\n }\n try {\n const mutation = [\"set\", \"clear\", \"member\", \"grant\"].includes(String(input.action));\n return await withRlsContext(\n db,\n input,\n async (scoped) => {\n if (mutation) {\n // The organization lifecycle can hold this fence while waiting for\n // exclusive tenancy. Never acquire shared tenancy ahead of it.\n await scoped.execute(sql`select pg_advisory_xact_lock(\n hashtextextended(${`organization-membership:${input.accountId}`},0))`);\n await scoped.execute(sql`select pg_advisory_xact_lock_shared(\n hashtextextended(${`session-tenancy:${input.workspaceId}`},0))`);\n }\n if (typeof input.actorSubjectId === \"string\")\n await setSubjectRlsContext(scoped, input.actorSubjectId);\n const [row] = await rawRows<{ result: T }>(\n scoped,\n sql`select usage_allowance_command(${JSON.stringify(input)}::jsonb) as result`,\n );\n if (!row) throw new Error(\"Usage allowance command returned no result\");\n return row.result;\n },\n undefined,\n mutation ? \"none\" : \"shared\",\n );\n } catch (error) {\n const cause = error as { code?: string; cause?: { code?: string } };\n if ((cause.code ?? cause.cause?.code) === \"40001\")\n throw new UsageAllowanceVersionConflictError();\n throw error;\n }\n}\n\nfunction actor(input: Actor): Actor & { actorType: string } {\n return { actorSubjectId: input.actorSubjectId, actorType: input.actorType ?? \"subject\" };\n}\nfunction version(value: number): void {\n amount(value, \"expectedVersion\");\n}\n\nexport async function getWorkspaceAllowance(\n db: Database,\n input: Scope,\n): Promise<WorkspaceAllowance | null> {\n return await command(db, { ...input, action: \"get\" });\n}\n\n/** Includes the lifecycle revision even when no configuration exists. */\nexport async function getWorkspaceAllowanceState(\n db: Database,\n input: Scope,\n): Promise<WorkspaceAllowanceState> {\n return await command(db, { ...input, action: \"state\" });\n}\n\nexport async function setWorkspaceAllowance(\n db: Database,\n input: Scope & Actor & WorkspaceAllowanceConfig & { expectedVersion: number },\n): Promise<WorkspaceAllowance> {\n const { accountId, workspaceId, expectedVersion, actorSubjectId, actorType } = input;\n const config: WorkspaceAllowanceConfig = {\n includedCredits: input.includedCredits,\n period: input.period,\n ...(input.anchorDay === undefined ? {} : { anchorDay: input.anchorDay }),\n ...(input.memberDefault === undefined ? {} : { memberDefault: input.memberDefault }),\n ...(input.thresholds === undefined ? {} : { thresholds: input.thresholds }),\n };\n version(expectedVersion);\n validateWorkspaceAllowanceConfig(config);\n return await command(db, {\n accountId,\n workspaceId,\n expectedVersion,\n ...actor({ actorSubjectId, actorType }),\n action: \"set\",\n config,\n });\n}\n\nexport async function clearWorkspaceAllowance(\n db: Database,\n input: Scope & Actor & { expectedVersion: number; operationId?: string },\n): Promise<{ version: number }> {\n version(input.expectedVersion);\n if (input.expectedVersion === 0) throw new UsageAllowanceVersionConflictError();\n if (\n input.operationId !== undefined &&\n (!input.operationId.trim() || new TextEncoder().encode(input.operationId).length > 256)\n )\n throw new Error(\"operationId must be non-empty and at most 256 bytes\");\n return await command(db, { ...input, ...actor(input), action: \"clear\" });\n}\n\nexport async function grantWorkspaceCredits(\n db: Database,\n input: Scope & Actor & { operationId: string; credits: number; expiresAt?: string | null },\n): Promise<{ operationId: string; credits: number; remaining: number; expiresAt: string | null }> {\n amount(input.credits, \"credits\", true);\n if (!input.operationId.trim() || new TextEncoder().encode(input.operationId).length > 256) {\n throw new Error(\"operationId must be non-empty and at most 256 bytes\");\n }\n if (input.expiresAt != null && !Number.isFinite(Date.parse(input.expiresAt)))\n throw new Error(\"Invalid grant expiresAt\");\n return await command(db, {\n ...input,\n ...actor(input),\n expiresAt: input.expiresAt ?? null,\n action: \"grant\",\n });\n}\n\nexport async function setMemberAllowance(\n db: Database,\n input: Scope &\n Actor & { rule: MemberAllowanceRule; expectedVersion: number } & (\n | { subjectId: string; externalIdentity?: never }\n | { externalIdentity: { source: string; externalId: string }; subjectId?: never }\n ),\n): Promise<MemberAllowance> {\n version(input.expectedVersion);\n validateMemberAllowanceRule(input.rule);\n return await command(db, { ...input, ...actor(input), action: \"member\" });\n}\n\ntype RawUsage = {\n config: WorkspaceAllowanceConfig | null;\n period: WorkspaceUsage[\"period\"];\n used: number;\n includedUsed: number;\n grantsUsed: number;\n grantsRemaining: number;\n memberCount: number;\n members: (MemberAllowance & {\n used: number;\n externalIdentity: { source: string; externalId: string } | null;\n })[];\n};\n\nexport function allowanceUsage(\n limit: number | null,\n used: number,\n resetsAt: string | null,\n thresholds = [0.8, 1],\n): AllowanceUsage {\n const fraction = limit === null ? null : limit === 0 ? 1 : used / limit;\n const remaining = limit === null ? null : Math.max(0, limit - used);\n return {\n limit,\n used,\n remaining,\n fraction,\n status:\n remaining === 0\n ? \"exhausted\"\n : fraction !== null && thresholds.some((t) => fraction >= t)\n ? \"warning\"\n : \"ok\",\n resetsAt,\n };\n}\n\nexport async function getWorkspaceUsage(\n db: Database,\n input: Scope & {\n period?: \"current\" | string;\n limit?: number;\n cursor?: string | null;\n subjectId?: string;\n },\n): Promise<WorkspaceUsage> {\n if (\n input.period !== undefined &&\n input.period !== \"current\" &&\n !/^\\d{4}-(0[1-9]|1[0-2])$/.test(input.period)\n ) {\n throw new Error(\"period must be current or YYYY-MM\");\n }\n const pageSize = Math.min(500, Math.max(1, Math.trunc(input.limit ?? 100)));\n const raw = await command<RawUsage>(db, { ...input, limit: pageSize, action: \"usage\" });\n return projectUsage(raw, pageSize);\n}\n\nfunction projectUsage(raw: RawUsage, pageSize: number): WorkspaceUsage {\n const config = raw.config;\n const pool = config\n ? config.includedCredits + Number(raw.grantsUsed) + Number(raw.grantsRemaining)\n : null;\n const memberPool = config ? config.includedCredits + Number(raw.grantsRemaining) : null;\n const resetsAt = config ? raw.period.end : null;\n const members = raw.members.slice(0, pageSize).map((member) => {\n let rule = member.rule;\n if (rule === null) {\n const fallback = config?.memberDefault ?? \"none\";\n rule =\n fallback === \"none\"\n ? null\n : fallback === \"equal_share\"\n ? { share: raw.memberCount > 0 ? 1 / Number(raw.memberCount) : 0 }\n : fallback;\n }\n const limit =\n rule === null\n ? null\n : \"credits\" in rule\n ? rule.credits\n : memberPool === null\n ? null\n : Math.min(Number.MAX_SAFE_INTEGER, Math.floor(memberPool * rule.share));\n return {\n ...member,\n rule: member.rule,\n ...allowanceUsage(limit, Number(member.used), resetsAt, config?.thresholds?.member),\n };\n });\n return {\n period: config ? raw.period : { start: null, end: null },\n workspace: {\n ...allowanceUsage(pool, Number(raw.used), resetsAt, config?.thresholds?.workspace),\n includedCredits: config?.includedCredits ?? 0,\n grantsRemaining: Number(raw.grantsRemaining),\n },\n members,\n nextCursor: raw.members.length > pageSize ? members.at(-1)!.subjectId : null,\n };\n}\n\nexport async function checkWorkspaceAllowance(\n db: Database,\n input: Scope & { subjectId?: string | null },\n): Promise<AllowanceExhausted | null> {\n const raw = await command<RawUsage>(db, {\n accountId: input.accountId,\n workspaceId: input.workspaceId,\n ...(input.subjectId ? { subjectId: input.subjectId } : {}),\n limit: 1,\n action: \"check\",\n });\n const usage = projectUsage(raw, 1);\n if (usage.workspace.status === \"exhausted\") {\n return {\n code: \"allowance_exhausted\",\n scope: \"workspace\",\n resetsAt: usage.workspace.resetsAt,\n message: \"The workspace usage allowance is exhausted.\",\n };\n }\n const member = input.subjectId\n ? usage.members.find((row) => row.subjectId === input.subjectId)\n : null;\n if (member?.status === \"exhausted\") {\n return {\n code: \"allowance_exhausted\",\n scope: \"member\",\n subjectId: member.subjectId,\n resetsAt: member.resetsAt,\n message: \"The member usage allowance is exhausted.\",\n };\n }\n return null;\n}\n\n/** Bounded periodic work, independent of reads/admission and webhook delivery. */\nexport async function maintainWorkspaceAllowances(\n db: Database,\n input: { limit?: number; memberLimit?: number } = {},\n): Promise<number> {\n const [row] = await rawRows<{ processed: number }>(\n db,\n sql`select maintain_usage_allowances(${input.limit ?? 20}::integer,${input.memberLimit ?? 100}::integer) as processed`,\n );\n return Number(row?.processed ?? 0);\n}\n","import { z } from \"zod\";\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport { sql } from \"drizzle-orm\";\nimport { rawRows, withRlsContext, type Database } from \"./database\";\n\nconst creditDebitAttributionContext = new AsyncLocalStorage<CreditDebitAttribution>();\n\nexport function currentCreditDebitAttribution(): CreditDebitAttribution {\n return creditDebitAttributionContext.getStore() ?? { kind: \"unknown\" };\n}\n\n/** Scope trusted HTTP/MCP admission facts across nested sandbox calls. */\nexport function withCreditDebitAttribution<T>(\n attribution: CreditDebitAttribution,\n fn: () => Promise<T>,\n): Promise<T> {\n return creditDebitAttributionContext.run(\n Object.freeze(CreditDebitAttribution.parse(attribution)),\n fn,\n );\n}\n\n/** Read one immutable turn, rejecting a missing receipt rather than service. */\nexport async function creditDebitAttributionForTurn(\n db: Database,\n input: { accountId: string; workspaceId: string; turnId: string },\n): Promise<CreditDebitAttribution> {\n return withRlsContext(db, input, async (tx) => {\n const [turn] = await rawRows<{ initiatingHumanSubjectId: string | null }>(\n tx,\n sql`SELECT initiating_human_subject_id AS \"initiatingHumanSubjectId\"\n FROM session_turns WHERE account_id=${input.accountId}::uuid\n AND workspace_id=${input.workspaceId}::uuid AND id=${input.turnId}::uuid`,\n );\n if (!turn) throw new Error(\"Paid credit debit initiating turn is unavailable\");\n return CreditDebitAttribution.parse({\n kind: \"turn\",\n turnId: input.turnId,\n initiatingHumanSubjectId: turn.initiatingHumanSubjectId,\n });\n });\n}\n\n/**\n * Trusted admission facts, not an access grant. Unknown is deliberately\n * distinct from pure service: old jobs/leases cannot prove a human initiator.\n */\nexport const CreditDebitAttribution = z.discriminatedUnion(\"kind\", [\n z.object({\n kind: z.literal(\"turn\"),\n turnId: z.uuid(),\n initiatingHumanSubjectId: z.string().min(1).max(1024).nullable(),\n }),\n z.object({\n kind: z.literal(\"human\"),\n initiatingHumanSubjectId: z.string().min(1).max(1024),\n }),\n z.object({ kind: z.literal(\"service\") }),\n z.object({ kind: z.literal(\"unknown\") }),\n]);\nexport type CreditDebitAttribution = z.infer<typeof CreditDebitAttribution>;\n\nexport function creditDebitAttributionMetadata(\n attribution: CreditDebitAttribution,\n): Record<string, string> {\n switch (attribution.kind) {\n case \"turn\":\n return {\n turnId: attribution.turnId,\n ...(attribution.initiatingHumanSubjectId\n ? { initiatingHumanSubjectId: attribution.initiatingHumanSubjectId }\n : {}),\n };\n case \"human\":\n return { initiatingHumanSubjectId: attribution.initiatingHumanSubjectId };\n case \"service\":\n return {};\n case \"unknown\":\n throw new Error(\"Paid credit debit has no frozen initiating attribution\");\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OAKK;AACP,SAAS,KAAK,IAAI,SAAS,OAAAA,YAAW;;;ACbtC,SAAS,OAAAC,YAAW;;;ACCpB,SAAS,yBAAyB;AAClC,SAAS,WAAW;AAGpB,IAAM,gCAAgC,IAAI,kBAA0C;AAE7E,SAAS,gCAAwD;AACtE,SAAO,8BAA8B,SAAS,KAAK,EAAE,MAAM,UAAU;AACvE;AAGO,SAAS,2BACd,aACA,IACY;AACZ,SAAO,8BAA8B;AAAA,IACnC,OAAO,OAAO,uBAAuB,MAAM,WAAW,CAAC;AAAA,IACvD;AAAA,EACF;AACF;AAGA,eAAsB,8BACpB,IACA,OACiC;AACjC,SAAO,eAAe,IAAI,OAAO,OAAO,OAAO;AAC7C,UAAM,CAAC,IAAI,IAAI,MAAM;AAAA,MACnB;AAAA,MACA;AAAA,8CACwC,MAAM,SAAS;AAAA,6BAChC,MAAM,WAAW,iBAAiB,MAAM,MAAM;AAAA,IACvE;AACA,QAAI,CAAC,KAAM,OAAM,IAAI,MAAM,kDAAkD;AAC7E,WAAO,uBAAuB,MAAM;AAAA,MAClC,MAAM;AAAA,MACN,QAAQ,MAAM;AAAA,MACd,0BAA0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CAAsC,MAAM,GAAG,GAAK;AAC5E;AAEA,SAAS,kBAAkB,OAAuB;AAChD,SAAO,MAAM,QAAQ,SAAS,GAAG,EAAE,KAAK,EAAE,MAAM,GAAG,IAAK;AAC1D;","names":["sql","sql","sql","sql"]}
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/persistence-errors.ts","../src/database-timing.ts","../src/database.ts"],"sourcesContent":["export type DatabaseFailureCode = \"db_deadlock\" | \"db_serialization_failure\" | \"db_failure\";\n\nexport type PersistenceRetryOutcome = \"not_retryable\" | \"exhausted\";\n\nexport type SafeDatabaseErrorFacts = {\n severity?: string;\n schema?: string;\n table?: string;\n column?: string;\n dataType?: string;\n constraint?: string;\n routine?: string;\n};\n\nexport type PersistenceFailureDetails = {\n code: DatabaseFailureCode;\n sqlState: string | null;\n stage: string;\n eventTypes: string[];\n correlationId: string;\n attempts: number;\n retryOutcome: PersistenceRetryOutcome;\n database: SafeDatabaseErrorFacts;\n};\n\nconst SQLSTATE_KEYS = [\"sqlState\", \"sqlstate\", \"code\"] as const;\nconst NESTED_ERROR_KEYS = [\"cause\", \"original\", \"driverError\", \"error\", \"errors\"] as const;\nconst RETRYABLE_DATABASE_TRANSPORT_CODES = new Set([\n // postgres.js connection lifecycle failures.\n \"CONNECTION_CLOSED\",\n \"CONNECTION_DESTROYED\",\n \"CONNECTION_ENDED\",\n \"CONNECT_TIMEOUT\",\n // Node socket/DNS failures surfaced unchanged by postgres.js.\n \"EAI_AGAIN\",\n \"ECONNABORTED\",\n \"ECONNREFUSED\",\n \"ECONNRESET\",\n \"EHOSTDOWN\",\n \"EHOSTUNREACH\",\n \"ENETDOWN\",\n \"ENETRESET\",\n \"ENETUNREACH\",\n \"ENOTFOUND\",\n \"EPIPE\",\n \"ETIMEDOUT\",\n]);\nconst DATABASE_ERROR_NAMES = new Set([\"DatabaseError\", \"DrizzleQueryError\", \"PostgresError\"]);\nconst DATABASE_DIAGNOSTIC_KEYS = [\n \"severity\",\n \"schema_name\",\n \"table_name\",\n \"column_name\",\n \"data_type_name\",\n \"constraint_name\",\n \"routine\",\n] as const;\nconst SAFE_FACT_KEYS = [\n [\"severity\", \"severity\"],\n [\"schema_name\", \"schema\"],\n [\"schema\", \"schema\"],\n [\"table_name\", \"table\"],\n [\"table\", \"table\"],\n [\"column_name\", \"column\"],\n [\"column\", \"column\"],\n [\"data_type_name\", \"dataType\"],\n [\"dataType\", \"dataType\"],\n [\"constraint_name\", \"constraint\"],\n [\"constraint\", \"constraint\"],\n [\"routine\", \"routine\"],\n] as const;\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return Boolean(value && typeof value === \"object\");\n}\n\nfunction safeFact(value: unknown): string | undefined {\n if (typeof value !== \"string\" || value.length === 0) return undefined;\n return value;\n}\n\n/** Find the driver SQLSTATE even when Drizzle wrapped it under nested causes. */\nexport function nestedPostgresSqlState(error: unknown): string | null {\n const queue: unknown[] = [error];\n const seen = new Set<unknown>();\n let fallback: string | null = null;\n while (queue.length > 0 && seen.size < 64) {\n const current = queue.shift();\n if (!isRecord(current) || seen.has(current)) continue;\n seen.add(current);\n for (const key of SQLSTATE_KEYS) {\n const value = current[key];\n if (typeof value !== \"string\" || !/^[0-9A-Z]{5}$/i.test(value)) continue;\n const normalized = value.toUpperCase();\n // Node transport codes such as EPIPE happen to be five characters but\n // are not PostgreSQL SQLSTATEs.\n if (RETRYABLE_DATABASE_TRANSPORT_CODES.has(normalized)) continue;\n if (normalized === \"40P01\" || normalized === \"40001\") return normalized;\n fallback ??= normalized;\n }\n for (const key of NESTED_ERROR_KEYS) {\n const nested = current[key];\n if (Array.isArray(nested)) queue.push(...nested);\n else if (nested !== undefined) queue.push(nested);\n }\n }\n return fallback;\n}\n\nexport function databaseFailureCode(sqlState: string | null): DatabaseFailureCode {\n if (sqlState === \"40P01\") return \"db_deadlock\";\n if (sqlState === \"40001\") return \"db_serialization_failure\";\n return \"db_failure\";\n}\n\nexport function isRetryablePersistenceSqlState(sqlState: string | null): boolean {\n return sqlState === \"40P01\" || sqlState === \"40001\";\n}\n\n/**\n * Recognize only explicit postgres.js/Node transport codes, including nested\n * driver causes. These failures can arrive as plain Errors with no SQLSTATE or\n * database diagnostic fields when the connection dies before PostgreSQL can\n * answer. Messages are intentionally ignored: they are unstable and may\n * contain connection detail.\n */\nexport function isRetryableDatabaseTransportFailure(error: unknown): boolean {\n const queue: unknown[] = [error];\n const seen = new Set<unknown>();\n while (queue.length > 0 && seen.size < 64) {\n const current = queue.shift();\n if (!isRecord(current) || seen.has(current)) continue;\n seen.add(current);\n\n for (const key of [\"code\", \"errno\"] as const) {\n const value = current[key];\n if (\n typeof value === \"string\" &&\n RETRYABLE_DATABASE_TRANSPORT_CODES.has(value.toUpperCase())\n ) {\n return true;\n }\n }\n\n for (const key of NESTED_ERROR_KEYS) {\n const nested = current[key];\n if (Array.isArray(nested)) queue.push(...nested);\n else if (nested !== undefined) queue.push(nested);\n }\n }\n return false;\n}\n\n/**\n * Distinguish database/ORM failures from expected domain exceptions when a\n * driver omitted SQLSTATE. This checks shape only; callers retain the original\n * failure independently as canonical error evidence.\n */\nexport function isDatabasePersistenceFailure(error: unknown): boolean {\n if (isRetryableDatabaseTransportFailure(error) || nestedPostgresSqlState(error) !== null) {\n return true;\n }\n\n const queue: unknown[] = [error];\n const seen = new Set<unknown>();\n while (queue.length > 0 && seen.size < 64) {\n const current = queue.shift();\n if (!isRecord(current) || seen.has(current)) continue;\n seen.add(current);\n\n if (typeof current.name === \"string\" && DATABASE_ERROR_NAMES.has(current.name)) {\n return true;\n }\n if (\n typeof current.query === \"string\" &&\n (Object.hasOwn(current, \"params\") || Object.hasOwn(current, \"parameters\"))\n ) {\n return true;\n }\n if (DATABASE_DIAGNOSTIC_KEYS.some((key) => Object.hasOwn(current, key))) {\n return true;\n }\n\n for (const key of NESTED_ERROR_KEYS) {\n const nested = current[key];\n if (Array.isArray(nested)) queue.push(...nested);\n else if (nested !== undefined) queue.push(nested);\n }\n }\n return false;\n}\n\n/** Extract only PostgreSQL diagnostic identifiers; never query text/parameters. */\nexport function safeDatabaseErrorFacts(error: unknown): SafeDatabaseErrorFacts {\n const queue: unknown[] = [error];\n const seen = new Set<unknown>();\n const facts: SafeDatabaseErrorFacts = {};\n while (queue.length > 0 && seen.size < 64) {\n const current = queue.shift();\n if (!isRecord(current) || seen.has(current)) continue;\n seen.add(current);\n for (const [source, destination] of SAFE_FACT_KEYS) {\n if (facts[destination] !== undefined) continue;\n const value = safeFact(current[source]);\n if (value !== undefined) facts[destination] = value;\n }\n for (const key of NESTED_ERROR_KEYS) {\n const nested = current[key];\n if (Array.isArray(nested)) queue.push(...nested);\n else if (nested !== undefined) queue.push(nested);\n }\n }\n return facts;\n}\n\n/**\n * Typed persistence classification that retains the exact original failure as\n * `cause` for internal diagnostics. The ordinary Error message is deliberately\n * stable and source-free so a generic presentation path can never disclose SQL,\n * parameters, or driver detail by rendering `.message`.\n */\nexport class SessionEventPersistenceError extends Error {\n readonly name = \"SessionEventPersistenceError\";\n\n constructor(\n readonly details: PersistenceFailureDetails,\n cause?: unknown,\n ) {\n const label =\n details.code === \"db_deadlock\"\n ? \"Database deadlock\"\n : details.code === \"db_serialization_failure\"\n ? \"Database serialization failure\"\n : \"Database failure\";\n const operation = `${label} while persisting ${details.eventTypes.join(\", \") || \"session events\"}`;\n super(operation, cause === undefined ? undefined : { cause });\n }\n\n get code(): DatabaseFailureCode {\n return this.details.code;\n }\n}\n\nexport function isSessionEventPersistenceError(\n error: unknown,\n): error is SessionEventPersistenceError {\n return error instanceof SessionEventPersistenceError;\n}\n\nexport type IdempotentPersistenceTransactionOptions = {\n stage: string;\n eventTypes?: string[];\n maxAttempts?: number;\n correlationId?: string;\n onRetry?: (input: { attempt: number; sqlState: \"40P01\" | \"40001\" }) => void | Promise<void>;\n};\n\n/**\n * Retry only the supplied idempotent database transaction/savepoint. Provider\n * inference, tools, NATS, and all other external effects must remain outside\n * this function. A single correlation ID follows every persistence attempt.\n */\nexport async function runIdempotentPersistenceTransaction<T>(\n options: IdempotentPersistenceTransactionOptions,\n transaction: (attempt: number) => Promise<T>,\n): Promise<T> {\n const maxAttempts = options.maxAttempts ?? 3;\n if (!Number.isInteger(maxAttempts) || maxAttempts < 1) {\n throw new Error(\"Persistence maxAttempts must be a positive integer\");\n }\n const correlationId = options.correlationId ?? crypto.randomUUID();\n const eventTypes = [...new Set(options.eventTypes ?? [])].sort();\n for (let attempt = 1; attempt <= maxAttempts; attempt += 1) {\n try {\n return await transaction(attempt);\n } catch (error) {\n const sqlState = nestedPostgresSqlState(error);\n const retryable = isRetryablePersistenceSqlState(sqlState);\n if (retryable && attempt < maxAttempts) {\n await options.onRetry?.({\n attempt,\n sqlState: sqlState as \"40P01\" | \"40001\",\n });\n continue;\n }\n if (!isDatabasePersistenceFailure(error)) throw error;\n throw new SessionEventPersistenceError(\n {\n code: databaseFailureCode(sqlState),\n sqlState,\n stage: options.stage,\n eventTypes,\n correlationId,\n attempts: attempt,\n retryOutcome: retryable ? \"exhausted\" : \"not_retryable\",\n database: safeDatabaseErrorFacts(error),\n },\n error,\n );\n }\n }\n throw new Error(\"Unreachable persistence retry state\");\n}\n","import { AsyncLocalStorage } from \"node:async_hooks\";\n\nexport type DatabaseTimingStage =\n | \"transaction_admission\"\n | \"savepoint_admission\"\n | \"rls_setup\"\n | \"scoped_callback\";\nexport type DatabaseTimingObservation = {\n stage: DatabaseTimingStage;\n outcome: \"completed\" | \"failed\";\n durationMs: number;\n};\nexport type DatabaseTimingObserver = (observation: DatabaseTimingObservation) => void;\n\nconst timingObserver = new AsyncLocalStorage<DatabaseTimingObserver>();\n\n/** Opt-in, async-local diagnostics only; no identity or query payload is retained. */\nexport function withDatabaseTimingObserver<T>(\n observer: DatabaseTimingObserver,\n work: () => Promise<T>,\n): Promise<T> {\n return timingObserver.run(observer, work);\n}\n\nexport function startDatabaseTiming(\n stage: DatabaseTimingStage,\n): (outcome: DatabaseTimingObservation[\"outcome\"]) => void {\n const observer = timingObserver.getStore();\n if (!observer) return () => undefined;\n const started = performance.now();\n let finished = false;\n return (outcome) => {\n if (finished) return;\n finished = true;\n try {\n // Do not await observers or let rejected async observers escape either.\n const result = observer({ stage, outcome, durationMs: performance.now() - started });\n void Promise.resolve(result).catch(() => undefined);\n } catch {\n // Diagnostics must not replace database results or the original error.\n }\n };\n}\n","import type { SessionAttachmentReadAccess } from \"./session-file-attachments\";\nimport { AsyncLocalStorage } from \"node:async_hooks\";\nimport { eq, sql, type SQL } from \"drizzle-orm\";\nimport type { PgDatabase, PgTransactionConfig } from \"drizzle-orm/pg-core\";\nimport { drizzle } from \"drizzle-orm/postgres-js\";\nimport postgres from \"postgres\";\n\nimport {\n runIdempotentPersistenceTransaction,\n type IdempotentPersistenceTransactionOptions,\n} from \"./persistence-errors\";\nimport { LOSSLESS_CONTENT_WRITER_APPLICATION_NAME } from \"./lossless-json\";\nimport * as schema from \"./schema\";\nimport { startDatabaseTiming } from \"./database-timing\";\n\n// §7.7 driver widening (Step I). `Database` is the structural, cross-driver\n// query-layer port: every helper in this file accepts `db: Database` and uses\n// only the methods present on drizzle's base `PgDatabase` (select/insert/update/\n// delete/transaction/execute). Widening from the concrete\n// `PostgresJsDatabase<typeof schema>` to `PgDatabase<any, typeof schema>` is a\n// pure TYPE change — no runtime behavior changes — that lets an embedded host\n// inject ANY drizzle pg driver handle (node-postgres, neon-http, etc.) bound to\n// OpenGeni's schema, not just the postgres-js handle `createDb` builds. The\n// `any` for the query-result HKT is deliberate: it keeps `db.execute(sql\\`…\\`)`\n// callable across drivers whose raw-result shapes differ (postgres-js returns a\n// row array; node-postgres returns `{ rows }`). The three raw `db.execute(…)`\n// reads that index a row array (`getManagedUserByEmail` here is the only\n// host-facing one — see `userLookup`) stay postgres-js-shaped for standalone;\n// `userLookup` is the injection seam for hosts on a different driver.\n// `PostgresJsDatabase<typeof schema>` is assignable to this, so standalone is\n// unaffected.\n\nexport type Database = PgDatabase<any, typeof schema>;\n\ndeclare const sessionActivityDatabaseBrand: unique symbol;\n\n/**\n * A workspace-scoped transaction whose session activity commit gate is open.\n * Session-row writers accept this narrower handle so new call sites cannot\n * accidentally bypass the once-per-transaction revision finalizer.\n */\nexport type SessionActivityDatabase = Database & {\n readonly [sessionActivityDatabaseBrand]: true;\n};\n\nexport type DbClient = {\n db: Database;\n close: () => Promise<void>;\n};\n\nexport type RlsContext = {\n accountId: string;\n workspaceId?: string | null;\n};\n\nexport type SessionRlsActorContext = {\n /** Server-authorized session attachment scope; never taken from request JSON. */\n sessionAttachmentReadAccess?: SessionAttachmentReadAccess;\n subjectId: string;\n /** Host-verified personal file ownership, never a request-supplied subject. */\n privateFileOwnerSubjectId?: string | null;\n initiatingHumanSubjectId?: string | null;\n};\n\nconst sessionRlsActorContext = new AsyncLocalStorage<SessionRlsActorContext>();\nexport function currentSessionAttachmentReadAccess(): SessionAttachmentReadAccess | undefined {\n return sessionRlsActorContext.getStore()?.sessionAttachmentReadAccess;\n}\n\n/**\n * Stable identity of the ambient session RLS actor: every field that\n * `setRlsContext` turns into database-visible GUCs or that scoped reads\n * consult. Two callers with equal keys see exactly the same rows, so the key\n * is safe for sharing a read result between them; `null` means no actor, in\n * which case the subject GUC stays empty for every such caller.\n */\nexport function currentSessionRlsActorIdentityKey(): string | null {\n const actor = sessionRlsActorContext.getStore();\n if (!actor) return null;\n return JSON.stringify([\n actor.subjectId,\n actor.initiatingHumanSubjectId ?? null,\n actor.privateFileOwnerSubjectId ?? null,\n actor.sessionAttachmentReadAccess ?? null,\n ]);\n}\n\nexport async function withSessionRlsActorContext<T>(\n actor: SessionRlsActorContext,\n fn: () => Promise<T>,\n): Promise<T> {\n if (!actor.subjectId.trim()) {\n throw new Error(\"withSessionRlsActorContext: a non-empty subjectId is required\");\n }\n if (\n actor.initiatingHumanSubjectId !== undefined &&\n actor.initiatingHumanSubjectId !== null &&\n !actor.initiatingHumanSubjectId.trim()\n ) {\n throw new Error(\n \"withSessionRlsActorContext: initiatingHumanSubjectId must be null or non-empty\",\n );\n }\n if (\n actor.privateFileOwnerSubjectId !== undefined &&\n actor.privateFileOwnerSubjectId !== null &&\n !actor.privateFileOwnerSubjectId.trim()\n ) {\n throw new Error(\n \"withSessionRlsActorContext: privateFileOwnerSubjectId must be null or non-empty\",\n );\n }\n return await sessionRlsActorContext.run(actor, fn);\n}\n\ntype RlsContextSettings = {\n accountId: string;\n workspaceId: string;\n subjectId: string;\n privateFileOwnerSubjectId: string;\n initiatingHumanSubjectId: string;\n personalResourceHumanSubjectId: string;\n personalResourceActorSubjectId: string;\n};\n\n/**\n * RLS posture for the connection OpenGeni's query layer runs over (Step I, §7.7).\n *\n * - `\"force\"` (DEFAULT — today's standalone behavior, byte-for-byte): OpenGeni\n * connects as a NON-OWNER role (`opengeni_app`) and every table carries\n * `FORCE ROW LEVEL SECURITY`, so the workspace/account GUCs set by\n * `setRlsContext` are the ONLY thing that admits rows — even the table owner\n * is subject to RLS. This is the Fork-A isolation guarantee.\n * - `\"scoped\"` (embedded Fork-B opt-in): the host runs OpenGeni's queries over a\n * role that OWNS the dedicated schema (RLS need not be forced for that role),\n * relying on the host's own tenant boundary. OpenGeni STILL emits the\n * `set_config('opengeni.account_id'/'workspace_id', …)` GUCs defensively on\n * every scoped query, so the application query path is byte-identical between\n * the two strategies and the app code is RLS-mode-agnostic. The strategy is a\n * declared posture (consumed by `provisionRoles` and as a documented\n * invariant), NOT a query-path branch — there is deliberately no `if\n * (strategy === …)` anywhere in the helpers below. Picking `\"scoped\"` does not\n * relax any GUC; it only changes which DB role the host provisions/connects as\n * and asserts that the host accepts owning the isolation boundary.\n */\nexport type RlsStrategy = \"force\" | \"scoped\";\n\n/**\n * Resolve a host-IdP/Better-Auth user *identifier* by email. Injected via\n * `createDb({ userLookup })` (Step I). UNSET → today's raw parameterized select\n * against Better Auth's `auth_users` table (see `getManagedUserByEmail`), which\n * relies on the postgres-js array-shaped `db.execute` result. An embedded host\n * whose identity lives elsewhere (a different IdP table, a different driver, or\n * a non-`auth_users` user store) injects this closure so OpenGeni never touches\n * `auth_users` directly. Returns the user id, or null when no such user exists.\n */\nexport type UserLookup = (db: Database, email: string) => Promise<string | null>;\n\nexport type ManagedUserProfile = {\n id: string;\n name: string | null;\n email: string;\n};\n\n/** Host identity projection used by organization administration rosters. */\nexport type UserProfileLookup = (\n db: Database,\n userIds: readonly string[],\n) => Promise<ManagedUserProfile[]>;\n\nexport type CreateDbOptions = {\n /**\n * The Postgres `search_path` for this connection (Step I, §7.8 runtime half).\n * UNSET → today's behavior: NO `search_path` startup parameter is sent, so the\n * server default applies (`public` for standalone, where every table + the\n * `vector` extension + `gen_random_uuid()` live). For an embedded dedicated\n * schema, pass e.g. `\"opengeni,opengeni_private,public\"` — postgres-js sends\n * it as a per-session startup parameter (the supported, query-param-free way;\n * URL `?search_path=` is IGNORED by postgres-js). Keep `public` LAST so the\n * `vector` type and `gen_random_uuid()` (which live in `public` on the\n * pgvector image) still resolve — the schema-isolation contract live footgun.\n */\n searchPath?: string;\n /** RLS posture; defaults to `\"force\"` (today's standalone). */\n rlsStrategy?: RlsStrategy;\n /** Host-provided user-by-email resolver; unset → today's raw `auth_users` query. */\n userLookup?: UserLookup;\n /** Host-provided user profile projection; unset → standalone `auth_users`. */\n userProfileLookup?: UserProfileLookup;\n /** postgres-js pool size; defaults to today's `10`. */\n max?: number;\n /**\n * Connection-local default transaction isolation sent in the postgres-js\n * startup parameters. This is intentionally not a role/database default:\n * tests and embedded callers can exercise a different ambient isolation\n * without mutating a shared PostgreSQL role or affecting other connections.\n */\n isolationLevel?: postgres.ConnectionParameters[\"default_transaction_isolation\"];\n};\n\n/**\n * The active RLS strategy + userLookup for an injected `Database`, recorded in a\n * side WeakMap so helpers (and `getManagedUserByEmail`) can consult the host's\n * binding without changing every call signature. A handle with no recorded\n * config (e.g. one built outside `createDb`, or in a test) falls back to the\n * standalone defaults: `rlsStrategy: \"force\"`, raw `auth_users` lookup.\n */\ntype DbBinding = {\n rlsStrategy: RlsStrategy;\n userLookup?: UserLookup;\n userProfileLookup?: UserProfileLookup;\n};\n\nconst dbBindings = new WeakMap<object, DbBinding>();\n\n/** The strategy bound to a handle (or the `\"force\"` default). */\nexport function rlsStrategyFor(db: Database): RlsStrategy {\n return dbBindings.get(db as unknown as object)?.rlsStrategy ?? \"force\";\n}\n\n/**\n * Run a raw SQL query and read its rows as a typed array.\n *\n * Why this exists: the Step I driver widening (`Database = PgDatabase<any, …>`)\n * deliberately sets the query-result HKT to `any` so `db.execute(…)` is callable\n * across drivers whose raw-result shapes differ (postgres-js → row array;\n * node-postgres → `{ rows }`). A side effect is that `db.execute<T>(…)` now\n * resolves to `any`, erasing the per-row element type at the call site. OpenGeni's\n * OWN internal raw queries usually run over the postgres-js handle `createDb`\n * builds (array result), while an embedded host may inject a node-postgres style\n * driver (`{ rows }`). Normalize those two standard shapes in one place; reject\n * an unknown driver result rather than silently treating it as an empty query.\n */\nexport async function rawRows<T extends Record<string, unknown>>(\n executor: Pick<Database, \"execute\">,\n query: SQL,\n): Promise<T[]> {\n const result = await executor.execute<T>(query);\n if (Array.isArray(result)) {\n return result as unknown as T[];\n }\n const rows = (result as unknown as { rows?: unknown }).rows;\n if (Array.isArray(rows)) {\n return rows as T[];\n }\n throw new Error(\"Unsupported database execute() result shape\");\n}\n\nexport type SandboxProviderReadLockIdentity = {\n workspaceId: string;\n sandboxGroupId: string;\n leaseEpoch: number;\n instanceId: string;\n};\n\nexport class SandboxProviderReadLockUnavailableError extends Error {\n constructor() {\n super(\"Sandbox provider reads are temporarily busy. Retry the request.\");\n this.name = \"SandboxProviderReadLockUnavailableError\";\n }\n}\n\nconst SANDBOX_PROVIDER_READ_LOCK_WAIT_MS = 15_000;\nconst SANDBOX_PROVIDER_READ_LOCK_POLL_MAX_MS = 200;\n\nfunction sandboxProviderReadLockKey(identity: SandboxProviderReadLockIdentity): string {\n return [\n \"sandbox-provider-read\",\n identity.workspaceId,\n identity.sandboxGroupId,\n identity.leaseEpoch,\n identity.instanceId,\n ].join(\":\");\n}\n\nasync function waitForSandboxProviderReadLockPoll(\n delayMs: number,\n signal?: AbortSignal | undefined,\n): Promise<void> {\n if (signal?.aborted) {\n throw signal.reason ?? new DOMException(\"The operation was aborted\", \"AbortError\");\n }\n await new Promise<void>((resolve, reject) => {\n let settled = false;\n let timer: ReturnType<typeof setTimeout>;\n const cleanup = () => signal?.removeEventListener(\"abort\", abort);\n const finish = () => {\n if (settled) return;\n settled = true;\n cleanup();\n resolve();\n };\n const abort = () => {\n if (settled) return;\n settled = true;\n clearTimeout(timer);\n cleanup();\n reject(signal?.reason ?? new DOMException(\"The operation was aborted\", \"AbortError\"));\n };\n timer = setTimeout(finish, delayMs);\n signal?.addEventListener(\"abort\", abort, { once: true });\n if (signal?.aborted) abort();\n });\n}\n\n/**\n * Cross-process exclusive boundary for one exact live provider instance's\n * API-direct read request. Modal accepts concurrent commands from one attached\n * handle, but independent API replicas reconstruct different handles; issuing\n * overlapping command groups through those handles can make an otherwise valid\n * read fail transiently. Keep concurrency inside one batched request while\n * serializing only separate requests that target the same lease identity.\n *\n * The callback intentionally runs while one transaction-pinned DB connection\n * holds the advisory lock. The direct lease holder separately prevents drain,\n * and the abort-aware bounded poll prevents a disconnected HTTP request from\n * occupying the connection indefinitely.\n */\nexport async function withSandboxProviderReadLock<T>(\n db: Database,\n identity: SandboxProviderReadLockIdentity,\n signal: AbortSignal | undefined,\n fn: () => Promise<T>,\n): Promise<T> {\n const key = sandboxProviderReadLockKey(identity);\n const deadline = Date.now() + SANDBOX_PROVIDER_READ_LOCK_WAIT_MS;\n return await db.transaction(async (lockedDb) => {\n let pollMs = 20;\n while (true) {\n if (signal?.aborted) {\n throw signal.reason ?? new DOMException(\"The operation was aborted\", \"AbortError\");\n }\n const [row] = await rawRows<{ acquired: boolean }>(\n lockedDb,\n sql`select pg_try_advisory_xact_lock(hashtextextended(${key}, 0)) as acquired`,\n );\n if (row?.acquired === true) return await fn();\n const remainingMs = deadline - Date.now();\n if (remainingMs <= 0) throw new SandboxProviderReadLockUnavailableError();\n await waitForSandboxProviderReadLockPoll(Math.min(pollMs, remainingMs), signal);\n pollMs = Math.min(pollMs * 2, SANDBOX_PROVIDER_READ_LOCK_POLL_MAX_MS);\n }\n });\n}\n\nexport function createDb(databaseUrl: string, options: CreateDbOptions = {}): DbClient {\n // `prepare: false` is REQUIRED for Azure Database for PostgreSQL Flexible\n // Server's transaction-pooling PgBouncer: postgres-js's default named prepared\n // statements (`s_N`) are bound to one backend, but a transaction pooler hands\n // each transaction a different backend, so a later `execute` intermittently\n // throws `prepared statement \"s_N\" does not exist`. Every RLS read in this\n // module (set_config + SELECT inside one db.transaction) rides on this pool, so\n // the failure surfaces as a \"worked, then didn't\" credential/permission read.\n // idle_timeout + max_lifetime recycle connections so a pooler-recycled backend\n // is never reused indefinitely; application_name aids server-side diagnostics.\n const client = postgres(databaseUrl, {\n max: options.max ?? 10,\n prepare: false,\n idle_timeout: 30,\n max_lifetime: 1800,\n // `connection` carries per-session Postgres STARTUP parameters. The exact\n // `application_name` is also the PgBouncer-compatible current-image receipt\n // for migration 0352's restrictive sessions policy; arbitrary custom startup\n // parameters are not portable through transaction poolers. `search_path`\n // (embedded only) is the supported, query-param-free way to scope a connection\n // to a dedicated schema — postgres-js IGNORES a URL `?search_path=`. Unset\n // searchPath → omit it so the server default (`public`) is unchanged for\n // standalone.\n connection: {\n application_name: LOSSLESS_CONTENT_WRITER_APPLICATION_NAME,\n ...(options.searchPath ? { search_path: options.searchPath } : {}),\n ...(options.isolationLevel ? { default_transaction_isolation: options.isolationLevel } : {}),\n },\n });\n const db = drizzle(client, { schema });\n dbBindings.set(db as unknown as object, {\n rlsStrategy: options.rlsStrategy ?? \"force\",\n ...(options.userLookup ? { userLookup: options.userLookup } : {}),\n ...(options.userProfileLookup ? { userProfileLookup: options.userProfileLookup } : {}),\n });\n return {\n db,\n close: async () => {\n await client.end();\n },\n };\n}\n\n/**\n * Register a host's `rlsStrategy`/`userLookup` against an externally-constructed\n * `Database` handle (e.g. one the embedded host built from its own driver and\n * injected, rather than via `createDb`). Lets the same WeakMap-backed lookups\n * work for injected handles. Standalone never calls this (it uses `createDb`).\n */\nexport function registerDbBinding(\n db: Database,\n binding: {\n rlsStrategy?: RlsStrategy;\n userLookup?: UserLookup;\n userProfileLookup?: UserProfileLookup;\n },\n): void {\n dbBindings.set(db as unknown as object, {\n rlsStrategy: binding.rlsStrategy ?? \"force\",\n ...(binding.userLookup ? { userLookup: binding.userLookup } : {}),\n ...(binding.userProfileLookup ? { userProfileLookup: binding.userProfileLookup } : {}),\n });\n}\n\nexport async function setRlsContext(db: Database, context: RlsContext): Promise<void> {\n // Fail loud on an empty/blank account id: a \"\" account would set an RLS GUC\n // that matches no tenant row, silently returning zero rows from every scoped\n // read (a phantom \"not found\" / \"no active subscription\"). An RLS context with\n // no account is always a bug at the call site, never a valid query scope.\n if (typeof context.accountId !== \"string\" || context.accountId.trim() === \"\") {\n throw new Error(\"setRlsContext: a non-empty accountId is required to establish an RLS context\");\n }\n // Transaction-local writer identity covers supported injected/embedded\n // database handles whose connection-level application_name is host-owned.\n // Standalone createDb connections also carry version receipts in their exact\n // application_name, while old OpenGeni binaries set neither current receipt.\n await db.execute(sql`select\n set_config('opengeni.account_id', ${context.accountId}, true),\n set_config('opengeni.workspace_id', ${context.workspaceId ?? \"\"}, true),\n set_config('opengeni.lossless_content_writer', '1', true),\n set_config('opengeni.sandbox_recovery_protocol_v2', '1', true),\n set_config('opengeni.pending_tool_event_output_v1', '1', true),\n set_config('opengeni.session_variable_set_attachments_v1', '1', true)`);\n const sessionActor = sessionRlsActorContext.getStore();\n if (sessionActor) {\n await setSubjectRlsContext(db, sessionActor.subjectId);\n await db.execute(\n sql`select set_config('opengeni.private_file_owner', ${sessionActor.privateFileOwnerSubjectId ?? \"\"}, true), set_config(\n 'opengeni.initiating_human_subject_id',\n ${sessionActor.initiatingHumanSubjectId ?? \"\"},\n true\n )`,\n );\n }\n}\n\nasync function readRlsContextSettings(db: Database): Promise<RlsContextSettings> {\n const [settings] = await rawRows<{\n account_id: string | null;\n workspace_id: string | null;\n subject_id: string | null;\n private_file_owner: string | null;\n initiating_human_subject_id: string | null;\n personal_resource_human_subject_id: string | null;\n personal_resource_actor_subject_id: string | null;\n }>(\n db,\n sql`select\n current_setting('opengeni.account_id', true) as account_id,\n current_setting('opengeni.workspace_id', true) as workspace_id,\n current_setting('opengeni.subject_id', true) as subject_id,\n current_setting('opengeni.private_file_owner', true) as private_file_owner,\n current_setting('opengeni.initiating_human_subject_id', true) as initiating_human_subject_id,\n current_setting('opengeni.personal_resource_human_subject_id', true) as personal_resource_human_subject_id,\n current_setting('opengeni.personal_resource_actor_subject_id', true) as personal_resource_actor_subject_id`,\n );\n return {\n accountId: settings?.account_id ?? \"\",\n workspaceId: settings?.workspace_id ?? \"\",\n subjectId: settings?.subject_id ?? \"\",\n privateFileOwnerSubjectId: settings?.private_file_owner ?? \"\",\n initiatingHumanSubjectId: settings?.initiating_human_subject_id ?? \"\",\n personalResourceHumanSubjectId: settings?.personal_resource_human_subject_id ?? \"\",\n personalResourceActorSubjectId: settings?.personal_resource_actor_subject_id ?? \"\",\n };\n}\n\nasync function assertRlsContextApplied(db: Database, context: RlsContext): Promise<void> {\n const applied = await readRlsContextSettings(db);\n const expectedWorkspaceId = context.workspaceId ?? \"\";\n if (applied.accountId !== context.accountId) {\n throw new Error(\n `RLS context not applied on the active backend: expected account ${context.accountId}, got \"${applied.accountId}\"`,\n );\n }\n if (applied.workspaceId !== expectedWorkspaceId) {\n throw new Error(\n `RLS context not applied on the active backend: expected workspace \"${expectedWorkspaceId}\", got \"${applied.workspaceId}\"`,\n );\n }\n}\n\nasync function restoreRlsContextSettings(\n db: Database,\n settings: RlsContextSettings,\n): Promise<void> {\n const [restored] = await rawRows<{\n account_id: string;\n workspace_id: string;\n subject_id: string;\n private_file_owner: string;\n initiating_human_subject_id: string;\n personal_resource_human_subject_id: string;\n personal_resource_actor_subject_id: string;\n }>(\n db,\n sql`select\n set_config('opengeni.account_id', ${settings.accountId}, true) as account_id,\n set_config('opengeni.workspace_id', ${settings.workspaceId}, true) as workspace_id,\n set_config('opengeni.subject_id', ${settings.subjectId}, true) as subject_id,\n set_config('opengeni.private_file_owner', ${settings.privateFileOwnerSubjectId}, true) as private_file_owner,\n set_config('opengeni.initiating_human_subject_id', ${settings.initiatingHumanSubjectId}, true) as initiating_human_subject_id,\n set_config('opengeni.personal_resource_human_subject_id', ${settings.personalResourceHumanSubjectId}, true) as personal_resource_human_subject_id,\n set_config('opengeni.personal_resource_actor_subject_id', ${settings.personalResourceActorSubjectId}, true) as personal_resource_actor_subject_id`,\n );\n if (\n restored?.account_id !== settings.accountId ||\n restored.workspace_id !== settings.workspaceId ||\n restored.subject_id !== settings.subjectId ||\n restored.private_file_owner !== settings.privateFileOwnerSubjectId ||\n restored.initiating_human_subject_id !== settings.initiatingHumanSubjectId ||\n restored.personal_resource_human_subject_id !== settings.personalResourceHumanSubjectId ||\n restored.personal_resource_actor_subject_id !== settings.personalResourceActorSubjectId\n ) {\n throw new Error(\"RLS context could not be restored after a nested scope\");\n }\n}\n\nexport async function withRlsContext<T>(\n db: Database,\n context: RlsContext,\n fn: (db: Database) => Promise<T>,\n transactionConfig?: PgTransactionConfig,\n sessionTenancyFence: \"shared\" | \"none\" = \"shared\",\n): Promise<T> {\n const restoreParentScope = isTransactionHandle(db);\n // Callback entry includes connection admission AND BEGIN/SAVEPOINT round-trip.\n // This is deliberately not advertised as pure pool wait.\n const admission = startDatabaseTiming(\n restoreParentScope ? \"savepoint_admission\" : \"transaction_admission\",\n );\n try {\n return await db.transaction(async (tx) => {\n admission(\"completed\");\n const setup = startDatabaseTiming(\"rls_setup\");\n const scoped = tx as unknown as Database;\n let parentScope: RlsContextSettings | null;\n try {\n parentScope = restoreParentScope ? await readRlsContextSettings(scoped) : null;\n await setRlsContext(scoped, context);\n if (context.workspaceId && sessionTenancyFence === \"shared\") {\n await scoped.execute(\n sql`select pg_advisory_xact_lock_shared(hashtextextended(${`session-tenancy:${context.workspaceId}`}, 0))`,\n );\n }\n // Defense-in-depth: read the LOCAL GUC back on THIS backend BEFORE running\n // the scoped query. The set_config and this read share one db.transaction,\n // which a transaction pooler pins to a single backend — so a mismatch here\n // means the context was genuinely lost (a torn transaction / pooler backend\n // swap), not normal operation. Without this guard such an event runs the\n // scoped read with an empty account_id and returns zero RLS-visible rows,\n // manufacturing a phantom \"no active subscription\" from a credential that is\n // in fact active. Convert that silent false into a loud, root-cause-bearing\n // error so the caller can retry rather than permanently mis-decide.\n await assertRlsContextApplied(scoped, context);\n setup(\"completed\");\n } catch (error) {\n setup(\"failed\");\n throw error;\n }\n const callback = startDatabaseTiming(\"scoped_callback\");\n let value: T;\n try {\n value = await fn(scoped);\n callback(\"completed\");\n } catch (error) {\n callback(\"failed\");\n throw error;\n }\n // A nested transaction is a savepoint, and SET LOCAL survives successful\n // savepoint release. Restore the parent tenant and actor proof together;\n // writer/protocol capabilities intentionally remain transaction-wide.\n if (parentScope) await restoreRlsContextSettings(scoped, parentScope);\n return value;\n }, transactionConfig);\n } catch (error) {\n // No-op after callback entry; only admission failures are counted here.\n admission(\"failed\");\n throw error;\n }\n}\n\n/**\n * Run one bounded database operation on a transaction-pinned backend.\n *\n * Callers that also have an application deadline should check their abort\n * signal before returning from `fn`; throwing there rolls the transaction back\n * even when the application deadline won a surrounding Promise race.\n */\nexport async function withDatabaseStatementTimeout<T>(\n db: Database,\n timeoutMs: number,\n fn: (db: Database) => Promise<T>,\n): Promise<T> {\n if (!Number.isFinite(timeoutMs) || timeoutMs <= 0) {\n throw new Error(\"withDatabaseStatementTimeout requires a positive timeout\");\n }\n const boundedTimeoutMs = Math.max(1, Math.floor(timeoutMs));\n return await db.transaction(async (tx) => {\n const scoped = tx as unknown as Database;\n await scoped.execute(\n sql`select set_config('statement_timeout', ${`${boundedTimeoutMs}ms`}, true)`,\n );\n return await fn(scoped);\n });\n}\n\nexport async function rlsContextForWorkspace(\n db: Database,\n workspaceId: string,\n): Promise<RlsContext> {\n const [row] = await db\n .select({ accountId: schema.workspaces.accountId })\n .from(schema.workspaces)\n .where(eq(schema.workspaces.id, workspaceId))\n .limit(1);\n if (!row) {\n throw new Error(`Workspace not found: ${workspaceId}`);\n }\n return { accountId: row.accountId, workspaceId };\n}\n\nexport async function withWorkspaceRls<T>(\n db: Database,\n workspaceId: string,\n fn: (db: Database) => Promise<T>,\n transactionConfig?: PgTransactionConfig,\n): Promise<T> {\n return await withRlsContext(\n db,\n await rlsContextForWorkspace(db, workspaceId),\n fn,\n transactionConfig,\n );\n}\n\ntype SessionActivityGate = {\n db: SessionActivityDatabase;\n owner: boolean;\n};\n\nfunction isTransactionHandle(db: Database): boolean {\n return typeof (db as Database & { rollback?: unknown }).rollback === \"function\";\n}\n\nasync function assertSessionActivityGateEntry(db: Database): Promise<void> {\n if (!isTransactionHandle(db)) return;\n const [gate] = await rawRows<{ state: string | null }>(\n db,\n sql`\n select nullif(\n current_setting('opengeni.session_activity_gate_state', true),\n ''\n ) as state\n `,\n );\n if (gate?.state === null || gate === undefined) {\n throw new Error(\"Session activity commit gate must start at the outer transaction boundary\");\n }\n}\n\nasync function beginSessionActivityGate(\n db: Database,\n workspaceId: string,\n): Promise<SessionActivityGate> {\n const [gate] = await rawRows<{\n prior_state: string | null;\n prior_workspace_id: string | null;\n }>(\n db,\n sql`\n with current_gate as materialized (\n select\n nullif(current_setting('opengeni.session_activity_gate_state', true), '') as prior_state,\n nullif(current_setting('opengeni.session_activity_gate_workspace_id', true), '') as prior_workspace_id\n )\n select\n prior_state,\n prior_workspace_id,\n case\n when prior_state is null\n then set_config('opengeni.session_activity_gate_state', 'open', true)\n else prior_state\n end as effective_state,\n case\n when prior_state is null\n then set_config('opengeni.session_activity_gate_workspace_id', ${workspaceId}, true)\n else prior_workspace_id\n end as effective_workspace_id\n from current_gate\n `,\n );\n if (!gate) {\n throw new Error(\"Session activity commit gate could not be opened\");\n }\n if (gate.prior_state === null) {\n return { db: db as SessionActivityDatabase, owner: true };\n }\n if (gate.prior_state === \"open\" && gate.prior_workspace_id === workspaceId) {\n return { db: db as SessionActivityDatabase, owner: false };\n }\n throw new Error(\"Session activity commit gate is already owned by another transaction scope\");\n}\n\nasync function finalizeSessionActivityGate(\n db: SessionActivityDatabase,\n workspaceId: string,\n): Promise<void> {\n // Resolve every deferred FK/constraint before taking the shared workspace\n // counter. The only row writes after that point stamp session rows already\n // modified and therefore already owned by this transaction.\n await db.execute(\n sql`select set_config('opengeni.session_activity_gate_state', 'preparing', true)`,\n );\n await db.execute(sql`set constraints all immediate`);\n // The first flush proves every pre-existing deferred obligation. Re-defer\n // only our commit guard so the marker-clearing writes below queue a fresh\n // proof against the final row state, then force that proof before returning.\n await db.execute(\n sql`set constraints sessions_activity_insert_commit_guard, sessions_activity_update_commit_guard deferred`,\n );\n const [result] = await rawRows<{\n had_pending: boolean;\n pending_count: number;\n revision: string | null;\n stamped_count: number;\n }>(\n db,\n sql`\n with gate as materialized (\n select set_config('opengeni.session_activity_gate_state', 'finalizing', true) as state\n ),\n pending as materialized (\n select ${schema.sessions.id}\n from ${schema.sessions}\n cross join gate\n where ${schema.sessions.workspaceId} = ${workspaceId}\n and ${schema.sessions.activityRevisionPendingXid}\n = pg_current_xact_id()::text::bigint\n ),\n advanced as (\n update ${schema.workspaceSessionActivityRevisions} as counter\n set revision = counter.revision + 1\n where counter.workspace_id = ${workspaceId}\n and exists (select 1 from pending)\n returning counter.revision\n ),\n stamped as (\n update ${schema.sessions} as session\n set\n activity_revision = advanced.revision,\n activity_revision_pending_xid = null\n from advanced\n where session.workspace_id = ${workspaceId}\n and session.activity_revision_pending_xid\n = pg_current_xact_id()::text::bigint\n returning session.id\n ),\n stats as materialized (\n select\n exists (select 1 from pending) as had_pending,\n (select count(*)::integer from pending) as pending_count,\n (select revision::text from advanced) as revision,\n (select count(*)::integer from stamped) as stamped_count\n ),\n finalized as materialized (\n select\n had_pending,\n pending_count,\n revision,\n stamped_count,\n set_config('opengeni.session_activity_gate_state', 'finalized', true) as state\n from stats\n )\n select had_pending, pending_count, revision, stamped_count\n from finalized\n `,\n );\n if (!result) {\n throw new Error(\"Session activity commit gate did not return a finalization result\");\n }\n if (result.had_pending && result.revision === null) {\n throw new Error(\"Workspace session activity counter is unavailable\");\n }\n if (result.stamped_count !== result.pending_count) {\n throw new Error(\"Session activity commit gate did not stamp every pending row\");\n }\n await db.execute(\n sql`set constraints sessions_activity_insert_commit_guard, sessions_activity_update_commit_guard immediate`,\n );\n // A restored activity scope can be followed by another workspace scope, or\n // by a SECURITY DEFINER lifecycle that opens and finalizes its own gate in\n // the same outer transaction. Keep the guards deferred after proving this\n // scope's final state so those later writes reach their own finalizer instead\n // of firing the commit guard at the first session update.\n await db.execute(\n sql`set constraints sessions_activity_insert_commit_guard, sessions_activity_update_commit_guard deferred`,\n );\n}\n\n/**\n * Run one workspace transaction with explicit once-per-transaction session\n * activity finalization. Nested callers reuse the outer gate; only its owner\n * finalizes immediately before the enclosing RLS transaction commits.\n */\nexport async function withSessionActivityRlsContext<T>(\n db: Database,\n context: RlsContext & { workspaceId: string },\n fn: (db: SessionActivityDatabase) => Promise<T>,\n transactionConfig?: PgTransactionConfig,\n fenceMode: \"shared\" | \"none\" = \"shared\",\n organizationMembershipFence = false,\n): Promise<T> {\n await assertSessionActivityGateEntry(db);\n return await withRlsContext(\n db,\n context,\n async (scopedDb) => {\n if (organizationMembershipFence) {\n // Claim can inherit membership-fenced authority after locking sessions.\n // Acquire membership before even a shared tenancy fence: an exclusive\n // tenancy/control waiter can otherwise complete the same lock cycle.\n await scopedDb.execute(sql`select pg_advisory_xact_lock(\n hashtextextended(${`organization-membership:${context.accountId}`}, 0))`);\n if (fenceMode === \"shared\") {\n await scopedDb.execute(sql`select pg_advisory_xact_lock_shared(\n hashtextextended(${`session-tenancy:${context.workspaceId}`}, 0))`);\n }\n }\n const gate = await beginSessionActivityGate(scopedDb, context.workspaceId);\n const value = await fn(gate.db);\n // The finalizer must never trust tenant GUCs that arbitrary callback code\n // could have changed. Nested RLS helpers restore their parent scope; this\n // assertion is the fail-closed commit-boundary proof for every other path.\n await assertRlsContextApplied(gate.db, context);\n if (gate.owner) {\n await finalizeSessionActivityGate(gate.db, context.workspaceId);\n }\n return value;\n },\n transactionConfig,\n organizationMembershipFence ? \"none\" : fenceMode,\n );\n}\n\nexport async function withWorkspaceSessionActivityRls<T>(\n db: Database,\n workspaceId: string,\n fn: (db: SessionActivityDatabase) => Promise<T>,\n transactionConfig?: PgTransactionConfig,\n): Promise<T> {\n return await withSessionActivityRlsContext(\n db,\n { ...(await rlsContextForWorkspace(db, workspaceId)), workspaceId },\n fn,\n transactionConfig,\n );\n}\n\n/**\n * Run one of several ordered workspace activity scopes inside a caller-owned\n * transaction. Each scope is fully finalized before restoring the caller's\n * gate GUCs, so the next workspace can reuse the same outer transaction.\n */\nexport async function withRestoredSessionActivityRlsContext<T>(\n db: Database,\n context: RlsContext & { workspaceId: string },\n fn: (db: SessionActivityDatabase) => Promise<T>,\n): Promise<T> {\n if (!isTransactionHandle(db)) {\n throw new Error(\"Sequential workspace activity scopes require a caller-owned transaction\");\n }\n const [prior] = await rawRows<{ state: string; workspace_id: string }>(\n db,\n sql`select\n coalesce(current_setting('opengeni.session_activity_gate_state', true), '') as state,\n coalesce(\n current_setting('opengeni.session_activity_gate_workspace_id', true), ''\n ) as workspace_id`,\n );\n try {\n return await withRlsContext(db, context, async (scopedDb) => {\n const gate = await beginSessionActivityGate(scopedDb, context.workspaceId);\n if (!gate.owner) {\n throw new Error(\"Sequential workspace activity scope unexpectedly reused an open gate\");\n }\n const value = await fn(gate.db);\n await assertRlsContextApplied(gate.db, context);\n await finalizeSessionActivityGate(gate.db, context.workspaceId);\n return value;\n });\n } finally {\n await db.execute(sql`select\n set_config('opengeni.session_activity_gate_state', ${prior?.state ?? \"\"}, true),\n set_config(\n 'opengeni.session_activity_gate_workspace_id', ${prior?.workspace_id ?? \"\"}, true\n )`);\n }\n}\n\n/**\n * Open one nested savepoint without losing the gate brand. Drizzle correctly\n * preserves the backend-local gate GUCs, but its structural transaction type\n * cannot express that provenance; keep the single trusted rebrand here.\n */\nexport async function withSessionActivitySavepoint<T>(\n db: SessionActivityDatabase,\n fn: (db: SessionActivityDatabase) => Promise<T>,\n): Promise<T> {\n return await db.transaction(async (tx) => await fn(tx as unknown as SessionActivityDatabase));\n}\n\nexport async function retrySessionActivityRls<T>(\n db: Database,\n workspaceId: string,\n options: IdempotentPersistenceTransactionOptions & {\n /** Claim can inherit membership-fenced causal authority after locking the\n * session. Acquire that fence before tenancy/control/session instead. An\n * enclosing transaction must preserve this same lock prefix. */\n organizationMembershipFence?: boolean;\n },\n fn: (db: SessionActivityDatabase) => Promise<T>,\n): Promise<T> {\n return await runIdempotentPersistenceTransaction(options, async () => {\n if (options.organizationMembershipFence) {\n const context = { ...(await rlsContextForWorkspace(db, workspaceId)), workspaceId };\n return await withSessionActivityRlsContext(db, context, fn, undefined, \"shared\", true);\n }\n return await withWorkspaceSessionActivityRls(db, workspaceId, fn);\n });\n}\n\nexport async function retryWorkspacePersistence<T>(\n db: Database,\n workspaceId: string,\n options: IdempotentPersistenceTransactionOptions,\n fn: (db: Database) => Promise<T>,\n): Promise<T> {\n return await runIdempotentPersistenceTransaction(options, async () => {\n return await withWorkspaceRls(db, workspaceId, fn);\n });\n}\n\nexport async function retryRlsPersistence<T>(\n db: Database,\n context: RlsContext,\n options: IdempotentPersistenceTransactionOptions,\n fn: (db: Database) => Promise<T>,\n): Promise<T> {\n return await runIdempotentPersistenceTransaction(options, async () => {\n return await withRlsContext(db, context, fn);\n });\n}\n\n/**\n * Personal workspace data needs both tenant and authenticated-principal GUCs.\n * `session_pins` uses this helper so FORCE RLS rejects another member's rows\n * even if a future query accidentally omits its explicit subject predicate.\n */\nexport async function withWorkspaceSubjectRls<T>(\n db: Database,\n workspaceId: string,\n subjectId: string,\n fn: (db: Database) => Promise<T>,\n transactionConfig?: PgTransactionConfig,\n sessionTenancyFence: \"shared\" | \"none\" = \"shared\",\n): Promise<T> {\n if (!subjectId.trim()) {\n throw new Error(\"withWorkspaceSubjectRls: a non-empty subjectId is required\");\n }\n const context = await rlsContextForWorkspace(db, workspaceId);\n return await withRlsContext(\n db,\n context,\n async (scopedDb) => {\n await setSubjectRlsContext(scopedDb, subjectId);\n return await fn(scopedDb);\n },\n transactionConfig,\n sessionTenancyFence,\n );\n}\n\nexport async function withWorkspaceSubjectSessionActivityRls<T>(\n db: Database,\n workspaceId: string,\n subjectId: string,\n fn: (db: SessionActivityDatabase) => Promise<T>,\n transactionConfig?: PgTransactionConfig,\n fenceMode: \"shared\" | \"none\" = \"shared\",\n organizationMembershipFence = false,\n): Promise<T> {\n if (!subjectId.trim()) {\n throw new Error(\"withWorkspaceSubjectSessionActivityRls: a non-empty subjectId is required\");\n }\n const context = await rlsContextForWorkspace(db, workspaceId);\n return await withSessionActivityRlsContext(\n db,\n { ...context, workspaceId },\n async (scopedDb) => {\n await setSubjectRlsContext(scopedDb, subjectId);\n return await fn(scopedDb);\n },\n transactionConfig,\n fenceMode,\n organizationMembershipFence,\n );\n}\n\n/** Apply and verify actor-private RLS on an already transaction-pinned handle. */\nexport async function setSubjectRlsContext(db: Database, subjectId: string): Promise<void> {\n if (!subjectId.trim()) {\n throw new Error(\"setSubjectRlsContext: a non-empty subjectId is required\");\n }\n await db.execute(sql`select set_config('opengeni.subject_id', ${subjectId}, true)`);\n const applied = await db.execute<{ subject_id: string | null }>(\n sql`select current_setting('opengeni.subject_id', true) as subject_id`,\n );\n if ((applied[0]?.subject_id ?? \"\") !== subjectId) {\n throw new Error(\"Authenticated subject RLS context was not applied on the active backend\");\n }\n}\n\nexport async function withWorkspaceUsageLock<T>(\n db: Database,\n workspaceId: string,\n fn: (db: Database) => Promise<T>,\n): Promise<T> {\n const context = await rlsContextForWorkspace(db, workspaceId);\n return await withRlsContext(db, context, async (scopedDb) => {\n await scopedDb.execute(sql`select pg_advisory_xact_lock(hashtext(${`usage:${workspaceId}`}))`);\n return await fn(scopedDb);\n });\n}\n\nexport async function withAccountRls<T>(\n db: Database,\n accountId: string,\n fn: (db: Database) => Promise<T>,\n): Promise<T> {\n return await withRlsContext(db, { accountId, workspaceId: null }, fn);\n}\n\n/** Internal lookup for the host binding attached to a database handle. */\nexport function dbBindingFor(db: Database): DbBinding | undefined {\n return dbBindings.get(db as unknown as object);\n}\n"],"mappings":";;;;;;;;;;;AAyBA,IAAM,gBAAgB,CAAC,YAAY,YAAY,MAAM;AACrD,IAAM,oBAAoB,CAAC,SAAS,YAAY,eAAe,SAAS,QAAQ;AAChF,IAAM,qCAAqC,oBAAI,IAAI;AAAA;AAAA,EAEjD;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA;AAAA,EAEA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,CAAC;AACD,IAAM,uBAAuB,oBAAI,IAAI,CAAC,iBAAiB,qBAAqB,eAAe,CAAC;AAC5F,IAAM,2BAA2B;AAAA,EAC/B;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AACA,IAAM,iBAAiB;AAAA,EACrB,CAAC,YAAY,UAAU;AAAA,EACvB,CAAC,eAAe,QAAQ;AAAA,EACxB,CAAC,UAAU,QAAQ;AAAA,EACnB,CAAC,cAAc,OAAO;AAAA,EACtB,CAAC,SAAS,OAAO;AAAA,EACjB,CAAC,eAAe,QAAQ;AAAA,EACxB,CAAC,UAAU,QAAQ;AAAA,EACnB,CAAC,kBAAkB,UAAU;AAAA,EAC7B,CAAC,YAAY,UAAU;AAAA,EACvB,CAAC,mBAAmB,YAAY;AAAA,EAChC,CAAC,cAAc,YAAY;AAAA,EAC3B,CAAC,WAAW,SAAS;AACvB;AAEA,SAAS,SAAS,OAAkD;AAClE,SAAO,QAAQ,SAAS,OAAO,UAAU,QAAQ;AACnD;AAEA,SAAS,SAAS,OAAoC;AACpD,MAA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