@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
@@ -0,0 +1,178 @@
1
+ import { EntryNotDiscardableError, UnknownEntryError } from '@optimystic/db-core';
2
+ import type { IKVStore } from '@optimystic/db-p2p';
3
+ import type { OfflineModeState, QueuedTransactionStatus, StrandOfflineController } from '@serfab/quereus-plugin-sereus';
4
+
5
+ /**
6
+ * Which acknowledgement a strand's transactions get from the moment it is set: `'offline'`, a
7
+ * commit resolves once the transaction is queued on this machine; `'online'`, once the strand's
8
+ * members have taken it.
9
+ */
10
+ export type StrandOfflineMode = 'online' | 'offline';
11
+
12
+ /** `CadreNodeConfig.strandOffline`: what a node needs to offer offline mode on its strands. */
13
+ export interface StrandOfflineConfig {
14
+ /**
15
+ * The journal for one strand: called once per database build with the strand id (a storage scope
16
+ * key). Queued transactions are saved in it before their commit resolves, so they survive a
17
+ * restart. A throw or rejection fails the build, as a storage provider's does.
18
+ *
19
+ * Nothing in cadre-core or Optimystic closes the store, and every rebuild (a wake, a retry while
20
+ * no seed peer answers) calls this again, so return the same store for the same scope and close
21
+ * it yourself after the node stops.
22
+ */
23
+ journal: (scope: string) => IKVStore | Promise<IKVStore>;
24
+ /** The mode every strand starts in until the app sets one. Default `'online'`. */
25
+ initialMode?: StrandOfflineMode;
26
+ }
27
+
28
+ /**
29
+ * `StrandDatabase.offline`: the strand connection's offline queue, less the mode switch, which
30
+ * goes through `CadreNode.setStrandOfflineMode` so that it outlives the database. Listeners taken
31
+ * here die with the database; the `CadreNode` events do not.
32
+ */
33
+ export type StrandOfflineQueue = Pick<StrandOfflineController,
34
+ 'queueState' | 'entries' | 'status' | 'outcomes' | 'acknowledge' | 'discard' | 'whenDrained' | 'onStatus' | 'onQueueState'>;
35
+
36
+ /** `strand:offline-status`: one status of one queued transaction. */
37
+ export interface StrandOfflineStatus {
38
+ readonly strandId: string;
39
+ readonly status: QueuedTransactionStatus;
40
+ }
41
+
42
+ /** `strand:queue-state`: the queue's state, for a status line. */
43
+ export interface StrandQueueState {
44
+ readonly strandId: string;
45
+ readonly state: OfflineModeState;
46
+ }
47
+
48
+ /**
49
+ * Where a strand database reports its offline queue. Retained with the launch config and handed to
50
+ * every database built for the launch, so the reports outlive hibernation and every other rebuild.
51
+ */
52
+ export interface StrandOfflineListeners {
53
+ onOfflineStatus?: (event: StrandOfflineStatus) => void;
54
+ onQueueState?: (event: StrandQueueState) => void;
55
+ }
56
+
57
+ /** Thrown by `CadreNode.setStrandOfflineMode` on a node configured without `strandOffline`. */
58
+ export class StrandOfflineUnavailableError extends Error {
59
+ readonly strandId: string;
60
+
61
+ constructor(strandId: string) {
62
+ super(
63
+ `Cannot put strand ${strandId} in offline mode: this node has no offline journal ` +
64
+ '(CadreNodeConfig.strandOffline is not configured), and a queue lost with the process would ' +
65
+ 'break the promise a local commit makes.'
66
+ );
67
+ this.name = 'StrandOfflineUnavailableError';
68
+ this.strandId = strandId;
69
+ }
70
+ }
71
+
72
+ /** Options for leaving a strand: `CadreNode.forgetJoinedStrand` and `CadreNode.unpublishStrand`. */
73
+ export interface StrandLeaveOptions {
74
+ /**
75
+ * Withdraw the strand's queued offline transactions that were never sent, then leave. Without
76
+ * it a leave refuses with {@link StrandQueueNotEmptyError} while the queue holds any: the app was
77
+ * told each of them was acknowledged, so dropping them is the user's choice to make.
78
+ */
79
+ discardQueued?: boolean;
80
+ }
81
+
82
+ /**
83
+ * Thrown by a leave ({@link StrandLeaveOptions}) while the strand's offline queue holds
84
+ * transactions and `discardQueued` was not given. Nothing was left or removed.
85
+ */
86
+ export class StrandQueueNotEmptyError extends Error {
87
+ readonly strandId: string;
88
+ /** The queued transactions, by the `StampId()` each one's statements returned. */
89
+ readonly transactionIds: readonly string[];
90
+
91
+ constructor(strandId: string, transactionIds: readonly string[]) {
92
+ super(
93
+ `Cannot leave strand ${strandId}: its offline queue holds ${transactionIds.length} transaction(s) its ` +
94
+ `members have not taken yet (${transactionIds.join(', ')}), so nothing was left or removed. Show the user ` +
95
+ 'the unsent changes, then leave again with { discardQueued: true } to drop them, or once ' +
96
+ 'strand.database.offline.whenDrained() resolves.'
97
+ );
98
+ this.name = 'StrandQueueNotEmptyError';
99
+ this.strandId = strandId;
100
+ this.transactionIds = transactionIds;
101
+ }
102
+ }
103
+
104
+ /**
105
+ * Withdraw every transaction in `queue` that was never sent, for a leave the app confirmed
106
+ * ({@link StrandLeaveOptions.discardQueued}). Returns the ones Optimystic refuses to withdraw
107
+ * (`EntryNotDiscardableError`): each was sent, or is being sent or restored, and the strand's
108
+ * members may already hold it, so it stays in the journal. One that settled meanwhile, or failed
109
+ * with a transaction it depended on, is already gone.
110
+ */
111
+ export function discardQueued(queue: Pick<StrandOfflineController, 'entries' | 'discard'>): string[] {
112
+ const kept: string[] = [];
113
+ for (const { transactionId } of queue.entries()) {
114
+ try {
115
+ queue.discard(transactionId);
116
+ } catch (error) {
117
+ if (error instanceof EntryNotDiscardableError) {
118
+ kept.push(transactionId);
119
+ } else if (!(error instanceof UnknownEntryError)) {
120
+ throw error;
121
+ }
122
+ }
123
+ }
124
+ return kept;
125
+ }
126
+
127
+ /**
128
+ * Whether a status shows the queue sending, which counts as strand activity for hibernation. A
129
+ * transaction waiting for its cohort (`queued`) is not, or an offline phone would never hibernate.
130
+ */
131
+ // NOTE: a send that keeps failing transiently while the cohort reads as reachable is re-tried at
132
+ // least every 60 s (Optimystic's re-check cap), so such a strand never idles. If a phone is found
133
+ // kept awake that way, count only `committed`, `partially-committed` and `failed`.
134
+ export function isSendingStatus(status: QueuedTransactionStatus): boolean {
135
+ return status.kind === 'submitting' || status.kind === 'committed'
136
+ || status.kind === 'partially-committed' || status.kind === 'failed';
137
+ }
138
+
139
+ /**
140
+ * What one strand database reports about its offline queue. Built once `connectToStrand` has
141
+ * returned (a restored transaction's `queued` status was delivered during its `hydrate`, before this
142
+ * subscribes: {@link published} reports the queue's state for that reason) and released first when
143
+ * the database closes, so nothing from a closing connection reaches the app.
144
+ */
145
+ export class StrandOfflineReport {
146
+ private wasPublished = false;
147
+ private released = false;
148
+ private readonly unsubscribe: () => void;
149
+
150
+ constructor(
151
+ private readonly strandId: string,
152
+ private readonly listeners: StrandOfflineListeners,
153
+ private readonly queue: StrandOfflineController | undefined
154
+ ) {
155
+ const unsubscribeStatus = queue?.onStatus((status) => listeners.onOfflineStatus?.({ strandId, status }));
156
+ const unsubscribeState = queue?.onQueueState((state) => listeners.onQueueState?.({ strandId, state }));
157
+ this.unsubscribe = () => {
158
+ unsubscribeStatus?.();
159
+ unsubscribeState?.();
160
+ };
161
+ }
162
+
163
+ /** The database was handed to the app: report the queue's state. */
164
+ published(): void {
165
+ if (this.released || this.wasPublished) {
166
+ return;
167
+ }
168
+ this.wasPublished = true;
169
+ if (this.queue) {
170
+ this.listeners.onQueueState?.({ strandId: this.strandId, state: this.queue.queueState() });
171
+ }
172
+ }
173
+
174
+ release(): void {
175
+ this.released = true;
176
+ this.unsubscribe();
177
+ }
178
+ }
package/src/types.ts CHANGED
@@ -9,6 +9,8 @@ import type { StrandMembershipReconciliationConfig } from './strand-membership-r
9
9
  import type { StrandFirstSyncConfig } from './strand-first-sync-gate.js';
