@serfab/cadre-core 1.9.0 → 1.11.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 (119) hide show
  1. package/dist/cadre-node.d.ts +148 -44
  2. package/dist/cadre-node.js +363 -139
  3. package/dist/cadre-node.js.map +1 -1
  4. package/dist/control-authorization.d.ts +2 -2
  5. package/dist/control-authorization.js +1 -0
  6. package/dist/control-authorization.js.map +1 -1
  7. package/dist/control-cohort.d.ts +1 -1
  8. package/dist/control-cohort.js +1 -1
  9. package/dist/control-database.d.ts +105 -14
  10. package/dist/control-database.js +242 -73
  11. package/dist/control-database.js.map +1 -1
  12. package/dist/control-formation-recorder.d.ts +6 -6
  13. package/dist/control-formation-recorder.js +7 -8
  14. package/dist/control-formation-recorder.js.map +1 -1
  15. package/dist/control-schema.d.ts +1 -1
  16. package/dist/control-schema.js +1066 -979
  17. package/dist/control-schema.js.map +1 -1
  18. package/dist/control-write-retry.d.ts +21 -15
  19. package/dist/control-write-retry.js +21 -15
  20. package/dist/control-write-retry.js.map +1 -1
  21. package/dist/diagnostics/connection-path.d.ts +1 -18
  22. package/dist/diagnostics/connection-path.js +1 -20
  23. package/dist/diagnostics/connection-path.js.map +1 -1
  24. package/dist/identity-key.d.ts +0 -30
  25. package/dist/identity-key.js +7 -40
  26. package/dist/identity-key.js.map +1 -1
  27. package/dist/index.d.ts +8 -5
  28. package/dist/index.js +12 -6
  29. package/dist/index.js.map +1 -1
  30. package/dist/invitation-bootstrap.d.ts +32 -0
  31. package/dist/invitation-bootstrap.js +30 -0
  32. package/dist/invitation-bootstrap.js.map +1 -0
  33. package/dist/link-budget.d.ts +204 -119
  34. package/dist/link-budget.js +208 -120
  35. package/dist/link-budget.js.map +1 -1
  36. package/dist/membership-connection-gater.d.ts +3 -3
  37. package/dist/membership-connection-gater.js +3 -3
  38. package/dist/peer-dial.d.ts +18 -14
  39. package/dist/peer-dial.js +18 -14
  40. package/dist/peer-dial.js.map +1 -1
  41. package/dist/peer-join-backfill.d.ts +3 -3
  42. package/dist/pending-join-runner.d.ts +186 -0
  43. package/dist/pending-join-runner.js +649 -0
  44. package/dist/pending-join-runner.js.map +1 -0
  45. package/dist/push-fanout.d.ts +1 -1
  46. package/dist/push-fanout.js +1 -1
  47. package/dist/relay-stun.d.ts +24 -0
  48. package/dist/relay-stun.js +51 -0
  49. package/dist/relay-stun.js.map +1 -0
  50. package/dist/seed-bootstrap.d.ts +2 -2
  51. package/dist/seed-bootstrap.js +5 -5
  52. package/dist/strand-addr-protocol.js +2 -2
  53. package/dist/strand-formation-deadlines.d.ts +5 -5
  54. package/dist/strand-formation-deadlines.js +5 -5
  55. package/dist/strand-formation-manager.d.ts +18 -4
  56. package/dist/strand-formation-manager.js +48 -33
  57. package/dist/strand-formation-manager.js.map +1 -1
  58. package/dist/strand-formation-protocol.d.ts +71 -14
  59. package/dist/strand-formation-protocol.js +222 -47
  60. package/dist/strand-formation-protocol.js.map +1 -1
  61. package/dist/strand-formation-rejection.d.ts +94 -0
  62. package/dist/strand-formation-rejection.js +77 -0
  63. package/dist/strand-formation-rejection.js.map +1 -0
  64. package/dist/strand-instance-manager.js +4 -1
  65. package/dist/strand-instance-manager.js.map +1 -1
  66. package/dist/strand-membership-writer.js +32 -32
  67. package/dist/strand-solicitation.d.ts +11 -3
  68. package/dist/strand-solicitation.js +9 -0
  69. package/dist/strand-solicitation.js.map +1 -1
  70. package/dist/types.d.ts +103 -17
  71. package/dist/types.js +5 -9
  72. package/dist/types.js.map +1 -1
  73. package/package.json +22 -22
  74. package/src/cadre-node.ts +8654 -8400
  75. package/src/control-authorization.ts +2 -1
  76. package/src/control-cohort.ts +128 -128
  77. package/src/control-database.ts +303 -82
  78. package/src/control-formation-recorder.ts +353 -354
  79. package/src/control-read-retry.ts +216 -216
  80. package/src/control-schema.ts +1078 -991
  81. package/src/control-write-retry.ts +567 -561
  82. package/src/diagnostics/connection-path.ts +1 -29
  83. package/src/formation-approval.ts +610 -610
  84. package/src/hibernation-manager.ts +638 -638
  85. package/src/identity-key.ts +7 -62
  86. package/src/index.ts +644 -627
  87. package/src/invitation-bootstrap.ts +40 -0
  88. package/src/joined-strand-store.ts +498 -498
  89. package/src/link-budget.ts +684 -583
  90. package/src/membership-connection-gater.ts +570 -570
  91. package/src/node-local-snapshot.ts +298 -298
  92. package/src/peer-dial.ts +315 -311
  93. package/src/peer-join-backfill.ts +710 -710
  94. package/src/pending-join-runner.ts +735 -0
  95. package/src/push-fanout.ts +1 -1
  96. package/src/relay-reservation.ts +1121 -1121
  97. package/src/relay-server.ts +98 -98
  98. package/src/relay-stun.ts +58 -0
  99. package/src/seed-bootstrap.ts +1448 -1448
  100. package/src/strand-addr-protocol.ts +547 -547
  101. package/src/strand-database.ts +287 -287
  102. package/src/strand-first-sync-gate.ts +391 -391
  103. package/src/strand-formation-deadlines.ts +227 -227
  104. package/src/strand-formation-manager.ts +725 -700
  105. package/src/strand-formation-protocol.ts +1036 -802
  106. package/src/strand-formation-rejection.ts +129 -0
  107. package/src/strand-fret-addrs.ts +145 -145
  108. package/src/strand-instance-manager.ts +1789 -1786
  109. package/src/strand-membership-writer.ts +1974 -1974
  110. package/src/strand-network-state-file.ts +40 -40
  111. package/src/strand-network-state.ts +243 -243
  112. package/src/strand-revocation-enforcer.ts +654 -654
  113. package/src/strand-solicitation.ts +532 -519
  114. package/src/strand-wake-protocol.ts +401 -401
  115. package/src/types.ts +2222 -2140
  116. package/dist/diagnostics/webrtc-turn-tracker.d.ts +0 -50
  117. package/dist/diagnostics/webrtc-turn-tracker.js +0 -196
  118. package/dist/diagnostics/webrtc-turn-tracker.js.map +0 -1
  119. package/src/diagnostics/webrtc-turn-tracker.ts +0 -222
