@serfab/cadre-host 1.10.0 → 1.12.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 +423 -423
- package/dist/ui/assets/index-C6nELNfC.js.map +1 -1
- package/package.json +4 -4
- package/src/__tests__/global-setup.ts +58 -58
- package/src/__tests__/orchestrator-handle-contract.ts +134 -134
- package/src/__tests__/orchestrator-node-identity.test.ts +203 -203
- package/src/__tests__/orchestrator-ports.test.ts +143 -143
- package/src/__tests__/orchestrator-teardown.ts +65 -65
- package/src/__tests__/orchestrator.test.ts +1063 -1063
- package/src/bin/host.ts +1357 -1357
- package/src/donation/__tests__/fake-orchestrator.test.ts +173 -173
- package/src/donation/donation-store.ts +176 -176
- package/src/donation/grant-service.ts +252 -252
- package/src/donation/index.ts +65 -65
- package/src/donation/types.ts +260 -260
- package/src/orchestrator/host-process-orchestrator.ts +1260 -1260
- package/src/orchestrator/port-allocator.ts +73 -73
- package/src/server/__tests__/grants-admin-route.test.ts +171 -171
- package/src/server/events/types.ts +24 -24
- package/src/server/index.ts +267 -267
- package/src/server/routes/grants-admin.ts +74 -74
|
@@ -1,173 +1,173 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Contract tests for `FakeOrchestrator` itself — the handle lifecycle it borrows
|
|
3
|
-
* from `HostProcessOrchestrator`. The donation suites lean on these semantics to
|
|
4
|
-
* express races that only exist because the real class drops and rejects
|
|
5
|
-
* handles, so this file exists to stop a later relaxation of the fake from
|
|
6
|
-
* quietly turning one of those tests green.
|
|
7
|
-
*
|
|
8
|
-
* The rules the fake shares with the real class live in
|
|
9
|
-
* `src/__tests__/orchestrator-handle-contract.ts` and run against both — here
|
|
10
|
-
* and in `src/__tests__/orchestrator.test.ts` — so a disagreement between the
|
|
11
|
-
* two fails a test. The cases in this file add what only the fake has: its
|
|
12
|
-
* recording arrays (`stopped`, `removed`, `reclaimedWorkdirs`) and its hooks.
|
|
13
|
-
* Each names the real behaviour it mirrors.
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
import { describe, expect, it } from 'vitest';
|
|
17
|
-
|
|
18
|
-
import type { OrchestratorCreateRequest } from '@serfab/cadre-provider';
|
|
19
|
-
|
|
20
|
-
import { describeHandleContract } from '../../__tests__/orchestrator-handle-contract.js';
|
|
21
|
-
import { FakeOrchestrator } from './fake-orchestrator.js';
|
|
22
|
-
|
|
23
|
-
const request = (containerId: string): OrchestratorCreateRequest => ({
|
|
24
|
-
containerId,
|
|
25
|
-
partyId: 'party-P',
|
|
26
|
-
bootstrapNodes: ['/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWA9hbnKrRnPRSPTRkzXqTHzGE8YpJ3JHZmQ5tGwLRTMmp'],
|
|
27
|
-
profile: 'storage',
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describeHandleContract<FakeOrchestrator>('FakeOrchestrator', {
|
|
31
|
-
make: () => new FakeOrchestrator(),
|
|
32
|
-
request,
|
|
33
|
-
// No process to wait for: a fake child is running as soon as it exists.
|
|
34
|
-
started: () => Promise.resolve(),
|
|
35
|
-
failNextCreate: (orch) => { orch.failCreate = true; },
|
|
36
|
-
});
|
|
37
|
-
|
|
38
|
-
describe('FakeOrchestrator handle lifecycle', () => {
|
|
39
|
-
it('drops the prior handle when a re-spawn succeeds (mirrors dropStaleHandle)', async () => {
|
|
40
|
-
const orch = new FakeOrchestrator();
|
|
41
|
-
const first = await orch.createContainer(request('grn_1'));
|
|
42
|
-
orch.crash(first.dockerId);
|
|
43
|
-
const second = await orch.createContainer(request('grn_1'));
|
|
44
|
-
|
|
45
|
-
await expect(orch.stopContainer(first.dockerId)).rejects.toThrow(
|
|
46
|
-
`Container not found: ${first.dockerId}`,
|
|
47
|
-
);
|
|
48
|
-
// The rejected call left no trace: `stopped` records handles acted on, not
|
|
49
|
-
// calls attempted.
|
|
50
|
-
expect(orch.stopped).toEqual([]);
|
|
51
|
-
// …and the container now resolves only to the new spawn.
|
|
52
|
-
expect(orch.resolveDockerId('grn_1')).toBe(second.dockerId);
|
|
53
|
-
|
|
54
|
-
await orch.stopContainer(second.dockerId);
|
|
55
|
-
expect(orch.stopped).toEqual([second.dockerId]);
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('keeps the prior handle live across the whole pre-drop await window', async () => {
|
|
59
|
-
const orch = new FakeOrchestrator();
|
|
60
|
-
const first = await orch.createContainer(request('grn_1'));
|
|
61
|
-
|
|
62
|
-
// The invariant the donation race tests rest on: a concurrent stop started
|
|
63
|
-
// from `onCreate` still finds the old handle. Deferring it onto a microtask
|
|
64
|
-
// puts it strictly after `onCreate` returns and strictly before the
|
|
65
|
-
// `createDelayMs` timer, so this one case pins the drop behind BOTH — with
|
|
66
|
-
// no dependence on wall-clock timing. The outcome is captured rather than
|
|
67
|
-
// rethrown so a regression fails this assertion instead of surfacing as an
|
|
68
|
-
// unhandled rejection.
|
|
69
|
-
orch.createDelayMs = 1;
|
|
70
|
-
let outcome: Promise<string> | undefined;
|
|
71
|
-
orch.onCreate = () => {
|
|
72
|
-
outcome ??= Promise.resolve()
|
|
73
|
-
.then(() => orch.stopContainer(first.dockerId))
|
|
74
|
-
.then(() => 'stopped', (err: Error) => `rejected: ${err.message}`);
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const second = await orch.createContainer(request('grn_1'));
|
|
78
|
-
|
|
79
|
-
expect(await outcome).toBe('stopped');
|
|
80
|
-
expect(orch.stopped).toEqual([first.dockerId]);
|
|
81
|
-
expect(orch.resolveDockerId('grn_1')).toBe(second.dockerId);
|
|
82
|
-
});
|
|
83
|
-
|
|
84
|
-
it('leaves the prior handle in place when the re-spawn fails (mirrors restoreDroppedHandles)', async () => {
|
|
85
|
-
const orch = new FakeOrchestrator();
|
|
86
|
-
const first = await orch.createContainer(request('grn_1'));
|
|
87
|
-
|
|
88
|
-
orch.failCreate = true;
|
|
89
|
-
await expect(orch.createContainer(request('grn_1'))).rejects.toThrow('spawn boom');
|
|
90
|
-
|
|
91
|
-
// Host state is exactly as the failed attempt found it — which is what lets
|
|
92
|
-
// `DonationSupervisor.giveUp` still stop the child it knows about.
|
|
93
|
-
await expect(orch.stopContainer(first.dockerId)).resolves.toBeUndefined();
|
|
94
|
-
expect(orch.stopped).toEqual([first.dockerId]);
|
|
95
|
-
expect(orch.resolveDockerId('grn_1')).toBe(first.dockerId);
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it('rejects a never-issued dockerId (mirrors requireHandle)', async () => {
|
|
99
|
-
const orch = new FakeOrchestrator();
|
|
100
|
-
|
|
101
|
-
await expect(orch.removeContainer('dock_nope')).rejects.toThrow(
|
|
102
|
-
'Container not found: dock_nope',
|
|
103
|
-
);
|
|
104
|
-
expect(orch.removed).toEqual([]);
|
|
105
|
-
});
|
|
106
|
-
|
|
107
|
-
it('rejects a second call once a handle has been removed (mirrors removeContainer deleting it)', async () => {
|
|
108
|
-
const orch = new FakeOrchestrator();
|
|
109
|
-
const { dockerId } = await orch.createContainer(request('grn_1'));
|
|
110
|
-
|
|
111
|
-
await orch.removeContainer(dockerId);
|
|
112
|
-
expect(orch.removed).toEqual([dockerId]);
|
|
113
|
-
|
|
114
|
-
await expect(orch.stopContainer(dockerId)).rejects.toThrow(
|
|
115
|
-
`Container not found: ${dockerId}`,
|
|
116
|
-
);
|
|
117
|
-
expect(orch.stopped).toEqual([]);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('fires onStop for a rejected stop, so a test can still observe the attempt', async () => {
|
|
121
|
-
const orch = new FakeOrchestrator();
|
|
122
|
-
const attempted: string[] = [];
|
|
123
|
-
orch.onStop = (id) => { attempted.push(id); };
|
|
124
|
-
|
|
125
|
-
await expect(orch.stopContainer('dock_nope')).rejects.toThrow('Container not found');
|
|
126
|
-
expect(attempted).toEqual(['dock_nope']);
|
|
127
|
-
expect(orch.stopped).toEqual([]);
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
it('refuses to reclaim the workdir of a container that still resolves (mirrors the live-handle guard)', async () => {
|
|
131
|
-
const orch = new FakeOrchestrator();
|
|
132
|
-
await orch.createContainer(request('grn_1'));
|
|
133
|
-
|
|
134
|
-
// A live handle owns that directory; only `removeContainer` — which stops
|
|
135
|
-
// the child first — may delete it.
|
|
136
|
-
expect(orch.reclaimWorkdir('grn_1')).toBe(false);
|
|
137
|
-
expect(orch.reclaimedWorkdirs).toEqual([]);
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it('reclaims the workdir of a container no handle owns', async () => {
|
|
141
|
-
const orch = new FakeOrchestrator();
|
|
142
|
-
|
|
143
|
-
// Never spawned — the stuck-`provisioning` record whose host died before a
|
|
144
|
-
// handle ever existed.
|
|
145
|
-
expect(orch.reclaimWorkdir('grn_orphan')).toBe(true);
|
|
146
|
-
expect(orch.reclaimedWorkdirs).toEqual(['grn_orphan']);
|
|
147
|
-
|
|
148
|
-
// …and once the container's own handle is removed, its id becomes
|
|
149
|
-
// reclaimable too.
|
|
150
|
-
const { dockerId } = await orch.createContainer(request('grn_1'));
|
|
151
|
-
expect(orch.reclaimWorkdir('grn_1')).toBe(false);
|
|
152
|
-
await orch.removeContainer(dockerId);
|
|
153
|
-
expect(orch.reclaimWorkdir('grn_1')).toBe(true);
|
|
154
|
-
expect(orch.reclaimedWorkdirs).toEqual(['grn_orphan', 'grn_1']);
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
it('fires onSpawned after the drop, with the new handle live and the old one gone', async () => {
|
|
158
|
-
const orch = new FakeOrchestrator();
|
|
159
|
-
const first = await orch.createContainer(request('grn_1'));
|
|
160
|
-
orch.crash(first.dockerId);
|
|
161
|
-
|
|
162
|
-
let observed: { spawned: string; resolves: string | undefined } | undefined;
|
|
163
|
-
orch.onSpawned = (dockerId) => {
|
|
164
|
-
observed = { spawned: dockerId, resolves: orch.resolveDockerId('grn_1') };
|
|
165
|
-
};
|
|
166
|
-
const second = await orch.createContainer(request('grn_1'));
|
|
167
|
-
|
|
168
|
-
// The post-drop world: the container already resolves to the new handle, so
|
|
169
|
-
// work driven from here sees what `abandonRespawn`'s caller would see.
|
|
170
|
-
expect(observed).toEqual({ spawned: second.dockerId, resolves: second.dockerId });
|
|
171
|
-
expect(second.dockerId).not.toBe(first.dockerId);
|
|
172
|
-
});
|
|
173
|
-
});
|
|
1
|
+
/**
|
|
2
|
+
* Contract tests for `FakeOrchestrator` itself — the handle lifecycle it borrows
|
|
3
|
+
* from `HostProcessOrchestrator`. The donation suites lean on these semantics to
|
|
4
|
+
* express races that only exist because the real class drops and rejects
|
|
5
|
+
* handles, so this file exists to stop a later relaxation of the fake from
|
|
6
|
+
* quietly turning one of those tests green.
|
|
7
|
+
*
|
|
8
|
+
* The rules the fake shares with the real class live in
|
|
9
|
+
* `src/__tests__/orchestrator-handle-contract.ts` and run against both — here
|
|
10
|
+
* and in `src/__tests__/orchestrator.test.ts` — so a disagreement between the
|
|
11
|
+
* two fails a test. The cases in this file add what only the fake has: its
|
|
12
|
+
* recording arrays (`stopped`, `removed`, `reclaimedWorkdirs`) and its hooks.
|
|
13
|
+
* Each names the real behaviour it mirrors.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { describe, expect, it } from 'vitest';
|
|
17
|
+
|
|
18
|
+
import type { OrchestratorCreateRequest } from '@serfab/cadre-provider';
|
|
19
|
+
|
|
20
|
+
import { describeHandleContract } from '../../__tests__/orchestrator-handle-contract.js';
|
|
21
|
+
import { FakeOrchestrator } from './fake-orchestrator.js';
|
|
22
|
+
|
|
23
|
+
const request = (containerId: string): OrchestratorCreateRequest => ({
|
|
24
|
+
containerId,
|
|
25
|
+
partyId: 'party-P',
|
|
26
|
+
bootstrapNodes: ['/ip4/127.0.0.1/tcp/4001/p2p/12D3KooWA9hbnKrRnPRSPTRkzXqTHzGE8YpJ3JHZmQ5tGwLRTMmp'],
|
|
27
|
+
profile: 'storage',
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describeHandleContract<FakeOrchestrator>('FakeOrchestrator', {
|
|
31
|
+
make: () => new FakeOrchestrator(),
|
|
32
|
+
request,
|
|
33
|
+
// No process to wait for: a fake child is running as soon as it exists.
|
|
34
|
+
started: () => Promise.resolve(),
|
|
35
|
+
failNextCreate: (orch) => { orch.failCreate = true; },
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe('FakeOrchestrator handle lifecycle', () => {
|
|
39
|
+
it('drops the prior handle when a re-spawn succeeds (mirrors dropStaleHandle)', async () => {
|
|
40
|
+
const orch = new FakeOrchestrator();
|
|
41
|
+
const first = await orch.createContainer(request('grn_1'));
|
|
42
|
+
orch.crash(first.dockerId);
|
|
43
|
+
const second = await orch.createContainer(request('grn_1'));
|
|
44
|
+
|
|
45
|
+
await expect(orch.stopContainer(first.dockerId)).rejects.toThrow(
|
|
46
|
+
`Container not found: ${first.dockerId}`,
|
|
47
|
+
);
|
|
48
|
+
// The rejected call left no trace: `stopped` records handles acted on, not
|
|
49
|
+
// calls attempted.
|
|
50
|
+
expect(orch.stopped).toEqual([]);
|
|
51
|
+
// …and the container now resolves only to the new spawn.
|
|
52
|
+
expect(orch.resolveDockerId('grn_1')).toBe(second.dockerId);
|
|
53
|
+
|
|
54
|
+
await orch.stopContainer(second.dockerId);
|
|
55
|
+
expect(orch.stopped).toEqual([second.dockerId]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('keeps the prior handle live across the whole pre-drop await window', async () => {
|
|
59
|
+
const orch = new FakeOrchestrator();
|
|
60
|
+
const first = await orch.createContainer(request('grn_1'));
|
|
61
|
+
|
|
62
|
+
// The invariant the donation race tests rest on: a concurrent stop started
|
|
63
|
+
// from `onCreate` still finds the old handle. Deferring it onto a microtask
|
|
64
|
+
// puts it strictly after `onCreate` returns and strictly before the
|
|
65
|
+
// `createDelayMs` timer, so this one case pins the drop behind BOTH — with
|
|
66
|
+
// no dependence on wall-clock timing. The outcome is captured rather than
|
|
67
|
+
// rethrown so a regression fails this assertion instead of surfacing as an
|
|
68
|
+
// unhandled rejection.
|
|
69
|
+
orch.createDelayMs = 1;
|
|
70
|
+
let outcome: Promise<string> | undefined;
|
|
71
|
+
orch.onCreate = () => {
|
|
72
|
+
outcome ??= Promise.resolve()
|
|
73
|
+
.then(() => orch.stopContainer(first.dockerId))
|
|
74
|
+
.then(() => 'stopped', (err: Error) => `rejected: ${err.message}`);
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
const second = await orch.createContainer(request('grn_1'));
|
|
78
|
+
|
|
79
|
+
expect(await outcome).toBe('stopped');
|
|
80
|
+
expect(orch.stopped).toEqual([first.dockerId]);
|
|
81
|
+
expect(orch.resolveDockerId('grn_1')).toBe(second.dockerId);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('leaves the prior handle in place when the re-spawn fails (mirrors restoreDroppedHandles)', async () => {
|
|
85
|
+
const orch = new FakeOrchestrator();
|
|
86
|
+
const first = await orch.createContainer(request('grn_1'));
|
|
87
|
+
|
|
88
|
+
orch.failCreate = true;
|
|
89
|
+
await expect(orch.createContainer(request('grn_1'))).rejects.toThrow('spawn boom');
|
|
90
|
+
|
|
91
|
+
// Host state is exactly as the failed attempt found it — which is what lets
|
|
92
|
+
// `DonationSupervisor.giveUp` still stop the child it knows about.
|
|
93
|
+
await expect(orch.stopContainer(first.dockerId)).resolves.toBeUndefined();
|
|
94
|
+
expect(orch.stopped).toEqual([first.dockerId]);
|
|
95
|
+
expect(orch.resolveDockerId('grn_1')).toBe(first.dockerId);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('rejects a never-issued dockerId (mirrors requireHandle)', async () => {
|
|
99
|
+
const orch = new FakeOrchestrator();
|
|
100
|
+
|
|
101
|
+
await expect(orch.removeContainer('dock_nope')).rejects.toThrow(
|
|
102
|
+
'Container not found: dock_nope',
|
|
103
|
+
);
|
|
104
|
+
expect(orch.removed).toEqual([]);
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it('rejects a second call once a handle has been removed (mirrors removeContainer deleting it)', async () => {
|
|
108
|
+
const orch = new FakeOrchestrator();
|
|
109
|
+
const { dockerId } = await orch.createContainer(request('grn_1'));
|
|
110
|
+
|
|
111
|
+
await orch.removeContainer(dockerId);
|
|
112
|
+
expect(orch.removed).toEqual([dockerId]);
|
|
113
|
+
|
|
114
|
+
await expect(orch.stopContainer(dockerId)).rejects.toThrow(
|
|
115
|
+
`Container not found: ${dockerId}`,
|
|
116
|
+
);
|
|
117
|
+
expect(orch.stopped).toEqual([]);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('fires onStop for a rejected stop, so a test can still observe the attempt', async () => {
|
|
121
|
+
const orch = new FakeOrchestrator();
|
|
122
|
+
const attempted: string[] = [];
|
|
123
|
+
orch.onStop = (id) => { attempted.push(id); };
|
|
124
|
+
|
|
125
|
+
await expect(orch.stopContainer('dock_nope')).rejects.toThrow('Container not found');
|
|
126
|
+
expect(attempted).toEqual(['dock_nope']);
|
|
127
|
+
expect(orch.stopped).toEqual([]);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
it('refuses to reclaim the workdir of a container that still resolves (mirrors the live-handle guard)', async () => {
|
|
131
|
+
const orch = new FakeOrchestrator();
|
|
132
|
+
await orch.createContainer(request('grn_1'));
|
|
133
|
+
|
|
134
|
+
// A live handle owns that directory; only `removeContainer` — which stops
|
|
135
|
+
// the child first — may delete it.
|
|
136
|
+
expect(orch.reclaimWorkdir('grn_1')).toBe(false);
|
|
137
|
+
expect(orch.reclaimedWorkdirs).toEqual([]);
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('reclaims the workdir of a container no handle owns', async () => {
|
|
141
|
+
const orch = new FakeOrchestrator();
|
|
142
|
+
|
|
143
|
+
// Never spawned — the stuck-`provisioning` record whose host died before a
|
|
144
|
+
// handle ever existed.
|
|
145
|
+
expect(orch.reclaimWorkdir('grn_orphan')).toBe(true);
|
|
146
|
+
expect(orch.reclaimedWorkdirs).toEqual(['grn_orphan']);
|
|
147
|
+
|
|
148
|
+
// …and once the container's own handle is removed, its id becomes
|
|
149
|
+
// reclaimable too.
|
|
150
|
+
const { dockerId } = await orch.createContainer(request('grn_1'));
|
|
151
|
+
expect(orch.reclaimWorkdir('grn_1')).toBe(false);
|
|
152
|
+
await orch.removeContainer(dockerId);
|
|
153
|
+
expect(orch.reclaimWorkdir('grn_1')).toBe(true);
|
|
154
|
+
expect(orch.reclaimedWorkdirs).toEqual(['grn_orphan', 'grn_1']);
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('fires onSpawned after the drop, with the new handle live and the old one gone', async () => {
|
|
158
|
+
const orch = new FakeOrchestrator();
|
|
159
|
+
const first = await orch.createContainer(request('grn_1'));
|
|
160
|
+
orch.crash(first.dockerId);
|
|
161
|
+
|
|
162
|
+
let observed: { spawned: string; resolves: string | undefined } | undefined;
|
|
163
|
+
orch.onSpawned = (dockerId) => {
|
|
164
|
+
observed = { spawned: dockerId, resolves: orch.resolveDockerId('grn_1') };
|
|
165
|
+
};
|
|
166
|
+
const second = await orch.createContainer(request('grn_1'));
|
|
167
|
+
|
|
168
|
+
// The post-drop world: the container already resolves to the new handle, so
|
|
169
|
+
// work driven from here sees what `abandonRespawn`'s caller would see.
|
|
170
|
+
expect(observed).toEqual({ spawned: second.dockerId, resolves: second.dockerId });
|
|
171
|
+
expect(second.dockerId).not.toBe(first.dockerId);
|
|
172
|
+
});
|
|
173
|
+
});
|