@tearleads/client-sdk 0.1.196 → 0.1.198

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 (52) hide show
  1. package/data/persistence/principalCurrentAcknowledgementPersistence.d.ts +13 -5
  2. package/data/persistence/principalCurrentAcknowledgementPersistence.js +30 -44
  3. package/data/persistence/principalCurrentAcknowledgementPersistence.js.map +1 -1
  4. package/data/persistence/principalHistory/principalHistoryEvidencePersistence.d.ts +3 -13
  5. package/data/persistence/principalHistory/principalHistoryEvidencePersistence.js +2 -18
  6. package/data/persistence/principalHistory/principalHistoryEvidencePersistence.js.map +1 -1
  7. package/data/persistence/principalHistory/principalHistoryPrefixPersistence.d.ts +119 -15
  8. package/data/persistence/principalHistory/principalHistoryPrefixPersistence.js +69 -99
  9. package/data/persistence/principalHistory/principalHistoryPrefixPersistence.js.map +1 -1
  10. package/data/persistence/principalHistory/principalHistoryRepository.js +134 -0
  11. package/data/persistence/principalHistory/principalHistoryRepository.js.map +1 -0
  12. package/data/persistence/principalHistory/principalHistoryStagePersistence.d.ts +119 -8
  13. package/data/persistence/principalHistory/principalHistoryStagePersistence.js +49 -80
  14. package/data/persistence/principalHistory/principalHistoryStagePersistence.js.map +1 -1
  15. package/data/persistence/principalHistory/principalHistoryStageRetention.js +2 -2
  16. package/data/persistence/principalHistory/principalHistoryStageRetention.js.map +1 -1
  17. package/internal/loro/changeTimestamps.d.ts +26 -0
  18. package/internal/loro/changeTimestamps.js +29 -0
  19. package/internal/loro/changeTimestamps.js.map +1 -0
  20. package/internal/loro/document.js +2 -0
  21. package/internal/loro/document.js.map +1 -1
  22. package/internal/loro/index.d.ts +1 -0
  23. package/internal/loro/index.js +1 -0
  24. package/internal/loro/index.js.map +1 -1
  25. package/package.json +1 -1
  26. package/workflows/principals/acknowledgements/acknowledgedPrincipalCurrents.js +3 -3
  27. package/workflows/principals/acknowledgements/acknowledgedPrincipalCurrents.js.map +1 -1
  28. package/workflows/principals/acknowledgements/acknowledgedPrincipalPredecessor.d.ts +1 -1
  29. package/workflows/principals/acknowledgements/acknowledgedPrincipalPredecessor.js +2 -2
  30. package/workflows/principals/acknowledgements/acknowledgedPrincipalPredecessor.js.map +1 -1
  31. package/workflows/principals/acknowledgements/reconcileAcknowledgedPrincipalCurrent.js +2 -2
  32. package/workflows/principals/acknowledgements/reconcileAcknowledgedPrincipalCurrent.js.map +1 -1
  33. package/workflows/principals/historyRecovery/loadRecoveredPrincipalHistoryPage.js +7 -6
  34. package/workflows/principals/historyRecovery/loadRecoveredPrincipalHistoryPage.js.map +1 -1
  35. package/workflows/principals/historyRecovery/principalHistoryRecoveryPrefix.js +4 -6
  36. package/workflows/principals/historyRecovery/principalHistoryRecoveryPrefix.js.map +1 -1
  37. package/workflows/principals/historyRecovery/principalHistoryRecoveryReferences.js +2 -3
  38. package/workflows/principals/historyRecovery/principalHistoryRecoveryReferences.js.map +1 -1
  39. package/workflows/principals/historyRecovery/principalHistoryRecoveryStage.js +3 -3
  40. package/workflows/principals/historyRecovery/principalHistoryRecoveryStage.js.map +1 -1
  41. package/workflows/principals/historyRecovery/recoverPrincipalPolicyHistory.js +4 -5
  42. package/workflows/principals/historyRecovery/recoverPrincipalPolicyHistory.js.map +1 -1
  43. package/workflows/principals/publicHistory/publicPrincipalHistoryPage.js +2 -3
  44. package/workflows/principals/publicHistory/publicPrincipalHistoryPage.js.map +1 -1
  45. package/workflows/principals/publicHistory/publicPrincipalHistoryProgress.js +9 -14
  46. package/workflows/principals/publicHistory/publicPrincipalHistoryProgress.js.map +1 -1
  47. package/workflows/principals/publicHistory/recoverPublicPrincipalHistory.js +2 -3
  48. package/workflows/principals/publicHistory/recoverPublicPrincipalHistory.js.map +1 -1
  49. package/workflows/principals/publicHistory/selectPublicPrincipalHistory.js +3 -5
  50. package/workflows/principals/publicHistory/selectPublicPrincipalHistory.js.map +1 -1
  51. package/workflows/principals/recoveryOperation/principalRecoveryDirectory.js +2 -3
  52. package/workflows/principals/recoveryOperation/principalRecoveryDirectory.js.map +1 -1
@@ -1,14 +1,10 @@
1
1
  import { and, eq } from "drizzle-orm";
2
- import { assertProjectionVerificationCurrent } from "../../keyingProjectionVerification/types.js";
3
- import { principalHistoryEvidenceTables, principalHistoryPrefixes, } from "../../sqlite/schema/principalHistoryEvidenceSchema.js";
4
- import { principalHistoryStageTables } from "../../sqlite/schema/principalHistoryStageSchema.js";
5
- import { getClientSQLitePersistenceRuntime } from "../../sqlite/sqlitePersistenceRuntime.js";
6
- import { ensureSqlTables } from "../../sqlite/sqlSchema.js";
2
+ import { principalHistoryPrefixes } from "../../sqlite/schema/principalHistoryEvidenceSchema.js";
7
3
  import { archivePrincipalHistoryKeyEnvelopes } from "../principalKeys/principalKeyEnvelopeArchivePersistence.js";
8
4
  import { reclaimPrincipalHistoryNodes } from "./principalHistoryNodeRetention.js";
9
5
  import { principalHistoryPrefixRootOwnerColumns, principalHistoryRootRetention, releasePrincipalHistoryRoot, writePrincipalHistoryRootEdges, } from "./principalHistoryRootOwnership.js";
10
6
  import { reclaimCompletedPrincipalHistoryStages } from "./principalHistoryStageRetention.js";
