@tearleads/client-sdk 0.1.200 → 0.1.201
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.
- package/data/persistence/principalMutationJournalPersistence.d.ts +20 -13
- package/data/persistence/principalMutationJournalPersistence.js +54 -43
- package/data/persistence/principalMutationJournalPersistence.js.map +1 -1
- package/package.json +1 -1
- package/workflows/organizations/principalMutations/principalMutationJournalManagement.js +5 -7
- package/workflows/organizations/principalMutations/principalMutationJournalManagement.js.map +1 -1
- package/workflows/organizations/principalMutations/principalMutationJournalSession.js +4 -6
- package/workflows/organizations/principalMutations/principalMutationJournalSession.js.map +1 -1
|
@@ -4,16 +4,23 @@ export type PrincipalMutationJournalRow = Omit<typeof principalMutationJournal.$
|
|
|
4
4
|
export declare class PendingPrincipalMutationError extends Error {
|
|
5
5
|
constructor();
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
/**
|
|
8
|
+
* The principal mutation journal, bound to one executor: at most one
|
|
9
|
+
* unresolved signed mutation per organization lane. Each write runs its own
|
|
10
|
+
* immediate guarded transaction and throws when `stillCurrent` refuses it.
|
|
11
|
+
*/
|
|
12
|
+
export declare class PrincipalMutationJournalRepository {
|
|
13
|
+
private readonly execSql;
|
|
14
|
+
constructor(execSql: ExecSql);
|
|
15
|
+
loadPrincipalMutationJournal(scopeId: string): Promise<PrincipalMutationJournalRow | null>;
|
|
16
|
+
/** Claim the organization lane before dispatch; never overwrite unresolved work. */
|
|
17
|
+
claimPrincipalMutationJournal(input: {
|
|
18
|
+
readonly row: PrincipalMutationJournalRow;
|
|
19
|
+
readonly stillCurrent: () => boolean;
|
|
20
|
+
}): Promise<void>;
|
|
21
|
+
/** A late acknowledgement cannot retire another operation's journal entry. */
|
|
22
|
+
clearPrincipalMutationJournal(input: {
|
|
23
|
+
readonly row: PrincipalMutationJournalRow;
|
|
24
|
+
readonly stillCurrent: () => boolean;
|
|
25
|
+
}): Promise<boolean>;
|
|
26
|
+
}
|
|
@@ -9,50 +9,61 @@ export class PendingPrincipalMutationError extends Error {
|
|
|
9
9
|
this.name = "PendingPrincipalMutationError";
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
const result = await runtime.guardedTransaction(async (tx) => {
|
|
33
|
-
const [existing] = await tx
|
|
34
|
-
.select({ scopeId: principalMutationJournal.scopeId })
|
|
12
|
+
/**
|
|
13
|
+
* The principal mutation journal, bound to one executor: at most one
|
|
14
|
+
* unresolved signed mutation per organization lane. Each write runs its own
|
|
15
|
+
* immediate guarded transaction and throws when `stillCurrent` refuses it.
|
|
16
|
+
*/
|
|
17
|
+
export class PrincipalMutationJournalRepository {
|
|
18
|
+
execSql;
|
|
19
|
+
constructor(execSql) {
|
|
20
|
+
this.execSql = execSql;
|
|
21
|
+
}
|
|
22
|
+
async loadPrincipalMutationJournal(scopeId) {
|
|
23
|
+
await ensureSqlTables(this.execSql, principalMutationJournalTables);
|
|
24
|
+
const { db } = getClientSQLitePersistenceRuntime(this.execSql);
|
|
25
|
+
const [row] = await db
|
|
26
|
+
.select({
|
|
27
|
+
scopeId: principalMutationJournal.scopeId,
|
|
28
|
+
organizationId: principalMutationJournal.organizationId,
|
|
29
|
+
serializedRequest: principalMutationJournal.serializedRequest,
|
|
30
|
+
signature: principalMutationJournal.signature,
|
|
31
|
+
})
|
|
35
32
|
.from(principalMutationJournal)
|
|
36
|
-
.where(eq(principalMutationJournal.scopeId,
|
|
33
|
+
.where(eq(principalMutationJournal.scopeId, scopeId))
|
|
37
34
|
.limit(1);
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
35
|
+
return row ?? null;
|
|
36
|
+
}
|
|
37
|
+
/** Claim the organization lane before dispatch; never overwrite unresolved work. */
|
|
38
|
+
async claimPrincipalMutationJournal(input) {
|
|
39
|
+
const row = { ...input.row };
|
|
40
|
+
await ensureSqlTables(this.execSql, principalMutationJournalTables);
|
|
41
|
+
const runtime = getClientSQLitePersistenceRuntime(this.execSql);
|
|
42
|
+
const result = await runtime.guardedTransaction(async (tx) => {
|
|
43
|
+
const [existing] = await tx
|
|
44
|
+
.select({ scopeId: principalMutationJournal.scopeId })
|
|
45
|
+
.from(principalMutationJournal)
|
|
46
|
+
.where(eq(principalMutationJournal.scopeId, row.scopeId))
|
|
47
|
+
.limit(1);
|
|
48
|
+
if (existing)
|
|
49
|
+
throw new PendingPrincipalMutationError();
|
|
50
|
+
await tx.insert(principalMutationJournal).values(row).run();
|
|
51
|
+
}, input.stillCurrent, { behavior: "immediate" });
|
|
52
|
+
assertProjectionVerificationCurrent(() => result.committed);
|
|
53
|
+
}
|
|
54
|
+
/** A late acknowledgement cannot retire another operation's journal entry. */
|
|
55
|
+
async clearPrincipalMutationJournal(input) {
|
|
56
|
+
const row = { ...input.row };
|
|
57
|
+
const runtime = getClientSQLitePersistenceRuntime(this.execSql);
|
|
58
|
+
const result = await runtime.guardedTransaction(async (tx) => {
|
|
59
|
+
const deleted = await tx
|
|
60
|
+
.delete(principalMutationJournal)
|
|
61
|
+
.where(and(eq(principalMutationJournal.scopeId, row.scopeId), eq(principalMutationJournal.organizationId, row.organizationId), eq(principalMutationJournal.serializedRequest, row.serializedRequest), eq(principalMutationJournal.signature, row.signature)))
|
|
62
|
+
.returning({ scopeId: principalMutationJournal.scopeId });
|
|
63
|
+
return deleted.length > 0;
|
|
64
|
+
}, input.stillCurrent, { behavior: "immediate" });
|
|
65
|
+
assertProjectionVerificationCurrent(() => result.committed);
|
|
66
|
+
return result.result === true;
|
|
67
|
+
}
|
|
57
68
|
}
|
|
58
69
|
//# sourceMappingURL=principalMutationJournalPersistence.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"principalMutationJournalPersistence.js","sourceRoot":"","sources":["principalMutationJournalPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,mCAAmC,EAAE,MAAM,uCAAuC,CAAC;AAC5F,OAAO,EACL,wBAAwB,EACxB,8BAA8B,GAC/B,MAAM,iDAAiD,CAAC;AACzD,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,CAAC;AACvF,OAAO,EAAgB,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAOpE,MAAM,OAAO,6BAA8B,SAAQ,KAAK;IACtD;QACE,KAAK,CACH,kGAAkG,CACnG,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,+BAA+B,CAAC;IAC9C,CAAC;CACF;AAED,MAAM,CAAC,KAAK,
|
|
1
|
+
{"version":3,"file":"principalMutationJournalPersistence.js","sourceRoot":"","sources":["principalMutationJournalPersistence.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,MAAM,aAAa,CAAC;AACtC,OAAO,EAAE,mCAAmC,EAAE,MAAM,uCAAuC,CAAC;AAC5F,OAAO,EACL,wBAAwB,EACxB,8BAA8B,GAC/B,MAAM,iDAAiD,CAAC;AACzD,OAAO,EAAE,iCAAiC,EAAE,MAAM,oCAAoC,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G,CAAC,iBAAiB,CACtB,EACD,EAAE,CAAC,wBAAwB,CAAC,SAAS,EAAE,GAAG,CAAC,SAAS,CAAC,CACtD,CACF;iBACA,SAAS,CAAC,EAAE,OAAO,EAAE,wBAAwB,CAAC,OAAO,EAAE,CAAC,CAAC;YAC5D,OAAO,OAAO,CAAC,MAAM,GAAG,CAAC,CAAC;QAC5B,CAAC,EACD,KAAK,CAAC,YAAY,EAClB,EAAE,QAAQ,EAAE,WAAW,EAAE,CAC1B,CAAC;QACF,mCAAmC,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QAC5D,OAAO,MAAM,CAAC,MAAM,KAAK,IAAI,CAAC;IAChC,CAAC;CACF","sourcesContent":["import { and, eq } from \"drizzle-orm\";\nimport { assertProjectionVerificationCurrent } from \"../keyingProjectionVerification/types\";\nimport {\n principalMutationJournal,\n principalMutationJournalTables,\n} from \"../sqlite/schema/principalMutationJournalSchema\";\nimport { getClientSQLitePersistenceRuntime } from \"../sqlite/sqlitePersistenceRuntime\";\nimport { type ExecSql, ensureSqlTables } from \"../sqlite/sqlSchema\";\n\nexport type PrincipalMutationJournalRow = Omit<\n typeof principalMutationJournal.$inferSelect,\n \"identityBundleFormat\"\n>;\n\nexport class PendingPrincipalMutationError extends Error {\n constructor() {\n super(\n \"Resolve the pending principal mutation or explicitly abandon its retry before submitting another\",\n );\n this.name = \"PendingPrincipalMutationError\";\n }\n}\n\n/**\n * The principal mutation journal, bound to one executor: at most one\n * unresolved signed mutation per organization lane. Each write runs its own\n * immediate guarded transaction and throws when `stillCurrent` refuses it.\n */\nexport class PrincipalMutationJournalRepository {\n private readonly execSql: ExecSql;\n\n constructor(execSql: ExecSql) {\n this.execSql = execSql;\n }\n\n async loadPrincipalMutationJournal(\n scopeId: string,\n ): Promise<PrincipalMutationJournalRow | null> {\n await ensureSqlTables(this.execSql, principalMutationJournalTables);\n const { db } = getClientSQLitePersistenceRuntime(this.execSql);\n const [row] = await db\n .select({\n scopeId: principalMutationJournal.scopeId,\n organizationId: principalMutationJournal.organizationId,\n serializedRequest: principalMutationJournal.serializedRequest,\n signature: principalMutationJournal.signature,\n })\n .from(principalMutationJournal)\n .where(eq(principalMutationJournal.scopeId, scopeId))\n .limit(1);\n return row ?? null;\n }\n\n /** Claim the organization lane before dispatch; never overwrite unresolved work. */\n async claimPrincipalMutationJournal(input: {\n readonly row: PrincipalMutationJournalRow;\n readonly stillCurrent: () => boolean;\n }): Promise<void> {\n const row = { ...input.row };\n await ensureSqlTables(this.execSql, principalMutationJournalTables);\n const runtime = getClientSQLitePersistenceRuntime(this.execSql);\n const result = await runtime.guardedTransaction(\n async (tx) => {\n const [existing] = await tx\n .select({ scopeId: principalMutationJournal.scopeId })\n .from(principalMutationJournal)\n .where(eq(principalMutationJournal.scopeId, row.scopeId))\n .limit(1);\n if (existing) throw new PendingPrincipalMutationError();\n await tx.insert(principalMutationJournal).values(row).run();\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => result.committed);\n }\n\n /** A late acknowledgement cannot retire another operation's journal entry. */\n async clearPrincipalMutationJournal(input: {\n readonly row: PrincipalMutationJournalRow;\n readonly stillCurrent: () => boolean;\n }): Promise<boolean> {\n const row = { ...input.row };\n const runtime = getClientSQLitePersistenceRuntime(this.execSql);\n const result = await runtime.guardedTransaction(\n async (tx) => {\n const deleted = await tx\n .delete(principalMutationJournal)\n .where(\n and(\n eq(principalMutationJournal.scopeId, row.scopeId),\n eq(principalMutationJournal.organizationId, row.organizationId),\n eq(\n principalMutationJournal.serializedRequest,\n row.serializedRequest,\n ),\n eq(principalMutationJournal.signature, row.signature),\n ),\n )\n .returning({ scopeId: principalMutationJournal.scopeId });\n return deleted.length > 0;\n },\n input.stillCurrent,\n { behavior: \"immediate\" },\n );\n assertProjectionVerificationCurrent(() => result.committed);\n return result.result === true;\n }\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { KeyingVerificationError } from "../../../internal/crypto/index.js";
|
|
2
2
|
import { canonicalKeyingJsonString } from "../../../data/keying/canonicalJson.js";
|
|
3
3
|
import { assertProjectionVerificationCurrent } from "../../../data/keyingProjectionVerification/types.js";
|
|
4
|
-
import {
|
|
4
|
+
import { PrincipalMutationJournalRepository } from "../../../data/persistence/principalMutationJournalPersistence.js";
|
|
5
5
|
import { openPrincipalMutation, principalMutationJournalScopeId, } from "../../../data/principals/principalMutationJournal.js";
|
|
6
6
|
import { principalMutationJournalRecordId, UnreadablePrincipalMutationError, } from "../../../data/principals/principalMutationJournalRecord.js";
|
|
7
7
|
import { getOrCreatePrincipalPolicyCoordinator } from "../../../data/principals/principalPolicyCoordinator.js";
|
|
8
8
|
async function loadAuthenticatedMutation(input) {
|
|
9
9
|
assertProjectionVerificationCurrent(input.stillCurrent);
|
|
10
|
-
const row = await
|
|
10
|
+
const row = await new PrincipalMutationJournalRepository(input.execSql).loadPrincipalMutationJournal(await principalMutationJournalScopeId(input.scope));
|
|
11
11
|
assertProjectionVerificationCurrent(input.stillCurrent);
|
|
12
12
|
if (!row)
|
|
13
13
|
return null;
|
|
@@ -37,7 +37,7 @@ export async function discardUnreadableJournaledPrincipalMutation(input) {
|
|
|
37
37
|
const expected = input.recordId;
|
|
38
38
|
return getOrCreatePrincipalPolicyCoordinator(input.execSql).runJournalOperation(input.scope, async () => {
|
|
39
39
|
assertProjectionVerificationCurrent(input.stillCurrent);
|
|
40
|
-
const row = await
|
|
40
|
+
const row = await new PrincipalMutationJournalRepository(input.execSql).loadPrincipalMutationJournal(await principalMutationJournalScopeId(input.scope));
|
|
41
41
|
assertProjectionVerificationCurrent(input.stillCurrent);
|
|
42
42
|
if (!row)
|
|
43
43
|
return false;
|
|
@@ -58,8 +58,7 @@ export async function discardUnreadableJournaledPrincipalMutation(input) {
|
|
|
58
58
|
}
|
|
59
59
|
if (!unreadable)
|
|
60
60
|
throw new Error("Readable principal mutations require ordinary abandonment");
|
|
61
|
-
const removed = await clearPrincipalMutationJournal({
|
|
62
|
-
execSql: input.execSql,
|
|
61
|
+
const removed = await new PrincipalMutationJournalRepository(input.execSql).clearPrincipalMutationJournal({
|
|
63
62
|
row,
|
|
64
63
|
stillCurrent: input.stillCurrent,
|
|
65
64
|
});
|
|
@@ -90,8 +89,7 @@ export async function abandonJournaledPrincipalMutation(input) {
|
|
|
90
89
|
if (canonicalKeyingJsonString(saved.mutation, "saved authored mutation") !==
|
|
91
90
|
expected)
|
|
92
91
|
throw new Error("The pending principal mutation changed after inspection");
|
|
93
|
-
const removed = await clearPrincipalMutationJournal({
|
|
94
|
-
execSql: input.execSql,
|
|
92
|
+
const removed = await new PrincipalMutationJournalRepository(input.execSql).clearPrincipalMutationJournal({
|
|
95
93
|
row: saved.row,
|
|
96
94
|
stillCurrent: input.stillCurrent,
|
|
97
95
|
});
|
package/workflows/organizations/principalMutations/principalMutationJournalManagement.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"principalMutationJournalManagement.js","sourceRoot":"","sources":["principalMutationJournalManagement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,mCAAmC,EAAE,MAAM,kDAAkD,CAAC;AACvG,OAAO,EACL,6BAA6B,EAC7B,4BAA4B,GAC7B,MAAM,+DAA+D,CAAC;AACvE,OAAO,EAEL,qBAAqB,EACrB,+BAA+B,GAChC,MAAM,mDAAmD,CAAC;AAC3D,OAAO,EACL,gCAAgC,EAChC,gCAAgC,GACjC,MAAM,yDAAyD,CAAC;AACjE,OAAO,EAAE,qCAAqC,EAAE,MAAM,qDAAqD,CAAC;AA2B5G,KAAK,UAAU,yBAAyB,CAAC,KAAsB;IAC7D,mCAAmC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;IACxD,MAAM,GAAG,GAAG,MAAM,4BAA4B,CA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AsB;IAEtB,8EAA8E;IAC9E,OAAO,CAAC,MAAM,yBAAyB,CAAC,KAAK,CAAC,CAAC,EAAE,QAAQ,IAAI,IAAI,CAAC;AACpE,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,iCAAiC,CACrD,KAGC;IAED,IAAI,KAAK,CAAC,yBAAyB,KAAK,IAAI;QAC1C,MAAM,IAAI,KAAK,CACb,qEAAqE,CACtE,CAAC;IACJ,MAAM,QAAQ,GAAG,yBAAyB,CACxC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC,EAC1C,6BAA6B,CAC9B,CAAC;IACF,OAAO,qCAAqC,CAC1C,KAAK,CAAC,OAAO,CACd,CAAC,mBAAmB,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE;QAC5C,MAAM,KAAK,GAAG,MAAM,yBAAyB,CAAC,KAAK,CAAC,CAAC;QACrD,IAAI,CAAC,KAAK;YAAE,OAAO,KAAK,CAAC;QACzB,IACE,yBAAyB,CAAC,KAAK,CAAC,QAAQ,EAAE,yBAAyB,CAAC;YACpE,QAAQ;YAER,MAAM,IAAI,KAAK,CACb,yDAAyD,CAC1D,CAAC;QACJ,MAAM,OAAO,GAAG,MAAM,6BAA6B,CAAC;YAClD,OAAO,EAAE,KAAK,CAAC,OAAO;YACtB,GAAG,EAAE,KAAK,CAAC,GAAG;YACd,YAAY,EAAE,KAAK,CAAC,YAAY;SACjC,CAAC,CAAC;QACH,mCAAmC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;QACxD,IAAI,CAAC,OAAO;YACV,MAAM,IAAI,KAAK,CACb,2DAA2D,CAC5D,CAAC;QACJ,OAAO,IAAI,CAAC;IACd,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { KeyingVerificationError } from \"@tearleads/crypto\";\nimport { canonicalKeyingJsonString } from \"../../../data/keying/canonicalJson\";\nimport { assertProjectionVerificationCurrent } from \"../../../data/keyingProjectionVerification/types\";\nimport {\n clearPrincipalMutationJournal,\n loadPrincipalMutationJournal,\n} from \"../../../data/persistence/principalMutationJournalPersistence\";\nimport {\n type AuthoredPrincipalMutation,\n openPrincipalMutation,\n principalMutationJournalScopeId,\n} from \"../../../data/principals/principalMutationJournal\";\nimport {\n principalMutationJournalRecordId,\n UnreadablePrincipalMutationError,\n} from \"../../../data/principals/principalMutationJournalRecord\";\nimport { getOrCreatePrincipalPolicyCoordinator } from \"../../../data/principals/principalPolicyCoordinator\";\nimport type { PrincipalMutationJournalContext } from \"./principalMutationJournalSession\";\n\nexport interface PrincipalMutationRecoveryApi {\n readonly readPendingPrincipalMutation: (\n organizationId: string,\n ) => Promise<AuthoredPrincipalMutation | null>;\n readonly recoverPendingPrincipalMutation: (\n organizationId: string,\n ) => Promise<void>;\n readonly abandonPendingPrincipalMutation: (\n organizationId: string,\n mutation: AuthoredPrincipalMutation,\n acknowledgeUnknownOutcome: true,\n ) => Promise<boolean>;\n readonly discardUnreadablePrincipalMutation: (\n organizationId: string,\n recordId: string,\n acknowledgeUnknownOutcome: true,\n ) => Promise<boolean>;\n}\n\ntype JournalIdentity = Pick<\n PrincipalMutationJournalContext,\n \"execSql\" | \"scope\" | \"signingKeyPair\" | \"stillCurrent\"\n>;\n\nasync function loadAuthenticatedMutation(input: JournalIdentity) {\n assertProjectionVerificationCurrent(input.stillCurrent);\n const row = await loadPrincipalMutationJournal(\n input.execSql,\n await principalMutationJournalScopeId(input.scope),\n );\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!row) return null;\n let mutation: AuthoredPrincipalMutation;\n try {\n mutation = await openPrincipalMutation({\n scope: input.scope,\n row,\n signingPublicKey: input.signingKeyPair.signingPublicKey,\n });\n } catch (error) {\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!(error instanceof KeyingVerificationError)) throw error;\n const recordId = await principalMutationJournalRecordId(row);\n assertProjectionVerificationCurrent(input.stillCurrent);\n throw new UnreadablePrincipalMutationError(\n recordId,\n error.code === \"invalid_shape\" ? \"format\" : \"authentication\",\n );\n }\n assertProjectionVerificationCurrent(input.stillCurrent);\n return { row, mutation };\n}\n\n/** Discard only the inspected unreadable bytes; never parse them for submission. */\nexport async function discardUnreadableJournaledPrincipalMutation(\n input: JournalIdentity & {\n readonly recordId: string;\n readonly acknowledgeUnknownOutcome: true;\n },\n): Promise<boolean> {\n if (input.acknowledgeUnknownOutcome !== true)\n throw new Error(\n \"Discarding unreadable work requires acknowledging its unknown outcome\",\n );\n const expected = input.recordId;\n return getOrCreatePrincipalPolicyCoordinator(\n input.execSql,\n ).runJournalOperation(input.scope, async () => {\n assertProjectionVerificationCurrent(input.stillCurrent);\n const row = await loadPrincipalMutationJournal(\n input.execSql,\n await principalMutationJournalScopeId(input.scope),\n );\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!row) return false;\n if ((await principalMutationJournalRecordId(row)) !== expected)\n throw new Error(\n \"The unreadable principal mutation changed after inspection\",\n );\n let unreadable = false;\n try {\n await openPrincipalMutation({\n scope: input.scope,\n row,\n signingPublicKey: input.signingKeyPair.signingPublicKey,\n });\n } catch (error) {\n if (!(error instanceof KeyingVerificationError)) throw error;\n unreadable = true;\n }\n if (!unreadable)\n throw new Error(\n \"Readable principal mutations require ordinary abandonment\",\n );\n const removed = await clearPrincipalMutationJournal({\n execSql: input.execSql,\n row,\n stillCurrent: input.stillCurrent,\n });\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!removed)\n throw new Error(\n \"The unreadable principal mutation changed during discard\",\n );\n return true;\n });\n}\n\n/** Inspect authenticated authored work without retrying it or changing checkpoints. */\nexport async function readJournaledPrincipalMutation(\n input: JournalIdentity,\n): Promise<AuthoredPrincipalMutation | null> {\n // Inspection may observe an owned dispatch; all mutations still use the lane.\n return (await loadAuthenticatedMutation(input))?.mutation ?? null;\n}\n\n/**\n * An explicit host/user choice to stop retries, not a claim that the write failed.\n * The exact inspected request must still own the lane; ordinary recovery never\n * calls this. Future authoring must still verify the latest policy state.\n */\nexport async function abandonJournaledPrincipalMutation(\n input: JournalIdentity & {\n readonly mutation: AuthoredPrincipalMutation;\n readonly acknowledgeUnknownOutcome: true;\n },\n): Promise<boolean> {\n if (input.acknowledgeUnknownOutcome !== true)\n throw new Error(\n \"Abandoning authored work requires acknowledging its unknown outcome\",\n );\n const expected = canonicalKeyingJsonString(\n JSON.parse(JSON.stringify(input.mutation)),\n \"inspected authored mutation\",\n );\n return getOrCreatePrincipalPolicyCoordinator(\n input.execSql,\n ).runJournalOperation(input.scope, async () => {\n const saved = await loadAuthenticatedMutation(input);\n if (!saved) return false;\n if (\n canonicalKeyingJsonString(saved.mutation, \"saved authored mutation\") !==\n expected\n )\n throw new Error(\n \"The pending principal mutation changed after inspection\",\n );\n const removed = await clearPrincipalMutationJournal({\n execSql: input.execSql,\n row: saved.row,\n stillCurrent: input.stillCurrent,\n });\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!removed)\n throw new Error(\n \"The pending principal mutation changed during abandonment\",\n );\n return true;\n });\n}\n"]}
|
|
1
|
+
{"version":3,"file":"principalMutationJournalManagement.js","sourceRoot":"","sources":["principalMutationJournalManagement.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,uBAAuB,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,yBAAyB,EAAE,MAAM,oCAAoC,CAAC;AAC/E,OAAO,EAAE,mCAAmC,EAAE,MAAM,kDAAkD,CAAC;AACvG,OAAO,EAAE,kCAAkC,EAAE,MAAM,+DAA+D,CAAC;AACnH,OAAO,EAEL,qBAAqB,EACrB,+BAA+B,GAChC,MAAM,mDAAmD,CAAC;AAC3D,OAAO,EACL,gCAAgC,EAChC,gCAAgC,GACjC,MAAM,yDAAyD,CAAC;AACjE,OAAO,EAAE,qCAAqC,EAAE,MAAM,qDAAqD,CAAC;AA2B5G,KAAK,UAAU,yBAAyB,CAAC,KAAsB;IAC7D,mCAAmC,CAAC,KAAK,CAAC,YAAY,C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ACd,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import { KeyingVerificationError } from \"@tearleads/crypto\";\nimport { canonicalKeyingJsonString } from \"../../../data/keying/canonicalJson\";\nimport { assertProjectionVerificationCurrent } from \"../../../data/keyingProjectionVerification/types\";\nimport { PrincipalMutationJournalRepository } from \"../../../data/persistence/principalMutationJournalPersistence\";\nimport {\n type AuthoredPrincipalMutation,\n openPrincipalMutation,\n principalMutationJournalScopeId,\n} from \"../../../data/principals/principalMutationJournal\";\nimport {\n principalMutationJournalRecordId,\n UnreadablePrincipalMutationError,\n} from \"../../../data/principals/principalMutationJournalRecord\";\nimport { getOrCreatePrincipalPolicyCoordinator } from \"../../../data/principals/principalPolicyCoordinator\";\nimport type { PrincipalMutationJournalContext } from \"./principalMutationJournalSession\";\n\nexport interface PrincipalMutationRecoveryApi {\n readonly readPendingPrincipalMutation: (\n organizationId: string,\n ) => Promise<AuthoredPrincipalMutation | null>;\n readonly recoverPendingPrincipalMutation: (\n organizationId: string,\n ) => Promise<void>;\n readonly abandonPendingPrincipalMutation: (\n organizationId: string,\n mutation: AuthoredPrincipalMutation,\n acknowledgeUnknownOutcome: true,\n ) => Promise<boolean>;\n readonly discardUnreadablePrincipalMutation: (\n organizationId: string,\n recordId: string,\n acknowledgeUnknownOutcome: true,\n ) => Promise<boolean>;\n}\n\ntype JournalIdentity = Pick<\n PrincipalMutationJournalContext,\n \"execSql\" | \"scope\" | \"signingKeyPair\" | \"stillCurrent\"\n>;\n\nasync function loadAuthenticatedMutation(input: JournalIdentity) {\n assertProjectionVerificationCurrent(input.stillCurrent);\n const row = await new PrincipalMutationJournalRepository(\n input.execSql,\n ).loadPrincipalMutationJournal(\n await principalMutationJournalScopeId(input.scope),\n );\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!row) return null;\n let mutation: AuthoredPrincipalMutation;\n try {\n mutation = await openPrincipalMutation({\n scope: input.scope,\n row,\n signingPublicKey: input.signingKeyPair.signingPublicKey,\n });\n } catch (error) {\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!(error instanceof KeyingVerificationError)) throw error;\n const recordId = await principalMutationJournalRecordId(row);\n assertProjectionVerificationCurrent(input.stillCurrent);\n throw new UnreadablePrincipalMutationError(\n recordId,\n error.code === \"invalid_shape\" ? \"format\" : \"authentication\",\n );\n }\n assertProjectionVerificationCurrent(input.stillCurrent);\n return { row, mutation };\n}\n\n/** Discard only the inspected unreadable bytes; never parse them for submission. */\nexport async function discardUnreadableJournaledPrincipalMutation(\n input: JournalIdentity & {\n readonly recordId: string;\n readonly acknowledgeUnknownOutcome: true;\n },\n): Promise<boolean> {\n if (input.acknowledgeUnknownOutcome !== true)\n throw new Error(\n \"Discarding unreadable work requires acknowledging its unknown outcome\",\n );\n const expected = input.recordId;\n return getOrCreatePrincipalPolicyCoordinator(\n input.execSql,\n ).runJournalOperation(input.scope, async () => {\n assertProjectionVerificationCurrent(input.stillCurrent);\n const row = await new PrincipalMutationJournalRepository(\n input.execSql,\n ).loadPrincipalMutationJournal(\n await principalMutationJournalScopeId(input.scope),\n );\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!row) return false;\n if ((await principalMutationJournalRecordId(row)) !== expected)\n throw new Error(\n \"The unreadable principal mutation changed after inspection\",\n );\n let unreadable = false;\n try {\n await openPrincipalMutation({\n scope: input.scope,\n row,\n signingPublicKey: input.signingKeyPair.signingPublicKey,\n });\n } catch (error) {\n if (!(error instanceof KeyingVerificationError)) throw error;\n unreadable = true;\n }\n if (!unreadable)\n throw new Error(\n \"Readable principal mutations require ordinary abandonment\",\n );\n const removed = await new PrincipalMutationJournalRepository(\n input.execSql,\n ).clearPrincipalMutationJournal({\n row,\n stillCurrent: input.stillCurrent,\n });\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!removed)\n throw new Error(\n \"The unreadable principal mutation changed during discard\",\n );\n return true;\n });\n}\n\n/** Inspect authenticated authored work without retrying it or changing checkpoints. */\nexport async function readJournaledPrincipalMutation(\n input: JournalIdentity,\n): Promise<AuthoredPrincipalMutation | null> {\n // Inspection may observe an owned dispatch; all mutations still use the lane.\n return (await loadAuthenticatedMutation(input))?.mutation ?? null;\n}\n\n/**\n * An explicit host/user choice to stop retries, not a claim that the write failed.\n * The exact inspected request must still own the lane; ordinary recovery never\n * calls this. Future authoring must still verify the latest policy state.\n */\nexport async function abandonJournaledPrincipalMutation(\n input: JournalIdentity & {\n readonly mutation: AuthoredPrincipalMutation;\n readonly acknowledgeUnknownOutcome: true;\n },\n): Promise<boolean> {\n if (input.acknowledgeUnknownOutcome !== true)\n throw new Error(\n \"Abandoning authored work requires acknowledging its unknown outcome\",\n );\n const expected = canonicalKeyingJsonString(\n JSON.parse(JSON.stringify(input.mutation)),\n \"inspected authored mutation\",\n );\n return getOrCreatePrincipalPolicyCoordinator(\n input.execSql,\n ).runJournalOperation(input.scope, async () => {\n const saved = await loadAuthenticatedMutation(input);\n if (!saved) return false;\n if (\n canonicalKeyingJsonString(saved.mutation, \"saved authored mutation\") !==\n expected\n )\n throw new Error(\n \"The pending principal mutation changed after inspection\",\n );\n const removed = await new PrincipalMutationJournalRepository(\n input.execSql,\n ).clearPrincipalMutationJournal({\n row: saved.row,\n stillCurrent: input.stillCurrent,\n });\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!removed)\n throw new Error(\n \"The pending principal mutation changed during abandonment\",\n );\n return true;\n });\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { assertProjectionVerificationCurrent } from "../../../data/keyingProjectionVerification/types.js";
|
|
2
|
-
import {
|
|
2
|
+
import { PrincipalMutationJournalRepository, } from "../../../data/persistence/principalMutationJournalPersistence.js";
|
|
3
3
|
import { openPrincipalMutation, principalMutationJournalScopeId, sealPrincipalMutation, } from "../../../data/principals/principalMutationJournal.js";
|
|
4
4
|
import { getOrCreatePrincipalPolicyCoordinator } from "../../../data/principals/principalPolicyCoordinator.js";
|
|
5
5
|
import { assertAuthoredPrincipalOperationReceipt, } from "./principalOperationReceipt.js";
|
|
@@ -47,8 +47,7 @@ async function submitSavedMutation(input, row, recovering) {
|
|
|
47
47
|
// an earlier disconnected attempt committed. Preserve the original intent.
|
|
48
48
|
throw new PrincipalMutationOutcomeUnknownError();
|
|
49
49
|
}
|
|
50
|
-
await clearPrincipalMutationJournal({
|
|
51
|
-
execSql: input.execSql,
|
|
50
|
+
await new PrincipalMutationJournalRepository(input.execSql).clearPrincipalMutationJournal({
|
|
52
51
|
row,
|
|
53
52
|
stillCurrent: input.stillCurrent,
|
|
54
53
|
});
|
|
@@ -60,8 +59,7 @@ export async function submitJournaledPrincipalMutation(input) {
|
|
|
60
59
|
return getOrCreatePrincipalPolicyCoordinator(input.execSql).runJournalOperation(input.scope, async () => {
|
|
61
60
|
assertProjectionVerificationCurrent(input.stillCurrent);
|
|
62
61
|
const row = await sealPrincipalMutation(input);
|
|
63
|
-
await claimPrincipalMutationJournal({
|
|
64
|
-
execSql: input.execSql,
|
|
62
|
+
await new PrincipalMutationJournalRepository(input.execSql).claimPrincipalMutationJournal({
|
|
65
63
|
row,
|
|
66
64
|
stillCurrent: input.stillCurrent,
|
|
67
65
|
});
|
|
@@ -72,7 +70,7 @@ export async function submitJournaledPrincipalMutation(input) {
|
|
|
72
70
|
export async function recoverJournaledPrincipalMutation(input) {
|
|
73
71
|
return getOrCreatePrincipalPolicyCoordinator(input.execSql).runJournalOperation(input.scope, async () => {
|
|
74
72
|
assertProjectionVerificationCurrent(input.stillCurrent);
|
|
75
|
-
const row = await
|
|
73
|
+
const row = await new PrincipalMutationJournalRepository(input.execSql).loadPrincipalMutationJournal(await principalMutationJournalScopeId(input.scope));
|
|
76
74
|
assertProjectionVerificationCurrent(input.stillCurrent);
|
|
77
75
|
if (!row)
|
|
78
76
|
return null;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"principalMutationJournalSession.js","sourceRoot":"","sources":["principalMutationJournalSession.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mCAAmC,EAAE,MAAM,kDAAkD,CAAC;AACvG,OAAO,EACL,
|
|
1
|
+
{"version":3,"file":"principalMutationJournalSession.js","sourceRoot":"","sources":["principalMutationJournalSession.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,mCAAmC,EAAE,MAAM,kDAAkD,CAAC;AACvG,OAAO,EACL,kCAAkC,GAEnC,MAAM,+DAA+D,CAAC;AACvE,OAAO,EAEL,qBAAqB,EAErB,+BAA+B,EAC/B,qBAAqB,GACtB,MAAM,mDAAmD,CAAC;AAC3D,OAAO,EAAE,qCAAqC,EAAE,MAAM,qDAAqD,CAAC;AAE5G,OAAO,EACL,uCAAuC,GAExC,MAAM,6BAA6B,CAAC;AAcrC,MAAM,OAAO,oCAAqC,SAAQ,KAAK;IAC7D,YAAY,OAAsB;QAChC,KAAK,CACH,wHAAwH,EACxH,OAAO,CACR,CAAC;QACF,IAAI,CAAC,IAAI,GAAG,sCAAsC,CAAC;IACrD,CAAC;CACF;AAED,SAAS,mBAAmB,CAAC,MAAsB;IACjD,IAAI,MAAM,CAAC,EAAE;QAAE,OAAO,KAAK,CAAC;IAC5B,8EAA8E;IAC9E,IAAI,MAAM,CAAC,IAAI,KAAK,WAAW;QAAE,OAAO,IAAI,CAAC;IAC7C,IAAI,MAAM,CAAC,IAAI,KAAK,MAAM;QAAE,OAAO,KAAK,CAAC;IACzC,OAAO,CACL,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAC,CAAC,QAAQ,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC;QACtD,CAAC,MAAM,CAAC,MAAM,KAAK,GAAG;YACpB,MAAM,CAAC,IAAI,KAAK,2CAA2C,CAAC,CAC/D,CAAC;AACJ,CAAC;AAED,KAAK,UAAU,mBAAmB,CAChC,KAAsC,EACtC,GAAgC,EAChC,UAAmB;IAEnB,MAAM,QAAQ,GAAG,MAAM,qBAAqB,CAAC;QAC3C,KAAK,EAAE,KAAK,CAAC,KAAK;QAClB,GAAG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,YAAY,CAAC,CAAC;QACxD,MAAM,GAAG,GAAG,MAAM,qBAAqB,CAAC,KAAK,CAAC,CAAC;QAC/C,MAAM,IAAI,kCAAkC,CAC1C,KAAK,CAAC,OAAO,CACd,CAAC,6BAA6B,CAAC;YAC9B,GAAG;YACH,YAAY,EAAE,KAAK,CAAC,YAAY;SACjC,CAAC,CAAC;QACH,OAAO,CAAC,MAAM,mBAAmB,CAAC,KAAK,EAAE,GAAG,EAAE,KAAK,CAAC,CAAC,CAAC,MAAM,CAAC;IAC/D,CAAC,CAAC,CAAC;AACL,CAAC;AAED,oFAAoF;AACpF,MAAM,CAAC,KAAK,UAAU,iCAAiC,CACrD,KAAsC;IAKtC,OAAO,qCAAqC,CAC1C,KAAK,CAAC,OAAO,CACd,CAAC,mBAAmB,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE;QAC5C,mCAAmC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;QACxD,MAAM,GAAG,GAAG,MAAM,IAAI,kCAAkC,CACtD,KAAK,CAAC,OAAO,CACd,CAAC,4BAA4B,CAC5B,MAAM,+BAA+B,CAAC,KAAK,CAAC,KAAK,CAAC,CACnD,CAAC;QACF,mCAAmC,CAAC,KAAK,CAAC,YAAY,CAAC,CAAC;QACxD,IAAI,CAAC,GAAG;YAAE,OAAO,IAAI,CAAC;QACtB,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,GAAG,MAAM,mBAAmB,CAAC,KAAK,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;QACzE,IAAI,CAAC,MAAM,CAAC,EAAE;YAAE,MAAM,IAAI,oCAAoC,EAAE,CAAC;QACjE,OAAO,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;IAC7C,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { RequestResult } from \"@tearleads/api-client\";\nimport type { SigningKeyPair } from \"@tearleads/crypto\";\nimport { assertProjectionVerificationCurrent } from \"../../../data/keyingProjectionVerification/types\";\nimport {\n PrincipalMutationJournalRepository,\n type PrincipalMutationJournalRow,\n} from \"../../../data/persistence/principalMutationJournalPersistence\";\nimport {\n type AuthoredPrincipalMutation,\n openPrincipalMutation,\n type PrincipalMutationJournalScope,\n principalMutationJournalScopeId,\n sealPrincipalMutation,\n} from \"../../../data/principals/principalMutationJournal\";\nimport { getOrCreatePrincipalPolicyCoordinator } from \"../../../data/principals/principalPolicyCoordinator\";\nimport type { ExecSql } from \"../../../data/sqlite/sqlSchema\";\nimport {\n assertAuthoredPrincipalOperationReceipt,\n type PrincipalMutationResponse,\n} from \"./principalOperationReceipt\";\n\ntype MutationResult = RequestResult<PrincipalMutationResponse>;\n\nexport interface PrincipalMutationJournalContext {\n readonly execSql: ExecSql;\n readonly scope: PrincipalMutationJournalScope;\n readonly signingKeyPair: SigningKeyPair;\n readonly stillCurrent: () => boolean;\n readonly submit: (\n mutation: AuthoredPrincipalMutation,\n ) => Promise<MutationResult>;\n}\n\nexport class PrincipalMutationOutcomeUnknownError extends Error {\n constructor(options?: ErrorOptions) {\n super(\n \"A saved policy request may have committed; resolve its outcome or explicitly abandon its retry before another mutation\",\n options,\n );\n this.name = \"PrincipalMutationOutcomeUnknownError\";\n }\n}\n\nfunction knownInitialRefusal(result: MutationResult): boolean {\n if (result.ok) return false;\n // ApiClient separates pre-dispatch cancellation from an uncertain sent write.\n if (result.kind === \"cancelled\") return true;\n if (result.kind !== \"http\") return false;\n return (\n [400, 401, 402, 403, 409].includes(result.status ?? 0) ||\n (result.status === 503 &&\n result.code === \"principal_history_preparation_unavailable\")\n );\n}\n\nasync function submitSavedMutation(\n input: PrincipalMutationJournalContext,\n row: PrincipalMutationJournalRow,\n recovering: boolean,\n): Promise<{ mutation: AuthoredPrincipalMutation; result: MutationResult }> {\n const mutation = await openPrincipalMutation({\n scope: input.scope,\n row,\n signingPublicKey: input.signingKeyPair.signingPublicKey,\n });\n assertProjectionVerificationCurrent(input.stillCurrent);\n // Keep the authenticated copy for acknowledgement checks even if a transport\n // mutates its argument while preparing or retrying the HTTP request.\n let result: MutationResult;\n try {\n result = await input.submit(structuredClone(mutation));\n } catch (cause) {\n assertProjectionVerificationCurrent(input.stillCurrent);\n throw new PrincipalMutationOutcomeUnknownError({ cause });\n }\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (result.ok) {\n await assertAuthoredPrincipalOperationReceipt(mutation, result.data);\n } else if (recovering || !knownInitialRefusal(result)) {\n // A refusal today (including loss of admin access) cannot establish whether\n // an earlier disconnected attempt committed. Preserve the original intent.\n throw new PrincipalMutationOutcomeUnknownError();\n }\n await new PrincipalMutationJournalRepository(\n input.execSql,\n ).clearPrincipalMutationJournal({\n row,\n stillCurrent: input.stillCurrent,\n });\n assertProjectionVerificationCurrent(input.stillCurrent);\n return { mutation, result };\n}\n\n/** No HTTP begins until the exact authored request is durably claimed. */\nexport async function submitJournaledPrincipalMutation(\n input: PrincipalMutationJournalContext & {\n readonly mutation: AuthoredPrincipalMutation;\n },\n): Promise<MutationResult> {\n return getOrCreatePrincipalPolicyCoordinator(\n input.execSql,\n ).runJournalOperation(input.scope, async () => {\n assertProjectionVerificationCurrent(input.stillCurrent);\n const row = await sealPrincipalMutation(input);\n await new PrincipalMutationJournalRepository(\n input.execSql,\n ).claimPrincipalMutationJournal({\n row,\n stillCurrent: input.stillCurrent,\n });\n return (await submitSavedMutation(input, row, false)).result;\n });\n}\n\n/** Retry only authenticated saved bytes, never an application mutation callback. */\nexport async function recoverJournaledPrincipalMutation(\n input: PrincipalMutationJournalContext,\n): Promise<{\n readonly mutation: AuthoredPrincipalMutation;\n readonly response: PrincipalMutationResponse;\n} | null> {\n return getOrCreatePrincipalPolicyCoordinator(\n input.execSql,\n ).runJournalOperation(input.scope, async () => {\n assertProjectionVerificationCurrent(input.stillCurrent);\n const row = await new PrincipalMutationJournalRepository(\n input.execSql,\n ).loadPrincipalMutationJournal(\n await principalMutationJournalScopeId(input.scope),\n );\n assertProjectionVerificationCurrent(input.stillCurrent);\n if (!row) return null;\n const { mutation, result } = await submitSavedMutation(input, row, true);\n if (!result.ok) throw new PrincipalMutationOutcomeUnknownError();\n return { mutation, response: result.data };\n });\n}\n"]}
|