@serfab/cadre-core 1.19.0 → 1.20.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/dist/cadre-node.d.ts +118 -16
- package/dist/cadre-node.js +210 -31
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-database.d.ts +2 -1
- package/dist/control-database.js +128 -126
- package/dist/control-database.js.map +1 -1
- package/dist/control-protocol-guard.js +3 -2
- package/dist/control-protocol-guard.js.map +1 -1
- package/dist/control-read-retry.d.ts +19 -16
- package/dist/control-read-retry.js +22 -54
- package/dist/control-read-retry.js.map +1 -1
- package/dist/control-retry.d.ts +50 -6
- package/dist/control-retry.js +53 -6
- package/dist/control-retry.js.map +1 -1
- package/dist/control-stream.js +2 -1
- package/dist/control-stream.js.map +1 -1
- package/dist/control-write-retry.d.ts +24 -16
- package/dist/control-write-retry.js +58 -30
- package/dist/control-write-retry.js.map +1 -1
- package/dist/describe-thrown.d.ts +12 -0
- package/dist/describe-thrown.js +59 -0
- package/dist/describe-thrown.js.map +1 -0
- package/dist/edge-node/config.d.ts +8 -2
- package/dist/edge-node/config.js +5 -1
- package/dist/edge-node/config.js.map +1 -1
- package/dist/edge-node/dissolve.d.ts +1 -1
- package/dist/edge-node/dissolve.js +1 -1
- package/dist/edge-node/dissolve.js.map +1 -1
- package/dist/edge-node/edge-join.js +4 -1
- package/dist/edge-node/edge-join.js.map +1 -1
- package/dist/edge-node/edge-node.d.ts +10 -0
- package/dist/edge-node/edge-node.js +54 -9
- package/dist/edge-node/edge-node.js.map +1 -1
- package/dist/edge-node/options.d.ts +11 -2
- package/dist/edge-node/options.js +11 -4
- package/dist/edge-node/options.js.map +1 -1
- package/dist/edge-node/platform.d.ts +15 -3
- package/dist/edge-node/strand-move.d.ts +0 -2
- package/dist/edge-node/strand-move.js +1 -4
- package/dist/edge-node/strand-move.js.map +1 -1
- package/dist/edge-node/strands.js +5 -3
- package/dist/edge-node/strands.js.map +1 -1
- package/dist/enrollment.js +2 -1
- package/dist/enrollment.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.js +9 -1
- package/dist/index.js.map +1 -1
- package/dist/member-redemption.js +3 -9
- package/dist/member-redemption.js.map +1 -1
- package/dist/node-local-snapshot.js +2 -1
- package/dist/node-local-snapshot.js.map +1 -1
- package/dist/peer-dial.js +3 -2
- package/dist/peer-dial.js.map +1 -1
- package/dist/pending-join-runner.d.ts +29 -2
- package/dist/pending-join-runner.js +60 -20
- package/dist/pending-join-runner.js.map +1 -1
- package/dist/push-notifier-apns.js +8 -7
- package/dist/push-notifier-apns.js.map +1 -1
- package/dist/push-notifier-fcm.js +6 -5
- package/dist/push-notifier-fcm.js.map +1 -1
- package/dist/push-notifier-shared.d.ts +0 -2
- package/dist/push-notifier-shared.js +4 -4
- package/dist/push-notifier-shared.js.map +1 -1
- package/dist/relay-reservation.js +4 -3
- package/dist/relay-reservation.js.map +1 -1
- package/dist/strand-addr-protocol.js +2 -1
- package/dist/strand-addr-protocol.js.map +1 -1
- package/dist/strand-app-schema-report.d.ts +53 -0
- package/dist/strand-app-schema-report.js +77 -0
- package/dist/strand-app-schema-report.js.map +1 -0
- package/dist/strand-database.d.ts +60 -0
- package/dist/strand-database.js +93 -0
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-first-sync-gate.js +4 -3
- package/dist/strand-first-sync-gate.js.map +1 -1
- package/dist/strand-formation-protocol.js +4 -3
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +73 -2
- package/dist/strand-instance-manager.js +112 -9
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-membership-reconciler.d.ts +13 -1
- package/dist/strand-membership-reconciler.js +25 -16
- package/dist/strand-membership-reconciler.js.map +1 -1
- package/dist/strand-membership-writer.d.ts +16 -2
- package/dist/strand-membership-writer.js +54 -8
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-move.js +4 -6
- package/dist/strand-move.js.map +1 -1
- package/dist/strand-offline.d.ts +102 -0
- package/dist/strand-offline.js +97 -0
- package/dist/strand-offline.js.map +1 -0
- package/dist/types.d.ts +37 -0
- package/dist/types.js.map +1 -1
- package/package.json +8 -8
- package/src/cadre-node.ts +230 -31
- package/src/control-database.ts +5594 -5592
- package/src/control-protocol-guard.ts +3 -2
- package/src/control-read-retry.ts +22 -58
- package/src/control-retry.ts +54 -6
- package/src/control-stream.ts +2 -1
- package/src/control-write-retry.ts +58 -30
- package/src/describe-thrown.ts +60 -0
- package/src/edge-node/config.ts +11 -2
- package/src/edge-node/dissolve.ts +2 -1
- package/src/edge-node/edge-join.ts +2 -1
- package/src/edge-node/edge-node.ts +57 -7
- package/src/edge-node/options.ts +20 -4
- package/src/edge-node/platform.ts +15 -3
- package/src/edge-node/strand-move.ts +1 -5
- package/src/edge-node/strands.ts +74 -72
- package/src/enrollment.ts +2 -1
- package/src/index.ts +27 -1
- package/src/member-redemption.ts +3 -9
- package/src/node-local-snapshot.ts +2 -1
- package/src/peer-dial.ts +605 -604
- package/src/pending-join-runner.ts +68 -22
- package/src/push-notifier-apns.ts +8 -7
- package/src/push-notifier-fcm.ts +6 -5
- package/src/push-notifier-shared.ts +5 -5
- package/src/relay-reservation.ts +1212 -1211
- package/src/strand-addr-protocol.ts +2 -1
- package/src/strand-app-schema-report.ts +115 -0
- package/src/strand-database.ts +123 -1
- package/src/strand-first-sync-gate.ts +600 -599
- package/src/strand-formation-protocol.ts +4 -3
- package/src/strand-instance-manager.ts +137 -10
- package/src/strand-membership-reconciler.ts +942 -928
- package/src/strand-membership-writer.ts +61 -14
- package/src/strand-move.ts +594 -597
- package/src/strand-offline.ts +178 -0
- package/src/types.ts +36 -0
package/src/cadre-node.ts
CHANGED
|
@@ -70,6 +70,14 @@ import { controlClusterPolicy, CONTROL_REPLICATION_BREADTH, DEFAULT_CHECKIN_WIND
|
|
|
70
70
|
import { generatePrivateKey } from '@optimystic/quereus-plugin-crypto';
|
|
71
71
|
import { ed25519KeyPairFromLibp2p, ed25519PublicKeyFromPrivate, requireEd25519PublicKeyB64 } from './ed25519-key.js';
|
|
72
72
|
import { strandTransportKey } from './strand-transport-key.js';
|
|
73
|
+
import {
|
|
74
|
+
isSendingStatus,
|
|
75
|
+
StrandOfflineUnavailableError,
|
|
76
|
+
StrandQueueNotEmptyError,
|
|
77
|
+
type StrandLeaveOptions,
|
|
78
|
+
type StrandOfflineMode,
|
|
79
|
+
type StrandOfflineStatus
|
|
80
|
+
} from './strand-offline.js';
|
|
73
81
|
import {
|
|
74
82
|
assertScopeKeyCharset,
|
|
75
83
|
assertStrandScopeKey,
|
|
@@ -223,6 +231,7 @@ import {
|
|
|
223
231
|
type ConnectionPathSummary
|
|
224
232
|
} from './diagnostics/connection-path.js';
|
|
225
233
|
import { timedStep } from './timed-step.js';
|
|
234
|
+
import { describeThrown, toError } from './describe-thrown.js';
|
|
226
235
|
|
|
227
236
|
const log = debug('sereus:cadre:node');
|
|
228
237
|
const timing = debug('sereus:cadre:timing');
|
|
@@ -1188,6 +1197,13 @@ export class CadreNode implements SAppIdLookup {
|
|
|
1188
1197
|
private reissuedHeldRevocations = false;
|
|
1189
1198
|
/** This node's own `CadrePeer` self-write committed local-only (re-touched on growth). */
|
|
1190
1199
|
private pendingSelfPeerWrite = false;
|
|
1200
|
+
/**
|
|
1201
|
+
* A background self-record publish was deferred because a write then could have forked the
|
|
1202
|
+
* party ({@link publishSelfRecordInBackground}); the next growth drain publishes unless the
|
|
1203
|
+
* record is current. Distinct from {@link pendingSelfPeerWrite}, which re-touches a record
|
|
1204
|
+
* already written, current or not.
|
|
1205
|
+
*/
|
|
1206
|
+
private selfPeerPublishDeferred = false;
|
|
1191
1207
|
/** This node's own `DeviceToken` self-write committed local-only (re-touched on growth). */
|
|
1192
1208
|
private pendingSelfDeviceWrite = false;
|
|
1193
1209
|
/**
|
|
@@ -3315,14 +3331,18 @@ export class CadreNode implements SAppIdLookup {
|
|
|
3315
3331
|
* so the timer is harmless even when registration only becomes possible later.
|
|
3316
3332
|
* A record this process already put in place that is still current (a founding owner's
|
|
3317
3333
|
* explicit publish, a redeemed invitation's admission, a restart's first-growth re-touch) is
|
|
3318
|
-
* not written again ({@link publishSelfRecordUnlessCurrent})
|
|
3319
|
-
*
|
|
3334
|
+
* not written again ({@link publishSelfRecordUnlessCurrent}); that check, not connectivity,
|
|
3335
|
+
* is what spares a device that redeemed within a second of starting a second write.
|
|
3336
|
+
*
|
|
3337
|
+
* A background publish ({@link publishSelfRecordInBackground}): in a party with other members
|
|
3338
|
+
* it waits for a reachable sibling, so two members restarted together, each alone at this
|
|
3339
|
+
* moment, do not each write the `CadrePeer` collection to the same revision.
|
|
3320
3340
|
*/
|
|
3321
3341
|
private scheduleSelfRegistration(): void {
|
|
3322
3342
|
this.selfRegistrationTimer = setTimeout(() => {
|
|
3323
3343
|
void (async () => {
|
|
3324
3344
|
try {
|
|
3325
|
-
await this.publishSelfRecordUnlessCurrent();
|
|
3345
|
+
await this.publishSelfRecordInBackground('boot', () => this.publishSelfRecordUnlessCurrent());
|
|
3326
3346
|
} catch (error) {
|
|
3327
3347
|
// Background task — a failed publish must not crash the node. A publish that loses
|
|
3328
3348
|
// its row to another writer — the owner's re-touch, or a restarted machine's own row
|
|
@@ -3342,6 +3362,38 @@ export class CadreNode implements SAppIdLookup {
|
|
|
3342
3362
|
(this.selfRegistrationTimer as { unref?: () => void } | null)?.unref?.();
|
|
3343
3363
|
}
|
|
3344
3364
|
|
|
3365
|
+
/**
|
|
3366
|
+
* Run `publish`, a self-record publish nothing needs made now (the boot one-shot, the heartbeat,
|
|
3367
|
+
* an address change, {@link registerSelfUnlessCouldFork}), unless a write now could fork the
|
|
3368
|
+
* party ({@link controlWriteCouldFork}). Then nothing is written: {@link selfPeerPublishDeferred}
|
|
3369
|
+
* is set, and the next growth drain publishes the record as it is by then. A deferral is not a
|
|
3370
|
+
* failure, so it neither stamps {@link lastSelfRecordPublishAt} (the reconcile pass's
|
|
3371
|
+
* {@link publishFirstSelfRecord} keeps trying) nor escalates a stale record.
|
|
3372
|
+
*/
|
|
3373
|
+
private async publishSelfRecordInBackground<T extends SelfRegistrationOutcome | 'current'>(
|
|
3374
|
+
reason: string,
|
|
3375
|
+
publish: () => Promise<T>
|
|
3376
|
+
): Promise<T | 'deferred'> {
|
|
3377
|
+
if (await this.controlWriteCouldFork()) {
|
|
3378
|
+
this.selfPeerPublishDeferred = true;
|
|
3379
|
+
log('Self-record publish (%s) deferred: no sibling reachable and the party has other members; the next growth drain publishes', reason);
|
|
3380
|
+
return 'deferred';
|
|
3381
|
+
}
|
|
3382
|
+
return await publish();
|
|
3383
|
+
}
|
|
3384
|
+
|
|
3385
|
+
/**
|
|
3386
|
+
* {@link registerSelf} for a caller that wants the record in place but cannot wait for a
|
|
3387
|
+
* sibling (`cadre start --owner`, so the first seed it mints carries its address). A party with
|
|
3388
|
+
* no other members writes at once; otherwise the write waits for a reachable sibling, as the
|
|
3389
|
+
* boot publish does, and this returns `'deferred'`. Two owner machines restarted together that
|
|
3390
|
+
* each called {@link registerSelf} here would fork the `CadrePeer` collection
|
|
3391
|
+
* ({@link controlWriteCouldFork}).
|
|
3392
|
+
*/
|
|
3393
|
+
async registerSelfUnlessCouldFork(): Promise<SelfRegistrationOutcome | 'deferred'> {
|
|
3394
|
+
return await this.publishSelfRecordInBackground('startup', () => this.registerSelf());
|
|
3395
|
+
}
|
|
3396
|
+
|
|
3345
3397
|
/**
|
|
3346
3398
|
* Publish (or refresh) this node's own signed `CadrePeer` address record so
|
|
3347
3399
|
* other members can resolve its current signaling/relay multiaddrs from its
|
|
@@ -3591,7 +3643,7 @@ export class CadreNode implements SAppIdLookup {
|
|
|
3591
3643
|
console.warn(
|
|
3592
3644
|
`[sereus] this machine has not published its own address record for ${Math.round(staleForMs / 60_000)} `
|
|
3593
3645
|
+ `minutes. The latest attempt (${reason}) failed with: `
|
|
3594
|
-
+ `${
|
|
3646
|
+
+ `${describeThrown(error)}. `
|
|
3595
3647
|
+ 'Other machines in the party discard an address record older than '
|
|
3596
3648
|
+ `${Math.round(DEFAULT_PEER_RECORD_MAX_AGE_MS / 60_000)} minutes, so any of them not already `
|
|
3597
3649
|
+ 'connected to this one can no longer reach it. Publishing keeps retrying; this is reported once.'
|
|
@@ -3680,8 +3732,8 @@ export class CadreNode implements SAppIdLookup {
|
|
|
3680
3732
|
* would write its row on the stale side of the fork, and then never see rows written while it
|
|
3681
3733
|
* was down (`cadre-invite-any-member` runs that pass and fails every time without this
|
|
3682
3734
|
* condition). A connection is not enough: until the sibling identifies, the write's cohort is
|
|
3683
|
-
* this node alone. The boot-time publish
|
|
3684
|
-
*
|
|
3735
|
+
* this node alone. The boot-time publish (alone only in a party with no other members) and the
|
|
3736
|
+
* first-growth drain already try; this retries on the passes after.
|
|
3685
3737
|
*
|
|
3686
3738
|
* A machine admitted by invitation finds its row already holding the record its redemption
|
|
3687
3739
|
* carried, and writes nothing ({@link publishSelfRecordUnlessCurrent}).
|
|
@@ -3723,7 +3775,7 @@ export class CadreNode implements SAppIdLookup {
|
|
|
3723
3775
|
}
|
|
3724
3776
|
|
|
3725
3777
|
const republish = (reason: string) => {
|
|
3726
|
-
void this.registerSelf().catch((error) => {
|
|
3778
|
+
void this.publishSelfRecordInBackground(reason, () => this.registerSelf()).catch((error) => {
|
|
3727
3779
|
log('Record refresh (%s) failed: %o', reason, error);
|
|
3728
3780
|
// The only failure in this file that degrades the whole party on its own, so it is
|
|
3729
3781
|
// the only one that escalates past `debug`. See escalateIfSelfRecordStale.
|
|
@@ -3842,6 +3894,7 @@ export class CadreNode implements SAppIdLookup {
|
|
|
3842
3894
|
this.pendingPeerWrites.clear();
|
|
3843
3895
|
this.pendingRevocations.clear();
|
|
3844
3896
|
this.pendingSelfPeerWrite = false;
|
|
3897
|
+
this.selfPeerPublishDeferred = false;
|
|
3845
3898
|
this.pendingSelfDeviceWrite = false;
|
|
3846
3899
|
// Self-record publication history is per-session too: a restarted node re-publishes
|
|
3847
3900
|
// from scratch, and carrying the previous lifetime's stamp forward would either
|
|
@@ -4898,14 +4951,20 @@ export class CadreNode implements SAppIdLookup {
|
|
|
4898
4951
|
|
|
4899
4952
|
// 2. Self rows. A self write made while alone is always re-touched: the re-touch
|
|
4900
4953
|
// is what carries it to the cohort. Otherwise the first growth re-touches a row
|
|
4901
|
-
// a prior process may have committed local-only,
|
|
4902
|
-
//
|
|
4954
|
+
// a prior process may have committed local-only, and a background publish deferred
|
|
4955
|
+
// while alone is made now, each unless the row already holds a current record this
|
|
4956
|
+
// process signed (a redeemed invitation's admission). Both flags are taken before
|
|
4957
|
+
// the publish, so one set again while it runs (the write committed alone after all,
|
|
4958
|
+
// a heartbeat deferred meanwhile) waits for the next growth.
|
|
4903
4959
|
// registerSelf is idempotent + single-flight, so this never races a
|
|
4904
4960
|
// duplicate publish with the heartbeat republish.
|
|
4905
|
-
|
|
4906
|
-
|
|
4961
|
+
const retouchSelf = this.pendingSelfPeerWrite;
|
|
4962
|
+
const publishDeferred = this.selfPeerPublishDeferred;
|
|
4963
|
+
this.pendingSelfPeerWrite = false;
|
|
4964
|
+
this.selfPeerPublishDeferred = false;
|
|
4965
|
+
if (firstGrowth || retouchSelf || publishDeferred) {
|
|
4966
|
+
const publish = retouchSelf ? this.registerSelf() : this.publishSelfRecordUnlessCurrent();
|
|
4907
4967
|
await publish.catch((error) => log('drain: registerSelf failed: %o', error));
|
|
4908
|
-
this.pendingSelfPeerWrite = false;
|
|
4909
4968
|
}
|
|
4910
4969
|
if (firstGrowth || this.pendingSelfDeviceWrite) {
|
|
4911
4970
|
await this.retouchSelfDeviceToken();
|
|
@@ -4924,8 +4983,10 @@ export class CadreNode implements SAppIdLookup {
|
|
|
4924
4983
|
// 4. In-session pending owner authorize writes.
|
|
4925
4984
|
await this.drainPendingPeerWrites();
|
|
4926
4985
|
|
|
4927
|
-
// 5. Pending joins this process wrote while alone
|
|
4986
|
+
// 5. Pending joins this process wrote while alone (their tombstones went out in step 1),
|
|
4987
|
+
// then the background attempts it deferred while alone.
|
|
4928
4988
|
await this.pendingJoinRunner?.reissueWritesMadeAlone();
|
|
4989
|
+
this.pendingJoinRunner?.resumeDeferredWrites();
|
|
4929
4990
|
}
|
|
4930
4991
|
|
|
4931
4992
|
/**
|
|
@@ -5608,7 +5669,7 @@ export class CadreNode implements SAppIdLookup {
|
|
|
5608
5669
|
log('Error starting strand %s: %o', strand.Id, error);
|
|
5609
5670
|
this.emit('strand:error', {
|
|
5610
5671
|
strandId: strand.Id,
|
|
5611
|
-
error:
|
|
5672
|
+
error: toError(error)
|
|
5612
5673
|
});
|
|
5613
5674
|
// Rethrow after the event: the only caller is the StrandWatcher's
|
|
5614
5675
|
// onStrandAdded callback, which uses the rejection to decide whether the
|
|
@@ -5686,7 +5747,7 @@ export class CadreNode implements SAppIdLookup {
|
|
|
5686
5747
|
log('Error stopping strand %s: %o', strandId, error);
|
|
5687
5748
|
this.emit('strand:error', {
|
|
5688
5749
|
strandId,
|
|
5689
|
-
error:
|
|
5750
|
+
error: toError(error)
|
|
5690
5751
|
});
|
|
5691
5752
|
}
|
|
5692
5753
|
}
|
|
@@ -5871,6 +5932,24 @@ export class CadreNode implements SAppIdLookup {
|
|
|
5871
5932
|
this.emit('strand:writable', { strandId });
|
|
5872
5933
|
}
|
|
5873
5934
|
|
|
5935
|
+
/**
|
|
5936
|
+
* A status from a strand's offline queue (`StartStrandConfig.offline`). The queue sending counts
|
|
5937
|
+
* as activity ({@link isSendingStatus}), so a check-in window that finds the cohort reachable stays
|
|
5938
|
+
* up while the queue drains, and the idle timer does not cut a drain short. Recorded through the
|
|
5939
|
+
* hibernation manager, not {@link recordStrandActivity}, whose push fan-out would wake other phones.
|
|
5940
|
+
*
|
|
5941
|
+
* Not on a strand being quiesced: activity there wakes it again once the quiesce ends, which would
|
|
5942
|
+
* undo a `hibernateAll` made as the app went to the background. A send that quiesce cuts short was
|
|
5943
|
+
* journaled before it went out, and the next build's restore re-sends it under the same action id.
|
|
5944
|
+
*/
|
|
5945
|
+
private handleOfflineStatus(event: StrandOfflineStatus): void {
|
|
5946
|
+
const instance = this.strandManager.getInstance(event.strandId);
|
|
5947
|
+
if (instance && isSendingStatus(event.status) && !this.strandManager.isQuiescing(event.strandId)) {
|
|
5948
|
+
this.hibernationManager.recordActivity(instance);
|
|
5949
|
+
}
|
|
5950
|
+
this.emit('strand:offline-status', event);
|
|
5951
|
+
}
|
|
5952
|
+
|
|
5874
5953
|
// Hibernation callbacks
|
|
5875
5954
|
private async handleStrandIdle(strandId: string): Promise<void> {
|
|
5876
5955
|
const instance = this.strandManager.getInstance(strandId);
|
|
@@ -6298,6 +6377,33 @@ export class CadreNode implements SAppIdLookup {
|
|
|
6298
6377
|
});
|
|
6299
6378
|
}
|
|
6300
6379
|
|
|
6380
|
+
/**
|
|
6381
|
+
* Switch a strand between offline mode, where a commit resolves once the transaction is queued on
|
|
6382
|
+
* this machine, and online mode (docs/strands.md → "Offline mode"). Affects transactions begun from
|
|
6383
|
+
* now on; one already open keeps the acknowledgement it began with. The mode belongs to the strand:
|
|
6384
|
+
* a hibernating strand takes it at its next build, and it lasts across hibernation and every
|
|
6385
|
+
* rebuild until the process exits, after which the strand starts in `strandOffline.initialMode`.
|
|
6386
|
+
*
|
|
6387
|
+
* @throws StrandOfflineUnavailableError when `CadreNodeConfig.strandOffline` is not configured.
|
|
6388
|
+
* @throws when this node does not run the strand.
|
|
6389
|
+
*/
|
|
6390
|
+
setStrandOfflineMode(strandId: string, mode: StrandOfflineMode): void {
|
|
6391
|
+
if (!this.config.strandOffline) {
|
|
6392
|
+
throw new StrandOfflineUnavailableError(strandId);
|
|
6393
|
+
}
|
|
6394
|
+
this.strandManager.setOfflineMode(strandId, mode);
|
|
6395
|
+
}
|
|
6396
|
+
|
|
6397
|
+
/**
|
|
6398
|
+
* A strand's offline mode ({@link setStrandOfflineMode}); `'online'` on a node without
|
|
6399
|
+
* `strandOffline`. The queue's own view, with what it holds, is `strand.database.offline.queueState()`.
|
|
6400
|
+
*
|
|
6401
|
+
* @throws when this node does not run the strand.
|
|
6402
|
+
*/
|
|
6403
|
+
getStrandOfflineMode(strandId: string): StrandOfflineMode {
|
|
6404
|
+
return this.strandManager.offlineMode(strandId);
|
|
6405
|
+
}
|
|
6406
|
+
|
|
6301
6407
|
/**
|
|
6302
6408
|
* Publish a strand row to the shared control database under this node's own
|
|
6303
6409
|
* owner identity, so other cadre members discover it via control-network
|
|
@@ -6781,16 +6887,25 @@ export class CadreNode implements SAppIdLookup {
|
|
|
6781
6887
|
* caller ever needs removal to mean "and erase the local copy", that is a separate purge
|
|
6782
6888
|
* step, not a widening of this method.
|
|
6783
6889
|
*
|
|
6890
|
+
* Refuses while the strand's offline queue on this machine holds transactions, unless
|
|
6891
|
+
* `options.discardQueued` (docs/strands.md → "Leaving a strand with unsent transactions"). The
|
|
6892
|
+
* check and the discard run before the control-plane delete, so a delete refused afterwards
|
|
6893
|
+
* leaves the discarded transactions discarded.
|
|
6894
|
+
*
|
|
6784
6895
|
* @param strandId - The `Strand` row id to remove (as passed to {@link publishStrand}).
|
|
6896
|
+
* @param options - `discardQueued`: withdraw the queued transactions that were never sent, and remove.
|
|
6897
|
+
* @throws StrandQueueNotEmptyError when the queue holds transactions and `discardQueued` is not
|
|
6898
|
+
* set; nothing was removed.
|
|
6785
6899
|
* @throws if the node is not started, has no owner signer wired, the id is blank, or
|
|
6786
6900
|
* the signer is not an enrolled owner (the schema's `Strand.AuthorizedDelete` rejects
|
|
6787
6901
|
* the write and the row survives). A rejection does NOT imply the row survived: the
|
|
6788
6902
|
* local stop runs after the control-plane delete has already committed, so a failure
|
|
6789
6903
|
* there throws over a completed removal.
|
|
6790
6904
|
*/
|
|
6791
|
-
async unpublishStrand(strandId: string): Promise<void> {
|
|
6905
|
+
async unpublishStrand(strandId: string, options?: StrandLeaveOptions): Promise<void> {
|
|
6792
6906
|
const signer = this.requireOwnerSigner(`unpublish strand ${strandId}`);
|
|
6793
6907
|
const trimmed = requireNonBlank(strandId, 'strand id');
|
|
6908
|
+
await this.settleQueueBeforeLeave(trimmed, options);
|
|
6794
6909
|
// One approval: the strand delete and its party key's, each with its tombstone.
|
|
6795
6910
|
const removed = await signer.approve({ partyId: this.partyId, action: `Remove the strand ${trimmed} from the cadre` },
|
|
6796
6911
|
(sign) => this.controlDatabase!.deleteStrand(trimmed, signer.publicKey, sign));
|
|
@@ -6952,6 +7067,11 @@ export class CadreNode implements SAppIdLookup {
|
|
|
6952
7067
|
// NOTE: founder-ness no longer waits for the owner signer, so a founding machine's launch
|
|
6953
7068
|
// that runs before the embedder wires it throws here and the watcher retries on its backoff;
|
|
6954
7069
|
// if that shows up as restart noise, skip the heal until `getOwnerSigner()` is set.
|
|
7070
|
+
// NOTE: a control write the strand watcher's launch makes in the background, not deferred
|
|
7071
|
+
// while a write could fork the party (`controlWriteCouldFork`): only the row's founder mints,
|
|
7072
|
+
// and only for a closed row stored without its key, so two machines forking `StrandPartyKey`
|
|
7073
|
+
// at once needs two such rows launching alone together. If this heal comes to run routinely
|
|
7074
|
+
// at boot, defer it the same way.
|
|
6955
7075
|
return await this.ensureStrandPartyKey(strand.Id);
|
|
6956
7076
|
}
|
|
6957
7077
|
|
|
@@ -7264,6 +7384,16 @@ export class CadreNode implements SAppIdLookup {
|
|
|
7264
7384
|
onWritable: (writableStrandId) => this.handleStrandWritable(writableStrandId),
|
|
7265
7385
|
onCommitDurability: (commit) => this.emit('strand:commit-durability', commit),
|
|
7266
7386
|
onCommitHeld: (held) => this.emit('strand:commit-held', held),
|
|
7387
|
+
offline: this.config.strandOffline && {
|
|
7388
|
+
...this.config.strandOffline,
|
|
7389
|
+
onOfflineStatus: (event) => this.handleOfflineStatus(event),
|
|
7390
|
+
onQueueState: (event) => this.emit('strand:queue-state', event)
|
|
7391
|
+
},
|
|
7392
|
+
appSchema: {
|
|
7393
|
+
onAppSchemaDeferred: (event) => this.emit('strand:app-schema-deferred', event),
|
|
7394
|
+
onAppSchemaApplied: (event) => this.emit('strand:app-schema-applied', event),
|
|
7395
|
+
onAppSchemaFailed: (event) => this.emit('strand:error', event)
|
|
7396
|
+
},
|
|
7267
7397
|
// Re-announce the delegate to ONE relay before the strand node's reservation
|
|
7268
7398
|
// supervisor re-drives it (see announceDelegateToRelay). Retained with the
|
|
7269
7399
|
// launch config, so a hibernation wake's rebuilt supervisors get it too.
|
|
@@ -8002,19 +8132,76 @@ export class CadreNode implements SAppIdLookup {
|
|
|
8002
8132
|
* The other members of the strand are not told, and this party's membership row in the
|
|
8003
8133
|
* strand stays, so a later re-formation reuses the same identity.
|
|
8004
8134
|
*
|
|
8135
|
+
* Refuses while the strand's offline queue on this machine holds transactions, unless
|
|
8136
|
+
* `options.discardQueued` (docs/strands.md → "Leaving a strand with unsent transactions"). The
|
|
8137
|
+
* check and the discard run before the party-wide row is removed, so a removal refused
|
|
8138
|
+
* afterwards leaves the discarded transactions discarded.
|
|
8139
|
+
*
|
|
8140
|
+
* @param options - `discardQueued`: withdraw the queued transactions that were never sent, and leave.
|
|
8141
|
+
* @throws StrandQueueNotEmptyError when the queue holds transactions and `discardQueued` is not
|
|
8142
|
+
* set; nothing was left.
|
|
8005
8143
|
* @throws when a party-wide row exists and this machine cannot sign its removal (not an
|
|
8006
8144
|
* enrolled owner) — {@link stopStrand} stops the strand on this machine only — or when
|
|
8007
8145
|
* the party-wide table cannot be read.
|
|
8008
8146
|
*/
|
|
8009
|
-
async forgetJoinedStrand(strandId: string): Promise<void> {
|
|
8147
|
+
async forgetJoinedStrand(strandId: string, options?: StrandLeaveOptions): Promise<void> {
|
|
8010
8148
|
if (!this._running) {
|
|
8011
8149
|
throw new Error('CadreNode not running');
|
|
8012
8150
|
}
|
|
8151
|
+
await this.settleQueueBeforeLeave(strandId, options);
|
|
8013
8152
|
await this.joinedStrands!.leave(strandId);
|
|
8014
8153
|
this.forgetStrandPeers(strandId, 'forgetJoinedStrand');
|
|
8015
8154
|
await this.stopStrand(strandId);
|
|
8016
8155
|
}
|
|
8017
8156
|
|
|
8157
|
+
/**
|
|
8158
|
+
* Before a leave changes anything: refuse while the strand's offline queue holds transactions,
|
|
8159
|
+
* unless the app asked to discard them (docs/strands.md → "Leaving a strand with unsent
|
|
8160
|
+
* transactions"). Transactions Optimystic will not withdraw stay in the journal, named in one
|
|
8161
|
+
* warning, and the leave goes on: they may already be saved, and a cohort that never returns
|
|
8162
|
+
* would otherwise block the leave forever.
|
|
8163
|
+
*
|
|
8164
|
+
* @throws StrandQueueNotEmptyError when the queue holds transactions and `discardQueued` is not set.
|
|
8165
|
+
* @throws when a hibernating strand has to be woken to discard and the wake fails.
|
|
8166
|
+
*/
|
|
8167
|
+
private async settleQueueBeforeLeave(strandId: string, options: StrandLeaveOptions | undefined): Promise<void> {
|
|
8168
|
+
const queued = this.strandManager.queuedTransactionIds(strandId);
|
|
8169
|
+
if (queued.length === 0) {
|
|
8170
|
+
return;
|
|
8171
|
+
}
|
|
8172
|
+
if (options?.discardQueued !== true) {
|
|
8173
|
+
throw new StrandQueueNotEmptyError(strandId, queued);
|
|
8174
|
+
}
|
|
8175
|
+
const kept = await this.discardQueuedForLeave(strandId);
|
|
8176
|
+
if (kept === undefined) {
|
|
8177
|
+
console.warn(
|
|
8178
|
+
`[sereus] strand ${strandId}: leaving with ${queued.length} queued transaction(s) not discarded ` +
|
|
8179
|
+
`(${queued.join(', ')}): the strand's database could not be built to reach its queue. They stay in ` +
|
|
8180
|
+
'this machine\'s offline journal and come back if this machine attaches the strand again.'
|
|
8181
|
+
);
|
|
8182
|
+
} else if (kept.length > 0) {
|
|
8183
|
+
console.warn(
|
|
8184
|
+
`[sereus] strand ${strandId}: leaving with ${kept.length} queued transaction(s) that could not be ` +
|
|
8185
|
+
`discarded (${kept.join(', ')}): each was sent, or is being sent, and the strand's members may already ` +
|
|
8186
|
+
'hold it. They stay in this machine\'s offline journal and settle if this machine attaches the strand again.'
|
|
8187
|
+
);
|
|
8188
|
+
}
|
|
8189
|
+
}
|
|
8190
|
+
|
|
8191
|
+
/**
|
|
8192
|
+
* Discard from the strand's live queue, waking a hibernating strand first (its queue is back
|
|
8193
|
+
* once a build restores it from the journal), and return what was kept. `undefined` when the
|
|
8194
|
+
* wake built no database (no seed peer has answered), so there was no queue to discard from.
|
|
8195
|
+
*/
|
|
8196
|
+
private async discardQueuedForLeave(strandId: string): Promise<readonly string[] | undefined> {
|
|
8197
|
+
const kept = this.strandManager.discardQueuedTransactions(strandId);
|
|
8198
|
+
if (kept !== undefined) {
|
|
8199
|
+
return kept;
|
|
8200
|
+
}
|
|
8201
|
+
await this.wakeStrand(strandId);
|
|
8202
|
+
return this.strandManager.discardQueuedTransactions(strandId);
|
|
8203
|
+
}
|
|
8204
|
+
|
|
8018
8205
|
/**
|
|
8019
8206
|
* Record `row` as a join from another party when this party's control database names it
|
|
8020
8207
|
* neither as its own strand nor as a party-wide join — the one case nothing else would
|
|
@@ -9373,8 +9560,8 @@ export class CadreNode implements SAppIdLookup {
|
|
|
9373
9560
|
* `OwnerKey.Authorized`; the row stores the vouching pair). The write notifies the
|
|
9374
9561
|
* membership hub, so the refresh it triggers has run by the time this resolves.
|
|
9375
9562
|
*
|
|
9376
|
-
* Refused while alone ({@link
|
|
9377
|
-
* so one committed with no
|
|
9563
|
+
* Refused while alone ({@link controlWriteCouldFork}): an `OwnerKey` add has no re-issue path,
|
|
9564
|
+
* so one committed with no sibling reachable would stay on this node.
|
|
9378
9565
|
*
|
|
9379
9566
|
* @param ownerPublicKeyB64 - the new owner's ed25519 public key (base64url).
|
|
9380
9567
|
* @param options.label - the user's name for the key ("phone", "paper backup"), bound into
|
|
@@ -9387,7 +9574,7 @@ export class CadreNode implements SAppIdLookup {
|
|
|
9387
9574
|
throw new Error('Seed bootstrap service not initialized. Call initializeSeedBootstrap() first.');
|
|
9388
9575
|
}
|
|
9389
9576
|
const key = requireEd25519PublicKeyB64(ownerPublicKeyB64, 'owner key');
|
|
9390
|
-
if (await this.
|
|
9577
|
+
if (await this.controlWriteCouldFork()) {
|
|
9391
9578
|
throw new OwnerKeyAddRefusedError(key, 'alone');
|
|
9392
9579
|
}
|
|
9393
9580
|
const added = await this.seedBootstrapService.addOwner(key, options.label);
|
|
@@ -9402,17 +9589,27 @@ export class CadreNode implements SAppIdLookup {
|
|
|
9402
9589
|
}
|
|
9403
9590
|
|
|
9404
9591
|
/**
|
|
9405
|
-
* Whether
|
|
9406
|
-
* {@link
|
|
9407
|
-
*
|
|
9408
|
-
*
|
|
9409
|
-
* party with no other members cannot fork
|
|
9410
|
-
*
|
|
9592
|
+
* Whether a control write made now could fork the party's history: no sibling is reachable
|
|
9593
|
+
* ({@link committedAlone}) while the party has other members. Such a write commits on this
|
|
9594
|
+
* node alone, and if another member writes the same collection meanwhile, both take it to the
|
|
9595
|
+
* same revision with different content, which no re-issue heals (docs/architecture.md →
|
|
9596
|
+
* "Writes made while alone"). A party with no other members cannot fork.
|
|
9597
|
+
*
|
|
9598
|
+
* What a caller does on `true` depends on the write. An `OwnerKey` add has no re-issue path at
|
|
9599
|
+
* all, so {@link addOwner}, {@link removeOwner}'s re-vouch and a local owner grant redemption
|
|
9600
|
+
* refuse it `alone`. A background write nothing waits on (the self-record publishes
|
|
9601
|
+
* {@link publishSelfRecordInBackground} makes, the pending-join runner's background attempts)
|
|
9602
|
+
* waits for a reachable sibling.
|
|
9411
9603
|
*
|
|
9412
9604
|
* Waits out an in-flight membership refresh first: until `start`'s first one lands the
|
|
9413
9605
|
* member snapshot is empty, which would read as a party with no other members.
|
|
9606
|
+
*
|
|
9607
|
+
* NOTE: a snapshot that stays empty on a node that has members (a failed first membership
|
|
9608
|
+
* read keeps the empty snapshot; an anchor never seeded trusts no row) also reads as "no
|
|
9609
|
+
* other members", so such a node writes alone as before this check existed. If a fork is
|
|
9610
|
+
* ever traced to that state, count the party from the unfiltered `CadrePeer` rows here.
|
|
9414
9611
|
*/
|
|
9415
|
-
private async
|
|
9612
|
+
private async controlWriteCouldFork(): Promise<boolean> {
|
|
9416
9613
|
await this.membershipGateDrain;
|
|
9417
9614
|
return this.committedAlone() && this.authorizedControlPeers.size > 0;
|
|
9418
9615
|
}
|
|
@@ -9455,7 +9652,7 @@ export class CadreNode implements SAppIdLookup {
|
|
|
9455
9652
|
*
|
|
9456
9653
|
* Steps: re-plan ({@link planOwnerKeyRemoval}) and throw {@link OwnerKeyRemovalRefusedError}
|
|
9457
9654
|
* on a refusal; check `options.revouch` against the plan's dependents and throw before any
|
|
9458
|
-
* write when it names anything else; refuse `alone` ({@link
|
|
9655
|
+
* write when it names anything else; refuse `alone` ({@link controlWriteCouldFork}) when an
|
|
9459
9656
|
* `OwnerKey` row is to be re-vouched, since that re-add has no re-issue path; **re-vouch
|
|
9460
9657
|
* first**, under this node's key, the dependents the caller chose to keep and this node's own
|
|
9461
9658
|
* row when the key vouched it (`ControlDatabase.revouchOwnerKey`, `reauthorizePeer`), so they
|
|
@@ -9480,7 +9677,7 @@ export class CadreNode implements SAppIdLookup {
|
|
|
9480
9677
|
throw new OwnerKeyRemovalRefusedError(key, plan.refusal);
|
|
9481
9678
|
}
|
|
9482
9679
|
const revouch = revouchWithinPlan(plan, options.revouch, this.peerId?.toString() ?? null);
|
|
9483
|
-
if (revouch.owners.length > 0 && await this.
|
|
9680
|
+
if (revouch.owners.length > 0 && await this.controlWriteCouldFork()) {
|
|
9484
9681
|
throw new OwnerKeyRemovalRefusedError(key, 'alone');
|
|
9485
9682
|
}
|
|
9486
9683
|
// The plan already refused 'not-an-owner' when no enrolled signer is wired.
|
|
@@ -10409,7 +10606,8 @@ export class CadreNode implements SAppIdLookup {
|
|
|
10409
10606
|
async redeemOwnerGrant(bundle: OwnerGrantBundle, signer: OwnerSigner, options: RedeemOwnerGrantOptions = {}): Promise<RedeemOwnerGrantResult> {
|
|
10410
10607
|
const { controlDatabase } = this.requireStartedForGrant(bundle, 'redeeming an owner grant');
|
|
10411
10608
|
// An enrolled owner machine is a party machine whether or not its own row is published yet
|
|
10412
|
-
// (
|
|
10609
|
+
// (its first self-record lands a second after start, or once it reaches a sibling in a party
|
|
10610
|
+
// with other members), so it writes locally too.
|
|
10413
10611
|
if (await this.isLiveMemberSelf() || await this.isOwner()) {
|
|
10414
10612
|
return await this.redeemOwnerGrantLocally(controlDatabase, bundle, signer);
|
|
10415
10613
|
}
|
|
@@ -10427,7 +10625,7 @@ export class CadreNode implements SAppIdLookup {
|
|
|
10427
10625
|
* check and the write, as the member handler carries it (`MemberRedemptionHandler`).
|
|
10428
10626
|
*/
|
|
10429
10627
|
private async redeemOwnerGrantLocally(controlDatabase: ControlDatabase, bundle: OwnerGrantBundle, signer: OwnerSigner): Promise<RedeemOwnerGrantResult> {
|
|
10430
|
-
if (await this.
|
|
10628
|
+
if (await this.controlWriteCouldFork()) {
|
|
10431
10629
|
throw new OwnerKeyAddRefusedError(signer.publicKey, 'alone');
|
|
10432
10630
|
}
|
|
10433
10631
|
const held = await controlDatabase.queryOwnerGrant(bundle.grant.key);
|
|
@@ -10967,6 +11165,7 @@ export class CadreNode implements SAppIdLookup {
|
|
|
10967
11165
|
remove: (id) => ownerWrite(`Clean up the finished pending join ${id}`,
|
|
10968
11166
|
(ownerKey, sign) => database().deletePendingJoin(id, ownerKey, sign)),
|
|
10969
11167
|
isAlone: () => this.committedAlone(),
|
|
11168
|
+
deferBackgroundWrites: () => this.controlWriteCouldFork(),
|
|
10970
11169
|
sAppIdOf: (row) => this.pendingJoinSAppId(row),
|
|
10971
11170
|
observeRows: (rows) => this.stageMembershipInvitesFromPendingJoins(rows),
|
|
10972
11171
|
emit: (status) => this.emit('pendingJoin:changed', status),
|