@serfab/cadre-core 1.22.0 → 1.24.0
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/README.md +216 -294
- package/dist/arachnode-stub.d.ts +12 -4
- package/dist/arachnode-stub.js +15 -8
- package/dist/arachnode-stub.js.map +1 -1
- package/dist/cadre-invite-protocol.js +5 -4
- package/dist/cadre-invite-protocol.js.map +1 -1
- package/dist/cadre-node.d.ts +159 -53
- package/dist/cadre-node.js +276 -89
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-authorization.d.ts +14 -2
- package/dist/control-authorization.js +8 -3
- package/dist/control-authorization.js.map +1 -1
- package/dist/control-cohort.d.ts +27 -13
- package/dist/control-cohort.js +21 -14
- package/dist/control-cohort.js.map +1 -1
- package/dist/control-database.d.ts +18 -16
- package/dist/control-database.js +57 -22
- package/dist/control-database.js.map +1 -1
- package/dist/control-protocol-guard.d.ts +1 -1
- package/dist/control-protocol-guard.js +7 -3
- package/dist/control-protocol-guard.js.map +1 -1
- package/dist/control-read-retry.d.ts +49 -11
- package/dist/control-read-retry.js +56 -15
- package/dist/control-read-retry.js.map +1 -1
- package/dist/control-retry.d.ts +32 -3
- package/dist/control-retry.js +35 -4
- package/dist/control-retry.js.map +1 -1
- package/dist/control-schema-version.d.ts +5 -5
- package/dist/control-schema-version.js +5 -5
- package/dist/control-schema-version.js.map +1 -1
- package/dist/control-schema.d.ts +5 -5
- package/dist/control-schema.js +1032 -1011
- package/dist/control-schema.js.map +1 -1
- package/dist/control-write-retry.d.ts +54 -65
- package/dist/control-write-retry.js +134 -158
- package/dist/control-write-retry.js.map +1 -1
- package/dist/delegate-admission.d.ts +1 -1
- package/dist/delegate-admission.js +1 -1
- package/dist/edge-node/config.d.ts +4 -3
- package/dist/edge-node/config.js +11 -9
- package/dist/edge-node/config.js.map +1 -1
- package/dist/edge-node/dissolve.d.ts +4 -4
- package/dist/edge-node/dissolve.js +4 -4
- package/dist/edge-node/edge-join.d.ts +1 -1
- package/dist/edge-node/edge-join.js +1 -1
- package/dist/edge-node/edge-node.js +2 -2
- package/dist/edge-node/edge-node.js.map +1 -1
- package/dist/edge-node/index.d.ts +1 -0
- package/dist/edge-node/index.js +1 -0
- package/dist/edge-node/index.js.map +1 -1
- package/dist/edge-node/join-credential.d.ts +1 -1
- package/dist/edge-node/join-credential.js +1 -1
- package/dist/edge-node/lazy-raw-storage.js +12 -0
- package/dist/edge-node/lazy-raw-storage.js.map +1 -1
- package/dist/edge-node/listen-addrs.d.ts +21 -0
- package/dist/edge-node/listen-addrs.js +25 -0
- package/dist/edge-node/listen-addrs.js.map +1 -0
- package/dist/edge-node/owner-grant.d.ts +1 -1
- package/dist/edge-node/owner-grant.js +1 -1
- package/dist/edge-node/platform.d.ts +5 -4
- package/dist/edge-node/platform.js.map +1 -1
- package/dist/edge-node/strand-move.d.ts +1 -1
- package/dist/edge-node/strand-move.js +1 -1
- package/dist/edge-node/strands.js +27 -11
- package/dist/edge-node/strands.js.map +1 -1
- package/dist/index.d.ts +7 -5
- package/dist/index.js +18 -7
- package/dist/index.js.map +1 -1
- package/dist/invitation-bootstrap.d.ts +28 -5
- package/dist/invitation-bootstrap.js +41 -8
- package/dist/invitation-bootstrap.js.map +1 -1
- package/dist/link-budget.d.ts +53 -4
- package/dist/link-budget.js +58 -5
- package/dist/link-budget.js.map +1 -1
- package/dist/machine-removal.d.ts +2 -2
- package/dist/machine-removal.js +4 -3
- package/dist/machine-removal.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +2 -2
- package/dist/membership-connection-gater.js +2 -2
- package/dist/membership-view.d.ts +2 -2
- package/dist/membership-view.js +6 -3
- package/dist/membership-view.js.map +1 -1
- package/dist/node-claim-payload.d.ts +15 -7
- package/dist/node-claim-payload.js +25 -9
- package/dist/node-claim-payload.js.map +1 -1
- package/dist/node-class.d.ts +36 -0
- package/dist/node-class.js +26 -0
- package/dist/node-class.js.map +1 -0
- package/dist/node-default-transports.d.ts +50 -3
- package/dist/node-default-transports.js +48 -3
- package/dist/node-default-transports.js.map +1 -1
- package/dist/node-roles.d.ts +118 -0
- package/dist/node-roles.js +142 -0
- package/dist/node-roles.js.map +1 -0
- package/dist/node-transports.d.ts +6 -2
- package/dist/node-transports.js +39 -7
- package/dist/node-transports.js.map +1 -1
- package/dist/owner-anchor-sync.js +1 -1
- package/dist/owner-key-export.js +1 -1
- package/dist/owner-key-flows.d.ts +1 -1
- package/dist/owner-key-store.js +1 -1
- package/dist/owner-signer.js +1 -1
- package/dist/peer-authorization.d.ts +3 -1
- package/dist/peer-authorization.js +3 -1
- package/dist/peer-authorization.js.map +1 -1
- package/dist/peer-dial.d.ts +74 -10
- package/dist/peer-dial.js +89 -17
- package/dist/peer-dial.js.map +1 -1
- package/dist/peer-join-backfill.js +4 -4
- package/dist/peer-join-backfill.js.map +1 -1
- package/dist/peer-record.d.ts +34 -9
- package/dist/peer-record.js +66 -13
- package/dist/peer-record.js.map +1 -1
- package/dist/pending-join-runner.d.ts +1 -1
- package/dist/pending-join-runner.js +1 -1
- package/dist/provider/client.d.ts +6 -5
- package/dist/provider/client.js +3 -3
- package/dist/provider/client.js.map +1 -1
- package/dist/provider/descriptor.d.ts +1 -1
- package/dist/provider/descriptor.js +1 -1
- package/dist/provider/oauth.d.ts +1 -1
- package/dist/provider/oauth.js +1 -1
- package/dist/provider/rent-node.d.ts +4 -3
- package/dist/provider/rent-node.js +3 -3
- package/dist/provider/rent-node.js.map +1 -1
- package/dist/relay-addrs.d.ts +62 -7
- package/dist/relay-addrs.js +74 -11
- package/dist/relay-addrs.js.map +1 -1
- package/dist/relay-reservation.d.ts +1 -1
- package/dist/relay-reservation.js +1 -1
- package/dist/relay-server.d.ts +6 -5
- package/dist/relay-server.js +5 -5
- package/dist/relay-server.js.map +1 -1
- package/dist/schema-verification.d.ts +13 -10
- package/dist/schema-verification.js +32 -24
- package/dist/schema-verification.js.map +1 -1
- package/dist/sealed-party-key.js +1 -1
- package/dist/seed-bootstrap.d.ts +4 -3
- package/dist/seed-bootstrap.js +6 -7
- package/dist/seed-bootstrap.js.map +1 -1
- package/dist/strand-addr-protocol.d.ts +8 -6
- package/dist/strand-addr-protocol.js +12 -10
- package/dist/strand-addr-protocol.js.map +1 -1
- package/dist/strand-app-schema-report.d.ts +16 -2
- package/dist/strand-app-schema-report.js +15 -0
- package/dist/strand-app-schema-report.js.map +1 -1
- package/dist/strand-cohort.d.ts +1 -4
- package/dist/strand-cohort.js.map +1 -1
- package/dist/strand-commit-durability.d.ts +1 -1
- package/dist/strand-database.d.ts +13 -6
- package/dist/strand-database.js +17 -9
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-first-sync-gate.d.ts +3 -2
- package/dist/strand-first-sync-gate.js +3 -2
- package/dist/strand-first-sync-gate.js.map +1 -1
- package/dist/strand-formation-deadlines.d.ts +2 -2
- package/dist/strand-formation-deadlines.js +2 -2
- package/dist/strand-formation-protocol.d.ts +1 -1
- package/dist/strand-formation-protocol.js +51 -16
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +134 -42
- package/dist/strand-instance-manager.js +331 -76
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-membership-reconciler.d.ts +8 -3
- package/dist/strand-membership-reconciler.js +8 -3
- package/dist/strand-membership-reconciler.js.map +1 -1
- package/dist/strand-membership-writer.d.ts +2 -2
- package/dist/strand-membership-writer.js +41 -39
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-move.js +1 -1
- package/dist/strand-network-config.d.ts +15 -5
- package/dist/strand-network-config.js +7 -6
- package/dist/strand-network-config.js.map +1 -1
- package/dist/strand-party-key-rotation.d.ts +7 -3
- package/dist/strand-party-key-rotation.js +7 -3
- package/dist/strand-party-key-rotation.js.map +1 -1
- package/dist/strand-party-machines.d.ts +19 -8
- package/dist/strand-party-machines.js +26 -11
- package/dist/strand-party-machines.js.map +1 -1
- package/dist/strand-party-peers.d.ts +36 -14
- package/dist/strand-party-peers.js +24 -15
- package/dist/strand-party-peers.js.map +1 -1
- package/dist/strand-peer-redial.d.ts +1 -1
- package/dist/strand-peer-redial.js +1 -1
- package/dist/strand-reactivity.d.ts +20 -17
- package/dist/strand-reactivity.js +31 -18
- package/dist/strand-reactivity.js.map +1 -1
- package/dist/strand-revocation-enforcer.d.ts +3 -3
- package/dist/strand-revocation-enforcer.js +3 -3
- package/dist/strand-wake-protocol.d.ts +19 -7
- package/dist/strand-wake-protocol.js +22 -10
- package/dist/strand-wake-protocol.js.map +1 -1
- package/dist/strand-watcher.d.ts +1 -1
- package/dist/strand-watcher.js +1 -1
- package/dist/table-change-watch.js +1 -1
- package/dist/types.d.ts +127 -57
- package/dist/types.js.map +1 -1
- package/dist/webrtc-upgrade.d.ts +254 -0
- package/dist/webrtc-upgrade.js +355 -0
- package/dist/webrtc-upgrade.js.map +1 -0
- package/package.json +9 -9
- package/src/announce-addrs.ts +93 -93
- package/src/arachnode-stub.ts +18 -10
- package/src/bootstrap-peer-store-file.ts +44 -44
- package/src/bootstrap-peer-store.ts +214 -214
- package/src/cadre-invite-protocol.ts +741 -740
- package/src/cadre-node.ts +12462 -12252
- package/src/claim-proof.ts +147 -147
- package/src/control-authorization.ts +290 -276
- package/src/control-cohort.ts +223 -204
- package/src/control-database.ts +72 -28
- package/src/control-formation-recorder.ts +359 -359
- package/src/control-protocol-guard.ts +370 -366
- package/src/control-read-retry.ts +222 -180
- package/src/control-retry.ts +36 -4
- package/src/control-schema-version.ts +301 -301
- package/src/control-schema.ts +1108 -1087
- package/src/control-write-retry.ts +639 -669
- package/src/delegate-admission.ts +351 -351
- package/src/describe-thrown.ts +60 -60
- package/src/ed25519-key.ts +185 -185
- package/src/edge-node/config.ts +13 -10
- package/src/edge-node/dissolve.ts +252 -252
- package/src/edge-node/edge-join.ts +320 -320
- package/src/edge-node/edge-node.ts +1095 -1095
- package/src/edge-node/index.ts +60 -59
- package/src/edge-node/join-credential.ts +16 -16
- package/src/edge-node/lazy-raw-storage.ts +194 -178
- package/src/edge-node/listen-addrs.ts +26 -0
- package/src/edge-node/node-local.ts +84 -84
- package/src/edge-node/options.ts +195 -195
- package/src/edge-node/owner-grant.ts +63 -63
- package/src/edge-node/platform.ts +5 -4
- package/src/edge-node/strand-move.ts +266 -266
- package/src/edge-node/strands.ts +27 -12
- package/src/enrolled-machine-store-file.ts +43 -43
- package/src/enrolled-machine-store.ts +280 -280
- package/src/fetch-impl.ts +23 -23
- package/src/file-durable-slot.ts +64 -64
- package/src/file-safe-name.ts +32 -32
- package/src/formation-approval.ts +604 -604
- package/src/formation-token.ts +63 -63
- package/src/fs-atomic.ts +92 -92
- package/src/index.ts +987 -957
- package/src/invitation-bootstrap.ts +152 -118
- package/src/joined-strand-store.ts +553 -553
- package/src/key-store-file.ts +142 -142
- package/src/link-budget.ts +61 -5
- package/src/machine-removal.ts +211 -210
- package/src/member-redemption.ts +643 -643
- package/src/membership-connection-gater.ts +650 -650
- package/src/membership-view.ts +147 -144
- package/src/node-claim-payload.ts +34 -12
- package/src/node-class.ts +47 -0
- package/src/node-default-transports.ts +65 -3
- package/src/node-roles.ts +220 -0
- package/src/node-transports.ts +86 -49
- package/src/owner-anchor-sync.ts +172 -172
- package/src/owner-grant-protocol.ts +547 -547
- package/src/owner-key-export.ts +348 -348
- package/src/owner-key-flows.ts +109 -109
- package/src/owner-key-store.ts +1 -1
- package/src/owner-keys.ts +179 -179
- package/src/owner-signer.ts +92 -92
- package/src/peer-addr-book.ts +252 -252
- package/src/peer-authorization.ts +584 -582
- package/src/peer-dial.ts +698 -605
- package/src/peer-join-backfill.ts +961 -961
- package/src/peer-record.ts +71 -16
- package/src/pending-join-runner.ts +798 -798
- package/src/primary-lan-address.ts +47 -47
- package/src/provider/client.ts +327 -326
- package/src/provider/descriptor.ts +129 -129
- package/src/provider/index.ts +59 -59
- package/src/provider/oauth.ts +285 -285
- package/src/provider/rent-node.ts +276 -275
- package/src/relay-addrs.ts +90 -12
- package/src/relay-reservation.ts +1 -1
- package/src/relay-server.ts +99 -98
- package/src/schema-verification.ts +38 -23
- package/src/sealed-party-key.ts +190 -190
- package/src/seed-bootstrap.ts +8 -11
- package/src/seed-trust-policy.ts +353 -353
- package/src/self-address-update.ts +19 -19
- package/src/strand-addr-protocol.ts +17 -14
- package/src/strand-app-schema-report.ts +147 -122
- package/src/strand-cohort.ts +1 -5
- package/src/strand-commit-durability.ts +165 -165
- package/src/strand-database.ts +522 -510
- package/src/strand-first-sync-gate.ts +3 -2
- package/src/strand-formation-deadlines.ts +227 -227
- package/src/strand-formation-manager.ts +731 -731
- package/src/strand-formation-protocol.ts +1174 -1138
- package/src/strand-fret-addrs.ts +179 -179
- package/src/strand-instance-manager.ts +381 -85
- package/src/strand-membership-reconciler.ts +1315 -1310
- package/src/strand-membership-writer.ts +2152 -2150
- package/src/strand-move.ts +1 -1
- package/src/strand-network-config.ts +16 -6
- package/src/strand-offline.ts +178 -178
- package/src/strand-party-key-rotation.ts +440 -436
- package/src/strand-party-machines.ts +308 -293
- package/src/strand-party-peers.ts +151 -129
- package/src/strand-peer-redial.ts +1 -1
- package/src/strand-reactivity.ts +36 -19
- package/src/strand-revocation-enforcer.ts +758 -758
- package/src/strand-wake-protocol.ts +406 -394
- package/src/strand-watcher.ts +1 -1
- package/src/table-change-watch.ts +285 -285
- package/src/timed-step.ts +28 -28
- package/src/timeout-scheduler.ts +29 -29
- package/src/trusted-owner-store.ts +242 -242
- package/src/types.ts +3182 -3112
- package/src/webrtc-upgrade.ts +439 -0
|
@@ -1,266 +1,266 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Carrying the strands of the party this app founded into the party it joins
|
|
3
|
-
* (docs/
|
|
4
|
-
*
|
|
5
|
-
* The app runs one node, so cadre-core's two ends of a move run on two nodes, one after the
|
|
6
|
-
* other: `exportStrandBundle` on the left party's node before the switch, `adoptStrandBundle` on
|
|
7
|
-
* the joined party's node once the app's key is wired as an owner there. Between them the bundle
|
|
8
|
-
* is kept here, one record per left party, so a join killed partway, or an owner key wired only
|
|
9
|
-
* on a later launch, finishes the move on a later start. The bundle carries read secrets, party
|
|
10
|
-
* keys and bearer invitations, so the record lives in the key store beside the identity.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { adoptStrandBundle, StrandMoveNotOwnerError } from '../strand-move.js';
|
|
14
|
-
import type { StrandBundle, StrandMoveReport } from '../strand-move.js';
|
|
15
|
-
import type { CadreNode } from '../cadre-node.js';
|
|
16
|
-
import type { KeyId, KeyStore } from '../key-store.js';
|
|
17
|
-
import { isOwnerGrant, type JoinCredential } from './join-credential.js';
|
|
18
|
-
import { toError } from '../describe-thrown.js';
|
|
19
|
-
import { toString as uint8ArrayToString, fromString as uint8ArrayFromString } from 'uint8arrays';
|
|
20
|
-
|
|
21
|
-
/** How a join's strand move settled: `JoinCadreResult.strandMove`. */
|
|
22
|
-
export type StrandMoveOutcome =
|
|
23
|
-
/** Adopted. `partial`: `report.failed` lists what the next start, or `finishStrandMove`, retries. */
|
|
24
|
-
| { state: 'done' | 'partial'; report: StrandMoveReport }
|
|
25
|
-
/** Not adopted yet: the app's key is not wired as an owner of the joined party. A start after it is adopts. */
|
|
26
|
-
| { state: 'deferred'; reason: 'owner-key-not-wired' }
|
|
27
|
-
/** Not adopted yet: the adoption threw (logged). The next start, or `finishStrandMove`, retries. */
|
|
28
|
-
| { state: 'deferred'; reason: 'adoption-failed'; error: Error };
|
|
29
|
-
|
|
30
|
-
/** A move still waiting: `EdgeNode.pendingStrandMove`. */
|
|
31
|
-
export interface PendingStrandMove {
|
|
32
|
-
fromPartyId: string;
|
|
33
|
-
/** The party the strands move into; the record is adopted there and nowhere else. */
|
|
34
|
-
toPartyId: string;
|
|
35
|
-
/** Strands of `fromPartyId` not seated in `toPartyId` yet. */
|
|
36
|
-
strandIds: string[];
|
|
37
|
-
/** Strands `toPartyId` refused permanently so far: it participates in them under its own identity. */
|
|
38
|
-
refused: StrandMoveReport['refused'];
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* `EdgeNode.joinCadre` refused before anything stopped: the party this app founded holds strands,
|
|
43
|
-
* pending joins or invitations, and a machine invitation does not make the app an owner of the
|
|
44
|
-
* party it joins. Every row a move seats there is owner-signed, so a member's join would leave
|
|
45
|
-
* them in a party the app no longer runs. Joining with an owner grant moves them.
|
|
46
|
-
*/
|
|
47
|
-
export class StrandsNeedOwnerJoinError extends Error {
|
|
48
|
-
readonly fromPartyId: string;
|
|
49
|
-
readonly toPartyId: string;
|
|
50
|
-
readonly strandCount: number;
|
|
51
|
-
|
|
52
|
-
constructor(bundle: StrandBundle, toPartyId: string) {
|
|
53
|
-
super(
|
|
54
|
-
`Cannot join party ${toPartyId} as a member: ${describeContent(bundle)} of party ${bundle.partyId} ` +
|
|
55
|
-
'would be left behind, and only an owner can move them. Join with an owner grant instead, made under the owner app\'s Owner keys.',
|
|
56
|
-
);
|
|
57
|
-
this.name = 'StrandsNeedOwnerJoinError';
|
|
58
|
-
this.fromPartyId = bundle.partyId;
|
|
59
|
-
this.toPartyId = toPartyId;
|
|
60
|
-
this.strandCount = bundle.strands.length;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/** What the key store holds for one left party between the export and a complete adoption. */
|
|
65
|
-
interface StrandMoveRecord {
|
|
66
|
-
v: typeof RECORD_VERSION;
|
|
67
|
-
targetPartyId: string;
|
|
68
|
-
bundle: StrandBundle;
|
|
69
|
-
/** Strand ids an earlier adoption seated. Never left out of a retry: an invitation bound to one needs it under `moved`. */
|
|
70
|
-
moved: string[];
|
|
71
|
-
/** Permanent refusals from earlier adoptions, left out of a retry and reported each time. */
|
|
72
|
-
refused: StrandMoveReport['refused'];
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const RECORD_VERSION = 1;
|
|
76
|
-
|
|
77
|
-
/** Key-store id prefix of every move record; the left party's id follows, base64url. */
|
|
78
|
-
const SLOT_PREFIX = 'cadre/strand-move/';
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* A persistence contract: `cadre/strand-move/<base64url of the party id's UTF-8 bytes>`, the
|
|
82
|
-
* id the React Native kit wrote before the records moved here (its `secureStoreKeySegment`
|
|
83
|
-
* encodes the same way), so an installed phone's record is read back.
|
|
84
|
-
*/
|
|
85
|
-
function slotId(fromPartyId: string): KeyId {
|
|
86
|
-
return SLOT_PREFIX + uint8ArrayToString(uint8ArrayFromString(fromPartyId, 'utf8'), 'base64url');
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
/** The move records, one key-store slot per left party. A read the key store refuses throws; it is never read as "no record". */
|
|
90
|
-
export class StrandMoveRecords {
|
|
91
|
-
constructor(private readonly keyStore: KeyStore) {}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
* Replaces the left party's record: a newer export is the fuller one.
|
|
95
|
-
*
|
|
96
|
-
* NOTE: on a phone, a bundle with a routing table per strand runs past expo-secure-store's
|
|
97
|
-
* 2048-byte value guideline; version 14 warns and stores it, and the iOS Keychain and Android's
|
|
98
|
-
* encrypted preferences hold more. If a later SDK throws instead, keep the routing tables
|
|
99
|
-
* (not secret) in the node-local store and only the secrets here.
|
|
100
|
-
*/
|
|
101
|
-
save(record: StrandMoveRecord): Promise<void> {
|
|
102
|
-
return this.keyStore.set(slotId(record.bundle.partyId), uint8ArrayFromString(JSON.stringify(record), 'utf8'));
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
load(fromPartyId: string): Promise<StrandMoveRecord | undefined> {
|
|
106
|
-
return this.read(slotId(fromPartyId));
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
remove(fromPartyId: string): Promise<void> {
|
|
110
|
-
return this.keyStore.delete(slotId(fromPartyId));
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
/** Every record naming `toPartyId`, in slot order. */
|
|
114
|
-
async naming(toPartyId: string): Promise<StrandMoveRecord[]> {
|
|
115
|
-
const slots = (await this.keyStore.list()).filter((slot) => slot.startsWith(SLOT_PREFIX)).sort();
|
|
116
|
-
const records: StrandMoveRecord[] = [];
|
|
117
|
-
for (const slot of slots) {
|
|
118
|
-
const record = await this.read(slot);
|
|
119
|
-
if (record?.targetPartyId === toPartyId) records.push(record);
|
|
120
|
-
}
|
|
121
|
-
return records;
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/**
|
|
125
|
-
* The record in `slot`. One that does not parse is deleted with a warning: nothing can adopt
|
|
126
|
-
* it, and the strands are still in the left party's storage. Its absence then reads as
|
|
127
|
-
* "nothing waiting", so code that deletes that storage must not take a missing record alone
|
|
128
|
-
* as proof that every strand reached the joined party.
|
|
129
|
-
*/
|
|
130
|
-
private async read(slot: KeyId): Promise<StrandMoveRecord | undefined> {
|
|
131
|
-
const bytes = await this.keyStore.get(slot);
|
|
132
|
-
if (bytes === undefined) return undefined;
|
|
133
|
-
const record = parseRecord(uint8ArrayToString(bytes, 'utf8'));
|
|
134
|
-
if (record !== undefined && slotId(record.bundle.partyId) === slot) return record;
|
|
135
|
-
console.warn(`[cadre-core/edge-node] deleting the strand move record in ${slot}: it is not one this kit wrote`);
|
|
136
|
-
try {
|
|
137
|
-
await this.keyStore.delete(slot);
|
|
138
|
-
} catch (err) {
|
|
139
|
-
// Read as absent either way; failing every start over a record nothing can use would be worse.
|
|
140
|
-
console.warn(`[cadre-core/edge-node] could not delete the strand move record in ${slot}:`, err);
|
|
141
|
-
}
|
|
142
|
-
return undefined;
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
/**
|
|
147
|
-
* The record a join writes before the switch, or `undefined` when the bundle holds nothing to
|
|
148
|
-
* move. Throws {@link StrandsNeedOwnerJoinError} when it does and `credential` is a machine
|
|
149
|
-
* invitation, which does not make the app an owner of the party it joins.
|
|
150
|
-
*/
|
|
151
|
-
export function joinMoveRecord(bundle: StrandBundle, credential: JoinCredential): StrandMoveRecord | undefined {
|
|
152
|
-
if (bundle.strands.length + bundle.pendingJoins.length + bundle.formationInvites.length === 0) return undefined;
|
|
153
|
-
if (!isOwnerGrant(credential)) throw new StrandsNeedOwnerJoinError(bundle, credential.partyId);
|
|
154
|
-
return { v: RECORD_VERSION, targetPartyId: credential.partyId, bundle, moved: [], refused: [] };
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/**
|
|
158
|
-
* Adopt `record` on `node`, running in the record's target party, without the strands an earlier
|
|
159
|
-
* attempt had refused; then delete the record when nothing failed, or save this attempt's
|
|
160
|
-
* progress. The report lists every refusal so far. Throws what `adoptStrandBundle` throws. A
|
|
161
|
-
* failure updating the record is only logged: the record left behind is adopted again, without
|
|
162
|
-
* harm, by the next start.
|
|
163
|
-
*/
|
|
164
|
-
export async function adoptMoveRecord(node: CadreNode, records: StrandMoveRecords, record: StrandMoveRecord): Promise<StrandMoveReport> {
|
|
165
|
-
const refused = new Set(record.refused.map((entry) => entry.strandId));
|
|
166
|
-
const strands = record.bundle.strands.filter((entry) => !refused.has(entry.row.Id));
|
|
167
|
-
const adopted = await adoptStrandBundle(node, { ...record.bundle, strands });
|
|
168
|
-
const report: StrandMoveReport = { ...adopted, refused: [...record.refused, ...adopted.refused] };
|
|
169
|
-
await keepWhatIsLeft(records, record, report);
|
|
170
|
-
return report;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
async function keepWhatIsLeft(records: StrandMoveRecords, record: StrandMoveRecord, report: StrandMoveReport): Promise<void> {
|
|
174
|
-
const fromPartyId = record.bundle.partyId;
|
|
175
|
-
try {
|
|
176
|
-
if (report.failed.length === 0) {
|
|
177
|
-
await records.remove(fromPartyId);
|
|
178
|
-
} else {
|
|
179
|
-
const moved = [...new Set([...record.moved, ...report.moved])];
|
|
180
|
-
await records.save({ ...record, moved, refused: report.refused });
|
|
181
|
-
}
|
|
182
|
-
} catch (err) {
|
|
183
|
-
console.warn(`[cadre-core/edge-node] could not update the strand move record of party ${fromPartyId}; the next start adopts it again:`, err);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
|
|
187
|
-
/**
|
|
188
|
-
* Adopt every record naming the node's party, by left party. A record the key store refuses to
|
|
189
|
-
* read throws; an adoption that throws is logged and reported as deferred, and its record kept.
|
|
190
|
-
*/
|
|
191
|
-
export async function adoptMovesInto(node: CadreNode, records: StrandMoveRecords): Promise<Map<string, StrandMoveOutcome>> {
|
|
192
|
-
const outcomes = new Map<string, StrandMoveOutcome>();
|
|
193
|
-
for (const record of await records.naming(node.partyId)) {
|
|
194
|
-
outcomes.set(record.bundle.partyId, await adoptOrDefer(node, records, record));
|
|
195
|
-
}
|
|
196
|
-
return outcomes;
|
|
197
|
-
}
|
|
198
|
-
|
|
199
|
-
async function adoptOrDefer(node: CadreNode, records: StrandMoveRecords, record: StrandMoveRecord): Promise<StrandMoveOutcome> {
|
|
200
|
-
try {
|
|
201
|
-
const report = await adoptMoveRecord(node, records, record);
|
|
202
|
-
return { state: report.failed.length === 0 ? 'done' : 'partial', report };
|
|
203
|
-
} catch (err) {
|
|
204
|
-
console.warn(`[cadre-core/edge-node] moving the strands of party ${record.bundle.partyId} failed; the next start retries:`, err);
|
|
205
|
-
return deferredOutcome(err);
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
export function deferredOutcome(err: unknown): StrandMoveOutcome {
|
|
210
|
-
if (err instanceof StrandMoveNotOwnerError) return { state: 'deferred', reason: 'owner-key-not-wired' };
|
|
211
|
-
return { state: 'deferred', reason: 'adoption-failed', error: toError(err) };
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
export function pendingMove(record: StrandMoveRecord): PendingStrandMove {
|
|
215
|
-
const settled = new Set([...record.moved, ...record.refused.map((entry) => entry.strandId)]);
|
|
216
|
-
return {
|
|
217
|
-
fromPartyId: record.bundle.partyId,
|
|
218
|
-
toPartyId: record.targetPartyId,
|
|
219
|
-
strandIds: record.bundle.strands.map((entry) => entry.row.Id).filter((strandId) => !settled.has(strandId)),
|
|
220
|
-
refused: record.refused,
|
|
221
|
-
};
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/** The record in `text`, or `undefined` (logged) when it is not one. */
|
|
225
|
-
function parseRecord(text: string): StrandMoveRecord | undefined {
|
|
226
|
-
let raw: unknown;
|
|
227
|
-
try {
|
|
228
|
-
raw = JSON.parse(text);
|
|
229
|
-
} catch (err) {
|
|
230
|
-
console.warn('[cadre-core/edge-node] a strand move record is not JSON:', err);
|
|
231
|
-
return undefined;
|
|
232
|
-
}
|
|
233
|
-
return isRecord(raw) ? raw : undefined;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
/** The record's shape down to the bundle's lists; their entries are cadre-core's export, written by this module. */
|
|
237
|
-
function isRecord(value: unknown): value is StrandMoveRecord {
|
|
238
|
-
if (!isObject(value) || value.v !== RECORD_VERSION || typeof value.targetPartyId !== 'string') return false;
|
|
239
|
-
if (!Array.isArray(value.moved) || !Array.isArray(value.refused)) return false;
|
|
240
|
-
const bundle = value.bundle;
|
|
241
|
-
return isObject(bundle)
|
|
242
|
-
&& bundle.v === 1
|
|
243
|
-
&& typeof bundle.partyId === 'string'
|
|
244
|
-
&& Array.isArray(bundle.strands)
|
|
245
|
-
&& Array.isArray(bundle.pendingJoins)
|
|
246
|
-
&& Array.isArray(bundle.formationInvites);
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
function isObject(value: unknown): value is Record<string, unknown> {
|
|
250
|
-
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/** "2 strands and 1 invitation": the bundle's non-empty lists. */
|
|
254
|
-
function describeContent(bundle: StrandBundle): string {
|
|
255
|
-
const parts = [
|
|
256
|
-
count(bundle.strands.length, 'strand'),
|
|
257
|
-
count(bundle.pendingJoins.length, 'pending join'),
|
|
258
|
-
count(bundle.formationInvites.length, 'invitation'),
|
|
259
|
-
].filter((part): part is string => part !== null);
|
|
260
|
-
return parts.length > 1 ? `${parts.slice(0, -1).join(', ')} and ${parts[parts.length - 1]}` : parts.join('');
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function count(n: number, noun: string): string | null {
|
|
264
|
-
if (n === 0) return null;
|
|
265
|
-
return `${n} ${noun}${n === 1 ? '' : 's'}`;
|
|
266
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Carrying the strands of the party this app founded into the party it joins
|
|
3
|
+
* (docs/cadre/apps.md → "Moving the app's strands").
|
|
4
|
+
*
|
|
5
|
+
* The app runs one node, so cadre-core's two ends of a move run on two nodes, one after the
|
|
6
|
+
* other: `exportStrandBundle` on the left party's node before the switch, `adoptStrandBundle` on
|
|
7
|
+
* the joined party's node once the app's key is wired as an owner there. Between them the bundle
|
|
8
|
+
* is kept here, one record per left party, so a join killed partway, or an owner key wired only
|
|
9
|
+
* on a later launch, finishes the move on a later start. The bundle carries read secrets, party
|
|
10
|
+
* keys and bearer invitations, so the record lives in the key store beside the identity.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { adoptStrandBundle, StrandMoveNotOwnerError } from '../strand-move.js';
|
|
14
|
+
import type { StrandBundle, StrandMoveReport } from '../strand-move.js';
|
|
15
|
+
import type { CadreNode } from '../cadre-node.js';
|
|
16
|
+
import type { KeyId, KeyStore } from '../key-store.js';
|
|
17
|
+
import { isOwnerGrant, type JoinCredential } from './join-credential.js';
|
|
18
|
+
import { toError } from '../describe-thrown.js';
|
|
19
|
+
import { toString as uint8ArrayToString, fromString as uint8ArrayFromString } from 'uint8arrays';
|
|
20
|
+
|
|
21
|
+
/** How a join's strand move settled: `JoinCadreResult.strandMove`. */
|
|
22
|
+
export type StrandMoveOutcome =
|
|
23
|
+
/** Adopted. `partial`: `report.failed` lists what the next start, or `finishStrandMove`, retries. */
|
|
24
|
+
| { state: 'done' | 'partial'; report: StrandMoveReport }
|
|
25
|
+
/** Not adopted yet: the app's key is not wired as an owner of the joined party. A start after it is adopts. */
|
|
26
|
+
| { state: 'deferred'; reason: 'owner-key-not-wired' }
|
|
27
|
+
/** Not adopted yet: the adoption threw (logged). The next start, or `finishStrandMove`, retries. */
|
|
28
|
+
| { state: 'deferred'; reason: 'adoption-failed'; error: Error };
|
|
29
|
+
|
|
30
|
+
/** A move still waiting: `EdgeNode.pendingStrandMove`. */
|
|
31
|
+
export interface PendingStrandMove {
|
|
32
|
+
fromPartyId: string;
|
|
33
|
+
/** The party the strands move into; the record is adopted there and nowhere else. */
|
|
34
|
+
toPartyId: string;
|
|
35
|
+
/** Strands of `fromPartyId` not seated in `toPartyId` yet. */
|
|
36
|
+
strandIds: string[];
|
|
37
|
+
/** Strands `toPartyId` refused permanently so far: it participates in them under its own identity. */
|
|
38
|
+
refused: StrandMoveReport['refused'];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* `EdgeNode.joinCadre` refused before anything stopped: the party this app founded holds strands,
|
|
43
|
+
* pending joins or invitations, and a machine invitation does not make the app an owner of the
|
|
44
|
+
* party it joins. Every row a move seats there is owner-signed, so a member's join would leave
|
|
45
|
+
* them in a party the app no longer runs. Joining with an owner grant moves them.
|
|
46
|
+
*/
|
|
47
|
+
export class StrandsNeedOwnerJoinError extends Error {
|
|
48
|
+
readonly fromPartyId: string;
|
|
49
|
+
readonly toPartyId: string;
|
|
50
|
+
readonly strandCount: number;
|
|
51
|
+
|
|
52
|
+
constructor(bundle: StrandBundle, toPartyId: string) {
|
|
53
|
+
super(
|
|
54
|
+
`Cannot join party ${toPartyId} as a member: ${describeContent(bundle)} of party ${bundle.partyId} ` +
|
|
55
|
+
'would be left behind, and only an owner can move them. Join with an owner grant instead, made under the owner app\'s Owner keys.',
|
|
56
|
+
);
|
|
57
|
+
this.name = 'StrandsNeedOwnerJoinError';
|
|
58
|
+
this.fromPartyId = bundle.partyId;
|
|
59
|
+
this.toPartyId = toPartyId;
|
|
60
|
+
this.strandCount = bundle.strands.length;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** What the key store holds for one left party between the export and a complete adoption. */
|
|
65
|
+
interface StrandMoveRecord {
|
|
66
|
+
v: typeof RECORD_VERSION;
|
|
67
|
+
targetPartyId: string;
|
|
68
|
+
bundle: StrandBundle;
|
|
69
|
+
/** Strand ids an earlier adoption seated. Never left out of a retry: an invitation bound to one needs it under `moved`. */
|
|
70
|
+
moved: string[];
|
|
71
|
+
/** Permanent refusals from earlier adoptions, left out of a retry and reported each time. */
|
|
72
|
+
refused: StrandMoveReport['refused'];
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
const RECORD_VERSION = 1;
|
|
76
|
+
|
|
77
|
+
/** Key-store id prefix of every move record; the left party's id follows, base64url. */
|
|
78
|
+
const SLOT_PREFIX = 'cadre/strand-move/';
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A persistence contract: `cadre/strand-move/<base64url of the party id's UTF-8 bytes>`, the
|
|
82
|
+
* id the React Native kit wrote before the records moved here (its `secureStoreKeySegment`
|
|
83
|
+
* encodes the same way), so an installed phone's record is read back.
|
|
84
|
+
*/
|
|
85
|
+
function slotId(fromPartyId: string): KeyId {
|
|
86
|
+
return SLOT_PREFIX + uint8ArrayToString(uint8ArrayFromString(fromPartyId, 'utf8'), 'base64url');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/** The move records, one key-store slot per left party. A read the key store refuses throws; it is never read as "no record". */
|
|
90
|
+
export class StrandMoveRecords {
|
|
91
|
+
constructor(private readonly keyStore: KeyStore) {}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Replaces the left party's record: a newer export is the fuller one.
|
|
95
|
+
*
|
|
96
|
+
* NOTE: on a phone, a bundle with a routing table per strand runs past expo-secure-store's
|
|
97
|
+
* 2048-byte value guideline; version 14 warns and stores it, and the iOS Keychain and Android's
|
|
98
|
+
* encrypted preferences hold more. If a later SDK throws instead, keep the routing tables
|
|
99
|
+
* (not secret) in the node-local store and only the secrets here.
|
|
100
|
+
*/
|
|
101
|
+
save(record: StrandMoveRecord): Promise<void> {
|
|
102
|
+
return this.keyStore.set(slotId(record.bundle.partyId), uint8ArrayFromString(JSON.stringify(record), 'utf8'));
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
load(fromPartyId: string): Promise<StrandMoveRecord | undefined> {
|
|
106
|
+
return this.read(slotId(fromPartyId));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
remove(fromPartyId: string): Promise<void> {
|
|
110
|
+
return this.keyStore.delete(slotId(fromPartyId));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/** Every record naming `toPartyId`, in slot order. */
|
|
114
|
+
async naming(toPartyId: string): Promise<StrandMoveRecord[]> {
|
|
115
|
+
const slots = (await this.keyStore.list()).filter((slot) => slot.startsWith(SLOT_PREFIX)).sort();
|
|
116
|
+
const records: StrandMoveRecord[] = [];
|
|
117
|
+
for (const slot of slots) {
|
|
118
|
+
const record = await this.read(slot);
|
|
119
|
+
if (record?.targetPartyId === toPartyId) records.push(record);
|
|
120
|
+
}
|
|
121
|
+
return records;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* The record in `slot`. One that does not parse is deleted with a warning: nothing can adopt
|
|
126
|
+
* it, and the strands are still in the left party's storage. Its absence then reads as
|
|
127
|
+
* "nothing waiting", so code that deletes that storage must not take a missing record alone
|
|
128
|
+
* as proof that every strand reached the joined party.
|
|
129
|
+
*/
|
|
130
|
+
private async read(slot: KeyId): Promise<StrandMoveRecord | undefined> {
|
|
131
|
+
const bytes = await this.keyStore.get(slot);
|
|
132
|
+
if (bytes === undefined) return undefined;
|
|
133
|
+
const record = parseRecord(uint8ArrayToString(bytes, 'utf8'));
|
|
134
|
+
if (record !== undefined && slotId(record.bundle.partyId) === slot) return record;
|
|
135
|
+
console.warn(`[cadre-core/edge-node] deleting the strand move record in ${slot}: it is not one this kit wrote`);
|
|
136
|
+
try {
|
|
137
|
+
await this.keyStore.delete(slot);
|
|
138
|
+
} catch (err) {
|
|
139
|
+
// Read as absent either way; failing every start over a record nothing can use would be worse.
|
|
140
|
+
console.warn(`[cadre-core/edge-node] could not delete the strand move record in ${slot}:`, err);
|
|
141
|
+
}
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* The record a join writes before the switch, or `undefined` when the bundle holds nothing to
|
|
148
|
+
* move. Throws {@link StrandsNeedOwnerJoinError} when it does and `credential` is a machine
|
|
149
|
+
* invitation, which does not make the app an owner of the party it joins.
|
|
150
|
+
*/
|
|
151
|
+
export function joinMoveRecord(bundle: StrandBundle, credential: JoinCredential): StrandMoveRecord | undefined {
|
|
152
|
+
if (bundle.strands.length + bundle.pendingJoins.length + bundle.formationInvites.length === 0) return undefined;
|
|
153
|
+
if (!isOwnerGrant(credential)) throw new StrandsNeedOwnerJoinError(bundle, credential.partyId);
|
|
154
|
+
return { v: RECORD_VERSION, targetPartyId: credential.partyId, bundle, moved: [], refused: [] };
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Adopt `record` on `node`, running in the record's target party, without the strands an earlier
|
|
159
|
+
* attempt had refused; then delete the record when nothing failed, or save this attempt's
|
|
160
|
+
* progress. The report lists every refusal so far. Throws what `adoptStrandBundle` throws. A
|
|
161
|
+
* failure updating the record is only logged: the record left behind is adopted again, without
|
|
162
|
+
* harm, by the next start.
|
|
163
|
+
*/
|
|
164
|
+
export async function adoptMoveRecord(node: CadreNode, records: StrandMoveRecords, record: StrandMoveRecord): Promise<StrandMoveReport> {
|
|
165
|
+
const refused = new Set(record.refused.map((entry) => entry.strandId));
|
|
166
|
+
const strands = record.bundle.strands.filter((entry) => !refused.has(entry.row.Id));
|
|
167
|
+
const adopted = await adoptStrandBundle(node, { ...record.bundle, strands });
|
|
168
|
+
const report: StrandMoveReport = { ...adopted, refused: [...record.refused, ...adopted.refused] };
|
|
169
|
+
await keepWhatIsLeft(records, record, report);
|
|
170
|
+
return report;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async function keepWhatIsLeft(records: StrandMoveRecords, record: StrandMoveRecord, report: StrandMoveReport): Promise<void> {
|
|
174
|
+
const fromPartyId = record.bundle.partyId;
|
|
175
|
+
try {
|
|
176
|
+
if (report.failed.length === 0) {
|
|
177
|
+
await records.remove(fromPartyId);
|
|
178
|
+
} else {
|
|
179
|
+
const moved = [...new Set([...record.moved, ...report.moved])];
|
|
180
|
+
await records.save({ ...record, moved, refused: report.refused });
|
|
181
|
+
}
|
|
182
|
+
} catch (err) {
|
|
183
|
+
console.warn(`[cadre-core/edge-node] could not update the strand move record of party ${fromPartyId}; the next start adopts it again:`, err);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Adopt every record naming the node's party, by left party. A record the key store refuses to
|
|
189
|
+
* read throws; an adoption that throws is logged and reported as deferred, and its record kept.
|
|
190
|
+
*/
|
|
191
|
+
export async function adoptMovesInto(node: CadreNode, records: StrandMoveRecords): Promise<Map<string, StrandMoveOutcome>> {
|
|
192
|
+
const outcomes = new Map<string, StrandMoveOutcome>();
|
|
193
|
+
for (const record of await records.naming(node.partyId)) {
|
|
194
|
+
outcomes.set(record.bundle.partyId, await adoptOrDefer(node, records, record));
|
|
195
|
+
}
|
|
196
|
+
return outcomes;
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function adoptOrDefer(node: CadreNode, records: StrandMoveRecords, record: StrandMoveRecord): Promise<StrandMoveOutcome> {
|
|
200
|
+
try {
|
|
201
|
+
const report = await adoptMoveRecord(node, records, record);
|
|
202
|
+
return { state: report.failed.length === 0 ? 'done' : 'partial', report };
|
|
203
|
+
} catch (err) {
|
|
204
|
+
console.warn(`[cadre-core/edge-node] moving the strands of party ${record.bundle.partyId} failed; the next start retries:`, err);
|
|
205
|
+
return deferredOutcome(err);
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
export function deferredOutcome(err: unknown): StrandMoveOutcome {
|
|
210
|
+
if (err instanceof StrandMoveNotOwnerError) return { state: 'deferred', reason: 'owner-key-not-wired' };
|
|
211
|
+
return { state: 'deferred', reason: 'adoption-failed', error: toError(err) };
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function pendingMove(record: StrandMoveRecord): PendingStrandMove {
|
|
215
|
+
const settled = new Set([...record.moved, ...record.refused.map((entry) => entry.strandId)]);
|
|
216
|
+
return {
|
|
217
|
+
fromPartyId: record.bundle.partyId,
|
|
218
|
+
toPartyId: record.targetPartyId,
|
|
219
|
+
strandIds: record.bundle.strands.map((entry) => entry.row.Id).filter((strandId) => !settled.has(strandId)),
|
|
220
|
+
refused: record.refused,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** The record in `text`, or `undefined` (logged) when it is not one. */
|
|
225
|
+
function parseRecord(text: string): StrandMoveRecord | undefined {
|
|
226
|
+
let raw: unknown;
|
|
227
|
+
try {
|
|
228
|
+
raw = JSON.parse(text);
|
|
229
|
+
} catch (err) {
|
|
230
|
+
console.warn('[cadre-core/edge-node] a strand move record is not JSON:', err);
|
|
231
|
+
return undefined;
|
|
232
|
+
}
|
|
233
|
+
return isRecord(raw) ? raw : undefined;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
/** The record's shape down to the bundle's lists; their entries are cadre-core's export, written by this module. */
|
|
237
|
+
function isRecord(value: unknown): value is StrandMoveRecord {
|
|
238
|
+
if (!isObject(value) || value.v !== RECORD_VERSION || typeof value.targetPartyId !== 'string') return false;
|
|
239
|
+
if (!Array.isArray(value.moved) || !Array.isArray(value.refused)) return false;
|
|
240
|
+
const bundle = value.bundle;
|
|
241
|
+
return isObject(bundle)
|
|
242
|
+
&& bundle.v === 1
|
|
243
|
+
&& typeof bundle.partyId === 'string'
|
|
244
|
+
&& Array.isArray(bundle.strands)
|
|
245
|
+
&& Array.isArray(bundle.pendingJoins)
|
|
246
|
+
&& Array.isArray(bundle.formationInvites);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function isObject(value: unknown): value is Record<string, unknown> {
|
|
250
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** "2 strands and 1 invitation": the bundle's non-empty lists. */
|
|
254
|
+
function describeContent(bundle: StrandBundle): string {
|
|
255
|
+
const parts = [
|
|
256
|
+
count(bundle.strands.length, 'strand'),
|
|
257
|
+
count(bundle.pendingJoins.length, 'pending join'),
|
|
258
|
+
count(bundle.formationInvites.length, 'invitation'),
|
|
259
|
+
].filter((part): part is string => part !== null);
|
|
260
|
+
return parts.length > 1 ? `${parts.slice(0, -1).join(', ')} and ${parts[parts.length - 1]}` : parts.join('');
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
function count(n: number, noun: string): string | null {
|
|
264
|
+
if (n === 0) return null;
|
|
265
|
+
return `${n} ${noun}${n === 1 ? '' : 's'}`;
|
|
266
|
+
}
|
package/src/edge-node/strands.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { StrandAwaitingFirstSyncError } from '../strand-first-sync-gate.js';
|
|
6
6
|
import type { CadreNode } from '../cadre-node.js';
|
|
7
7
|
import type { StrandConfig, StrandInstance } from '../types.js';
|
|
8
|
-
import {
|
|
8
|
+
import { causeChain, reportsUnansweredShortfall } from '../control-retry.js';
|
|
9
9
|
|
|
10
10
|
/** Default patience for {@link attachStrandWhenWritable}: generous enough that running out means something is wrong. */
|
|
11
11
|
export const DEFAULT_STRAND_PATIENCE_MS = 240_000;
|
|
@@ -37,16 +37,6 @@ export async function attachStrandWhenWritable(
|
|
|
37
37
|
}
|
|
38
38
|
}
|
|
39
39
|
|
|
40
|
-
/**
|
|
41
|
-
* Optimystic's cluster coordinator reports a write that could not gather enough approvals this
|
|
42
|
-
* way. It is transient right after a node restarts alone: its cohort has not reconnected yet.
|
|
43
|
-
*
|
|
44
|
-
* NOTE: matched on the message. Optimystic 1.20.0 raises this failure as `PromiseShortfallError` and
|
|
45
|
-
* keeps the sentence unchanged as a contract, so the match holds; moving to the type is the second arm
|
|
46
|
-
* of backlog `debt-control-write-retry-matches-the-shortfall-by-type`.
|
|
47
|
-
*/
|
|
48
|
-
const SUPER_MAJORITY_FAILURE = /Failed to get super-majority/i;
|
|
49
|
-
|
|
50
40
|
/**
|
|
51
41
|
* Run a write, retrying only on the transient super-majority failure the first writes after a lone
|
|
52
42
|
* restart hit (v1.9 release notes): `attempts` tries, `delayMs` apart. Any other error, and the
|
|
@@ -69,6 +59,31 @@ export async function retryAfterRestart<T>(
|
|
|
69
59
|
}
|
|
70
60
|
}
|
|
71
61
|
|
|
62
|
+
/**
|
|
63
|
+
* Optimystic's cluster coordinator reporting a write too few of the cohort answered. Transient right
|
|
64
|
+
* after a node restarts alone: its cohort has not reconnected yet. A lone node's own refusal to
|
|
65
|
+
* write by itself is claimed too — `CoordinatorRepo.refuseSoloPend` builds the shortfall with
|
|
66
|
+
* `approvals: 1, rejections: 0`, a zero rejection count like any silent-cohort shortfall, and
|
|
67
|
+
* `soloRefused` naming why, which nothing here branches on.
|
|
68
|
+
*
|
|
69
|
+
* The SAME predicate the control-write classifier claims the shortfall with
|
|
70
|
+
* ({@link reportsUnansweredShortfall}), so the funnel every control write passes through and this
|
|
71
|
+
* helper cannot drift apart on which shortfall is worth re-presenting. What this helper does NOT
|
|
72
|
+
* share is that funnel's commit-phase veto: it wraps an app's own write, with no transactor
|
|
73
|
+
* aggregate around it to read a phase off.
|
|
74
|
+
*
|
|
75
|
+
* Matching the class rather than the sentence NARROWS this helper, deliberately. A shortfall
|
|
76
|
+
* carrying a REJECTION is no longer claimed (a member voted no, and no backoff changes that), nor is
|
|
77
|
+
* a non-`Error` throw, which can carry no typed link at all — and this helper sleeps 3 s between
|
|
78
|
+
* five attempts, so claiming one on its text would spend 12 s before the identical failure.
|
|
79
|
+
*
|
|
80
|
+
* It does not measurably widen the reach, despite now walking the chain: the old match read
|
|
81
|
+
* `describeThrown(err)`, the top-level `message` alone, but every wrapper between the raise site and
|
|
82
|
+
* an app interpolates the inner message into its own, so the sentence was visible there anyway.
|
|
83
|
+
* Reaching the typed link at any depth is what makes the claim independent of that — an upstream
|
|
84
|
+
* rewording, or a wrapper that summarises instead of interpolating, no longer silently disables the
|
|
85
|
+
* retry.
|
|
86
|
+
*/
|
|
72
87
|
function isTransientWriteFailure(err: unknown): boolean {
|
|
73
|
-
return
|
|
88
|
+
return reportsUnansweredShortfall(causeChain(err));
|
|
74
89
|
}
|