@tearleads/client-sdk 0.1.82 → 0.1.84

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 (157) hide show
  1. package/data/documents/documentSync.js.map +1 -1
  2. package/data/documents/shared/crypto.js.map +1 -1
  3. package/data/documents/shared/types.d.ts +1 -1
  4. package/data/documents/shared/types.js.map +1 -1
  5. package/data/persistence/container-contents/containerContentsPersistence.js +2 -2
  6. package/data/persistence/container-contents/containerContentsPersistence.js.map +1 -1
  7. package/data/persistence/container-contents/containerContentsPersistenceTypes.d.ts +1 -1
  8. package/data/persistence/container-contents/containerContentsPersistenceTypes.js.map +1 -1
  9. package/data/persistence/container-contents/containerDeletionPersistence.js +1 -1
  10. package/data/persistence/container-contents/containerDeletionPersistence.js.map +1 -1
  11. package/data/persistence/container-contents/containerMetadataMutationPersistence.js +3 -3
  12. package/data/persistence/container-contents/containerMetadataMutationPersistence.js.map +1 -1
  13. package/data/persistence/container-contents/containerMetadataPullContinuationPersistence.js +1 -1
  14. package/data/persistence/container-contents/containerMetadataPullContinuationPersistence.js.map +1 -1
  15. package/data/persistence/container-contents/containerMetadataRows.js +1 -1
  16. package/data/persistence/container-contents/containerMetadataRows.js.map +1 -1
  17. package/data/persistence/container-contents/containerPendingUpdatePersistence.js +2 -2
  18. package/data/persistence/container-contents/containerPendingUpdatePersistence.js.map +1 -1
  19. package/data/persistence/container-contents/containerReconcilePersistence.js +1 -1
  20. package/data/persistence/container-contents/containerReconcilePersistence.js.map +1 -1
  21. package/data/persistence/container-contents/containerRemovalWork.js +1 -1
  22. package/data/persistence/container-contents/containerRemovalWork.js.map +1 -1
  23. package/data/persistence/documents/documentDiscoveryReset.d.ts +6 -0
  24. package/data/persistence/documents/documentsPersistence.js +2 -2
  25. package/data/persistence/documents/documentsPersistence.js.map +1 -1
  26. package/data/persistence/documents/internal/createDocumentWithHistoryCheckpoint.js +1 -1
  27. package/data/persistence/documents/internal/createDocumentWithHistoryCheckpoint.js.map +1 -1
  28. package/data/persistence/documents/internal/discardDocument.js +2 -2
  29. package/data/persistence/documents/internal/discardDocument.js.map +1 -1
  30. package/data/persistence/documents/internal/documentDeletionPersistence.js +2 -2
  31. package/data/persistence/documents/internal/documentDeletionPersistence.js.map +1 -1
  32. package/data/persistence/documents/internal/documentMutationPersistence.js +3 -3
  33. package/data/persistence/documents/internal/documentMutationPersistence.js.map +1 -1
  34. package/data/persistence/documents/internal/documentPullContinuationPersistence.js +1 -1
  35. package/data/persistence/documents/internal/documentPullContinuationPersistence.js.map +1 -1
  36. package/data/persistence/documents/internal/documentRecordIdentity.js.map +1 -1
  37. package/data/persistence/documents/internal/documentRows.js.map +1 -1
  38. package/data/persistence/documents/internal/ensureDocumentsSchema.js +1 -1
  39. package/data/persistence/documents/internal/ensureDocumentsSchema.js.map +1 -1
  40. package/data/persistence/documents/internal/orphanSideRows.js +1 -1
  41. package/data/persistence/documents/internal/orphanSideRows.js.map +1 -1
  42. package/data/persistence/documents/internal/rowQueryPersistence.js +1 -1
  43. package/data/persistence/documents/internal/rowQueryPersistence.js.map +1 -1
  44. package/data/persistence/documents/internal/syncQueuePersistence.js +2 -2
  45. package/data/persistence/documents/internal/syncQueuePersistence.js.map +1 -1
  46. package/data/{sqlite/documentPersistence → persistence/documents/records}/documentHistoryPersistence.d.ts +1 -1
  47. package/data/{sqlite/documentPersistence → persistence/documents/records}/documentHistoryPersistence.js +5 -5
  48. package/data/persistence/documents/records/documentHistoryPersistence.js.map +1 -0
  49. package/data/{sqlite/documentPersistence → persistence/documents/records}/documentPendingUpdatePersistence.d.ts +2 -2
  50. package/data/{sqlite/documentPersistence → persistence/documents/records}/documentPendingUpdatePersistence.js +2 -2
  51. package/data/persistence/documents/records/documentPendingUpdatePersistence.js.map +1 -0
  52. package/data/{sqlite/documentPersistence → persistence/documents/records}/documentPersistenceTypes.d.ts +2 -2
  53. package/data/persistence/documents/records/documentPersistenceTypes.js.map +1 -0
  54. package/data/{sqlite/documentPersistence → persistence/documents/records}/documentRecordPersistence.d.ts +1 -1
  55. package/data/{sqlite/documentPersistence → persistence/documents/records}/documentRecordPersistence.js +4 -4
  56. package/data/persistence/documents/records/documentRecordPersistence.js.map +1 -0
  57. package/data/{sqlite/documentPersistence → persistence/documents/records}/documentSyncFailurePersistence.d.ts +2 -2
  58. package/data/{sqlite/documentPersistence → persistence/documents/records}/documentSyncFailurePersistence.js +3 -3
  59. package/data/persistence/documents/records/documentSyncFailurePersistence.js.map +1 -0
  60. package/data/{sqlite/documentPersistence → persistence/documents/records}/index.d.ts +2 -2
  61. package/data/{sqlite/documentPersistence → persistence/documents/records}/index.js +4 -4
  62. package/data/persistence/documents/records/index.js.map +1 -0
  63. package/data/persistence/documents/types.d.ts +2 -2
  64. package/data/persistence/documents/types.js.map +1 -1
  65. package/{workflows/sync/remoteResetPrincipalScope.d.ts → data/persistence/principalPolicy/principalPolicyRemoteReset.d.ts} +1 -1
  66. package/{workflows/sync/remoteResetPrincipalScope.js → data/persistence/principalPolicy/principalPolicyRemoteReset.js} +7 -7
  67. package/data/persistence/principalPolicy/principalPolicyRemoteReset.js.map +1 -0
  68. package/data/persistence/verifiedManifestBundlePersistence.d.ts +2 -0
  69. package/data/persistence/verifiedManifestBundlePersistence.js +7 -0
  70. package/data/persistence/verifiedManifestBundlePersistence.js.map +1 -1
  71. package/data/sqlite/{documentPersistence/documentOrphanBlobReclaims.js → schema/documentOrphanBlobReclaimSchema.js} +1 -1
  72. package/data/sqlite/schema/documentOrphanBlobReclaimSchema.js.map +1 -0
  73. package/data/sqlite/schema/index.js +1 -1
  74. package/data/sqlite/schema/index.js.map +1 -1
  75. package/data/sqlite/sqliteBindBudget.js +12 -0
  76. package/data/sqlite/sqliteBindBudget.js.map +1 -1
  77. package/package.json +1 -1
  78. package/workflows/container-contents/container-state/create/createIntentSeed.js.map +1 -1
  79. package/workflows/container-contents/container-state/create/createWithMetadataPlan.js.map +1 -1
  80. package/workflows/container-contents/container-state/types.js.map +1 -1
  81. package/workflows/container-contents/metadata/index.js.map +1 -1
  82. package/workflows/container-contents/metadata/metadataEvents.d.ts +1 -1
  83. package/workflows/container-contents/metadata/metadataEvents.js.map +1 -1
  84. package/workflows/container-contents/metadata/metadataIncomingUpdateIsolation.js +1 -1
  85. package/workflows/container-contents/metadata/metadataIncomingUpdateIsolation.js.map +1 -1
  86. package/workflows/container-contents/metadata/metadataRemoteSync.d.ts +1 -1
  87. package/workflows/container-contents/metadata/metadataRemoteSync.js.map +1 -1
  88. package/workflows/container-contents/pendingWrites/index.js +1 -1
  89. package/workflows/container-contents/pendingWrites/index.js.map +1 -1
  90. package/workflows/container-contents/recovery/accessRestoration.js +1 -1
  91. package/workflows/container-contents/recovery/accessRestoration.js.map +1 -1
  92. package/workflows/container-contents/remoteReset/containerRemoteReset.d.ts +185 -0
  93. package/workflows/container-contents/remoteReset/containerRemoteReset.js +174 -0
  94. package/workflows/container-contents/remoteReset/containerRemoteReset.js.map +1 -0
  95. package/workflows/container-contents/remoteReset/remoteResetCursorScope.d.ts +14 -0
  96. package/workflows/{sync → container-contents/remoteReset}/remoteResetCursorScope.js +2 -2
  97. package/workflows/container-contents/remoteReset/remoteResetCursorScope.js.map +1 -0
  98. package/workflows/documents/create/documentCreationPersistence.js.map +1 -1
  99. package/workflows/documents/create/index.d.ts +1 -1
  100. package/workflows/documents/create/index.js.map +1 -1
  101. package/workflows/documents/documentMutationCommit.d.ts +1 -1
  102. package/workflows/documents/documentMutationCommit.js.map +1 -1
  103. package/workflows/documents/index.d.ts +2 -2
  104. package/workflows/documents/index.js +2 -2
  105. package/workflows/documents/index.js.map +1 -1
  106. package/workflows/documents/remoteReset/documentRemoteReset.d.ts +56 -0
  107. package/workflows/documents/remoteReset/documentRemoteReset.js +135 -0
  108. package/workflows/documents/remoteReset/documentRemoteReset.js.map +1 -0
  109. package/workflows/{sync → documents/remoteReset}/remoteResetAttachments.js +3 -3
  110. package/workflows/documents/remoteReset/remoteResetAttachments.js.map +1 -0
  111. package/workflows/{sync → documents/remoteReset}/remoteResetPlans.d.ts +1 -1
  112. package/workflows/{sync → documents/remoteReset}/remoteResetPlans.js +7 -7
  113. package/workflows/documents/remoteReset/remoteResetPlans.js.map +1 -0
  114. package/workflows/documents/sync/index.js.map +1 -1
  115. package/workflows/documents/sync/initialSync.d.ts +1 -1
  116. package/workflows/documents/sync/initialSync.js.map +1 -1
  117. package/workflows/documents/sync/readOnlySync.d.ts +1 -1
  118. package/workflows/documents/sync/readOnlySync.js.map +1 -1
  119. package/workflows/documents/sync/syncAttemptPlanning.js.map +1 -1
  120. package/workflows/documents/sync/syncAttemptState.d.ts +1 -1
  121. package/workflows/documents/sync/syncAttemptState.js.map +1 -1
  122. package/workflows/documents/sync/syncResponseResult.js.map +1 -1
  123. package/workflows/documents/sync/syncSubmittedResult.js.map +1 -1
  124. package/workflows/documents/syncFailures/index.js.map +1 -1
  125. package/workflows/documents/syncPlan/syncContentKeys.d.ts +1 -1
  126. package/workflows/documents/syncPlan/syncContentKeys.js.map +1 -1
  127. package/workflows/documents/syncPlan/syncPlanMaterial.js.map +1 -1
  128. package/workflows/documents/syncPlan/syncPlanRequestBounds.js.map +1 -1
  129. package/workflows/documents/syncRekeys/syncContainerRekeys.js.map +1 -1
  130. package/workflows/documents/syncRekeys/syncRecoveryRekey.d.ts +1 -1
  131. package/workflows/documents/syncRekeys/syncRecoveryRekey.js +1 -1
  132. package/workflows/documents/syncRekeys/syncRecoveryRekey.js.map +1 -1
  133. package/workflows/organizations/readModel/listLocalOrganizations.js +1 -1
  134. package/workflows/organizations/readModel/listLocalOrganizations.js.map +1 -1
  135. package/workflows/organizations/readModel/organizationRemoteReset.js +25 -0
  136. package/workflows/organizations/readModel/organizationRemoteReset.js.map +1 -0
  137. package/workflows/registration/persistRegistrationBootstrap.d.ts +1 -1
  138. package/workflows/registration/persistRegistrationBootstrap.js.map +1 -1
  139. package/workflows/sync/remoteReset.js +60 -229
  140. package/workflows/sync/remoteReset.js.map +1 -1
  141. package/workflows/sync/remoteResetScope.d.ts +7 -10
  142. package/workflows/sync/remoteResetScope.js +26 -108
  143. package/workflows/sync/remoteResetScope.js.map +1 -1
  144. package/data/sqlite/documentPersistence/documentHistoryPersistence.js.map +0 -1
  145. package/data/sqlite/documentPersistence/documentOrphanBlobReclaims.js.map +0 -1
  146. package/data/sqlite/documentPersistence/documentPendingUpdatePersistence.js.map +0 -1
  147. package/data/sqlite/documentPersistence/documentPersistenceTypes.js.map +0 -1
  148. package/data/sqlite/documentPersistence/documentRecordPersistence.js.map +0 -1
  149. package/data/sqlite/documentPersistence/documentSyncFailurePersistence.js.map +0 -1
  150. package/data/sqlite/documentPersistence/index.js.map +0 -1
  151. package/workflows/sync/remoteResetAttachments.js.map +0 -1
  152. package/workflows/sync/remoteResetBatches.js +0 -13
  153. package/workflows/sync/remoteResetBatches.js.map +0 -1
  154. package/workflows/sync/remoteResetCursorScope.js.map +0 -1
  155. package/workflows/sync/remoteResetPlans.js.map +0 -1
  156. package/workflows/sync/remoteResetPrincipalScope.js.map +0 -1
  157. /package/data/{sqlite/documentPersistence → persistence/documents/records}/documentPersistenceTypes.js +0 -0
@@ -1,10 +1,10 @@
1
1
  import { and, eq, or } from "drizzle-orm";
2
- import { principalPolicies, principalPolicyBundleHistory, principalPolicyBundleReferences, principalPolicyCheckpoints, principalPolicyOrganizations, } from "../../data/sqlite/schema/index.js";
3
- import { principalHistoryEntries, principalHistoryNodes, principalHistoryPrefixes, } from "../../data/sqlite/schema/principalHistoryEvidenceSchema.js";
4
- import { principalHistoryNodeReferences, principalHistoryRootOwners, } from "../../data/sqlite/schema/principalHistoryNodeRetentionSchema.js";
5
- import { principalKeyEnvelopeArchive } from "../../data/sqlite/schema/principalHistoryRetentionSchema.js";
6
- import { principalHistoryStages } from "../../data/sqlite/schema/principalHistoryStageSchema.js";
7
- import { remoteResetBatches } from "./remoteResetBatches.js";
2
+ import { principalPolicies, principalPolicyBundleHistory, principalPolicyBundleReferences, principalPolicyCheckpoints, principalPolicyOrganizations, } from "../../sqlite/schema/index.js";
3
+ import { principalHistoryEntries, principalHistoryNodes, principalHistoryPrefixes, } from "../../sqlite/schema/principalHistoryEvidenceSchema.js";
4
+ import { principalHistoryNodeReferences, principalHistoryRootOwners, } from "../../sqlite/schema/principalHistoryNodeRetentionSchema.js";
5
+ import { principalKeyEnvelopeArchive } from "../../sqlite/schema/principalHistoryRetentionSchema.js";
6
+ import { principalHistoryStages } from "../../sqlite/schema/principalHistoryStageSchema.js";
7
+ import { remoteResetBatches } from "../../sqlite/sqliteBindBudget.js";
8
8
  /** Current cache writes atomically pin ownership; reset never backfills it. */
9
9
  export async function loadRemoteResetPrincipalScope(tx, organizationId) {
10
10
  const [policyRows, retainedRows, ownershipRows] = await Promise.all([
@@ -100,4 +100,4 @@ export async function clearRemoteResetPrincipalRows(input) {
100
100
  }
101
101
  }
102
102
  }
103
- //# sourceMappingURL=remoteResetPrincipalScope.js.map
103
+ //# sourceMappingURL=principalPolicyRemoteReset.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"principalPolicyRemoteReset.js","sourceRoot":"","sources":["principalPolicyRemoteReset.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EACL,iBAAiB,EACjB,4BAA4B,EAC5B,+BAA+B,EAC/B,0BAA0B,EAC1B,4BAA4B,GAC7B,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,uBAAuB,EACvB,qBAAqB,EACrB,wBAAwB,GACzB,MAAM,oDAAoD,CAAC;AAC5D,OAAO,EACL,8BAA8B,EAC9B,0BAA0B,GAC3B,MAAM,yDAAyD,CAAC;AACjE,OAAO,EAAE,2BAA2B,EAAE,MAAM,qDAAqD,CAAC;AAClG,OAAO,EAAE,sBAAsB,EAAE,MAAM,iDAAiD,CAAC;AACzF,OAAO,EAAE,kBAAkB,EAAE,MAAM,+BAA+B,CAAC;AAanE,+EAA+E;AAC/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,MAAM,EAAE,CAAC,EAAE,CAAC;AAC3D,CAAC;AAED,+EAA+E;AAC/E,4EAA4E;AAC5E,MAAM,CAAC,KAAK,UAAU,6BAA6B,CAAC,KAInD;IACC,KAAK,MAAM,KAAK,IAAI;QAClB,8BAA8B;QAC9B,0BAA0B;QAC1B,uBAAuB;QACvB,qBAAqB;QACrB,wBAAwB;QACxB,2BAA2B;KAC5B;QACC,MAAM,KAAK,CAAC,EAAE;aACX,MAAM,CAAC,KAAK,CAAC;aACb,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,cAAc,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;aACrD,GAAG,EAAE,CAAC;IACX,MAAM,KAAK,CAAC,EAAE;SACX,MAAM,CAAC,sBAAsB,CAAC;SAC9B,KAAK,CAAC,EAAE,CAAC,sBAAsB,CAAC,cAAc,EAAE,KAAK,CAAC,cAAc,CAAC,CAAC;SACtE,GAAG,EAAE,CAAC;IACT,KAAK,MAAM,cAAc,IAAI,kBAAkB,CAC7C,KAAK,CAAC,QAAQ,CAAC,aAAa,CAC7B,EAAE,CAAC;QACF,KAAK,MAAM,KAAK,IAAI;YAClB,iBAAiB;YACjB,4BAA4B;YAC5B,+BAA+B;SACvB,EAAE,CAAC;YACX,MAAM,KAAK,CAAC,EAAE;iBACX,MAAM,CAAC,KAAK,CAAC;iBACb,KAAK,CACJ,EAAE,CACA,GAAG,cAAc,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,EAAE,CAClC,GAAG,CACD,EAAE,CAAC,KAAK,CAAC,aAAa,EAAE,SAAS,CAAC,aAAa,CAAC,EAChD,EAAE,CAAC,KAAK,CAAC,WAAW,EAAE,SAAS,CAAC,WAAW,CAAC,CAC7C,CACF,CACF,CACF;iBACA,GAAG,EAAE,CAAC;QACX,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["import { and, eq, or } from \"drizzle-orm\";\nimport {\n principalPolicies,\n principalPolicyBundleHistory,\n principalPolicyBundleReferences,\n principalPolicyCheckpoints,\n principalPolicyOrganizations,\n} from \"../../sqlite/schema\";\nimport {\n principalHistoryEntries,\n principalHistoryNodes,\n principalHistoryPrefixes,\n} from \"../../sqlite/schema/principalHistoryEvidenceSchema\";\nimport {\n principalHistoryNodeReferences,\n principalHistoryRootOwners,\n} from \"../../sqlite/schema/principalHistoryNodeRetentionSchema\";\nimport { principalKeyEnvelopeArchive } from \"../../sqlite/schema/principalHistoryRetentionSchema\";\nimport { principalHistoryStages } from \"../../sqlite/schema/principalHistoryStageSchema\";\nimport { remoteResetBatches } from \"../../sqlite/sqliteBindBudget\";\nimport type { ClientSQLiteTransactionScope } from \"../../sqlite/sqlitePersistenceRuntime\";\n\nexport interface RemoteResetPrincipalKey {\n readonly principalId: string;\n readonly principalType: \"group\" | \"organization\";\n}\n\ninterface PrincipalRow {\n readonly id: string;\n readonly type: string;\n}\n\n/** Current cache writes atomically pin ownership; reset never backfills it. */\nexport async function loadRemoteResetPrincipalScope(\n tx: ClientSQLiteTransactionScope,\n organizationId: string,\n): Promise<{\n readonly policyRows: readonly PrincipalRow[];\n readonly principalKeys: readonly RemoteResetPrincipalKey[];\n}> {\n const [policyRows, retainedRows, ownershipRows] = await Promise.all([\n tx\n .select({\n id: principalPolicies.principalId,\n type: principalPolicies.principalType,\n })\n .from(principalPolicies),\n Promise.all(\n [\n principalPolicyBundleHistory,\n principalPolicyBundleReferences,\n principalPolicyCheckpoints,\n ].map((table) =>\n tx\n .selectDistinct({ id: table.principalId, type: table.principalType })\n .from(table),\n ),\n ),\n tx\n .select({\n organizationId: principalPolicyOrganizations.organizationId,\n principalId: principalPolicyOrganizations.principalId,\n principalType: principalPolicyOrganizations.principalType,\n })\n .from(principalPolicyOrganizations),\n ]);\n const owners = new Map(\n ownershipRows.map((row) => [\n `${row.principalType}\\0${row.principalId}`,\n row.organizationId,\n ]),\n );\n for (const row of [...policyRows, ...retainedRows.flat()]) {\n const owner = owners.get(`${row.type}\\0${row.id}`);\n if (row.type === \"group\" && !owner) {\n throw new Error(\n \"Unowned group policy cache requires a local database reset before organization purge\",\n );\n }\n if (row.type !== \"group\" && row.type !== \"organization\") {\n throw new Error(\"Principal policy cache has an invalid principal type\");\n }\n if (\n row.type === \"organization\" &&\n owner !== undefined &&\n owner !== row.id\n ) {\n throw new Error(\n \"Organization policy cache ownership does not match its identity\",\n );\n }\n }\n const keys = new Map<string, RemoteResetPrincipalKey>();\n keys.set(`organization\\0${organizationId}`, {\n principalId: organizationId,\n principalType: \"organization\",\n });\n for (const row of ownershipRows) {\n if (row.principalType !== \"group\" && row.principalType !== \"organization\") {\n throw new Error(\n \"Principal policy cache ownership has an invalid principal type\",\n );\n }\n if (!row.organizationId) {\n throw new Error(\n \"Principal policy cache ownership requires a local database reset\",\n );\n }\n if (row.organizationId !== organizationId) continue;\n keys.set(`${row.principalType}\\0${row.principalId}`, {\n principalId: row.principalId,\n principalType: row.principalType,\n });\n }\n return { policyRows, principalKeys: [...keys.values()] };\n}\n\n// Trust checkpoints and their ownership survive reset. Clearing cached bundles\n// must not turn a previously observed principal or object into first sight.\nexport async function clearRemoteResetPrincipalRows(input: {\n organizationId: string;\n snapshot: { principalKeys: readonly RemoteResetPrincipalKey[] };\n tx: ClientSQLiteTransactionScope;\n}): Promise<void> {\n for (const table of [\n principalHistoryNodeReferences,\n principalHistoryRootOwners,\n principalHistoryEntries,\n principalHistoryNodes,\n principalHistoryPrefixes,\n principalKeyEnvelopeArchive,\n ])\n await input.tx\n .delete(table)\n .where(eq(table.organizationId, input.organizationId))\n .run();\n await input.tx\n .delete(principalHistoryStages)\n .where(eq(principalHistoryStages.organizationId, input.organizationId))\n .run();\n for (const principalBatch of remoteResetBatches(\n input.snapshot.principalKeys,\n )) {\n for (const table of [\n principalPolicies,\n principalPolicyBundleHistory,\n principalPolicyBundleReferences,\n ] as const) {\n await input.tx\n .delete(table)\n .where(\n or(\n ...principalBatch.map((principal) =>\n and(\n eq(table.principalType, principal.principalType),\n eq(table.principalId, principal.principalId),\n ),\n ),\n ),\n )\n .run();\n }\n }\n}\n"]}
@@ -43,3 +43,5 @@ export declare function writeVerifiedManifestBundlesInTransaction(tx: ClientSQLi
43
43
  export declare function loadVerifiedManifestCitations(execSql: ExecSql, target: WriterProjectionTarget, limit?: number): Promise<AccessManifestBundleWireResponse[]>;
44
44
  /** Cites from whichever database the signed-in identity currently uses. */
45
45
  export declare function createVerifiedManifestCitations(resolveExecSql: () => ExecSql | null): VerifiedManifestCitations;
46
+ /** Drops an organization's verified bundles when a remote reset forgets it. */
47
+ export declare function deleteVerifiedManifestBundlesForOrganization(tx: ClientSQLiteTransactionScope, organizationId: string): Promise<void>;
@@ -279,4 +279,11 @@ export function createVerifiedManifestCitations(resolveExecSql) {
279
279
  },
280
280
  };
281
281
  }
