@tearleads/client-sdk 0.1.67 → 0.1.69

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. package/data/containers/shared/principalPolicies.js +8 -12
  2. package/data/containers/shared/principalPolicies.js.map +1 -1
  3. package/data/persistence/principalHistory/principalHistoryStagePersistence.d.ts +2 -0
  4. package/data/persistence/principalHistory/principalHistoryStagePersistence.js +4 -0
  5. package/data/persistence/principalHistory/principalHistoryStagePersistence.js.map +1 -1
  6. package/internal/validators/operation/attachments.d.ts +48 -8
  7. package/internal/validators/operation/auth.d.ts +456 -76
  8. package/internal/validators/operation/containerMutations.d.ts +264 -44
  9. package/internal/validators/operation/documentMutations.d.ts +72 -12
  10. package/internal/validators/operation/documentSync.d.ts +24 -4
  11. package/internal/validators/operation/organizations.d.ts +528 -88
  12. package/internal/validators/operation/principals.d.ts +96 -16
  13. package/internal/validators/request/blob.d.ts +48 -8
  14. package/internal/validators/request/container.d.ts +96 -16
  15. package/internal/validators/request/container.js +5 -2
  16. package/internal/validators/request/container.js.map +1 -1
  17. package/internal/validators/request/containerMetadata.d.ts +96 -16
  18. package/internal/validators/request/document.d.ts +60 -10
  19. package/internal/validators/request/documentSyncSchema.d.ts +24 -4
  20. package/internal/validators/request/documentSyncSchema.js +2 -1
  21. package/internal/validators/request/documentSyncSchema.js.map +1 -1
  22. package/internal/validators/request/index.d.ts +1 -0
  23. package/internal/validators/request/index.js +1 -0
  24. package/internal/validators/request/index.js.map +1 -1
  25. package/internal/validators/request/organization.d.ts +96 -16
  26. package/internal/validators/request/organizationProvisioning.d.ts +1456 -246
  27. package/internal/validators/request/principal.d.ts +120 -20
  28. package/internal/validators/request/principalPolicyRef.d.ts +42 -0
  29. package/internal/validators/request/principalPolicyRef.js +19 -0
  30. package/internal/validators/request/principalPolicyRef.js.map +1 -0
  31. package/internal/validators/request/registration.d.ts +456 -76
  32. package/internal/validators/util/containerLimits.d.ts +8 -0
  33. package/internal/validators/util/containerLimits.js +8 -0
  34. package/internal/validators/util/containerLimits.js.map +1 -1
  35. package/package.json +1 -1
  36. package/workflows/container-contents/storedContainerState.js +5 -1
  37. package/workflows/container-contents/storedContainerState.js.map +1 -1
  38. package/workflows/containers/child/mutationRequestCore.js +2 -2
  39. package/workflows/containers/child/mutationRequestCore.js.map +1 -1
  40. package/workflows/containers/child/recitePlan.js +2 -2
  41. package/workflows/containers/child/recitePlan.js.map +1 -1
  42. package/workflows/containers/root/create.js +17 -28
  43. package/workflows/containers/root/create.js.map +1 -1
  44. package/workflows/containers/root/initialGroupPolicy.js +0 -23
  45. package/workflows/containers/root/initialGroupPolicy.js.map +1 -1
  46. package/workflows/documents/syncFailures/syncPolicyRepair.js +2 -8
  47. package/workflows/documents/syncFailures/syncPolicyRepair.js.map +1 -1
  48. package/workflows/principals/recoveryOperation/supersededStageResume.js +21 -0
  49. package/workflows/principals/recoveryOperation/supersededStageResume.js.map +1 -0
  50. package/workflows/principals/runtimePolicyRecovery.js +12 -9
  51. package/workflows/principals/runtimePolicyRecovery.js.map +1 -1
  52. package/workflows/registration/provisioningBootstrapBuilders.d.ts +12 -2