11
- const principalHistoryPrefixColumns = {
7
+ export const principalHistoryPrefixColumns = {
12
8
  scopeId: principalHistoryPrefixes.scopeId,
13
9
  organizationId: principalHistoryPrefixes.organizationId,
14
10
  version: principalHistoryPrefixes.version,
@@ -16,104 +12,78 @@ const principalHistoryPrefixColumns = {
16
12
  currentJson: principalHistoryPrefixes.currentJson,
17
13
  progress: principalHistoryPrefixes.progress,
18
14
  };
19
- export async function loadPrincipalHistoryPrefix(execSql, scopeId) {
20
- await ensureSqlTables(execSql, principalHistoryEvidenceTables);
21
- const { db } = getClientSQLitePersistenceRuntime(execSql);
22
- const [prefix] = await db
23
- .select(principalHistoryPrefixColumns)
24
- .from(principalHistoryPrefixes)
25
- .where(eq(principalHistoryPrefixes.scopeId, scopeId))
26
- .limit(1);
27
- return prefix ?? null;
28
- }
29
15
  /**
30
16
  * Preserve identical completed progress; this sink does not authenticate hints.
31
17
  * Callers must discard an unreadable prefix or identify it with rejectedPrefix
32
- * before publishing a repair of the same head and current artifacts.
18
+ * before publishing a repair of the same head and current artifacts. Updates
19
+ * the owned `prefix` copy's progress when it reuses the stored progress.
33
20
  */
34
- export async function savePrincipalHistoryPrefix(input) {
35
- const prefix = { ...input.prefix };
36
- const indexRootHash = input.indexRootHash;
37
- const rejected = input.rejectedPrefix ? { ...input.rejectedPrefix } : null;
38
- await ensureSqlTables(input.execSql, [
39
- ...principalHistoryEvidenceTables,
40
- ...principalHistoryStageTables,
41
- ]);
42
- const runtime = getClientSQLitePersistenceRuntime(input.execSql);
43
- const result = await runtime.guardedTransaction(async (tx) => {
44
- const [previous] = await tx
45
- .select()
46
- .from(principalHistoryPrefixes)
47
- .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))
48
- .limit(1);
49
- const replacesRejected = previous &&
50
- rejected?.scopeId === previous.scopeId &&
51
- rejected.progress === previous.progress;
52
- if (previous && previous.version > prefix.version && !replacesRejected)
53
- return;
54
- if (previous &&
55
- previous.version === prefix.version &&
56
- previous.organizationId === prefix.organizationId &&
57
- previous.headJson === prefix.headJson &&
58
- previous.currentJson === prefix.currentJson &&
59
- !replacesRejected) {
60
- // Resealing an unchanged prefix would invalidate an acknowledgement's
61
- // progress CAS even though its authenticated predecessor is unchanged.
62
- // Still refresh authenticated root ownership and retention below.
63
- // Reuse authenticates this opaque progress; rejected hints are removed
64
- // or explicitly replaced before repair can rely on the new prefix.
65
- prefix.progress = previous.progress;
66
- }
67
- // Archiving reads only these three fields, so an identical previous
68
- // prefix would repeat the same idempotent write.
69
- if (previous &&
70
- (previous.organizationId !== prefix.organizationId ||
71
- previous.currentJson !== prefix.currentJson ||
72
- previous.version !== prefix.version))
73
- await archivePrincipalHistoryKeyEnvelopes(tx, previous);
74
- await archivePrincipalHistoryKeyEnvelopes(tx, prefix);
75
- const root = {
76
- scopeId: prefix.scopeId,
77
- organizationId: prefix.organizationId,
78
- rootHash: indexRootHash,
79
- };
80
- const row = { ...prefix, rootHash: indexRootHash };
81
- await tx
82
- .insert(principalHistoryPrefixes)
83
- .values(row)
84
- .onConflictDoUpdate({
85
- target: principalHistoryPrefixes.scopeId,
86
- set: row,
87
- })
88
- .run();
89
- await reclaimCompletedPrincipalHistoryStages(tx, prefix);
90
- await writePrincipalHistoryRootEdges(tx, principalHistoryRootRetention(previous, root));
91
- await reclaimPrincipalHistoryNodes(tx, prefix);
92
- }, input.stillCurrent, { behavior: "immediate" });
93
- assertProjectionVerificationCurrent(() => result.committed);
21
+ export async function savePrincipalHistoryPrefixInTransaction(tx, prefix, indexRootHash, rejected) {
22
+ const [previous] = await tx
23
+ .select()
24
+ .from(principalHistoryPrefixes)
25
+ .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))
26
+ .limit(1);
27
+ const replacesRejected = previous &&
28
+ rejected?.scopeId === previous.scopeId &&
29
+ rejected.progress === previous.progress;
30
+ if (previous && previous.version > prefix.version && !replacesRejected)
31
+ return;
32
+ if (previous &&
33
+ previous.version === prefix.version &&
34
+ previous.organizationId === prefix.organizationId &&
35
+ previous.headJson === prefix.headJson &&
36
+ previous.currentJson === prefix.currentJson &&
37
+ !replacesRejected) {
38
+ // Resealing an unchanged prefix would invalidate an acknowledgement's
39
+ // progress CAS even though its authenticated predecessor is unchanged.
40
+ // Still refresh authenticated root ownership and retention below.
41
+ // Reuse authenticates this opaque progress; rejected hints are removed
42
+ // or explicitly replaced before repair can rely on the new prefix.
43
+ prefix.progress = previous.progress;
44
+ }
45
+ // Archiving reads only these three fields, so an identical previous
46
+ // prefix would repeat the same idempotent write.
47
+ if (previous &&
48
+ (previous.organizationId !== prefix.organizationId ||
49
+ previous.currentJson !== prefix.currentJson ||
50
+ previous.version !== prefix.version))
51
+ await archivePrincipalHistoryKeyEnvelopes(tx, previous);
52
+ await archivePrincipalHistoryKeyEnvelopes(tx, prefix);
53
+ const root = {
54
+ scopeId: prefix.scopeId,
55
+ organizationId: prefix.organizationId,
56
+ rootHash: indexRootHash,
57
+ };
58
+ const row = { ...prefix, rootHash: indexRootHash };
59
+ await tx
60
+ .insert(principalHistoryPrefixes)
61
+ .values(row)
62
+ .onConflictDoUpdate({
63
+ target: principalHistoryPrefixes.scopeId,
64
+ set: row,
65
+ })
66
+ .run();
67
+ await reclaimCompletedPrincipalHistoryStages(tx, prefix);
68
+ await writePrincipalHistoryRootEdges(tx, principalHistoryRootRetention(previous, root));
69
+ await reclaimPrincipalHistoryNodes(tx, prefix);
94
70
  }
95
71
  /** Invalid hints are disposable, but a losing reader must preserve a newer hint. */
96
- export async function discardPrincipalHistoryPrefix(input) {
97
- const prefix = { ...input.prefix };
98
- await ensureSqlTables(input.execSql, principalHistoryEvidenceTables);
99
- const runtime = getClientSQLitePersistenceRuntime(input.execSql);
100
- const result = await runtime.guardedTransaction(async (tx) => {
101
- const [current] = await tx
102
- .select({
103
- progress: principalHistoryPrefixes.progress,
104
- ...principalHistoryPrefixRootOwnerColumns,
105
- })
106
- .from(principalHistoryPrefixes)
107
- .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))
108
- .limit(1);
109
- if (!current || current.progress !== prefix.progress)
110
- return;
111
- await releasePrincipalHistoryRoot(tx, current);
112
- await tx
113
- .delete(principalHistoryPrefixes)
114
- .where(and(eq(principalHistoryPrefixes.scopeId, prefix.scopeId), eq(principalHistoryPrefixes.progress, prefix.progress)))
115
- .run();
116
- }, input.stillCurrent, { behavior: "immediate" });
117
- assertProjectionVerificationCurrent(() => result.committed);
72
+ export async function discardPrincipalHistoryPrefixInTransaction(tx, prefix) {
73
+ const [current] = await tx
74
+ .select({
75
+ progress: principalHistoryPrefixes.progress,
76
+ ...principalHistoryPrefixRootOwnerColumns,
77
+ })
78
+ .from(principalHistoryPrefixes)
79
+ .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))
80
+ .limit(1);
81
+ if (!current || current.progress !== prefix.progress)
82
+ return;
83
+ await releasePrincipalHistoryRoot(tx, current);
84
+ await tx
85
+ .delete(principalHistoryPrefixes)
86
+ .where(and(eq(principalHistoryPrefixes.scopeId, prefix.scopeId), eq(principalHistoryPrefixes.progress, prefix.progress)))
87
+ .run();
118
88
  }
