@serfab/cadre-host 1.14.0 → 1.15.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 (107) hide show
  1. package/README.md +19 -4
  2. package/dist/bin/host.js +32 -20
  3. package/dist/bin/host.js.map +1 -1
  4. package/dist/bin/nat-output.d.ts +5 -0
  5. package/dist/bin/nat-output.js +12 -1
  6. package/dist/bin/nat-output.js.map +1 -1
  7. package/dist/bin/ui-access.d.ts +22 -0
  8. package/dist/bin/ui-access.js +44 -0
  9. package/dist/bin/ui-access.js.map +1 -0
  10. package/dist/hosted/claim-details.d.ts +25 -3
  11. package/dist/hosted/claim-details.js +31 -37
  12. package/dist/hosted/claim-details.js.map +1 -1
  13. package/dist/hosted/hosted-node-service.d.ts +7 -0
  14. package/dist/hosted/hosted-node-service.js +16 -4
  15. package/dist/hosted/hosted-node-service.js.map +1 -1
  16. package/dist/hosted/hosted-node-watcher.d.ts +16 -4
  17. package/dist/hosted/hosted-node-watcher.js +45 -9
  18. package/dist/hosted/hosted-node-watcher.js.map +1 -1
  19. package/dist/hosted/node-status.d.ts +19 -3
  20. package/dist/hosted/node-status.js +21 -5
  21. package/dist/hosted/node-status.js.map +1 -1
  22. package/dist/hosted/types.d.ts +12 -5
  23. package/dist/hosted/types.js.map +1 -1
  24. package/dist/installer/config.d.ts +23 -0
  25. package/dist/installer/config.js +46 -0
  26. package/dist/installer/config.js.map +1 -1
  27. package/dist/installer/index.d.ts +2 -0
  28. package/dist/installer/index.js +7 -16
  29. package/dist/installer/index.js.map +1 -1
  30. package/dist/installer/wizard.d.ts +12 -2
  31. package/dist/installer/wizard.js +15 -7
  32. package/dist/installer/wizard.js.map +1 -1
  33. package/dist/nat/index.d.ts +2 -1
  34. package/dist/nat/index.js +1 -0
  35. package/dist/nat/index.js.map +1 -1
  36. package/dist/nat/nat-service.d.ts +22 -5
  37. package/dist/nat/nat-service.js +75 -26
  38. package/dist/nat/nat-service.js.map +1 -1
  39. package/dist/nat/nat-store.js +11 -0
  40. package/dist/nat/nat-store.js.map +1 -1
  41. package/dist/nat/network-mode.d.ts +19 -0
  42. package/dist/nat/network-mode.js +32 -0
  43. package/dist/nat/network-mode.js.map +1 -0
  44. package/dist/nat/reachability.d.ts +5 -4
  45. package/dist/nat/reachability.js +19 -6
  46. package/dist/nat/reachability.js.map +1 -1
  47. package/dist/nat/types.d.ts +26 -6
  48. package/dist/nat/types.js.map +1 -1
  49. package/dist/orchestrator/host-process-orchestrator.d.ts +3 -3
  50. package/dist/orchestrator/host-process-orchestrator.js +6 -15
  51. package/dist/orchestrator/host-process-orchestrator.js.map +1 -1
  52. package/dist/package-version.d.ts +1 -0
  53. package/dist/package-version.js +4 -0
  54. package/dist/package-version.js.map +1 -0
  55. package/dist/server/routes/settings.d.ts +1 -0
  56. package/dist/server/routes/settings.js +15 -1
  57. package/dist/server/routes/settings.js.map +1 -1
  58. package/dist/server/routes/status.js +2 -32
  59. package/dist/server/routes/status.js.map +1 -1
  60. package/dist/ui/assets/index-BHd8CWZA.css +1 -0
  61. package/dist/ui/assets/index-Ccr2GjPT.js +23 -0
  62. package/dist/ui/assets/{index-4FWuU8ye.js.map → index-Ccr2GjPT.js.map} +1 -1
  63. package/dist/ui/index.html +2 -2
  64. package/package.json +4 -5
  65. package/src/__tests__/cli.smoke.test.ts +1 -1
  66. package/src/__tests__/orchestrator-claim-secret.test.ts +1 -2
  67. package/src/__tests__/orchestrator-env-scrub.test.ts +160 -163
  68. package/src/__tests__/orchestrator-handle-contract.ts +1 -1
  69. package/src/__tests__/orchestrator-node-identity.test.ts +0 -5
  70. package/src/__tests__/orchestrator-push.test.ts +3 -3
  71. package/src/__tests__/orchestrator.test.ts +3 -5
  72. package/src/__tests__/ui-access.test.ts +30 -0
  73. package/src/bin/host.ts +30 -20
  74. package/src/bin/nat-output.ts +12 -1
  75. package/src/bin/ui-access.ts +50 -0
  76. package/src/hosted/__tests__/fake-orchestrator.test.ts +0 -1
  77. package/src/hosted/__tests__/fake-orchestrator.ts +263 -265
  78. package/src/hosted/__tests__/hosted-node-service.test.ts +708 -609
  79. package/src/hosted/__tests__/hosted-node-supervisor.test.ts +475 -474
  80. package/src/hosted/claim-details.ts +44 -37
  81. package/src/hosted/hosted-node-service.ts +23 -4
  82. package/src/hosted/hosted-node-watcher.ts +288 -250
  83. package/src/hosted/node-status.ts +31 -5
  84. package/src/hosted/types.ts +160 -153
  85. package/src/installer/__tests__/installer.smoke.test.ts +184 -182
  86. package/src/installer/__tests__/wizard.test.ts +76 -56
  87. package/src/installer/config.ts +66 -0
  88. package/src/installer/index.ts +252 -259
  89. package/src/installer/wizard.ts +101 -87
  90. package/src/nat/__tests__/nat-service.test.ts +608 -540
  91. package/src/nat/__tests__/nat-store.test.ts +130 -127
  92. package/src/nat/__tests__/network-mode.test.ts +38 -0
  93. package/src/nat/index.ts +91 -87
  94. package/src/nat/nat-service.ts +79 -26
  95. package/src/nat/nat-store.ts +249 -237
  96. package/src/nat/network-mode.ts +36 -0
  97. package/src/nat/reachability.ts +155 -143
  98. package/src/nat/types.ts +232 -209
  99. package/src/orchestrator/host-process-orchestrator.ts +1089 -1098
  100. package/src/package-version.ts +4 -0
  101. package/src/server/__tests__/fakes.ts +41 -40
  102. package/src/server/__tests__/hosted-nodes-route.test.ts +161 -160
  103. package/src/server/__tests__/settings-route.test.ts +31 -0
  104. package/src/server/routes/settings.ts +128 -115
  105. package/src/server/routes/status.ts +72 -102
  106. package/dist/ui/assets/index-4FWuU8ye.js +0 -19
  107. package/dist/ui/assets/index-Di9Q4d01.css +0 -1
