@serfab/cadre-core 0.12.0 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -2
- package/dist/cadre-node.d.ts +328 -25
- package/dist/cadre-node.js +760 -59
- package/dist/cadre-node.js.map +1 -1
- package/dist/control-database.d.ts +19 -0
- package/dist/control-database.js +51 -9
- package/dist/control-database.js.map +1 -1
- package/dist/control-schema.d.ts +1 -1
- package/dist/control-schema.js +30 -2
- package/dist/control-schema.js.map +1 -1
- package/dist/enrolled-machine-store-file.d.ts +13 -0
- package/dist/enrolled-machine-store-file.js +41 -0
- package/dist/enrolled-machine-store-file.js.map +1 -0
- package/dist/enrolled-machine-store.d.ts +81 -0
- package/dist/enrolled-machine-store.js +225 -0
- package/dist/enrolled-machine-store.js.map +1 -0
- package/dist/file-durable-slot.js +5 -3
- package/dist/file-durable-slot.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.js +23 -5
- package/dist/index.js.map +1 -1
- package/dist/membership-connection-gater.d.ts +15 -1
- package/dist/membership-connection-gater.js +9 -3
- package/dist/membership-connection-gater.js.map +1 -1
- package/dist/node-local-snapshot.d.ts +6 -0
- package/dist/node-local-snapshot.js +7 -1
- package/dist/node-local-snapshot.js.map +1 -1
- package/dist/{strand-backfill.d.ts → peer-join-backfill.d.ts} +49 -20
- package/dist/{strand-backfill.js → peer-join-backfill.js} +133 -52
- package/dist/peer-join-backfill.js.map +1 -0
- package/dist/relay-addrs.d.ts +73 -0
- package/dist/relay-addrs.js +195 -0
- package/dist/relay-addrs.js.map +1 -1
- package/dist/strand-database.d.ts +12 -0
- package/dist/strand-database.js +16 -0
- package/dist/strand-database.js.map +1 -1
- package/dist/strand-formation-manager.d.ts +12 -0
- package/dist/strand-formation-manager.js +10 -3
- package/dist/strand-formation-manager.js.map +1 -1
- package/dist/strand-formation-protocol.d.ts +76 -2
- package/dist/strand-formation-protocol.js +78 -2
- package/dist/strand-formation-protocol.js.map +1 -1
- package/dist/strand-instance-manager.d.ts +147 -14
- package/dist/strand-instance-manager.js +225 -45
- package/dist/strand-instance-manager.js.map +1 -1
- package/dist/strand-membership-writer.d.ts +12 -0
- package/dist/strand-membership-writer.js +23 -1
- package/dist/strand-membership-writer.js.map +1 -1
- package/dist/strand-network-config.d.ts +84 -0
- package/dist/strand-network-config.js +136 -0
- package/dist/strand-network-config.js.map +1 -0
- package/dist/strand-revocation-enforcer.d.ts +392 -0
- package/dist/strand-revocation-enforcer.js +533 -0
- package/dist/strand-revocation-enforcer.js.map +1 -0
- package/dist/strand-solicitation.d.ts +7 -0
- package/dist/strand-solicitation.js +8 -2
- package/dist/strand-solicitation.js.map +1 -1
- package/dist/strand-watcher.js +6 -1
- package/dist/strand-watcher.js.map +1 -1
- package/dist/types.d.ts +210 -16
- package/dist/types.js +7 -1
- package/dist/types.js.map +1 -1
- package/package.json +16 -8
- package/src/cadre-node.ts +6353 -5603
- package/src/control-database.ts +53 -9
- package/src/control-schema.ts +30 -2
- package/src/enrolled-machine-store-file.ts +43 -0
- package/src/enrolled-machine-store.ts +280 -0
- package/src/file-durable-slot.ts +5 -3
- package/src/index.ts +52 -10
- package/src/membership-connection-gater.ts +9 -3
- package/src/node-local-snapshot.ts +13 -1
- package/src/peer-addr-book.ts +251 -250
- package/src/{strand-backfill.ts → peer-join-backfill.ts} +478 -389
- package/src/relay-addrs.ts +218 -0
- package/src/strand-database.ts +17 -0
- package/src/strand-formation-manager.ts +22 -3
- package/src/strand-formation-protocol.ts +808 -692
- package/src/strand-instance-manager.ts +317 -54
- package/src/strand-membership-writer.ts +23 -1
- package/src/strand-network-config.ts +172 -0
- package/src/strand-revocation-enforcer.ts +653 -0
- package/src/strand-solicitation.ts +484 -471
- package/src/strand-watcher.ts +319 -314
- package/src/types.ts +1554 -1354
- package/dist/strand-backfill.js.map +0 -1
package/src/strand-watcher.ts
CHANGED
|
@@ -1,314 +1,319 @@
|
|
|
1
|
-
import debug from 'debug';
|
|
2
|
-
import type { StrandFilter, StrandRow } from './types.js';
|
|
3
|
-
|
|
4
|
-
const log = debug('sereus:cadre:strand-watcher');
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Outcome of evaluating a strand against the current filter.
|
|
8
|
-
* - `pass`: admit (final)
|
|
9
|
-
* - `reject`: exclude
|
|
10
|
-
* - `defer`: admit provisionally; sAppId not yet known, re-evaluate next poll
|
|
11
|
-
*/
|
|
12
|
-
type FilterDecision = 'pass' | 'reject' | 'defer';
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Ceiling on the retry delay for a strand whose launch keeps failing. Attempts
|
|
16
|
-
* are never abandoned — the failure this backoff exists for (a transient network
|
|
17
|
-
* or storage fault) can last a long time — but a permanently-unlaunchable strand
|
|
18
|
-
* must not re-attempt on every poll and storm the host app with `strand:error`.
|
|
19
|
-
*/
|
|
20
|
-
const MAX_RETRY_BACKOFF_MS = 5 * 60 * 1000;
|
|
21
|
-
|
|
22
|
-
/** Per-strand consecutive-failure state driving the launch retry backoff. */
|
|
23
|
-
interface StrandFailureState {
|
|
24
|
-
/** Consecutive `onStrandAdded` failures since the last success. */
|
|
25
|
-
failures: number;
|
|
26
|
-
/** Earliest wall-clock time (ms) at which the next attempt may run. */
|
|
27
|
-
nextAttemptAt: number;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
/**
|
|
31
|
-
* Extended strand row that includes the sAppId for filtering purposes.
|
|
32
|
-
* The sAppId is provided by the hosting application, not from the control network.
|
|
33
|
-
*/
|
|
34
|
-
export interface StrandRowWithApp extends StrandRow {
|
|
35
|
-
/** sApp ID if known (for filtering) */
|
|
36
|
-
sAppId?: string;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Callback for strand changes
|
|
41
|
-
*/
|
|
42
|
-
export interface StrandWatcherCallbacks {
|
|
43
|
-
onStrandAdded: (strand: StrandRow) => Promise<void>;
|
|
44
|
-
onStrandRemoved: (strandId: string) => Promise<void>;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Interface for querying strands from control network
|
|
49
|
-
*/
|
|
50
|
-
export interface StrandQueryable {
|
|
51
|
-
queryStrands(): Promise<StrandRow[]>;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
/**
|
|
55
|
-
* Interface for looking up sAppId for a strand (for filtering)
|
|
56
|
-
*/
|
|
57
|
-
export interface SAppIdLookup {
|
|
58
|
-
/** Get the sAppId for a strand, if known */
|
|
59
|
-
getSAppId(strandId: string): string | undefined;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Watches the control network's Strand table for changes and triggers
|
|
64
|
-
* strand instance start/stop via callbacks.
|
|
65
|
-
*
|
|
66
|
-
* Uses polling until Optimystic supports reactive subscriptions.
|
|
67
|
-
*/
|
|
68
|
-
export class StrandWatcher {
|
|
69
|
-
private readonly filter: StrandFilter;
|
|
70
|
-
private readonly pollInterval: number;
|
|
71
|
-
private readonly callbacks: StrandWatcherCallbacks;
|
|
72
|
-
private readonly queryable: StrandQueryable;
|
|
73
|
-
private readonly sAppIdLookup?: SAppIdLookup;
|
|
74
|
-
/**
|
|
75
|
-
* Wall-clock source for the retry backoff. Read fresh at each use rather than
|
|
76
|
-
* snapshotted per poll: a poll awaits `onStrandAdded`, and a launch that fails
|
|
77
|
-
* slowly (e.g. a dial timeout) would otherwise be scheduled off the clock as it
|
|
78
|
-
* read *before* the attempt, putting `nextAttemptAt` in the past. Injectable so
|
|
79
|
-
* tests can advance time deterministically without fake timers.
|
|
80
|
-
*/
|
|
81
|
-
private readonly now: () => number;
|
|
82
|
-
|
|
83
|
-
private knownStrands: Map<string, StrandRow> = new Map();
|
|
84
|
-
/** Ids admitted under a `defer` decision; re-evaluated each poll until they resolve. */
|
|
85
|
-
private provisional: Set<string> = new Set();
|
|
86
|
-
/** Ids whose last launch attempt threw, with the backoff gating their retry. */
|
|
87
|
-
private failureStates: Map<string, StrandFailureState> = new Map();
|
|
88
|
-
private pollTimer: ReturnType<typeof setInterval> | null = null;
|
|
89
|
-
private initialPollTimer: ReturnType<typeof setTimeout> | null = null;
|
|
90
|
-
private running = false;
|
|
91
|
-
|
|
92
|
-
constructor(
|
|
93
|
-
queryable: StrandQueryable,
|
|
94
|
-
callbacks: StrandWatcherCallbacks,
|
|
95
|
-
filter: StrandFilter = { mode: 'all' },
|
|
96
|
-
pollInterval: number = 5000,
|
|
97
|
-
sAppIdLookup?: SAppIdLookup,
|
|
98
|
-
now: () => number = Date.now
|
|
99
|
-
) {
|
|
100
|
-
this.queryable = queryable;
|
|
101
|
-
this.callbacks = callbacks;
|
|
102
|
-
this.filter = filter;
|
|
103
|
-
this.pollInterval = pollInterval;
|
|
104
|
-
this.sAppIdLookup = sAppIdLookup;
|
|
105
|
-
this.now = now;
|
|
106
|
-
log('StrandWatcher created with filter: %o, interval: %dms', filter, pollInterval);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
|
-
/**
|
|
110
|
-
* Evaluate a strand against the current filter, distinguishing a not-yet-known
|
|
111
|
-
* sAppId (`defer`) from a known non-match (`reject`). A `defer` admission is
|
|
112
|
-
* provisional and re-checked on subsequent polls.
|
|
113
|
-
*/
|
|
114
|
-
private evaluateFilter(strand: StrandRow): FilterDecision {
|
|
115
|
-
switch (this.filter.mode) {
|
|
116
|
-
case 'all':
|
|
117
|
-
return 'pass';
|
|
118
|
-
case 'none':
|
|
119
|
-
return 'reject';
|
|
120
|
-
case 'strandId':
|
|
121
|
-
return strand.Id === this.filter.strandId ? 'pass' : 'reject';
|
|
122
|
-
case 'sAppId': {
|
|
123
|
-
if (!this.sAppIdLookup) {
|
|
124
|
-
// No way to ever decide - admit permanently (no lookup configured).
|
|
125
|
-
return 'pass';
|
|
126
|
-
}
|
|
127
|
-
const sAppId = this.sAppIdLookup.getSAppId(strand.Id);
|
|
128
|
-
if (sAppId === undefined) {
|
|
129
|
-
// sAppId not yet known - admit provisionally and re-evaluate next poll.
|
|
130
|
-
log('sAppId unknown for strand %s - deferring filter decision', strand.Id);
|
|
131
|
-
return 'defer';
|
|
132
|
-
}
|
|
133
|
-
const matches = sAppId === this.filter.sAppId;
|
|
134
|
-
log('sAppId filter: strand %s has sAppId %s, filter wants %s, match=%s',
|
|
135
|
-
strand.Id, sAppId, this.filter.sAppId, matches);
|
|
136
|
-
return matches ? 'pass' : 'reject';
|
|
137
|
-
}
|
|
138
|
-
default:
|
|
139
|
-
return 'pass';
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Record a failed launch attempt and schedule when the strand may be retried:
|
|
145
|
-
* `pollInterval * 2^(failures-1)`, capped at {@link MAX_RETRY_BACKOFF_MS}.
|
|
146
|
-
*/
|
|
147
|
-
private recordFailure(strandId: string): void {
|
|
148
|
-
const failures = (this.failureStates.get(strandId)?.failures ?? 0) + 1;
|
|
149
|
-
const delay = Math.min(this.pollInterval * 2 ** (failures - 1), MAX_RETRY_BACKOFF_MS);
|
|
150
|
-
this.failureStates.set(strandId, { failures, nextAttemptAt: this.now() + delay });
|
|
151
|
-
log('Strand %s launch failed (%d consecutive) - next attempt in %dms', strandId, failures, delay);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Poll for strand changes.
|
|
156
|
-
*/
|
|
157
|
-
private async poll(): Promise<void> {
|
|
158
|
-
if (!this.running) return;
|
|
159
|
-
|
|
160
|
-
try {
|
|
161
|
-
const currentStrands = await this.queryable.queryStrands();
|
|
162
|
-
const currentMap = new Map(currentStrands.map(s => [s.Id, s]));
|
|
163
|
-
|
|
164
|
-
// Find added strands
|
|
165
|
-
for (const strand of currentStrands) {
|
|
166
|
-
if (this.knownStrands.has(strand.Id)) continue;
|
|
167
|
-
const failure = this.failureStates.get(strand.Id);
|
|
168
|
-
// Clock read per candidate, not once per poll: an earlier strand's launch
|
|
169
|
-
// may have taken a long time, and this one's gate must reflect that.
|
|
170
|
-
if (failure && this.now() < failure.nextAttemptAt) continue; // backing off after a failed launch
|
|
171
|
-
const decision = this.evaluateFilter(strand);
|
|
172
|
-
if (decision === 'reject') continue;
|
|
173
|
-
log('Strand added: %s', strand.Id);
|
|
174
|
-
// Set BEFORE the await: this doubles as the in-flight guard that stops the
|
|
175
|
-
// interval timer from starting a second concurrent launch for the same strand.
|
|
176
|
-
this.knownStrands.set(strand.Id, strand);
|
|
177
|
-
if (decision === 'defer') {
|
|
178
|
-
// Provisional admission: sAppId unknown, re-check on later polls.
|
|
179
|
-
this.provisional.add(strand.Id);
|
|
180
|
-
}
|
|
181
|
-
try {
|
|
182
|
-
await this.callbacks.onStrandAdded(strand);
|
|
183
|
-
this.failureStates.delete(strand.Id);
|
|
184
|
-
} catch (error) {
|
|
185
|
-
log('Error handling strand add for %s: %o', strand.Id, error);
|
|
186
|
-
// A failed launch leaves nothing running (StrandInstanceManager drops the
|
|
187
|
-
// record), so forget the strand and let a later poll retry it — gated by
|
|
188
|
-
// the backoff recorded here.
|
|
189
|
-
this
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
this.provisional.delete(strandId);
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
//
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
this.running
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
if (this.
|
|
290
|
-
|
|
291
|
-
this.
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
this.
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
1
|
+
import debug from 'debug';
|
|
2
|
+
import type { StrandFilter, StrandRow } from './types.js';
|
|
3
|
+
|
|
4
|
+
const log = debug('sereus:cadre:strand-watcher');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Outcome of evaluating a strand against the current filter.
|
|
8
|
+
* - `pass`: admit (final)
|
|
9
|
+
* - `reject`: exclude
|
|
10
|
+
* - `defer`: admit provisionally; sAppId not yet known, re-evaluate next poll
|
|
11
|
+
*/
|
|
12
|
+
type FilterDecision = 'pass' | 'reject' | 'defer';
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Ceiling on the retry delay for a strand whose launch keeps failing. Attempts
|
|
16
|
+
* are never abandoned — the failure this backoff exists for (a transient network
|
|
17
|
+
* or storage fault) can last a long time — but a permanently-unlaunchable strand
|
|
18
|
+
* must not re-attempt on every poll and storm the host app with `strand:error`.
|
|
19
|
+
*/
|
|
20
|
+
const MAX_RETRY_BACKOFF_MS = 5 * 60 * 1000;
|
|
21
|
+
|
|
22
|
+
/** Per-strand consecutive-failure state driving the launch retry backoff. */
|
|
23
|
+
interface StrandFailureState {
|
|
24
|
+
/** Consecutive `onStrandAdded` failures since the last success. */
|
|
25
|
+
failures: number;
|
|
26
|
+
/** Earliest wall-clock time (ms) at which the next attempt may run. */
|
|
27
|
+
nextAttemptAt: number;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Extended strand row that includes the sAppId for filtering purposes.
|
|
32
|
+
* The sAppId is provided by the hosting application, not from the control network.
|
|
33
|
+
*/
|
|
34
|
+
export interface StrandRowWithApp extends StrandRow {
|
|
35
|
+
/** sApp ID if known (for filtering) */
|
|
36
|
+
sAppId?: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Callback for strand changes
|
|
41
|
+
*/
|
|
42
|
+
export interface StrandWatcherCallbacks {
|
|
43
|
+
onStrandAdded: (strand: StrandRow) => Promise<void>;
|
|
44
|
+
onStrandRemoved: (strandId: string) => Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Interface for querying strands from control network
|
|
49
|
+
*/
|
|
50
|
+
export interface StrandQueryable {
|
|
51
|
+
queryStrands(): Promise<StrandRow[]>;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Interface for looking up sAppId for a strand (for filtering)
|
|
56
|
+
*/
|
|
57
|
+
export interface SAppIdLookup {
|
|
58
|
+
/** Get the sAppId for a strand, if known */
|
|
59
|
+
getSAppId(strandId: string): string | undefined;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Watches the control network's Strand table for changes and triggers
|
|
64
|
+
* strand instance start/stop via callbacks.
|
|
65
|
+
*
|
|
66
|
+
* Uses polling until Optimystic supports reactive subscriptions.
|
|
67
|
+
*/
|
|
68
|
+
export class StrandWatcher {
|
|
69
|
+
private readonly filter: StrandFilter;
|
|
70
|
+
private readonly pollInterval: number;
|
|
71
|
+
private readonly callbacks: StrandWatcherCallbacks;
|
|
72
|
+
private readonly queryable: StrandQueryable;
|
|
73
|
+
private readonly sAppIdLookup?: SAppIdLookup;
|
|
74
|
+
/**
|
|
75
|
+
* Wall-clock source for the retry backoff. Read fresh at each use rather than
|
|
76
|
+
* snapshotted per poll: a poll awaits `onStrandAdded`, and a launch that fails
|
|
77
|
+
* slowly (e.g. a dial timeout) would otherwise be scheduled off the clock as it
|
|
78
|
+
* read *before* the attempt, putting `nextAttemptAt` in the past. Injectable so
|
|
79
|
+
* tests can advance time deterministically without fake timers.
|
|
80
|
+
*/
|
|
81
|
+
private readonly now: () => number;
|
|
82
|
+
|
|
83
|
+
private knownStrands: Map<string, StrandRow> = new Map();
|
|
84
|
+
/** Ids admitted under a `defer` decision; re-evaluated each poll until they resolve. */
|
|
85
|
+
private provisional: Set<string> = new Set();
|
|
86
|
+
/** Ids whose last launch attempt threw, with the backoff gating their retry. */
|
|
87
|
+
private failureStates: Map<string, StrandFailureState> = new Map();
|
|
88
|
+
private pollTimer: ReturnType<typeof setInterval> | null = null;
|
|
89
|
+
private initialPollTimer: ReturnType<typeof setTimeout> | null = null;
|
|
90
|
+
private running = false;
|
|
91
|
+
|
|
92
|
+
constructor(
|
|
93
|
+
queryable: StrandQueryable,
|
|
94
|
+
callbacks: StrandWatcherCallbacks,
|
|
95
|
+
filter: StrandFilter = { mode: 'all' },
|
|
96
|
+
pollInterval: number = 5000,
|
|
97
|
+
sAppIdLookup?: SAppIdLookup,
|
|
98
|
+
now: () => number = Date.now
|
|
99
|
+
) {
|
|
100
|
+
this.queryable = queryable;
|
|
101
|
+
this.callbacks = callbacks;
|
|
102
|
+
this.filter = filter;
|
|
103
|
+
this.pollInterval = pollInterval;
|
|
104
|
+
this.sAppIdLookup = sAppIdLookup;
|
|
105
|
+
this.now = now;
|
|
106
|
+
log('StrandWatcher created with filter: %o, interval: %dms', filter, pollInterval);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Evaluate a strand against the current filter, distinguishing a not-yet-known
|
|
111
|
+
* sAppId (`defer`) from a known non-match (`reject`). A `defer` admission is
|
|
112
|
+
* provisional and re-checked on subsequent polls.
|
|
113
|
+
*/
|
|
114
|
+
private evaluateFilter(strand: StrandRow): FilterDecision {
|
|
115
|
+
switch (this.filter.mode) {
|
|
116
|
+
case 'all':
|
|
117
|
+
return 'pass';
|
|
118
|
+
case 'none':
|
|
119
|
+
return 'reject';
|
|
120
|
+
case 'strandId':
|
|
121
|
+
return strand.Id === this.filter.strandId ? 'pass' : 'reject';
|
|
122
|
+
case 'sAppId': {
|
|
123
|
+
if (!this.sAppIdLookup) {
|
|
124
|
+
// No way to ever decide - admit permanently (no lookup configured).
|
|
125
|
+
return 'pass';
|
|
126
|
+
}
|
|
127
|
+
const sAppId = this.sAppIdLookup.getSAppId(strand.Id);
|
|
128
|
+
if (sAppId === undefined) {
|
|
129
|
+
// sAppId not yet known - admit provisionally and re-evaluate next poll.
|
|
130
|
+
log('sAppId unknown for strand %s - deferring filter decision', strand.Id);
|
|
131
|
+
return 'defer';
|
|
132
|
+
}
|
|
133
|
+
const matches = sAppId === this.filter.sAppId;
|
|
134
|
+
log('sAppId filter: strand %s has sAppId %s, filter wants %s, match=%s',
|
|
135
|
+
strand.Id, sAppId, this.filter.sAppId, matches);
|
|
136
|
+
return matches ? 'pass' : 'reject';
|
|
137
|
+
}
|
|
138
|
+
default:
|
|
139
|
+
return 'pass';
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Record a failed launch attempt and schedule when the strand may be retried:
|
|
145
|
+
* `pollInterval * 2^(failures-1)`, capped at {@link MAX_RETRY_BACKOFF_MS}.
|
|
146
|
+
*/
|
|
147
|
+
private recordFailure(strandId: string): void {
|
|
148
|
+
const failures = (this.failureStates.get(strandId)?.failures ?? 0) + 1;
|
|
149
|
+
const delay = Math.min(this.pollInterval * 2 ** (failures - 1), MAX_RETRY_BACKOFF_MS);
|
|
150
|
+
this.failureStates.set(strandId, { failures, nextAttemptAt: this.now() + delay });
|
|
151
|
+
log('Strand %s launch failed (%d consecutive) - next attempt in %dms', strandId, failures, delay);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* Poll for strand changes.
|
|
156
|
+
*/
|
|
157
|
+
private async poll(): Promise<void> {
|
|
158
|
+
if (!this.running) return;
|
|
159
|
+
|
|
160
|
+
try {
|
|
161
|
+
const currentStrands = await this.queryable.queryStrands();
|
|
162
|
+
const currentMap = new Map(currentStrands.map(s => [s.Id, s]));
|
|
163
|
+
|
|
164
|
+
// Find added strands
|
|
165
|
+
for (const strand of currentStrands) {
|
|
166
|
+
if (this.knownStrands.has(strand.Id)) continue;
|
|
167
|
+
const failure = this.failureStates.get(strand.Id);
|
|
168
|
+
// Clock read per candidate, not once per poll: an earlier strand's launch
|
|
169
|
+
// may have taken a long time, and this one's gate must reflect that.
|
|
170
|
+
if (failure && this.now() < failure.nextAttemptAt) continue; // backing off after a failed launch
|
|
171
|
+
const decision = this.evaluateFilter(strand);
|
|
172
|
+
if (decision === 'reject') continue;
|
|
173
|
+
log('Strand added: %s', strand.Id);
|
|
174
|
+
// Set BEFORE the await: this doubles as the in-flight guard that stops the
|
|
175
|
+
// interval timer from starting a second concurrent launch for the same strand.
|
|
176
|
+
this.knownStrands.set(strand.Id, strand);
|
|
177
|
+
if (decision === 'defer') {
|
|
178
|
+
// Provisional admission: sAppId unknown, re-check on later polls.
|
|
179
|
+
this.provisional.add(strand.Id);
|
|
180
|
+
}
|
|
181
|
+
try {
|
|
182
|
+
await this.callbacks.onStrandAdded(strand);
|
|
183
|
+
this.failureStates.delete(strand.Id);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
log('Error handling strand add for %s: %o', strand.Id, error);
|
|
186
|
+
// A failed launch leaves nothing running (StrandInstanceManager drops the
|
|
187
|
+
// record), so forget the strand and let a later poll retry it — gated by
|
|
188
|
+
// the backoff recorded here. One case does leave something running: a row
|
|
189
|
+
// this machine published lands on an instance something else already
|
|
190
|
+
// attached, and honouring the founder request on it (CadreNode.launchStrand
|
|
191
|
+
// → StrandInstanceManager.foundExistingStrand) throws. The instance stays up
|
|
192
|
+
// as a joiner and the retry re-attempts the bootstrap on it, which is what
|
|
193
|
+
// should happen — but do not read the line above as "nothing is running".
|
|
194
|
+
this.knownStrands.delete(strand.Id);
|
|
195
|
+
this.provisional.delete(strand.Id);
|
|
196
|
+
this.recordFailure(strand.Id);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
// Re-evaluate provisional admissions whose sAppId may now be resolvable.
|
|
201
|
+
// Snapshot ids so we can mutate provisional/knownStrands inside the loop.
|
|
202
|
+
for (const strandId of [...this.provisional]) {
|
|
203
|
+
const strand = currentMap.get(strandId);
|
|
204
|
+
if (!strand) continue; // handled by the removed-strand loop below
|
|
205
|
+
const decision = this.evaluateFilter(strand);
|
|
206
|
+
if (decision === 'pass') {
|
|
207
|
+
// Resolved to a match - admission is now final.
|
|
208
|
+
this.provisional.delete(strandId);
|
|
209
|
+
} else if (decision === 'reject') {
|
|
210
|
+
// Resolved to a non-match - stop the provisionally-admitted strand.
|
|
211
|
+
log('Provisional strand rejected on re-evaluation: %s', strandId);
|
|
212
|
+
this.knownStrands.delete(strandId);
|
|
213
|
+
this.provisional.delete(strandId);
|
|
214
|
+
try {
|
|
215
|
+
await this.callbacks.onStrandRemoved(strandId);
|
|
216
|
+
} catch (error) {
|
|
217
|
+
log('Error handling strand remove for %s: %o', strandId, error);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
// decision === 'defer': still unknown, leave provisional for next poll.
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Find removed strands. A strand whose launch failed is no longer in
|
|
224
|
+
// knownStrands, so it correctly never fires onStrandRemoved — nothing was
|
|
225
|
+
// ever started for it.
|
|
226
|
+
for (const [strandId] of this.knownStrands) {
|
|
227
|
+
if (!currentMap.has(strandId)) {
|
|
228
|
+
log('Strand removed: %s', strandId);
|
|
229
|
+
this.knownStrands.delete(strandId);
|
|
230
|
+
this.provisional.delete(strandId);
|
|
231
|
+
try {
|
|
232
|
+
await this.callbacks.onStrandRemoved(strandId);
|
|
233
|
+
} catch (error) {
|
|
234
|
+
log('Error handling strand remove for %s: %o', strandId, error);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Drop backoff state for strands whose control-network row is gone; a row
|
|
240
|
+
// that reappears is a fresh strand and gets a fresh first attempt.
|
|
241
|
+
for (const strandId of [...this.failureStates.keys()]) {
|
|
242
|
+
if (!currentMap.has(strandId)) {
|
|
243
|
+
this.failureStates.delete(strandId);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
} catch (error) {
|
|
247
|
+
log('Error polling strands: %o', error);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Start watching for strand changes
|
|
253
|
+
*/
|
|
254
|
+
async start(): Promise<void> {
|
|
255
|
+
if (this.running) {
|
|
256
|
+
log('StrandWatcher already running');
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
log('Starting StrandWatcher');
|
|
261
|
+
this.running = true;
|
|
262
|
+
|
|
263
|
+
// Set up periodic polling
|
|
264
|
+
this.pollTimer = setInterval(() => {
|
|
265
|
+
void this.poll();
|
|
266
|
+
}, this.pollInterval);
|
|
267
|
+
|
|
268
|
+
// Defer the first poll so start() doesn't block the caller.
|
|
269
|
+
// Strands added via addStrand() typically arrive after start() completes.
|
|
270
|
+
this.initialPollTimer = setTimeout(() => {
|
|
271
|
+
this.initialPollTimer = null;
|
|
272
|
+
void this.poll();
|
|
273
|
+
}, 100);
|
|
274
|
+
|
|
275
|
+
log('StrandWatcher started');
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Stop watching for strand changes
|
|
280
|
+
*/
|
|
281
|
+
async stop(): Promise<void> {
|
|
282
|
+
if (!this.running) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
log('Stopping StrandWatcher');
|
|
287
|
+
this.running = false;
|
|
288
|
+
|
|
289
|
+
if (this.initialPollTimer) {
|
|
290
|
+
clearTimeout(this.initialPollTimer);
|
|
291
|
+
this.initialPollTimer = null;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
if (this.pollTimer) {
|
|
295
|
+
clearInterval(this.pollTimer);
|
|
296
|
+
this.pollTimer = null;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
this.knownStrands.clear();
|
|
300
|
+
this.provisional.clear();
|
|
301
|
+
this.failureStates.clear();
|
|
302
|
+
log('StrandWatcher stopped');
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* Get currently known strands
|
|
307
|
+
*/
|
|
308
|
+
getKnownStrands(): Map<string, StrandRow> {
|
|
309
|
+
return new Map(this.knownStrands);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Force an immediate poll (useful for testing)
|
|
314
|
+
*/
|
|
315
|
+
async forcePoll(): Promise<void> {
|
|
316
|
+
await this.poll();
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
|