119
89
  //# sourceMappingURL=principalHistoryPrefixPersistence.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"principalHistoryPrefixPersistence.js","sourceRoot":"","sources":["principalHistoryPrefixPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,mCAAmC,EAAE,MAAM,0CAA0C,CAAC;AAC/F,OAAO,EACL,8BAA8B,EAC9B,wBAAwB,GACzB,MAAM,oDAAoD,CAAC;AAC5D,OAAO,EAAE,2BAA2B,EAAE,MAAM,iDAAiD,CAAC;AAC9F,OAAO,EAAE,iCAAiC,EAAE,MAAM,uCAAuC,CAAC;AAC1F,OAAO,EAAgB,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAAE,mCAAmC,EAAE,MAAM,yDAAyD,CAAC;AAC9G,OAAO,EAAE,4BAA4B,EAAE,MAAM,iCAAiC,CAAC;AAC/E,OAAO,EACL,sCAAsC,EACtC,6BAA6B,EAC7B,2BAA2B,EAC3B,8BAA8B,GAC/B,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,sCAAsC,EAAE,MAAM,kCAAkC,CAAC;AAO1F,MAAM,6BAA6B,GAAG;IACpC,OAAO,EAAE,wBAAwB,CAAC,OAAO;IACzC,cAAc,EAAE,wBAAwB,CAAC,cAAc;IACvD,OAAO,EAAE,wBAAwB,CAAC,OAAO;IACzC,QAAQ,EAAE,wBAAwB,CAAC,QAAQ;IAC3C,WAAW,EAAE,wBAAwB,CAAC,WAAW;IACjD,QAAQ,EAAE,wBAAwB,CAAC,QAAQ;CAC5C,CAAC;AAEF,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC9C,OAAgB,EAChB,OAAe;IAEf,MAAM,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CAAC,CAAC;QAC/C,MAAM,EAAE;aACL,MAAM,CAAC,wBAAwB,CAAC;aAChC,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,wBAAwB,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,EACpD,EAAE,CAAC,wBAAwB,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,CACvD,CACF;aACA,GAAG,EAAE,CAAC;IACX,CAAC,EACD,KAAK,CAAC,YAAY,EAClB,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;IACF,mCAAmC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;AAC9D,CAAC","sourcesContent":["import { and, eq } from \"drizzle-orm\";\nimport { assertProjectionVerificationCurrent } from \"../../keyingProjectionVerification/types\";\nimport {\n principalHistoryEvidenceTables,\n principalHistoryPrefixes,\n} from \"../../sqlite/schema/principalHistoryEvidenceSchema\";\nimport { principalHistoryStageTables } from \"../../sqlite/schema/principalHistoryStageSchema\";\nimport { getClientSQLitePersistenceRuntime } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../../sqlite/sqlSchema\";\nimport { archivePrincipalHistoryKeyEnvelopes } from \"../principalKeys/principalKeyEnvelopeArchivePersistence\";\nimport { reclaimPrincipalHistoryNodes } from \"./principalHistoryNodeRetention\";\nimport {\n principalHistoryPrefixRootOwnerColumns,\n principalHistoryRootRetention,\n releasePrincipalHistoryRoot,\n writePrincipalHistoryRootEdges,\n} from \"./principalHistoryRootOwnership\";\nimport { reclaimCompletedPrincipalHistoryStages } from \"./principalHistoryStageRetention\";\n\nexport type PrincipalHistoryPrefix = Omit<\n typeof principalHistoryPrefixes.$inferSelect,\n \"rootHash\" | \"identityBundleFormat\"\n>;\n\nconst principalHistoryPrefixColumns = {\n scopeId: principalHistoryPrefixes.scopeId,\n organizationId: principalHistoryPrefixes.organizationId,\n version: principalHistoryPrefixes.version,\n headJson: principalHistoryPrefixes.headJson,\n currentJson: principalHistoryPrefixes.currentJson,\n progress: principalHistoryPrefixes.progress,\n};\n\nexport async function loadPrincipalHistoryPrefix(\n execSql: ExecSql,\n scopeId: string,\n): Promise<PrincipalHistoryPrefix | null> {\n await ensureSqlTables(execSql, principalHistoryEvidenceTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const [prefix] = await db\n .select(principalHistoryPrefixColumns)\n .from(principalHistoryPrefixes)\n .where(eq(principalHistoryPrefixes.scopeId, scopeId))\n .limit(1);\n return prefix ?? null;\n}\n\n/**\n * Preserve identical completed progress; this sink does not authenticate hints.\n * Callers must discard an unreadable prefix or identify it with rejectedPrefix\n * before publishing a repair of the same head and current artifacts.\n */\nexport async function savePrincipalHistoryPrefix(input: {\n readonly execSql: ExecSql;\n readonly prefix: PrincipalHistoryPrefix;\n readonly indexRootHash: string;\n readonly stillCurrent: () => boolean;\n readonly rejectedPrefix?: PrincipalHistoryPrefix | null;\n}): Promise<void> {\n const prefix = { ...input.prefix };\n const indexRootHash = input.indexRootHash;\n const rejected = input.rejectedPrefix ? { ...input.rejectedPrefix } : null;\n await ensureSqlTables(input.execSql, [\n ...principalHistoryEvidenceTables,\n ...principalHistoryStageTables,\n ]);\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n const result = await runtime.guardedTransaction(\n async (tx) => {\n const [previous] = await tx\n .select()\n .from(principalHistoryPrefixes)\n .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))\n .limit(1);\n const replacesRejected =\n previous &&\n rejected?.scopeId === previous.scopeId &&\n rejected.progress === previous.progress;\n if (previous && previous.version > prefix.version && !replacesRejected)\n return;\n if (\n previous &&\n previous.version === prefix.version &&\n previous.organizationId === prefix.organizationId &&\n previous.headJson === prefix.headJson &&\n previous.currentJson === prefix.currentJson &&\n !replacesRejected\n ) {\n // Resealing an unchanged prefix would invalidate an acknowledgement's\n // progress CAS even though its authenticated predecessor is unchanged.\n // Still refresh authenticated root ownership and retention below.\n // Reuse authenticates this opaque progress; rejected hints are removed\n // or explicitly replaced before repair can rely on the new prefix.\n prefix.progress = previous.progress;\n }\n // Archiving reads only these three fields, so an identical previous\n // prefix would repeat the same idempotent write.\n if (\n previous &&\n (previous.organizationId !== prefix.organizationId ||\n previous.currentJson !== prefix.currentJson ||\n previous.version !== prefix.version)\n )\n await archivePrincipalHistoryKeyEnvelopes(tx, previous);\n await archivePrincipalHistoryKeyEnvelopes(tx, prefix);\n const root = {\n scopeId: prefix.scopeId,\n organizationId: prefix.organizationId,\n rootHash: indexRootHash,\n };\n const row = { ...prefix, rootHash: indexRootHash };\n await tx\n .insert(principalHistoryPrefixes)\n .values(row)\n .onConflictDoUpdate({\n target: principalHistoryPrefixes.scopeId,\n set: row,\n })\n .run();\n await reclaimCompletedPrincipalHistoryStages(tx, prefix);\n await writePrincipalHistoryRootEdges(\n tx,\n principalHistoryRootRetention(previous, root),\n );\n await reclaimPrincipalHistoryNodes(tx, prefix);\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => result.committed);\n}\n\n/** Invalid hints are disposable, but a losing reader must preserve a newer hint. */\nexport async function discardPrincipalHistoryPrefix(input: {\n readonly execSql: ExecSql;\n readonly prefix: PrincipalHistoryPrefix;\n readonly stillCurrent: () => boolean;\n}): Promise<void> {\n const prefix = { ...input.prefix };\n await ensureSqlTables(input.execSql, principalHistoryEvidenceTables);\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n const result = await runtime.guardedTransaction(\n async (tx) => {\n const [current] = await tx\n .select({\n progress: principalHistoryPrefixes.progress,\n ...principalHistoryPrefixRootOwnerColumns,\n })\n .from(principalHistoryPrefixes)\n .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))\n .limit(1);\n if (!current || current.progress !== prefix.progress) return;\n await releasePrincipalHistoryRoot(tx, current);\n await tx\n .delete(principalHistoryPrefixes)\n .where(\n and(\n eq(principalHistoryPrefixes.scopeId, prefix.scopeId),\n eq(principalHistoryPrefixes.progress, prefix.progress),\n ),\n )\n .run();\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => result.committed);\n}\n"]}
