@tearleads/client-sdk 0.1.47 → 0.1.48

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.
@@ -1,9 +1,8 @@
1
- import { and, eq } from "drizzle-orm";
1
+ import { and, eq, lte } from "drizzle-orm";
2
2
  import { normalizeEffectiveAccessLevel } from "../../../accessLevel.js";
3
3
  import { DEFAULT_DOCUMENT_KIND } from "../../../documents/documentConstants.js";
4
4
  import { deriveStoredDocumentTitle } from "../../../documents/documentKinds.js";
5
5
  import { serializeDocumentSyncPullContinuation } from "../../../documents/shared/pullContinuation.js";
6
- import { documentRecordSelection, mapSelectedDocumentRecord, } from "../../../sqlite/documentPersistence.js";
7
6
  import { documentProjection, documentProjectionText, documents, } from "../../../sqlite/schema.js";
8
7
  import { getClientSQLitePersistenceRuntime, } from "../../../sqlite/sqlitePersistenceRuntime.js";
9
8
  import { DOCUMENTS_APP_KIND } from "./constants.js";
@@ -23,15 +22,6 @@ export async function hasDocumentRow(execSql, localId) {
23
22
  .limit(1);
24
23
  return rows.length > 0;
25
24
  }
26
- async function loadDocumentRecordInTransaction(input) {
27
- const { localId, tx } = input;
28
- const rows = await tx
29
- .select(documentRecordSelection)
30
- .from(documents)
31
- .where(and(eq(documents.appKind, DOCUMENTS_APP_KIND), eq(documents.localId, localId)))
32
- .limit(1);
33
- return rows[0] ? mapSelectedDocumentRecord(rows[0]) : null;
34
- }
35
25
  function toDocumentRecordRow(input) {
36
26
  const { document, updatedAt } = input;
37
27
  return {
@@ -113,23 +103,22 @@ export async function createDocumentRowsIfAbsent(input) {
113
103
  }
114
104
  export async function saveDocumentRows(input) {
115
105
  const { document, tx, updatedAt } = input;
116
- const existing = await loadDocumentRecordInTransaction({
117
- localId: document.id,
118
- tx,
119
- });
120
- if (existing &&
121
- (existing.recoveryGeneration ?? 0) > (document.recoveryGeneration ?? 0)) {
122
- return false;
123
- }
124
106
  const documentRow = toDocumentRecordRow({ document, updatedAt });
125
- await tx
107
+ // The recovery-generation fence rides on the upsert: a stored row from a
108
+ // newer recovery generation is left untouched and returns no row, so a
109
+ // stale writer skips the projection writes without a separate read.
110
+ const saved = await tx
126
111
  .insert(documents)
127
112
  .values(documentRow)
128
113
  .onConflictDoUpdate({
129
114
  target: [documents.appKind, documents.localId],
130
115
  set: documentRow,
116
+ setWhere: lte(documents.recoveryGeneration, documentRow.recoveryGeneration),
131
117
  })
132
- .run();
118
+ .returning({ localId: documents.localId });
119
+ if (saved.length === 0) {
120
+ return false;
121
+ }
133
122
  await saveDocumentProjectionRows(input);
134
123
  return true;
135
124
  }
@@ -145,29 +134,36 @@ export async function resolveDocumentSaveTimestamp(input) {
145
134
  if (options?.updatedAt) {
146
135
  return options.updatedAt;
147
136
  }
148
- const [existingRecord, projectionRows] = await Promise.all([
149
- loadDocumentRecordInTransaction({
150
- localId: document.id,
151
- tx,
152
- }),
153
- tx
154
- .select({
155
- documentKind: documentProjection.documentKind,
156
- text: documentProjectionText.text,
157
- title: documentProjection.title,
158
- updatedAt: documentProjection.updatedAt,
159
- })
160
- .from(documentProjection)
161
- .leftJoin(documentProjectionText, eq(documentProjectionText.localId, documentProjection.localId))
162
- .where(eq(documentProjection.localId, document.id))
163
- .limit(1),
164
- ]);
165
- const existingProjection = projectionRows[0];
137
+ // One read for the stored row and its projection; the projection columns
138
+ // are not nullable, so they are null exactly when the projection is absent.
139
+ const [existing] = await tx
140
+ .select({
141
+ snapshotEndVersion: documents.snapshotEndVersion,
142
+ documentKind: documentProjection.documentKind,
143
+ text: documentProjectionText.text,
144
+ title: documentProjection.title,
145
+ updatedAt: documentProjection.updatedAt,
146
+ })
147
+ .from(documents)
148
+ .leftJoin(documentProjection, eq(documentProjection.localId, documents.localId))
149
+ .leftJoin(documentProjectionText, eq(documentProjectionText.localId, documentProjection.localId))
150
+ .where(and(eq(documents.appKind, DOCUMENTS_APP_KIND), eq(documents.localId, document.id)))
151
+ .limit(1);
152
+ const existingProjection = existing?.documentKind != null &&
153
+ existing.title != null &&
154
+ existing.updatedAt != null
155
+ ? {
156
+ documentKind: existing.documentKind,
157
+ text: existing.text,
158
+ title: existing.title,
159
+ updatedAt: existing.updatedAt,
160
+ }
161
+ : undefined;
166
162
  const now = new Date().toISOString();
167
- return didStoredDocumentContentChange(existingRecord
163
+ return didStoredDocumentContentChange(existing
168
164
  ? {
169
165
  documentKind: getProjectionDocumentKind(existingProjection),
170
- snapshotEndVersion: existingRecord.snapshotEndVersion,
166
+ snapshotEndVersion: existing.snapshotEndVersion,
171
167
  text: getProjectionText(existingProjection),
172
168
  title: getProjectionTitle(existingProjection),
173
169
  }
@@ -1 +1 @@
1
- {"version":3,"file":"documentRows.js","sourceRoot":"","sources":["documentRows.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,6BAA6B,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,EAAE,yBAAyB,EAAE,MAAM,kCAAkC,CAAC;AAC7E,OAAO,EAAE,qCAAqC,EAAE,MAAM,4CAA4C,CAAC;AACnG,OAAO,EAGL,uBAAuB,EACvB,yBAAyB,GAC1B,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EACL,kBAAkB,EAClB,sBAAsB,EACtB,SAAS,GACV,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAEL,iCAAiC,GAClC,MAAM,0CAA0C,CAAC;AAGlD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EACL,yBAAyB,EACzB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,0BAA0B,CAAC;AAElC,MAAM,UAAU,gBAAgB,CAAC,OAAe;IAC9C,OAAO;QACL,OAAO,EAAE,kBAAkB;QAC3B,OAAO;KACR,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,cAAc,C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,EACR,QAAQ,CACT;QACC,CAAC,CAAC,GAAG;QACL,CAAC,CAAC,sBAAsB,CAAC,kBAAkB,CAAC,IAAI,GAAG,CAAC;AACxD,CAAC","sourcesContent":["import { and, eq } from \"drizzle-orm\";\nimport { normalizeEffectiveAccessLevel } from \"../../../accessLevel\";\nimport { DEFAULT_DOCUMENT_KIND } from \"../../../documents/documentConstants\";\nimport { deriveStoredDocumentTitle } from \"../../../documents/documentKinds\";\nimport { serializeDocumentSyncPullContinuation } from \"../../../documents/shared/pullContinuation\";\nimport {\n type DocumentRecord as BaseDocumentRecord,\n type DocumentScope,\n documentRecordSelection,\n mapSelectedDocumentRecord,\n} from \"../../../sqlite/documentPersistence\";\nimport {\n documentProjection,\n documentProjectionText,\n documents,\n} from \"../../../sqlite/schema\";\nimport {\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../../sqlite/sqlSchema\";\nimport type { StoredDocumentRecord } from \"../types\";\nimport { DOCUMENTS_APP_KIND } from \"./constants\";\nimport {\n getProjectionDocumentKind,\n getProjectionText,\n getProjectionTitle,\n getProjectionUpdatedAt,\n} from \"./documentProjectionRows\";\n\nexport function getDocumentScope(localId: string): DocumentScope {\n return {\n appKind: DOCUMENTS_APP_KIND,\n localId,\n };\n}\n\nexport async function hasDocumentRow(\n execSql: ExecSql,\n localId: string,\n): Promise<boolean> {\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const rows = await db\n .select({ localId: documents.localId })\n .from(documents)\n .where(\n and(\n eq(documents.appKind, DOCUMENTS_APP_KIND),\n eq(documents.localId, localId),\n ),\n )\n .limit(1);\n return rows.length > 0;\n}\n\nasync function loadDocumentRecordInTransaction(input: {\n localId: string;\n tx: ClientSQLiteTransactionScope;\n}): Promise<BaseDocumentRecord | null> {\n const { localId, tx } = input;\n const rows = await tx\n .select(documentRecordSelection)\n .from(documents)\n .where(\n and(\n eq(documents.appKind, DOCUMENTS_APP_KIND),\n eq(documents.localId, localId),\n ),\n )\n .limit(1);\n\n return rows[0] ? mapSelectedDocumentRecord(rows[0]) : null;\n}\n\nfunction toDocumentRecordRow(input: {\n document: StoredDocumentRecord;\n updatedAt: string;\n}) {\n const { document, updatedAt } = input;\n return {\n appKind: DOCUMENTS_APP_KIND,\n localId: document.id,\n documentId: document.documentId,\n recoveryGeneration: document.recoveryGeneration ?? 0,\n recoveryDocumentId: document.documentId\n ? null\n : (document.recoveryDocumentId ?? null),\n snapshotEndVersion: document.snapshotEndVersion,\n accessEpoch: document.accessEpoch,\n accessStateHash: document.accessStateHash ?? null,\n effectiveAccessLevel: normalizeEffectiveAccessLevel(\n document.effectiveAccessLevel,\n ),\n lastCommitLsn: document.lastCommitLsn ?? null,\n documentManifestBundle: document.documentManifestBundle ?? null,\n contentKeyBundle: document.contentKeyBundle ?? null,\n documentKekTargets: document.documentKekTargets ?? null,\n // Only touch the outgoing-delta marker when the caller manages it. Callers\n // that leave it undefined (e.g. discovery upserts, registration bootstrap)\n // must not clobber a marker a device-first deferRemoteSync write persisted.\n ...(document.pendingBaseVersion === undefined\n ? {}\n : { pendingBaseVersion: document.pendingBaseVersion }),\n ...(document.pullContinuation === undefined\n ? {}\n : {\n pullContinuation: serializeDocumentSyncPullContinuation(\n document.pullContinuation,\n ),\n }),\n updatedAt,\n };\n}\n\nasync function saveDocumentProjectionRows(input: {\n document: StoredDocumentRecord;\n tx: ClientSQLiteTransactionScope;\n updatedAt: string;\n}): Promise<void> {\n const { document, tx, updatedAt } = input;\n const projectionRow = {\n localId: document.id,\n documentId: document.documentId,\n containerId: document.containerId,\n documentKind: document.documentKind ?? DEFAULT_DOCUMENT_KIND,\n title: document.title ?? deriveStoredDocumentTitle(document.text),\n updatedAt,\n };\n await tx\n .insert(documentProjection)\n .values(projectionRow)\n .onConflictDoUpdate({\n target: documentProjection.localId,\n set: projectionRow,\n })\n .run();\n\n const projectionTextRow = {\n localId: document.id,\n text: document.text,\n };\n await tx\n .insert(documentProjectionText)\n .values(projectionTextRow)\n .onConflictDoUpdate({\n target: documentProjectionText.localId,\n set: projectionTextRow,\n })\n .run();\n}\n\nexport async function createDocumentRowsIfAbsent(input: {\n document: StoredDocumentRecord;\n tx: ClientSQLiteTransactionScope;\n updatedAt: string;\n}): Promise<boolean> {\n const { document, tx, updatedAt } = input;\n const documentRow = toDocumentRecordRow({ document, updatedAt });\n const inserted = await tx\n .insert(documents)\n .values(documentRow)\n .onConflictDoNothing({\n target: [documents.appKind, documents.localId],\n })\n .returning({ localId: documents.localId });\n if (inserted.length === 0) {\n return false;\n }\n\n await saveDocumentProjectionRows(input);\n return true;\n}\n\nexport async function saveDocumentRows(input: {\n document: StoredDocumentRecord;\n tx: ClientSQLiteTransactionScope;\n updatedAt: string;\n}): Promise<boolean> {\n const { document, tx, updatedAt } = input;\n const existing = await loadDocumentRecordInTransaction({\n localId: document.id,\n tx,\n });\n if (\n existing &&\n (existing.recoveryGeneration ?? 0) > (document.recoveryGeneration ?? 0)\n ) {\n return false;\n }\n const documentRow = toDocumentRecordRow({ document, updatedAt });\n await tx\n .insert(documents)\n .values(documentRow)\n .onConflictDoUpdate({\n target: [documents.appKind, documents.localId],\n set: documentRow,\n })\n .run();\n await saveDocumentProjectionRows(input);\n return true;\n}\n\nfunction didStoredDocumentContentChange(\n existing: Pick<\n StoredDocumentRecord,\n \"documentKind\" | \"snapshotEndVersion\" | \"text\" | \"title\"\n > | null,\n next: Pick<\n StoredDocumentRecord,\n \"documentKind\" | \"snapshotEndVersion\" | \"text\" | \"title\"\n >,\n): boolean {\n return (\n existing === null ||\n existing.documentKind !== (next.documentKind ?? DEFAULT_DOCUMENT_KIND) ||\n existing.snapshotEndVersion !== next.snapshotEndVersion ||\n existing.text !== next.text ||\n existing.title !== (next.title ?? deriveStoredDocumentTitle(next.text))\n );\n}\n\nexport async function resolveDocumentSaveTimestamp(input: {\n document: StoredDocumentRecord;\n options?: { updatedAt?: string } | undefined;\n tx: ClientSQLiteTransactionScope;\n}): Promise<string> {\n const { document, options, tx } = input;\n if (options?.updatedAt) {\n return options.updatedAt;\n }\n\n const [existingRecord, projectionRows] = await Promise.all([\n loadDocumentRecordInTransaction({\n localId: document.id,\n tx,\n }),\n tx\n .select({\n documentKind: documentProjection.documentKind,\n text: documentProjectionText.text,\n title: documentProjection.title,\n updatedAt: documentProjection.updatedAt,\n })\n .from(documentProjection)\n .leftJoin(\n documentProjectionText,\n eq(documentProjectionText.localId, documentProjection.localId),\n )\n .where(eq(documentProjection.localId, document.id))\n .limit(1),\n ]);\n const existingProjection = projectionRows[0];\n const now = new Date().toISOString();\n return didStoredDocumentContentChange(\n existingRecord\n ? {\n documentKind: getProjectionDocumentKind(existingProjection),\n snapshotEndVersion: existingRecord.snapshotEndVersion,\n text: getProjectionText(existingProjection),\n title: getProjectionTitle(existingProjection),\n }\n : null,\n document,\n )\n ? now\n : getProjectionUpdatedAt(existingProjection) || now;\n}\n"]}
1
+ {"version":3,"file":"documentRows.js","sourceRoot":"","sources":["documentRows.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,6BAA6B,EAAE,MAAM,sBAAsB,CAAC;AACrE,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAC;AAC7E,OAAO,EAAE,yBAAyB,EAAE,MAAM,kCAAkC,CAAC;AAC7E,OAAO,EAAE,qCAAqC,EAAE,MAAM,4CAA4C,CAAC;AAEnG,OAAO,EACL,kBAAkB,EAClB,sBAAsB,EACtB,SAAS,GACV,MAAM,wBAAwB,CAAC;AAChC,OAAO,EAEL,iCAAiC,GAClC,MAAM,0CAA0C,CAAC;AAGlD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EACL,yBAAyB,EACzB,iBAAiB,EACjB,kBAAkB,EAClB,sBAAsB,GACvB,MAAM,0BAA0B,CAAC;AAElC,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CAAC,CAAC,IAAI,EACR,QAAQ,CACT;QACC,CAAC,CAAC,GAAG;QACL,CAAC,CAAC,sBAAsB,CAAC,kBAAkB,CAAC,IAAI,GAAG,CAAC;AACxD,CAAC","sourcesContent":["import { and, eq, lte } from \"drizzle-orm\";\nimport { normalizeEffectiveAccessLevel } from \"../../../accessLevel\";\nimport { DEFAULT_DOCUMENT_KIND } from \"../../../documents/documentConstants\";\nimport { deriveStoredDocumentTitle } from \"../../../documents/documentKinds\";\nimport { serializeDocumentSyncPullContinuation } from \"../../../documents/shared/pullContinuation\";\nimport type { DocumentScope } from \"../../../sqlite/documentPersistence\";\nimport {\n documentProjection,\n documentProjectionText,\n documents,\n} from \"../../../sqlite/schema\";\nimport {\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../../../sqlite/sqlitePersistenceRuntime\";\nimport type { ExecSql } from \"../../../sqlite/sqlSchema\";\nimport type { StoredDocumentRecord } from \"../types\";\nimport { DOCUMENTS_APP_KIND } from \"./constants\";\nimport {\n getProjectionDocumentKind,\n getProjectionText,\n getProjectionTitle,\n getProjectionUpdatedAt,\n} from \"./documentProjectionRows\";\n\nexport function getDocumentScope(localId: string): DocumentScope {\n return {\n appKind: DOCUMENTS_APP_KIND,\n localId,\n };\n}\n\nexport async function hasDocumentRow(\n execSql: ExecSql,\n localId: string,\n): Promise<boolean> {\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const rows = await db\n .select({ localId: documents.localId })\n .from(documents)\n .where(\n and(\n eq(documents.appKind, DOCUMENTS_APP_KIND),\n eq(documents.localId, localId),\n ),\n )\n .limit(1);\n return rows.length > 0;\n}\n\nfunction toDocumentRecordRow(input: {\n document: StoredDocumentRecord;\n updatedAt: string;\n}) {\n const { document, updatedAt } = input;\n return {\n appKind: DOCUMENTS_APP_KIND,\n localId: document.id,\n documentId: document.documentId,\n recoveryGeneration: document.recoveryGeneration ?? 0,\n recoveryDocumentId: document.documentId\n ? null\n : (document.recoveryDocumentId ?? null),\n snapshotEndVersion: document.snapshotEndVersion,\n accessEpoch: document.accessEpoch,\n accessStateHash: document.accessStateHash ?? null,\n effectiveAccessLevel: normalizeEffectiveAccessLevel(\n document.effectiveAccessLevel,\n ),\n lastCommitLsn: document.lastCommitLsn ?? null,\n documentManifestBundle: document.documentManifestBundle ?? null,\n contentKeyBundle: document.contentKeyBundle ?? null,\n documentKekTargets: document.documentKekTargets ?? null,\n // Only touch the outgoing-delta marker when the caller manages it. Callers\n // that leave it undefined (e.g. discovery upserts, registration bootstrap)\n // must not clobber a marker a device-first deferRemoteSync write persisted.\n ...(document.pendingBaseVersion === undefined\n ? {}\n : { pendingBaseVersion: document.pendingBaseVersion }),\n ...(document.pullContinuation === undefined\n ? {}\n : {\n pullContinuation: serializeDocumentSyncPullContinuation(\n document.pullContinuation,\n ),\n }),\n updatedAt,\n };\n}\n\nasync function saveDocumentProjectionRows(input: {\n document: StoredDocumentRecord;\n tx: ClientSQLiteTransactionScope;\n updatedAt: string;\n}): Promise<void> {\n const { document, tx, updatedAt } = input;\n const projectionRow = {\n localId: document.id,\n documentId: document.documentId,\n containerId: document.containerId,\n documentKind: document.documentKind ?? DEFAULT_DOCUMENT_KIND,\n title: document.title ?? deriveStoredDocumentTitle(document.text),\n updatedAt,\n };\n await tx\n .insert(documentProjection)\n .values(projectionRow)\n .onConflictDoUpdate({\n target: documentProjection.localId,\n set: projectionRow,\n })\n .run();\n\n const projectionTextRow = {\n localId: document.id,\n text: document.text,\n };\n await tx\n .insert(documentProjectionText)\n .values(projectionTextRow)\n .onConflictDoUpdate({\n target: documentProjectionText.localId,\n set: projectionTextRow,\n })\n .run();\n}\n\nexport async function createDocumentRowsIfAbsent(input: {\n document: StoredDocumentRecord;\n tx: ClientSQLiteTransactionScope;\n updatedAt: string;\n}): Promise<boolean> {\n const { document, tx, updatedAt } = input;\n const documentRow = toDocumentRecordRow({ document, updatedAt });\n const inserted = await tx\n .insert(documents)\n .values(documentRow)\n .onConflictDoNothing({\n target: [documents.appKind, documents.localId],\n })\n .returning({ localId: documents.localId });\n if (inserted.length === 0) {\n return false;\n }\n\n await saveDocumentProjectionRows(input);\n return true;\n}\n\nexport async function saveDocumentRows(input: {\n document: StoredDocumentRecord;\n tx: ClientSQLiteTransactionScope;\n updatedAt: string;\n}): Promise<boolean> {\n const { document, tx, updatedAt } = input;\n const documentRow = toDocumentRecordRow({ document, updatedAt });\n // The recovery-generation fence rides on the upsert: a stored row from a\n // newer recovery generation is left untouched and returns no row, so a\n // stale writer skips the projection writes without a separate read.\n const saved = await tx\n .insert(documents)\n .values(documentRow)\n .onConflictDoUpdate({\n target: [documents.appKind, documents.localId],\n set: documentRow,\n setWhere: lte(\n documents.recoveryGeneration,\n documentRow.recoveryGeneration,\n ),\n })\n .returning({ localId: documents.localId });\n if (saved.length === 0) {\n return false;\n }\n await saveDocumentProjectionRows(input);\n return true;\n}\n\nfunction didStoredDocumentContentChange(\n existing: Pick<\n StoredDocumentRecord,\n \"documentKind\" | \"snapshotEndVersion\" | \"text\" | \"title\"\n > | null,\n next: Pick<\n StoredDocumentRecord,\n \"documentKind\" | \"snapshotEndVersion\" | \"text\" | \"title\"\n >,\n): boolean {\n return (\n existing === null ||\n existing.documentKind !== (next.documentKind ?? DEFAULT_DOCUMENT_KIND) ||\n existing.snapshotEndVersion !== next.snapshotEndVersion ||\n existing.text !== next.text ||\n existing.title !== (next.title ?? deriveStoredDocumentTitle(next.text))\n );\n}\n\nexport async function resolveDocumentSaveTimestamp(input: {\n document: StoredDocumentRecord;\n options?: { updatedAt?: string } | undefined;\n tx: ClientSQLiteTransactionScope;\n}): Promise<string> {\n const { document, options, tx } = input;\n if (options?.updatedAt) {\n return options.updatedAt;\n }\n\n // One read for the stored row and its projection; the projection columns\n // are not nullable, so they are null exactly when the projection is absent.\n const [existing] = await tx\n .select({\n snapshotEndVersion: documents.snapshotEndVersion,\n documentKind: documentProjection.documentKind,\n text: documentProjectionText.text,\n title: documentProjection.title,\n updatedAt: documentProjection.updatedAt,\n })\n .from(documents)\n .leftJoin(\n documentProjection,\n eq(documentProjection.localId, documents.localId),\n )\n .leftJoin(\n documentProjectionText,\n eq(documentProjectionText.localId, documentProjection.localId),\n )\n .where(\n and(\n eq(documents.appKind, DOCUMENTS_APP_KIND),\n eq(documents.localId, document.id),\n ),\n )\n .limit(1);\n const existingProjection =\n existing?.documentKind != null &&\n existing.title != null &&\n existing.updatedAt != null\n ? {\n documentKind: existing.documentKind,\n text: existing.text,\n title: existing.title,\n updatedAt: existing.updatedAt,\n }\n : undefined;\n const now = new Date().toISOString();\n return didStoredDocumentContentChange(\n existing\n ? {\n documentKind: getProjectionDocumentKind(existingProjection),\n snapshotEndVersion: existing.snapshotEndVersion,\n text: getProjectionText(existingProjection),\n title: getProjectionTitle(existingProjection),\n }\n : null,\n document,\n )\n ? now\n : getProjectionUpdatedAt(existingProjection) || now;\n}\n"]}
@@ -2,9 +2,12 @@ import { compareCanonicalStrings, KeyingVerificationError, PRINCIPAL_HISTORY_PAG
2
2
  import { isPrincipalPolicyStateChainEntryResponse, } from "../../internal/validators/response/index.js";
3
3
  import { and, eq } from "drizzle-orm";
4
4
  import { principalHistoryEntries, principalHistoryEvidenceTables, principalHistoryNodes, } from "../sqlite/principalHistoryEvidenceSchema.js";
5
+ import { sqliteInsertBatchSize } from "../sqlite/sqliteBindBudget.js";
5
6
  import { getClientSQLitePersistenceRuntime, } from "../sqlite/sqlitePersistenceRuntime.js";
7
+ import { excludedValue } from "../sqlite/sqliteUpsert.js";
6
8
  import { ensureSqlTables } from "../sqlite/sqlSchema.js";
7
- import { recordPrincipalHistoryNodeEdges } from "./principalHistoryNodeRetention.js";
9
+ import { writePrincipalHistoryNodes } from "./principalHistoryNodeRetention.js";
10
+ const ENTRY_WRITE_BATCH_SIZE = sqliteInsertBatchSize(principalHistoryEntries);
8
11
  /** Prepare only a page accepted by the signature/authorization verifier. */
9
12
  export async function preparePrincipalHistoryEvidencePage(input) {
10
13
  if (input.entries.length > PRINCIPAL_HISTORY_PAGE_ENTRY_LIMIT ||
@@ -22,32 +25,28 @@ export async function preparePrincipalHistoryEvidencePage(input) {
22
25
  /** Publish in the same guarded transaction as the accepted page's progress. */
23
26
  export async function writePrincipalHistoryEvidencePage(tx, page) {
24
27
  const scope = { scopeId: page.scopeId, organizationId: page.organizationId };
25
- for (const entry of [...page.entries].sort((a, b) => compareCanonicalStrings(a.leafHash, b.leafHash))) {
26
- const row = { ...scope, ...entry };
28
+ // A repeated leaf keeps its last entry, as sequential upserts would.
29
+ const entries = new Map(page.entries.map((entry) => [
30
+ entry.leafHash,
31
+ { ...scope, leafHash: entry.leafHash, entryJson: entry.entryJson },
32
+ ]));
33
+ const rows = [...entries.values()].sort((a, b) => compareCanonicalStrings(a.leafHash, b.leafHash));
34
+ for (let index = 0; index < rows.length; index += ENTRY_WRITE_BATCH_SIZE)
27
35
  await tx
28
36
  .insert(principalHistoryEntries)
29
- .values(row)
37
+ .values(rows.slice(index, index + ENTRY_WRITE_BATCH_SIZE))
30
38
  .onConflictDoUpdate({
31
39
  target: [
32
40
  principalHistoryEntries.scopeId,
33
41
  principalHistoryEntries.leafHash,
34
42
  ],
35
- set: row,
43
+ set: {
44
+ organizationId: excludedValue(principalHistoryEntries.organizationId),
45
+ entryJson: excludedValue(principalHistoryEntries.entryJson),
46
+ },
36
47
  })
37
48
  .run();
38
- }
39
- for (const node of [...page.nodes].sort((a, b) => compareCanonicalStrings(a.hash, b.hash))) {
40
- await recordPrincipalHistoryNodeEdges(tx, scope, node);
41
- const row = { ...scope, ...node };
42
- await tx
43
- .insert(principalHistoryNodes)
44
- .values(row)
45
- .onConflictDoUpdate({
46
- target: [principalHistoryNodes.scopeId, principalHistoryNodes.hash],
47
- set: row,
48
- })
49
- .run();
50
- }
49
+ await writePrincipalHistoryNodes(tx, scope, [...page.nodes].sort((a, b) => compareCanonicalStrings(a.hash, b.hash)));
51
50
  }
52
51
  /** Returns untrusted material. The caller must verify it against the issued root. */
53
52
  export async function loadPrincipalHistoryReference(input) {
@@ -1 +1 @@
1
- {"version":3,"file":"principalHistoryEvidencePersistence.js","sourceRoot":"","sources":["principalHistoryEvidencePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,kCAAkC,EAGlC,yBAAyB,EAEzB,iCAAiC,GAElC,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,wCAAwC,GAEzC,MAAM,gCAAgC,CAAC;AACxC,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EACL,uBAAuB,EACvB,8BAA8B,EAC9B,qBAAqB,GACtB,MAAM,0CAA0C,CAAC;AAClD,OAAO,EAEL,iCAAiC,GAClC,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAgB,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACpE,OAAO,EAAE,+BAA+B,EAAE,MAAM,iCAAiC,CAAC;AAalF,4EAA4E;AAC5E,MAAM,CAAC,KAAK,UAAU,mCAAmC,CAAC,KAMzD;IACC,IACE,KAAK,CAAC,OAAO,CAAC,MAAM,GAAG,kCAAkC;QACzD,KAAK,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC,GAAG,kCAAkC;QAE3D,MAAM,IAAI,uBAAuB,CAC/B,eAAe,EACf,kDAAkD,CACnD,CAAC;IACJ,MAAM,KAAK,GAAG,eAAe,CAAC,KAAK,CAAC,CAAC;IACrC,OAAO;QACL,GAAG,KAAK;QACR,OAAO,EAAE,MAAM,OAAO,CAAC,GAAG,CACxB,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CAAC,CAAC;YAClC,QAAQ,EAAE,MAAM,yBAAyB,CAAC,KAAK,CAAC,KAAK,CAAC;YACtD,SAAS,EAAE,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC;SACjC,CAAC,CAAC,CACJ;KACF,CAAC;AACJ,CAAC;AAED,+EAA+E;AAC/E,MAAM,C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CAAC;QACH,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACjD,CAAC;IAAC,MAAM,CAAC;QACP,KAAK,GAAG,IAAI,CAAC;IACf,CAAC;IACD,IAAI,CAAC,wCAAwC,CAAC,KAAK,CAAC;QAClD,MAAM,IAAI,uBAAuB,CAC/B,oBAAoB,EACpB,+CAA+C,CAChD,CAAC;IACJ,MAAM,SAAS,GAAG,KAAK,CAAC,iBAAiB,IAAI;QAC3C,aAAa,EAAE,KAAK,CAAC,KAAK,CAAC,aAAa;QACxC,WAAW,EAAE,KAAK,CAAC,KAAK,CAAC,WAAW;QACpC,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,OAAO;QAC5B,QAAQ,EAAE,KAAK,CAAC,KAAK,CAAC,QAAQ;QAC9B,SAAS,EAAE,KAAK,CAAC,KAAK,CAAC,SAAS;QAChC,cAAc,EAAE,KAAK,CAAC,KAAK,CAAC,cAAc;KAC3C,CAAC;IACF,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC;AACrD,CAAC","sourcesContent":["import {\n compareCanonicalStrings,\n KeyingVerificationError,\n PRINCIPAL_HISTORY_PAGE_ENTRY_LIMIT,\n type PrincipalHistoryIndexNode,\n type PrincipalPolicyHistoryReferenceProof,\n principalHistoryIndexLeaf,\n type ReferencedPrincipalHead,\n resolvePrincipalHistoryIndexProof,\n type VerifiedPrincipalPolicyHistory,\n} from \"@tearleads/crypto\";\nimport {\n isPrincipalPolicyStateChainEntryResponse,\n type PrincipalPolicyStateChainEntryResponse,\n} from \"@tearleads/validators/response\";\nimport { and, eq } from \"drizzle-orm\";\nimport {\n principalHistoryEntries,\n principalHistoryEvidenceTables,\n principalHistoryNodes,\n} from \"../sqlite/principalHistoryEvidenceSchema\";\nimport {\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\nimport { recordPrincipalHistoryNodeEdges } from \"./principalHistoryNodeRetention\";\n\nexport interface PrincipalHistoryEvidencePage {\n readonly indexRootHash: string;\n readonly scopeId: string;\n readonly organizationId: string;\n readonly entries: readonly {\n readonly leafHash: string;\n readonly entryJson: string;\n }[];\n readonly nodes: readonly PrincipalHistoryIndexNode[];\n}\n\n/** Prepare only a page accepted by the signature/authorization verifier. */\nexport async function preparePrincipalHistoryEvidencePage(input: {\n readonly indexRootHash: string;\n readonly scopeId: string;\n readonly organizationId: string;\n readonly entries: readonly PrincipalPolicyStateChainEntryResponse[];\n readonly nodes: readonly PrincipalHistoryIndexNode[];\n}): Promise<PrincipalHistoryEvidencePage> {\n if (\n input.entries.length > PRINCIPAL_HISTORY_PAGE_ENTRY_LIMIT ||\n input.nodes.length > 2 * PRINCIPAL_HISTORY_PAGE_ENTRY_LIMIT\n )\n throw new KeyingVerificationError(\n \"invalid_shape\",\n \"Principal evidence page exceeds its batch budget\",\n );\n const owned = structuredClone(input);\n return {\n ...owned,\n entries: await Promise.all(\n owned.entries.map(async (entry) => ({\n leafHash: await principalHistoryIndexLeaf(entry.state),\n entryJson: JSON.stringify(entry),\n })),\n ),\n };\n}\n\n/** Publish in the same guarded transaction as the accepted page's progress. */\nexport async function writePrincipalHistoryEvidencePage(\n tx: ClientSQLiteTransactionScope,\n page: PrincipalHistoryEvidencePage,\n): Promise<void> {\n const scope = { scopeId: page.scopeId, organizationId: page.organizationId };\n for (const entry of [...page.entries].sort((a, b) =>\n compareCanonicalStrings(a.leafHash, b.leafHash),\n )) {\n const row = { ...scope, ...entry };\n await tx\n .insert(principalHistoryEntries)\n .values(row)\n .onConflictDoUpdate({\n target: [\n principalHistoryEntries.scopeId,\n principalHistoryEntries.leafHash,\n ],\n set: row,\n })\n .run();\n }\n for (const node of [...page.nodes].sort((a, b) =>\n compareCanonicalStrings(a.hash, b.hash),\n )) {\n await recordPrincipalHistoryNodeEdges(tx, scope, node);\n const row = { ...scope, ...node };\n await tx\n .insert(principalHistoryNodes)\n .values(row)\n .onConflictDoUpdate({\n target: [principalHistoryNodes.scopeId, principalHistoryNodes.hash],\n set: row,\n })\n .run();\n }\n}\n\n/** Returns untrusted material. The caller must verify it against the issued root. */\nexport async function loadPrincipalHistoryReference(input: {\n readonly execSql: ExecSql;\n readonly scopeId: string;\n readonly history: VerifiedPrincipalPolicyHistory;\n readonly version: number;\n readonly expectedReference?: ReferencedPrincipalHead | undefined;\n}): Promise<PrincipalPolicyHistoryReferenceProof> {\n await ensureSqlTables(input.execSql, principalHistoryEvidenceTables);\n const { db } = getClientSQLitePersistenceRuntime(input.execSql);\n const resolved = await resolvePrincipalHistoryIndexProof({\n rootHash: input.history.indexRootHash,\n treeSize: input.history.currentEntry.state.version,\n version: input.version,\n readNode: async (hash) => {\n const [row] = await db\n .select()\n .from(principalHistoryNodes)\n .where(\n and(\n eq(principalHistoryNodes.scopeId, input.scopeId),\n eq(principalHistoryNodes.hash, hash),\n ),\n )\n .limit(1);\n return row ?? null;\n },\n });\n const [row] = await db\n .select()\n .from(principalHistoryEntries)\n .where(\n and(\n eq(principalHistoryEntries.scopeId, input.scopeId),\n eq(principalHistoryEntries.leafHash, resolved.leafHash),\n ),\n )\n .limit(1);\n let entry: unknown;\n try {\n entry = row ? JSON.parse(row.entryJson) : null;\n } catch {\n entry = null;\n }\n if (!isPrincipalPolicyStateChainEntryResponse(entry))\n throw new KeyingVerificationError(\n \"missing_dependency\",\n \"Principal history entry is missing or corrupt\",\n );\n const reference = input.expectedReference ?? {\n principalType: entry.state.principalType,\n principalId: entry.state.principalId,\n version: entry.state.version,\n keyEpoch: entry.state.keyEpoch,\n stateHash: entry.state.stateHash,\n keyFingerprint: entry.state.keyFingerprint,\n };\n return { reference, entry, proof: resolved.proof };\n}\n"]}
1
+ {"version":3,"file":"principalHistoryEvidencePersist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AAuB,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EAClD,EAAE,CAAC,uBAAuB,CAAC,QAAQ,EAAE,QAAQ,CAAC,QAAQ,CAAC,CACxD,CACF;SACA,KAAK,CAAC,CAAC,CAAC,CAAC;IACZ,IAAI,KAAc,CAAC;IACnB,IAAI,CAAC;QACH,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACjD,CAAC;IAAC,MAAM,CAAC;QACP,KAAK,GAAG,IAAI,CAAC;IACf,CAAC;IACD,IAAI,CAAC,wCAAwC,CAAC,KAAK,CAAC;QAClD,MAAM,IAAI,uBAAuB,CAC/B,oBAAoB,EACpB,+CAA+C,CAChD,CAAC;IACJ,MAAM,SAAS,GAAG,KAAK,CAAC,iBAAiB,IAAI;QAC3C,aAAa,EAAE,KAAK,CAAC,KAAK,CAAC,aAAa;QACxC,WAAW,EAAE,KAAK,CAAC,KAAK,CAAC,WAAW;QACpC,OAAO,EAAE,KAAK,CAAC,KAAK,CAAC,OAAO;QAC5B,QAAQ,EAAE,KAAK,CAAC,KAAK,CAAC,QAAQ;QAC9B,SAAS,EAAE,KAAK,CAAC,KAAK,CAAC,SAAS;QAChC,cAAc,EAAE,KAAK,CAAC,KAAK,CAAC,cAAc;KAC3C,CAAC;IACF,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,QAAQ,CAAC,KAAK,EAAE,CAAC;AACrD,CAAC","sourcesContent":["import {\n compareCanonicalStrings,\n KeyingVerificationError,\n PRINCIPAL_HISTORY_PAGE_ENTRY_LIMIT,\n type PrincipalHistoryIndexNode,\n type PrincipalPolicyHistoryReferenceProof,\n principalHistoryIndexLeaf,\n type ReferencedPrincipalHead,\n resolvePrincipalHistoryIndexProof,\n type VerifiedPrincipalPolicyHistory,\n} from \"@tearleads/crypto\";\nimport {\n isPrincipalPolicyStateChainEntryResponse,\n type PrincipalPolicyStateChainEntryResponse,\n} from \"@tearleads/validators/response\";\nimport { and, eq } from \"drizzle-orm\";\nimport {\n principalHistoryEntries,\n principalHistoryEvidenceTables,\n principalHistoryNodes,\n} from \"../sqlite/principalHistoryEvidenceSchema\";\nimport { sqliteInsertBatchSize } from \"../sqlite/sqliteBindBudget\";\nimport {\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../sqlite/sqlitePersistenceRuntime\";\nimport { excludedValue } from \"../sqlite/sqliteUpsert\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\nimport { writePrincipalHistoryNodes } from \"./principalHistoryNodeRetention\";\n\nconst ENTRY_WRITE_BATCH_SIZE = sqliteInsertBatchSize(principalHistoryEntries);\n\nexport interface PrincipalHistoryEvidencePage {\n readonly indexRootHash: string;\n readonly scopeId: string;\n readonly organizationId: string;\n readonly entries: readonly {\n readonly leafHash: string;\n readonly entryJson: string;\n }[];\n readonly nodes: readonly PrincipalHistoryIndexNode[];\n}\n\n/** Prepare only a page accepted by the signature/authorization verifier. */\nexport async function preparePrincipalHistoryEvidencePage(input: {\n readonly indexRootHash: string;\n readonly scopeId: string;\n readonly organizationId: string;\n readonly entries: readonly PrincipalPolicyStateChainEntryResponse[];\n readonly nodes: readonly PrincipalHistoryIndexNode[];\n}): Promise<PrincipalHistoryEvidencePage> {\n if (\n input.entries.length > PRINCIPAL_HISTORY_PAGE_ENTRY_LIMIT ||\n input.nodes.length > 2 * PRINCIPAL_HISTORY_PAGE_ENTRY_LIMIT\n )\n throw new KeyingVerificationError(\n \"invalid_shape\",\n \"Principal evidence page exceeds its batch budget\",\n );\n const owned = structuredClone(input);\n return {\n ...owned,\n entries: await Promise.all(\n owned.entries.map(async (entry) => ({\n leafHash: await principalHistoryIndexLeaf(entry.state),\n entryJson: JSON.stringify(entry),\n })),\n ),\n };\n}\n\n/** Publish in the same guarded transaction as the accepted page's progress. */\nexport async function writePrincipalHistoryEvidencePage(\n tx: ClientSQLiteTransactionScope,\n page: PrincipalHistoryEvidencePage,\n): Promise<void> {\n const scope = { scopeId: page.scopeId, organizationId: page.organizationId };\n // A repeated leaf keeps its last entry, as sequential upserts would.\n const entries = new Map(\n page.entries.map((entry) => [\n entry.leafHash,\n { ...scope, leafHash: entry.leafHash, entryJson: entry.entryJson },\n ]),\n );\n const rows = [...entries.values()].sort((a, b) =>\n compareCanonicalStrings(a.leafHash, b.leafHash),\n );\n for (let index = 0; index < rows.length; index += ENTRY_WRITE_BATCH_SIZE)\n await tx\n .insert(principalHistoryEntries)\n .values(rows.slice(index, index + ENTRY_WRITE_BATCH_SIZE))\n .onConflictDoUpdate({\n target: [\n principalHistoryEntries.scopeId,\n principalHistoryEntries.leafHash,\n ],\n set: {\n organizationId: excludedValue(principalHistoryEntries.organizationId),\n entryJson: excludedValue(principalHistoryEntries.entryJson),\n },\n })\n .run();\n await writePrincipalHistoryNodes(\n tx,\n scope,\n [...page.nodes].sort((a, b) => compareCanonicalStrings(a.hash, b.hash)),\n );\n}\n\n/** Returns untrusted material. The caller must verify it against the issued root. */\nexport async function loadPrincipalHistoryReference(input: {\n readonly execSql: ExecSql;\n readonly scopeId: string;\n readonly history: VerifiedPrincipalPolicyHistory;\n readonly version: number;\n readonly expectedReference?: ReferencedPrincipalHead | undefined;\n}): Promise<PrincipalPolicyHistoryReferenceProof> {\n await ensureSqlTables(input.execSql, principalHistoryEvidenceTables);\n const { db } = getClientSQLitePersistenceRuntime(input.execSql);\n const resolved = await resolvePrincipalHistoryIndexProof({\n rootHash: input.history.indexRootHash,\n treeSize: input.history.currentEntry.state.version,\n version: input.version,\n readNode: async (hash) => {\n const [row] = await db\n .select()\n .from(principalHistoryNodes)\n .where(\n and(\n eq(principalHistoryNodes.scopeId, input.scopeId),\n eq(principalHistoryNodes.hash, hash),\n ),\n )\n .limit(1);\n return row ?? null;\n },\n });\n const [row] = await db\n .select()\n .from(principalHistoryEntries)\n .where(\n and(\n eq(principalHistoryEntries.scopeId, input.scopeId),\n eq(principalHistoryEntries.leafHash, resolved.leafHash),\n ),\n )\n .limit(1);\n let entry: unknown;\n try {\n entry = row ? JSON.parse(row.entryJson) : null;\n } catch {\n entry = null;\n }\n if (!isPrincipalPolicyStateChainEntryResponse(entry))\n throw new KeyingVerificationError(\n \"missing_dependency\",\n \"Principal history entry is missing or corrupt\",\n );\n const reference = input.expectedReference ?? {\n principalType: entry.state.principalType,\n principalId: entry.state.principalId,\n version: entry.state.version,\n keyEpoch: entry.state.keyEpoch,\n stateHash: entry.state.stateHash,\n keyFingerprint: entry.state.keyFingerprint,\n };\n return { reference, entry, proof: resolved.proof };\n}\n"]}
@@ -1,48 +1,173 @@
1
- import { and, asc, eq } from "drizzle-orm";
1
+ import { and, asc, eq, inArray } from "drizzle-orm";
2
2
  import { principalHistoryNodes } from "../sqlite/principalHistoryEvidenceSchema.js";
3
3
  import { principalHistoryNodeReferences } from "../sqlite/principalHistoryNodeRetentionSchema.js";
4
- import { decrementPrincipalHistoryNodeReference, incrementPrincipalHistoryNodeReference, } from "./principalHistoryRootOwnership.js";
5
- /** Register edges before inserting their proof node, preserving pre-index evidence. */
6
- export async function recordPrincipalHistoryNodeEdges(tx, scope, node) {
7
- const key = and(eq(principalHistoryNodeReferences.scopeId, scope.scopeId), eq(principalHistoryNodeReferences.hash, node.hash));
8
- const [previous] = await tx
9
- .select()
10
- .from(principalHistoryNodeReferences)
11
- .where(key)
12
- .limit(1);
13
- if (previous?.leftHash === node.leftHash &&
14
- previous.rightHash === node.rightHash)
4
+ import { sqliteInsertBatchSize, sqliteRowsPerStatement, } from "../sqlite/sqliteBindBudget.js";
5
+ import { excludedValue } from "../sqlite/sqliteUpsert.js";
6
+ import { decrementPrincipalHistoryNodeReference, } from "./principalHistoryRootOwnership.js";
7
+ const REFERENCE_WRITE_BATCH_SIZE = sqliteInsertBatchSize(principalHistoryNodeReferences);
8
+ const NODE_WRITE_BATCH_SIZE = sqliteInsertBatchSize(principalHistoryNodes);
9
+ // The scope id binds once per lookup.
10
+ const HASH_LOOKUP_BATCH_SIZE = sqliteRowsPerStatement(1, 1);
11
+ function batches(items, size) {
12
+ const result = [];
13
+ for (let index = 0; index < items.length; index += size)
14
+ result.push(items.slice(index, index + size));
15
+ return result;
16
+ }
17
+ async function loadNodeReferences(tx, scope, hashes, into) {
18
+ const missing = [...new Set(hashes)].filter((hash) => !into.has(hash));
19
+ for (const batch of batches(missing, HASH_LOOKUP_BATCH_SIZE)) {
20
+ for (const hash of batch)
21
+ into.set(hash, null);
22
+ const rows = await tx
23
+ .select()
24
+ .from(principalHistoryNodeReferences)
25
+ .where(and(eq(principalHistoryNodeReferences.scopeId, scope.scopeId), inArray(principalHistoryNodeReferences.hash, batch)));
26
+ for (const row of rows)
27
+ into.set(row.hash, row);
28
+ }
29
+ }
30
+ async function loadStoredNodeHashes(tx, scope, hashes) {
31
+ const stored = new Set();
32
+ for (const batch of batches([...new Set(hashes)], HASH_LOOKUP_BATCH_SIZE)) {
33
+ const rows = await tx
34
+ .select({ hash: principalHistoryNodes.hash })
35
+ .from(principalHistoryNodes)
36
+ .where(and(eq(principalHistoryNodes.scopeId, scope.scopeId), inArray(principalHistoryNodes.hash, batch)));
37
+ for (const row of rows)
38
+ stored.add(row.hash);
39
+ }
40
+ return stored;
41
+ }
42
+ /**
43
+ * Replays the per-node edge bookkeeping in memory: increment inserts or adds
44
+ * one, decrement floors at zero within the scope's organization, and an edge
45
+ * change keeps the count while marking nodes first stored here as managed.
46
+ */
47
+ function replayNodeEdges(scope, nodes, loaded, storedNodeHashes) {
48
+ const references = new Map(loaded);
49
+ const read = (hash) => {
50
+ const reference = references.get(hash);
51
+ if (reference === undefined)
52
+ throw new Error("Principal history node reference was not loaded");
53
+ return reference;
54
+ };
55
+ const increment = (hash) => {
56
+ const reference = read(hash);
57
+ references.set(hash, reference
58
+ ? { ...reference, referenceCount: reference.referenceCount + 1 }
59
+ : {
60
+ ...scope,
61
+ hash,
62
+ referenceCount: 1,
63
+ managed: false,
64
+ leftHash: null,
65
+ rightHash: null,
66
+ });
67
+ };
68
+ const decrement = (hash) => {
69
+ const reference = read(hash);
70
+ if (reference?.organizationId === scope.organizationId)
71
+ references.set(hash, {
72
+ ...reference,
73
+ referenceCount: Math.max(reference.referenceCount - 1, 0),
74
+ });
75
+ };
76
+ for (const node of nodes) {
77
+ const previous = read(node.hash);
78
+ if (previous?.leftHash !== node.leftHash ||
79
+ previous.rightHash !== node.rightHash) {
80
+ const managed = previous?.managed || !storedNodeHashes.has(node.hash);
81
+ increment(node.leftHash);
82
+ increment(node.rightHash);
83
+ const current = read(node.hash);
84
+ references.set(node.hash, {
85
+ ...(current ?? {
86
+ ...scope,
87
+ hash: node.hash,
88
+ referenceCount: previous?.referenceCount ?? 0,
89
+ }),
90
+ leftHash: node.leftHash,
91
+ rightHash: node.rightHash,
92
+ managed,
93
+ });
94
+ for (const hash of [previous?.leftHash, previous?.rightHash])
95
+ if (hash)
96
+ decrement(hash);
97
+ }
98
+ storedNodeHashes.add(node.hash);
99
+ }
100
+ return references;
101
+ }
102
+ function isSameNodeReference(left, right) {
103
+ return (left.organizationId === right.organizationId &&
104
+ left.referenceCount === right.referenceCount &&
105
+ left.managed === right.managed &&
106
+ left.leftHash === right.leftHash &&
107
+ left.rightHash === right.rightHash);
108
+ }
109
+ /**
110
+ * Register edges and store proof nodes in the given order, with the same
111
+ * result as recording each node's edges and then upserting it. Reads the
112
+ * touched liveness rows once, replays the bookkeeping, and writes the changed
113
+ * rows and the nodes as multi-row upserts.
114
+ */
115
+ export async function writePrincipalHistoryNodes(tx, scope, nodes) {
116
+ if (nodes.length === 0)
15
117
  return;
16
- const [existing] = await tx
17
- .select({ hash: principalHistoryNodes.hash })
18
- .from(principalHistoryNodes)
19
- .where(and(eq(principalHistoryNodes.scopeId, scope.scopeId), eq(principalHistoryNodes.hash, node.hash)))
20
- .limit(1);
21
- for (const hash of [node.leftHash, node.rightHash])
22
- await incrementPrincipalHistoryNodeReference(tx, scope, hash);
23
- await tx
24
- .insert(principalHistoryNodeReferences)
25
- .values({
26
- ...scope,
27
- ...node,
28
- referenceCount: previous?.referenceCount ?? 0,
29
- managed: previous?.managed || !existing,
30
- })
31
- .onConflictDoUpdate({
32
- target: [
33
- principalHistoryNodeReferences.scopeId,
34
- principalHistoryNodeReferences.hash,
35
- ],
36
- set: {
118
+ const loaded = new Map();
119
+ await loadNodeReferences(tx, scope, nodes.flatMap((node) => [node.hash, node.leftHash, node.rightHash]), loaded);
120
+ // An edge change releases the stored row's previous children.
121
+ await loadNodeReferences(tx, scope, nodes.flatMap((node) => {
122
+ const stored = loaded.get(node.hash);
123
+ return [stored?.leftHash, stored?.rightHash].filter((hash) => Boolean(hash));
124
+ }), loaded);
125
+ const replayed = replayNodeEdges(scope, nodes, loaded, await loadStoredNodeHashes(tx, scope, nodes.map((node) => node.hash)));
126
+ const changed = [...replayed].flatMap(([hash, reference]) => {
127
+ const before = loaded.get(hash);
128
+ return reference && !(before && isSameNodeReference(before, reference))
129
+ ? [reference]
130
+ : [];
131
+ });
132
+ for (const batch of batches(changed, REFERENCE_WRITE_BATCH_SIZE))
133
+ await tx
134
+ .insert(principalHistoryNodeReferences)
135
+ .values(batch)
136
+ .onConflictDoUpdate({
137
+ target: [
138
+ principalHistoryNodeReferences.scopeId,
139
+ principalHistoryNodeReferences.hash,
140
+ ],
141
+ set: {
142
+ referenceCount: excludedValue(principalHistoryNodeReferences.referenceCount),
143
+ managed: excludedValue(principalHistoryNodeReferences.managed),
144
+ leftHash: excludedValue(principalHistoryNodeReferences.leftHash),
145
+ rightHash: excludedValue(principalHistoryNodeReferences.rightHash),
146
+ },
147
+ })
148
+ .run();
149
+ const nodeRows = new Map(nodes.map((node) => [
150
+ node.hash,
151
+ {
152
+ ...scope,
153
+ hash: node.hash,
37
154
  leftHash: node.leftHash,
38
155
  rightHash: node.rightHash,
39
- managed: previous?.managed || !existing,
40
156
  },
41
- })
42
- .run();
43
- for (const hash of [previous?.leftHash, previous?.rightHash])
44
- if (hash)
45
- await decrementPrincipalHistoryNodeReference(tx, scope, hash);
157
+ ]));
158
+ for (const batch of batches([...nodeRows.values()], NODE_WRITE_BATCH_SIZE))
159
+ await tx
160
+ .insert(principalHistoryNodes)
161
+ .values(batch)
162
+ .onConflictDoUpdate({
163
+ target: [principalHistoryNodes.scopeId, principalHistoryNodes.hash],
164
+ set: {
165
+ organizationId: excludedValue(principalHistoryNodes.organizationId),
166
+ leftHash: excludedValue(principalHistoryNodes.leftHash),
167
+ rightHash: excludedValue(principalHistoryNodes.rightHash),
168
+ },
169
+ })
170
+ .run();
46
171
  }
47
172
  /** A bounded cascade releases only managed proof nodes with no live incoming edge. */
48
173
  export async function reclaimPrincipalHistoryNodes(tx, scope) {
@@ -1 +1 @@
1
- {"version":3,"file":"principalHistoryNodeRetention.js","sourceRoot":"","sources":["principalHistoryNodeRetention.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,0CAA0C,CAAC;AACjF,OAAO,EAAE,8BAA8B,EAAE,MAAM,+CAA+C,CAAC;AAE/F,OAAO,EACL,sCAAsC,EACtC,sCAAsC,GAEvC,MAAM,iCAAiC,CAAC;AAEzC,uFAAuF;AACvF,MAAM,CAAC,KAAK,UAAU,+BAA+B,CACnD,EAAgC,EAChC,KAAgC,EAChC,IAA+B;IAE/B,MAAM,GAAG,GAAG,GAAG,CACb,EAAE,CAAC,8BAA8B,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACzD,EAAE,CAAC,8BAA8B,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CACnD,CAAC;IACF,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAM,EAAE;SACxB,MAAM,EAAE;SACR,IAAI,CAAC,8BAA8B,CAAC;SACpC,KAAK,CAAC,GAAG,CAAC;SACV,KAAK,CAAC,CAAC,CA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CACD,EAAE,CAAC,8BAA8B,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACzD,EAAE,CAAC,8BAA8B,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CACnD,CACF;iBACA,GAAG,EAAE,CAAC;YACT,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC;gBAChD,IAAI,IAAI;oBAAE,MAAM,sCAAsC,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;YAC1E,SAAS,IAAI,CAAC,CAAC;QACjB,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import type { PrincipalHistoryIndexNode } from \"@tearleads/crypto\";\nimport { and, asc, eq } from \"drizzle-orm\";\nimport { principalHistoryNodes } from \"../sqlite/principalHistoryEvidenceSchema\";\nimport { principalHistoryNodeReferences } from \"../sqlite/principalHistoryNodeRetentionSchema\";\nimport type { ClientSQLiteTransactionScope } from \"../sqlite/sqlitePersistenceRuntime\";\nimport {\n decrementPrincipalHistoryNodeReference,\n incrementPrincipalHistoryNodeReference,\n type PrincipalHistoryNodeScope,\n} from \"./principalHistoryRootOwnership\";\n\n/** Register edges before inserting their proof node, preserving pre-index evidence. */\nexport async function recordPrincipalHistoryNodeEdges(\n tx: ClientSQLiteTransactionScope,\n scope: PrincipalHistoryNodeScope,\n node: PrincipalHistoryIndexNode,\n) {\n const key = and(\n eq(principalHistoryNodeReferences.scopeId, scope.scopeId),\n eq(principalHistoryNodeReferences.hash, node.hash),\n );\n const [previous] = await tx\n .select()\n .from(principalHistoryNodeReferences)\n .where(key)\n .limit(1);\n if (\n previous?.leftHash === node.leftHash &&\n previous.rightHash === node.rightHash\n )\n return;\n const [existing] = await tx\n .select({ hash: principalHistoryNodes.hash })\n .from(principalHistoryNodes)\n .where(\n and(\n eq(principalHistoryNodes.scopeId, scope.scopeId),\n eq(principalHistoryNodes.hash, node.hash),\n ),\n )\n .limit(1);\n for (const hash of [node.leftHash, node.rightHash])\n await incrementPrincipalHistoryNodeReference(tx, scope, hash);\n await tx\n .insert(principalHistoryNodeReferences)\n .values({\n ...scope,\n ...node,\n referenceCount: previous?.referenceCount ?? 0,\n managed: previous?.managed || !existing,\n })\n .onConflictDoUpdate({\n target: [\n principalHistoryNodeReferences.scopeId,\n principalHistoryNodeReferences.hash,\n ],\n set: {\n leftHash: node.leftHash,\n rightHash: node.rightHash,\n managed: previous?.managed || !existing,\n },\n })\n .run();\n for (const hash of [previous?.leftHash, previous?.rightHash])\n if (hash) await decrementPrincipalHistoryNodeReference(tx, scope, hash);\n}\n\n/** A bounded cascade releases only managed proof nodes with no live incoming edge. */\nexport async function reclaimPrincipalHistoryNodes(\n tx: ClientSQLiteTransactionScope,\n scope: PrincipalHistoryNodeScope,\n): Promise<number> {\n let reclaimed = 0;\n while (reclaimed < 64) {\n const obsolete = await tx\n .select()\n .from(principalHistoryNodeReferences)\n .where(\n and(\n eq(principalHistoryNodeReferences.scopeId, scope.scopeId),\n eq(\n principalHistoryNodeReferences.organizationId,\n scope.organizationId,\n ),\n eq(principalHistoryNodeReferences.managed, true),\n eq(principalHistoryNodeReferences.referenceCount, 0),\n ),\n )\n .orderBy(asc(principalHistoryNodeReferences.hash))\n .limit(64 - reclaimed);\n if (obsolete.length === 0) break;\n for (const node of obsolete) {\n await tx\n .delete(principalHistoryNodes)\n .where(\n and(\n eq(principalHistoryNodes.scopeId, scope.scopeId),\n eq(principalHistoryNodes.organizationId, scope.organizationId),\n eq(principalHistoryNodes.hash, node.hash),\n ),\n )\n .run();\n await tx\n .delete(principalHistoryNodeReferences)\n .where(\n and(\n eq(principalHistoryNodeReferences.scopeId, scope.scopeId),\n eq(principalHistoryNodeReferences.hash, node.hash),\n ),\n )\n .run();\n for (const hash of [node.leftHash, node.rightHash])\n if (hash) await decrementPrincipalHistoryNodeReference(tx, scope, hash);\n reclaimed += 1;\n }\n }\n return reclaimed;\n}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C,OAAO,CAAC,EAChD,EAAE,CAAC,qBAAqB,CAAC,cAAc,EAAE,KAAK,CAAC,cAAc,CAAC,EAC9D,EAAE,CAAC,qBAAqB,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CAC1C,CACF;iBACA,GAAG,EAAE,CAAC;YACT,MAAM,EAAE;iBACL,MAAM,CAAC,8BAA8B,CAAC;iBACtC,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,8BAA8B,CAAC,OAAO,EAAE,KAAK,CAAC,OAAO,CAAC,EACzD,EAAE,CAAC,8BAA8B,CAAC,IAAI,EAAE,IAAI,CAAC,IAAI,CAAC,CACnD,CACF;iBACA,GAAG,EAAE,CAAC;YACT,KAAK,MAAM,IAAI,IAAI,CAAC,IAAI,CAAC,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC;gBAChD,IAAI,IAAI;oBAAE,MAAM,sCAAsC,CAAC,EAAE,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;YAC1E,SAAS,IAAI,CAAC,CAAC;QACjB,CAAC;IACH,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC","sourcesContent":["import type { PrincipalHistoryIndexNode } from \"@tearleads/crypto\";\nimport { and, asc, eq, inArray } from \"drizzle-orm\";\nimport { principalHistoryNodes } from \"../sqlite/principalHistoryEvidenceSchema\";\nimport { principalHistoryNodeReferences } from \"../sqlite/principalHistoryNodeRetentionSchema\";\nimport {\n sqliteInsertBatchSize,\n sqliteRowsPerStatement,\n} from \"../sqlite/sqliteBindBudget\";\nimport type { ClientSQLiteTransactionScope } from \"../sqlite/sqlitePersistenceRuntime\";\nimport { excludedValue } from \"../sqlite/sqliteUpsert\";\nimport {\n decrementPrincipalHistoryNodeReference,\n type PrincipalHistoryNodeScope,\n} from \"./principalHistoryRootOwnership\";\n\ntype PrincipalHistoryNodeReference =\n typeof principalHistoryNodeReferences.$inferSelect;\n\nconst REFERENCE_WRITE_BATCH_SIZE = sqliteInsertBatchSize(\n principalHistoryNodeReferences,\n);\nconst NODE_WRITE_BATCH_SIZE = sqliteInsertBatchSize(principalHistoryNodes);\n// The scope id binds once per lookup.\nconst HASH_LOOKUP_BATCH_SIZE = sqliteRowsPerStatement(1, 1);\n\nfunction batches<T>(items: readonly T[], size: number): T[][] {\n const result: T[][] = [];\n for (let index = 0; index < items.length; index += size)\n result.push(items.slice(index, index + size));\n return result;\n}\n\nasync function loadNodeReferences(\n tx: ClientSQLiteTransactionScope,\n scope: PrincipalHistoryNodeScope,\n hashes: readonly string[],\n into: Map<string, PrincipalHistoryNodeReference | null>,\n): Promise<void> {\n const missing = [...new Set(hashes)].filter((hash) => !into.has(hash));\n for (const batch of batches(missing, HASH_LOOKUP_BATCH_SIZE)) {\n for (const hash of batch) into.set(hash, null);\n const rows = await tx\n .select()\n .from(principalHistoryNodeReferences)\n .where(\n and(\n eq(principalHistoryNodeReferences.scopeId, scope.scopeId),\n inArray(principalHistoryNodeReferences.hash, batch),\n ),\n );\n for (const row of rows) into.set(row.hash, row);\n }\n}\n\nasync function loadStoredNodeHashes(\n tx: ClientSQLiteTransactionScope,\n scope: PrincipalHistoryNodeScope,\n hashes: readonly string[],\n): Promise<Set<string>> {\n const stored = new Set<string>();\n for (const batch of batches([...new Set(hashes)], HASH_LOOKUP_BATCH_SIZE)) {\n const rows = await tx\n .select({ hash: principalHistoryNodes.hash })\n .from(principalHistoryNodes)\n .where(\n and(\n eq(principalHistoryNodes.scopeId, scope.scopeId),\n inArray(principalHistoryNodes.hash, batch),\n ),\n );\n for (const row of rows) stored.add(row.hash);\n }\n return stored;\n}\n\n/**\n * Replays the per-node edge bookkeeping in memory: increment inserts or adds\n * one, decrement floors at zero within the scope's organization, and an edge\n * change keeps the count while marking nodes first stored here as managed.\n */\nfunction replayNodeEdges(\n scope: PrincipalHistoryNodeScope,\n nodes: readonly PrincipalHistoryIndexNode[],\n loaded: ReadonlyMap<string, PrincipalHistoryNodeReference | null>,\n storedNodeHashes: Set<string>,\n): Map<string, PrincipalHistoryNodeReference | null> {\n const references = new Map(loaded);\n const read = (hash: string) => {\n const reference = references.get(hash);\n if (reference === undefined)\n throw new Error(\"Principal history node reference was not loaded\");\n return reference;\n };\n const increment = (hash: string) => {\n const reference = read(hash);\n references.set(\n hash,\n reference\n ? { ...reference, referenceCount: reference.referenceCount + 1 }\n : {\n ...scope,\n hash,\n referenceCount: 1,\n managed: false,\n leftHash: null,\n rightHash: null,\n },\n );\n };\n const decrement = (hash: string) => {\n const reference = read(hash);\n if (reference?.organizationId === scope.organizationId)\n references.set(hash, {\n ...reference,\n referenceCount: Math.max(reference.referenceCount - 1, 0),\n });\n };\n for (const node of nodes) {\n const previous = read(node.hash);\n if (\n previous?.leftHash !== node.leftHash ||\n previous.rightHash !== node.rightHash\n ) {\n const managed = previous?.managed || !storedNodeHashes.has(node.hash);\n increment(node.leftHash);\n increment(node.rightHash);\n const current = read(node.hash);\n references.set(node.hash, {\n ...(current ?? {\n ...scope,\n hash: node.hash,\n referenceCount: previous?.referenceCount ?? 0,\n }),\n leftHash: node.leftHash,\n rightHash: node.rightHash,\n managed,\n });\n for (const hash of [previous?.leftHash, previous?.rightHash])\n if (hash) decrement(hash);\n }\n storedNodeHashes.add(node.hash);\n }\n return references;\n}\n\nfunction isSameNodeReference(\n left: PrincipalHistoryNodeReference,\n right: PrincipalHistoryNodeReference,\n): boolean {\n return (\n left.organizationId === right.organizationId &&\n left.referenceCount === right.referenceCount &&\n left.managed === right.managed &&\n left.leftHash === right.leftHash &&\n left.rightHash === right.rightHash\n );\n}\n\n/**\n * Register edges and store proof nodes in the given order, with the same\n * result as recording each node's edges and then upserting it. Reads the\n * touched liveness rows once, replays the bookkeeping, and writes the changed\n * rows and the nodes as multi-row upserts.\n */\nexport async function writePrincipalHistoryNodes(\n tx: ClientSQLiteTransactionScope,\n scope: PrincipalHistoryNodeScope,\n nodes: readonly PrincipalHistoryIndexNode[],\n): Promise<void> {\n if (nodes.length === 0) return;\n const loaded = new Map<string, PrincipalHistoryNodeReference | null>();\n await loadNodeReferences(\n tx,\n scope,\n nodes.flatMap((node) => [node.hash, node.leftHash, node.rightHash]),\n loaded,\n );\n // An edge change releases the stored row's previous children.\n await loadNodeReferences(\n tx,\n scope,\n nodes.flatMap((node) => {\n const stored = loaded.get(node.hash);\n return [stored?.leftHash, stored?.rightHash].filter(\n (hash): hash is string => Boolean(hash),\n );\n }),\n loaded,\n );\n const replayed = replayNodeEdges(\n scope,\n nodes,\n loaded,\n await loadStoredNodeHashes(\n tx,\n scope,\n nodes.map((node) => node.hash),\n ),\n );\n const changed = [...replayed].flatMap(([hash, reference]) => {\n const before = loaded.get(hash);\n return reference && !(before && isSameNodeReference(before, reference))\n ? [reference]\n : [];\n });\n for (const batch of batches(changed, REFERENCE_WRITE_BATCH_SIZE))\n await tx\n .insert(principalHistoryNodeReferences)\n .values(batch)\n .onConflictDoUpdate({\n target: [\n principalHistoryNodeReferences.scopeId,\n principalHistoryNodeReferences.hash,\n ],\n set: {\n referenceCount: excludedValue(\n principalHistoryNodeReferences.referenceCount,\n ),\n managed: excludedValue(principalHistoryNodeReferences.managed),\n leftHash: excludedValue(principalHistoryNodeReferences.leftHash),\n rightHash: excludedValue(principalHistoryNodeReferences.rightHash),\n },\n })\n .run();\n const nodeRows = new Map(\n nodes.map((node) => [\n node.hash,\n {\n ...scope,\n hash: node.hash,\n leftHash: node.leftHash,\n rightHash: node.rightHash,\n },\n ]),\n );\n for (const batch of batches([...nodeRows.values()], NODE_WRITE_BATCH_SIZE))\n await tx\n .insert(principalHistoryNodes)\n .values(batch)\n .onConflictDoUpdate({\n target: [principalHistoryNodes.scopeId, principalHistoryNodes.hash],\n set: {\n organizationId: excludedValue(principalHistoryNodes.organizationId),\n leftHash: excludedValue(principalHistoryNodes.leftHash),\n rightHash: excludedValue(principalHistoryNodes.rightHash),\n },\n })\n .run();\n}\n\n/** A bounded cascade releases only managed proof nodes with no live incoming edge. */\nexport async function reclaimPrincipalHistoryNodes(\n tx: ClientSQLiteTransactionScope,\n scope: PrincipalHistoryNodeScope,\n): Promise<number> {\n let reclaimed = 0;\n while (reclaimed < 64) {\n const obsolete = await tx\n .select()\n .from(principalHistoryNodeReferences)\n .where(\n and(\n eq(principalHistoryNodeReferences.scopeId, scope.scopeId),\n eq(\n principalHistoryNodeReferences.organizationId,\n scope.organizationId,\n ),\n eq(principalHistoryNodeReferences.managed, true),\n eq(principalHistoryNodeReferences.referenceCount, 0),\n ),\n )\n .orderBy(asc(principalHistoryNodeReferences.hash))\n .limit(64 - reclaimed);\n if (obsolete.length === 0) break;\n for (const node of obsolete) {\n await tx\n .delete(principalHistoryNodes)\n .where(\n and(\n eq(principalHistoryNodes.scopeId, scope.scopeId),\n eq(principalHistoryNodes.organizationId, scope.organizationId),\n eq(principalHistoryNodes.hash, node.hash),\n ),\n )\n .run();\n await tx\n .delete(principalHistoryNodeReferences)\n .where(\n and(\n eq(principalHistoryNodeReferences.scopeId, scope.scopeId),\n eq(principalHistoryNodeReferences.hash, node.hash),\n ),\n )\n .run();\n for (const hash of [node.leftHash, node.rightHash])\n if (hash) await decrementPrincipalHistoryNodeReference(tx, scope, hash);\n reclaimed += 1;\n }\n }\n return reclaimed;\n}\n"]}
@@ -1,52 +1,115 @@
1
1
  import { KeyingVerificationError } from "../../internal/crypto/index.js";
2
- import { and, eq } from "drizzle-orm";
2
+ import { and, eq, inArray, or } from "drizzle-orm";
3
3
  import { principalPolicyBundleStates } from "../principalPolicyStates.js";
4
4
  import { principalPolicyBundleReferences } from "../sqlite/schema.js";
5
- function exactReferenceWhere(row) {
6
- return and(eq(principalPolicyBundleReferences.principalType, row.principalType), eq(principalPolicyBundleReferences.principalId, row.principalId), eq(principalPolicyBundleReferences.version, row.version), eq(principalPolicyBundleReferences.stateHash, row.stateHash), eq(principalPolicyBundleReferences.keyEpoch, row.keyEpoch), eq(principalPolicyBundleReferences.keyFingerprint, row.keyFingerprint));
5
+ import { sqliteInsertBatchSize, sqliteRowsPerStatement, } from "../sqlite/sqliteBindBudget.js";
6
+ const REFERENCE_INSERT_BATCH_SIZE = sqliteInsertBatchSize(principalPolicyBundleReferences);
7
+ // Principal type and id plus the bundle version and state hash bind once.
8
+ const REFERENCE_LOOKUP_VERSIONS = sqliteRowsPerStatement(1, 4);
9
+ /** The exact six-field match; the lookup already fixes the principal. */
10
+ function isSameReference(stored, row) {
11
+ return (stored.version === row.version &&
12
+ stored.stateHash === row.stateHash &&
13
+ stored.keyEpoch === row.keyEpoch &&
14
+ stored.keyFingerprint === row.keyFingerprint);
7
15
  }
8
- async function assertNoIndexedConflict(tx, row) {
9
- const conflicts = await tx
10
- .select({
11
- bundleStateHash: principalPolicyBundleReferences.bundleStateHash,
12
- })
13
- .from(principalPolicyBundleReferences)
14
- .where(and(exactReferenceWhere(row), eq(principalPolicyBundleReferences.bundleVersion, row.bundleVersion)));
15
- if (conflicts.some(({ bundleStateHash }) => bundleStateHash !== row.bundleStateHash)) {
16
- throw new KeyingVerificationError("equivocation", "principal policy reference has conflicting bundle heads at one version");
16
+ /** The table's primary key within one principal. */
17
+ function referenceKey(row) {
18
+ return JSON.stringify([
19
+ row.version,
20
+ row.stateHash,
21
+ row.keyEpoch,
22
+ row.keyFingerprint,
23
+ row.bundleStateHash,
24
+ ]);
25
+ }
26
+ /** Rows either equivocation check can match, read before this bundle's writes. */
27
+ async function loadStoredReferences(tx, head, versions) {
28
+ const stored = [];
29
+ for (let index = 0; index < versions.length; index += REFERENCE_LOOKUP_VERSIONS) {
30
+ stored.push(...(await tx
31
+ .select({
32
+ version: principalPolicyBundleReferences.version,
33
+ stateHash: principalPolicyBundleReferences.stateHash,
34
+ keyEpoch: principalPolicyBundleReferences.keyEpoch,
35
+ keyFingerprint: principalPolicyBundleReferences.keyFingerprint,
36
+ bundleVersion: principalPolicyBundleReferences.bundleVersion,
37
+ bundleStateHash: principalPolicyBundleReferences.bundleStateHash,
38
+ })
39
+ .from(principalPolicyBundleReferences)
40
+ .where(and(eq(principalPolicyBundleReferences.principalType, head.principalType), eq(principalPolicyBundleReferences.principalId, head.principalId), inArray(principalPolicyBundleReferences.version, versions.slice(index, index + REFERENCE_LOOKUP_VERSIONS)), or(eq(principalPolicyBundleReferences.bundleVersion, head.version), eq(principalPolicyBundleReferences.bundleStateHash, head.stateHash))))));
17
41
  }
42
+ return stored;
18
43
  }
19
- export async function indexPrincipalPolicyBundleInTransaction(tx, bundle) {
20
- const head = bundle.currentState;
21
- for (const state of principalPolicyBundleStates(bundle)) {
22
- if (state.principalType !== head.principalType ||
23
- state.principalId !== head.principalId) {
24
- throw new KeyingVerificationError("object_mismatch", "principal policy bundle reference belongs to another principal");
25
- }
26
- const row = {
27
- principalType: state.principalType,
28
- principalId: state.principalId,
29
- version: state.version,
30
- stateHash: state.stateHash,
31
- keyEpoch: state.keyEpoch,
32
- keyFingerprint: state.keyFingerprint,
33
- bundleVersion: head.version,
34
- bundleStateHash: head.stateHash,
35
- };
36
- await assertNoIndexedConflict(tx, row);
44
+ /**
45
+ * Rows written earlier in the same bundle all carry its head, so neither
46
+ * check can match them: each state's outcome depends only on stored rows.
47
+ */
48
+ function referenceConflict(row, stored) {
49
+ const sameReference = stored.filter((candidate) => isSameReference(candidate, row));
50
+ if (sameReference.some((candidate) => candidate.bundleVersion === row.bundleVersion &&
51
+ candidate.bundleStateHash !== row.bundleStateHash))
52
+ return new KeyingVerificationError("equivocation", "principal policy reference has conflicting bundle heads at one version");
53
+ const indexed = sameReference.find((candidate) => candidate.bundleStateHash === row.bundleStateHash);
54
+ if (indexed && indexed.bundleVersion !== row.bundleVersion)
55
+ return new KeyingVerificationError("equivocation", "principal policy reference index conflicts with the bundle head");
56
+ return null;
57
+ }
58
+ async function insertMissingReferences(tx, rows, stored) {
59
+ const present = new Set(stored.map(referenceKey));
60
+ const missing = new Map();
61
+ for (const row of rows) {
62
+ const key = referenceKey(row);
63
+ if (!present.has(key) && !missing.has(key))
64
+ missing.set(key, row);
65
+ }
66
+ const values = [...missing.values()];
67
+ for (let index = 0; index < values.length; index += REFERENCE_INSERT_BATCH_SIZE) {
37
68
  await tx
38
69
  .insert(principalPolicyBundleReferences)
39
- .values(row)
70
+ .values(values.slice(index, index + REFERENCE_INSERT_BATCH_SIZE))
40
71
  .onConflictDoNothing()
41
72
  .run();
42
- const [stored] = await tx
43
- .select({ bundleVersion: principalPolicyBundleReferences.bundleVersion })
44
- .from(principalPolicyBundleReferences)
45
- .where(and(exactReferenceWhere(row), eq(principalPolicyBundleReferences.bundleStateHash, row.bundleStateHash)))
46
- .limit(1);
47
- if (stored?.bundleVersion !== row.bundleVersion) {
48
- throw new KeyingVerificationError("equivocation", "principal policy reference index conflicts with the bundle head");
73
+ }
74
+ }
75
+ /**
76
+ * Index every state of a verified bundle under its head. Fails in state order
77
+ * with the per-state checks' errors, keeping the states indexed before the
78
+ * first failure.
79
+ */
80
+ export async function indexPrincipalPolicyBundleInTransaction(tx, bundle) {
81
+ const head = bundle.currentState;
82
+ const states = principalPolicyBundleStates(bundle);
83
+ const foreignIndex = states.findIndex((state) => state.principalType !== head.principalType ||
84
+ state.principalId !== head.principalId);
85
+ const rows = (foreignIndex === -1 ? states : states.slice(0, foreignIndex)).map((state) => ({
86
+ principalType: state.principalType,
87
+ principalId: state.principalId,
88
+ version: state.version,
89
+ stateHash: state.stateHash,
90
+ keyEpoch: state.keyEpoch,
91
+ keyFingerprint: state.keyFingerprint,
92
+ bundleVersion: head.version,
93
+ bundleStateHash: head.stateHash,
94
+ }));
95
+ const stored = rows.length === 0
96
+ ? []
97
+ : await loadStoredReferences(tx, head, [
98
+ ...new Set(rows.map((row) => row.version)),
99
+ ]);
100
+ let failure = null;
101
+ let accepted = rows.length;
102
+ for (const [index, row] of rows.entries()) {
103
+ failure = referenceConflict(row, stored);
104
+ if (failure) {
105
+ accepted = index;
106
+ break;
49
107
  }
50
108
  }
109
+ if (!failure && foreignIndex !== -1)
110
+ failure = new KeyingVerificationError("object_mismatch", "principal policy bundle reference belongs to another principal");
111
+ await insertMissingReferences(tx, rows.slice(0, accepted), stored);
112
+ if (failure)
113
+ throw failure;
51
114
  }
52
115
  //# sourceMappingURL=principalPolicyReferenceIndexPersistence.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"principalPolicyReferenceIndexPersistence.js","sourceRoot":"","sources":["principalPolicyReferenceIndexPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAE5D,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,+BAA+B,EAAE,MAAM,kBAAkB,CAAC;AAMnE,SAAS,mBAAmB,CAAC,GAAgC;IAC3D,OAAO,GAAG,CACR,EAAE,CAAC,+BAA+B,CAAC,aAAa,EAAE,GAAG,CAAC,aAAa,CAAC,EACpE,EAAE,CAAC,+BAA+B,CAAC,WAAW,EAAE,GAAG,CAAC,WAAW,CAAC,EAChE,EAAE,CAAC,+BAA+B,CAAC,OAAO,EAAE,GAAG,CAAC,OAAO,CAAC,EACxD,EAAE,CAAC,+BAA+B,CAAC,SAAS,EAAE,GAAG,CAAC,SAAS,CAAC,EAC5D,EAAE,CAAC,+BAA+B,CAAC,QAAQ,EAAE,GAAG,CAAC,QAAQ,CAAC,EAC1D,EAAE,CAAC,+BAA+B,CAAC,cAAc,EAAE,GAAG,CAAC,cAAc,CAAC,CACvE,CAAC;AACJ,CAAC;AAED,KAAK,UAAU,uBAAuB,CACpC,EAAgC,EAChC,GAAgC;IAEhC,MAAM,SAAS,GAAG,MAAM,EAAE;SACvB,MAAM,CAAC;QACN,eAAe,EAAE,+BAA+B,CAAC,eAAe;KACjE,CAAC;SACD,IAAI,CAAC,+BAA+B,CAAC;SACrC,KAAK,CACJ,GAAG,CACD,mBAAmB,CAAC,GAAG,CAAC,EACxB,EAAE,CAAC,+BAA+B,CAAC,aAAa,EAAE,GAAG,CAAC,aAAa,CAAC,CACrE,CACF,CAAC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AA+B,CAAC;aACrC,KAAK,CACJ,GAAG,CACD,mBAAmB,CAAC,GAAG,CAAC,EACxB,EAAE,CACA,+BAA+B,CAAC,eAAe,EAC/C,GAAG,CAAC,eAAe,CACpB,CACF,CACF;aACA,KAAK,CAAC,CAAC,CAAC,CAAC;QACZ,IAAI,MAAM,EAAE,aAAa,KAAK,GAAG,CAAC,aAAa,EAAE,CAAC;YAChD,MAAM,IAAI,uBAAuB,CAC/B,cAAc,EACd,iEAAiE,CAClE,CAAC;QACJ,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { KeyingVerificationError } from \"@tearleads/crypto\";\nimport type { PrincipalPolicyBundleResponse } from \"@tearleads/validators/response\";\nimport { and, eq } from \"drizzle-orm\";\nimport { principalPolicyBundleStates } from \"../principalPolicyStates\";\nimport { principalPolicyBundleReferences } from \"../sqlite/schema\";\nimport type { ClientSQLiteTransactionScope } from \"../sqlite/sqlitePersistenceRuntime\";\n\ntype PrincipalPolicyReferenceRow =\n typeof principalPolicyBundleReferences.$inferInsert;\n\nfunction exactReferenceWhere(row: PrincipalPolicyReferenceRow) {\n return and(\n eq(principalPolicyBundleReferences.principalType, row.principalType),\n eq(principalPolicyBundleReferences.principalId, row.principalId),\n eq(principalPolicyBundleReferences.version, row.version),\n eq(principalPolicyBundleReferences.stateHash, row.stateHash),\n eq(principalPolicyBundleReferences.keyEpoch, row.keyEpoch),\n eq(principalPolicyBundleReferences.keyFingerprint, row.keyFingerprint),\n );\n}\n\nasync function assertNoIndexedConflict(\n tx: ClientSQLiteTransactionScope,\n row: PrincipalPolicyReferenceRow,\n): Promise<void> {\n const conflicts = await tx\n .select({\n bundleStateHash: principalPolicyBundleReferences.bundleStateHash,\n })\n .from(principalPolicyBundleReferences)\n .where(\n and(\n exactReferenceWhere(row),\n eq(principalPolicyBundleReferences.bundleVersion, row.bundleVersion),\n ),\n );\n if (\n conflicts.some(\n ({ bundleStateHash }) => bundleStateHash !== row.bundleStateHash,\n )\n ) {\n throw new KeyingVerificationError(\n \"equivocation\",\n \"principal policy reference has conflicting bundle heads at one version\",\n );\n }\n}\n\nexport async function indexPrincipalPolicyBundleInTransaction(\n tx: ClientSQLiteTransactionScope,\n bundle: PrincipalPolicyBundleResponse,\n): Promise<void> {\n const head = bundle.currentState;\n for (const state of principalPolicyBundleStates(bundle)) {\n if (\n state.principalType !== head.principalType ||\n state.principalId !== head.principalId\n ) {\n throw new KeyingVerificationError(\n \"object_mismatch\",\n \"principal policy bundle reference belongs to another principal\",\n );\n }\n const row: PrincipalPolicyReferenceRow = {\n principalType: state.principalType,\n principalId: state.principalId,\n version: state.version,\n stateHash: state.stateHash,\n keyEpoch: state.keyEpoch,\n keyFingerprint: state.keyFingerprint,\n bundleVersion: head.version,\n bundleStateHash: head.stateHash,\n };\n await assertNoIndexedConflict(tx, row);\n await tx\n .insert(principalPolicyBundleReferences)\n .values(row)\n .onConflictDoNothing()\n .run();\n const [stored] = await tx\n .select({ bundleVersion: principalPolicyBundleReferences.bundleVersion })\n .from(principalPolicyBundleReferences)\n .where(\n and(\n exactReferenceWhere(row),\n eq(\n principalPolicyBundleReferences.bundleStateHash,\n row.bundleStateHash,\n ),\n ),\n )\n .limit(1);\n if (stored?.bundleVersion !== row.bundleVersion) {\n throw new KeyingVerificationError(\n \"equivocation\",\n \"principal policy reference index conflicts with the bundle head\",\n );\n }\n }\n}\n"]}
1
+ {"version":3,"file":"principalPolicyReferenceIndexPersistence.js","sourceRoot":"","sources":["principalPolicyReferenceIndexPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAE5D,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACnD,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,+BAA+B,EAAE,MAAM,kBAAkB,CAAC;AACnE,OAAO,EACL,qBAAqB,EACrB,sBAAsB,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iBAAiB,EACjB,gEAAgE,CACjE,CAAC;IACJ,MAAM,uBAAuB,CAAC,EAAE,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,CAAC,EAAE,MAAM,CAAC,CAAC;IACnE,IAAI,OAAO;QAAE,MAAM,OAAO,CAAC;AAC7B,CAAC","sourcesContent":["import { KeyingVerificationError } from \"@tearleads/crypto\";\nimport type { PrincipalPolicyBundleResponse } from \"@tearleads/validators/response\";\nimport { and, eq, inArray, or } from \"drizzle-orm\";\nimport { principalPolicyBundleStates } from \"../principalPolicyStates\";\nimport { principalPolicyBundleReferences } from \"../sqlite/schema\";\nimport {\n sqliteInsertBatchSize,\n sqliteRowsPerStatement,\n} from \"../sqlite/sqliteBindBudget\";\nimport type { ClientSQLiteTransactionScope } from \"../sqlite/sqlitePersistenceRuntime\";\n\ntype PrincipalPolicyReferenceRow =\n typeof principalPolicyBundleReferences.$inferInsert;\n\ntype StoredPrincipalPolicyReference = Pick<\n typeof principalPolicyBundleReferences.$inferSelect,\n | \"version\"\n | \"stateHash\"\n | \"keyEpoch\"\n | \"keyFingerprint\"\n | \"bundleVersion\"\n | \"bundleStateHash\"\n>;\n\nconst REFERENCE_INSERT_BATCH_SIZE = sqliteInsertBatchSize(\n principalPolicyBundleReferences,\n);\n// Principal type and id plus the bundle version and state hash bind once.\nconst REFERENCE_LOOKUP_VERSIONS = sqliteRowsPerStatement(1, 4);\n\n/** The exact six-field match; the lookup already fixes the principal. */\nfunction isSameReference(\n stored: StoredPrincipalPolicyReference,\n row: PrincipalPolicyReferenceRow,\n): boolean {\n return (\n stored.version === row.version &&\n stored.stateHash === row.stateHash &&\n stored.keyEpoch === row.keyEpoch &&\n stored.keyFingerprint === row.keyFingerprint\n );\n}\n\n/** The table's primary key within one principal. */\nfunction referenceKey(\n row: Pick<\n StoredPrincipalPolicyReference,\n \"version\" | \"stateHash\" | \"keyEpoch\" | \"keyFingerprint\" | \"bundleStateHash\"\n >,\n): string {\n return JSON.stringify([\n row.version,\n row.stateHash,\n row.keyEpoch,\n row.keyFingerprint,\n row.bundleStateHash,\n ]);\n}\n\n/** Rows either equivocation check can match, read before this bundle's writes. */\nasync function loadStoredReferences(\n tx: ClientSQLiteTransactionScope,\n head: PrincipalPolicyBundleResponse[\"currentState\"],\n versions: readonly number[],\n): Promise<StoredPrincipalPolicyReference[]> {\n const stored: StoredPrincipalPolicyReference[] = [];\n for (\n let index = 0;\n index < versions.length;\n index += REFERENCE_LOOKUP_VERSIONS\n ) {\n stored.push(\n ...(await tx\n .select({\n version: principalPolicyBundleReferences.version,\n stateHash: principalPolicyBundleReferences.stateHash,\n keyEpoch: principalPolicyBundleReferences.keyEpoch,\n keyFingerprint: principalPolicyBundleReferences.keyFingerprint,\n bundleVersion: principalPolicyBundleReferences.bundleVersion,\n bundleStateHash: principalPolicyBundleReferences.bundleStateHash,\n })\n .from(principalPolicyBundleReferences)\n .where(\n and(\n eq(\n principalPolicyBundleReferences.principalType,\n head.principalType,\n ),\n eq(principalPolicyBundleReferences.principalId, head.principalId),\n inArray(\n principalPolicyBundleReferences.version,\n versions.slice(index, index + REFERENCE_LOOKUP_VERSIONS),\n ),\n or(\n eq(principalPolicyBundleReferences.bundleVersion, head.version),\n eq(\n principalPolicyBundleReferences.bundleStateHash,\n head.stateHash,\n ),\n ),\n ),\n )),\n );\n }\n return stored;\n}\n\n/**\n * Rows written earlier in the same bundle all carry its head, so neither\n * check can match them: each state's outcome depends only on stored rows.\n */\nfunction referenceConflict(\n row: PrincipalPolicyReferenceRow,\n stored: readonly StoredPrincipalPolicyReference[],\n): KeyingVerificationError | null {\n const sameReference = stored.filter((candidate) =>\n isSameReference(candidate, row),\n );\n if (\n sameReference.some(\n (candidate) =>\n candidate.bundleVersion === row.bundleVersion &&\n candidate.bundleStateHash !== row.bundleStateHash,\n )\n )\n return new KeyingVerificationError(\n \"equivocation\",\n \"principal policy reference has conflicting bundle heads at one version\",\n );\n const indexed = sameReference.find(\n (candidate) => candidate.bundleStateHash === row.bundleStateHash,\n );\n if (indexed && indexed.bundleVersion !== row.bundleVersion)\n return new KeyingVerificationError(\n \"equivocation\",\n \"principal policy reference index conflicts with the bundle head\",\n );\n return null;\n}\n\nasync function insertMissingReferences(\n tx: ClientSQLiteTransactionScope,\n rows: readonly PrincipalPolicyReferenceRow[],\n stored: readonly StoredPrincipalPolicyReference[],\n): Promise<void> {\n const present = new Set(stored.map(referenceKey));\n const missing = new Map<string, PrincipalPolicyReferenceRow>();\n for (const row of rows) {\n const key = referenceKey(row);\n if (!present.has(key) && !missing.has(key)) missing.set(key, row);\n }\n const values = [...missing.values()];\n for (\n let index = 0;\n index < values.length;\n index += REFERENCE_INSERT_BATCH_SIZE\n ) {\n await tx\n .insert(principalPolicyBundleReferences)\n .values(values.slice(index, index + REFERENCE_INSERT_BATCH_SIZE))\n .onConflictDoNothing()\n .run();\n }\n}\n\n/**\n * Index every state of a verified bundle under its head. Fails in state order\n * with the per-state checks' errors, keeping the states indexed before the\n * first failure.\n */\nexport async function indexPrincipalPolicyBundleInTransaction(\n tx: ClientSQLiteTransactionScope,\n bundle: PrincipalPolicyBundleResponse,\n): Promise<void> {\n const head = bundle.currentState;\n const states = principalPolicyBundleStates(bundle);\n const foreignIndex = states.findIndex(\n (state) =>\n state.principalType !== head.principalType ||\n state.principalId !== head.principalId,\n );\n const rows: PrincipalPolicyReferenceRow[] = (\n foreignIndex === -1 ? states : states.slice(0, foreignIndex)\n ).map((state) => ({\n principalType: state.principalType,\n principalId: state.principalId,\n version: state.version,\n stateHash: state.stateHash,\n keyEpoch: state.keyEpoch,\n keyFingerprint: state.keyFingerprint,\n bundleVersion: head.version,\n bundleStateHash: head.stateHash,\n }));\n const stored =\n rows.length === 0\n ? []\n : await loadStoredReferences(tx, head, [\n ...new Set(rows.map((row) => row.version)),\n ]);\n let failure: KeyingVerificationError | null = null;\n let accepted = rows.length;\n for (const [index, row] of rows.entries()) {\n failure = referenceConflict(row, stored);\n if (failure) {\n accepted = index;\n break;\n }\n }\n if (!failure && foreignIndex !== -1)\n failure = new KeyingVerificationError(\n \"object_mismatch\",\n \"principal policy bundle reference belongs to another principal\",\n );\n await insertMissingReferences(tx, rows.slice(0, accepted), stored);\n if (failure) throw failure;\n}\n"]}
@@ -0,0 +1,9 @@
1
+ import { sql } from "drizzle-orm";
2
+ /**
3
+ * The value a multi-row upsert proposed for `column`. A `set` clause needs it
4
+ * to apply each conflicting row's own value rather than one shared value.
5
+ */
6
+ export function excludedValue(column) {
7
+ return sql `excluded.${sql.identifier(column.name)}`;
8
+ }
9
+ //# sourceMappingURL=sqliteUpsert.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sqliteUpsert.js","sourceRoot":"","sources":["sqliteUpsert.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,GAAG,EAAE,MAAM,aAAa,CAAC;AAG5C;;;GAGG;AACH,MAAM,UAAU,aAAa,CAAC,MAAoB;IAChD,OAAO,GAAG,CAAA,YAAY,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;AACtD,CAAC","sourcesContent":["import { type SQL, sql } from \"drizzle-orm\";\nimport type { SQLiteColumn } from \"drizzle-orm/sqlite-core\";\n\n/**\n * The value a multi-row upsert proposed for `column`. A `set` clause needs it\n * to apply each conflicting row's own value rather than one shared value.\n */\nexport function excludedValue(column: SQLiteColumn): SQL {\n return sql`excluded.${sql.identifier(column.name)}`;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tearleads/client-sdk",
3
- "version": "0.1.47",
3
+ "version": "0.1.48",
4
4
  "description": "React-free Tearleads client runtime: local SQLite persistence, keys, sync, encrypted blobs, and domain workflows.",
5
5
  "license": "UNLICENSED",
6
6
  "repository": {