@serfab/cadre-host 0.10.0 → 0.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 (138) hide show
  1. package/dist/server/static.js +38 -38
  2. package/dist/ui/assets/index-C-iks41M.js.map +1 -1
  3. package/dist/ui/index.html +12 -12
  4. package/package.json +4 -4
  5. package/service/cadre-host.service.tmpl +16 -16
  6. package/service/com.serfab.cadre-host.plist.tmpl +35 -35
  7. package/service/install-service.ps1 +98 -98
  8. package/service/uninstall-service.ps1 +19 -19
  9. package/src/__tests__/build-targets.test.ts +26 -26
  10. package/src/__tests__/cli-invite.smoke.test.ts +192 -192
  11. package/src/__tests__/cli-nat.smoke.test.ts +212 -212
  12. package/src/__tests__/cli.smoke.test.ts +46 -46
  13. package/src/__tests__/global-setup.ts +51 -51
  14. package/src/__tests__/host-owner.smoke.test.ts +123 -123
  15. package/src/__tests__/orchestrator-env-scrub.test.ts +140 -140
  16. package/src/__tests__/orchestrator-node-identity.test.ts +204 -204
  17. package/src/__tests__/orchestrator-owner.test.ts +198 -198
  18. package/src/__tests__/orchestrator-pin-keys.test.ts +153 -153
  19. package/src/__tests__/orchestrator-push.test.ts +145 -145
  20. package/src/__tests__/orchestrator.test.ts +951 -951
  21. package/src/auth/__tests__/duration.test.ts +28 -28
  22. package/src/auth/__tests__/self-label.test.ts +85 -85
  23. package/src/auth/__tests__/trust-circle-store.test.ts +115 -115
  24. package/src/auth/__tests__/trust-circle.test.ts +445 -445
  25. package/src/auth/duration.ts +33 -33
  26. package/src/auth/index.ts +21 -21
  27. package/src/auth/self-label.ts +74 -74
  28. package/src/auth/trust-circle-store.ts +155 -155
  29. package/src/auth/types.ts +99 -99
  30. package/src/donation/__tests__/donation-service.test.ts +1068 -1068
  31. package/src/donation/__tests__/donation-supervisor.test.ts +510 -510
  32. package/src/donation/__tests__/fake-orchestrator.test.ts +168 -168
  33. package/src/donation/__tests__/fake-orchestrator.ts +225 -225
  34. package/src/donation/__tests__/grant-service.test.ts +213 -213
  35. package/src/donation/__tests__/grant-store.test.ts +113 -113
  36. package/src/donation/donation-service.ts +962 -962
  37. package/src/donation/donation-store.ts +171 -171
  38. package/src/donation/donation-supervisor.ts +388 -388
  39. package/src/donation/grant-service.ts +200 -200
  40. package/src/donation/grant-store.ts +152 -152
  41. package/src/donation/index.ts +64 -64
  42. package/src/donation/types.ts +243 -243
  43. package/src/installer/__tests__/identity.test.ts +34 -34
  44. package/src/installer/__tests__/paths.test.ts +47 -47
  45. package/src/installer/__tests__/service-host.test.ts +62 -62
  46. package/src/installer/__tests__/wizard.test.ts +64 -64
  47. package/src/installer/identity.ts +49 -49
  48. package/src/installer/paths.ts +109 -109
  49. package/src/installer/platform.ts +27 -27
  50. package/src/installer/service-host/index.ts +21 -21
  51. package/src/installer/service-host/template.ts +21 -21
  52. package/src/installer/wizard.ts +103 -103
  53. package/src/nat/__tests__/address-resolver.test.ts +82 -82
  54. package/src/nat/__tests__/ddns-duckdns.test.ts +83 -83
  55. package/src/nat/__tests__/ddns-updater.test.ts +253 -253
  56. package/src/nat/__tests__/external-ip.test.ts +124 -124
  57. package/src/nat/__tests__/nat-service.test.ts +497 -497
  58. package/src/nat/__tests__/nat-store.test.ts +93 -93
  59. package/src/nat/__tests__/port-mapper.test.ts +191 -191
  60. package/src/nat/__tests__/reachability.test.ts +24 -24
  61. package/src/nat/__tests__/secrets.test.ts +178 -178
  62. package/src/nat/address-resolver.ts +51 -51
  63. package/src/nat/ddns/duckdns.ts +76 -76
  64. package/src/nat/ddns/index.ts +55 -55
  65. package/src/nat/ddns/updater.ts +243 -243
  66. package/src/nat/external-ip.ts +154 -154
  67. package/src/nat/index.ts +71 -71
  68. package/src/nat/nat-service.ts +501 -501
  69. package/src/nat/nat-store.ts +154 -154
  70. package/src/nat/port-mapper.ts +329 -329
  71. package/src/nat/reachability.ts +34 -34
  72. package/src/nat/secrets/file-store.ts +130 -130
  73. package/src/nat/secrets/index.ts +112 -112
  74. package/src/nat/secrets/keytar-store.ts +67 -67
  75. package/src/nat/types.ts +159 -159
  76. package/src/orchestrator/__tests__/node-identity.test.ts +62 -62
  77. package/src/orchestrator/host-process-orchestrator.ts +1133 -1133
  78. package/src/orchestrator/index.ts +10 -10
  79. package/src/orchestrator/log-rotator.ts +53 -53
  80. package/src/orchestrator/node-identity.ts +40 -40
  81. package/src/orchestrator/pid-liveness.ts +24 -24
  82. package/src/orchestrator/port-allocator.ts +91 -91
  83. package/src/orchestrator/state-store.ts +90 -90
  84. package/src/orchestrator/types.ts +141 -141
  85. package/src/owner/__tests__/owner-node-client.test.ts +194 -194
  86. package/src/owner/index.ts +12 -12
  87. package/src/owner/owner-node-client.ts +259 -259
  88. package/src/push/__tests__/push.test.ts +90 -90
  89. package/src/push/index.ts +146 -146
  90. package/src/server/__tests__/bus.test.ts +38 -38
  91. package/src/server/__tests__/error-handler.test.ts +118 -118
  92. package/src/server/__tests__/grants-admin-route.test.ts +73 -73
  93. package/src/server/__tests__/grants-route.test.ts +263 -263
  94. package/src/server/__tests__/nodes-route.test.ts +259 -259
  95. package/src/server/__tests__/origin-guard.test.ts +72 -72
  96. package/src/server/__tests__/publishers.test.ts +216 -216
  97. package/src/server/__tests__/server.smoke.test.ts +253 -253
  98. package/src/server/__tests__/settings-route.test.ts +194 -194
  99. package/src/server/__tests__/sse-route.test.ts +119 -119
  100. package/src/server/__tests__/static.test.ts +145 -145
  101. package/src/server/__tests__/status-route.test.ts +134 -134
  102. package/src/server/__tests__/strands-route.test.ts +233 -233
  103. package/src/server/error-handler.ts +178 -178
  104. package/src/server/events/bus.ts +42 -42
  105. package/src/server/events/sse-route.ts +81 -81
  106. package/src/server/events/types.ts +23 -23
  107. package/src/server/index.ts +267 -267
  108. package/src/server/origin-guard.ts +84 -84
  109. package/src/server/routes/grants-admin.ts +41 -41
  110. package/src/server/routes/grants.ts +210 -210
  111. package/src/server/routes/nat.ts +63 -63
  112. package/src/server/routes/nodes.ts +170 -170
  113. package/src/server/routes/settings.ts +123 -123
  114. package/src/server/routes/status.ts +118 -118
  115. package/src/server/routes/strands.ts +54 -54
  116. package/src/server/routes/trust-circle.ts +49 -49
  117. package/src/server/routes/update.ts +43 -43
  118. package/src/server/server.ts +122 -122
  119. package/src/server/settings-store.ts +37 -37
  120. package/src/server/static.ts +207 -207
  121. package/src/strands/__tests__/strand-service.test.ts +165 -165
  122. package/src/strands/index.ts +14 -14
  123. package/src/strands/strand-service.ts +135 -135
  124. package/src/strands/types.ts +102 -102
  125. package/src/update/__tests__/apply.test.ts +158 -158
  126. package/src/update/__tests__/manifest.test.ts +128 -128
  127. package/src/update/__tests__/release-key.test.ts +42 -42
  128. package/src/update/__tests__/sign.test.ts +78 -78
  129. package/src/update/__tests__/store.test.ts +81 -81
  130. package/src/update/__tests__/update-service.integration.test.ts +256 -256
  131. package/src/update/__tests__/version.test.ts +31 -31
  132. package/src/update/apply.ts +190 -190
  133. package/src/update/index.ts +341 -341
  134. package/src/update/release-key.ts +71 -71
  135. package/src/update/sign.ts +74 -74
  136. package/src/update/store.ts +101 -101
  137. package/src/update/types.ts +109 -109
  138. package/src/update/version.ts +80 -80