282
+ /** Drops an organization's verified bundles when a remote reset forgets it. */
283
+ export async function deleteVerifiedManifestBundlesForOrganization(tx, organizationId) {
284
+ await tx
285
+ .delete(bundles)
286
+ .where(eq(bundles.organizationId, organizationId))
287
+ .run();
288
+ }
282
289
  //# sourceMappingURL=verifiedManifestBundlePersistence.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"verifiedManifestBundlePersistence.js","sourceRoot":"","sources":["verifiedManifestBundlePersistence.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAEL,kCAAkC,GACnC,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAC1B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EACL,eAAe,EACf,2BAA2B,EAC3B,iCAAiC,GAClC,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AAC9D,OAAO,EACL,sBAAsB,EACtB,0BAA0B,GAC3B,MAA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AK;QACpB,MAAM,OAAO,CAAC,MAAM,UAAU,CAAC,EAAE,EAAE,cAAc,EAAE,KAAK,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IAC1E,IAAI,OAAO,CAAC,MAAM,GAAG,CAAC;QACpB,MAAM,iCAAiC,CAAC,OAAO,CAAC;aAC7C,WAAW,CAAC,CAAC,MAAM,EAAE,EAAE,CACtB,MAAM;aACH,MAAM,CAAC,OAAO,CAAC;aACf,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;aAC7C,GAAG,EAAE,CACT;aACA,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC;IAC5B,OAAO,CAAC,GAAG,KAAK,CAAC,MAAM,EAAE,CAAC,CAAC;AAC7B,CAAC;AAED,2EAA2E;AAC3E,MAAM,UAAU,+BAA+B,CAC7C,cAAoC;IAEpC,OAAO;QACL,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;YACrB,MAAM,OAAO,GAAG,cAAc,EAAE,CAAC;YACjC,OAAO,OAAO,CAAC,CAAC,CAAC,6BAA6B,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACvE,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import type {\n VerifiedManifestCitations,\n WriterProjectionTarget,\n} from \"@tearleads/api-client\";\nimport { toFingerprint } from \"@tearleads/crypto\";\nimport {\n type AccessManifestBundleWireResponse,\n isAccessManifestBundleWireResponse,\n} from \"@tearleads/validators/response\";\nimport {\n KNOWN_MANIFEST_HINT_LIMIT,\n MAX_CONTAINER_PATH_LENGTH,\n} from \"@tearleads/validators/util\";\nimport { and, asc, desc, eq, inArray, sql } from \"drizzle-orm\";\nimport {\n containerTables,\n documentContainerProjection,\n documentContainerProjectionTables,\n} from \"../sqlite/schema\";\nimport { containers } from \"../sqlite/schema/containerSchema\";\nimport {\n documentDiscoveryHeads,\n pendingDocumentDiscoveries,\n} from \"../sqlite/schema/documentDiscoveryEvidenceSchema\";\nimport {\n verifiedAccessManifestBundles as bundles,\n verifiedManifestBundleTables,\n} from \"../sqlite/schema/verifiedManifestBundleSchema\";\nimport {\n type ClientSQLiteDatabase,\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\n\n/**\n * Bounds local bytes (a bundle is ~10–20 KB). Evicting the least recently\n * verified rows only makes a later read carry those bundles again.\n */\nexport const MAX_VERIFIED_MANIFEST_BUNDLES = 1024;\nconst STATEMENT_BATCH = 200;\n\nexport interface VerifiedManifestBundle {\n readonly bundle: AccessManifestBundleWireResponse;\n readonly checkpoint: {\n readonly objectKind: string;\n readonly organizationId: string;\n readonly objectId: string;\n readonly epoch: number;\n };\n}\n\nfunction sha256Hex(text: string): Promise<string> {\n return toFingerprint(new TextEncoder().encode(text));\n}\n\nfunction batches<T>(values: readonly T[]): T[][] {\n const result: T[][] = [];\n for (let index = 0; index < values.length; index += STATEMENT_BATCH)\n result.push(values.slice(index, index + STATEMENT_BATCH));\n return result;\n}\n\nasync function pruneInTransaction(\n tx: ClientSQLiteTransactionScope,\n): Promise<void> {\n const [row] = await tx.select({ count: sql<number>`count(*)` }).from(bundles);\n const excess = (row?.count ?? 0) - MAX_VERIFIED_MANIFEST_BUNDLES;\n if (excess <= 0) return;\n const evicted = await tx\n .select({ manifestHash: bundles.manifestHash })\n .from(bundles)\n .orderBy(asc(bundles.verifiedAt), asc(bundles.manifestHash))\n .limit(excess);\n for (const batch of batches(evicted.map((entry) => entry.manifestHash)))\n await tx.delete(bundles).where(inArray(bundles.manifestHash, batch)).run();\n}\n\n/** A verified bundle's exact JSON and that JSON's digest. */\nexport type VerifiedManifestBundleRow = Omit<\n typeof bundles.$inferInsert,\n \"verifiedAt\"\n>;\n\n/**\n * Prepares rows for bundles that just passed projection verification. The\n * digests are computed here, before the checkpoint transaction that writes\n * them begins.\n */\nexport function verifiedManifestBundleRows(\n entries: readonly VerifiedManifestBundle[],\n): Promise<VerifiedManifestBundleRow[]> {\n const unique = new Map(\n entries.map((entry) => [entry.bundle.manifestHash, entry]),\n );\n return Promise.all(\n [...unique.values()].map(async ({ bundle, checkpoint }) => {\n const bundleJson = JSON.stringify(bundle);\n return {\n manifestHash: bundle.manifestHash,\n organizationId: checkpoint.organizationId,\n objectKind: checkpoint.objectKind,\n objectId: checkpoint.objectId,\n epoch: checkpoint.epoch,\n bundleJson,\n bundleDigest: await sha256Hex(bundleJson),\n };\n }),\n );\n}\n\n/**\n * Records verified bundles inside the transaction that finalizes their\n * projection's checkpoints, so they commit or roll back with it and no cache\n * work outlives the verification. Freshly verified bytes replace any stored\n * copy.\n */\nexport async function writeVerifiedManifestBundlesInTransaction(\n tx: ClientSQLiteTransactionScope,\n rows: readonly VerifiedManifestBundleRow[],\n verifiedAt: string,\n): Promise<void> {\n if (rows.length === 0) return;\n const stored = await storedDigests(\n tx,\n rows.map((row) => row.manifestHash),\n );\n // An identical stored row only needs its recency refreshed.\n const unchanged = rows.filter(\n (row) => stored.get(row.manifestHash) === row.bundleDigest,\n );\n for (const batch of batches(unchanged.map((row) => row.manifestHash)))\n await tx\n .update(bundles)\n .set({ verifiedAt })\n .where(inArray(bundles.manifestHash, batch))\n .run();\n for (const row of rows) {\n if (stored.get(row.manifestHash) === row.bundleDigest) continue;\n const verified = { ...row, verifiedAt };\n await tx\n .insert(bundles)\n .values(verified)\n .onConflictDoUpdate({ target: bundles.manifestHash, set: verified })\n .run();\n }\n await pruneInTransaction(tx);\n}\n\nasync function storedDigests(\n tx: ClientSQLiteTransactionScope,\n manifestHashes: readonly string[],\n): Promise<Map<string, string>> {\n const digests = new Map<string, string>();\n for (const batch of batches(manifestHashes)) {\n const rows = await tx\n .select({\n bundleDigest: bundles.bundleDigest,\n manifestHash: bundles.manifestHash,\n })\n .from(bundles)\n .where(inArray(bundles.manifestHash, batch));\n for (const row of rows) digests.set(row.manifestHash, row.bundleDigest);\n }\n return digests;\n}\n\n/** The requested object first, then each container whose lineage it cites. */\nasync function containerLineage(\n db: ClientSQLiteDatabase,\n containerId: string,\n): Promise<string[]> {\n const lineage = [containerId];\n while (lineage.length < MAX_CONTAINER_PATH_LENGTH) {\n const [row] = await db\n .select({ parentId: containers.parentId })\n .from(containers)\n .where(eq(containers.id, lineage.at(-1) ?? \"\"))\n .limit(1);\n const parentId = row?.parentId;\n if (!parentId || lineage.includes(parentId)) break;\n lineage.push(parentId);\n }\n return lineage;\n}\n\nasync function citedObjects(\n db: ClientSQLiteDatabase,\n target: WriterProjectionTarget,\n): Promise<{ readonly kind: string; readonly id: string }[]> {\n if (target.kind === \"container\")\n return (await containerLineage(db, target.id)).map((id) => ({\n kind: \"container\",\n id,\n }));\n // Linked containers, containers listing the document before its signed\n // placement is checked, and a container whose metadata document it is.\n const placements = [\n ...(await db\n .select({ containerId: documentContainerProjection.containerId })\n .from(documentContainerProjection)\n .where(eq(documentContainerProjection.documentId, target.id))),\n ...(await db\n .select({ containerId: pendingDocumentDiscoveries.containerId })\n .from(pendingDocumentDiscoveries)\n .where(eq(pendingDocumentDiscoveries.documentId, target.id))),\n ...(await db\n .select({ containerId: containers.id })\n .from(containers)\n .where(eq(containers.metadataDocumentId, target.id))),\n ];\n const containerIds = new Set<string>();\n for (const { containerId } of placements)\n if (containerId)\n for (const id of await containerLineage(db, containerId))\n containerIds.add(id);\n return [\n { kind: \"document\", id: target.id },\n ...[...containerIds].map((id) => ({ kind: \"container\", id })),\n ];\n}\n\n/**\n * The one organization every local source places the read in. Verified\n * ownership (stored verified bundles of the target and of the containers on\n * its walk, and a document's verified discovery head) and local placement\n * (listed container rows reached through links, pending listings, metadata,\n * or parents) must all agree: any disagreement, or no source at all, yields\n * null, so the read is sent with no hints rather than another\n * organization's.\n */\nasync function targetOrganization(\n db: ClientSQLiteDatabase,\n target: WriterProjectionTarget,\n objects: readonly { readonly kind: string; readonly id: string }[],\n): Promise<string | null> {\n const containerIds = objects\n .filter((object) => object.kind === \"container\")\n .map((object) => object.id);\n const listed =\n containerIds.length === 0\n ? []\n : await db\n .select({ organizationId: containers.organizationId })\n .from(containers)\n .where(inArray(containers.id, containerIds));\n const verifiedContainers =\n containerIds.length === 0\n ? []\n : await db\n .select({ organizationId: bundles.organizationId })\n .from(bundles)\n .where(\n and(\n eq(bundles.objectKind, \"container\"),\n inArray(bundles.objectId, containerIds),\n ),\n );\n const verifiedTarget = await db\n .select({ organizationId: bundles.organizationId })\n .from(bundles)\n .where(\n and(eq(bundles.objectKind, target.kind), eq(bundles.objectId, target.id)),\n );\n const discovered =\n target.kind === \"document\"\n ? await db\n .select({ organizationId: documentDiscoveryHeads.organizationId })\n .from(documentDiscoveryHeads)\n .where(eq(documentDiscoveryHeads.documentId, target.id))\n : [];\n const sources = [listed, verifiedContainers, verifiedTarget, discovered];\n const organizations = new Set(\n sources.flat().map((row) => row.organizationId),\n );\n const [organizationId] = organizations;\n return organizations.size === 1 && organizationId ? organizationId : null;\n}\n\ninterface StoredBundleRow {\n readonly bundleDigest: string;\n readonly bundleJson: string;\n readonly manifestHash: string;\n}\n\nconst storedColumns = {\n bundleDigest: bundles.bundleDigest,\n bundleJson: bundles.bundleJson,\n manifestHash: bundles.manifestHash,\n};\n\nasync function readStoredBundle(\n row: StoredBundleRow,\n): Promise<AccessManifestBundleWireResponse | null> {\n if ((await sha256Hex(row.bundleJson)) !== row.bundleDigest) return null;\n try {\n const bundle: unknown = JSON.parse(row.bundleJson);\n return isAccessManifestBundleWireResponse(bundle) &&\n bundle.manifestHash === row.manifestHash\n ? bundle\n : null;\n } catch {\n return null;\n }\n}\n\nfunction objectRows(\n db: ClientSQLiteDatabase,\n organizationId: string,\n object: { readonly kind: string; readonly id: string },\n limit: number,\n) {\n return db\n .select(storedColumns)\n .from(bundles)\n .where(\n and(\n eq(bundles.organizationId, organizationId),\n eq(bundles.objectKind, object.kind),\n eq(bundles.objectId, object.id),\n ),\n )\n .orderBy(desc(bundles.epoch))\n .limit(limit);\n}\n\nfunction recentRows(\n db: ClientSQLiteDatabase,\n organizationId: string,\n limit: number,\n) {\n return db\n .select(storedColumns)\n .from(bundles)\n .where(eq(bundles.organizationId, organizationId))\n .orderBy(desc(bundles.verifiedAt), asc(bundles.manifestHash))\n .limit(limit);\n}\n\n/**\n * Verified bundles of the target's object and of the containers its\n * projection walks, newest epochs first; then the most recently verified rows\n * of its organization, since a just-verified parent is the likeliest part of\n * a new child's projection. Citations never leave the target's organization:\n * an object whose organization the device cannot place cites nothing.\n * Damaged rows are dropped.\n */\nexport async function loadVerifiedManifestCitations(\n execSql: ExecSql,\n target: WriterProjectionTarget,\n limit = KNOWN_MANIFEST_HINT_LIMIT,\n): Promise<AccessManifestBundleWireResponse[]> {\n await ensureSqlTables(execSql, [\n ...verifiedManifestBundleTables,\n ...containerTables,\n ...documentContainerProjectionTables,\n ]);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const cited = new Map<string, AccessManifestBundleWireResponse>();\n const damaged: string[] = [];\n const collect = async (rows: readonly StoredBundleRow[]) => {\n for (const row of rows) {\n if (cited.size >= limit || cited.has(row.manifestHash)) continue;\n const bundle = await readStoredBundle(row);\n if (bundle) cited.set(row.manifestHash, bundle);\n else damaged.push(row.manifestHash);\n }\n };\n const objects = await citedObjects(db, target);\n const organizationId = await targetOrganization(db, target, objects);\n if (organizationId === null) return [];\n for (const object of objects)\n if (cited.size < limit)\n await collect(\n await objectRows(db, organizationId, object, limit - cited.size),\n );\n if (cited.size < limit)\n await collect(await recentRows(db, organizationId, limit + cited.size));\n if (damaged.length > 0)\n await getClientSQLitePersistenceRuntime(execSql)\n .runMutation((handle) =>\n handle\n .delete(bundles)\n .where(inArray(bundles.manifestHash, damaged))\n .run(),\n )\n .catch(() => undefined);\n return [...cited.values()];\n}\n\n/** Cites from whichever database the signed-in identity currently uses. */\nexport function createVerifiedManifestCitations(\n resolveExecSql: () => ExecSql | null,\n): VerifiedManifestCitations {\n return {\n cite: async (target) => {\n const execSql = resolveExecSql();\n return execSql ? loadVerifiedManifestCitations(execSql, target) : [];\n },\n };\n}\n"]}