10
10
  import type { StrandDatabase } from './strand-database.js';
11
11
  import type { StrandCommitDurability, StrandCommitHeld } from './strand-commit-durability.js';
12
+ import type { StrandOfflineConfig, StrandOfflineStatus, StrandQueueState } from './strand-offline.js';
13
+ import type { StrandAppSchemaDeferred } from './strand-app-schema-report.js';
12
14
  import type { SeedTrustPolicy } from './seed-trust-policy.js';
13
15
  import type { KeyStore, KeyId } from './key-store.js';
14
16
  import type { TrustedOwnerStore, TrustSource } from './trusted-owner-store.js';
@@ -961,6 +963,16 @@ export interface CadreNodeConfig {
961
963
  */
962
964
  strandFirstSync?: StrandFirstSyncConfig;
963
965
 
966
+ /**
967
+ * Offline mode on this node's strands (docs/strands.md → "Offline mode"): with it, an app can
968
+ * switch a strand so that a commit resolves once the transaction is queued on this machine
969
+ * (`CadreNode.setStrandOfflineMode`), and the queue is saved in `journal` so it survives a
970
+ * restart. Absent: no strand gets a queue, and `setStrandOfflineMode` throws
971
+ * `StrandOfflineUnavailableError`. The control database never gets one. The always-on programs
972
+ * configure none: a node that is always connected gains nothing from a local acknowledgement.
973
+ */
974
+ strandOffline?: StrandOfflineConfig;
975
+
964
976
  /** Hibernation configuration */
965
977
  hibernation?: HibernationConfig;
966
978
 
@@ -1760,6 +1772,30 @@ export interface CadreNodeEvents {
1760
1772
  * because its database closed, hibernated or the node restarted.
1761
1773
  */
1762
1774
  'strand:commit-held': StrandCommitHeld;
1775
+ /**
1776
+ * Emitted for every status of a transaction queued in a strand's offline queue (`queued`,
1777
+ * `submitting`, `committed`, …), by the `StampId()` its statement returned. Survives hibernation
1778
+ * and every rebuild, unlike a listener on `StrandDatabase.offline`. A restored transaction's
1779
+ * `queued` status after a restart is not emitted: it was delivered before anything subscribed, so
1780
+ * read `offline.entries()` on the `strand:queue-state` that publication emits. A drained commit
1781
+ * also emits `strand:commit-durability` as an online one does. See docs/strands.md, "Offline mode".
1782
+ */
1783
+ 'strand:offline-status': StrandOfflineStatus;
1784
+ /**
1785
+ * Emitted whenever a strand's offline queue or mode changes, and once each time the strand's
1786
+ * database is published with the queue's state at that moment.
1787
+ */
1788
+ 'strand:queue-state': StrandQueueState;
1789
+ /**
1790
+ * Emitted when a strand's database is published while its `App` schema apply is held back, by
1791
+ * queued offline transactions or by a strand cohort it could not reach (`waitingFor`): the database
1792
+ * runs the earlier schema although `StrandInstance.sAppInfo` names the new version, and
1793
+ * `StrandDatabase.appSchemaState()` reads `'waiting-for-queue'` or `'waiting-for-cohort'`. A retry
1794
+ * that fails for another reason is emitted as `strand:error`; a cancelled one emits nothing.
1795
+ */
1796
+ 'strand:app-schema-deferred': StrandAppSchemaDeferred;
1797
+ /** Emitted when an `App` schema apply that `strand:app-schema-deferred` reported lands. */
1798
+ 'strand:app-schema-applied': { strandId: string };
1763
1799
  /**
1764
1800
  * Emitted when this node discovers it is no longer a member of a CLOSED
1765
1801
  * strand — its party was removed by a manager, or it left. Detected by the