@@ -1,204 +1,204 @@
1
- /**
2
- * HostProcessOrchestrator per-node identity tests.
3
- *
4
- * A donated node must keep the peer id its host cadre approved across restarts,
5
- * and needs a durable on-disk home for its node-local stores (`cadre-cli start`
6
- * opens the file-backed bootstrap-peer / trusted-owner stores only when a
7
- * protobuf identity key file is configured). The orchestrator therefore writes
8
- * `<workdir>/identity.key` once per container and passes it as
9
- * `--identity-protobuf`.
10
- *
11
- * A fake CLI records the `--identity-protobuf` argument it was launched with, so
12
- * the spawn wiring is observable without a real cadre node.
13
- */
14
-
15
- import { afterEach, beforeEach, describe, expect, it } from 'vitest';
16
- import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
17
- import { tmpdir } from 'node:os';
18
- import { join } from 'node:path';
19
-
20
- import { HostProcessOrchestrator, OWNER_CONTAINER_ID } from '../orchestrator/host-process-orchestrator.js';
21
- import { loadIdentity } from '../installer/identity.js';
22
-
23
- // Writes the --identity-protobuf argument (as seen on its own command line) next
24
- // to the startup token, then behaves like a minimal long-lived node.
25
- const FAKE_CLI = `
26
- import fs from 'node:fs';
27
- import path from 'node:path';
28
- const args = process.argv.slice(2);
29
- const get = (flag) => { const i = args.indexOf(flag); return i >= 0 ? args[i + 1] : undefined; };
30
- const tokenPath = get('--startup-token-file');
31
- const token = process.env.CADRE_STARTUP_TOKEN ?? '';
32
- if (tokenPath) {
33
- const dir = path.dirname(tokenPath);
34
- try { fs.writeFileSync(path.join(dir, 'identity-arg.txt'), get('--identity-protobuf') ?? '', 'utf8'); } catch (e) { console.error(e); }
35
- if (token) { try { fs.writeFileSync(tokenPath, token, 'utf8'); } catch (e) { console.error(e); } }
36
- }
37
- process.on('SIGTERM', () => process.exit(0));
38
- setInterval(() => {}, 1 << 30);
39
- `;
40
-
41
- let tmpRoot: string;
42
- let scriptPath: string;
43
- const orchestrators: HostProcessOrchestrator[] = [];
44
-
45
- beforeEach(() => {
46
- tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-nodeid-'));
47
- scriptPath = join(tmpRoot, 'fake-cli.mjs');
48
- writeFileSync(scriptPath, FAKE_CLI, 'utf8');
49
- });
50
-
51
- afterEach(async () => {
52
- for (const orch of orchestrators) {
53
- for (const n of orch.listNodes()) {
54
- try { await orch.removeContainer(n.dockerId); } catch { /* ignore */ }
55
- }
56
- }
57
- orchestrators.length = 0;
58
- await sleep(50);
59
- try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
60
- });
61
-
62
- function makeOrchestrator(
63
- rootDir: string,
64
- portRange: { start: number; end: number } = { start: 17500, end: 17999 },
65
- ): HostProcessOrchestrator {
66
- mkdirSync(rootDir, { recursive: true });
67
- const orch = new HostProcessOrchestrator({
68
- rootDir,
69
- portRange,
70
- stopTimeoutMs: 1500,
71
- spawn: { entrypoint: scriptPath },
72
- });
73
- orchestrators.push(orch);
74
- return orch;
75
- }
76
-
77
- async function waitForFile(path: string, timeoutMs = 5000): Promise<string> {
78
- const deadline = Date.now() + timeoutMs;
79
- while (Date.now() < deadline) {
80
- if (existsSync(path)) return readFileSync(path, 'utf8');
81
- await sleep(50);
82
- }
83
- throw new Error(`file never appeared: ${path}`);
84
- }
85
-
86
- function sleep(ms: number): Promise<void> {
87
- return new Promise((res) => setTimeout(res, ms));
88
- }
89
-
90
- describe('HostProcessOrchestrator node identity', () => {
91
- it('passes --identity-protobuf pointing at a real key inside the node workdir', async () => {
92
- const rootDir = join(tmpRoot, 'a');
93
- const orch = makeOrchestrator(rootDir);
94
- await orch.init();
95
-
96
- await orch.createContainer({
97
- containerId: 'donated-1',
98
- partyId: 'foreign-P',
99
- bootstrapNodes: [],
100
- profile: 'storage',
101
- pinnedOwnerKeys: ['key-a'],
102
- });
103
-
104
- const workdir = join(rootDir, 'donated-1');
105
- const arg = await waitForFile(join(workdir, 'identity-arg.txt'));
106
- expect(arg).toBe(join(workdir, 'identity.key'));
107
- // Decodes as a libp2p protobuf private key → the child gets a usable identity.
108
- expect(loadIdentity(arg).peerId).toMatch(/^12D3Koo/);
109
- });
110
-
111
- it('reuses the same identity when the same containerId is spawned again', async () => {
112
- const rootDir = join(tmpRoot, 'b');
113
- const orch = makeOrchestrator(rootDir);
114
- await orch.init();
115
-
116
- const first = await orch.createContainer({
117
- containerId: 'donated-2',
118
- partyId: 'foreign-P',
119
- bootstrapNodes: [],
120
- profile: 'storage',
121
- pinnedOwnerKeys: ['key-a'],
122
- });
123
- const workdir = join(rootDir, 'donated-2');
124
- await waitForFile(join(workdir, 'identity-arg.txt'));
125
- const before = loadIdentity(join(workdir, 'identity.key')).peerId;
126
-
127
- // Stop (not remove — removal deletes the workdir) and re-spawn, as a restart
128
- // of a live loan would.
129
- await orch.stopContainer(first.dockerId);
130
- await orch.createContainer({
131
- containerId: 'donated-2',
132
- partyId: 'foreign-P',
133
- bootstrapNodes: [],
134
- profile: 'storage',
135
- pinnedOwnerKeys: ['key-a'],
136
- });
137
-
138
- const after = loadIdentity(join(workdir, 'identity.key')).peerId;
139
- expect(after).toBe(before);
140
- });
141
-
142
- it('destroys the identity with the workdir when the loan is terminated', async () => {
143
- const rootDir = join(tmpRoot, 'c');
144
- const orch = makeOrchestrator(rootDir);
145
- await orch.init();
146
-
147
- const created = await orch.createContainer({
148
- containerId: 'donated-3',
149
- partyId: 'foreign-P',
150
- bootstrapNodes: [],
151
- profile: 'storage',
152
- pinnedOwnerKeys: ['key-a'],
153
- });
154
- const identityPath = join(rootDir, 'donated-3', 'identity.key');
155
- await waitForFile(identityPath);
156
-
157
- await orch.removeContainer(created.dockerId);
158
- expect(existsSync(identityPath)).toBe(false);
159
- expect(existsSync(join(rootDir, 'donated-3'))).toBe(false);
160
- });
161
-
162
- // The failure the identity step can actually produce is a damaged key file,
163
- // and `DonationService.provision` turns every such throw into an `error`
164
- // record and lets the grantee retry. If the spawn path reserved its ports
165
- // before the step that throws, each retry would burn four more ports out of a
166
- // bounded range until provisioning stopped working altogether.
167
- it('reserves no ports when the identity step fails', async () => {
168
- const rootDir = join(tmpRoot, 'd');
169
- // Exactly one node's worth of ports (health, metrics, p2p, admin).
170
- const orch = makeOrchestrator(rootDir, { start: 18100, end: 18103 });
171
- await orch.init();
172
-
173
- mkdirSync(join(rootDir, 'donated-bad'), { recursive: true });
174
- writeFileSync(join(rootDir, 'donated-bad', 'identity.key'), 'not a protobuf private key', 'utf8');
175
-
176
- const request = {
177
- partyId: 'foreign-P',
178
- bootstrapNodes: [],
179
- profile: 'storage' as const,
180
- pinnedOwnerKeys: ['key-a'],
181
- };
182
- await expect(orch.createContainer({ ...request, containerId: 'donated-bad' })).rejects.toThrow();
183
-
184
- // The whole range is still free, so a healthy container still gets its four.
185
- const ok = await orch.createContainer({ ...request, containerId: 'donated-good' });
186
- expect(ok.p2pPort).toBeGreaterThanOrEqual(18100);
187
- });
188
-
189
- // The owner node carries the HOST's installer identity from <dataDir>, not a
190
- // per-workdir key — a second key in its workdir would mean the host's own
191
- // cadre node quietly changed peer id.
192
- it('leaves the owner node on its own installer identity', async () => {
193
- const rootDir = join(tmpRoot, 'e');
194
- const orch = makeOrchestrator(rootDir);
195
- await orch.init();
196
-
197
- const hostIdentity = join(tmpRoot, 'host-identity.key');
198
- await orch.ensureOwnerNode({ identityPath: hostIdentity, partyId: 'host-P', libp2pPort: 4101 });
199
-
200
- const ownerWorkdir = join(rootDir, OWNER_CONTAINER_ID);
201
- expect(await waitForFile(join(ownerWorkdir, 'identity-arg.txt'))).toBe(hostIdentity);
202
- expect(existsSync(join(ownerWorkdir, 'identity.key'))).toBe(false);
203
- });
204
- });
1
+ /**
2
+ * HostProcessOrchestrator per-node identity tests.
3
+ *
4
+ * A donated node must keep the peer id its host cadre approved across restarts,
5
+ * and needs a durable on-disk home for its node-local stores (`cadre-cli start`
6
+ * opens the file-backed bootstrap-peer / trusted-owner stores only when a
7
+ * protobuf identity key file is configured). The orchestrator therefore writes
8
+ * `<workdir>/identity.key` once per container and passes it as
9
+ * `--identity-protobuf`.
10
+ *
11
+ * A fake CLI records the `--identity-protobuf` argument it was launched with, so
12
+ * the spawn wiring is observable without a real cadre node.
13
+ */
14
+
15
+ import { afterEach, beforeEach, describe, expect, it } from 'vitest';
16
+ import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
17
+ import { tmpdir } from 'node:os';
18
+ import { join } from 'node:path';
19
+
20
+ import { HostProcessOrchestrator, OWNER_CONTAINER_ID } from '../orchestrator/host-process-orchestrator.js';
21
+ import { loadIdentity } from '../installer/identity.js';
22
+
23
+ // Writes the --identity-protobuf argument (as seen on its own command line) next
24
+ // to the startup token, then behaves like a minimal long-lived node.
25
+ const FAKE_CLI = `
26
+ import fs from 'node:fs';
27
+ import path from 'node:path';
28
+ const args = process.argv.slice(2);
29
+ const get = (flag) => { const i = args.indexOf(flag); return i >= 0 ? args[i + 1] : undefined; };
30
+ const tokenPath = get('--startup-token-file');
31
+ const token = process.env.CADRE_STARTUP_TOKEN ?? '';
32
+ if (tokenPath) {
33
+ const dir = path.dirname(tokenPath);
34
+ try { fs.writeFileSync(path.join(dir, 'identity-arg.txt'), get('--identity-protobuf') ?? '', 'utf8'); } catch (e) { console.error(e); }
35
+ if (token) { try { fs.writeFileSync(tokenPath, token, 'utf8'); } catch (e) { console.error(e); } }
36
+ }
37
+ process.on('SIGTERM', () => process.exit(0));
38
+ setInterval(() => {}, 1 << 30);
39
+ `;
40
+
41
+ let tmpRoot: string;
42
+ let scriptPath: string;
43
+ const orchestrators: HostProcessOrchestrator[] = [];
44
+
45
+ beforeEach(() => {
46
+ tmpRoot = mkdtempSync(join(tmpdir(), 'cadre-host-nodeid-'));
47
+ scriptPath = join(tmpRoot, 'fake-cli.mjs');
48
+ writeFileSync(scriptPath, FAKE_CLI, 'utf8');
49
+ });
50
+
51
+ afterEach(async () => {
52
+ for (const orch of orchestrators) {
53
+ for (const n of orch.listNodes()) {
54
+ try { await orch.removeContainer(n.dockerId); } catch { /* ignore */ }
55
+ }
56
+ }
57
+ orchestrators.length = 0;
58
+ await sleep(50);
59
+ try { rmSync(tmpRoot, { recursive: true, force: true }); } catch { /* ignore */ }
60
+ });
61
+
62
+ function makeOrchestrator(
63
+ rootDir: string,
64
+ portRange: { start: number; end: number } = { start: 17500, end: 17999 },
65
+ ): HostProcessOrchestrator {
66
+ mkdirSync(rootDir, { recursive: true });
67
+ const orch = new HostProcessOrchestrator({
68
+ rootDir,
69
+ portRange,
70
+ stopTimeoutMs: 1500,
71
+ spawn: { entrypoint: scriptPath },
72
+ });
73
+ orchestrators.push(orch);
74
+ return orch;
75
+ }
76
+
77
+ async function waitForFile(path: string, timeoutMs = 5000): Promise<string> {
78
+ const deadline = Date.now() + timeoutMs;
79
+ while (Date.now() < deadline) {
80
+ if (existsSync(path)) return readFileSync(path, 'utf8');
81
+ await sleep(50);
82
+ }
83
+ throw new Error(`file never appeared: ${path}`);
84
+ }
85
+
86
+ function sleep(ms: number): Promise<void> {
87
+ return new Promise((res) => setTimeout(res, ms));
88
+ }
89
+
90
+ describe('HostProcessOrchestrator node identity', () => {
91
+ it('passes --identity-protobuf pointing at a real key inside the node workdir', async () => {
92
+ const rootDir = join(tmpRoot, 'a');
93
+ const orch = makeOrchestrator(rootDir);
94
+ await orch.init();
95
+
96
+ await orch.createContainer({
97
+ containerId: 'donated-1',
98
+ partyId: 'foreign-P',
99
+ bootstrapNodes: [],
100
+ profile: 'storage',
101
+ pinnedOwnerKeys: ['key-a'],
102
+ });
103
+
104
+ const workdir = join(rootDir, 'donated-1');
105
+ const arg = await waitForFile(join(workdir, 'identity-arg.txt'));
106
+ expect(arg).toBe(join(workdir, 'identity.key'));
107
+ // Decodes as a libp2p protobuf private key → the child gets a usable identity.
108
+ expect(loadIdentity(arg).peerId).toMatch(/^12D3Koo/);
109
+ });
110
+
111
+ it('reuses the same identity when the same containerId is spawned again', async () => {
112
+ const rootDir = join(tmpRoot, 'b');
113
+ const orch = makeOrchestrator(rootDir);
114
+ await orch.init();
115
+
116
+ const first = await orch.createContainer({
117
+ containerId: 'donated-2',
118
+ partyId: 'foreign-P',
119
+ bootstrapNodes: [],
120
+ profile: 'storage',
121
+ pinnedOwnerKeys: ['key-a'],
122
+ });
123
+ const workdir = join(rootDir, 'donated-2');
124
+ await waitForFile(join(workdir, 'identity-arg.txt'));
125
+ const before = loadIdentity(join(workdir, 'identity.key')).peerId;
126
+
127
+ // Stop (not remove — removal deletes the workdir) and re-spawn, as a restart
128
+ // of a live loan would.
129
+ await orch.stopContainer(first.dockerId);
130
+ await orch.createContainer({
131
+ containerId: 'donated-2',
132
+ partyId: 'foreign-P',
133
+ bootstrapNodes: [],
134
+ profile: 'storage',
135
+ pinnedOwnerKeys: ['key-a'],
136
+ });
137
+
138
+ const after = loadIdentity(join(workdir, 'identity.key')).peerId;
139
+ expect(after).toBe(before);
140
+ });
141
+
142
+ it('destroys the identity with the workdir when the loan is terminated', async () => {
143
+ const rootDir = join(tmpRoot, 'c');
144
+ const orch = makeOrchestrator(rootDir);
145
+ await orch.init();
146
+
147
+ const created = await orch.createContainer({
148
+ containerId: 'donated-3',
149
+ partyId: 'foreign-P',
150
+ bootstrapNodes: [],
151
+ profile: 'storage',
152
+ pinnedOwnerKeys: ['key-a'],
153
+ });
154
+ const identityPath = join(rootDir, 'donated-3', 'identity.key');
155
+ await waitForFile(identityPath);
156
+
157
+ await orch.removeContainer(created.dockerId);
158
+ expect(existsSync(identityPath)).toBe(false);
159
+ expect(existsSync(join(rootDir, 'donated-3'))).toBe(false);
160
+ });
161
+
162
+ // The failure the identity step can actually produce is a damaged key file,
163
+ // and `DonationService.provision` turns every such throw into an `error`
164
+ // record and lets the grantee retry. If the spawn path reserved its ports
165
+ // before the step that throws, each retry would burn four more ports out of a
166
+ // bounded range until provisioning stopped working altogether.
167
+ it('reserves no ports when the identity step fails', async () => {
168
+ const rootDir = join(tmpRoot, 'd');
169
+ // Exactly one node's worth of ports (health, metrics, p2p, admin).
170
+ const orch = makeOrchestrator(rootDir, { start: 18100, end: 18103 });
171
+ await orch.init();
172
+
173
+ mkdirSync(join(rootDir, 'donated-bad'), { recursive: true });
174
+ writeFileSync(join(rootDir, 'donated-bad', 'identity.key'), 'not a protobuf private key', 'utf8');
175
+
176
+ const request = {
177
+ partyId: 'foreign-P',
178
+ bootstrapNodes: [],
179
+ profile: 'storage' as const,
180
+ pinnedOwnerKeys: ['key-a'],
181
+ };
182
+ await expect(orch.createContainer({ ...request, containerId: 'donated-bad' })).rejects.toThrow();
183
+
184
+ // The whole range is still free, so a healthy container still gets its four.
185
+ const ok = await orch.createContainer({ ...request, containerId: 'donated-good' });
186
+ expect(ok.p2pPort).toBeGreaterThanOrEqual(18100);
187
+ });
188
+
189
+ // The owner node carries the HOST's installer identity from <dataDir>, not a
190
+ // per-workdir key — a second key in its workdir would mean the host's own
191
+ // cadre node quietly changed peer id.
192
+ it('leaves the owner node on its own installer identity', async () => {
193
+ const rootDir = join(tmpRoot, 'e');
194
+ const orch = makeOrchestrator(rootDir);
195
+ await orch.init();
196
+
197
+ const hostIdentity = join(tmpRoot, 'host-identity.key');
198
+ await orch.ensureOwnerNode({ identityPath: hostIdentity, partyId: 'host-P', libp2pPort: 4101 });
199
+
200
+ const ownerWorkdir = join(rootDir, OWNER_CONTAINER_ID);
201
+ expect(await waitForFile(join(ownerWorkdir, 'identity-arg.txt'))).toBe(hostIdentity);
202
+ expect(existsSync(join(ownerWorkdir, 'identity.key'))).toBe(false);
203
+ });
204
+ });