1
+ {"version":3,"file":"verifiedManifestBundlePersistence.js","sourceRoot":"","sources":["verifiedManifestBundlePersistence.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,aAAa,EAAE,MAAM,mBAAmB,CAAC;AAClD,OAAO,EAEL,kCAAkC,GACnC,MAAM,gCAAgC,CAAC;AACxC,OAAO,EACL,yBAAyB,EACzB,yBAAyB,GAC1B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,EAAE,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAC/D,OAAO,EACL,eAAe,EACf,2BAA2B,EAC3B,iCAAiC,GAClC,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EAAE,UAAU,EAAE,MAAM,kCAAkC,CAAC;AAC9D,OAAO,EACL,sBAAsB,EACtB,0BAA0B,GAC3B,MAAM,kDAAkD,CAAC;AAC1D,OAAO,EACL,6BAA6B,IAAI,OAAO,EACxC,4BAA4B,GAC7B,MAAM,+CAA+C,CAAC;AACvD,OAAO,EAGL,iCAAiC,GAClC,MAAM,oCAAoC,CAAC;AAC5C,OAAO,EAAgB,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAEpE;;;GAGG;AACH,MAAM,CAAC,MAAM,6BAA6B,GAAG,IAAI,CAAC;AAClD,MAAM,eAAe,GAAG,GAAG,CAAC;AAY5B,SAAS,SAAS,CAAC,IAAY;IAC7B,OAAO,aAAa,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC;AACvD,CAAC;AAED,SAAS,OAAO,CAAI,MAAoB;IACtC,MAAM,MAAM,GAAU,EAAE,CAAC;IACzB,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE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ACL,IAAI,EAAE,KAAK,EAAE,MAAM,EAAE,EAAE;YACrB,MAAM,OAAO,GAAG,cAAc,EAAE,CAAC;YACjC,OAAO,OAAO,CAAC,CAAC,CAAC,6BAA6B,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;QACvE,CAAC;KACF,CAAC;AACJ,CAAC;AAED,+EAA+E;AAC/E,MAAM,CAAC,KAAK,UAAU,4CAA4C,CAChE,EAAgC,EAChC,cAAsB;IAEtB,MAAM,EAAE;SACL,MAAM,CAAC,OAAO,CAAC;SACf,KAAK,CAAC,EAAE,CAAC,OAAO,CAAC,cAAc,EAAE,cAAc,CAAC,CAAC;SACjD,GAAG,EAAE,CAAC;AACX,CAAC","sourcesContent":["import type {\n VerifiedManifestCitations,\n WriterProjectionTarget,\n} from \"@tearleads/api-client\";\nimport { toFingerprint } from \"@tearleads/crypto\";\nimport {\n type AccessManifestBundleWireResponse,\n isAccessManifestBundleWireResponse,\n} from \"@tearleads/validators/response\";\nimport {\n KNOWN_MANIFEST_HINT_LIMIT,\n MAX_CONTAINER_PATH_LENGTH,\n} from \"@tearleads/validators/util\";\nimport { and, asc, desc, eq, inArray, sql } from \"drizzle-orm\";\nimport {\n containerTables,\n documentContainerProjection,\n documentContainerProjectionTables,\n} from \"../sqlite/schema\";\nimport { containers } from \"../sqlite/schema/containerSchema\";\nimport {\n documentDiscoveryHeads,\n pendingDocumentDiscoveries,\n} from \"../sqlite/schema/documentDiscoveryEvidenceSchema\";\nimport {\n verifiedAccessManifestBundles as bundles,\n verifiedManifestBundleTables,\n} from \"../sqlite/schema/verifiedManifestBundleSchema\";\nimport {\n type ClientSQLiteDatabase,\n type ClientSQLiteTransactionScope,\n getClientSQLitePersistenceRuntime,\n} from \"../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\n\n/**\n * Bounds local bytes (a bundle is ~10–20 KB). Evicting the least recently\n * verified rows only makes a later read carry those bundles again.\n */\nexport const MAX_VERIFIED_MANIFEST_BUNDLES = 1024;\nconst STATEMENT_BATCH = 200;\n\nexport interface VerifiedManifestBundle {\n readonly bundle: AccessManifestBundleWireResponse;\n readonly checkpoint: {\n readonly objectKind: string;\n readonly organizationId: string;\n readonly objectId: string;\n readonly epoch: number;\n };\n}\n\nfunction sha256Hex(text: string): Promise<string> {\n return toFingerprint(new TextEncoder().encode(text));\n}\n\nfunction batches<T>(values: readonly T[]): T[][] {\n const result: T[][] = [];\n for (let index = 0; index < values.length; index += STATEMENT_BATCH)\n result.push(values.slice(index, index + STATEMENT_BATCH));\n return result;\n}\n\nasync function pruneInTransaction(\n tx: ClientSQLiteTransactionScope,\n): Promise<void> {\n const [row] = await tx.select({ count: sql<number>`count(*)` }).from(bundles);\n const excess = (row?.count ?? 0) - MAX_VERIFIED_MANIFEST_BUNDLES;\n if (excess <= 0) return;\n const evicted = await tx\n .select({ manifestHash: bundles.manifestHash })\n .from(bundles)\n .orderBy(asc(bundles.verifiedAt), asc(bundles.manifestHash))\n .limit(excess);\n for (const batch of batches(evicted.map((entry) => entry.manifestHash)))\n await tx.delete(bundles).where(inArray(bundles.manifestHash, batch)).run();\n}\n\n/** A verified bundle's exact JSON and that JSON's digest. */\nexport type VerifiedManifestBundleRow = Omit<\n typeof bundles.$inferInsert,\n \"verifiedAt\"\n>;\n\n/**\n * Prepares rows for bundles that just passed projection verification. The\n * digests are computed here, before the checkpoint transaction that writes\n * them begins.\n */\nexport function verifiedManifestBundleRows(\n entries: readonly VerifiedManifestBundle[],\n): Promise<VerifiedManifestBundleRow[]> {\n const unique = new Map(\n entries.map((entry) => [entry.bundle.manifestHash, entry]),\n );\n return Promise.all(\n [...unique.values()].map(async ({ bundle, checkpoint }) => {\n const bundleJson = JSON.stringify(bundle);\n return {\n manifestHash: bundle.manifestHash,\n organizationId: checkpoint.organizationId,\n objectKind: checkpoint.objectKind,\n objectId: checkpoint.objectId,\n epoch: checkpoint.epoch,\n bundleJson,\n bundleDigest: await sha256Hex(bundleJson),\n };\n }),\n );\n}\n\n/**\n * Records verified bundles inside the transaction that finalizes their\n * projection's checkpoints, so they commit or roll back with it and no cache\n * work outlives the verification. Freshly verified bytes replace any stored\n * copy.\n */\nexport async function writeVerifiedManifestBundlesInTransaction(\n tx: ClientSQLiteTransactionScope,\n rows: readonly VerifiedManifestBundleRow[],\n verifiedAt: string,\n): Promise<void> {\n if (rows.length === 0) return;\n const stored = await storedDigests(\n tx,\n rows.map((row) => row.manifestHash),\n );\n // An identical stored row only needs its recency refreshed.\n const unchanged = rows.filter(\n (row) => stored.get(row.manifestHash) === row.bundleDigest,\n );\n for (const batch of batches(unchanged.map((row) => row.manifestHash)))\n await tx\n .update(bundles)\n .set({ verifiedAt })\n .where(inArray(bundles.manifestHash, batch))\n .run();\n for (const row of rows) {\n if (stored.get(row.manifestHash) === row.bundleDigest) continue;\n const verified = { ...row, verifiedAt };\n await tx\n .insert(bundles)\n .values(verified)\n .onConflictDoUpdate({ target: bundles.manifestHash, set: verified })\n .run();\n }\n await pruneInTransaction(tx);\n}\n\nasync function storedDigests(\n tx: ClientSQLiteTransactionScope,\n manifestHashes: readonly string[],\n): Promise<Map<string, string>> {\n const digests = new Map<string, string>();\n for (const batch of batches(manifestHashes)) {\n const rows = await tx\n .select({\n bundleDigest: bundles.bundleDigest,\n manifestHash: bundles.manifestHash,\n })\n .from(bundles)\n .where(inArray(bundles.manifestHash, batch));\n for (const row of rows) digests.set(row.manifestHash, row.bundleDigest);\n }\n return digests;\n}\n\n/** The requested object first, then each container whose lineage it cites. */\nasync function containerLineage(\n db: ClientSQLiteDatabase,\n containerId: string,\n): Promise<string[]> {\n const lineage = [containerId];\n while (lineage.length < MAX_CONTAINER_PATH_LENGTH) {\n const [row] = await db\n .select({ parentId: containers.parentId })\n .from(containers)\n .where(eq(containers.id, lineage.at(-1) ?? \"\"))\n .limit(1);\n const parentId = row?.parentId;\n if (!parentId || lineage.includes(parentId)) break;\n lineage.push(parentId);\n }\n return lineage;\n}\n\nasync function citedObjects(\n db: ClientSQLiteDatabase,\n target: WriterProjectionTarget,\n): Promise<{ readonly kind: string; readonly id: string }[]> {\n if (target.kind === \"container\")\n return (await containerLineage(db, target.id)).map((id) => ({\n kind: \"container\",\n id,\n }));\n // Linked containers, containers listing the document before its signed\n // placement is checked, and a container whose metadata document it is.\n const placements = [\n ...(await db\n .select({ containerId: documentContainerProjection.containerId })\n .from(documentContainerProjection)\n .where(eq(documentContainerProjection.documentId, target.id))),\n ...(await db\n .select({ containerId: pendingDocumentDiscoveries.containerId })\n .from(pendingDocumentDiscoveries)\n .where(eq(pendingDocumentDiscoveries.documentId, target.id))),\n ...(await db\n .select({ containerId: containers.id })\n .from(containers)\n .where(eq(containers.metadataDocumentId, target.id))),\n ];\n const containerIds = new Set<string>();\n for (const { containerId } of placements)\n if (containerId)\n for (const id of await containerLineage(db, containerId))\n containerIds.add(id);\n return [\n { kind: \"document\", id: target.id },\n ...[...containerIds].map((id) => ({ kind: \"container\", id })),\n ];\n}\n\n/**\n * The one organization every local source places the read in. Verified\n * ownership (stored verified bundles of the target and of the containers on\n * its walk, and a document's verified discovery head) and local placement\n * (listed container rows reached through links, pending listings, metadata,\n * or parents) must all agree: any disagreement, or no source at all, yields\n * null, so the read is sent with no hints rather than another\n * organization's.\n */\nasync function targetOrganization(\n db: ClientSQLiteDatabase,\n target: WriterProjectionTarget,\n objects: readonly { readonly kind: string; readonly id: string }[],\n): Promise<string | null> {\n const containerIds = objects\n .filter((object) => object.kind === \"container\")\n .map((object) => object.id);\n const listed =\n containerIds.length === 0\n ? []\n : await db\n .select({ organizationId: containers.organizationId })\n .from(containers)\n .where(inArray(containers.id, containerIds));\n const verifiedContainers =\n containerIds.length === 0\n ? []\n : await db\n .select({ organizationId: bundles.organizationId })\n .from(bundles)\n .where(\n and(\n eq(bundles.objectKind, \"container\"),\n inArray(bundles.objectId, containerIds),\n ),\n );\n const verifiedTarget = await db\n .select({ organizationId: bundles.organizationId })\n .from(bundles)\n .where(\n and(eq(bundles.objectKind, target.kind), eq(bundles.objectId, target.id)),\n );\n const discovered =\n target.kind === \"document\"\n ? await db\n .select({ organizationId: documentDiscoveryHeads.organizationId })\n .from(documentDiscoveryHeads)\n .where(eq(documentDiscoveryHeads.documentId, target.id))\n : [];\n const sources = [listed, verifiedContainers, verifiedTarget, discovered];\n const organizations = new Set(\n sources.flat().map((row) => row.organizationId),\n );\n const [organizationId] = organizations;\n return organizations.size === 1 && organizationId ? organizationId : null;\n}\n\ninterface StoredBundleRow {\n readonly bundleDigest: string;\n readonly bundleJson: string;\n readonly manifestHash: string;\n}\n\nconst storedColumns = {\n bundleDigest: bundles.bundleDigest,\n bundleJson: bundles.bundleJson,\n manifestHash: bundles.manifestHash,\n};\n\nasync function readStoredBundle(\n row: StoredBundleRow,\n): Promise<AccessManifestBundleWireResponse | null> {\n if ((await sha256Hex(row.bundleJson)) !== row.bundleDigest) return null;\n try {\n const bundle: unknown = JSON.parse(row.bundleJson);\n return isAccessManifestBundleWireResponse(bundle) &&\n bundle.manifestHash === row.manifestHash\n ? bundle\n : null;\n } catch {\n return null;\n }\n}\n\nfunction objectRows(\n db: ClientSQLiteDatabase,\n organizationId: string,\n object: { readonly kind: string; readonly id: string },\n limit: number,\n) {\n return db\n .select(storedColumns)\n .from(bundles)\n .where(\n and(\n eq(bundles.organizationId, organizationId),\n eq(bundles.objectKind, object.kind),\n eq(bundles.objectId, object.id),\n ),\n )\n .orderBy(desc(bundles.epoch))\n .limit(limit);\n}\n\nfunction recentRows(\n db: ClientSQLiteDatabase,\n organizationId: string,\n limit: number,\n) {\n return db\n .select(storedColumns)\n .from(bundles)\n .where(eq(bundles.organizationId, organizationId))\n .orderBy(desc(bundles.verifiedAt), asc(bundles.manifestHash))\n .limit(limit);\n}\n\n/**\n * Verified bundles of the target's object and of the containers its\n * projection walks, newest epochs first; then the most recently verified rows\n * of its organization, since a just-verified parent is the likeliest part of\n * a new child's projection. Citations never leave the target's organization:\n * an object whose organization the device cannot place cites nothing.\n * Damaged rows are dropped.\n */\nexport async function loadVerifiedManifestCitations(\n execSql: ExecSql,\n target: WriterProjectionTarget,\n limit = KNOWN_MANIFEST_HINT_LIMIT,\n): Promise<AccessManifestBundleWireResponse[]> {\n await ensureSqlTables(execSql, [\n ...verifiedManifestBundleTables,\n ...containerTables,\n ...documentContainerProjectionTables,\n ]);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const cited = new Map<string, AccessManifestBundleWireResponse>();\n const damaged: string[] = [];\n const collect = async (rows: readonly StoredBundleRow[]) => {\n for (const row of rows) {\n if (cited.size >= limit || cited.has(row.manifestHash)) continue;\n const bundle = await readStoredBundle(row);\n if (bundle) cited.set(row.manifestHash, bundle);\n else damaged.push(row.manifestHash);\n }\n };\n const objects = await citedObjects(db, target);\n const organizationId = await targetOrganization(db, target, objects);\n if (organizationId === null) return [];\n for (const object of objects)\n if (cited.size < limit)\n await collect(\n await objectRows(db, organizationId, object, limit - cited.size),\n );\n if (cited.size < limit)\n await collect(await recentRows(db, organizationId, limit + cited.size));\n if (damaged.length > 0)\n await getClientSQLitePersistenceRuntime(execSql)\n .runMutation((handle) =>\n handle\n .delete(bundles)\n .where(inArray(bundles.manifestHash, damaged))\n .run(),\n )\n .catch(() => undefined);\n return [...cited.values()];\n}\n\n/** Cites from whichever database the signed-in identity currently uses. */\nexport function createVerifiedManifestCitations(\n resolveExecSql: () => ExecSql | null,\n): VerifiedManifestCitations {\n return {\n cite: async (target) => {\n const execSql = resolveExecSql();\n return execSql ? loadVerifiedManifestCitations(execSql, target) : [];\n },\n };\n}\n\n/** Drops an organization's verified bundles when a remote reset forgets it. */\nexport async function deleteVerifiedManifestBundlesForOrganization(\n tx: ClientSQLiteTransactionScope,\n organizationId: string,\n): Promise<void> {\n await tx\n .delete(bundles)\n .where(eq(bundles.organizationId, organizationId))\n .run();\n}\n"]}
@@ -3,4 +3,4 @@ import { defineSqlTableSchema } from "../sqlTableSchema.js";
3
3
  /** Durable byte-reclamation queue populated before orphan attachment rows go. */
4
4
  export const documentOrphanBlobReclaims = sqliteTable("document_orphan_blob_reclaims", { storageKey: text("storage_key").primaryKey() });
5
5
  export const documentOrphanBlobReclaimTable = defineSqlTableSchema(documentOrphanBlobReclaims);
6
- //# sourceMappingURL=documentOrphanBlobReclaims.js.map
6
+ //# sourceMappingURL=documentOrphanBlobReclaimSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"documentOrphanBlobReclaimSchema.js","sourceRoot":"","sources":["documentOrphanBlobReclaimSchema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,IAAI,EAAE,MAAM,yBAAyB,CAAC;AAC5D,OAAO,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAEzD,iFAAiF;AACjF,MAAM,CAAC,MAAM,0BAA0B,GAAG,WAAW,CACnD,+BAA+B,EAC/B,EAAE,UAAU,EAAE,IAAI,CAAC,aAAa,CAAC,CAAC,UAAU,EAAE,EAAE,CACjD,CAAC;AAEF,MAAM,CAAC,MAAM,8BAA8B,GAAG,oBAAoB,CAChE,0BAA0B,CAC3B,CAAC","sourcesContent":["import { sqliteTable, text } from \"drizzle-orm/sqlite-core\";\nimport { defineSqlTableSchema } from \"../sqlTableSchema\";\n\n/** Durable byte-reclamation queue populated before orphan attachment rows go. */\nexport const documentOrphanBlobReclaims = sqliteTable(\n \"document_orphan_blob_reclaims\",\n { storageKey: text(\"storage_key\").primaryKey() },\n);\n\nexport const documentOrphanBlobReclaimTable = defineSqlTableSchema(\n documentOrphanBlobReclaims,\n);\n"]}
@@ -1,11 +1,11 @@
1
1
  import { sql } from "drizzle-orm";
2
2
  import { index, integer, primaryKey, sqliteTable, text, uniqueIndex, } from "drizzle-orm/sqlite-core";
3
- import { documentOrphanBlobReclaims, documentOrphanBlobReclaimTable, } from "../documentPersistence/documentOrphanBlobReclaims.js";
4
3
  import { defineSqlTableSchema } from "../sqlTableSchema.js";
5
4
  import { containerDocumentTombstoneHolds } from "./containerDocumentTombstoneHoldSchema.js";
6
5
  import { containerSQLiteSchema, containerTableSchemas, } from "./containerSchema.js";
7
6
  import { documentAttachmentBlobProjection, documentPendingAttachments, } from "./documentAttachmentSchema.js";
8
7
  import { documentDiscoveryHeads, documentDiscoverySequence, pendingDocumentDiscoveries, } from "./documentDiscoveryEvidenceSchema.js";
8
+ import { documentOrphanBlobReclaims, documentOrphanBlobReclaimTable, } from "./documentOrphanBlobReclaimSchema.js";
9
9
  import { documentIntentLinkTargets } from "./documentPlacementIntentSchema.js";
10
10
  import { localIntents } from "./localIntentSchema.js";
11
11
  import * as organizationDataUsage from "./organizationDataUsageSchema.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EACL,KAAK,EACL,OAAO,EACP,UAAU,EACV,WAAW,EACX,IAAI,EACJ,WAAW,GACZ,MAAM,yBAAyB,CAAC;AACjC,OAAO,EACL,0BAA0B,EAC1B,8BAA8B,GAC/B,MAAM,mDAAmD,CAAC;AAC3D,OAAO,EAAE,oBAAoB,EAAuB,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,+BAA+B,EAAE,MAAM,wCAAwC,CAAC;AACzF,OAAO,EACL,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,gCAAgC,EAChC,0BAA0B,GAC3B,MAAM,4BAA4B,CAAC;AACpC,OAAO,EACL,sBAAsB,EACtB,yBAAyB,EACzB,0BAA0B,GAC3B,MAAM,mCAAmC,CAAC;AAC3C,OAAO,EAAE,yBAAyB,EAAE,MAAM,iCAAiC,CAAC;AAC5E,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,OAAO,KAAK,qBAAqB,MAAM,+BAA+B,CAAC;AACvE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAA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MAAM,4BAA4B,GAAkC;IACzE;QACE,GAAG,oBAAoB,CAAC,uBAAuB,CAAC;QAChD,eAAe,EAAE,CAAC,YAAY,CAAC;KAChC;CACF,CAAC;AAEF,MAAM,CAAC,MAAM,eAAe,GAAkC;IAC5D,GAAG,8BAA8B;IACjC,GAAG,cAAc;IACjB,GAAG,qBAAqB;IACxB,GAAG,sBAAsB;IACzB,GAAG,4BAA4B;IAC/B,GAAG,4BAA4B;IAC/B,GAAG,sBAAsB;IACzB,GAAG,eAAe;IAClB,GAAG,iCAAiC;IACpC,GAAG,6BAA6B;IAChC,GAAG,wBAAwB;IAC3B,GAAG,4BAA4B;IAC/B,GAAG,2BAA2B;IAC9B,GAAG,qBAAqB,CAAC,2BAA2B;IACpD,GAAG,mBAAmB,CAAC,qCAAqC;CAC7D,CAAC;AAEF,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,8BAA8B;IAC9B,0BAA0B;IAC1B,2BAA2B;IAC3B,wBAAwB;IACxB,uBAAuB;IACvB,qBAAqB;IACrB,wBAAwB;IACxB,sBAAsB;IACtB,oBAAoB;IACpB,yBAAyB;IACzB,SAAS;IACT,iBAAiB;IACjB,4BAA4B;IAC5B,+BAA+B;IAC/B,4BAA4B;IAC5B,yBAAyB;IACzB,wBAAwB;IACxB,0BAA0B;IAC1B,6BAA6B;IAC7B,uBAAuB;IACvB,iBAAiB;IACjB,GAAG,qBAAqB;IACxB,2BAA2B;IAC3B,YAAY;IACZ,yBAAyB;IACzB,kBAAkB;IAClB,0BAA0B;IAC1B,gCAAgC;IAChC,0BAA0B,EAAE,0BAA0B;IACtD,uBAAuB;IACvB,GAAG,iCAAiC;IACpC,GAAG,qBAAqB,CAAC,iCAAiC;IAC1D,GAAG,mBAAmB,CAAC,2CAA2C;CACnE,CAAC","sourcesContent":["import { sql } from \"drizzle-orm\";\nimport {\n index,\n integer,\n primaryKey,\n sqliteTable,\n text,\n uniqueIndex,\n} from \"drizzle-orm/sqlite-core\";\nimport {\n documentOrphanBlobReclaims,\n documentOrphanBlobReclaimTable,\n} from \"../documentPersistence/documentOrphanBlobReclaims\";\nimport { defineSqlTableSchema, type SqlTableSchema } from \"../sqlTableSchema\";\nimport { containerDocumentTombstoneHolds } from \"./containerDocumentTombstoneHoldSchema\";\nimport {\n containerSQLiteSchema,\n containerTableSchemas,\n} from \"./containerSchema\";\nimport {\n documentAttachmentBlobProjection,\n documentPendingAttachments,\n} from \"./documentAttachmentSchema\";\nimport {\n documentDiscoveryHeads,\n documentDiscoverySequence,\n pendingDocumentDiscoveries,\n} from \"./documentDiscoveryEvidenceSchema\";\nimport { documentIntentLinkTargets } from \"./documentPlacementIntentSchema\";\nimport { localIntents } from \"./localIntentSchema\";\nimport * as organizationDataUsage from \"./organizationDataUsageSchema\";\nimport { organizationFounders } from \"./organizationFounderSchema\";\nimport * as provisioningAttempt from \"./organizationProvisioningAttemptSchema\";\nimport {\n organizationReadModelSQLiteSchema,\n organizationReadModelTables,\n} from \"./organizationReadModelTableRegistry\";\nimport { principalGrantRetirements } from \"./principalGrantRetirementSchema\";\nimport {\n principalHistoryEntries,\n principalHistoryEvidenceTables,\n principalHistoryNodes,\n principalHistoryPrefixes,\n} from \"./principalHistoryEvidenceSchema\";\nimport {\n principalHistoryNodeReferences,\n principalHistoryRootOwners,\n} from \"./principalHistoryNodeRetentionSchema\";\nimport { principalKeyEnvelopeArchive } from \"./principalHistoryRetentionSchema\";\nimport {\n principalHistoryStages,\n principalHistoryStageTables,\n} from \"./principalHistoryStageSchema\";\nimport {\n principalMutationJournal,\n principalMutationJournalTables,\n} from \"./principalMutationJournalSchema\";\nimport {\n principalPolicies,\n principalPolicyBundleHistory,\n principalPolicyBundleReferences,\n principalPolicyCheckpoints,\n principalPolicyOrganizations,\n} from \"./principalPolicySchema\";\nimport {\n securityIncidents,\n securityIncidentTable,\n} from \"./securityIncidentSchema\";\nimport {\n verifiedAccessManifestBundles,\n verifiedManifestBundleTables,\n} from \"./verifiedManifestBundleSchema\";\n\nexport {\n containerHydrationTombstones,\n containers,\n dormantContainerMetadata,\n dormantMetadataSweepRequests,\n supersededContainerBindings,\n} from \"./containerSchema\";\nexport {\n documentAttachmentBlobProjection,\n documentPendingAttachments,\n} from \"./documentAttachmentSchema\";\nexport {\n inContainerIntentLanes,\n inLocalIntentLane,\n LOCAL_INTENT_LANES,\n LOCAL_INTENT_SUBJECT,\n localIntents,\n} from \"./localIntentSchema\";\nexport {\n principalPolicies,\n principalPolicyBundleHistory,\n principalPolicyBundleReferences,\n principalPolicyCheckpoints,\n principalPolicyOrganizations,\n} from \"./principalPolicySchema\";\nexport { securityIncidents } from \"./securityIncidentSchema\";\nexport { containerDocumentTombstoneHolds, organizationReadModelTables };\n\nconst accessLevelColumn = \"effective_access_level\";\n\n/**\n * Durable Loro-backed document records shared by app features.\n *\n * This table stores encrypted document runtime state for multiple app domains.\n * `appKind` namespaces records for documents and container metadata. User-facing\n * list data is projected into feature-specific read models.\n *\n * Columns:\n * - `appKind`: Local domain namespace for the row, such as `documents` or\n * `container-metadata`.\n * - `localId`: Stable local id inside `appKind`. It may be created before a\n * server `documentId` exists.\n * - `documentId`: Server document id once the record is known remotely, or\n * `null` while the document is local-only.\n * - `accessEpoch`: Latest access epoch persisted for the document. Defaults to\n * the initial epoch.\n * - `accessStateHash`: Current document access/manifest state hash returned by\n * sync, when known.\n * - `lastCommitLsn`: Last remote commit cursor applied to this local document.\n * - `documentManifestBundle`: Serialized document manifest bundle used for\n * encrypted document sync.\n * - `contentKeyBundle`: Serialized content-key material needed to open the\n * local document.\n * - `documentKekTargets`: Serialized KEK target state for document key wraps.\n * - `pendingBaseVersion`: Encoded oplog version through which every persisted\n * op is already enqueued or synced. Persisted so a restart restores the\n * outgoing-delta marker instead of re-seeding it past a device-first\n * `deferRemoteSync` write's un-advanced op. `null` when unset.\n * - `snapshotEndVersion`: Encoded end version vector of the persisted content\n * (which lives in the durable-history tables, never on this row), written on\n * every content persist. Lets the pending-write queue's deferred-tail scan\n * compare version coverage without loading content. Empty string when the\n * document has never hydrated content, which readers treat as \"no deferred\n * tail claimable\".\n * - `recoveryGeneration`: Atomic raw-history recovery writer fence.\n * - `recoveryDocumentId`: Former server id for cross-client post-purge recreation.\n * - `pullContinuation`: Durable progress for the next bounded remote pull.\n * - `syncFailureStatus`, `syncFailureMessage`, `syncFailureAttemptedAt`: The\n * last terminal sync failure or quarantined incoming update for this scope.\n * The durable write queue (queued rows of `documentHistoryUpdates`) has no\n * status columns, so these let the write-queue view and footer explain why\n * the document is blocked. `syncFailureMessage` is non-null exactly when a\n * failure is recorded; a newer failure replaces the old, and a later fully\n * validated sync pass, a remote reset, or the scope's teardown clears all\n * three.\n * - `updatedAt`: Local timestamp for the last persisted runtime-state update.\n *\n * Indexes:\n * - `(appKind, localId)` is the primary key and scopes local document lookups.\n * - `(appKind, documentId) where documentId is not null` supports resolving a\n * server document id back to the local row during discovery and relinking.\n */\nexport const documents = sqliteTable(\n \"documents\",\n {\n appKind: text(\"app_kind\").notNull(),\n localId: text(\"local_id\").notNull(),\n documentId: text(\"document_id\"),\n accessEpoch: integer(\"access_epoch\").notNull().default(1),\n accessStateHash: text(\"access_state_hash\"),\n effectiveAccessLevel: text(accessLevelColumn).notNull().default(\"read\"),\n lastCommitLsn: text(\"last_commit_lsn\"),\n documentManifestBundle: text(\"document_manifest_bundle\"),\n contentKeyBundle: text(\"content_key_bundle\"),\n documentKekTargets: text(\"document_kek_targets\"),\n pendingBaseVersion: text(\"pending_base_version\"),\n pullContinuation: text(\"pull_continuation\"),\n recoveryGeneration: integer(\"recovery_generation\").notNull().default(0),\n recoveryDocumentId: text(\"recovery_document_id\"),\n snapshotEndVersion: text(\"snapshot_end_version\").notNull().default(\"\"),\n syncFailureStatus: integer(\"sync_failure_status\"),\n syncFailureMessage: text(\"sync_failure_message\"),\n syncFailureAttemptedAt: text(\"sync_failure_attempted_at\"),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.appKind, table.localId] }),\n index(\"documents_app_document_idx\")\n .on(table.appKind, table.documentId)\n .where(sql`${table.documentId} IS NOT NULL`),\n ],\n);\n\n/**\n * Full-history persistence for local documents (checkpoint + tail) — the ONLY\n * durable content source. Record rows in `documents` carry runtime metadata\n * and the content frontier (`snapshotEndVersion`), never content. The\n * checkpoint holds a full-history snapshot refreshed only at compaction, and\n * the tail appends every update (local deltas and decrypted remote updates)\n * written since. Restore = import checkpoint + replay tail; compaction =\n * export full history from the live doc, replace the checkpoint, clear the\n * tail — all bounded and amortized. Container metadata stores its content (an\n * exported-updates blob) as its checkpoint row, with no tail.\n */\nexport const documentHistoryCheckpoints = sqliteTable(\n \"document_history_checkpoints\",\n {\n appKind: text(\"app_kind\").notNull(),\n localId: text(\"local_id\").notNull(),\n snapshot: text(\"snapshot\").notNull(),\n endVersionVector: text(\"end_version_vector\").notNull(),\n revision: text(\"revision\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [primaryKey({ columns: [table.appKind, table.localId] })],\n);\n\n/**\n * The local update log: one row per Loro update, serving as both the history\n * tail (restore = checkpoint + tail) and the outbound write queue.\n *\n * Every locally authored update is stored once. While it waits for sync it\n * carries an `updateId` (the id submitted to the server) and the version-vector\n * metadata the submission needs; settlement clears them, leaving the row as\n * ordinary tail history until compaction folds it into a checkpoint.\n *\n * Columns:\n * - `id`: Row id; tail compaction deletes by it.\n * - `appKind`, `localId`: Document scope matching `documents`.\n * - `updateData`: Serialized Loro update payload.\n * - `origin`: \"local\" (authored on this device) or \"remote\" (decrypted\n * pulled update) for tail rows. Restores use it to advance the\n * outgoing-delta marker across remote rows — their ops are already\n * server-side — while a local row left behind by a crash stays below the\n * marker so the next edit re-derives and sends it. \"outbox\" marks a queued\n * update with no tail role (container metadata, whose content is its\n * checkpoint, and full-state re-uploads after a remote reset); restores and\n * compaction never see it and settlement deletes it.\n * - `createdAt`: Insertion timestamp; with `rowid`, deterministic replay order.\n * - `updateId`: Present exactly while the row is queued for sync; rekeyed\n * after a lost acknowledgement poisons the submitted id.\n * - `partialStartVersionVector` / `partialEndVersionVector`: Version vectors\n * around a queued partial update.\n * - `sourceVersionVector`: Present on a queued rotation baseline checkpoint.\n * - `rekeyCount`: Durable bound on rekeys of a queued row.\n *\n * Invariant: compaction never deletes a queued row, so a checkpoint can cover\n * a queued update's operations without removing it from the queue.\n */\nexport const documentHistoryUpdates = sqliteTable(\n \"document_history_updates\",\n {\n id: text(\"id\"),\n appKind: text(\"app_kind\").notNull(),\n localId: text(\"local_id\").notNull(),\n updateData: text(\"update_data\").notNull(),\n origin: text(\"origin\").notNull(),\n createdAt: text(\"created_at\").notNull(),\n updateId: text(\"update_id\"),\n partialStartVersionVector: text(\"partial_start_version_vector\"),\n partialEndVersionVector: text(\"partial_end_version_vector\"),\n sourceVersionVector: text(\"source_version_vector\"),\n rekeyCount: integer(\"rekey_count\").notNull().default(0),\n },\n (table) => [\n primaryKey({ columns: [table.id] }),\n index(\"document_history_updates_scope_created_idx\").on(\n table.appKind,\n table.localId,\n table.createdAt,\n ),\n uniqueIndex(\"document_history_updates_update_id_idx\")\n .on(table.updateId)\n .where(sql`${table.updateId} IS NOT NULL`),\n ],\n);\n\n/**\n * Anti-rollback pin for verified access manifests.\n *\n * Records the highest access-manifest epoch this client has verified for each\n * object, so a later projection that serves an older epoch (rollback) or a\n * conflicting manifest at the same epoch (equivocation) is detected before any\n * key is unwrapped. Updated monotonically after a projection verifies cleanly.\n *\n * Columns:\n * - `objectKind`: Access object kind (`container`, `document`, ...).\n * - `organizationId`: Organization boundary that owns the object.\n * - `objectId`: Stable object id.\n * - `epoch`: Highest verified manifest epoch.\n * - `manifestHash`: Manifest hash pinned at that epoch.\n * - `updatedAt`: Local timestamp for the pin.\n *\n * Indexes:\n * - `(objectKind, organizationId, objectId)` is the primary key and gives one\n * pin per object.\n */\nexport const accessManifestCheckpoints = sqliteTable(\n \"access_manifest_checkpoints\",\n {\n objectKind: text(\"object_kind\").notNull(),\n organizationId: text(\"organization_id\").notNull(),\n objectId: text(\"object_id\").notNull(),\n epoch: integer(\"epoch\").notNull(),\n manifestHash: text(\"manifest_hash\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [\n primaryKey({\n columns: [table.objectKind, table.organizationId, table.objectId],\n }),\n ],\n);\n\n/**\n * Durable terminal pins for documents whose signed purge proof was verified.\n * These survive remote-state resets so a dishonest API cannot later replay the\n * pre-purge writer projection and resurrect locally deleted content.\n */\nexport const documentPurgeCheckpoints = sqliteTable(\n \"document_purge_checkpoints\",\n {\n documentId: text(\"document_id\").primaryKey(),\n organizationId: text(\"organization_id\").notNull(),\n documentManifestHash: text(\"document_manifest_hash\").notNull(),\n purgeEventHash: text(\"purge_event_hash\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n);\n\n/**\n * Durable trust-on-first-use pins for remote user identity bundles.\n *\n * The first validated identity observed for a user in a configured API trust\n * domain is inserted once. Later observations must match every suite, public\n * key, and fingerprint exactly; the persistence adapter never updates these\n * rows. `firstSeenAt` records when the local trust decision was made and is\n * likewise immutable.\n *\n * Indexes:\n * - `(identityTrustDomain, userId)` is the primary key and scopes a user pin to\n * the host-configured API authority that supplied it.\n * - `(identityTrustDomain, signingKeyFingerprint)` binds the reverse identity\n * mapping durably, including across session recreation. There is no\n * migration for a table that already violates it. Earlier builds pinned a\n * registration's user id before the server confirmed it, so a device that\n * ran one may hold two users on one key; deployments are greenfield, and\n * such a device clears its local data, which recreates this table empty. A\n * device that used an environment before a reset does the same (see\n * docs/developer/greenfield-reset.md).\n */\nexport const trustedUserIdentityPins = sqliteTable(\n \"trusted_user_identity_pins\",\n {\n identityTrustDomain: text(\"identity_trust_domain\").notNull(),\n userId: text(\"user_id\").notNull(),\n formatVersion: integer(\"format_version\").notNull(),\n signingSuite: text(\"signing_suite\").notNull(),\n signingPublicKey: text(\"signing_public_key\").notNull(),\n signingKeyFingerprint: text(\"signing_key_fingerprint\").notNull(),\n encapsulationSuite: text(\"encapsulation_suite\").notNull(),\n encapsulationPublicKey: text(\"encapsulation_public_key\").notNull(),\n encapsulationKeyFingerprint: text(\n \"encapsulation_key_fingerprint\",\n ).notNull(),\n firstSeenAt: text(\"first_seen_at\").notNull(),\n },\n (table) => [\n primaryKey({\n columns: [table.identityTrustDomain, table.userId],\n }),\n uniqueIndex(\"trusted_user_identity_pins_domain_signing_fingerprint_idx\").on(\n table.identityTrustDomain,\n table.signingKeyFingerprint,\n ),\n ],\n);\n\n/**\n * Local many-to-many projection between documents and containers.\n *\n * The document summary row stores one selected `containerId` for primary list\n * placement, while this table keeps the full set of linked containers returned\n * by container/document sync. Removing a container deletes its links and may\n * repair affected document summary rows to point at another remaining link.\n *\n * Columns:\n * - `documentId`: Server document id whose container links are projected.\n * - `containerId`: Linked container id.\n * - `updatedAt`: Local timestamp for the last replacement of the link set.\n *\n * Indexes:\n * - `(documentId, containerId)` is the primary key and prevents duplicate\n * links for the same document/container pair.\n */\nexport const documentContainerProjection = sqliteTable(\n \"document_container_projection\",\n {\n documentId: text(\"document_id\").notNull(),\n containerId: text(\"container_id\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [primaryKey({ columns: [table.documentId, table.containerId] })],\n);\n\n/**\n * Queryable document summary and detail projection.\n *\n * The durable Loro state lives in `documents`. This table stores the fields the\n * app needs for document lists, summaries, and fast detail hydration without\n * opening the full Loro snapshot on every read.\n *\n * Columns:\n * - `localId`: Stable local document id. This joins to `documents.localId` for\n * rows where `documents.appKind = documents`.\n * - `documentId`: Server document id when known.\n * - `containerId`: Selected container for primary document placement, or\n * `null` if the document is not currently linked to a visible container.\n * - `organizationId`: Last-known owning organization. Stamped from the\n * container row when the document is detached because its container was\n * removed locally (e.g. access to a shared org was revoked and the container\n * tombstoned), so pending writes keep their org attribution after the\n * `containers` join source disappears. Read as a fallback only — a live\n * container join always wins.\n * - `documentKind`: Stored document kind. Defaults to `note`.\n * - `title`: Plain projected title. Defaults to the note fallback title.\n * - `updatedAt`: Timestamp used for document list ordering.\n *\n * The projected full text lives in `documentProjectionText`, keyed by the same\n * `localId`. Keeping it out of this row keeps list/window scans narrow: the\n * sidebar and summary queries read titles and placement from rows whose size\n * does not grow with document content.\n *\n * Indexes:\n * - `localId` is the primary key for document projection lookups.\n * - `(containerId) where containerId is not null` serves container-scoped\n * window/count scans without walking the whole table.\n * - `(organizationId) where containerId is null` serves the orphan-recovery\n * existence and window scans.\n * - `(documentId) where documentId is not null` serves the linked-placement\n * join from `documentContainerProjection` and server-id lookups.\n */\nexport const documentProjection = sqliteTable(\n \"document_projection\",\n {\n localId: text(\"local_id\"),\n documentId: text(\"document_id\"),\n containerId: text(\"container_id\"),\n organizationId: text(\"organization_id\"),\n documentKind: text(\"document_kind\").notNull().default(\"note\"),\n title: text(\"title\").notNull().default(\"Untitled note\"),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.localId] }),\n index(\"document_projection_container_idx\")\n .on(table.containerId)\n .where(sql`${table.containerId} IS NOT NULL`),\n index(\"document_projection_orphan_organization_idx\")\n .on(table.organizationId)\n .where(sql`${table.containerId} IS NULL`),\n index(\"document_projection_document_idx\")\n .on(table.documentId)\n .where(sql`${table.documentId} IS NOT NULL`),\n ],\n);\n\n/**\n * Projected document text, split from `documentProjection` so content size\n * never bloats the rows that list/window queries scan. Loaded only by detail\n * reads (`loadDocument`) and the save-timestamp change check.\n *\n * Columns:\n * - `localId`: Stable local document id, matching `documentProjection.localId`.\n * - `text`: Plain projected text used to rebuild summaries/details.\n */\nexport const documentProjectionText = sqliteTable(\n \"document_projection_text\",\n {\n localId: text(\"local_id\"),\n text: text(\"text\").notNull(),\n },\n (table) => [primaryKey({ columns: [table.localId] })],\n);\n\n/**\n * Per-lane sync state for container data: the server cursor and the local\n * freshness marker share one row per lane.\n *\n * Container sync is split into lanes, such as a parent-container child lane or\n * a container-document membership lane. The cursor lets the next request\n * resume without replaying the entire tree. The freshness marker records the\n * last fully drained check independently of the cursor: empty server\n * responses do not always carry a new watermark, but startup still needs to\n * know that it recently checked the lane before polling again.\n *\n * Columns:\n * - `laneKind`: Sync lane category, such as `container_parent` or\n * `container_documents`.\n * - `laneId`: Lane identity. Root parent sync uses `root`, child sync uses a\n * parent-prefixed container id, and document lanes use the container id.\n * - `watermarkUpdatedAt`: Server `updatedAt` value for the stored watermark;\n * null until a watermark is saved.\n * - `watermarkId`: Server id tie-breaker for the stored watermark; null\n * exactly when `watermarkUpdatedAt` is.\n * - `updatedAt`: Local timestamp for when the cursor was saved.\n * - `checkedAt`: Local timestamp for the most recent fully drained lane check;\n * null until one completes.\n *\n * Indexes:\n * - `(laneKind, laneId)` is the primary key and stores one row per lane.\n */\nexport const containerSyncWatermarks = sqliteTable(\n \"container_sync_watermarks\",\n {\n laneKind: text(\"lane_kind\").notNull(),\n laneId: text(\"lane_id\").notNull(),\n watermarkUpdatedAt: text(\"watermark_updated_at\"),\n watermarkId: text(\"watermark_id\"),\n updatedAt: text(\"updated_at\"),\n checkedAt: text(\"checked_at\"),\n },\n (table) => [primaryKey({ columns: [table.laneKind, table.laneId] })],\n);\n\nexport const documentTables: ReadonlyArray<SqlTableSchema> = [\n {\n ...defineSqlTableSchema(documents),\n requiredColumns: [\n \"pull_continuation\",\n \"recovery_generation\",\n \"recovery_document_id\",\n \"sync_failure_status\",\n \"sync_failure_message\",\n \"sync_failure_attempted_at\",\n ],\n },\n defineSqlTableSchema(documentHistoryCheckpoints),\n {\n ...defineSqlTableSchema(documentHistoryUpdates),\n requiredColumns: [\"update_id\"],\n },\n];\n\nexport const principalPolicyTables: ReadonlyArray<SqlTableSchema> = [\n {\n ...defineSqlTableSchema(principalPolicies),\n requiredColumns: [\"current_grants_json\"],\n },\n {\n ...defineSqlTableSchema(principalPolicyBundleHistory),\n requiredColumns: [\"current_grants_json\"],\n },\n defineSqlTableSchema(principalPolicyBundleReferences),\n defineSqlTableSchema(principalPolicyOrganizations),\n ...principalHistoryStageTables,\n ...principalHistoryEvidenceTables,\n];\n\nexport const keyingCheckpointTables: ReadonlyArray<SqlTableSchema> = [\n defineSqlTableSchema(organizationFounders),\n defineSqlTableSchema(accessManifestCheckpoints),\n defineSqlTableSchema(principalGrantRetirements),\n defineSqlTableSchema(documentPurgeCheckpoints),\n defineSqlTableSchema(principalPolicyCheckpoints),\n];\n\nexport const trustedUserIdentityPinTables: ReadonlyArray<SqlTableSchema> = [\n defineSqlTableSchema(trustedUserIdentityPins),\n];\n\nconst securityIncidentTables: ReadonlyArray<SqlTableSchema> = [\n securityIncidentTable,\n];\n\nexport const containerTables = containerTableSchemas;\n\nexport const documentContainerProjectionTables: ReadonlyArray<SqlTableSchema> =\n [\n defineSqlTableSchema(documentContainerProjection),\n {\n ...defineSqlTableSchema(containerDocumentTombstoneHolds),\n requiredColumns: [\"hidden\"],\n },\n defineSqlTableSchema(pendingDocumentDiscoveries),\n {\n ...defineSqlTableSchema(documentDiscoveryHeads),\n requiredColumns: [\"organization_id\"],\n },\n defineSqlTableSchema(documentDiscoverySequence),\n ];\n\nexport const localIntentTables: ReadonlyArray<SqlTableSchema> = [\n defineSqlTableSchema(localIntents),\n];\n\nexport const documentPlacementIntentTables: ReadonlyArray<SqlTableSchema> = [\n ...localIntentTables,\n defineSqlTableSchema(documentIntentLinkTargets),\n];\n\nexport const documentProjectionTables: ReadonlyArray<SqlTableSchema> = [\n defineSqlTableSchema(documentProjection),\n defineSqlTableSchema(documentProjectionText),\n {\n ...defineSqlTableSchema(documentPendingAttachments),\n requiredColumns: [\"content_sha256\"],\n },\n {\n ...defineSqlTableSchema(documentAttachmentBlobProjection),\n requiredColumns: [\"content_sha256\"],\n },\n documentOrphanBlobReclaimTable,\n];\n\nexport const containerSyncWatermarkTables: ReadonlyArray<SqlTableSchema> = [\n {\n ...defineSqlTableSchema(containerSyncWatermarks),\n requiredColumns: [\"checked_at\"],\n },\n];\n\nexport const clientSqlTables: ReadonlyArray<SqlTableSchema> = [\n ...principalMutationJournalTables,\n ...documentTables,\n ...principalPolicyTables,\n ...keyingCheckpointTables,\n ...verifiedManifestBundleTables,\n ...trustedUserIdentityPinTables,\n ...securityIncidentTables,\n ...containerTables,\n ...documentContainerProjectionTables,\n ...documentPlacementIntentTables,\n ...documentProjectionTables,\n ...containerSyncWatermarkTables,\n ...organizationReadModelTables,\n ...organizationDataUsage.organizationDataUsageTables,\n ...provisioningAttempt.organizationProvisioningAttemptTables,\n];\n\nexport const clientSQLiteSchema = {\n principalHistoryNodeReferences,\n principalHistoryRootOwners,\n principalKeyEnvelopeArchive,\n principalMutationJournal,\n principalHistoryEntries,\n principalHistoryNodes,\n principalHistoryPrefixes,\n principalHistoryStages,\n organizationFounders,\n principalGrantRetirements,\n documents,\n principalPolicies,\n principalPolicyBundleHistory,\n principalPolicyBundleReferences,\n principalPolicyOrganizations,\n accessManifestCheckpoints,\n documentPurgeCheckpoints,\n principalPolicyCheckpoints,\n verifiedAccessManifestBundles,\n trustedUserIdentityPins,\n securityIncidents,\n ...containerSQLiteSchema,\n documentContainerProjection,\n localIntents,\n documentIntentLinkTargets,\n documentProjection,\n documentPendingAttachments,\n documentAttachmentBlobProjection,\n documentOrphanBlobReclaims: documentOrphanBlobReclaims,\n containerSyncWatermarks,\n ...organizationReadModelSQLiteSchema,\n ...organizationDataUsage.organizationDataUsageSQLiteSchema,\n ...provisioningAttempt.organizationProvisioningAttemptSQLiteSchema,\n};\n"]}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EACL,KAAK,EACL,OAAO,EACP,UAAU,EACV,WAAW,EACX,IAAI,EACJ,WAAW,GACZ,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,oBAAoB,EAAuB,MAAM,mBAAmB,CAAC;AAC9E,OAAO,EAAE,+BAA+B,EAAE,MAAM,wCAAwC,CAAC;AACzF,OAAO,EACL,qBAAqB,EACrB,qBAAqB,GACtB,MAAM,mBAAmB,CAAC;AAC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AC1B,6BAA6B;IAC7B,uBAAuB;IACvB,iBAAiB;IACjB,GAAG,qBAAqB;IACxB,2BAA2B;IAC3B,YAAY;IACZ,yBAAyB;IACzB,kBAAkB;IAClB,0BAA0B;IAC1B,gCAAgC;IAChC,0BAA0B,EAAE,0BAA0B;IACtD,uBAAuB;IACvB,GAAG,iCAAiC;IACpC,GAAG,qBAAqB,CAAC,iCAAiC;IAC1D,GAAG,mBAAmB,CAAC,2CAA2C;CACnE,CAAC","sourcesContent":["import { sql } from \"drizzle-orm\";\nimport {\n index,\n integer,\n primaryKey,\n sqliteTable,\n text,\n uniqueIndex,\n} from \"drizzle-orm/sqlite-core\";\nimport { defineSqlTableSchema, type SqlTableSchema } from \"../sqlTableSchema\";\nimport { containerDocumentTombstoneHolds } from \"./containerDocumentTombstoneHoldSchema\";\nimport {\n containerSQLiteSchema,\n containerTableSchemas,\n} from \"./containerSchema\";\nimport {\n documentAttachmentBlobProjection,\n documentPendingAttachments,\n} from \"./documentAttachmentSchema\";\nimport {\n documentDiscoveryHeads,\n documentDiscoverySequence,\n pendingDocumentDiscoveries,\n} from \"./documentDiscoveryEvidenceSchema\";\nimport {\n documentOrphanBlobReclaims,\n documentOrphanBlobReclaimTable,\n} from \"./documentOrphanBlobReclaimSchema\";\nimport { documentIntentLinkTargets } from \"./documentPlacementIntentSchema\";\nimport { localIntents } from \"./localIntentSchema\";\nimport * as organizationDataUsage from \"./organizationDataUsageSchema\";\nimport { organizationFounders } from \"./organizationFounderSchema\";\nimport * as provisioningAttempt from \"./organizationProvisioningAttemptSchema\";\nimport {\n organizationReadModelSQLiteSchema,\n organizationReadModelTables,\n} from \"./organizationReadModelTableRegistry\";\nimport { principalGrantRetirements } from \"./principalGrantRetirementSchema\";\nimport {\n principalHistoryEntries,\n principalHistoryEvidenceTables,\n principalHistoryNodes,\n principalHistoryPrefixes,\n} from \"./principalHistoryEvidenceSchema\";\nimport {\n principalHistoryNodeReferences,\n principalHistoryRootOwners,\n} from \"./principalHistoryNodeRetentionSchema\";\nimport { principalKeyEnvelopeArchive } from \"./principalHistoryRetentionSchema\";\nimport {\n principalHistoryStages,\n principalHistoryStageTables,\n} from \"./principalHistoryStageSchema\";\nimport {\n principalMutationJournal,\n principalMutationJournalTables,\n} from \"./principalMutationJournalSchema\";\nimport {\n principalPolicies,\n principalPolicyBundleHistory,\n principalPolicyBundleReferences,\n principalPolicyCheckpoints,\n principalPolicyOrganizations,\n} from \"./principalPolicySchema\";\nimport {\n securityIncidents,\n securityIncidentTable,\n} from \"./securityIncidentSchema\";\nimport {\n verifiedAccessManifestBundles,\n verifiedManifestBundleTables,\n} from \"./verifiedManifestBundleSchema\";\n\nexport {\n containerHydrationTombstones,\n containers,\n dormantContainerMetadata,\n dormantMetadataSweepRequests,\n supersededContainerBindings,\n} from \"./containerSchema\";\nexport {\n documentAttachmentBlobProjection,\n documentPendingAttachments,\n} from \"./documentAttachmentSchema\";\nexport {\n inContainerIntentLanes,\n inLocalIntentLane,\n LOCAL_INTENT_LANES,\n LOCAL_INTENT_SUBJECT,\n localIntents,\n} from \"./localIntentSchema\";\nexport {\n principalPolicies,\n principalPolicyBundleHistory,\n principalPolicyBundleReferences,\n principalPolicyCheckpoints,\n principalPolicyOrganizations,\n} from \"./principalPolicySchema\";\nexport { securityIncidents } from \"./securityIncidentSchema\";\nexport { containerDocumentTombstoneHolds, organizationReadModelTables };\n\nconst accessLevelColumn = \"effective_access_level\";\n\n/**\n * Durable Loro-backed document records shared by app features.\n *\n * This table stores encrypted document runtime state for multiple app domains.\n * `appKind` namespaces records for documents and container metadata. User-facing\n * list data is projected into feature-specific read models.\n *\n * Columns:\n * - `appKind`: Local domain namespace for the row, such as `documents` or\n * `container-metadata`.\n * - `localId`: Stable local id inside `appKind`. It may be created before a\n * server `documentId` exists.\n * - `documentId`: Server document id once the record is known remotely, or\n * `null` while the document is local-only.\n * - `accessEpoch`: Latest access epoch persisted for the document. Defaults to\n * the initial epoch.\n * - `accessStateHash`: Current document access/manifest state hash returned by\n * sync, when known.\n * - `lastCommitLsn`: Last remote commit cursor applied to this local document.\n * - `documentManifestBundle`: Serialized document manifest bundle used for\n * encrypted document sync.\n * - `contentKeyBundle`: Serialized content-key material needed to open the\n * local document.\n * - `documentKekTargets`: Serialized KEK target state for document key wraps.\n * - `pendingBaseVersion`: Encoded oplog version through which every persisted\n * op is already enqueued or synced. Persisted so a restart restores the\n * outgoing-delta marker instead of re-seeding it past a device-first\n * `deferRemoteSync` write's un-advanced op. `null` when unset.\n * - `snapshotEndVersion`: Encoded end version vector of the persisted content\n * (which lives in the durable-history tables, never on this row), written on\n * every content persist. Lets the pending-write queue's deferred-tail scan\n * compare version coverage without loading content. Empty string when the\n * document has never hydrated content, which readers treat as \"no deferred\n * tail claimable\".\n * - `recoveryGeneration`: Atomic raw-history recovery writer fence.\n * - `recoveryDocumentId`: Former server id for cross-client post-purge recreation.\n * - `pullContinuation`: Durable progress for the next bounded remote pull.\n * - `syncFailureStatus`, `syncFailureMessage`, `syncFailureAttemptedAt`: The\n * last terminal sync failure or quarantined incoming update for this scope.\n * The durable write queue (queued rows of `documentHistoryUpdates`) has no\n * status columns, so these let the write-queue view and footer explain why\n * the document is blocked. `syncFailureMessage` is non-null exactly when a\n * failure is recorded; a newer failure replaces the old, and a later fully\n * validated sync pass, a remote reset, or the scope's teardown clears all\n * three.\n * - `updatedAt`: Local timestamp for the last persisted runtime-state update.\n *\n * Indexes:\n * - `(appKind, localId)` is the primary key and scopes local document lookups.\n * - `(appKind, documentId) where documentId is not null` supports resolving a\n * server document id back to the local row during discovery and relinking.\n */\nexport const documents = sqliteTable(\n \"documents\",\n {\n appKind: text(\"app_kind\").notNull(),\n localId: text(\"local_id\").notNull(),\n documentId: text(\"document_id\"),\n accessEpoch: integer(\"access_epoch\").notNull().default(1),\n accessStateHash: text(\"access_state_hash\"),\n effectiveAccessLevel: text(accessLevelColumn).notNull().default(\"read\"),\n lastCommitLsn: text(\"last_commit_lsn\"),\n documentManifestBundle: text(\"document_manifest_bundle\"),\n contentKeyBundle: text(\"content_key_bundle\"),\n documentKekTargets: text(\"document_kek_targets\"),\n pendingBaseVersion: text(\"pending_base_version\"),\n pullContinuation: text(\"pull_continuation\"),\n recoveryGeneration: integer(\"recovery_generation\").notNull().default(0),\n recoveryDocumentId: text(\"recovery_document_id\"),\n snapshotEndVersion: text(\"snapshot_end_version\").notNull().default(\"\"),\n syncFailureStatus: integer(\"sync_failure_status\"),\n syncFailureMessage: text(\"sync_failure_message\"),\n syncFailureAttemptedAt: text(\"sync_failure_attempted_at\"),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.appKind, table.localId] }),\n index(\"documents_app_document_idx\")\n .on(table.appKind, table.documentId)\n .where(sql`${table.documentId} IS NOT NULL`),\n ],\n);\n\n/**\n * Full-history persistence for local documents (checkpoint + tail) — the ONLY\n * durable content source. Record rows in `documents` carry runtime metadata\n * and the content frontier (`snapshotEndVersion`), never content. The\n * checkpoint holds a full-history snapshot refreshed only at compaction, and\n * the tail appends every update (local deltas and decrypted remote updates)\n * written since. Restore = import checkpoint + replay tail; compaction =\n * export full history from the live doc, replace the checkpoint, clear the\n * tail — all bounded and amortized. Container metadata stores its content (an\n * exported-updates blob) as its checkpoint row, with no tail.\n */\nexport const documentHistoryCheckpoints = sqliteTable(\n \"document_history_checkpoints\",\n {\n appKind: text(\"app_kind\").notNull(),\n localId: text(\"local_id\").notNull(),\n snapshot: text(\"snapshot\").notNull(),\n endVersionVector: text(\"end_version_vector\").notNull(),\n revision: text(\"revision\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [primaryKey({ columns: [table.appKind, table.localId] })],\n);\n\n/**\n * The local update log: one row per Loro update, serving as both the history\n * tail (restore = checkpoint + tail) and the outbound write queue.\n *\n * Every locally authored update is stored once. While it waits for sync it\n * carries an `updateId` (the id submitted to the server) and the version-vector\n * metadata the submission needs; settlement clears them, leaving the row as\n * ordinary tail history until compaction folds it into a checkpoint.\n *\n * Columns:\n * - `id`: Row id; tail compaction deletes by it.\n * - `appKind`, `localId`: Document scope matching `documents`.\n * - `updateData`: Serialized Loro update payload.\n * - `origin`: \"local\" (authored on this device) or \"remote\" (decrypted\n * pulled update) for tail rows. Restores use it to advance the\n * outgoing-delta marker across remote rows — their ops are already\n * server-side — while a local row left behind by a crash stays below the\n * marker so the next edit re-derives and sends it. \"outbox\" marks a queued\n * update with no tail role (container metadata, whose content is its\n * checkpoint, and full-state re-uploads after a remote reset); restores and\n * compaction never see it and settlement deletes it.\n * - `createdAt`: Insertion timestamp; with `rowid`, deterministic replay order.\n * - `updateId`: Present exactly while the row is queued for sync; rekeyed\n * after a lost acknowledgement poisons the submitted id.\n * - `partialStartVersionVector` / `partialEndVersionVector`: Version vectors\n * around a queued partial update.\n * - `sourceVersionVector`: Present on a queued rotation baseline checkpoint.\n * - `rekeyCount`: Durable bound on rekeys of a queued row.\n *\n * Invariant: compaction never deletes a queued row, so a checkpoint can cover\n * a queued update's operations without removing it from the queue.\n */\nexport const documentHistoryUpdates = sqliteTable(\n \"document_history_updates\",\n {\n id: text(\"id\"),\n appKind: text(\"app_kind\").notNull(),\n localId: text(\"local_id\").notNull(),\n updateData: text(\"update_data\").notNull(),\n origin: text(\"origin\").notNull(),\n createdAt: text(\"created_at\").notNull(),\n updateId: text(\"update_id\"),\n partialStartVersionVector: text(\"partial_start_version_vector\"),\n partialEndVersionVector: text(\"partial_end_version_vector\"),\n sourceVersionVector: text(\"source_version_vector\"),\n rekeyCount: integer(\"rekey_count\").notNull().default(0),\n },\n (table) => [\n primaryKey({ columns: [table.id] }),\n index(\"document_history_updates_scope_created_idx\").on(\n table.appKind,\n table.localId,\n table.createdAt,\n ),\n uniqueIndex(\"document_history_updates_update_id_idx\")\n .on(table.updateId)\n .where(sql`${table.updateId} IS NOT NULL`),\n ],\n);\n\n/**\n * Anti-rollback pin for verified access manifests.\n *\n * Records the highest access-manifest epoch this client has verified for each\n * object, so a later projection that serves an older epoch (rollback) or a\n * conflicting manifest at the same epoch (equivocation) is detected before any\n * key is unwrapped. Updated monotonically after a projection verifies cleanly.\n *\n * Columns:\n * - `objectKind`: Access object kind (`container`, `document`, ...).\n * - `organizationId`: Organization boundary that owns the object.\n * - `objectId`: Stable object id.\n * - `epoch`: Highest verified manifest epoch.\n * - `manifestHash`: Manifest hash pinned at that epoch.\n * - `updatedAt`: Local timestamp for the pin.\n *\n * Indexes:\n * - `(objectKind, organizationId, objectId)` is the primary key and gives one\n * pin per object.\n */\nexport const accessManifestCheckpoints = sqliteTable(\n \"access_manifest_checkpoints\",\n {\n objectKind: text(\"object_kind\").notNull(),\n organizationId: text(\"organization_id\").notNull(),\n objectId: text(\"object_id\").notNull(),\n epoch: integer(\"epoch\").notNull(),\n manifestHash: text(\"manifest_hash\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [\n primaryKey({\n columns: [table.objectKind, table.organizationId, table.objectId],\n }),\n ],\n);\n\n/**\n * Durable terminal pins for documents whose signed purge proof was verified.\n * These survive remote-state resets so a dishonest API cannot later replay the\n * pre-purge writer projection and resurrect locally deleted content.\n */\nexport const documentPurgeCheckpoints = sqliteTable(\n \"document_purge_checkpoints\",\n {\n documentId: text(\"document_id\").primaryKey(),\n organizationId: text(\"organization_id\").notNull(),\n documentManifestHash: text(\"document_manifest_hash\").notNull(),\n purgeEventHash: text(\"purge_event_hash\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n);\n\n/**\n * Durable trust-on-first-use pins for remote user identity bundles.\n *\n * The first validated identity observed for a user in a configured API trust\n * domain is inserted once. Later observations must match every suite, public\n * key, and fingerprint exactly; the persistence adapter never updates these\n * rows. `firstSeenAt` records when the local trust decision was made and is\n * likewise immutable.\n *\n * Indexes:\n * - `(identityTrustDomain, userId)` is the primary key and scopes a user pin to\n * the host-configured API authority that supplied it.\n * - `(identityTrustDomain, signingKeyFingerprint)` binds the reverse identity\n * mapping durably, including across session recreation. There is no\n * migration for a table that already violates it. Earlier builds pinned a\n * registration's user id before the server confirmed it, so a device that\n * ran one may hold two users on one key; deployments are greenfield, and\n * such a device clears its local data, which recreates this table empty. A\n * device that used an environment before a reset does the same (see\n * docs/developer/greenfield-reset.md).\n */\nexport const trustedUserIdentityPins = sqliteTable(\n \"trusted_user_identity_pins\",\n {\n identityTrustDomain: text(\"identity_trust_domain\").notNull(),\n userId: text(\"user_id\").notNull(),\n formatVersion: integer(\"format_version\").notNull(),\n signingSuite: text(\"signing_suite\").notNull(),\n signingPublicKey: text(\"signing_public_key\").notNull(),\n signingKeyFingerprint: text(\"signing_key_fingerprint\").notNull(),\n encapsulationSuite: text(\"encapsulation_suite\").notNull(),\n encapsulationPublicKey: text(\"encapsulation_public_key\").notNull(),\n encapsulationKeyFingerprint: text(\n \"encapsulation_key_fingerprint\",\n ).notNull(),\n firstSeenAt: text(\"first_seen_at\").notNull(),\n },\n (table) => [\n primaryKey({\n columns: [table.identityTrustDomain, table.userId],\n }),\n uniqueIndex(\"trusted_user_identity_pins_domain_signing_fingerprint_idx\").on(\n table.identityTrustDomain,\n table.signingKeyFingerprint,\n ),\n ],\n);\n\n/**\n * Local many-to-many projection between documents and containers.\n *\n * The document summary row stores one selected `containerId` for primary list\n * placement, while this table keeps the full set of linked containers returned\n * by container/document sync. Removing a container deletes its links and may\n * repair affected document summary rows to point at another remaining link.\n *\n * Columns:\n * - `documentId`: Server document id whose container links are projected.\n * - `containerId`: Linked container id.\n * - `updatedAt`: Local timestamp for the last replacement of the link set.\n *\n * Indexes:\n * - `(documentId, containerId)` is the primary key and prevents duplicate\n * links for the same document/container pair.\n */\nexport const documentContainerProjection = sqliteTable(\n \"document_container_projection\",\n {\n documentId: text(\"document_id\").notNull(),\n containerId: text(\"container_id\").notNull(),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [primaryKey({ columns: [table.documentId, table.containerId] })],\n);\n\n/**\n * Queryable document summary and detail projection.\n *\n * The durable Loro state lives in `documents`. This table stores the fields the\n * app needs for document lists, summaries, and fast detail hydration without\n * opening the full Loro snapshot on every read.\n *\n * Columns:\n * - `localId`: Stable local document id. This joins to `documents.localId` for\n * rows where `documents.appKind = documents`.\n * - `documentId`: Server document id when known.\n * - `containerId`: Selected container for primary document placement, or\n * `null` if the document is not currently linked to a visible container.\n * - `organizationId`: Last-known owning organization. Stamped from the\n * container row when the document is detached because its container was\n * removed locally (e.g. access to a shared org was revoked and the container\n * tombstoned), so pending writes keep their org attribution after the\n * `containers` join source disappears. Read as a fallback only — a live\n * container join always wins.\n * - `documentKind`: Stored document kind. Defaults to `note`.\n * - `title`: Plain projected title. Defaults to the note fallback title.\n * - `updatedAt`: Timestamp used for document list ordering.\n *\n * The projected full text lives in `documentProjectionText`, keyed by the same\n * `localId`. Keeping it out of this row keeps list/window scans narrow: the\n * sidebar and summary queries read titles and placement from rows whose size\n * does not grow with document content.\n *\n * Indexes:\n * - `localId` is the primary key for document projection lookups.\n * - `(containerId) where containerId is not null` serves container-scoped\n * window/count scans without walking the whole table.\n * - `(organizationId) where containerId is null` serves the orphan-recovery\n * existence and window scans.\n * - `(documentId) where documentId is not null` serves the linked-placement\n * join from `documentContainerProjection` and server-id lookups.\n */\nexport const documentProjection = sqliteTable(\n \"document_projection\",\n {\n localId: text(\"local_id\"),\n documentId: text(\"document_id\"),\n containerId: text(\"container_id\"),\n organizationId: text(\"organization_id\"),\n documentKind: text(\"document_kind\").notNull().default(\"note\"),\n title: text(\"title\").notNull().default(\"Untitled note\"),\n updatedAt: text(\"updated_at\").notNull(),\n },\n (table) => [\n primaryKey({ columns: [table.localId] }),\n index(\"document_projection_container_idx\")\n .on(table.containerId)\n .where(sql`${table.containerId} IS NOT NULL`),\n index(\"document_projection_orphan_organization_idx\")\n .on(table.organizationId)\n .where(sql`${table.containerId} IS NULL`),\n index(\"document_projection_document_idx\")\n .on(table.documentId)\n .where(sql`${table.documentId} IS NOT NULL`),\n ],\n);\n\n/**\n * Projected document text, split from `documentProjection` so content size\n * never bloats the rows that list/window queries scan. Loaded only by detail\n * reads (`loadDocument`) and the save-timestamp change check.\n *\n * Columns:\n * - `localId`: Stable local document id, matching `documentProjection.localId`.\n * - `text`: Plain projected text used to rebuild summaries/details.\n */\nexport const documentProjectionText = sqliteTable(\n \"document_projection_text\",\n {\n localId: text(\"local_id\"),\n text: text(\"text\").notNull(),\n },\n (table) => [primaryKey({ columns: [table.localId] })],\n);\n\n/**\n * Per-lane sync state for container data: the server cursor and the local\n * freshness marker share one row per lane.\n *\n * Container sync is split into lanes, such as a parent-container child lane or\n * a container-document membership lane. The cursor lets the next request\n * resume without replaying the entire tree. The freshness marker records the\n * last fully drained check independently of the cursor: empty server\n * responses do not always carry a new watermark, but startup still needs to\n * know that it recently checked the lane before polling again.\n *\n * Columns:\n * - `laneKind`: Sync lane category, such as `container_parent` or\n * `container_documents`.\n * - `laneId`: Lane identity. Root parent sync uses `root`, child sync uses a\n * parent-prefixed container id, and document lanes use the container id.\n * - `watermarkUpdatedAt`: Server `updatedAt` value for the stored watermark;\n * null until a watermark is saved.\n * - `watermarkId`: Server id tie-breaker for the stored watermark; null\n * exactly when `watermarkUpdatedAt` is.\n * - `updatedAt`: Local timestamp for when the cursor was saved.\n * - `checkedAt`: Local timestamp for the most recent fully drained lane check;\n * null until one completes.\n *\n * Indexes:\n * - `(laneKind, laneId)` is the primary key and stores one row per lane.\n */\nexport const containerSyncWatermarks = sqliteTable(\n \"container_sync_watermarks\",\n {\n laneKind: text(\"lane_kind\").notNull(),\n laneId: text(\"lane_id\").notNull(),\n watermarkUpdatedAt: text(\"watermark_updated_at\"),\n watermarkId: text(\"watermark_id\"),\n updatedAt: text(\"updated_at\"),\n checkedAt: text(\"checked_at\"),\n },\n (table) => [primaryKey({ columns: [table.laneKind, table.laneId] })],\n);\n\nexport const documentTables: ReadonlyArray<SqlTableSchema> = [\n {\n ...defineSqlTableSchema(documents),\n requiredColumns: [\n \"pull_continuation\",\n \"recovery_generation\",\n \"recovery_document_id\",\n \"sync_failure_status\",\n \"sync_failure_message\",\n \"sync_failure_attempted_at\",\n ],\n },\n defineSqlTableSchema(documentHistoryCheckpoints),\n {\n ...defineSqlTableSchema(documentHistoryUpdates),\n requiredColumns: [\"update_id\"],\n },\n];\n\nexport const principalPolicyTables: ReadonlyArray<SqlTableSchema> = [\n {\n ...defineSqlTableSchema(principalPolicies),\n requiredColumns: [\"current_grants_json\"],\n },\n {\n ...defineSqlTableSchema(principalPolicyBundleHistory),\n requiredColumns: [\"current_grants_json\"],\n },\n defineSqlTableSchema(principalPolicyBundleReferences),\n defineSqlTableSchema(principalPolicyOrganizations),\n ...principalHistoryStageTables,\n ...principalHistoryEvidenceTables,\n];\n\nexport const keyingCheckpointTables: ReadonlyArray<SqlTableSchema> = [\n defineSqlTableSchema(organizationFounders),\n defineSqlTableSchema(accessManifestCheckpoints),\n defineSqlTableSchema(principalGrantRetirements),\n defineSqlTableSchema(documentPurgeCheckpoints),\n defineSqlTableSchema(principalPolicyCheckpoints),\n];\n\nexport const trustedUserIdentityPinTables: ReadonlyArray<SqlTableSchema> = [\n defineSqlTableSchema(trustedUserIdentityPins),\n];\n\nconst securityIncidentTables: ReadonlyArray<SqlTableSchema> = [\n securityIncidentTable,\n];\n\nexport const containerTables = containerTableSchemas;\n\nexport const documentContainerProjectionTables: ReadonlyArray<SqlTableSchema> =\n [\n defineSqlTableSchema(documentContainerProjection),\n {\n ...defineSqlTableSchema(containerDocumentTombstoneHolds),\n requiredColumns: [\"hidden\"],\n },\n defineSqlTableSchema(pendingDocumentDiscoveries),\n {\n ...defineSqlTableSchema(documentDiscoveryHeads),\n requiredColumns: [\"organization_id\"],\n },\n defineSqlTableSchema(documentDiscoverySequence),\n ];\n\nexport const localIntentTables: ReadonlyArray<SqlTableSchema> = [\n defineSqlTableSchema(localIntents),\n];\n\nexport const documentPlacementIntentTables: ReadonlyArray<SqlTableSchema> = [\n ...localIntentTables,\n defineSqlTableSchema(documentIntentLinkTargets),\n];\n\nexport const documentProjectionTables: ReadonlyArray<SqlTableSchema> = [\n defineSqlTableSchema(documentProjection),\n defineSqlTableSchema(documentProjectionText),\n {\n ...defineSqlTableSchema(documentPendingAttachments),\n requiredColumns: [\"content_sha256\"],\n },\n {\n ...defineSqlTableSchema(documentAttachmentBlobProjection),\n requiredColumns: [\"content_sha256\"],\n },\n documentOrphanBlobReclaimTable,\n];\n\nexport const containerSyncWatermarkTables: ReadonlyArray<SqlTableSchema> = [\n {\n ...defineSqlTableSchema(containerSyncWatermarks),\n requiredColumns: [\"checked_at\"],\n },\n];\n\nexport const clientSqlTables: ReadonlyArray<SqlTableSchema> = [\n ...principalMutationJournalTables,\n ...documentTables,\n ...principalPolicyTables,\n ...keyingCheckpointTables,\n ...verifiedManifestBundleTables,\n ...trustedUserIdentityPinTables,\n ...securityIncidentTables,\n ...containerTables,\n ...documentContainerProjectionTables,\n ...documentPlacementIntentTables,\n ...documentProjectionTables,\n ...containerSyncWatermarkTables,\n ...organizationReadModelTables,\n ...organizationDataUsage.organizationDataUsageTables,\n ...provisioningAttempt.organizationProvisioningAttemptTables,\n];\n\nexport const clientSQLiteSchema = {\n principalHistoryNodeReferences,\n principalHistoryRootOwners,\n principalKeyEnvelopeArchive,\n principalMutationJournal,\n principalHistoryEntries,\n principalHistoryNodes,\n principalHistoryPrefixes,\n principalHistoryStages,\n organizationFounders,\n principalGrantRetirements,\n documents,\n principalPolicies,\n principalPolicyBundleHistory,\n principalPolicyBundleReferences,\n principalPolicyOrganizations,\n accessManifestCheckpoints,\n documentPurgeCheckpoints,\n principalPolicyCheckpoints,\n verifiedAccessManifestBundles,\n trustedUserIdentityPins,\n securityIncidents,\n ...containerSQLiteSchema,\n documentContainerProjection,\n localIntents,\n documentIntentLinkTargets,\n documentProjection,\n documentPendingAttachments,\n documentAttachmentBlobProjection,\n documentOrphanBlobReclaims: documentOrphanBlobReclaims,\n containerSyncWatermarks,\n ...organizationReadModelSQLiteSchema,\n ...organizationDataUsage.organizationDataUsageSQLiteSchema,\n ...provisioningAttempt.organizationProvisioningAttemptSQLiteSchema,\n};\n"]}