@@ -1,298 +1,298 @@
1
- /**
2
- * Shared machinery for the node's **node-local** records — the small,
3
- * NEVER-replicated, per-party sets a node keeps for itself and never sources
4
- * from shared group state:
5
- *
6
- * - the trusted-owner anchor (`trusted-owner-store.ts`),
7
- * - the cold-start bootstrap-peer store (`bootstrap-peer-store.ts`),
8
- * - the strand network state (`strand-network-state.ts`): per strand, the state
9
- * Optimystic's db-p2p saved for the strand node, opaque here past a structural
10
- * check.
11
- *
12
- * ANOTHER node-local record — the enrolled-machine count
13
- * (`enrolled-machine-store.ts`) — reuses {@link DurableSlot} but deliberately
14
- * NOT this module's snapshot machinery: it is one scalar rather than an entry
15
- * map, and it must cold-start rather than throw on an unreadable slot. Its
16
- * module comment carries the reasoning; do not fold it in here.
17
- *
18
- * Every record here is the same mechanism with a different payload: a
19
- * `{ version, partyId, <entries> }` envelope, snapshot-written whole on every
20
- * change, loaded under one fail-safe-but-not-fail-silent policy. The only part
21
- * that differs per platform is WHERE the bytes are kept — Node writes a file,
22
- * a browser writes IndexedDB, React Native writes SecureStore or its LevelDB
23
- * KV, NativeScript writes SQLite. That difference is the whole of
24
- * {@link DurableSlot}, which the embedding app supplies; everything else lives
25
- * here.
26
- *
27
- * This module is cross-platform (no `node:` imports), so it is safe in the
28
- * package's default entry — the Node-only `FileDurableSlot`
29
- * (`file-durable-slot.ts`) is what stays behind the `*-store-file` subpaths.
30
- */
31
- import debug from 'debug';
32
-
33
- const log = debug('sereus:cadre:node-local-snapshot');
34
-
35
- /**
36
- * A single durable text slot, supplied by the embedding app. The only
37
- * platform-specific part of a persistent node-local store.
38
- */
39
- export interface DurableSlot {
40
- /**
41
- * The slot's persisted text, or `undefined` when it was never written.
42
- *
43
- * MUST throw (not return `undefined`) when the slot exists but cannot be
44
- * read. That distinction is load-bearing: `undefined` means "cold start,
45
- * nothing was ever here", and callers snapshot-write the whole record, so a
46
- * failed read reported as `undefined` silently converts a recoverable error
47
- * (permissions, a blocked database upgrade, an I/O fault) into destruction
48
- * of a still-intact record on the next save.
49
- *
50
- * A slot always reports the fault; what a READER does with it is the
51
- * reader's policy. The two records here rethrow it; the enrolled-machine
52
- * count catches it and cold-starts, because losing a repair hint must not
53
- * stop a node. So an implementation must never soften this to `undefined`
54
- * on a reader's behalf.
55
- */
56
- load(): Promise<string | undefined>;
57
- /**
58
- * Durably replace the slot's text. Callers snapshot-write the whole record,
59
- * so an implementation never needs to merge.
60
- */
61
- save(text: string): Promise<void>;
62
- }
63
-
64
- /** Envelope version. Guards future migrations; an unknown version loads empty. */
65
- const ENVELOPE_VERSION = 1;
66
-
67
- /**
68
- * What one unusable entry means for its siblings — the single intentional
69
- * difference between the records:
70
- *
71
- * - `'discard-all'` (the trusted-owner anchor): a record that cannot be read
72
- * in full is not an anchor. Trusting a *subset* of the keys a file claims is
73
- * a silent, security-relevant downgrade, so the whole record is discarded
74
- * and the node trusts no one until re-seeded out of band.
75
- * - `'drop-entry'` (the bootstrap-peer store and the strand network state):
76
- * the record is a best-effort dial list and nothing in
77
- * it is trust-bearing, so one junk entry must not discard a stranded node's
78
- * only remaining way back to its party or its strand's peers.
79
- */
80
- export type UnusableEntryPolicy = 'discard-all' | 'drop-entry';
81
-
82
- /** What a particular node-local record persists, and how it validates it. */
83
- export interface NodeLocalSnapshotSpec<E> {
84
- /** Human name of the record, used in load errors and logs. */
85
- readonly label: string;
86
- /** Envelope property holding the `key -> entry` record (`owners`, `peers`). */
87
- readonly payloadKey: string;
88
- /** See {@link UnusableEntryPolicy}. */
89
- readonly unusableEntry: UnusableEntryPolicy;
90
- /**
91
- * Validate one persisted entry, returning the value to hold in memory
92
- * (copied, so later mutation of the parsed JSON cannot reach it) or
93
- * `undefined` when the entry is unusable.
94
- */
95
- readonly acceptEntry: (key: string, entry: unknown) => E | undefined;
96
- }
97
-
98
- /**
99
- * A loaded node-local record over a {@link DurableSlot}: an in-memory
100
- * `key -> entry` map plus a serialised full-snapshot write chain.
101
- *
102
- * NOTE: writes are serialised in-process only. Two processes (or two browser
103
- * tabs) sharing ONE slot for ONE party would each snapshot-write their own
104
- * view, so the loser's entries are dropped. Fine for every backend today —
105
- * each node gets its own directory / origin database — but a single slot
106
- * backing two concurrent nodes of one party needs a lock or a merge-on-write,
107
- * not a snapshot replace.
108
- */
109
- export class NodeLocalSnapshot<E> {
110
- /**
111
- * Serialises persists: every write ({@link put}, {@link remove}) snapshot-writes
112
- * the full entry set, so chaining writes keeps them ordered and the last landed
113
- * snapshot complete.
114
- */
115
- private writeChain: Promise<void> = Promise.resolve();
116
-
117
- private constructor(
118
- private readonly slot: DurableSlot,
119
- readonly partyId: string,
120
- private readonly spec: NodeLocalSnapshotSpec<E>,
121
- private readonly entries: Map<string, E>
122
- ) {}
123
-
124
- /**
125
- * Load (or cold-start) the party's record from `slot`. Failure policy, per
126
- * the trust model of the anchor this was first written for: only a real,
127
- * well-formed, matching-party record yields entries.
128
- *
129
- * A *decidable* non-record — slot never written, unparsable JSON, unknown
130
- * envelope shape, `partyId` mismatch (a slot reused for a different party
131
- * must not leak into this one) — yields an EMPTY record, the safe direction:
132
- * the node holds nothing until it is re-seeded, rather than acting on stale
133
- * or foreign state.
134
- *
135
- * A slot that is *present but unreadable* ({@link DurableSlot.load} threw)
136
- * is NOT decidable and **throws** instead — mirroring `FileKeyStore.get`.
137
- * Loading empty there would both hide a real misconfiguration and let the
138
- * next {@link put} snapshot-write silently destroy a still-intact record.
139
- */
140
- static async open<E>(
141
- slot: DurableSlot,
142
- partyId: string,
143
- spec: NodeLocalSnapshotSpec<E>
144
- ): Promise<NodeLocalSnapshot<E>> {
145
- return new NodeLocalSnapshot(slot, partyId, spec, await loadEntries(slot, partyId, spec));
146
- }
147
-
148
- has(key: string): boolean {
149
- return this.entries.has(key);
150
- }
151
-
152
- /** The entry held for `key`, if any. Needs no copy: {@link put} replaces an entry, never mutates it. */
153
- get(key: string): E | undefined {
154
- return this.entries.get(key);
155
- }
156
-
157
- /** Fresh copy of the keys — a snapshot decoupled from later {@link put} / {@link remove} calls. */
158
- keySnapshot(): Set<string> {
159
- return new Set(this.entries.keys());
160
- }
161
-
162
- /**
163
- * Fresh copy of the `key -> entry` map — a snapshot decoupled from later
164
- * {@link put} / {@link remove} calls. The entries themselves need no copy:
165
- * {@link put} REPLACES an entry rather than mutating it in place.
166
- */
167
- entrySnapshot(): Map<string, E> {
168
- return new Map(this.entries);
169
- }
170
-
171
- /**
172
- * Add or replace an entry: the in-memory map updates SYNCHRONOUSLY (so a
173
- * synchronous caller may consult the store the moment this returns), then
174
- * the full snapshot is persisted. A persist failure rejects the returned
175
- * promise but leaves the entry in memory — this session's decision stands,
176
- * and any later successful write re-lands the complete set.
177
- */
178
- put(key: string, entry: E): Promise<void> {
179
- this.entries.set(key, entry);
180
- return this.queuePersist();
181
- }
182
-
183
- /**
184
- * Remove an entry, with {@link put}'s contract: the in-memory map updates
185
- * SYNCHRONOUSLY, then the full snapshot is persisted. Removing an absent key
186
- * changes nothing, so it writes nothing.
187
- */
188
- remove(key: string): Promise<void> {
189
- if (!this.entries.delete(key)) {
190
- return Promise.resolve();
191
- }
192
- return this.queuePersist();
193
- }
194
-
195
- /** Chain a full-snapshot write behind every earlier one (see {@link put}). */
196
- private queuePersist(): Promise<void> {
197
- const persist = this.writeChain.then(() => this.persistSnapshot());
198
- // The chain itself must survive a failed persist (the next write retries the
199
- // full snapshot); the caller still observes the rejection via `persist`.
200
- this.writeChain = persist.catch((error) => {
201
- log('%s: persist failed for party %s: %o', this.spec.label, this.partyId, error);
202
- });
203
- return persist;
204
- }
205
-
206
- private async persistSnapshot(): Promise<void> {
207
- const envelope: Record<string, unknown> = {
208
- version: ENVELOPE_VERSION,
209
- partyId: this.partyId,
210
- [this.spec.payloadKey]: Object.fromEntries(this.entries),
211
- };
212
- await this.slot.save(JSON.stringify(envelope, null, '\t'));
213
- }
214
- }
215
-
216
- /** Read + validate the slot's text into the initial entry map (see {@link NodeLocalSnapshot.open}). */
217
- async function loadEntries<E>(
218
- slot: DurableSlot,
219
- partyId: string,
220
- spec: NodeLocalSnapshotSpec<E>
221
- ): Promise<Map<string, E>> {
222
- const entries = new Map<string, E>();
223
- let text: string | undefined;
224
- try {
225
- text = await slot.load();
226
- } catch (error) {
227
- // Present but unreadable: the one non-decidable case, and the one that must
228
- // not cold-start empty (see NodeLocalSnapshot.open). The slot's own text is
229
- // folded into the message, not left only on `cause`: the operator-facing
230
- // print sites (e.g. `cadre-cli start`) log `error.message` alone, and the
231
- // slot is what knows the platform detail worth acting on (which file, which
232
- // database).
233
- throw new Error(
234
- `failed to read the ${spec.label} for party ${partyId}: ${error instanceof Error ? error.message : String(error)}`,
235
- { cause: error }
236
- );
237
- }
238
- if (text === undefined) return entries;
239
-
240
- let parsed: unknown;
241
- try {
242
- parsed = JSON.parse(text);
243
- } catch (error) {
244
- log('%s: corrupt slot for party %s (cold start): %o', spec.label, partyId, error);
245
- return entries;
246
- }
247
-
248
- const payload = envelopePayload(parsed, partyId, spec.payloadKey);
249
- if (!payload) {
250
- log('%s: slot for party %s has an unknown shape or a foreign partyId (cold start)', spec.label, partyId);
251
- return entries;
252
- }
253
-
254
- for (const [key, raw] of Object.entries(payload)) {
255
- const entry = spec.acceptEntry(key, raw);
256
- if (entry === undefined) {
257
- if (spec.unusableEntry === 'discard-all') {
258
- log('%s: unusable entry for %s discards the whole record (party %s, cold start)', spec.label, key, partyId);
259
- return new Map();
260
- }
261
- log('%s: dropping unusable entry for %s (party %s)', spec.label, key, partyId);
262
- continue;
263
- }
264
- entries.set(key, entry);
265
- }
266
- log('%s: loaded %d entr(ies) for party %s', spec.label, entries.size, partyId);
267
- return entries;
268
- }
269
-
270
- /**
271
- * The envelope's `key -> entry` record, or undefined on any envelope mismatch —
272
- * wrong version, wrong (or absent) `partyId`, missing or non-record payload.
273
- * Individual entries are judged separately by
274
- * {@link NodeLocalSnapshotSpec.acceptEntry}, because one bad entry does not
275
- * always mean a bad record.
276
- */
277
- function envelopePayload(
278
- value: unknown,
279
- partyId: string,
280
- payloadKey: string
281
- ): Record<string, unknown> | undefined {
282
- if (!isRecord(value)) return undefined;
283
- // A non-string partyId cannot equal the requested one, so this covers both
284
- // "malformed partyId" and "another party's record".
285
- if (value.version !== ENVELOPE_VERSION || value.partyId !== partyId) return undefined;
286
- const payload = value[payloadKey];
287
- return isRecord(payload) ? payload : undefined;
288
- }
289
-
290
- /**
291
- * A plain JSON object usable as a `key -> value` record. Arrays are excluded
292
- * explicitly: they are `typeof 'object'`, so an array payload would otherwise
293
- * reach `acceptEntry` once per numeric index — the entries all get rejected in
294
- * the end, but by accident rather than by decision, and noisily.
295
- */
296
- function isRecord(value: unknown): value is Record<string, unknown> {
297
- return typeof value === 'object' && value !== null && !Array.isArray(value);
298
- }
1
+ /**
2
+ * Shared machinery for the node's **node-local** records — the small,
3
+ * NEVER-replicated, per-party sets a node keeps for itself and never sources
4
+ * from shared group state:
5
+ *
6
+ * - the trusted-owner anchor (`trusted-owner-store.ts`),
7
+ * - the cold-start bootstrap-peer store (`bootstrap-peer-store.ts`),
8
+ * - the strand network state (`strand-network-state.ts`): per strand, the state
9
+ * Optimystic's db-p2p saved for the strand node, opaque here past a structural
10
+ * check.
11
+ *
12
+ * ANOTHER node-local record — the enrolled-machine count
13
+ * (`enrolled-machine-store.ts`) — reuses {@link DurableSlot} but deliberately
14
+ * NOT this module's snapshot machinery: it is one scalar rather than an entry
15
+ * map, and it must cold-start rather than throw on an unreadable slot. Its
16
+ * module comment carries the reasoning; do not fold it in here.
17
+ *
18
+ * Every record here is the same mechanism with a different payload: a
19
+ * `{ version, partyId, <entries> }` envelope, snapshot-written whole on every
20
+ * change, loaded under one fail-safe-but-not-fail-silent policy. The only part
21
+ * that differs per platform is WHERE the bytes are kept — Node writes a file,
22
+ * a browser writes IndexedDB, React Native writes SecureStore or its LevelDB
23
+ * KV, NativeScript writes SQLite. That difference is the whole of
24
+ * {@link DurableSlot}, which the embedding app supplies; everything else lives
25
+ * here.
26
+ *
27
+ * This module is cross-platform (no `node:` imports), so it is safe in the
28
+ * package's default entry — the Node-only `FileDurableSlot`
29
+ * (`file-durable-slot.ts`) is what stays behind the `*-store-file` subpaths.
30
+ */
31
+ import debug from 'debug';
32
+
33
+ const log = debug('sereus:cadre:node-local-snapshot');
34
+
35
+ /**
36
+ * A single durable text slot, supplied by the embedding app. The only
37
+ * platform-specific part of a persistent node-local store.
38
+ */
39
+ export interface DurableSlot {
40
+ /**
41
+ * The slot's persisted text, or `undefined` when it was never written.
42
+ *
43
+ * MUST throw (not return `undefined`) when the slot exists but cannot be
44
+ * read. That distinction is load-bearing: `undefined` means "cold start,
45
+ * nothing was ever here", and callers snapshot-write the whole record, so a
46
+ * failed read reported as `undefined` silently converts a recoverable error
47
+ * (permissions, a blocked database upgrade, an I/O fault) into destruction
48
+ * of a still-intact record on the next save.
49
+ *
50
+ * A slot always reports the fault; what a READER does with it is the
51
+ * reader's policy. The two records here rethrow it; the enrolled-machine
52
+ * count catches it and cold-starts, because losing a repair hint must not
53
+ * stop a node. So an implementation must never soften this to `undefined`
54
+ * on a reader's behalf.
55
+ */
56
+ load(): Promise<string | undefined>;
57
+ /**
58
+ * Durably replace the slot's text. Callers snapshot-write the whole record,
59
+ * so an implementation never needs to merge.
60
+ */
61
+ save(text: string): Promise<void>;
62
+ }
63
+
64
+ /** Envelope version. Guards future migrations; an unknown version loads empty. */
65
+ const ENVELOPE_VERSION = 1;
66
+
67
+ /**
68
+ * What one unusable entry means for its siblings — the single intentional
69
+ * difference between the records:
70
+ *
71
+ * - `'discard-all'` (the trusted-owner anchor): a record that cannot be read
72
+ * in full is not an anchor. Trusting a *subset* of the keys a file claims is
73
+ * a silent, security-relevant downgrade, so the whole record is discarded
74
+ * and the node trusts no one until re-seeded out of band.
75
+ * - `'drop-entry'` (the bootstrap-peer store and the strand network state):
76
+ * the record is a best-effort dial list and nothing in
77
+ * it is trust-bearing, so one junk entry must not discard a stranded node's
78
+ * only remaining way back to its party or its strand's peers.
79
+ */
80
+ export type UnusableEntryPolicy = 'discard-all' | 'drop-entry';
81
+
82
+ /** What a particular node-local record persists, and how it validates it. */
83
+ export interface NodeLocalSnapshotSpec<E> {
84
+ /** Human name of the record, used in load errors and logs. */
85
+ readonly label: string;
86
+ /** Envelope property holding the `key -> entry` record (`owners`, `peers`). */
87
+ readonly payloadKey: string;
88
+ /** See {@link UnusableEntryPolicy}. */
89
+ readonly unusableEntry: UnusableEntryPolicy;
90
+ /**
91
+ * Validate one persisted entry, returning the value to hold in memory
92
+ * (copied, so later mutation of the parsed JSON cannot reach it) or
93
+ * `undefined` when the entry is unusable.
94
+ */
95
+ readonly acceptEntry: (key: string, entry: unknown) => E | undefined;
96
+ }
97
+
98
+ /**
99
+ * A loaded node-local record over a {@link DurableSlot}: an in-memory
100
+ * `key -> entry` map plus a serialised full-snapshot write chain.
101
+ *
102
+ * NOTE: writes are serialised in-process only. Two processes (or two browser
103
+ * tabs) sharing ONE slot for ONE party would each snapshot-write their own
104
+ * view, so the loser's entries are dropped. Fine for every backend today —
105
+ * each node gets its own directory / origin database — but a single slot
106
+ * backing two concurrent nodes of one party needs a lock or a merge-on-write,
107
+ * not a snapshot replace.
108
+ */
109
+ export class NodeLocalSnapshot<E> {
110
+ /**
111
+ * Serialises persists: every write ({@link put}, {@link remove}) snapshot-writes
112
+ * the full entry set, so chaining writes keeps them ordered and the last landed
113
+ * snapshot complete.
114
+ */
115
+ private writeChain: Promise<void> = Promise.resolve();
116
+
117
+ private constructor(
118
+ private readonly slot: DurableSlot,
119
+ readonly partyId: string,
120
+ private readonly spec: NodeLocalSnapshotSpec<E>,
121
+ private readonly entries: Map<string, E>
122
+ ) {}
123
+
124
+ /**
125
+ * Load (or cold-start) the party's record from `slot`. Failure policy, per
126
+ * the trust model of the anchor this was first written for: only a real,
127
+ * well-formed, matching-party record yields entries.
128
+ *
129
+ * A *decidable* non-record — slot never written, unparsable JSON, unknown
130
+ * envelope shape, `partyId` mismatch (a slot reused for a different party
131
+ * must not leak into this one) — yields an EMPTY record, the safe direction:
132
+ * the node holds nothing until it is re-seeded, rather than acting on stale
133
+ * or foreign state.
134
+ *
135
+ * A slot that is *present but unreadable* ({@link DurableSlot.load} threw)
136
+ * is NOT decidable and **throws** instead — mirroring `FileKeyStore.get`.
137
+ * Loading empty there would both hide a real misconfiguration and let the
138
+ * next {@link put} snapshot-write silently destroy a still-intact record.
139
+ */
140
+ static async open<E>(
141
+ slot: DurableSlot,
142
+ partyId: string,
143
+ spec: NodeLocalSnapshotSpec<E>
144
+ ): Promise<NodeLocalSnapshot<E>> {
145
+ return new NodeLocalSnapshot(slot, partyId, spec, await loadEntries(slot, partyId, spec));
146
+ }
147
+
148
+ has(key: string): boolean {
149
+ return this.entries.has(key);
150
+ }
151
+
152
+ /** The entry held for `key`, if any. Needs no copy: {@link put} replaces an entry, never mutates it. */
153
+ get(key: string): E | undefined {
154
+ return this.entries.get(key);
155
+ }
156
+
157
+ /** Fresh copy of the keys — a snapshot decoupled from later {@link put} / {@link remove} calls. */
158
+ keySnapshot(): Set<string> {
159
+ return new Set(this.entries.keys());
160
+ }
161
+
162
+ /**
163
+ * Fresh copy of the `key -> entry` map — a snapshot decoupled from later
164
+ * {@link put} / {@link remove} calls. The entries themselves need no copy:
165
+ * {@link put} REPLACES an entry rather than mutating it in place.
166
+ */
167
+ entrySnapshot(): Map<string, E> {
168
+ return new Map(this.entries);
169
+ }
170
+
171
+ /**
172
+ * Add or replace an entry: the in-memory map updates SYNCHRONOUSLY (so a
173
+ * synchronous caller may consult the store the moment this returns), then
174
+ * the full snapshot is persisted. A persist failure rejects the returned
175
+ * promise but leaves the entry in memory — this session's decision stands,
176
+ * and any later successful write re-lands the complete set.
177
+ */
178
+ put(key: string, entry: E): Promise<void> {
179
+ this.entries.set(key, entry);
180
+ return this.queuePersist();
181
+ }
182
+
183
+ /**
184
+ * Remove an entry, with {@link put}'s contract: the in-memory map updates
185
+ * SYNCHRONOUSLY, then the full snapshot is persisted. Removing an absent key
186
+ * changes nothing, so it writes nothing.
187
+ */
188
+ remove(key: string): Promise<void> {
189
+ if (!this.entries.delete(key)) {
190
+ return Promise.resolve();
191
+ }
192
+ return this.queuePersist();
193
+ }
194
+
195
+ /** Chain a full-snapshot write behind every earlier one (see {@link put}). */
196
+ private queuePersist(): Promise<void> {
197
+ const persist = this.writeChain.then(() => this.persistSnapshot());
198
+ // The chain itself must survive a failed persist (the next write retries the
199
+ // full snapshot); the caller still observes the rejection via `persist`.
200
+ this.writeChain = persist.catch((error) => {
201
+ log('%s: persist failed for party %s: %o', this.spec.label, this.partyId, error);
202
+ });
203
+ return persist;
204
+ }
205
+
206
+ private async persistSnapshot(): Promise<void> {
207
+ const envelope: Record<string, unknown> = {
208
+ version: ENVELOPE_VERSION,
209
+ partyId: this.partyId,
210
+ [this.spec.payloadKey]: Object.fromEntries(this.entries),
211
+ };
212
+ await this.slot.save(JSON.stringify(envelope, null, '\t'));
213
+ }
214
+ }
215
+
216
+ /** Read + validate the slot's text into the initial entry map (see {@link NodeLocalSnapshot.open}). */
217
+ async function loadEntries<E>(
218
+ slot: DurableSlot,
219
+ partyId: string,
220
+ spec: NodeLocalSnapshotSpec<E>
221
+ ): Promise<Map<string, E>> {
222
+ const entries = new Map<string, E>();
223
+ let text: string | undefined;
224
+ try {
225
+ text = await slot.load();
226
+ } catch (error) {
227
+ // Present but unreadable: the one non-decidable case, and the one that must
228
+ // not cold-start empty (see NodeLocalSnapshot.open). The slot's own text is
229
+ // folded into the message, not left only on `cause`: the operator-facing
230
+ // print sites (e.g. `cadre-cli start`) log `error.message` alone, and the
231
+ // slot is what knows the platform detail worth acting on (which file, which
232
+ // database).
233
+ throw new Error(
234
+ `failed to read the ${spec.label} for party ${partyId}: ${error instanceof Error ? error.message : String(error)}`,
235
+ { cause: error }
236
+ );
237
+ }
238
+ if (text === undefined) return entries;
239
+
240
+ let parsed: unknown;
241
+ try {
242
+ parsed = JSON.parse(text);
243
+ } catch (error) {
244
+ log('%s: corrupt slot for party %s (cold start): %o', spec.label, partyId, error);
245
+ return entries;
246
+ }
247
+
248
+ const payload = envelopePayload(parsed, partyId, spec.payloadKey);
249
+ if (!payload) {
250
+ log('%s: slot for party %s has an unknown shape or a foreign partyId (cold start)', spec.label, partyId);
251
+ return entries;
252
+ }
253
+
254
+ for (const [key, raw] of Object.entries(payload)) {
255
+ const entry = spec.acceptEntry(key, raw);
256
+ if (entry === undefined) {
257
+ if (spec.unusableEntry === 'discard-all') {
258
+ log('%s: unusable entry for %s discards the whole record (party %s, cold start)', spec.label, key, partyId);
259
+ return new Map();
260
+ }
261
+ log('%s: dropping unusable entry for %s (party %s)', spec.label, key, partyId);
262
+ continue;
263
+ }
264
+ entries.set(key, entry);
265
+ }
266
+ log('%s: loaded %d entr(ies) for party %s', spec.label, entries.size, partyId);
267
+ return entries;
268
+ }
269
+
270
+ /**
271
+ * The envelope's `key -> entry` record, or undefined on any envelope mismatch —
272
+ * wrong version, wrong (or absent) `partyId`, missing or non-record payload.
273
+ * Individual entries are judged separately by
274
+ * {@link NodeLocalSnapshotSpec.acceptEntry}, because one bad entry does not
275
+ * always mean a bad record.
276
+ */
277
+ function envelopePayload(
278
+ value: unknown,
279
+ partyId: string,
280
+ payloadKey: string
281
+ ): Record<string, unknown> | undefined {
282
+ if (!isRecord(value)) return undefined;
283
+ // A non-string partyId cannot equal the requested one, so this covers both
284
+ // "malformed partyId" and "another party's record".
285
+ if (value.version !== ENVELOPE_VERSION || value.partyId !== partyId) return undefined;
286
+ const payload = value[payloadKey];
287
+ return isRecord(payload) ? payload : undefined;
288
+ }
289
+
290
+ /**
291
+ * A plain JSON object usable as a `key -> value` record. Arrays are excluded
292
+ * explicitly: they are `typeof 'object'`, so an array payload would otherwise
293
+ * reach `acceptEntry` once per numeric index — the entries all get rejected in
294
+ * the end, but by accident rather than by decision, and noisily.
295
+ */
296
+ function isRecord(value: unknown): value is Record<string, unknown> {
297
+ return typeof value === 'object' && value !== null && !Array.isArray(value);
298
+ }