@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.
Files changed (131) hide show
  1. package/dist/cadre-node.d.ts +118 -16
  2. package/dist/cadre-node.js +210 -31
  3. package/dist/cadre-node.js.map +1 -1
  4. package/dist/control-database.d.ts +2 -1
  5. package/dist/control-database.js +128 -126
  6. package/dist/control-database.js.map +1 -1
  7. package/dist/control-protocol-guard.js +3 -2
  8. package/dist/control-protocol-guard.js.map +1 -1
  9. package/dist/control-read-retry.d.ts +19 -16
  10. package/dist/control-read-retry.js +22 -54
  11. package/dist/control-read-retry.js.map +1 -1
  12. package/dist/control-retry.d.ts +50 -6
  13. package/dist/control-retry.js +53 -6
  14. package/dist/control-retry.js.map +1 -1
  15. package/dist/control-stream.js +2 -1
  16. package/dist/control-stream.js.map +1 -1
  17. package/dist/control-write-retry.d.ts +24 -16
  18. package/dist/control-write-retry.js +58 -30
  19. package/dist/control-write-retry.js.map +1 -1
  20. package/dist/describe-thrown.d.ts +12 -0
  21. package/dist/describe-thrown.js +59 -0
  22. package/dist/describe-thrown.js.map +1 -0
  23. package/dist/edge-node/config.d.ts +8 -2
  24. package/dist/edge-node/config.js +5 -1
  25. package/dist/edge-node/config.js.map +1 -1
  26. package/dist/edge-node/dissolve.d.ts +1 -1
  27. package/dist/edge-node/dissolve.js +1 -1
  28. package/dist/edge-node/dissolve.js.map +1 -1
  29. package/dist/edge-node/edge-join.js +4 -1
  30. package/dist/edge-node/edge-join.js.map +1 -1
  31. package/dist/edge-node/edge-node.d.ts +10 -0
  32. package/dist/edge-node/edge-node.js +54 -9
  33. package/dist/edge-node/edge-node.js.map +1 -1
  34. package/dist/edge-node/options.d.ts +11 -2
  35. package/dist/edge-node/options.js +11 -4
  36. package/dist/edge-node/options.js.map +1 -1
  37. package/dist/edge-node/platform.d.ts +15 -3
  38. package/dist/edge-node/strand-move.d.ts +0 -2
  39. package/dist/edge-node/strand-move.js +1 -4
  40. package/dist/edge-node/strand-move.js.map +1 -1
  41. package/dist/edge-node/strands.js +5 -3
  42. package/dist/edge-node/strands.js.map +1 -1
  43. package/dist/enrollment.js +2 -1
  44. package/dist/enrollment.js.map +1 -1
  45. package/dist/index.d.ts +6 -2
  46. package/dist/index.js +9 -1
  47. package/dist/index.js.map +1 -1
  48. package/dist/member-redemption.js +3 -9
  49. package/dist/member-redemption.js.map +1 -1
  50. package/dist/node-local-snapshot.js +2 -1
  51. package/dist/node-local-snapshot.js.map +1 -1
  52. package/dist/peer-dial.js +3 -2
  53. package/dist/peer-dial.js.map +1 -1
  54. package/dist/pending-join-runner.d.ts +29 -2
  55. package/dist/pending-join-runner.js +60 -20
  56. package/dist/pending-join-runner.js.map +1 -1
  57. package/dist/push-notifier-apns.js +8 -7
  58. package/dist/push-notifier-apns.js.map +1 -1
  59. package/dist/push-notifier-fcm.js +6 -5
  60. package/dist/push-notifier-fcm.js.map +1 -1
  61. package/dist/push-notifier-shared.d.ts +0 -2
  62. package/dist/push-notifier-shared.js +4 -4
  63. package/dist/push-notifier-shared.js.map +1 -1
  64. package/dist/relay-reservation.js +4 -3
  65. package/dist/relay-reservation.js.map +1 -1
  66. package/dist/strand-addr-protocol.js +2 -1
  67. package/dist/strand-addr-protocol.js.map +1 -1
  68. package/dist/strand-app-schema-report.d.ts +53 -0
  69. package/dist/strand-app-schema-report.js +77 -0
  70. package/dist/strand-app-schema-report.js.map +1 -0
  71. package/dist/strand-database.d.ts +60 -0
  72. package/dist/strand-database.js +93 -0
  73. package/dist/strand-database.js.map +1 -1
  74. package/dist/strand-first-sync-gate.js +4 -3
  75. package/dist/strand-first-sync-gate.js.map +1 -1
  76. package/dist/strand-formation-protocol.js +4 -3
  77. package/dist/strand-formation-protocol.js.map +1 -1
  78. package/dist/strand-instance-manager.d.ts +73 -2
  79. package/dist/strand-instance-manager.js +112 -9
  80. package/dist/strand-instance-manager.js.map +1 -1
  81. package/dist/strand-membership-reconciler.d.ts +13 -1
  82. package/dist/strand-membership-reconciler.js +25 -16
  83. package/dist/strand-membership-reconciler.js.map +1 -1
  84. package/dist/strand-membership-writer.d.ts +16 -2
  85. package/dist/strand-membership-writer.js +54 -8
  86. package/dist/strand-membership-writer.js.map +1 -1
  87. package/dist/strand-move.js +4 -6
  88. package/dist/strand-move.js.map +1 -1
  89. package/dist/strand-offline.d.ts +102 -0
  90. package/dist/strand-offline.js +97 -0
  91. package/dist/strand-offline.js.map +1 -0
  92. package/dist/types.d.ts +37 -0
  93. package/dist/types.js.map +1 -1
  94. package/package.json +8 -8
  95. package/src/cadre-node.ts +230 -31
  96. package/src/control-database.ts +5594 -5592
  97. package/src/control-protocol-guard.ts +3 -2
  98. package/src/control-read-retry.ts +22 -58
  99. package/src/control-retry.ts +54 -6
  100. package/src/control-stream.ts +2 -1
  101. package/src/control-write-retry.ts +58 -30
  102. package/src/describe-thrown.ts +60 -0
  103. package/src/edge-node/config.ts +11 -2
  104. package/src/edge-node/dissolve.ts +2 -1
  105. package/src/edge-node/edge-join.ts +2 -1
  106. package/src/edge-node/edge-node.ts +57 -7
  107. package/src/edge-node/options.ts +20 -4
  108. package/src/edge-node/platform.ts +15 -3
  109. package/src/edge-node/strand-move.ts +1 -5
  110. package/src/edge-node/strands.ts +74 -72
  111. package/src/enrollment.ts +2 -1
  112. package/src/index.ts +27 -1
  113. package/src/member-redemption.ts +3 -9
  114. package/src/node-local-snapshot.ts +2 -1
  115. package/src/peer-dial.ts +605 -604
  116. package/src/pending-join-runner.ts +68 -22
  117. package/src/push-notifier-apns.ts +8 -7
  118. package/src/push-notifier-fcm.ts +6 -5
  119. package/src/push-notifier-shared.ts +5 -5
  120. package/src/relay-reservation.ts +1212 -1211
  121. package/src/strand-addr-protocol.ts +2 -1
  122. package/src/strand-app-schema-report.ts +115 -0
  123. package/src/strand-database.ts +123 -1
  124. package/src/strand-first-sync-gate.ts +600 -599
  125. package/src/strand-formation-protocol.ts +4 -3
  126. package/src/strand-instance-manager.ts +137 -10
  127. package/src/strand-membership-reconciler.ts +942 -928
  128. package/src/strand-membership-writer.ts +61 -14
  129. package/src/strand-move.ts +594 -597
  130. package/src/strand-offline.ts +178 -0
  131. package/src/types.ts +36 -0