@@ -1,265 +1,263 @@
1
- /**
2
- * Shared fake orchestrator for the hosted-node unit tests — no real child
3
- * processes. Records every create / stop / remove, hands out deterministic
4
- * unique spawn results, and tracks per-`dockerId` liveness so a test can crash
5
- * one node and let the supervisor observe exactly that one as down.
6
- *
7
- * This file holds no tests of its own. The handle rules it shares with
8
- * `HostProcessOrchestrator` live in `src/__tests__/orchestrator-handle-contract.ts`
9
- * and run against both classes, so the two cannot come to disagree unnoticed.
10
- * The sibling `fake-orchestrator.test.ts` runs that contract and pins what only
11
- * the fake has (its recording arrays and hooks). Both exist so a future agent
12
- * cannot make a failing hosted-node test go green by relaxing the fake.
13
- */
14
-
15
- import type {
16
- Orchestrator,
17
- OrchestratorCreateResult,
18
- OrchestratorStats,
19
- } from '@serfab/cadre-provider';
20
-
21
- import type { HostedSpawnRequest, ManagedNodeInfo, NodePorts, NodeStateListener } from '../../orchestrator/types.js';
22
-
23
- function sleep(ms: number): Promise<void> {
24
- return new Promise((res) => setTimeout(res, ms));
25
- }
26
-
27
- /** What the fake remembers about one spawned child. */
28
- interface FakeChild {
29
- containerId: string;
30
- partyId: string;
31
- profile: 'storage' | 'transaction';
32
- running: boolean;
33
- /** The ports a re-spawn comes back on (the real `reusedNodePorts`). */
34
- ports: NodePorts;
35
- }
36
-
37
- /**
38
- * Mirrors the parts of `HostProcessOrchestrator`'s handle lifecycle the hosted-node
39
- * code depends on:
40
- *
41
- * - a **successful** `createContainer` drops every prior handle for the same
42
- * `containerId` (the real `dropStaleHandle`), so an old `dockerId` stops
43
- * resolving the moment its container re-spawns — and the new child comes back
44
- * on the dropped handle's ports (the real `reusedNodePorts`);
45
- * - a **failed** `createContainer` leaves those handles exactly as it found them
46
- * (the real `restoreDroppedHandles`);
47
- * - `createContainer` refuses a container whose previous child is still running
48
- * (the real `refuseRespawnOverLiveChild`), so a test re-spawns only after a
49
- * `stopContainer` or a {@link FakeOrchestrator.crash};
50
- * - `stopContainer` / `removeContainer` throw `Container not found: <dockerId>`
51
- * for a handle this orchestrator no longer knows (the real `requireHandle`);
52
- * - `reclaimWorkdir` refuses a containerId that still resolves to a live
53
- * handle (the real guard — that directory belongs to `removeContainer`).
54
- *
55
- * Consequently `stopped` / `removed` mean **"handles this orchestrator actually
56
- * acted on"**, not "calls attempted" — a stop aimed at a dropped handle is
57
- * rejected and never appears. `onStop` still fires for those rejected calls, so
58
- * a test can observe the attempt separately from its outcome.
59
- */
60
- export class FakeOrchestrator implements Orchestrator {
61
- createCalls: HostedSpawnRequest[] = [];
62
- stopped: string[] = [];
63
- removed: string[] = [];
64
- /**
65
- * Container ids whose workdir this orchestrator actually removed. Same
66
- * "acted on, not attempted" rule as `stopped` / `removed`: a refused reclaim
67
- * leaves no entry.
68
- */
69
- reclaimedWorkdirs: string[] = [];
70
- failCreate = false;
71
- createDelayMs = 0;
72
- /**
73
- * Observation hook — lets a test inspect the world as the stop happens. Fires
74
- * even when the handle is unknown and the stop is then rejected.
75
- */
76
- onStop?: (dockerId: string) => void;
77
- /**
78
- * Observation hook — fires before the spawn resolves or fails, modelling the
79
- * real class's *pre-drop* `await` window (`ensureNodeIdentity`, `resolvePush`).
80
- * Work driven from here sees the previous handle still alive. Contrast
81
- * {@link onSpawned}.
82
- */
83
- onCreate?: (request: HostedSpawnRequest) => void;
84
- /**
85
- * Observation hook — fires once the drop has landed and the new child is
86
- * registered, immediately before `createContainer` resolves. Models the
87
- * microtask gap between the real `createContainer` returning and its caller's
88
- * `await` resuming: the real drop → launch → return runs synchronously, so
89
- * this is the earliest point other code can observe the post-drop world.
90
- * Contrast {@link onCreate}.
91
- */
92
- onSpawned?: (dockerId: string) => void;
93
- /** Observation hook — fires before the liveness answer is computed. */
94
- onIsRunning?: (dockerId: string) => void;
95
-
96
- private readonly children = new Map<string, FakeChild>();
97
- private readonly listeners = new Set<NodeStateListener>();
98
- private counter = 0;
99
-
100
- async createContainer(request: HostedSpawnRequest): Promise<OrchestratorCreateResult> {
101
- this.createCalls.push(request);
102
- this.onCreate?.(request);
103
- if (this.createDelayMs) await sleep(this.createDelayMs);
104
- if (this.failCreate) throw new Error('spawn boom');
105
- // After the delay, as in the real class, where the check is the last `await`
106
- // before the drop: a stop that lands during the spawn window is seen here.
107
- this.refuseRespawnOverLiveChild(request.containerId);
108
- // Drop the prior handles for this container, mirroring `dropStaleHandle`.
109
- // Success-only, and placed here on purpose: the real drop happens after the
110
- // spawn's every `await` and cannot fail afterwards, and a create that throws
111
- // puts the handles back (`restoreDroppedHandles`) — so a failed create must
112
- // leave them untouched. `HostedNodeSupervisor`'s give-up test depends on that.
113
- let ports: NodePorts | undefined;
114
- for (const [id, child] of this.children) {
115
- if (child.containerId === request.containerId) {
116
- ports = child.ports;
117
- this.children.delete(id);
118
- }
119
- }
120
- const n = ++this.counter;
121
- const dockerId = `dock_${n}`;
122
- ports ??= { health: 9000 + n, metrics: 9100 + n, p2p: 4000 + n, ws: 4100 + n };
123
- this.children.set(dockerId, {
124
- containerId: request.containerId,
125
- partyId: request.partyId,
126
- profile: request.profile,
127
- running: true,
128
- ports,
129
- });
130
- this.onSpawned?.(dockerId);
131
- return {
132
- dockerId,
133
- healthEndpoint: `http://127.0.0.1:${ports.health}/health`,
134
- metricsEndpoint: `http://127.0.0.1:${ports.metrics}/metrics`,
135
- seedEndpoint: `http://127.0.0.1:${ports.health}/seed`,
136
- seedToken: `seed-token-${n}`,
137
- p2pPort: ports.p2p,
138
- };
139
- }
140
-
141
- async stopContainer(dockerId: string): Promise<void> {
142
- // Before the check: a test may want to observe that a stop was *attempted*
143
- // even when this orchestrator rejects it.
144
- this.onStop?.(dockerId);
145
- const child = this.requireChild(dockerId);
146
- this.stopped.push(dockerId);
147
- child.running = false;
148
- }
149
-
150
- async removeContainer(dockerId: string): Promise<void> {
151
- // NOTE: the real `removeContainer` stops a still-live child first; this one
152
- // only deletes, so a remove aimed at a running child records no `stopped`
153
- // entry and emits no state change. Every hosted-node caller stops before it
154
- // reclaims, so nothing sees the difference today — if one stops doing that,
155
- // stop the child here too.
156
- this.requireChild(dockerId);
157
- this.removed.push(dockerId);
158
- this.children.delete(dockerId);
159
- }
160
-
161
- /**
162
- * NOTE: this and {@link getLogs} take an unknown `dockerId` where the real
163
- * class throws (`requireHandle`). No hosted-node path calls either, so the gap
164
- * is unobservable — route them through `requireChild` if one starts to.
165
- */
166
- async getStats(): Promise<OrchestratorStats> {
167
- return { cpuPercent: 0, memoryBytes: 0, networkRxBytes: 0, networkTxBytes: 0 };
168
- }
169
-
170
- /** Unknown handles read as not running, matching the real orchestrator. */
171
- async isRunning(dockerId: string): Promise<boolean> {
172
- this.onIsRunning?.(dockerId);
173
- return this.children.get(dockerId)?.running ?? false;
174
- }
175
-
176
- /** Resolve a spawn's friendly containerId back to its dockerId. */
177
- resolveDockerId(containerId: string): string | undefined {
178
- for (const [dockerId, child] of this.children) {
179
- if (child.containerId === containerId) return dockerId;
180
- }
181
- return undefined;
182
- }
183
-
184
- /** The node by containerId or dockerId, as the real `getNode`. */
185
- getNode(idOrDockerId: string): ManagedNodeInfo | undefined {
186
- const direct = this.children.get(idOrDockerId);
187
- if (direct) return toNodeInfo(idOrDockerId, direct);
188
- for (const [dockerId, child] of this.children) {
189
- if (child.containerId === idOrDockerId) return toNodeInfo(dockerId, child);
190
- }
191
- return undefined;
192
- }
193
-
194
- listNodes(): ManagedNodeInfo[] {
195
- return [...this.children].map(([dockerId, child]) => toNodeInfo(dockerId, child));
196
- }
197
-
198
- /**
199
- * Remove the workdir of a container no handle owns, mirroring the real
200
- * `reclaimWorkdir`. Refuses (returns `false`, records nothing) while a child
201
- * for that containerId still resolves — that directory belongs to
202
- * `removeContainer`, which stops the child first.
203
- *
204
- * There is no directory here to check for existence, so a first reclaim of an
205
- * unresolvable id always reports `true`; the real class returns `false` when
206
- * the path is already gone. Nothing in the hosted-node layer branches on the
207
- * return value, so the gap is unobservable — model it if one starts to.
208
- */
209
- reclaimWorkdir(containerId: string): boolean {
210
- if (this.resolveDockerId(containerId)) return false;
211
- this.reclaimedWorkdirs.push(containerId);
212
- return true;
213
- }
214
-
215
- async getLogs(): Promise<string> {
216
- return '';
217
- }
218
-
219
- onStateChange(listener: NodeStateListener): () => void {
220
- this.listeners.add(listener);
221
- return () => { this.listeners.delete(listener); };
222
- }
223
-
224
- /** Kill a child out from under the host — the crash the supervisor exists for. */
225
- crash(dockerId: string): void {
226
- const child = this.children.get(dockerId);
227
- if (!child?.running) return;
228
- child.running = false;
229
- this.emit(toNodeInfo(dockerId, child));
230
- }
231
-
232
- private emit(info: ManagedNodeInfo): void {
233
- for (const listener of this.listeners) listener(info);
234
- }
235
-
236
- /** Mirrors the real method of the same name for a child this process spawned. */
237
- private refuseRespawnOverLiveChild(containerId: string): void {
238
- for (const child of this.children.values()) {
239
- if (child.containerId === containerId && child.running) {
240
- throw new Error(`container ${containerId} is still running`);
241
- }
242
- }
243
- }
244
-
245
- /** Mirrors `HostProcessOrchestrator.requireHandle` — unknown handles throw. */
246
- private requireChild(dockerId: string): FakeChild {
247
- const child = this.children.get(dockerId);
248
- if (!child) throw new Error(`Container not found: ${dockerId}`);
249
- return child;
250
- }
251
- }
252
-
253
- function toNodeInfo(dockerId: string, child: FakeChild): ManagedNodeInfo {
254
- return {
255
- id: child.containerId,
256
- dockerId,
257
- partyId: child.partyId,
258
- profile: child.profile,
259
- status: child.running ? 'running' : 'stopped',
260
- spawnedAt: new Date(0).toISOString(),
261
- workdir: `/fake/${child.containerId}`,
262
- ports: { ...child.ports },
263
- announcedAddrs: [],
264
- };
265
- }
1
+ /**
2
+ * Shared fake orchestrator for the hosted-node unit tests — no real child
3
+ * processes. Records every create / stop / remove, hands out deterministic
4
+ * unique spawn results, and tracks per-`dockerId` liveness so a test can crash
5
+ * one node and let the supervisor observe exactly that one as down.
6
+ *
7
+ * This file holds no tests of its own. The handle rules it shares with
8
+ * `HostProcessOrchestrator` live in `src/__tests__/orchestrator-handle-contract.ts`
9
+ * and run against both classes, so the two cannot come to disagree unnoticed.
10
+ * The sibling `fake-orchestrator.test.ts` runs that contract and pins what only
11
+ * the fake has (its recording arrays and hooks). Both exist so a future agent
12
+ * cannot make a failing hosted-node test go green by relaxing the fake.
13
+ */
14
+
15
+ import type {
16
+ Orchestrator,
17
+ OrchestratorCreateResult,
18
+ OrchestratorStats,
19
+ } from '@serfab/cadre-provider';
20
+
21
+ import type { HostedSpawnRequest, ManagedNodeInfo, NodePorts, NodeStateListener } from '../../orchestrator/types.js';
22
+
23
+ function sleep(ms: number): Promise<void> {
24
+ return new Promise((res) => setTimeout(res, ms));
25
+ }
26
+
27
+ /** What the fake remembers about one spawned child. */
28
+ interface FakeChild {
29
+ containerId: string;
30
+ partyId: string;
31
+ profile: 'storage' | 'transaction';
32
+ running: boolean;
33
+ /** The ports a re-spawn comes back on (the real `reusedNodePorts`). */
34
+ ports: NodePorts;
35
+ }
36
+
37
+ /**
38
+ * Mirrors the parts of `HostProcessOrchestrator`'s handle lifecycle the hosted-node
39
+ * code depends on:
40
+ *
41
+ * - a **successful** `createContainer` drops every prior handle for the same
42
+ * `containerId` (the real `dropStaleHandle`), so an old `dockerId` stops
43
+ * resolving the moment its container re-spawns — and the new child comes back
44
+ * on the dropped handle's ports (the real `reusedNodePorts`);
45
+ * - a **failed** `createContainer` leaves those handles exactly as it found them
46
+ * (the real `restoreDroppedHandles`);
47
+ * - `createContainer` refuses a container whose previous child is still running
48
+ * (the real `refuseRespawnOverLiveChild`), so a test re-spawns only after a
49
+ * `stopContainer` or a {@link FakeOrchestrator.crash};
50
+ * - `stopContainer` / `removeContainer` throw `Container not found: <dockerId>`
51
+ * for a handle this orchestrator no longer knows (the real `requireHandle`);
52
+ * - `reclaimWorkdir` refuses a containerId that still resolves to a live
53
+ * handle (the real guard — that directory belongs to `removeContainer`).
54
+ *
55
+ * Consequently `stopped` / `removed` mean **"handles this orchestrator actually
56
+ * acted on"**, not "calls attempted" — a stop aimed at a dropped handle is
57
+ * rejected and never appears. `onStop` still fires for those rejected calls, so
58
+ * a test can observe the attempt separately from its outcome.
59
+ */
60
+ export class FakeOrchestrator implements Orchestrator {
61
+ createCalls: HostedSpawnRequest[] = [];
62
+ stopped: string[] = [];
63
+ removed: string[] = [];
64
+ /**
65
+ * Container ids whose workdir this orchestrator actually removed. Same
66
+ * "acted on, not attempted" rule as `stopped` / `removed`: a refused reclaim
67
+ * leaves no entry.
68
+ */
69
+ reclaimedWorkdirs: string[] = [];
70
+ failCreate = false;
71
+ createDelayMs = 0;
72
+ /**
73
+ * Observation hook — lets a test inspect the world as the stop happens. Fires
74
+ * even when the handle is unknown and the stop is then rejected.
75
+ */
76
+ onStop?: (dockerId: string) => void;
77
+ /**
78
+ * Observation hook — fires before the spawn resolves or fails, modelling the
79
+ * real class's *pre-drop* `await` window (`ensureNodeIdentity`, `resolvePush`).
80
+ * Work driven from here sees the previous handle still alive. Contrast
81
+ * {@link onSpawned}.
82
+ */
83
+ onCreate?: (request: HostedSpawnRequest) => void;
84
+ /**
85
+ * Observation hook — fires once the drop has landed and the new child is
86
+ * registered, immediately before `createContainer` resolves. Models the
87
+ * microtask gap between the real `createContainer` returning and its caller's
88
+ * `await` resuming: the real drop → launch → return runs synchronously, so
89
+ * this is the earliest point other code can observe the post-drop world.
90
+ * Contrast {@link onCreate}.
91
+ */
92
+ onSpawned?: (dockerId: string) => void;
93
+ /** Observation hook — fires before the liveness answer is computed. */
94
+ onIsRunning?: (dockerId: string) => void;
95
+
96
+ private readonly children = new Map<string, FakeChild>();
97
+ private readonly listeners = new Set<NodeStateListener>();
98
+ private counter = 0;
99
+
100
+ async createContainer(request: HostedSpawnRequest): Promise<OrchestratorCreateResult> {
101
+ this.createCalls.push(request);
102
+ this.onCreate?.(request);
103
+ if (this.createDelayMs) await sleep(this.createDelayMs);
104
+ if (this.failCreate) throw new Error('spawn boom');
105
+ // After the delay, as in the real class, where the check is the last `await`
106
+ // before the drop: a stop that lands during the spawn window is seen here.
107
+ this.refuseRespawnOverLiveChild(request.containerId);
108
+ // Drop the prior handles for this container, mirroring `dropStaleHandle`.
109
+ // Success-only, and placed here on purpose: the real drop happens after the
110
+ // spawn's every `await` and cannot fail afterwards, and a create that throws
111
+ // puts the handles back (`restoreDroppedHandles`) — so a failed create must
112
+ // leave them untouched. `HostedNodeSupervisor`'s give-up test depends on that.
113
+ let ports: NodePorts | undefined;
114
+ for (const [id, child] of this.children) {
115
+ if (child.containerId === request.containerId) {
116
+ ports = child.ports;
117
+ this.children.delete(id);
118
+ }
119
+ }
120
+ const n = ++this.counter;
121
+ const dockerId = `dock_${n}`;
122
+ ports ??= { health: 9000 + n, metrics: 9100 + n, p2p: 4000 + n, ws: 4100 + n };
123
+ this.children.set(dockerId, {
124
+ containerId: request.containerId,
125
+ partyId: request.partyId,
126
+ profile: request.profile,
127
+ running: true,
128
+ ports,
129
+ });
130
+ this.onSpawned?.(dockerId);
131
+ return {
132
+ dockerId,
133
+ healthEndpoint: `http://127.0.0.1:${ports.health}/health`,
134
+ metricsEndpoint: `http://127.0.0.1:${ports.metrics}/metrics`,
135
+ p2pPort: ports.p2p,
136
+ };
137
+ }
138
+
139
+ async stopContainer(dockerId: string): Promise<void> {
140
+ // Before the check: a test may want to observe that a stop was *attempted*
141
+ // even when this orchestrator rejects it.
142
+ this.onStop?.(dockerId);
143
+ const child = this.requireChild(dockerId);
144
+ this.stopped.push(dockerId);
145
+ child.running = false;
146
+ }
147
+
148
+ async removeContainer(dockerId: string): Promise<void> {
149
+ // NOTE: the real `removeContainer` stops a still-live child first; this one
150
+ // only deletes, so a remove aimed at a running child records no `stopped`
151
+ // entry and emits no state change. Every hosted-node caller stops before it
152
+ // reclaims, so nothing sees the difference today — if one stops doing that,
153
+ // stop the child here too.
154
+ this.requireChild(dockerId);
155
+ this.removed.push(dockerId);
156
+ this.children.delete(dockerId);
157
+ }
158
+
159
+ /**
160
+ * NOTE: this and {@link getLogs} take an unknown `dockerId` where the real
161
+ * class throws (`requireHandle`). No hosted-node path calls either, so the gap
162
+ * is unobservable — route them through `requireChild` if one starts to.
163
+ */
164
+ async getStats(): Promise<OrchestratorStats> {
165
+ return { cpuPercent: 0, memoryBytes: 0, networkRxBytes: 0, networkTxBytes: 0 };
166
+ }
167
+
168
+ /** Unknown handles read as not running, matching the real orchestrator. */
169
+ async isRunning(dockerId: string): Promise<boolean> {
170
+ this.onIsRunning?.(dockerId);
171
+ return this.children.get(dockerId)?.running ?? false;
172
+ }
173
+
174
+ /** Resolve a spawn's friendly containerId back to its dockerId. */
175
+ resolveDockerId(containerId: string): string | undefined {
176
+ for (const [dockerId, child] of this.children) {
177
+ if (child.containerId === containerId) return dockerId;
178
+ }
179
+ return undefined;
180
+ }
181
+
182
+ /** The node by containerId or dockerId, as the real `getNode`. */
183
+ getNode(idOrDockerId: string): ManagedNodeInfo | undefined {
184
+ const direct = this.children.get(idOrDockerId);
185
+ if (direct) return toNodeInfo(idOrDockerId, direct);
186
+ for (const [dockerId, child] of this.children) {
187
+ if (child.containerId === idOrDockerId) return toNodeInfo(dockerId, child);
188
+ }
189
+ return undefined;
190
+ }
191
+
192
+ listNodes(): ManagedNodeInfo[] {
193
+ return [...this.children].map(([dockerId, child]) => toNodeInfo(dockerId, child));
194
+ }
195
+
196
+ /**
197
+ * Remove the workdir of a container no handle owns, mirroring the real
198
+ * `reclaimWorkdir`. Refuses (returns `false`, records nothing) while a child
199
+ * for that containerId still resolves — that directory belongs to
200
+ * `removeContainer`, which stops the child first.
201
+ *
202
+ * There is no directory here to check for existence, so a first reclaim of an
203
+ * unresolvable id always reports `true`; the real class returns `false` when
204
+ * the path is already gone. Nothing in the hosted-node layer branches on the
205
+ * return value, so the gap is unobservable — model it if one starts to.
206
+ */
207
+ reclaimWorkdir(containerId: string): boolean {
208
+ if (this.resolveDockerId(containerId)) return false;
209
+ this.reclaimedWorkdirs.push(containerId);
210
+ return true;
211
+ }
212
+
213
+ async getLogs(): Promise<string> {
214
+ return '';
215
+ }
216
+
217
+ onStateChange(listener: NodeStateListener): () => void {
218
+ this.listeners.add(listener);
219
+ return () => { this.listeners.delete(listener); };
220
+ }
221
+
222
+ /** Kill a child out from under the host — the crash the supervisor exists for. */
223
+ crash(dockerId: string): void {
224
+ const child = this.children.get(dockerId);
225
+ if (!child?.running) return;
226
+ child.running = false;
227
+ this.emit(toNodeInfo(dockerId, child));
228
+ }
229
+
230
+ private emit(info: ManagedNodeInfo): void {
231
+ for (const listener of this.listeners) listener(info);
232
+ }
233
+
234
+ /** Mirrors the real method of the same name for a child this process spawned. */
235
+ private refuseRespawnOverLiveChild(containerId: string): void {
236
+ for (const child of this.children.values()) {
237
+ if (child.containerId === containerId && child.running) {
238
+ throw new Error(`container ${containerId} is still running`);
239
+ }
240
+ }
241
+ }
242
+
243
+ /** Mirrors `HostProcessOrchestrator.requireHandle` — unknown handles throw. */
244
+ private requireChild(dockerId: string): FakeChild {
245
+ const child = this.children.get(dockerId);
246
+ if (!child) throw new Error(`Container not found: ${dockerId}`);
247
+ return child;
248
+ }
249
+ }
250
+
251
+ function toNodeInfo(dockerId: string, child: FakeChild): ManagedNodeInfo {
252
+ return {
253
+ id: child.containerId,
254
+ dockerId,
255
+ partyId: child.partyId,
256
+ profile: child.profile,
257
+ status: child.running ? 'running' : 'stopped',
258
+ spawnedAt: new Date(0).toISOString(),
259
+ workdir: `/fake/${child.containerId}`,
260
+ ports: { ...child.ports },
261
+ announcedAddrs: [],
262
+ };
263
+ }