@@ -27,4 +27,16 @@ export function sqliteRowsPerStatement(variablesPerRow, fixedVariables = 0) {
27
27
  export function sqliteInsertBatchSize(table) {
28
28
  return sqliteRowsPerStatement(Object.keys(getTableColumns(table)).length);
29
29
  }
30
+ // Keep bulk inserts, IN predicates, and expanded pair predicates comfortably
31
+ // below SQLite's common 999-bind and expression-depth limits. Some queued rows
32
+ // consume more than ten binds each, so one conservative size covers every
33
+ // reset statement.
34
+ export const REMOTE_RESET_SQL_BATCH_SIZE = 50;
35
+ export function remoteResetBatches(values) {
36
+ const batches = [];
37
+ for (let offset = 0; offset < values.length; offset += REMOTE_RESET_SQL_BATCH_SIZE) {
38
+ batches.push(values.slice(offset, offset + REMOTE_RESET_SQL_BATCH_SIZE));
39
+ }
40
+ return batches;
41
+ }
30
42
  //# sourceMappingURL=sqliteBindBudget.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"sqliteBindBudget.js","sourceRoot":"","sources":["sqliteBindBudget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAG9C;;;;GAIG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,GAAG,CAAC;AAEhD;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CACpC,eAAuB,EACvB,cAAc,GAAG,CAAC;IAElB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CACrB,CAAC,4BAA4B,GAAG,cAAc,CAAC,GAAG,eAAe,CAClE,CAAC;IACF,IACE,CAAC,MAAM,CAAC,SAAS,CAAC,eAAe,CAAC;QAClC,eAAe,GAAG,CAAC;QACnB,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC;QACjC,cAAc,GAAG,CAAC;QAClB,IAAI,GAAG,CAAC,EACR,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,KAAkB;IACtD,OAAO,sBAAsB,CAAC,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AAC5E,CAAC","sourcesContent":["import { getTableColumns } from \"drizzle-orm\";\nimport type { SQLiteTable } from \"drizzle-orm/sqlite-core\";\n\n/**\n * SQLite's historical default `SQLITE_MAX_VARIABLE_NUMBER`. Some hosts raise\n * it, but the SDK runs on several SQLite builds, so every statement it batches\n * stays within this budget.\n */\nexport const SQLITE_BOUND_VARIABLE_BUDGET = 999;\n\n/**\n * Rows (or list entries) one statement may carry when each binds\n * `variablesPerRow` values on top of `fixedVariables` shared ones.\n */\nexport function sqliteRowsPerStatement(\n variablesPerRow: number,\n fixedVariables = 0,\n): number {\n const rows = Math.floor(\n (SQLITE_BOUND_VARIABLE_BUDGET - fixedVariables) / variablesPerRow,\n );\n if (\n !Number.isInteger(variablesPerRow) ||\n variablesPerRow < 1 ||\n !Number.isInteger(fixedVariables) ||\n fixedVariables < 0 ||\n rows < 1\n ) {\n throw new Error(\"SQLite statement cannot fit its bound variables\");\n }\n return rows;\n}\n\n/**\n * Rows per multi-row INSERT into `table`. Every column may bind one value,\n * including explicit nulls, so the column count is the per-row ceiling.\n */\nexport function sqliteInsertBatchSize(table: SQLiteTable): number {\n return sqliteRowsPerStatement(Object.keys(getTableColumns(table)).length);\n}\n"]}
1
+ {"version":3,"file":"sqliteBindBudget.js","sourceRoot":"","sources":["sqliteBindBudget.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAG9C;;;;GAIG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,GAAG,CAAC;AAEhD;;;GAGG;AACH,MAAM,UAAU,sBAAsB,CACpC,eAAuB,EACvB,cAAc,GAAG,CAAC;IAElB,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CACrB,CAAC,4BAA4B,GAAG,cAAc,CAAC,GAAG,eAAe,CAClE,CAAC;IACF,IACE,CAAC,MAAM,CAAC,SAAS,CAAC,eAAe,CAAC;QAClC,eAAe,GAAG,CAAC;QACnB,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC;QACjC,cAAc,GAAG,CAAC;QAClB,IAAI,GAAG,CAAC,EACR,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;;GAGG;AACH,MAAM,UAAU,qBAAqB,CAAC,KAAkB;IACtD,OAAO,sBAAsB,CAAC,MAAM,CAAC,IAAI,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC;AAC5E,CAAC;AAED,6EAA6E;AAC7E,+EAA+E;AAC/E,0EAA0E;AAC1E,mBAAmB;AACnB,MAAM,CAAC,MAAM,2BAA2B,GAAG,EAAE,CAAC;AAE9C,MAAM,UAAU,kBAAkB,CAChC,MAAoB;IAEpB,MAAM,OAAO,GAAU,EAAE,CAAC;IAC1B,KACE,IAAI,MAAM,GAAG,CAAC,EACd,MAAM,GAAG,MAAM,CAAC,MAAM,EACtB,MAAM,IAAI,2BAA2B,EACrC,CAAC;QACD,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,GAAG,2BAA2B,CAAC,CAAC,CAAC;IAC3E,CAAC;IACD,OAAO,OAAO,CAAC;AACjB,CAAC","sourcesContent":["import { getTableColumns } from \"drizzle-orm\";\nimport type { SQLiteTable } from \"drizzle-orm/sqlite-core\";\n\n/**\n * SQLite's historical default `SQLITE_MAX_VARIABLE_NUMBER`. Some hosts raise\n * it, but the SDK runs on several SQLite builds, so every statement it batches\n * stays within this budget.\n */\nexport const SQLITE_BOUND_VARIABLE_BUDGET = 999;\n\n/**\n * Rows (or list entries) one statement may carry when each binds\n * `variablesPerRow` values on top of `fixedVariables` shared ones.\n */\nexport function sqliteRowsPerStatement(\n variablesPerRow: number,\n fixedVariables = 0,\n): number {\n const rows = Math.floor(\n (SQLITE_BOUND_VARIABLE_BUDGET - fixedVariables) / variablesPerRow,\n );\n if (\n !Number.isInteger(variablesPerRow) ||\n variablesPerRow < 1 ||\n !Number.isInteger(fixedVariables) ||\n fixedVariables < 0 ||\n rows < 1\n ) {\n throw new Error(\"SQLite statement cannot fit its bound variables\");\n }\n return rows;\n}\n\n/**\n * Rows per multi-row INSERT into `table`. Every column may bind one value,\n * including explicit nulls, so the column count is the per-row ceiling.\n */\nexport function sqliteInsertBatchSize(table: SQLiteTable): number {\n return sqliteRowsPerStatement(Object.keys(getTableColumns(table)).length);\n}\n\n// Keep bulk inserts, IN predicates, and expanded pair predicates comfortably\n// below SQLite's common 999-bind and expression-depth limits. Some queued rows\n// consume more than ten binds each, so one conservative size covers every\n// reset statement.\nexport const REMOTE_RESET_SQL_BATCH_SIZE = 50;\n\nexport function remoteResetBatches<T>(\n values: readonly T[],\n): readonly (readonly T[])[] {\n const batches: T[][] = [];\n for (\n let offset = 0;\n offset < values.length;\n offset += REMOTE_RESET_SQL_BATCH_SIZE\n ) {\n batches.push(values.slice(offset, offset + REMOTE_RESET_SQL_BATCH_SIZE));\n }\n return batches;\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tearleads/client-sdk",
3
- "version": "0.1.82",
3
+ "version": "0.1.84",
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": {
@@ -1 +1 @@
1
- {"version":3,"file":"createIntentSeed.js","sourceRoot":"","sources":["createIntentSeed.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,EAAE,2BAA2B,EAAE,MAAM,qCAAqC,CAAC;AAClF,OAAO,EACL,2CAA2C,EAC3C,iDAAiD,GAClD,MAAM,0CA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,EACnB,MAAwB;IAExB,MAAM,UAAU,GAAG,kCAAkC,CAAC,WAAW,CAAC,CAAC;IACnE,kBAAkB,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;IACpC,IAAI,CAAC;QACH,OAAO,MAAM,MAAM,EAAE,CAAC;IACxB,CAAC;YAAS,CAAC;QACT,kBAAkB,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;IACzC,CAAC;AACH,CAAC","sourcesContent":["import { encodeVersionVector } from \"@tearleads/loro\";\nimport type { PendingUpdateRecord } from \"../../../../data/sqlite/documentPersistence\";\nimport { documentCreateSeedCandidate } from \"../../../documents/sync/initialSync\";\nimport {\n discardUnacceptedContainerMetadataUpdateIds,\n preRegisterMaterializedContainerMetadataUpdateIds,\n} from \"../../metadata/metadataSyncRegistrations\";\nimport type { ContainerState } from \"../../remoteHydration\";\nimport type { createRemoteContainer } from \"../remote\";\nimport type { ContainerCreateIntentSyncState } from \"../types\";\nimport { plannedContainerMetadataDocumentId } from \"./createWithMetadataPlan\";\n\n/**\n * The queued first metadata update, when it holds the whole document, commits\n * with the create instead of a following metadata sync. A seed only saves that\n * sync, so a queue that cannot be read means no seed rather than a failed pass.\n */\nexport async function loadMetadataSeedUpdate(\n state: ContainerCreateIntentSyncState,\n containerState: ContainerState,\n) {\n try {\n return documentCreateSeedCandidate({\n documentVersion: encodeVersionVector(containerState.doc),\n pendingUpdates: await state.persistence.listPendingUpdates(\n state.runtime.infra.execSql,\n containerState.container.id,\n ),\n });\n } catch {\n return null;\n }\n}\n\n/**\n * Runs a seeded create with the seed's id registered as self-authored first,\n * as a metadata sync registers the ids it sends, so the create's own update\n * echo cannot arm the metadata sync the seed replaces. An id the create did\n * not commit is discarded.\n */\nexport async function withRegisteredMetadataSeed(\n locallyAcceptedUpdateIds: Set<string> | undefined,\n seed: PendingUpdateRecord | null,\n create: () => ReturnType<typeof createRemoteContainer>,\n): ReturnType<typeof createRemoteContainer> {\n const registered: string[] = [];\n if (seed) {\n preRegisterMaterializedContainerMetadataUpdateIds(\n locallyAcceptedUpdateIds,\n registered,\n [seed.id],\n );\n }\n let accepted: string[] = [];\n try {\n const created = await create();\n if (created && typeof created !== \"symbol\" && created.metadataSeed) {\n accepted = [created.metadataSeed.updateId];\n }\n return created;\n } finally {\n discardUnacceptedContainerMetadataUpdateIds(\n locallyAcceptedUpdateIds,\n registered,\n accepted,\n );\n }\n}\n\n/**\n * Runs a folder's create with its planned metadata document id registered, so\n * a peer's metadata hint that arrives before the response installs the id\n * still queues the folder's first read (see\n * `listContainerMetadataDocumentUpdateIds`).\n */\nexport async function withPlannedMetadataDocumentId<T>(\n plannedDocumentIds: Set<string> | undefined,\n containerId: string,\n create: () => Promise<T>,\n): Promise<T> {\n const documentId = plannedContainerMetadataDocumentId(containerId);\n plannedDocumentIds?.add(documentId);\n try {\n return await create();\n } finally {\n plannedDocumentIds?.delete(documentId);\n }\n}\n"]}
1
+ {"version":3,"file":"createIntentSeed.js","sourceRoot":"","sources":["createIntentSeed.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,OAAO,EAAE,2BAA2B,EAAE,MAAM,qCAAqC,CAAC;AAClF,OAAO,EACL,2CAA2C,EAC3C,iDAAiD,GAClD,MAAM,0CAA0C,CAAC;AAIlD,OAAO,EAAE,kCAAkC,EAAE,MAAM,0BAA0B,CAAC;AAE9E;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,sBAAsB,CAC1C,KAAqC,EACrC,cAA8B;IAE9B,IAAI,CAAC;QACH,OAAO,2BAA2B,CAAC;YACjC,eAAe,EAAE,mBAAmB,CAAC,cAAc,CAAC,GAAG,CAAC;YACxD,cAAc,EAAE,MAAM,KAAK,CAAC,WAAW,CAAC,kBAAkB,CACxD,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,EAC3B,cAAc,CAAC,SAAS,CAAC,EAAE,CAC5B;SACF,CAAC,CAAC;IACL,CAAC;IAAC,MAAM,CAAC;QACP,OAAO,IAAI,CAAC;IACd,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,0BAA0B,CAC9C,wBAAiD,EACjD,IAAgC,EAChC,MAAsD;IAEtD,MAAM,UAAU,GAAa,EAAE,CAAC;IAChC,IAAI,IAAI,EAAE,CAAC;QACT,iDAAiD,CAC/C,wBAAwB,EACxB,UAAU,EACV,CAAC,IAAI,CAAC,EAAE,CAAC,CACV,CAAC;IACJ,CAAC;IACD,IAAI,QAAQ,GAAa,EAAE,CAAC;IAC5B,IAAI,CAAC;QACH,MAAM,OAAO,GAAG,MAAM,MAAM,EAAE,CAAC;QAC/B,IAAI,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,IAAI,OAAO,CAAC,YAAY,EAAE,CAAC;YACnE,QAAQ,GAAG,CAAC,OAAO,CAAC,YAAY,CAAC,QAAQ,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,OAAO,CAAC;IACjB,CAAC;YAAS,CAAC;QACT,2CAA2C,CACzC,wBAAwB,EACxB,UAAU,EACV,QAAQ,CACT,CAAC;IACJ,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,6BAA6B,CACjD,kBAA2C,EAC3C,WAAmB,EACnB,MAAwB;IAExB,MAAM,UAAU,GAAG,kCAAkC,CAAC,WAAW,CAAC,CAAC;IACnE,kBAAkB,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;IACpC,IAAI,CAAC;QACH,OAAO,MAAM,MAAM,EAAE,CAAC;IACxB,CAAC;YAAS,CAAC;QACT,kBAAkB,EAAE,MAAM,CAAC,UAAU,CAAC,CAAC;IACzC,CAAC;AACH,CAAC","sourcesContent":["import { encodeVersionVector } from \"@tearleads/loro\";\nimport type { PendingUpdateRecord } from \"../../../../data/persistence/documents/records\";\nimport { documentCreateSeedCandidate } from \"../../../documents/sync/initialSync\";\nimport {\n discardUnacceptedContainerMetadataUpdateIds,\n preRegisterMaterializedContainerMetadataUpdateIds,\n} from \"../../metadata/metadataSyncRegistrations\";\nimport type { ContainerState } from \"../../remoteHydration\";\nimport type { createRemoteContainer } from \"../remote\";\nimport type { ContainerCreateIntentSyncState } from \"../types\";\nimport { plannedContainerMetadataDocumentId } from \"./createWithMetadataPlan\";\n\n/**\n * The queued first metadata update, when it holds the whole document, commits\n * with the create instead of a following metadata sync. A seed only saves that\n * sync, so a queue that cannot be read means no seed rather than a failed pass.\n */\nexport async function loadMetadataSeedUpdate(\n state: ContainerCreateIntentSyncState,\n containerState: ContainerState,\n) {\n try {\n return documentCreateSeedCandidate({\n documentVersion: encodeVersionVector(containerState.doc),\n pendingUpdates: await state.persistence.listPendingUpdates(\n state.runtime.infra.execSql,\n containerState.container.id,\n ),\n });\n } catch {\n return null;\n }\n}\n\n/**\n * Runs a seeded create with the seed's id registered as self-authored first,\n * as a metadata sync registers the ids it sends, so the create's own update\n * echo cannot arm the metadata sync the seed replaces. An id the create did\n * not commit is discarded.\n */\nexport async function withRegisteredMetadataSeed(\n locallyAcceptedUpdateIds: Set<string> | undefined,\n seed: PendingUpdateRecord | null,\n create: () => ReturnType<typeof createRemoteContainer>,\n): ReturnType<typeof createRemoteContainer> {\n const registered: string[] = [];\n if (seed) {\n preRegisterMaterializedContainerMetadataUpdateIds(\n locallyAcceptedUpdateIds,\n registered,\n [seed.id],\n );\n }\n let accepted: string[] = [];\n try {\n const created = await create();\n if (created && typeof created !== \"symbol\" && created.metadataSeed) {\n accepted = [created.metadataSeed.updateId];\n }\n return created;\n } finally {\n discardUnacceptedContainerMetadataUpdateIds(\n locallyAcceptedUpdateIds,\n registered,\n accepted,\n );\n }\n}\n\n/**\n * Runs a folder's create with its planned metadata document id registered, so\n * a peer's metadata hint that arrives before the response installs the id\n * still queues the folder's first read (see\n * `listContainerMetadataDocumentUpdateIds`).\n */\nexport async function withPlannedMetadataDocumentId<T>(\n plannedDocumentIds: Set<string> | undefined,\n containerId: string,\n create: () => Promise<T>,\n): Promise<T> {\n const documentId = plannedContainerMetadataDocumentId(containerId);\n plannedDocumentIds?.add(documentId);\n try {\n return await create();\n } finally {\n plannedDocumentIds?.delete(documentId);\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"createWithMetadataPlan.js","sourceRoot":"","sources":["createWithMetadataPlan.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oCAAoC,EAAE,MAAM,qEAAqE,CAAC;AAE3H,OAAO,EACL,oCAAoC,EACpC,4CAA4C,GAC7C,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,mCAAmC,GAEpC,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,kCAAkC,EAAE,MAAM,yCAAyC,CAAC;AAsB7F,mFAAmF;AACnF,MAAM,UAAU,kCAAkC,CAChD,WAAmB;IAEnB,OAAO,WAAW,CAAC;AACrB,CAAC;AAED,KAAK,UAAU,UAAU,CAAC,KAAqC;IAC7D,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;IAC5C,8EAA8E;IAC9E,4EAA4E;IAC5E,MAAM,aAAa,GAAG,MAAM,oCAAoC,CAAC;QAC/D,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,WAAW,EAAE,KAAK,CAAC,WAAW;QAC9B,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,OAAO;QACP,OAAO,EAAE,KAAK,CAAC,gBAAgB;QAC/B,kBAAkB,EAAE,kCAAkC,CAAC,KAAK,CAAC,WAAW,CAAC;QACzE,UAAU,EAAE,KAAK,CAAC,UAAU;QAC5B,gBAAgB,EAAE,KAAK,CAAC,gBAAgB;QACxC,eAAe,EAAE,KAAK,CAAC,eAAe;QACtC,wBAAwB,EAAE,KAAK,CAAC,wBAAwB;QACxD,QAAQ,EAAE,KAAK,CAAC,iBAAiB;QACjC,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,+BAA+B,EAAE,kCAAkC,CACjE,KAAK,CAAC,OAAO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B,CAAC,QAAQ,CAAC,CAAC;IACnD,MAAM,gBAAgB,GACpB,MAAM,SAAS,CAAC,4BAA4B,CAAC,QAAQ,CAAC,CAAC;IACzD,IAAI,CAAC,gBAAgB,IAAI,KAAK,CAAC,YAAY,EAAE,EAAE,KAAK,KAAK;QAAE,OAAO,IAAI,CAAC;IACvE,IACE,gBAAgB,CAAC,WAAW,KAAK,QAAQ;QACzC,gBAAgB,CAAC,cAAc,KAAK,KAAK,CAAC,gBAAgB,CAAC,cAAc,EACzE,CAAC;QACD,MAAM,IAAI,KAAK,CACb,0EAA0E,CAC3E,CAAC;IACJ,CAAC;IACD,OAAO,UAAU,CAAC,EAAE,GAAG,KAAK,EAAE,gBAAgB,EAAE,CAAC,CAAC;AACpD,CAAC","sourcesContent":["import type { ContainerSystemSlot } from \"@tearleads/validators/containerSystemSlot\";\nimport type { ContainerWriterProjectionResponse } from \"@tearleads/validators/response\";\nimport type { ProjectionUserKeyResolver } from \"../../../../data/keyingProjectionVerification\";\nimport { ProjectionDependencyUnavailableError } from \"../../../../data/keyingProjectionVerification/dependencyUnavailable\";\nimport type { PendingUpdateRecord } from \"../../../../data/sqlite/documentPersistence\";\nimport {\n buildMaterializedContainerCreatePlan,\n childContainerWriterProjectionFromCreatePlan,\n} from \"../../../containers\";\nimport {\n buildMaterializedDocumentCreatePlan,\n type resolveDocumentCreateAuthor,\n} from \"../../../documents\";\nimport { createRuntimePrincipalPolicyWarmer } from \"../../../principals/runtimePolicyWarmer\";\nimport type { ContainerWorkflowRuntime } from \"../types\";\n\nexport interface ContainerWithMetadataPlanInput {\n readonly author: NonNullable<ReturnType<typeof resolveDocumentCreateAuthor>>;\n readonly containerEventId: string;\n readonly containerId: string;\n readonly containerKey: Uint8Array;\n readonly containerSignedAt: string;\n readonly metadataContentKey: Uint8Array;\n readonly metadataEventId: string;\n /** The queued update holding the metadata document's whole content. */\n readonly metadataSeedUpdate?: PendingUpdateRecord | undefined;\n readonly metadataSignedAt: string;\n readonly parentProjection: ContainerWriterProjectionResponse;\n readonly parentSecretKey: Uint8Array;\n readonly resolveProjectionUserKey: ProjectionUserKeyResolver;\n readonly runtime: ContainerWorkflowRuntime;\n readonly systemSlot?: ContainerSystemSlot | null | undefined;\n readonly stillCurrent?: (() => boolean) | undefined;\n}\n\n/** A folder's metadata document takes the folder's id, known before the create. */\nexport function plannedContainerMetadataDocumentId(\n containerId: string,\n): string {\n return containerId;\n}\n\nasync function buildPlans(input: ContainerWithMetadataPlanInput) {\n const execSql = input.runtime.infra.execSql;\n // Rebuild parent- and policy-dependent hashes, signatures, and wraps on every\n // attempt while retaining the logical mutation identities and key material.\n const containerPlan = await buildMaterializedContainerCreatePlan({\n author: input.author,\n containerId: input.containerId,\n containerKey: input.containerKey,\n execSql,\n eventId: input.containerEventId,\n metadataDocumentId: plannedContainerMetadataDocumentId(input.containerId),\n systemSlot: input.systemSlot,\n parentProjection: input.parentProjection,\n parentSecretKey: input.parentSecretKey,\n resolveProjectionUserKey: input.resolveProjectionUserKey,\n signedAt: input.containerSignedAt,\n stillCurrent: input.stillCurrent,\n warmReferencedPrincipalPolicies: createRuntimePrincipalPolicyWarmer(\n input.runtime,\n ),\n });\n const childProjection = childContainerWriterProjectionFromCreatePlan({\n materializedPlan: containerPlan,\n parentProjection: input.parentProjection,\n });\n const metadataDocumentPlan = await buildMaterializedDocumentCreatePlan({\n author: input.author,\n containerProjection: childProjection,\n contentKey: input.metadataContentKey,\n documentId: containerPlan.plan.metadataDocumentId,\n eventId: input.metadataEventId,\n execSql,\n knownContainerKeks: new Map([\n [containerPlan.plan.containerKeyEpochId, containerPlan.containerKey],\n ]),\n signedAt: input.metadataSignedAt,\n stillCurrent: input.stillCurrent,\n targetSecretKey: input.parentSecretKey,\n trustedLocalProjection: true,\n });\n return { containerPlan, childProjection, metadataDocumentPlan };\n}\n\n/** Refresh stale history evidence once, strictly before submitting the write. */\nexport async function buildContainerWithMetadataPlans(\n input: ContainerWithMetadataPlanInput,\n) {\n try {\n return await buildPlans(input);\n } catch (error) {\n if (!(error instanceof ProjectionDependencyUnavailableError)) throw error;\n }\n if (input.stillCurrent?.() === false) return null;\n const { apiClient } = input.runtime;\n const parentId = input.parentProjection.containerId;\n apiClient.evictContainerWriterProjection(parentId);\n const parentProjection =\n await apiClient.getContainerWriterProjection(parentId);\n if (!parentProjection || input.stillCurrent?.() === false) return null;\n if (\n parentProjection.containerId !== parentId ||\n parentProjection.organizationId !== input.parentProjection.organizationId\n ) {\n throw new Error(\n \"Refreshed parent projection belongs to another container or organization\",\n );\n }\n return buildPlans({ ...input, parentProjection });\n}\n"]}
1
+ {"version":3,"file":"createWithMetadataPlan.js","sourceRoot":"","sources":["createWithMetadataPlan.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,oCAAoC,EAAE,MAAM,qEAAqE,CAAC;AAE3H,OAAO,EACL,oCAAoC,EACpC,4CAA4C,GAC7C,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EACL,mCAAmC,GAEpC,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EAAE,kCAAkC,EAAE,MAAM,yCAAyC,CAAC;AAsB7F,mFAAmF;AACnF,MAAM,UAAU,kCAAkC,CAChD,WAAmB;IAEnB,OAAO,WAAW,CAAC;AACrB,CAAC;AAED,KAAK,UAAU,UAAU,CAAC,KAAqC;IAC7D,MAAM,OAAO,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC;IAC5C,8EAA8E;IAC9E,4EAA4E;IAC5E,MAAM,aAAa,GAAG,MAAM,oCAAoC,CAAC;QAC/D,MAAM,EAAE,KAAK,CAAC,MAAM;QACpB,WAAW,EAAE,KAAK,CAAC,WAAW;QAC9B,YAAY,EAAE,KAAK,CAAC,YAAY;QAChC,OAAO;QACP,OAAO,EAAE,KAAK,CAAC,gBAAgB;QAC/B,kBAAkB,EAAE,kCAAkC,CAAC,KAAK,CAAC,WAAW,CAAC;QACzE,UAAU,EAAE,KAAK,CAAC,UAAU;QAC5B,gBAAgB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,CAAC;IACD,IAAI,KAAK,CAAC,YAAY,EAAE,EAAE,KAAK,KAAK;QAAE,OAAO,IAAI,CAAC;IAClD,MAAM,EAAE,SAAS,EAAE,GAAG,KAAK,CAAC,OAAO,CAAC;IACpC,MAAM,QAAQ,GAAG,KAAK,CAAC,gBAAgB,CAAC,WAAW,CAAC;IACpD,SAAS,CAAC,8BAA8B,CAAC,QAAQ,CAAC,CAAC;IACnD,MAAM,gBAAgB,GACpB,MAAM,SAAS,CAAC,4BAA4B,CAAC,QAAQ,CAAC,CAAC;IACzD,IAAI,CAAC,gBAAgB,IAAI,KAAK,CAAC,YAAY,EAAE,EAAE,KAAK,KAAK;QAAE,OAAO,IAAI,CAAC;IACvE,IACE,gBAAgB,CAAC,WAAW,KAAK,QAAQ;QACzC,gBAAgB,CAAC,cAAc,KAAK,KAAK,CAAC,gBAAgB,CAAC,cAAc,EACzE,CAAC;QACD,MAAM,IAAI,KAAK,CACb,0EAA0E,CAC3E,CAAC;IACJ,CAAC;IACD,OAAO,UAAU,CAAC,EAAE,GAAG,KAAK,EAAE,gBAAgB,EAAE,CAAC,CAAC;AACpD,CAAC","sourcesContent":["import type { ContainerSystemSlot } from \"@tearleads/validators/containerSystemSlot\";\nimport type { ContainerWriterProjectionResponse } from \"@tearleads/validators/response\";\nimport type { ProjectionUserKeyResolver } from \"../../../../data/keyingProjectionVerification\";\nimport { ProjectionDependencyUnavailableError } from \"../../../../data/keyingProjectionVerification/dependencyUnavailable\";\nimport type { PendingUpdateRecord } from \"../../../../data/persistence/documents/records\";\nimport {\n buildMaterializedContainerCreatePlan,\n childContainerWriterProjectionFromCreatePlan,\n} from \"../../../containers\";\nimport {\n buildMaterializedDocumentCreatePlan,\n type resolveDocumentCreateAuthor,\n} from \"../../../documents\";\nimport { createRuntimePrincipalPolicyWarmer } from \"../../../principals/runtimePolicyWarmer\";\nimport type { ContainerWorkflowRuntime } from \"../types\";\n\nexport interface ContainerWithMetadataPlanInput {\n readonly author: NonNullable<ReturnType<typeof resolveDocumentCreateAuthor>>;\n readonly containerEventId: string;\n readonly containerId: string;\n readonly containerKey: Uint8Array;\n readonly containerSignedAt: string;\n readonly metadataContentKey: Uint8Array;\n readonly metadataEventId: string;\n /** The queued update holding the metadata document's whole content. */\n readonly metadataSeedUpdate?: PendingUpdateRecord | undefined;\n readonly metadataSignedAt: string;\n readonly parentProjection: ContainerWriterProjectionResponse;\n readonly parentSecretKey: Uint8Array;\n readonly resolveProjectionUserKey: ProjectionUserKeyResolver;\n readonly runtime: ContainerWorkflowRuntime;\n readonly systemSlot?: ContainerSystemSlot | null | undefined;\n readonly stillCurrent?: (() => boolean) | undefined;\n}\n\n/** A folder's metadata document takes the folder's id, known before the create. */\nexport function plannedContainerMetadataDocumentId(\n containerId: string,\n): string {\n return containerId;\n}\n\nasync function buildPlans(input: ContainerWithMetadataPlanInput) {\n const execSql = input.runtime.infra.execSql;\n // Rebuild parent- and policy-dependent hashes, signatures, and wraps on every\n // attempt while retaining the logical mutation identities and key material.\n const containerPlan = await buildMaterializedContainerCreatePlan({\n author: input.author,\n containerId: input.containerId,\n containerKey: input.containerKey,\n execSql,\n eventId: input.containerEventId,\n metadataDocumentId: plannedContainerMetadataDocumentId(input.containerId),\n systemSlot: input.systemSlot,\n parentProjection: input.parentProjection,\n parentSecretKey: input.parentSecretKey,\n resolveProjectionUserKey: input.resolveProjectionUserKey,\n signedAt: input.containerSignedAt,\n stillCurrent: input.stillCurrent,\n warmReferencedPrincipalPolicies: createRuntimePrincipalPolicyWarmer(\n input.runtime,\n ),\n });\n const childProjection = childContainerWriterProjectionFromCreatePlan({\n materializedPlan: containerPlan,\n parentProjection: input.parentProjection,\n });\n const metadataDocumentPlan = await buildMaterializedDocumentCreatePlan({\n author: input.author,\n containerProjection: childProjection,\n contentKey: input.metadataContentKey,\n documentId: containerPlan.plan.metadataDocumentId,\n eventId: input.metadataEventId,\n execSql,\n knownContainerKeks: new Map([\n [containerPlan.plan.containerKeyEpochId, containerPlan.containerKey],\n ]),\n signedAt: input.metadataSignedAt,\n stillCurrent: input.stillCurrent,\n targetSecretKey: input.parentSecretKey,\n trustedLocalProjection: true,\n });\n return { containerPlan, childProjection, metadataDocumentPlan };\n}\n\n/** Refresh stale history evidence once, strictly before submitting the write. */\nexport async function buildContainerWithMetadataPlans(\n input: ContainerWithMetadataPlanInput,\n) {\n try {\n return await buildPlans(input);\n } catch (error) {\n if (!(error instanceof ProjectionDependencyUnavailableError)) throw error;\n }\n if (input.stillCurrent?.() === false) return null;\n const { apiClient } = input.runtime;\n const parentId = input.parentProjection.containerId;\n apiClient.evictContainerWriterProjection(parentId);\n const parentProjection =\n await apiClient.getContainerWriterProjection(parentId);\n if (!parentProjection || input.stillCurrent?.() === false) return null;\n if (\n parentProjection.containerId !== parentId ||\n parentProjection.organizationId !== input.parentProjection.organizationId\n ) {\n throw new Error(\n \"Refreshed parent projection belongs to another container or organization\",\n );\n }\n return buildPlans({ ...input, parentProjection });\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["types.ts"],"names":[],"mappings":"","sourcesContent":["import type { ContainerSystemSlot } from \"@tearleads/validators/containerSystemSlot\";\nimport type {\n ContainerDeleteResponse,\n ContainerWriterProjectionResponse,\n ReferencedPrincipalStateResponse,\n} from \"@tearleads/validators/response\";\nimport type { ProjectionUserKeyResolver } from \"../../../data/keyingProjectionVerification\";\nimport type { DocumentRecord } from \"../../../data/sqlite/documentPersistence\";\nimport type {\n createRemoteContainer,\n moveRemoteContainer,\n shareRemoteContainer,\n} from \"../../containers\";\nimport type { createRemoteDocument } from \"../../documents\";\nimport type { shareRemoteContainerWithGroup } from \"../../organizations/groupShare\";\nimport type { CurrentPrincipalPolicyReader } from \"../../principals/currentPrincipalPolicyReader\";\nimport type { PrincipalPolicyHistoryReader } from \"../../principals/principalPolicyHistoryReader\";\nimport type {\n ContainerContentsPersistence,\n ContainerCreateIntentRecord,\n ContainerMoveIntentRecord,\n} from \"../containerPersistence\";\nimport type { createInitializedContainerMetadataDocument } from \"../metadata/containerMetadataDocument\";\nimport type {\n ContainerState,\n RemoteContainer as HydratedRemoteContainer,\n RemoteContainerHydrationHost,\n} from \"../remoteHydration\";\nimport type { ContainerContentsWorkflowRuntime } from \"../runtime\";\n\nexport type ContainerWorkflowApi = Parameters<\n typeof createRemoteContainer\n>[0][\"apiClient\"] &\n Parameters<typeof shareRemoteContainer>[0][\"apiClient\"] &\n Parameters<typeof shareRemoteContainerWithGroup>[0][\"apiClient\"] &\n Parameters<typeof moveRemoteContainer>[0][\"apiClient\"] &\n Parameters<typeof createRemoteDocument>[0][\"apiClient\"] &\n Partial<PrincipalPolicyHistoryReader> &\n CurrentPrincipalPolicyReader & {\n deleteContainerResult(\n containerId: string,\n options?: {\n expectedPaymentRequiredOrganizationId?: string;\n reportErrors?: boolean;\n requiredAncestorId?: string | undefined;\n },\n ): Promise<\n | { data: ContainerDeleteResponse; ok: true }\n | {\n ok: false;\n report: () => void;\n status: number | null;\n }\n >;\n primeContainerWriterProjection(\n containerId: string,\n projection: ContainerWriterProjectionResponse,\n ): void;\n };\n\nexport interface ContainerWorkflowRuntime\n extends Pick<\n ContainerContentsWorkflowRuntime,\n | \"auth\"\n | \"crypto\"\n | \"infra\"\n | \"resolveTrustedUserIdentity\"\n | \"state\"\n | \"util\"\n > {\n readonly apiClient: ContainerWorkflowApi;\n}\n\nexport interface CreatedRemoteContainerState {\n accessManifestHash: string;\n /**\n * The metadata seed the create committed, settled by the caller instead of\n * a first metadata sync. Absent when the create carried no seed or fell back\n * to a plain create.\n */\n metadataSeed?:\n | { readonly commitLsn: string | null; readonly updateId: string }\n | undefined;\n /** The slot this client signed into the create; never the server's echo. */\n systemSlot: ContainerSystemSlot | null;\n containerId: string;\n createdAt: string;\n metadataDocumentId: string;\n organizationId: string;\n /** A created container is a child; the acknowledgement echoed this parent. */\n parentId: string;\n persistedMetadataState: Pick<\n DocumentRecord,\n | \"documentId\"\n | \"contentKeyBundle\"\n | \"documentKekTargets\"\n | \"documentManifestBundle\"\n >;\n updatedAt: string;\n}\n\nexport interface SharedRemoteContainerState {\n accessEpoch: number;\n accessManifestHash: string;\n createdAt: string;\n metadataDocumentId: string;\n referencedPrincipalHeads: ReferencedPrincipalStateResponse[];\n updatedAt: string;\n writerProjection: ContainerWriterProjectionResponse | null;\n}\n\nexport interface SharedContainerState {\n container: ContainerState[\"container\"];\n record: ContainerState[\"record\"];\n}\n\nexport type SharedContainerStateResult =\n | { status: \"missing\" }\n | { status: \"confirmed\" }\n | (SharedContainerState & {\n status: \"identity-superseded\" | \"persisted\";\n });\n\nexport type RemoteContainer = HydratedRemoteContainer;\n\nexport type ContainerMetadataDocumentState = Awaited<\n ReturnType<typeof createInitializedContainerMetadataDocument>\n>[\"doc\"];\n\nexport interface CreatedChildContainerState {\n containerState: ContainerState;\n initialUpdate: Uint8Array;\n shouldRequestSync: boolean;\n}\n\nexport interface ContainerIntentSyncState {\n containersById: ReadonlyMap<string, ContainerState>;\n /**\n * Metadata update ids this device sent, so their own update echoes are\n * classified as self-authored (shared with the metadata sync).\n */\n locallyAcceptedMetadataUpdateIds?: Set<string> | undefined;\n /** Metadata document ids of folder creates in flight (shared with hints). */\n plannedMetadataDocumentIds?: Set<string> | undefined;\n persistence: ContainerContentsPersistence;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n /**\n * Verifies that a listed container is this device's pending create before it\n * is adopted and returns where it sits; `assertContainerCreateAdoptable`\n * unless a test replaces it.\n */\n verifyCreateAdoption?: (input: {\n readonly containerId: string;\n readonly expectedOrganizationId: string;\n }) => Promise<AdoptedContainerPlacement>;\n}\n\n/** Where a verified listed create committed, and where its container is now. */\nexport interface AdoptedContainerPlacement {\n /** The parent its signed epoch-1 `container.create` named. */\n readonly createdParentId: string;\n /** The verified head's parent; another writer may have moved it since. */\n readonly currentParentId: string;\n}\n\nexport type ContainerCreateIntentSyncState = ContainerIntentSyncState;\n\nexport type ContainerCreateIntentSyncHost = Pick<\n RemoteContainerHydrationHost,\n \"persistContainerState\"\n>;\n\nexport interface ContainerCreateIntentSyncInput {\n host: ContainerCreateIntentSyncHost;\n /** A parent whose own create has not settled takes no child create. */\n isCreatePending: (containerId: string) => boolean;\n isCurrent: () => boolean;\n isRemoteSyncBlocked: (organizationId: string) => boolean;\n intent: ContainerCreateIntentRecord;\n requestRemoteReconciliation: (parentContainerId: string | null) => void;\n state: ContainerCreateIntentSyncState;\n}\n\nexport type ContainerMoveIntentSyncState = ContainerIntentSyncState;\n\nexport type ContainerMoveIntentSyncHost = Pick<\n RemoteContainerHydrationHost,\n \"persistContainerState\" | \"updateSnapshot\"\n>;\n\n/** Later passes for moves the server accepted but no pass settled. */\nexport interface AcceptedMoveRetry {\n /** The server holds the move: schedule a replay to settle it. */\n arm: (intentId: string) => void;\n /** A replay failed; continue an accepted move's retries, if it has some. */\n resume: (intentId: string) => void;\n}\n\nexport interface ContainerMoveIntentSyncInput {\n host: ContainerMoveIntentSyncHost;\n isCurrent: () => boolean;\n /** A folder whose create has not settled takes no structural writes. */\n isCreatePending: (containerId: string) => boolean;\n isRemoteSyncBlocked: (organizationId: string) => boolean;\n intent: ContainerMoveIntentRecord;\n requestRemoteReconciliation: (parentContainerId: string | null) => void;\n acceptedMoveRetry: AcceptedMoveRetry;\n state: ContainerMoveIntentSyncState;\n}\n"]}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["types.ts"],"names":[],"mappings":"","sourcesContent":["import type { ContainerSystemSlot } from \"@tearleads/validators/containerSystemSlot\";\nimport type {\n ContainerDeleteResponse,\n ContainerWriterProjectionResponse,\n ReferencedPrincipalStateResponse,\n} from \"@tearleads/validators/response\";\nimport type { ProjectionUserKeyResolver } from \"../../../data/keyingProjectionVerification\";\nimport type { DocumentRecord } from \"../../../data/persistence/documents/records\";\nimport type {\n createRemoteContainer,\n moveRemoteContainer,\n shareRemoteContainer,\n} from \"../../containers\";\nimport type { createRemoteDocument } from \"../../documents\";\nimport type { shareRemoteContainerWithGroup } from \"../../organizations/groupShare\";\nimport type { CurrentPrincipalPolicyReader } from \"../../principals/currentPrincipalPolicyReader\";\nimport type { PrincipalPolicyHistoryReader } from \"../../principals/principalPolicyHistoryReader\";\nimport type {\n ContainerContentsPersistence,\n ContainerCreateIntentRecord,\n ContainerMoveIntentRecord,\n} from \"../containerPersistence\";\nimport type { createInitializedContainerMetadataDocument } from \"../metadata/containerMetadataDocument\";\nimport type {\n ContainerState,\n RemoteContainer as HydratedRemoteContainer,\n RemoteContainerHydrationHost,\n} from \"../remoteHydration\";\nimport type { ContainerContentsWorkflowRuntime } from \"../runtime\";\n\nexport type ContainerWorkflowApi = Parameters<\n typeof createRemoteContainer\n>[0][\"apiClient\"] &\n Parameters<typeof shareRemoteContainer>[0][\"apiClient\"] &\n Parameters<typeof shareRemoteContainerWithGroup>[0][\"apiClient\"] &\n Parameters<typeof moveRemoteContainer>[0][\"apiClient\"] &\n Parameters<typeof createRemoteDocument>[0][\"apiClient\"] &\n Partial<PrincipalPolicyHistoryReader> &\n CurrentPrincipalPolicyReader & {\n deleteContainerResult(\n containerId: string,\n options?: {\n expectedPaymentRequiredOrganizationId?: string;\n reportErrors?: boolean;\n requiredAncestorId?: string | undefined;\n },\n ): Promise<\n | { data: ContainerDeleteResponse; ok: true }\n | {\n ok: false;\n report: () => void;\n status: number | null;\n }\n >;\n primeContainerWriterProjection(\n containerId: string,\n projection: ContainerWriterProjectionResponse,\n ): void;\n };\n\nexport interface ContainerWorkflowRuntime\n extends Pick<\n ContainerContentsWorkflowRuntime,\n | \"auth\"\n | \"crypto\"\n | \"infra\"\n | \"resolveTrustedUserIdentity\"\n | \"state\"\n | \"util\"\n > {\n readonly apiClient: ContainerWorkflowApi;\n}\n\nexport interface CreatedRemoteContainerState {\n accessManifestHash: string;\n /**\n * The metadata seed the create committed, settled by the caller instead of\n * a first metadata sync. Absent when the create carried no seed or fell back\n * to a plain create.\n */\n metadataSeed?:\n | { readonly commitLsn: string | null; readonly updateId: string }\n | undefined;\n /** The slot this client signed into the create; never the server's echo. */\n systemSlot: ContainerSystemSlot | null;\n containerId: string;\n createdAt: string;\n metadataDocumentId: string;\n organizationId: string;\n /** A created container is a child; the acknowledgement echoed this parent. */\n parentId: string;\n persistedMetadataState: Pick<\n DocumentRecord,\n | \"documentId\"\n | \"contentKeyBundle\"\n | \"documentKekTargets\"\n | \"documentManifestBundle\"\n >;\n updatedAt: string;\n}\n\nexport interface SharedRemoteContainerState {\n accessEpoch: number;\n accessManifestHash: string;\n createdAt: string;\n metadataDocumentId: string;\n referencedPrincipalHeads: ReferencedPrincipalStateResponse[];\n updatedAt: string;\n writerProjection: ContainerWriterProjectionResponse | null;\n}\n\nexport interface SharedContainerState {\n container: ContainerState[\"container\"];\n record: ContainerState[\"record\"];\n}\n\nexport type SharedContainerStateResult =\n | { status: \"missing\" }\n | { status: \"confirmed\" }\n | (SharedContainerState & {\n status: \"identity-superseded\" | \"persisted\";\n });\n\nexport type RemoteContainer = HydratedRemoteContainer;\n\nexport type ContainerMetadataDocumentState = Awaited<\n ReturnType<typeof createInitializedContainerMetadataDocument>\n>[\"doc\"];\n\nexport interface CreatedChildContainerState {\n containerState: ContainerState;\n initialUpdate: Uint8Array;\n shouldRequestSync: boolean;\n}\n\nexport interface ContainerIntentSyncState {\n containersById: ReadonlyMap<string, ContainerState>;\n /**\n * Metadata update ids this device sent, so their own update echoes are\n * classified as self-authored (shared with the metadata sync).\n */\n locallyAcceptedMetadataUpdateIds?: Set<string> | undefined;\n /** Metadata document ids of folder creates in flight (shared with hints). */\n plannedMetadataDocumentIds?: Set<string> | undefined;\n persistence: ContainerContentsPersistence;\n resolveProjectionUserKey: ProjectionUserKeyResolver;\n runtime: ContainerWorkflowRuntime;\n /**\n * Verifies that a listed container is this device's pending create before it\n * is adopted and returns where it sits; `assertContainerCreateAdoptable`\n * unless a test replaces it.\n */\n verifyCreateAdoption?: (input: {\n readonly containerId: string;\n readonly expectedOrganizationId: string;\n }) => Promise<AdoptedContainerPlacement>;\n}\n\n/** Where a verified listed create committed, and where its container is now. */\nexport interface AdoptedContainerPlacement {\n /** The parent its signed epoch-1 `container.create` named. */\n readonly createdParentId: string;\n /** The verified head's parent; another writer may have moved it since. */\n readonly currentParentId: string;\n}\n\nexport type ContainerCreateIntentSyncState = ContainerIntentSyncState;\n\nexport type ContainerCreateIntentSyncHost = Pick<\n RemoteContainerHydrationHost,\n \"persistContainerState\"\n>;\n\nexport interface ContainerCreateIntentSyncInput {\n host: ContainerCreateIntentSyncHost;\n /** A parent whose own create has not settled takes no child create. */\n isCreatePending: (containerId: string) => boolean;\n isCurrent: () => boolean;\n isRemoteSyncBlocked: (organizationId: string) => boolean;\n intent: ContainerCreateIntentRecord;\n requestRemoteReconciliation: (parentContainerId: string | null) => void;\n state: ContainerCreateIntentSyncState;\n}\n\nexport type ContainerMoveIntentSyncState = ContainerIntentSyncState;\n\nexport type ContainerMoveIntentSyncHost = Pick<\n RemoteContainerHydrationHost,\n \"persistContainerState\" | \"updateSnapshot\"\n>;\n\n/** Later passes for moves the server accepted but no pass settled. */\nexport interface AcceptedMoveRetry {\n /** The server holds the move: schedule a replay to settle it. */\n arm: (intentId: string) => void;\n /** A replay failed; continue an accepted move's retries, if it has some. */\n resume: (intentId: string) => void;\n}\n\nexport interface ContainerMoveIntentSyncInput {\n host: ContainerMoveIntentSyncHost;\n isCurrent: () => boolean;\n /** A folder whose create has not settled takes no structural writes. */\n isCreatePending: (containerId: string) => boolean;\n isRemoteSyncBlocked: (organizationId: string) => boolean;\n intent: ContainerMoveIntentRecord;\n requestRemoteReconciliation: (parentContainerId: string | null) => void;\n acceptedMoveRetry: AcceptedMoveRetry;\n state: ContainerMoveIntentSyncState;\n}\n"]}