@@ -1,6 +1,7 @@
1
1
  import type { Libp2p, PeerId } from '@libp2p/interface';
2
2
  import type { Multiaddr } from '@multiformats/multiaddr';
3
3
  import type { CadreNodeConfig, StrandInstance, StrandRow, StrandConfig, FoundStrandConfig, FoundStrandResult, FoundStrandOptions, FoundStrandBackgroundResult, SAppConfig, CadreNodeEvents, ControlNetworkSeed, ApplySeedResult, SeedAckMessage, SeedDeliveryTarget, AddDroneOptions, DroneInitResult, OpenInvitation, FormStrandResult, StrandFormationDisclosure, StrandMembershipInvite, ResolveOpts, SelfRegistrationOutcome, ServiceWakeResult, PushPlatform, DeviceTokenRecord, CadreInviteStatus, CadreMachine, CadreOwnerKey, MachineRemovalPlan, MachineRemovalResult, OwnerKeyRemovalPlan, OwnerKeyRemovalResult, RemoveOwnerOptions, CadreRemoval, CadreInvitationReach, CreateCadreInvitationOptions, CreateCadreInvitationResult, RedeemCadreInvitationOptions, RedeemCadreInvitationResult, CreateOwnerGrantOptions, CreateOwnerGrantResult, OwnerGrantStatus, RedeemOwnerGrantOptions, RedeemOwnerGrantResult, JoinWithOwnerGrantResult, ResolveDeviceTokenOpts, PendingJoinStatus } from './types.js';