@@ -1,19 +1,15 @@
1
- import { readCanonicalRecord } from "../../keying/canonicalJson.js";
2
- export function principalPolicyRequestRecord(policy, label = "Container principal policy") {
3
- return readCanonicalRecord({
1
+ /**
2
+ * The head a container mutation cites for a policy it was planned against.
3
+ * The API authorizes from its own verified copy of that head, so the policy
4
+ * itself never travels.
5
+ */
6
+ export function principalPolicyRef(policy) {
7
+ return {
4
8
  principalType: policy.principalType,
5
9
  principalId: policy.principalId,
6
10
  version: policy.version,
7
- keyEpoch: policy.keyEpoch,
8
11
  stateHash: policy.stateHash,
9
- state: {
10
- ...policy.state,
11
- stateHash: policy.stateHash,
12
- },
13
- projection: policy.projection,
14
- grants: policy.grants,
15
- checkpoint: policy.checkpoint,
16
- }, label);
12
+ };
17
13
  }
18
14
  function principalPolicyKey(policy) {
19
15
  return [
@@ -1 +1 @@
1
- {"version":3,"file":"principalPolicies.js","sourceRoot":"","sources":["principalPolicies.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AAMjE,MAAM,UAAU,4BAA4B,CAC1C,MAAyC,EACzC,KAAK,GAAG,4BAA4B;IAEpC,OAAO,mBAAmB,CACxB;QACE,aAAa,EAAE,MAAM,CAAC,aAAa;QACnC,WAAW,EAAE,MAAM,CAAC,WAAW;QAC/B,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,QAAQ,EAAE,MAAM,CAAC,QAAQ;QACzB,SAAS,EAAE,MAAM,CAAC,SAAS;QAC3B,KAAK,EAAE;YACL,GAAG,MAAM,CAAC,KAAK;YACf,SAAS,EAAE,MAAM,CAAC,SAAS;SAC5B;QACD,UAAU,EAAE,MAAM,CAAC,UAAU;QAC7B,MAAM,EAAE,MAAM,CAAC,MAAM;QACrB,UAAU,EAAE,MAAM,CAAC,UAAU;KAC9B,EACD,KAAK,CACN,CAAC;AACJ,CAAC;AAED,SAAS,kBAAkB,CACzB,MAGC;IAED,OAAO;QACL,MAAM,CAAC,aAAa;QACpB,MAAM,CAAC,WAAW;QAClB,MAAM,CAAC,OAAO;QACd,MAAM,CAAC,QAAQ;QACf,MAAM,CAAC,SAAS;KACjB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACd,CAAC;AAED,MAAM,UAAU,uBAAuB,CAErC,QAAsB;IACtB,MAAM,aAAa,GAAG,IAAI,GAAG,EAAa,CAAC;IAE3C,KAAK,MAAM,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC9B,aAAa,CAAC,GAAG,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;IACxD,CAAC;IAED,OAAO,CAAC,GAAG,aAAa,CAAC,MAAM,EAAE,CAAC,CAAC;AACrC,CAAC","sourcesContent":["import type { VerifiedPrincipalPolicy } from \"@tearleads/crypto\";\nimport { readCanonicalRecord } from \"../../keying/canonicalJson\";\nimport type {\n PrincipalPolicyCheckpointEvidence,\n PrincipalPolicyCurrentEvidence,\n} from \"../../principals/principalPolicyEvidence\";\n\nexport function principalPolicyRequestRecord(\n policy: PrincipalPolicyCheckpointEvidence,\n label = \"Container principal policy\",\n): Record<string, unknown> {\n return readCanonicalRecord(\n {\n principalType: policy.principalType,\n principalId: policy.principalId,\n version: policy.version,\n keyEpoch: policy.keyEpoch,\n stateHash: policy.stateHash,\n state: {\n ...policy.state,\n stateHash: policy.stateHash,\n },\n projection: policy.projection,\n grants: policy.grants,\n checkpoint: policy.checkpoint,\n },\n label,\n );\n}\n\nfunction principalPolicyKey(\n policy: Pick<\n VerifiedPrincipalPolicy,\n \"keyEpoch\" | \"principalId\" | \"principalType\" | \"stateHash\" | \"version\"\n >,\n): string {\n return [\n policy.principalType,\n policy.principalId,\n policy.version,\n policy.keyEpoch,\n policy.stateHash,\n ].join(\":\");\n}\n\nexport function uniquePrincipalPolicies<\n T extends PrincipalPolicyCurrentEvidence,\n>(policies: readonly T[]): T[] {\n const policiesByKey = new Map<string, T>();\n\n for (const policy of policies) {\n policiesByKey.set(principalPolicyKey(policy), policy);\n }\n\n return [...policiesByKey.values()];\n}\n"]}
1
+ {"version":3,"file":"principalPolicies.js","sourceRoot":"","sources":["principalPolicies.ts"],"names":[],"mappings":"AAIA;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAChC,MAGC;IAED,OAAO;QACL,aAAa,EAAE,MAAM,CAAC,aAAa;QACnC,WAAW,EAAE,MAAM,CAAC,WAAW;QAC/B,OAAO,EAAE,MAAM,CAAC,OAAO;QACvB,SAAS,EAAE,MAAM,CAAC,SAAS;KAC5B,CAAC;AACJ,CAAC;AAED,SAAS,kBAAkB,CACzB,MAGC;IAED,OAAO;QACL,MAAM,CAAC,aAAa;QACpB,MAAM,CAAC,WAAW;QAClB,MAAM,CAAC,OAAO;QACd,MAAM,CAAC,QAAQ;QACf,MAAM,CAAC,SAAS;KACjB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACd,CAAC;AAED,MAAM,UAAU,uBAAuB,CAErC,QAAsB;IACtB,MAAM,aAAa,GAAG,IAAI,GAAG,EAAa,CAAC;IAE3C,KAAK,MAAM,MAAM,IAAI,QAAQ,EAAE,CAAC;QAC9B,aAAa,CAAC,GAAG,CAAC,kBAAkB,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC,CAAC;IACxD,CAAC;IAED,OAAO,CAAC,GAAG,aAAa,CAAC,MAAM,EAAE,CAAC,CAAC;AACrC,CAAC","sourcesContent":["import type { VerifiedPrincipalPolicy } from \"@tearleads/crypto\";\nimport type { PrincipalPolicyRef } from \"@tearleads/validators/request\";\nimport type { PrincipalPolicyCurrentEvidence } from \"../../principals/principalPolicyEvidence\";\n\n/**\n * The head a container mutation cites for a policy it was planned against.\n * The API authorizes from its own verified copy of that head, so the policy\n * itself never travels.\n */\nexport function principalPolicyRef(\n policy: Pick<\n VerifiedPrincipalPolicy,\n \"principalId\" | \"principalType\" | \"stateHash\" | \"version\"\n >,\n): PrincipalPolicyRef {\n return {\n principalType: policy.principalType,\n principalId: policy.principalId,\n version: policy.version,\n stateHash: policy.stateHash,\n };\n}\n\nfunction principalPolicyKey(\n policy: Pick<\n VerifiedPrincipalPolicy,\n \"keyEpoch\" | \"principalId\" | \"principalType\" | \"stateHash\" | \"version\"\n >,\n): string {\n return [\n policy.principalType,\n policy.principalId,\n policy.version,\n policy.keyEpoch,\n policy.stateHash,\n ].join(\":\");\n}\n\nexport function uniquePrincipalPolicies<\n T extends PrincipalPolicyCurrentEvidence,\n>(policies: readonly T[]): T[] {\n const policiesByKey = new Map<string, T>();\n\n for (const policy of policies) {\n policiesByKey.set(principalPolicyKey(policy), policy);\n }\n\n return [...policiesByKey.values()];\n}\n"]}
@@ -3,6 +3,8 @@ import { type ExecSql } from "../../sqlite/sqlSchema.js";
3
3
  import { type PrincipalHistoryEvidencePage } from "./principalHistoryEvidencePersistence.js";
4
4
  /** A stage's verification work; its retention hints are written by persistence. */
5
5
  export type PrincipalHistoryStage = Omit<typeof principalHistoryStages.$inferSelect, "scopeId" | "touchedAt">;
6
+ /** Another writer replaced this stage's progress between load and save. */
7
+ export declare function isPrincipalHistoryStageChangedError(error: unknown): boolean;
6
8
  export declare function loadPrincipalHistoryStage(execSql: ExecSql, id: string): Promise<PrincipalHistoryStage | null>;
7
9
  /** Publish accepted entries and index nodes atomically with authenticated progress. */
8
10
  export declare function savePrincipalHistoryStage(input: {
@@ -25,6 +25,10 @@ class PrincipalHistoryStageChangedError extends Error {
25
25
  this.name = "PrincipalHistoryStageChangedError";
26
26
  }
27
27
  }
28
+ /** Another writer replaced this stage's progress between load and save. */
29
+ export function isPrincipalHistoryStageChangedError(error) {
30
+ return error instanceof PrincipalHistoryStageChangedError;
31
+ }
28
32
  export async function loadPrincipalHistoryStage(execSql, id) {
29
33
  await ensureSqlTables(execSql, principalHistoryStageTables);
30
34
  const { db } = getClientSQLitePersistenceRuntime(execSql);
@@ -1 +1 @@
1
- {"version":3,"file":"principalHistoryStagePersistence.js","sourceRoot":"","sources":["principalHistoryStagePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,mCAAmC,EAAE,MAAM,0CAA0C,CAAC;AAC/F,OAAO,EAAE,8BAA8B,EAAE,MAAM,oDAAoD,CAAC;AACpG,OAAO,EAAE,mCAAmC,EAAE,MAAM,yDAAyD,CAAC;AAC9G,OAAO,EACL,sBAAsB,EACtB,2BAA2B,GAC5B,MAAM,iDAAiD,CAAC;AACzD,OAAO,EAAE,iCAAiC,EAAE,MAAM,uCAAuC,CAAC;AAC1F,OAAO,EAAgB,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAEL,iCAAiC,GAClC,MAAM,uCAAuC,CAAC;AAC/C,OAAO,EAAE,uCAAuC,EAAE,MAAM,uCAAuC,CAAC;AAChG,OAAO,E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AG,CACD,EAAE,CAAC,sBAAsB,CAAC,EAAE,EAAE,KAAK,CAAC,EAAE,CAAC,EACvC,EAAE,CAAC,sBAAsB,CAAC,QAAQ,EAAE,KAAK,CAAC,QAAQ,CAAC,CACpD,CACF;aACA,GAAG,EAAE,CAAC;IACX,CAAC,EACD,YAAY,EACZ,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;IACF,mCAAmC,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import { and, eq } from \"drizzle-orm\";\nimport { assertProjectionVerificationCurrent } from \"../../keyingProjectionVerification/types\";\nimport { principalHistoryEvidenceTables } from \"../../sqlite/schema/principalHistoryEvidenceSchema\";\nimport { principalHistoryNodeRetentionTables } from \"../../sqlite/schema/principalHistoryNodeRetentionSchema\";\nimport {\n principalHistoryStages,\n principalHistoryStageTables,\n} from \"../../sqlite/schema/principalHistoryStageSchema\";\nimport { getClientSQLitePersistenceRuntime } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../../sqlite/sqlSchema\";\nimport {\n type PrincipalHistoryEvidencePage,\n writePrincipalHistoryEvidencePage,\n} from \"./principalHistoryEvidencePersistence\";\nimport { reclaimIncompletePrincipalHistoryStages } from \"./principalHistoryIncompleteRetention\";\nimport { reclaimPrincipalHistoryNodes } from \"./principalHistoryNodeRetention\";\nimport {\n releasePrincipalHistoryRoot,\n retainPrincipalHistoryRoot,\n} from \"./principalHistoryRootOwnership\";\nimport { reclaimUnpublishedPrincipalHistoryStages } from \"./principalHistoryUnpublishedRetention\";\n\n/** A stage's verification work; its retention hints are written by persistence. */\nexport type PrincipalHistoryStage = Omit<\n typeof principalHistoryStages.$inferSelect,\n \"scopeId\" | \"touchedAt\"\n>;\n\nconst principalHistoryStageColumns = {\n id: principalHistoryStages.id,\n organizationId: principalHistoryStages.organizationId,\n currentJson: principalHistoryStages.currentJson,\n afterVersion: principalHistoryStages.afterVersion,\n complete: principalHistoryStages.complete,\n progress: principalHistoryStages.progress,\n};\n\nclass PrincipalHistoryStageChangedError extends Error {\n readonly code = \"principal_history_stage_changed\";\n constructor() {\n super(\n \"Another operation changed the saved principal history; resume from its current state\",\n );\n this.name = \"PrincipalHistoryStageChangedError\";\n }\n}\n\nexport async function loadPrincipalHistoryStage(\n execSql: ExecSql,\n id: string,\n): Promise<PrincipalHistoryStage | null> {\n await ensureSqlTables(execSql, principalHistoryStageTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const [row] = await db\n .select(principalHistoryStageColumns)\n .from(principalHistoryStages)\n .where(eq(principalHistoryStages.id, id))\n .limit(1);\n return row ?? null;\n}\n\n/** Publish accepted entries and index nodes atomically with authenticated progress. */\nexport async function savePrincipalHistoryStage(input: {\n readonly execSql: ExecSql;\n readonly stage: PrincipalHistoryStage;\n readonly evidence: PrincipalHistoryEvidencePage;\n readonly previousProgress: string | null;\n readonly stillCurrent: () => boolean;\n}): Promise<void> {\n const evidence = structuredClone(input.evidence);\n const stage = { ...input.stage, scopeId: evidence.scopeId };\n await ensureSqlTables(input.execSql, [\n ...principalHistoryStageTables,\n ...principalHistoryEvidenceTables,\n ]);\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n const saved = await runtime.guardedTransaction(\n async (tx) => {\n const [previous] = await tx\n .select({ progress: principalHistoryStages.progress })\n .from(principalHistoryStages)\n .where(eq(principalHistoryStages.id, stage.id))\n .limit(1);\n if ((previous?.progress ?? null) !== input.previousProgress)\n throw new PrincipalHistoryStageChangedError();\n await writePrincipalHistoryEvidencePage(tx, evidence);\n const row = { ...stage, touchedAt: Date.now() };\n await tx\n .insert(principalHistoryStages)\n .values(row)\n .onConflictDoUpdate({ target: principalHistoryStages.id, set: row })\n .run();\n await retainPrincipalHistoryRoot(tx, {\n id: `stage:${stage.id}`,\n scopeId: evidence.scopeId,\n organizationId: stage.organizationId,\n rootHash: evidence.indexRootHash,\n });\n await reclaimIncompletePrincipalHistoryStages(tx, stage);\n await reclaimUnpublishedPrincipalHistoryStages(tx, stage);\n await reclaimPrincipalHistoryNodes(tx, evidence);\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => saved.committed);\n}\n\nexport async function discardPrincipalHistoryStage(\n execSql: ExecSql,\n stage: PrincipalHistoryStage,\n stillCurrent: () => boolean,\n) {\n await ensureSqlTables(execSql, [\n ...principalHistoryStageTables,\n ...principalHistoryNodeRetentionTables,\n ]);\n const runtime = getClientSQLitePersistenceRuntime(execSql);\n const discarded = await runtime.guardedTransaction(\n async (db) => {\n const [current] = await db\n .select({ progress: principalHistoryStages.progress })\n .from(principalHistoryStages)\n .where(eq(principalHistoryStages.id, stage.id))\n .limit(1);\n if (current?.progress !== stage.progress) return;\n await releasePrincipalHistoryRoot(db, `stage:${stage.id}`);\n await db\n .delete(principalHistoryStages)\n .where(\n and(\n eq(principalHistoryStages.id, stage.id),\n eq(principalHistoryStages.progress, stage.progress),\n ),\n )\n .run();\n },\n stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => discarded.committed);\n}\n"]}
1
+ {"version":3,"file":"principalHistoryStagePersistence.js","sourceRoot":"","sources":["principalHistoryStagePersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,mCAAmC,EAAE,MAAM,0CAA0C,CAAC;AAC/F,OAAO,EAAE,8BAA8B,EAAE,MAAM,oDAAoD,CAAC;AACpG,OAAO,EAAE,mCAAmC,EAAE,MAAM,yDAAyD,CAAC;AAC9G,OAAO,EACL,sBAAsB,EACtB,2BAA2B,GAC5B,MAAM,iDAAiD,CAAC;AACzD,OAAO,EAAE,iCAAiC,EAAE,MAAM,uCAAuC,CAAC;AAC1F,OAAO,EAAgB,eAAe,EAAE,MAAM,wBAAwB,CAAC;AACvE,OAAO,EAEL,iCAAiC,GAClC,MAAM,uCA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AAK,CAAC,QAAQ;YAAE,OAAO;QACjD,MAAM,2BAA2B,CAAC,EAAE,EAAE,SAAS,KAAK,CAAC,EAAE,EAAE,CAAC,CAAC;QAC3D,MAAM,EAAE;aACL,MAAM,CAAC,sBAAsB,CAAC;aAC9B,KAAK,CACJ,GAAG,CACD,EAAE,CAAC,sBAAsB,CAAC,EAAE,EAAE,KAAK,CAAC,EAAE,CAAC,EACvC,EAAE,CAAC,sBAAsB,CAAC,QAAQ,EAAE,KAAK,CAAC,QAAQ,CAAC,CACpD,CACF;aACA,GAAG,EAAE,CAAC;IACX,CAAC,EACD,YAAY,EACZ,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;IACF,mCAAmC,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import { and, eq } from \"drizzle-orm\";\nimport { assertProjectionVerificationCurrent } from \"../../keyingProjectionVerification/types\";\nimport { principalHistoryEvidenceTables } from \"../../sqlite/schema/principalHistoryEvidenceSchema\";\nimport { principalHistoryNodeRetentionTables } from \"../../sqlite/schema/principalHistoryNodeRetentionSchema\";\nimport {\n principalHistoryStages,\n principalHistoryStageTables,\n} from \"../../sqlite/schema/principalHistoryStageSchema\";\nimport { getClientSQLitePersistenceRuntime } from \"../../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../../sqlite/sqlSchema\";\nimport {\n type PrincipalHistoryEvidencePage,\n writePrincipalHistoryEvidencePage,\n} from \"./principalHistoryEvidencePersistence\";\nimport { reclaimIncompletePrincipalHistoryStages } from \"./principalHistoryIncompleteRetention\";\nimport { reclaimPrincipalHistoryNodes } from \"./principalHistoryNodeRetention\";\nimport {\n releasePrincipalHistoryRoot,\n retainPrincipalHistoryRoot,\n} from \"./principalHistoryRootOwnership\";\nimport { reclaimUnpublishedPrincipalHistoryStages } from \"./principalHistoryUnpublishedRetention\";\n\n/** A stage's verification work; its retention hints are written by persistence. */\nexport type PrincipalHistoryStage = Omit<\n typeof principalHistoryStages.$inferSelect,\n \"scopeId\" | \"touchedAt\"\n>;\n\nconst principalHistoryStageColumns = {\n id: principalHistoryStages.id,\n organizationId: principalHistoryStages.organizationId,\n currentJson: principalHistoryStages.currentJson,\n afterVersion: principalHistoryStages.afterVersion,\n complete: principalHistoryStages.complete,\n progress: principalHistoryStages.progress,\n};\n\nclass PrincipalHistoryStageChangedError extends Error {\n readonly code = \"principal_history_stage_changed\";\n constructor() {\n super(\n \"Another operation changed the saved principal history; resume from its current state\",\n );\n this.name = \"PrincipalHistoryStageChangedError\";\n }\n}\n\n/** Another writer replaced this stage's progress between load and save. */\nexport function isPrincipalHistoryStageChangedError(error: unknown): boolean {\n return error instanceof PrincipalHistoryStageChangedError;\n}\n\nexport async function loadPrincipalHistoryStage(\n execSql: ExecSql,\n id: string,\n): Promise<PrincipalHistoryStage | null> {\n await ensureSqlTables(execSql, principalHistoryStageTables);\n const { db } = getClientSQLitePersistenceRuntime(execSql);\n const [row] = await db\n .select(principalHistoryStageColumns)\n .from(principalHistoryStages)\n .where(eq(principalHistoryStages.id, id))\n .limit(1);\n return row ?? null;\n}\n\n/** Publish accepted entries and index nodes atomically with authenticated progress. */\nexport async function savePrincipalHistoryStage(input: {\n readonly execSql: ExecSql;\n readonly stage: PrincipalHistoryStage;\n readonly evidence: PrincipalHistoryEvidencePage;\n readonly previousProgress: string | null;\n readonly stillCurrent: () => boolean;\n}): Promise<void> {\n const evidence = structuredClone(input.evidence);\n const stage = { ...input.stage, scopeId: evidence.scopeId };\n await ensureSqlTables(input.execSql, [\n ...principalHistoryStageTables,\n ...principalHistoryEvidenceTables,\n ]);\n const runtime = getClientSQLitePersistenceRuntime(input.execSql);\n const saved = await runtime.guardedTransaction(\n async (tx) => {\n const [previous] = await tx\n .select({ progress: principalHistoryStages.progress })\n .from(principalHistoryStages)\n .where(eq(principalHistoryStages.id, stage.id))\n .limit(1);\n if ((previous?.progress ?? null) !== input.previousProgress)\n throw new PrincipalHistoryStageChangedError();\n await writePrincipalHistoryEvidencePage(tx, evidence);\n const row = { ...stage, touchedAt: Date.now() };\n await tx\n .insert(principalHistoryStages)\n .values(row)\n .onConflictDoUpdate({ target: principalHistoryStages.id, set: row })\n .run();\n await retainPrincipalHistoryRoot(tx, {\n id: `stage:${stage.id}`,\n scopeId: evidence.scopeId,\n organizationId: stage.organizationId,\n rootHash: evidence.indexRootHash,\n });\n await reclaimIncompletePrincipalHistoryStages(tx, stage);\n await reclaimUnpublishedPrincipalHistoryStages(tx, stage);\n await reclaimPrincipalHistoryNodes(tx, evidence);\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => saved.committed);\n}\n\nexport async function discardPrincipalHistoryStage(\n execSql: ExecSql,\n stage: PrincipalHistoryStage,\n stillCurrent: () => boolean,\n) {\n await ensureSqlTables(execSql, [\n ...principalHistoryStageTables,\n ...principalHistoryNodeRetentionTables,\n ]);\n const runtime = getClientSQLitePersistenceRuntime(execSql);\n const discarded = await runtime.guardedTransaction(\n async (db) => {\n const [current] = await db\n .select({ progress: principalHistoryStages.progress })\n .from(principalHistoryStages)\n .where(eq(principalHistoryStages.id, stage.id))\n .limit(1);\n if (current?.progress !== stage.progress) return;\n await releasePrincipalHistoryRoot(db, `stage:${stage.id}`);\n await db\n .delete(principalHistoryStages)\n .where(\n and(\n eq(principalHistoryStages.id, stage.id),\n eq(principalHistoryStages.progress, stage.progress),\n ),\n )\n .run();\n },\n stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => discarded.committed);\n}\n"]}
@@ -45,7 +45,12 @@ export declare const bindBlobAttachmentOperation: Omit<{
45
45
  keyring: Record<string, unknown> | null;
46
46
  manifest: Record<string, unknown>;
47
47
  predecessorBridge: Record<string, unknown> | null;
48
- principalPolicies: Record<string, unknown>[];
48
+ principalPolicyRefs: {
49
+ principalType: "group" | "organization";
50
+ principalId: string;
51
+ version: number;
52
+ stateHash: string;
53
+ }[];
49
54
  wraps: Record<string, unknown>[];
50
55
  containerManifestHistoryRefs?: {
51
56
  containerId: string;
@@ -103,7 +108,12 @@ export declare const bindBlobAttachmentOperation: Omit<{
103
108
  keyring: Record<string, unknown> | null;
104
109
  manifest: Record<string, unknown>;
105
110
  predecessorBridge: Record<string, unknown> | null;
106
- principalPolicies: Record<string, unknown>[];
111
+ principalPolicyRefs: {
112
+ principalType: "group" | "organization";
113
+ principalId: string;
114
+ version: number;
115
+ stateHash: string;
116
+ }[];
107
117
  wraps: Record<string, unknown>[];
108
118
  containerManifestHistoryRefs?: {
109
119
  containerId: string;
@@ -161,7 +171,12 @@ export declare const bindBlobAttachmentOperation: Omit<{
161
171
  keyring: Record<string, unknown> | null;
162
172
  manifest: Record<string, unknown>;
163
173
  predecessorBridge: Record<string, unknown> | null;
164
- principalPolicies: Record<string, unknown>[];
174
+ principalPolicyRefs: {
175
+ principalType: "group" | "organization";
176
+ principalId: string;
177
+ version: number;
178
+ stateHash: string;
179
+ }[];
165
180
  wraps: Record<string, unknown>[];
166
181
  containerManifestHistoryRefs?: {
167
182
  containerId: string;
@@ -219,7 +234,12 @@ export declare const bindBlobAttachmentOperation: Omit<{
219
234
  keyring: Record<string, unknown> | null;
220
235
  manifest: Record<string, unknown>;
221
236
  predecessorBridge: Record<string, unknown> | null;
222
- principalPolicies: Record<string, unknown>[];
237
+ principalPolicyRefs: {
238
+ principalType: "group" | "organization";
239
+ principalId: string;
240
+ version: number;
241
+ stateHash: string;
242
+ }[];
223
243
  wraps: Record<string, unknown>[];
224
244
  containerManifestHistoryRefs?: {
225
245
  containerId: string;
@@ -647,7 +667,12 @@ export declare const detachBlobAttachmentOperation: Omit<{
647
667
  keyring: Record<string, unknown> | null;
648
668
  manifest: Record<string, unknown>;
649
669
  predecessorBridge: Record<string, unknown> | null;
650
- principalPolicies: Record<string, unknown>[];
670
+ principalPolicyRefs: {
671
+ principalType: "group" | "organization";
672
+ principalId: string;
673
+ version: number;
674
+ stateHash: string;
675
+ }[];
651
676
  wraps: Record<string, unknown>[];
652
677
  containerManifestHistoryRefs?: {
653
678
  containerId: string;
@@ -687,7 +712,12 @@ export declare const detachBlobAttachmentOperation: Omit<{
687
712
  keyring: Record<string, unknown> | null;
688
713
  manifest: Record<string, unknown>;
689
714
  predecessorBridge: Record<string, unknown> | null;
690
- principalPolicies: Record<string, unknown>[];
715
+ principalPolicyRefs: {
716
+ principalType: "group" | "organization";
717
+ principalId: string;
718
+ version: number;
719
+ stateHash: string;
720
+ }[];
691
721
  wraps: Record<string, unknown>[];
692
722
  containerManifestHistoryRefs?: {
693
723
  containerId: string;
@@ -727,7 +757,12 @@ export declare const detachBlobAttachmentOperation: Omit<{
727
757
  keyring: Record<string, unknown> | null;
728
758
  manifest: Record<string, unknown>;
729
759
  predecessorBridge: Record<string, unknown> | null;
730
- principalPolicies: Record<string, unknown>[];
760
+ principalPolicyRefs: {
761
+ principalType: "group" | "organization";
762
+ principalId: string;
763
+ version: number;
764
+ stateHash: string;
765
+ }[];
731
766
  wraps: Record<string, unknown>[];
732
767
  containerManifestHistoryRefs?: {
733
768
  containerId: string;
@@ -767,7 +802,12 @@ export declare const detachBlobAttachmentOperation: Omit<{
767
802
  keyring: Record<string, unknown> | null;
768
803
  manifest: Record<string, unknown>;
769
804
  predecessorBridge: Record<string, unknown> | null;
770
- principalPolicies: Record<string, unknown>[];
805
+ principalPolicyRefs: {
806
+ principalType: "group" | "organization";
807
+ principalId: string;
808
+ version: number;
809
+ stateHash: string;
810
+ }[];
771
811
  wraps: Record<string, unknown>[];
772
812
  containerManifestHistoryRefs?: {
773
813
  containerId: string;