1
+ {"version":3,"file":"principalHistoryPrefixPersistence.js","sourceRoot":"","sources":["principalHistoryPrefixPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,wBAAwB,EAAE,MAAM,oDAAoD,CAAC;AAE9F,OAAO,EAAE,mCAAmC,EAAE,MAAM,yDAAyD,CAAC;AAC9G,OAAO,EAAE,4BAA4B,EAAE,MAAM,iCAAiC,CAAC;AAC/E,OAAO,EACL,sCAAsC,EACtC,6BAA6B,EAC7B,2BAA2B,EAC3B,8BAA8B,GAC/B,MAAM,iCAAiC,CAAC;AACzC,OAAO,EAAE,sCAAsC,EAAE,MAAM,kCAAkC,CAAC;AAO1F,MAAM,CAAC,MAAM,6BAA6B,GAAG;IAC3C,OAAO,EAAE,wBAAwB,CAAC,OAAO;IACzC,cAAc,EAAE,wBAAwB,CAAC,cAAc;IACvD,OAAO,EAAE,wBAAwB,CAAC,OAAO;IACzC,QAAQ,EAAE,wBAAwB,CAAC,QAAQ;IAC3C,WAAW,EAAE,wBAAwB,CAAC,WAAW;IACjD,QAAQ,EAAE,wBAAwB,CAAC,QAAQ;CAC5C,CAAC;AAEF;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,uCAAuC,CAC3D,EAAgC,EAChC,MAA8B,EAC9B,aAAqB,EACrB,QAAuC;IAEvC,MAAM,CAAC,QAAQ,CAAC,GAAG,MAAM,EAAE;SACxB,MAAM,EAAE;SACR,IAAI,CAAC,wBAAwB,CAAC;SAC9B,KAAK,CAAC,EAAE,CAAC,wBAAwB,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;SAC3D,KAAK,CAAC,CAAC,CAAC,CAAC;IACZ,MAAM,gBAAgB,GACpB,QAAQ;Q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AClB,MAAM,EAAE,wBAAwB,CAAC,OAAO;QACxC,GAAG,EAAE,GAAG;KACT,CAAC;SACD,GAAG,EAAE,CAAC;IACT,MAAM,sCAAsC,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IACzD,MAAM,8BAA8B,CAClC,EAAE,EACF,6BAA6B,CAAC,QAAQ,EAAE,IAAI,CAAC,CAC9C,CAAC;IACF,MAAM,4BAA4B,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;AACjD,CAAC;AAED,oFAAoF;AACpF,MAAM,CAAC,KAAK,UAAU,0CAA0C,CAC9D,EAAgC,EAChC,MAA8B;IAE9B,MAAM,CAAC,OAAO,CAAC,GAAG,MAAM,EAAE;SACvB,MAAM,CAAC;QACN,QAAQ,EAAE,wBAAwB,CAAC,QAAQ;QAC3C,GAAG,sCAAsC;KAC1C,CAAC;SACD,IAAI,CAAC,wBAAwB,CAAC;SAC9B,KAAK,CAAC,EAAE,CAAC,wBAAwB,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,CAAC;SAC3D,KAAK,CAAC,CAAC,CAAC,CAAC;IACZ,IAAI,CAAC,OAAO,IAAI,OAAO,CAAC,QAAQ,KAAK,MAAM,CAAC,QAAQ;QAAE,OAAO;IAC7D,MAAM,2BAA2B,CAAC,EAAE,EAAE,OAAO,CAAC,CAAC;IAC/C,MAAM,EAAE;SACL,MAAM,CAAC,wBAAwB,CAAC;SAChC,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,wBAAwB,CAAC,OAAO,EAAE,MAAM,CAAC,OAAO,CAAC,EACpD,EAAE,CAAC,wBAAwB,CAAC,QAAQ,EAAE,MAAM,CAAC,QAAQ,CAAC,CACvD,CACF;SACA,GAAG,EAAE,CAAC;AACX,CAAC","sourcesContent":["import { and, eq } from \"drizzle-orm\";\nimport { principalHistoryPrefixes } from \"../../sqlite/schema/principalHistoryEvidenceSchema\";\nimport type { ClientSQLiteTransactionScope } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { archivePrincipalHistoryKeyEnvelopes } from \"../principalKeys/principalKeyEnvelopeArchivePersistence\";\nimport { reclaimPrincipalHistoryNodes } from \"./principalHistoryNodeRetention\";\nimport {\n principalHistoryPrefixRootOwnerColumns,\n principalHistoryRootRetention,\n releasePrincipalHistoryRoot,\n writePrincipalHistoryRootEdges,\n} from \"./principalHistoryRootOwnership\";\nimport { reclaimCompletedPrincipalHistoryStages } from \"./principalHistoryStageRetention\";\n\nexport type PrincipalHistoryPrefix = Omit<\n typeof principalHistoryPrefixes.$inferSelect,\n \"rootHash\" | \"identityBundleFormat\"\n>;\n\nexport const principalHistoryPrefixColumns = {\n scopeId: principalHistoryPrefixes.scopeId,\n organizationId: principalHistoryPrefixes.organizationId,\n version: principalHistoryPrefixes.version,\n headJson: principalHistoryPrefixes.headJson,\n currentJson: principalHistoryPrefixes.currentJson,\n progress: principalHistoryPrefixes.progress,\n};\n\n/**\n * Preserve identical completed progress; this sink does not authenticate hints.\n * Callers must discard an unreadable prefix or identify it with rejectedPrefix\n * before publishing a repair of the same head and current artifacts. Updates\n * the owned `prefix` copy's progress when it reuses the stored progress.\n */\nexport async function savePrincipalHistoryPrefixInTransaction(\n tx: ClientSQLiteTransactionScope,\n prefix: PrincipalHistoryPrefix,\n indexRootHash: string,\n rejected: PrincipalHistoryPrefix | null,\n): Promise<void> {\n const [previous] = await tx\n .select()\n .from(principalHistoryPrefixes)\n .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))\n .limit(1);\n const replacesRejected =\n previous &&\n rejected?.scopeId === previous.scopeId &&\n rejected.progress === previous.progress;\n if (previous && previous.version > prefix.version && !replacesRejected)\n return;\n if (\n previous &&\n previous.version === prefix.version &&\n previous.organizationId === prefix.organizationId &&\n previous.headJson === prefix.headJson &&\n previous.currentJson === prefix.currentJson &&\n !replacesRejected\n ) {\n // Resealing an unchanged prefix would invalidate an acknowledgement's\n // progress CAS even though its authenticated predecessor is unchanged.\n // Still refresh authenticated root ownership and retention below.\n // Reuse authenticates this opaque progress; rejected hints are removed\n // or explicitly replaced before repair can rely on the new prefix.\n prefix.progress = previous.progress;\n }\n // Archiving reads only these three fields, so an identical previous\n // prefix would repeat the same idempotent write.\n if (\n previous &&\n (previous.organizationId !== prefix.organizationId ||\n previous.currentJson !== prefix.currentJson ||\n previous.version !== prefix.version)\n )\n await archivePrincipalHistoryKeyEnvelopes(tx, previous);\n await archivePrincipalHistoryKeyEnvelopes(tx, prefix);\n const root = {\n scopeId: prefix.scopeId,\n organizationId: prefix.organizationId,\n rootHash: indexRootHash,\n };\n const row = { ...prefix, rootHash: indexRootHash };\n await tx\n .insert(principalHistoryPrefixes)\n .values(row)\n .onConflictDoUpdate({\n target: principalHistoryPrefixes.scopeId,\n set: row,\n })\n .run();\n await reclaimCompletedPrincipalHistoryStages(tx, prefix);\n await writePrincipalHistoryRootEdges(\n tx,\n principalHistoryRootRetention(previous, root),\n );\n await reclaimPrincipalHistoryNodes(tx, prefix);\n}\n\n/** Invalid hints are disposable, but a losing reader must preserve a newer hint. */\nexport async function discardPrincipalHistoryPrefixInTransaction(\n tx: ClientSQLiteTransactionScope,\n prefix: PrincipalHistoryPrefix,\n): Promise<void> {\n const [current] = await tx\n .select({\n progress: principalHistoryPrefixes.progress,\n ...principalHistoryPrefixRootOwnerColumns,\n })\n .from(principalHistoryPrefixes)\n .where(eq(principalHistoryPrefixes.scopeId, prefix.scopeId))\n .limit(1);\n if (!current || current.progress !== prefix.progress) return;\n await releasePrincipalHistoryRoot(tx, current);\n await tx\n .delete(principalHistoryPrefixes)\n .where(\n and(\n eq(principalHistoryPrefixes.scopeId, prefix.scopeId),\n eq(principalHistoryPrefixes.progress, prefix.progress),\n ),\n )\n .run();\n}\n"]}
@@ -0,0 +1,134 @@
1
+ import { eq } from "drizzle-orm";
2
+ import { assertProjectionVerificationCurrent } from "../../keyingProjectionVerification/types.js";
3
+ import { keyingCheckpointTables } from "../../sqlite/schema/keyingCheckpointSchema.js";
4
+ import { principalHistoryEvidenceTables, principalHistoryPrefixes, } from "../../sqlite/schema/principalHistoryEvidenceSchema.js";
5
+ import { principalHistoryStages, principalHistoryStageTables, } from "../../sqlite/schema/principalHistoryStageSchema.js";
6
+ import { principalPolicyTables } from "../../sqlite/schema/principalPolicySchema.js";
7
+ import { getClientSQLitePersistenceRuntime } from "../../sqlite/sqlitePersistenceRuntime.js";
8
+ import { ensureSqlTables } from "../../sqlite/sqlSchema.js";
9
+ import { assertPublicationScope, persistAcknowledgedPrincipalCurrentsInTransaction, } from "../principalCurrentAcknowledgementPersistence.js";
10
+ import { readPrincipalHistoryReferences, } from "./principalHistoryEvidencePersistence.js";
11
+ import { discardPrincipalHistoryPrefixInTransaction, principalHistoryPrefixColumns, savePrincipalHistoryPrefixInTransaction, } from "./principalHistoryPrefixPersistence.js";
12
+ import { discardPrincipalHistoryStageInTransaction, principalHistoryStageColumns, savePrincipalHistoryStageInTransaction, } from "./principalHistoryStagePersistence.js";
13
+ /**
14
+ * Principal history storage bound to one executor and clock: resumable
15
+ * stages, published prefixes, their evidence, and acknowledged currents. Each
16
+ * write ensures its tables and runs its own immediate guarded transaction.
17
+ * Work inside a caller's transaction takes the locked handle per call through
18
+ * the `*InTransaction` functions instead.
19
+ */
20
+ export class PrincipalHistoryRepository {
21
+ execSql;
22
+ now;
23
+ constructor(execSql, now = () => new Date()) {
24
+ this.execSql = execSql;
25
+ this.now = now;
26
+ }
27
+ async loadPrincipalHistoryStage(id) {
28
+ await ensureSqlTables(this.execSql, principalHistoryStageTables);
29
+ const { db } = getClientSQLitePersistenceRuntime(this.execSql);
30
+ const [row] = await db
31
+ .select(principalHistoryStageColumns)
32
+ .from(principalHistoryStages)
33
+ .where(eq(principalHistoryStages.id, id))
34
+ .limit(1);
35
+ return row ?? null;
36
+ }
37
+ /** Publish accepted entries and index nodes atomically with authenticated progress. */
38
+ async savePrincipalHistoryStage(input) {
39
+ const evidence = structuredClone(input.evidence);
40
+ const stage = { ...input.stage, scopeId: evidence.scopeId };
41
+ const previousProgress = input.previousProgress;
42
+ await ensureSqlTables(this.execSql, [
43
+ ...principalHistoryStageTables,
44
+ ...principalHistoryEvidenceTables,
45
+ ]);
46
+ const runtime = getClientSQLitePersistenceRuntime(this.execSql);
47
+ const saved = await runtime.guardedTransaction((tx) => savePrincipalHistoryStageInTransaction(tx, { stage, evidence, previousProgress }, this.now().getTime()), input.stillCurrent, { behavior: "immediate" });
48
+ assertProjectionVerificationCurrent(() => saved.committed);
49
+ }
50
+ async discardPrincipalHistoryStage(stage, stillCurrent) {
51
+ await ensureSqlTables(this.execSql, [
52
+ ...principalHistoryStageTables,
53
+ ...principalHistoryEvidenceTables,
54
+ ]);
55
+ const runtime = getClientSQLitePersistenceRuntime(this.execSql);
56
+ const discarded = await runtime.guardedTransaction((tx) => discardPrincipalHistoryStageInTransaction(tx, stage), stillCurrent, { behavior: "immediate" });
57
+ assertProjectionVerificationCurrent(() => discarded.committed);
58
+ }
59
+ async loadPrincipalHistoryPrefix(scopeId) {
60
+ await ensureSqlTables(this.execSql, principalHistoryEvidenceTables);
61
+ const { db } = getClientSQLitePersistenceRuntime(this.execSql);
62
+ const [prefix] = await db
63
+ .select(principalHistoryPrefixColumns)
64
+ .from(principalHistoryPrefixes)
65
+ .where(eq(principalHistoryPrefixes.scopeId, scopeId))
66
+ .limit(1);
67
+ return prefix ?? null;
68
+ }
69
+ /**
70
+ * Preserve identical completed progress; this sink does not authenticate hints.
71
+ * Callers must discard an unreadable prefix or identify it with rejectedPrefix
72
+ * before publishing a repair of the same head and current artifacts.
73
+ */
74
+ async savePrincipalHistoryPrefix(input) {
75
+ const prefix = { ...input.prefix };
76
+ const indexRootHash = input.indexRootHash;
77
+ const rejected = input.rejectedPrefix ? { ...input.rejectedPrefix } : null;
78
+ await ensureSqlTables(this.execSql, [
79
+ ...principalHistoryEvidenceTables,
80
+ ...principalHistoryStageTables,
81
+ ]);
82
+ const runtime = getClientSQLitePersistenceRuntime(this.execSql);
83
+ const result = await runtime.guardedTransaction((tx) => savePrincipalHistoryPrefixInTransaction(tx, prefix, indexRootHash, rejected), input.stillCurrent, { behavior: "immediate" });
84
+ assertProjectionVerificationCurrent(() => result.committed);
85
+ }
86
+ /** Invalid hints are disposable, but a losing reader must preserve a newer hint. */
87
+ async discardPrincipalHistoryPrefix(input) {
88
+ const prefix = { ...input.prefix };
89
+ await ensureSqlTables(this.execSql, principalHistoryEvidenceTables);
90
+ const runtime = getClientSQLitePersistenceRuntime(this.execSql);
91
+ const result = await runtime.guardedTransaction((tx) => discardPrincipalHistoryPrefixInTransaction(tx, prefix), input.stillCurrent, { behavior: "immediate" });
92
+ assertProjectionVerificationCurrent(() => result.committed);
93
+ }
94
+ /**
95
+ * Returns untrusted material, in `versions` order. The caller must verify it
96
+ * against the issued root. Index nodes are read one level at a time for every
97
+ * version and entries in one set read; the first version that fails, in
98
+ * order, throws the error loading it alone would.
99
+ */
100
+ async loadPrincipalHistoryReferences(input) {
101
+ await ensureSqlTables(this.execSql, principalHistoryEvidenceTables);
102
+ const { db } = getClientSQLitePersistenceRuntime(this.execSql);
103
+ return await readPrincipalHistoryReferences(db, input);
104
+ }
105
+ /** Returns untrusted material. The caller must verify it against the issued root. */
106
+ async loadPrincipalHistoryReference(input) {
107
+ const [loaded] = await this.loadPrincipalHistoryReferences({
108
+ ...input,
109
+ versions: [input.version],
110
+ });
111
+ if (!loaded)
112
+ throw new Error("Principal history reference was not loaded");
113
+ return input.expectedReference
114
+ ? { ...loaded, reference: input.expectedReference }
115
+ : loaded;
116
+ }
117
+ /** Store current artifacts and resumable progress in the same transaction as their pins. */
118
+ async persistAcknowledgedPrincipalCurrents(input) {
119
+ const entries = structuredClone(input.entries);
120
+ const retirements = structuredClone(input.retirements ?? []);
121
+ const organizationId = input.organizationId;
122
+ const stillCurrent = input.stillCurrent;
123
+ assertPublicationScope(entries, organizationId);
124
+ assertProjectionVerificationCurrent(stillCurrent);
125
+ await ensureSqlTables(this.execSql, [
126
+ ...principalPolicyTables,
127
+ ...keyingCheckpointTables,
128
+ ]);
129
+ const runtime = getClientSQLitePersistenceRuntime(this.execSql);
130
+ const stored = await runtime.guardedTransaction((tx) => persistAcknowledgedPrincipalCurrentsInTransaction(tx, { organizationId, entries, retirements }, this.now), stillCurrent, { behavior: "immediate" });
131
+ assertProjectionVerificationCurrent(() => stored.committed);
132
+ }
133
+ }
134
+ //# sourceMappingURL=principalHistoryRepository.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"principalHistoryRepository.js","sourceRoot":"","sources":["principalHistoryRepository.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACjC,OAAO,EAAE,mCAAmC,EAAE,MAAM,0CAA0C,CAAC;AAC/F,OAAO,EAAE,sBAAsB,EAAE,MAAM,4CAA4C,CAAC;AACpF,OAAO,EACL,8BAA8B,EAC9B,wBAAwB,GACzB,MAAM,oDAAoD,CAAC;AAC5D,OAAO,EACL,sBAAsB,EACtB,2BAA2B,GAC5B,MAAM,iDAAiD,CAAC;AACzD,OAAO,EAAE,qBAAqB,EAAE,MAAM,2CAA2C,CAAC;AAClF,OAAO,EAAE,iCAAiC,EAAE,MAAM,uCAAuC,CAAC;AAC1F,OAAO,EAAgB,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAEL,sBAAsB,EACtB,iDAAiD,GAClD,MAAM,+CAA+C,CAAC;AACvD,OAAO,EAEL,8BAA8B,GAC/B,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EACL,0CAA0C,EAE1C,6BAA6B,EAC7B,uCAAuC,GACxC,MAAM,qCAAqC,CAAC;AAC7C,OAAO,EACL,yCAAyC,EAEzC,4BAA4B,EAC5B,sCAAsC,GACvC,MAAM,oCAAoC,CAAC;AAE5C;;;;;;GAMG;AACH,MAAM,OAAO,0BAA0B;IACpB,OAAO,CAAU;IACjB,GAAG,CAAa;IAEjC,YAAY,OAAgB,EAAE,MAAkB,GAAG,EAAE,CAAC,IAAI,IAAI,EAAE;Q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E,MAAM,MAAM,GAAG,MAAM,OAAO,CAAC,kBAAkB,CAC7C,CAAC,EAAE,EAAE,EAAE,CACL,iDAAiD,CAC/C,EAAE,EACF,EAAE,cAAc,EAAE,OAAO,EAAE,WAAW,EAAE,EACxC,IAAI,CAAC,GAAG,CACT,EACH,YAAY,EACZ,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;QACF,mCAAmC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;IAC9D,CAAC;CACF","sourcesContent":["import type {\n PrincipalPolicyHistoryReferenceProof,\n ReferencedPrincipalHead,\n VerifiedPrincipalPolicyHistory,\n} from \"@tearleads/crypto\";\nimport { eq } from \"drizzle-orm\";\nimport { assertProjectionVerificationCurrent } from \"../../keyingProjectionVerification/types\";\nimport { keyingCheckpointTables } from \"../../sqlite/schema/keyingCheckpointSchema\";\nimport {\n principalHistoryEvidenceTables,\n principalHistoryPrefixes,\n} from \"../../sqlite/schema/principalHistoryEvidenceSchema\";\nimport {\n principalHistoryStages,\n principalHistoryStageTables,\n} from \"../../sqlite/schema/principalHistoryStageSchema\";\nimport { principalPolicyTables } from \"../../sqlite/schema/principalPolicySchema\";\nimport { getClientSQLitePersistenceRuntime } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../../sqlite/sqlSchema\";\nimport {\n type AcknowledgedPrincipalCurrents,\n assertPublicationScope,\n persistAcknowledgedPrincipalCurrentsInTransaction,\n} from \"../principalCurrentAcknowledgementPersistence\";\nimport {\n type PrincipalHistoryEvidencePage,\n readPrincipalHistoryReferences,\n} from \"./principalHistoryEvidencePersistence\";\nimport {\n discardPrincipalHistoryPrefixInTransaction,\n type PrincipalHistoryPrefix,\n principalHistoryPrefixColumns,\n savePrincipalHistoryPrefixInTransaction,\n} from \"./principalHistoryPrefixPersistence\";\nimport {\n discardPrincipalHistoryStageInTransaction,\n type PrincipalHistoryStage,\n principalHistoryStageColumns,\n savePrincipalHistoryStageInTransaction,\n} from \"./principalHistoryStagePersistence\";\n\n/**\n * Principal history storage bound to one executor and clock: resumable\n * stages, published prefixes, their evidence, and acknowledged currents. Each\n * write ensures its tables and runs its own immediate guarded transaction.\n * Work inside a caller's transaction takes the locked handle per call through\n * the `*InTransaction` functions instead.\n */\nexport class PrincipalHistoryRepository {\n private readonly execSql: ExecSql;\n private readonly now: () => Date;\n\n constructor(execSql: ExecSql, now: () => Date = () => new Date()) {\n this.execSql = execSql;\n this.now = now;\n }\n\n async loadPrincipalHistoryStage(\n id: string,\n ): Promise<PrincipalHistoryStage | null> {\n await ensureSqlTables(this.execSql, principalHistoryStageTables);\n const { db } = getClientSQLitePersistenceRuntime(this.execSql);\n const [row] = await db\n .select(principalHistoryStageColumns)\n .from(principalHistoryStages)\n .where(eq(principalHistoryStages.id, id))\n .limit(1);\n return row ?? null;\n }\n\n /** Publish accepted entries and index nodes atomically with authenticated progress. */\n async savePrincipalHistoryStage(input: {\n readonly stage: PrincipalHistoryStage;\n readonly evidence: PrincipalHistoryEvidencePage;\n readonly previousProgress: string | null;\n readonly stillCurrent: () => boolean;\n }): Promise<void> {\n const evidence = structuredClone(input.evidence);\n const stage = { ...input.stage, scopeId: evidence.scopeId };\n const previousProgress = input.previousProgress;\n await ensureSqlTables(this.execSql, [\n ...principalHistoryStageTables,\n ...principalHistoryEvidenceTables,\n ]);\n const runtime = getClientSQLitePersistenceRuntime(this.execSql);\n const saved = await runtime.guardedTransaction(\n (tx) =>\n savePrincipalHistoryStageInTransaction(\n tx,\n { stage, evidence, previousProgress },\n this.now().getTime(),\n ),\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => saved.committed);\n }\n\n async discardPrincipalHistoryStage(\n stage: PrincipalHistoryStage,\n stillCurrent: () => boolean,\n ): Promise<void> {\n await ensureSqlTables(this.execSql, [\n ...principalHistoryStageTables,\n ...principalHistoryEvidenceTables,\n ]);\n const runtime = getClientSQLitePersistenceRuntime(this.execSql);\n const discarded = await runtime.guardedTransaction(\n (tx) => discardPrincipalHistoryStageInTransaction(tx, stage),\n stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => discarded.committed);\n }\n\n async loadPrincipalHistoryPrefix(\n scopeId: string,\n ): Promise<PrincipalHistoryPrefix | null> {\n await ensureSqlTables(this.execSql, principalHistoryEvidenceTables);\n const { db } = getClientSQLitePersistenceRuntime(this.execSql);\n const [prefix] = await db\n .select(principalHistoryPrefixColumns)\n .from(principalHistoryPrefixes)\n .where(eq(principalHistoryPrefixes.scopeId, scopeId))\n .limit(1);\n return prefix ?? null;\n }\n\n /**\n * Preserve identical completed progress; this sink does not authenticate hints.\n * Callers must discard an unreadable prefix or identify it with rejectedPrefix\n * before publishing a repair of the same head and current artifacts.\n */\n async savePrincipalHistoryPrefix(input: {\n readonly prefix: PrincipalHistoryPrefix;\n readonly indexRootHash: string;\n readonly stillCurrent: () => boolean;\n readonly rejectedPrefix?: PrincipalHistoryPrefix | null;\n }): Promise<void> {\n const prefix = { ...input.prefix };\n const indexRootHash = input.indexRootHash;\n const rejected = input.rejectedPrefix ? { ...input.rejectedPrefix } : null;\n await ensureSqlTables(this.execSql, [\n ...principalHistoryEvidenceTables,\n ...principalHistoryStageTables,\n ]);\n const runtime = getClientSQLitePersistenceRuntime(this.execSql);\n const result = await runtime.guardedTransaction(\n (tx) =>\n savePrincipalHistoryPrefixInTransaction(\n tx,\n prefix,\n indexRootHash,\n rejected,\n ),\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => result.committed);\n }\n\n /** Invalid hints are disposable, but a losing reader must preserve a newer hint. */\n async discardPrincipalHistoryPrefix(input: {\n readonly prefix: PrincipalHistoryPrefix;\n readonly stillCurrent: () => boolean;\n }): Promise<void> {\n const prefix = { ...input.prefix };\n await ensureSqlTables(this.execSql, principalHistoryEvidenceTables);\n const runtime = getClientSQLitePersistenceRuntime(this.execSql);\n const result = await runtime.guardedTransaction(\n (tx) => discardPrincipalHistoryPrefixInTransaction(tx, prefix),\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => result.committed);\n }\n\n /**\n * Returns untrusted material, in `versions` order. The caller must verify it\n * against the issued root. Index nodes are read one level at a time for every\n * version and entries in one set read; the first version that fails, in\n * order, throws the error loading it alone would.\n */\n async loadPrincipalHistoryReferences(input: {\n readonly scopeId: string;\n readonly history: VerifiedPrincipalPolicyHistory;\n readonly versions: readonly number[];\n }): Promise<PrincipalPolicyHistoryReferenceProof[]> {\n await ensureSqlTables(this.execSql, principalHistoryEvidenceTables);\n const { db } = getClientSQLitePersistenceRuntime(this.execSql);\n return await readPrincipalHistoryReferences(db, input);\n }\n\n /** Returns untrusted material. The caller must verify it against the issued root. */\n async loadPrincipalHistoryReference(input: {\n readonly scopeId: string;\n readonly history: VerifiedPrincipalPolicyHistory;\n readonly version: number;\n readonly expectedReference?: ReferencedPrincipalHead | undefined;\n }): Promise<PrincipalPolicyHistoryReferenceProof> {\n const [loaded] = await this.loadPrincipalHistoryReferences({\n ...input,\n versions: [input.version],\n });\n if (!loaded) throw new Error(\"Principal history reference was not loaded\");\n return input.expectedReference\n ? { ...loaded, reference: input.expectedReference }\n : loaded;\n }\n\n /** Store current artifacts and resumable progress in the same transaction as their pins. */\n async persistAcknowledgedPrincipalCurrents(\n input: AcknowledgedPrincipalCurrents,\n ): Promise<void> {\n const entries = structuredClone(input.entries);\n const retirements = structuredClone(input.retirements ?? []);\n const organizationId = input.organizationId;\n const stillCurrent = input.stillCurrent;\n assertPublicationScope(entries, organizationId);\n assertProjectionVerificationCurrent(stillCurrent);\n await ensureSqlTables(this.execSql, [\n ...principalPolicyTables,\n ...keyingCheckpointTables,\n ]);\n const runtime = getClientSQLitePersistenceRuntime(this.execSql);\n const stored = await runtime.guardedTransaction(\n (tx) =>\n persistAcknowledgedPrincipalCurrentsInTransaction(\n tx,\n { organizationId, entries, retirements },\n this.now,\n ),\n stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => stored.committed);\n }\n}\n"]}
@@ -1,17 +1,128 @@
1
1
  import { principalHistoryStages } from "../../sqlite/schema/principalHistoryStageSchema.js";
2
- import { type ExecSql } from "../../sqlite/sqlSchema.js";
2
+ import type { ClientSQLiteTransactionScope } from "../../sqlite/sqlitePersistenceRuntime.js";
3
3
  import { type PrincipalHistoryEvidencePage } from "./principalHistoryEvidencePersistence.js";
4
4
  /** A stage's verification work; its retention hints are written by persistence. */
5
5
  export type PrincipalHistoryStage = Omit<typeof principalHistoryStages.$inferSelect, "scopeId" | "touchedAt" | "rootHash" | "identityBundleFormat">;
6
+ export declare const principalHistoryStageColumns: {
7
+ id: import("drizzle-orm/sqlite-core").SQLiteColumn<{
8
+ name: "id";
9
+ tableName: "principal_history_stages";
10
+ dataType: "string";
11
+ columnType: "SQLiteText";
12
+ data: string;
13
+ driverParam: string;
14
+ notNull: true;
15
+ hasDefault: false;
16
+ isPrimaryKey: true;
17
+ isAutoincrement: false;
18
+ hasRuntimeDefault: false;
19
+ enumValues: [string, ...string[]];
20
+ baseColumn: never;
21
+ identity: undefined;
22
+ generated: undefined;
23
+ }, {}, {
24
+ length: number | undefined;
25
+ }>;
26
+ organizationId: import("drizzle-orm/sqlite-core").SQLiteColumn<{
27
+ name: "organization_id";
28
+ tableName: "principal_history_stages";
29
+ dataType: "string";
30
+ columnType: "SQLiteText";
31
+ data: string;
32
+ driverParam: string;
33
+ notNull: true;
34
+ hasDefault: false;
35
+ isPrimaryKey: false;
36
+ isAutoincrement: false;
37
+ hasRuntimeDefault: false;
38
+ enumValues: [string, ...string[]];
39
+ baseColumn: never;
40
+ identity: undefined;
41
+ generated: undefined;
42
+ }, {}, {
43
+ length: number | undefined;
44
+ }>;
45
+ currentJson: import("drizzle-orm/sqlite-core").SQLiteColumn<{
46
+ name: "current_json";
47
+ tableName: "principal_history_stages";
48
+ dataType: "string";
49
+ columnType: "SQLiteText";
50
+ data: string;
51
+ driverParam: string;
52
+ notNull: true;
53
+ hasDefault: false;
54
+ isPrimaryKey: false;
55
+ isAutoincrement: false;
56
+ hasRuntimeDefault: false;
57
+ enumValues: [string, ...string[]];
58
+ baseColumn: never;
59
+ identity: undefined;
60
+ generated: undefined;
61
+ }, {}, {
62
+ length: number | undefined;
63
+ }>;
64
+ afterVersion: import("drizzle-orm/sqlite-core").SQLiteColumn<{
65
+ name: "after_version";
66
+ tableName: "principal_history_stages";
67
+ dataType: "number";
68
+ columnType: "SQLiteInteger";
69
+ data: number;
70
+ driverParam: number;
71
+ notNull: true;
72
+ hasDefault: false;
73
+ isPrimaryKey: false;
74
+ isAutoincrement: false;
75
+ hasRuntimeDefault: false;
76
+ enumValues: undefined;
77
+ baseColumn: never;
78
+ identity: undefined;
79
+ generated: undefined;
80
+ }, {}, {}>;
81
+ complete: import("drizzle-orm/sqlite-core").SQLiteColumn<{
82
+ name: "complete";
83
+ tableName: "principal_history_stages";
84
+ dataType: "boolean";
85
+ columnType: "SQLiteBoolean";
86
+ data: boolean;
87
+ driverParam: number;
88
+ notNull: true;
89
+ hasDefault: false;
90
+ isPrimaryKey: false;
91
+ isAutoincrement: false;
92
+ hasRuntimeDefault: false;
93
+ enumValues: undefined;
94
+ baseColumn: never;
95
+ identity: undefined;
96
+ generated: undefined;
97
+ }, {}, {}>;
98
+ progress: import("drizzle-orm/sqlite-core").SQLiteColumn<{
99
+ name: "progress";
100
+ tableName: "principal_history_stages";
101
+ dataType: "string";
102
+ columnType: "SQLiteText";
103
+ data: string;
104
+ driverParam: string;
105
+ notNull: true;
106
+ hasDefault: false;
107
+ isPrimaryKey: false;
108
+ isAutoincrement: false;
109
+ hasRuntimeDefault: false;
110
+ enumValues: [string, ...string[]];
111
+ baseColumn: never;
112
+ identity: undefined;
113
+ generated: undefined;
114
+ }, {}, {
115
+ length: number | undefined;
116
+ }>;
117
+ };
6
118
  /** Another writer replaced this stage's progress between load and save. */
7
119
  export declare function isPrincipalHistoryStageChangedError(error: unknown): boolean;
8
- export declare function loadPrincipalHistoryStage(execSql: ExecSql, id: string): Promise<PrincipalHistoryStage | null>;
9
120
  /** Publish accepted entries and index nodes atomically with authenticated progress. */
10
- export declare function savePrincipalHistoryStage(input: {
11
- readonly execSql: ExecSql;
12
- readonly stage: PrincipalHistoryStage;
121
+ export declare function savePrincipalHistoryStageInTransaction(tx: ClientSQLiteTransactionScope, input: {
122
+ readonly stage: PrincipalHistoryStage & {
123
+ readonly scopeId: string;
124
+ };
13
125
  readonly evidence: PrincipalHistoryEvidencePage;
14
126
  readonly previousProgress: string | null;
15
- readonly stillCurrent: () => boolean;
16
- }): Promise<void>;
17
- export declare function discardPrincipalHistoryStage(execSql: ExecSql, stage: PrincipalHistoryStage, stillCurrent: () => boolean): Promise<void>;
127
+ }, touchedAt: number): Promise<void>;
128
+ export declare function discardPrincipalHistoryStageInTransaction(tx: ClientSQLiteTransactionScope, stage: PrincipalHistoryStage): Promise<void>;