4
+ import { type StrandLeaveOptions, type StrandOfflineMode } from './strand-offline.js';
4
5
  import { type TrustedOwnerStore, type TrustSource } from './trusted-owner-store.js';
5
6
  import { type OwnerSign, type OwnerSigner } from './owner-signer.js';
6
7
  import { type BootstrapPeerStore } from './bootstrap-peer-store.js';
@@ -700,6 +701,13 @@ export declare class CadreNode implements SAppIdLookup {
700
701
  private reissuedHeldRevocations;
701
702
  /** This node's own `CadrePeer` self-write committed local-only (re-touched on growth). */
702
703
  private pendingSelfPeerWrite;
704
+ /**
705
+ * A background self-record publish was deferred because a write then could have forked the
706
+ * party ({@link publishSelfRecordInBackground}); the next growth drain publishes unless the
707
+ * record is current. Distinct from {@link pendingSelfPeerWrite}, which re-touches a record
708
+ * already written, current or not.
709
+ */
710
+ private selfPeerPublishDeferred;
703
711
  /** This node's own `DeviceToken` self-write committed local-only (re-touched on growth). */
704
712
  private pendingSelfDeviceWrite;
705
713
  /**
@@ -1527,10 +1535,32 @@ export declare class CadreNode implements SAppIdLookup {
1527
1535
  * so the timer is harmless even when registration only becomes possible later.
1528
1536
  * A record this process already put in place that is still current (a founding owner's
1529
1537
  * explicit publish, a redeemed invitation's admission, a restart's first-growth re-touch) is
1530
- * not written again ({@link publishSelfRecordUnlessCurrent}). Whether the node is connected
1531
- * yet does not matter: a device that redeems within a second of starting is.
1538
+ * not written again ({@link publishSelfRecordUnlessCurrent}); that check, not connectivity,
1539
+ * is what spares a device that redeemed within a second of starting a second write.
1540
+ *
1541
+ * A background publish ({@link publishSelfRecordInBackground}): in a party with other members
1542
+ * it waits for a reachable sibling, so two members restarted together, each alone at this
1543
+ * moment, do not each write the `CadrePeer` collection to the same revision.
1532
1544
  */
1533
1545
  private scheduleSelfRegistration;
1546
+ /**
1547
+ * Run `publish`, a self-record publish nothing needs made now (the boot one-shot, the heartbeat,
1548
+ * an address change, {@link registerSelfUnlessCouldFork}), unless a write now could fork the
1549
+ * party ({@link controlWriteCouldFork}). Then nothing is written: {@link selfPeerPublishDeferred}
1550
+ * is set, and the next growth drain publishes the record as it is by then. A deferral is not a
1551
+ * failure, so it neither stamps {@link lastSelfRecordPublishAt} (the reconcile pass's
1552
+ * {@link publishFirstSelfRecord} keeps trying) nor escalates a stale record.
1553
+ */
1554
+ private publishSelfRecordInBackground;
1555
+ /**
1556
+ * {@link registerSelf} for a caller that wants the record in place but cannot wait for a
1557
+ * sibling (`cadre start --owner`, so the first seed it mints carries its address). A party with
1558
+ * no other members writes at once; otherwise the write waits for a reachable sibling, as the
1559
+ * boot publish does, and this returns `'deferred'`. Two owner machines restarted together that
1560
+ * each called {@link registerSelf} here would fork the `CadrePeer` collection
1561
+ * ({@link controlWriteCouldFork}).
1562
+ */
1563
+ registerSelfUnlessCouldFork(): Promise<SelfRegistrationOutcome | 'deferred'>;
1534
1564
  /**
1535
1565
  * Publish (or refresh) this node's own signed `CadrePeer` address record so
1536
1566
  * other members can resolve its current signaling/relay multiaddrs from its
@@ -1688,8 +1718,8 @@ export declare class CadreNode implements SAppIdLookup {
1688
1718
  * would write its row on the stale side of the fork, and then never see rows written while it
1689
1719
  * was down (`cadre-invite-any-member` runs that pass and fails every time without this
1690
1720
  * condition). A connection is not enough: until the sibling identifies, the write's cohort is
1691
- * this node alone. The boot-time publish and the first-growth drain already try at the
1692
- * node's lone and first-sibling moments; this retries on the passes after.
1721
+ * this node alone. The boot-time publish (alone only in a party with no other members) and the
1722
+ * first-growth drain already try; this retries on the passes after.
1693
1723
  *
1694
1724
  * A machine admitted by invitation finds its row already holding the record its redemption
1695
1725
  * carried, and writes nothing ({@link publishSelfRecordUnlessCurrent}).
@@ -2441,6 +2471,17 @@ export declare class CadreNode implements SAppIdLookup {
2441
2471
  * using. Emits `strand:writable` after the timers are settled.
2442
2472
  */
2443
2473
  private handleStrandWritable;
2474
+ /**
2475
+ * A status from a strand's offline queue (`StartStrandConfig.offline`). The queue sending counts
2476
+ * as activity ({@link isSendingStatus}), so a check-in window that finds the cohort reachable stays
2477
+ * up while the queue drains, and the idle timer does not cut a drain short. Recorded through the
2478
+ * hibernation manager, not {@link recordStrandActivity}, whose push fan-out would wake other phones.
2479
+ *
2480
+ * Not on a strand being quiesced: activity there wakes it again once the quiesce ends, which would
2481
+ * undo a `hibernateAll` made as the app went to the background. A send that quiesce cuts short was
2482
+ * journaled before it went out, and the next build's restore re-sends it under the same action id.
2483
+ */
2484
+ private handleOfflineStatus;
2444
2485
  private handleStrandIdle;
2445
2486
  /**
2446
2487
  * Hibernate a strand: release its strand-network resources via the strand
@@ -2595,6 +2636,24 @@ export declare class CadreNode implements SAppIdLookup {
2595
2636
  whenStrandWritable(strandId: string, options?: {
2596
2637
  timeoutMs?: number;
2597
2638
  }): Promise<StrandInstance>;
2639
+ /**
2640
+ * Switch a strand between offline mode, where a commit resolves once the transaction is queued on
2641
+ * this machine, and online mode (docs/strands.md → "Offline mode"). Affects transactions begun from
2642
+ * now on; one already open keeps the acknowledgement it began with. The mode belongs to the strand:
2643
+ * a hibernating strand takes it at its next build, and it lasts across hibernation and every
2644
+ * rebuild until the process exits, after which the strand starts in `strandOffline.initialMode`.
2645
+ *
2646
+ * @throws StrandOfflineUnavailableError when `CadreNodeConfig.strandOffline` is not configured.
2647
+ * @throws when this node does not run the strand.
2648
+ */
2649
+ setStrandOfflineMode(strandId: string, mode: StrandOfflineMode): void;
2650
+ /**
2651
+ * A strand's offline mode ({@link setStrandOfflineMode}); `'online'` on a node without
2652
+ * `strandOffline`. The queue's own view, with what it holds, is `strand.database.offline.queueState()`.
2653
+ *
2654
+ * @throws when this node does not run the strand.
2655
+ */
2656
+ getStrandOfflineMode(strandId: string): StrandOfflineMode;
2598
2657
  /**
2599
2658
  * Publish a strand row to the shared control database under this node's own
2600
2659
  * owner identity, so other cadre members discover it via control-network
@@ -2845,14 +2904,22 @@ export declare class CadreNode implements SAppIdLookup {
2845
2904
  * caller ever needs removal to mean "and erase the local copy", that is a separate purge
2846
2905
  * step, not a widening of this method.
2847
2906
  *
2907
+ * Refuses while the strand's offline queue on this machine holds transactions, unless
2908
+ * `options.discardQueued` (docs/strands.md → "Leaving a strand with unsent transactions"). The
2909
+ * check and the discard run before the control-plane delete, so a delete refused afterwards
2910
+ * leaves the discarded transactions discarded.
2911
+ *
2848
2912
  * @param strandId - The `Strand` row id to remove (as passed to {@link publishStrand}).
2913
+ * @param options - `discardQueued`: withdraw the queued transactions that were never sent, and remove.
2914
+ * @throws StrandQueueNotEmptyError when the queue holds transactions and `discardQueued` is not
2915
+ * set; nothing was removed.
2849
2916
  * @throws if the node is not started, has no owner signer wired, the id is blank, or
2850
2917
  * the signer is not an enrolled owner (the schema's `Strand.AuthorizedDelete` rejects
2851
2918
  * the write and the row survives). A rejection does NOT imply the row survived: the
2852
2919
  * local stop runs after the control-plane delete has already committed, so a failure
2853
2920
  * there throws over a completed removal.
2854
2921
  */
2855
- unpublishStrand(strandId: string): Promise<void>;
2922
+ unpublishStrand(strandId: string, options?: StrandLeaveOptions): Promise<void>;
2856
2923
  /**
2857
2924
  * Seat this party's own strand membership identity key — the `StrandPartyKey` row —
2858
2925
  * for `strandId`, minting a fresh one when none is given, and adopting the stored one
@@ -3400,11 +3467,36 @@ export declare class CadreNode implements SAppIdLookup {
3400
3467
  * The other members of the strand are not told, and this party's membership row in the
3401
3468
  * strand stays, so a later re-formation reuses the same identity.
3402
3469
  *
3470
+ * Refuses while the strand's offline queue on this machine holds transactions, unless
3471
+ * `options.discardQueued` (docs/strands.md → "Leaving a strand with unsent transactions"). The
3472
+ * check and the discard run before the party-wide row is removed, so a removal refused
3473
+ * afterwards leaves the discarded transactions discarded.
3474
+ *
3475
+ * @param options - `discardQueued`: withdraw the queued transactions that were never sent, and leave.
3476
+ * @throws StrandQueueNotEmptyError when the queue holds transactions and `discardQueued` is not
3477
+ * set; nothing was left.
3403
3478
  * @throws when a party-wide row exists and this machine cannot sign its removal (not an
3404
3479
  * enrolled owner) — {@link stopStrand} stops the strand on this machine only — or when
3405
3480
  * the party-wide table cannot be read.
3406
3481
  */
3407
- forgetJoinedStrand(strandId: string): Promise<void>;
3482
+ forgetJoinedStrand(strandId: string, options?: StrandLeaveOptions): Promise<void>;
3483
+ /**
3484
+ * Before a leave changes anything: refuse while the strand's offline queue holds transactions,
3485
+ * unless the app asked to discard them (docs/strands.md → "Leaving a strand with unsent
3486
+ * transactions"). Transactions Optimystic will not withdraw stay in the journal, named in one
3487
+ * warning, and the leave goes on: they may already be saved, and a cohort that never returns
3488
+ * would otherwise block the leave forever.
3489
+ *
3490
+ * @throws StrandQueueNotEmptyError when the queue holds transactions and `discardQueued` is not set.
3491
+ * @throws when a hibernating strand has to be woken to discard and the wake fails.
3492
+ */
3493
+ private settleQueueBeforeLeave;
3494
+ /**
3495
+ * Discard from the strand's live queue, waking a hibernating strand first (its queue is back
3496
+ * once a build restores it from the journal), and return what was kept. `undefined` when the
3497
+ * wake built no database (no seed peer has answered), so there was no queue to discard from.
3498
+ */
3499
+ private discardQueuedForLeave;
3408
3500
  /**
3409
3501
  * Record `row` as a join from another party when this party's control database names it
3410
3502
  * neither as its own strand nor as a party-wide join — the one case nothing else would
@@ -4115,8 +4207,8 @@ export declare class CadreNode implements SAppIdLookup {
4115
4207
  * `OwnerKey.Authorized`; the row stores the vouching pair). The write notifies the
4116
4208
  * membership hub, so the refresh it triggers has run by the time this resolves.
4117
4209
  *
4118
- * Refused while alone ({@link ownerKeyWriteAlone}): an `OwnerKey` add has no re-issue path,
4119
- * so one committed with no control connection would stay on this node.
4210
+ * Refused while alone ({@link controlWriteCouldFork}): an `OwnerKey` add has no re-issue path,
4211
+ * so one committed with no sibling reachable would stay on this node.
4120
4212
  *
4121
4213
  * @param ownerPublicKeyB64 - the new owner's ed25519 public key (base64url).
4122
4214
  * @param options.label - the user's name for the key ("phone", "paper backup"), bound into
@@ -4128,17 +4220,27 @@ export declare class CadreNode implements SAppIdLookup {
4128
4220
  label?: string;
4129
4221
  }): Promise<boolean>;
4130
4222
  /**
4131
- * Whether an `OwnerKey` add made now ({@link addOwner}, or the re-add of
4132
- * {@link removeOwner}'s re-vouch) is refused `alone`: this node holds no control connection
4133
- * while the party has other members. An `OwnerKey` row has no update to re-touch, so nothing
4134
- * re-issues it on cohort growth, and a row written now would fork this node's history. A
4135
- * party with no other members cannot fork, so a solo founder adding an owner key is not
4136
- * refused.
4223
+ * Whether a control write made now could fork the party's history: no sibling is reachable
4224
+ * ({@link committedAlone}) while the party has other members. Such a write commits on this
4225
+ * node alone, and if another member writes the same collection meanwhile, both take it to the
4226
+ * same revision with different content, which no re-issue heals (docs/architecture.md →
4227
+ * "Writes made while alone"). A party with no other members cannot fork.
4228
+ *
4229
+ * What a caller does on `true` depends on the write. An `OwnerKey` add has no re-issue path at
4230
+ * all, so {@link addOwner}, {@link removeOwner}'s re-vouch and a local owner grant redemption
4231
+ * refuse it `alone`. A background write nothing waits on (the self-record publishes
4232
+ * {@link publishSelfRecordInBackground} makes, the pending-join runner's background attempts)
4233
+ * waits for a reachable sibling.
4137
4234
  *
4138
4235
  * Waits out an in-flight membership refresh first: until `start`'s first one lands the
4139
4236
  * member snapshot is empty, which would read as a party with no other members.
4237
+ *
4238
+ * NOTE: a snapshot that stays empty on a node that has members (a failed first membership
4239
+ * read keeps the empty snapshot; an anchor never seeded trusts no row) also reads as "no
4240
+ * other members", so such a node writes alone as before this check existed. If a fork is
4241
+ * ever traced to that state, count the party from the unfiltered `CadrePeer` rows here.
4140
4242
  */
4141
- private ownerKeyWriteAlone;
4243
+ private controlWriteCouldFork;
4142
4244
  /**
4143
4245
  * Every live owner key with its label, who vouched it, whether this node's owner signer
4144
4246
  * holds it and whether this node's trusted-owner anchor trusts it ({@link CadreOwnerKey}),
@@ -4163,7 +4265,7 @@ export declare class CadreNode implements SAppIdLookup {
4163
4265
  *
4164
4266
  * Steps: re-plan ({@link planOwnerKeyRemoval}) and throw {@link OwnerKeyRemovalRefusedError}
4165
4267
  * on a refusal; check `options.revouch` against the plan's dependents and throw before any
4166
- * write when it names anything else; refuse `alone` ({@link ownerKeyWriteAlone}) when an
4268
+ * write when it names anything else; refuse `alone` ({@link controlWriteCouldFork}) when an
4167
4269
  * `OwnerKey` row is to be re-vouched, since that re-add has no re-issue path; **re-vouch
4168
4270
  * first**, under this node's key, the dependents the caller chose to keep and this node's own
4169
4271
  * row when the key vouched it (`ControlDatabase.revouchOwnerKey`, `reauthorizePeer`), so they