@antelopejs/dms 0.18.0 → 0.18.1
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/CHANGELOG.md +31 -0
- package/README.md +3 -3
- package/bin/control/backend-records.test.mjs +276 -0
- package/bin/control/backend.mjs +212 -0
- package/bin/control/backend.test.mjs +325 -0
- package/bin/control/base.mjs +3 -7
- package/bin/control/browser-client.mjs +6 -4
- package/bin/control/browser.mjs +173 -36
- package/bin/control/build-cache.mjs +394 -0
- package/bin/control/build-cache.test.mjs +270 -0
- package/bin/control/build-inputs.mjs +172 -0
- package/bin/control/build-key-refusals.test.mjs +138 -0
- package/bin/control/build-key.mjs +479 -0
- package/bin/control/build-key.test.mjs +392 -0
- package/bin/control/build-layout.test.mjs +314 -0
- package/bin/control/commands-stack.mjs +46 -6
- package/bin/control/commands-verify.mjs +9 -2
- package/bin/control/doctor.mjs +87 -16
- package/bin/control/forwarder.mjs +86 -0
- package/bin/control/forwarder.test.mjs +141 -0
- package/bin/control/frontend-check.mjs +54 -0
- package/bin/control/frontend-workspace.mjs +150 -0
- package/bin/control/frontend-workspace.test.mjs +133 -0
- package/bin/control/helpers/env.mjs +21 -0
- package/bin/control/helpers/evidence.mjs +13 -0
- package/bin/control/helpers/fake-run.mjs +290 -0
- package/bin/control/helpers/key-fixture.mjs +82 -0
- package/bin/control/helpers/pids.mjs +27 -0
- package/bin/control/helpers/repo.mjs +13 -0
- package/bin/control/helpers/tree.mjs +40 -0
- package/bin/control/identity.mjs +238 -0
- package/bin/control/identity.test.mjs +166 -0
- package/bin/control/journey.mjs +64 -16
- package/bin/control/journey.test.mjs +60 -2
- package/bin/control/namespaces.test.mjs +211 -0
- package/bin/control/options.mjs +2 -0
- package/bin/control/pid-guard.test.mjs +202 -0
- package/bin/control/prepared-workspace.mjs +20 -0
- package/bin/control/processes.mjs +122 -33
- package/bin/control/processes.test.mjs +255 -0
- package/bin/control/renderer.test.mjs +2 -1
- package/bin/control/run-copy.mjs +77 -0
- package/bin/control/run-state.mjs +142 -16
- package/bin/control/stack.mjs +259 -210
- package/bin/control/tab.mjs +13 -6
- package/bin/control/tab.test.mjs +444 -0
- package/bin/control/timing.mjs +244 -0
- package/bin/control/timing.test.mjs +197 -0
- package/bin/control/verify/decode.mjs +34 -21
- package/bin/control/verify/decode.test.mjs +39 -0
- package/bin/control/verify/doors.test.mjs +2 -0
- package/bin/control/verify/map.mjs +57 -7
- package/bin/control/verify/map.test.mjs +264 -0
- package/bin/control/verify/skill.mjs +12 -3
- package/bin/control/verify/types.mjs +2 -2
- package/bin/control/verify/verdict.mjs +37 -7
- package/bin/dms-control.mjs +14 -6
- package/frontend-vue/layers/dms-layout/app/components/layout/DashboardSearch.vue +24 -1
- package/package.json +1 -1
- package/skills/dms-project/SKILL.md +6 -3
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { spawn } from "node:child_process";
|
|
3
|
+
import { once } from "node:events";
|
|
4
|
+
import { mkdtempSync, rmSync } from "node:fs";
|
|
5
|
+
import { createServer } from "node:net";
|
|
6
|
+
import { tmpdir } from "node:os";
|
|
7
|
+
import { join } from "node:path";
|
|
8
|
+
import test from "node:test";
|
|
9
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
10
|
+
import { canChoosePid, chooseNextPid } from "./helpers/pids.mjs";
|
|
11
|
+
import { isRunning, startTimeOf } from "./identity.mjs";
|
|
12
|
+
import {
|
|
13
|
+
descendantsOf,
|
|
14
|
+
freePort,
|
|
15
|
+
killRecorded,
|
|
16
|
+
listenersOf,
|
|
17
|
+
startDetached,
|
|
18
|
+
stopRecorded,
|
|
19
|
+
} from "./processes.mjs";
|
|
20
|
+
|
|
21
|
+
const SLOW_STOP_MS = 5_000;
|
|
22
|
+
const POLL_MS = 20;
|
|
23
|
+
|
|
24
|
+
/** Longer than one poll of stopRecorded's grace period, so the SIGTERM is sent before the target dies. */
|
|
25
|
+
const STOP_POLL_MS = 600;
|
|
26
|
+
|
|
27
|
+
/** How often the test tries to give the target's pid to a new process: a process of another test file can take the pid between the choice and the start, and then holds it itself. */
|
|
28
|
+
const TAKE_ATTEMPTS = 20;
|
|
29
|
+
|
|
30
|
+
const HOLDER = `
|
|
31
|
+
const { createServer } = require("node:net");
|
|
32
|
+
createServer().listen(Number(process.argv[1]), "127.0.0.1");
|
|
33
|
+
process.on("SIGTERM", () => {});
|
|
34
|
+
setInterval(() => {}, 1000);
|
|
35
|
+
`;
|
|
36
|
+
|
|
37
|
+
const LEADER = `
|
|
38
|
+
const { spawn } = require("node:child_process");
|
|
39
|
+
const [sameGroup, ownGroup] = process.argv.slice(1);
|
|
40
|
+
const holder = (port, options) =>
|
|
41
|
+
spawn(process.execPath, ["-e", ${JSON.stringify(HOLDER)}, port], { stdio: "ignore", ...options });
|
|
42
|
+
holder(sameGroup, {});
|
|
43
|
+
holder(ownGroup, { detached: true });
|
|
44
|
+
process.on("SIGTERM", () => {});
|
|
45
|
+
setInterval(() => {}, 1000);
|
|
46
|
+
`;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Replaces `process.kill` for the test with a recorder that passes on only
|
|
50
|
+
* the calls aimed at `allowed` or its group, so a regression records a call
|
|
51
|
+
* instead of signalling every process of the user.
|
|
52
|
+
*
|
|
53
|
+
* @param {import("node:test").TestContext} t The test
|
|
54
|
+
* @param {number} [allowed] The one process a call may reach
|
|
55
|
+
* @returns {unknown[][]} The calls `process.kill` received
|
|
56
|
+
*/
|
|
57
|
+
function recordKills(t, allowed) {
|
|
58
|
+
/** @type {unknown[][]} */
|
|
59
|
+
const calls = [];
|
|
60
|
+
const original = process.kill.bind(process);
|
|
61
|
+
process.kill = (pid, signal) => {
|
|
62
|
+
calls.push([pid, signal]);
|
|
63
|
+
return allowed !== undefined && Math.abs(pid) === allowed
|
|
64
|
+
? original(pid, signal)
|
|
65
|
+
: true;
|
|
66
|
+
};
|
|
67
|
+
t.after(() => {
|
|
68
|
+
process.kill = original;
|
|
69
|
+
});
|
|
70
|
+
return calls;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Starts a SIGTERM-proof leader with one child in its group and one in a
|
|
75
|
+
* group of its own, each holding a port.
|
|
76
|
+
*
|
|
77
|
+
* @param {import("node:test").TestContext} t The test
|
|
78
|
+
* @param {number[]} ports The ports of the child in the group and of the other
|
|
79
|
+
* @returns {Promise<ReturnType<typeof startDetached>>} The leader's record
|
|
80
|
+
*/
|
|
81
|
+
async function startTree(t, ports) {
|
|
82
|
+
const logs = mkdtempSync(join(tmpdir(), "dms-control-processes-test-"));
|
|
83
|
+
const leader = startDetached({
|
|
84
|
+
name: "tree",
|
|
85
|
+
program: process.execPath,
|
|
86
|
+
args: ["-e", LEADER, ...ports.map(String)],
|
|
87
|
+
cwd: logs,
|
|
88
|
+
env: process.env,
|
|
89
|
+
logs,
|
|
90
|
+
});
|
|
91
|
+
t.after(async () => {
|
|
92
|
+
await killRecorded([leader]).catch(() => []);
|
|
93
|
+
rmSync(logs, { recursive: true, force: true });
|
|
94
|
+
});
|
|
95
|
+
for (let waited = 0; waited < SLOW_STOP_MS; waited += POLL_MS) {
|
|
96
|
+
if (ports.every((port) => listenersOf(port)?.length)) return leader;
|
|
97
|
+
await sleep(POLL_MS);
|
|
98
|
+
}
|
|
99
|
+
throw new Error("the tree never listened");
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/** @param {number} port */
|
|
103
|
+
function bind(port) {
|
|
104
|
+
return new Promise((resolve, reject) => {
|
|
105
|
+
const server = createServer();
|
|
106
|
+
server.once("error", reject);
|
|
107
|
+
server.listen(port, "127.0.0.1", () => server.close(() => resolve(true)));
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
await test("killRecorded ends a SIGTERM-proof tree at once and frees every port", async (t) => {
|
|
112
|
+
const ports = [await freePort(), await freePort()];
|
|
113
|
+
const leader = await startTree(t, ports);
|
|
114
|
+
const started = Date.now();
|
|
115
|
+
const lines = await killRecorded([leader]);
|
|
116
|
+
assert.ok(
|
|
117
|
+
Date.now() - started < SLOW_STOP_MS,
|
|
118
|
+
"a SIGTERM and a grace period take ten seconds on this tree",
|
|
119
|
+
);
|
|
120
|
+
assert.equal(isRunning(leader.pid), false);
|
|
121
|
+
assert.equal(lines.length, 3);
|
|
122
|
+
for (const port of ports) assert.equal(await bind(port), true);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
await test("killRecorded resolves for a process that is already gone", async () => {
|
|
126
|
+
assert.deepEqual(
|
|
127
|
+
await killRecorded([{ pid: 2 ** 22 + 1, startTime: 1 }]),
|
|
128
|
+
[],
|
|
129
|
+
);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
await test("killRecorded returns once a killed process is a zombie nothing reaps; waiting for the pid to vanish throws after the grace period", async (t) => {
|
|
133
|
+
const dir = mkdtempSync(join(tmpdir(), "dms-control-zombie-"));
|
|
134
|
+
const parent = startDetached({
|
|
135
|
+
name: "zombie-parent",
|
|
136
|
+
program: "sh",
|
|
137
|
+
args: ["-c", "sleep 60 & exec sleep 60"],
|
|
138
|
+
cwd: dir,
|
|
139
|
+
env: process.env,
|
|
140
|
+
logs: dir,
|
|
141
|
+
});
|
|
142
|
+
t.after(async () => {
|
|
143
|
+
await killRecorded([parent]).catch(() => []);
|
|
144
|
+
rmSync(dir, { recursive: true, force: true });
|
|
145
|
+
});
|
|
146
|
+
let children = descendantsOf([parent.pid]);
|
|
147
|
+
for (
|
|
148
|
+
let waited = 0;
|
|
149
|
+
children.length === 0 && waited < SLOW_STOP_MS;
|
|
150
|
+
waited += POLL_MS
|
|
151
|
+
) {
|
|
152
|
+
await sleep(POLL_MS);
|
|
153
|
+
children = descendantsOf([parent.pid]);
|
|
154
|
+
}
|
|
155
|
+
assert.equal(children.length, 1);
|
|
156
|
+
const records = children.map((pid) => ({ pid, startTime: startTimeOf(pid) }));
|
|
157
|
+
const outcome = await Promise.race([
|
|
158
|
+
killRecorded(records).then(() => "returned"),
|
|
159
|
+
sleep(3_000).then(() => "still waiting"),
|
|
160
|
+
]);
|
|
161
|
+
assert.equal(outcome, "returned");
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
await test("killRecorded kills a process the caller started and signals nothing else", async (t) => {
|
|
165
|
+
const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], {
|
|
166
|
+
stdio: "ignore",
|
|
167
|
+
detached: true,
|
|
168
|
+
});
|
|
169
|
+
t.after(() => child.kill());
|
|
170
|
+
const pid = child.pid;
|
|
171
|
+
assert.ok(pid !== undefined && pid > 1, "the child started");
|
|
172
|
+
const calls = recordKills(t, pid);
|
|
173
|
+
await killRecorded([{ pid, startTime: startTimeOf(pid) }]);
|
|
174
|
+
assert.equal(isRunning(pid), false);
|
|
175
|
+
assert.notDeepEqual(calls, []);
|
|
176
|
+
assert.deepEqual(
|
|
177
|
+
calls.filter(([target]) => target !== pid && target !== -pid),
|
|
178
|
+
[],
|
|
179
|
+
);
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
await test("killRecorded and stopRecorded leave alone a process whose start time is not the one recorded for its pid; a version that signals by pid alone kills it", async (t) => {
|
|
183
|
+
const child = spawn(process.execPath, ["-e", "setInterval(() => {}, 1000)"], {
|
|
184
|
+
stdio: "ignore",
|
|
185
|
+
});
|
|
186
|
+
t.after(() => child.kill());
|
|
187
|
+
const pid = child.pid;
|
|
188
|
+
assert.ok(pid !== undefined && pid > 1, "the child started");
|
|
189
|
+
const started = startTimeOf(pid);
|
|
190
|
+
assert.ok(started !== null, "the child has a start time");
|
|
191
|
+
const calls = recordKills(t);
|
|
192
|
+
const earlier = { pid, startTime: started - 1 };
|
|
193
|
+
await killRecorded([earlier]);
|
|
194
|
+
await stopRecorded([earlier, { pid, startedAt: "2026-01-01T00:00:00.000Z" }]);
|
|
195
|
+
assert.deepEqual(
|
|
196
|
+
calls.filter(([, signal]) => signal !== 0),
|
|
197
|
+
[],
|
|
198
|
+
);
|
|
199
|
+
assert.equal(isRunning(pid), true);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
await test(
|
|
203
|
+
"stopRecorded sends no SIGKILL to a pid another process took during the SIGTERM grace period; a version that kills every target it signalled kills that process",
|
|
204
|
+
{
|
|
205
|
+
skip:
|
|
206
|
+
!canChoosePid() &&
|
|
207
|
+
"choosing the next pid needs a PID namespace the test owns",
|
|
208
|
+
},
|
|
209
|
+
async (t) => {
|
|
210
|
+
const target = spawn(
|
|
211
|
+
process.execPath,
|
|
212
|
+
[
|
|
213
|
+
"-e",
|
|
214
|
+
'process.on("SIGTERM", () => {}); console.log("ready"); setInterval(() => {}, 1000);',
|
|
215
|
+
],
|
|
216
|
+
{ stdio: ["ignore", "pipe", "ignore"] },
|
|
217
|
+
);
|
|
218
|
+
t.after(() => target.kill("SIGKILL"));
|
|
219
|
+
const pid = target.pid;
|
|
220
|
+
assert.ok(pid !== undefined && pid > 1, "the target started");
|
|
221
|
+
await once(target.stdout, "data");
|
|
222
|
+
const record = { pid, startTime: startTimeOf(pid) };
|
|
223
|
+
/** @type {import("node:child_process").ChildProcess[]} */
|
|
224
|
+
const takers = [];
|
|
225
|
+
let taken = false;
|
|
226
|
+
target.once("exit", () => {
|
|
227
|
+
for (
|
|
228
|
+
let attempt = 0;
|
|
229
|
+
attempt < TAKE_ATTEMPTS && startTimeOf(pid) === null;
|
|
230
|
+
attempt += 1
|
|
231
|
+
) {
|
|
232
|
+
chooseNextPid(pid);
|
|
233
|
+
takers.push(spawn("sleep", ["60"], { stdio: "ignore" }));
|
|
234
|
+
}
|
|
235
|
+
taken = startTimeOf(pid) !== null;
|
|
236
|
+
});
|
|
237
|
+
t.after(() => takers.forEach((taker) => taker.kill("SIGKILL")));
|
|
238
|
+
const original = process.kill.bind(process);
|
|
239
|
+
/** @type {typeof process.kill} */
|
|
240
|
+
const probesOnly = (to, signal) =>
|
|
241
|
+
signal === 0 ? original(to, signal) : true;
|
|
242
|
+
const kill = t.mock.method(process, "kill", probesOnly);
|
|
243
|
+
const stopping = stopRecorded([record]);
|
|
244
|
+
await sleep(STOP_POLL_MS);
|
|
245
|
+
const exited = once(target, "exit");
|
|
246
|
+
target.kill("SIGKILL");
|
|
247
|
+
await exited;
|
|
248
|
+
await stopping;
|
|
249
|
+
assert.equal(taken, true, "another process took the pid");
|
|
250
|
+
const signals = kill.mock.calls
|
|
251
|
+
.map((call) => call.arguments[1])
|
|
252
|
+
.filter((signal) => signal !== 0);
|
|
253
|
+
assert.deepEqual(signals, ["SIGTERM"]);
|
|
254
|
+
},
|
|
255
|
+
);
|
|
@@ -15,7 +15,8 @@ import { targetOf } from "./commands-stack.mjs";
|
|
|
15
15
|
import { git } from "./git.mjs";
|
|
16
16
|
import { resolveRenderer } from "./renderer.mjs";
|
|
17
17
|
import { decodeSkillConfig } from "./verify/decode.mjs";
|
|
18
|
-
import {
|
|
18
|
+
import { layOutCopy } from "./run-copy.mjs";
|
|
19
|
+
import { cliPaths } from "./stack.mjs";
|
|
19
20
|
|
|
20
21
|
/**
|
|
21
22
|
* @param {import("node:test").TestContext} t The test
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cpSync,
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
readdirSync,
|
|
6
|
+
symlinkSync,
|
|
7
|
+
} from "node:fs";
|
|
8
|
+
import { basename, join, relative, sep } from "node:path";
|
|
9
|
+
import { WORKSPACE_FOLDER } from "./build-key.mjs";
|
|
10
|
+
|
|
11
|
+
const COPY_SKIPPED = new Set(["node_modules", ".antelope", WORKSPACE_FOLDER]);
|
|
12
|
+
const SHADOW_SKIPPED = new Set([".antelope", WORKSPACE_FOLDER, ".git"]);
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {string} real
|
|
16
|
+
* @param {string} shadow
|
|
17
|
+
* @param {string} keep
|
|
18
|
+
*/
|
|
19
|
+
function shadowLevel(real, shadow, keep) {
|
|
20
|
+
mkdirSync(shadow, { recursive: true });
|
|
21
|
+
for (const entry of readdirSync(real)) {
|
|
22
|
+
if (entry === keep || SHADOW_SKIPPED.has(entry)) continue;
|
|
23
|
+
symlinkSync(join(real, entry), join(shadow, entry));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* A `node_modules` that is `modules` with `@antelopejs/dms-frontend` replaced
|
|
29
|
+
* by the renderer checkout, every other entry linked to the original.
|
|
30
|
+
*
|
|
31
|
+
* @param {string} modules The playground's installed modules
|
|
32
|
+
* @param {string} shadow The run's `node_modules`
|
|
33
|
+
* @param {string} renderer The renderer checkout
|
|
34
|
+
*/
|
|
35
|
+
export function linkRenderer(modules, shadow, renderer) {
|
|
36
|
+
const scope = "@antelopejs";
|
|
37
|
+
shadowLevel(modules, shadow, scope);
|
|
38
|
+
const scoped = join(modules, scope);
|
|
39
|
+
const shadowScope = join(shadow, scope);
|
|
40
|
+
if (existsSync(scoped)) shadowLevel(scoped, shadowScope, "dms-frontend");
|
|
41
|
+
else mkdirSync(shadowScope);
|
|
42
|
+
symlinkSync(renderer, join(shadowScope, "dms-frontend"));
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Lays out the run's copy of the checkout: from the checkout root down to
|
|
47
|
+
* the playground, every sibling is linked, so relative paths such as a
|
|
48
|
+
* tsconfig `extends` resolve as in the checkout. The playground itself is
|
|
49
|
+
* copied without its installed modules and build, so `.antelope/` and the
|
|
50
|
+
* frontend build belong to the run. Its `node_modules` is the playground's,
|
|
51
|
+
* except that `@antelopejs/dms-frontend` is the `renderer` checkout when
|
|
52
|
+
* there is one.
|
|
53
|
+
*
|
|
54
|
+
* @param {string} playground The playground folder
|
|
55
|
+
* @param {string} checkout The checkout root, or the module folder outside one
|
|
56
|
+
* @param {string} root The run's copy of the checkout
|
|
57
|
+
* @param {string} [renderer] The renderer checkout the run serves
|
|
58
|
+
* @returns {string} The run's playground folder
|
|
59
|
+
*/
|
|
60
|
+
export function layOutCopy(playground, checkout, root, renderer) {
|
|
61
|
+
const segments = relative(checkout, playground).split(sep);
|
|
62
|
+
let real = checkout;
|
|
63
|
+
let shadow = root;
|
|
64
|
+
for (const segment of segments) {
|
|
65
|
+
shadowLevel(real, shadow, segment);
|
|
66
|
+
real = join(real, segment);
|
|
67
|
+
shadow = join(shadow, segment);
|
|
68
|
+
}
|
|
69
|
+
cpSync(playground, shadow, {
|
|
70
|
+
recursive: true,
|
|
71
|
+
filter: (source) => !COPY_SKIPPED.has(basename(source)),
|
|
72
|
+
});
|
|
73
|
+
const modules = join(playground, "node_modules");
|
|
74
|
+
if (renderer) linkRenderer(modules, join(shadow, "node_modules"), renderer);
|
|
75
|
+
else symlinkSync(modules, join(shadow, "node_modules"));
|
|
76
|
+
return shadow;
|
|
77
|
+
}
|
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
|
-
import {
|
|
2
|
+
import {
|
|
3
|
+
existsSync,
|
|
4
|
+
mkdirSync,
|
|
5
|
+
readdirSync,
|
|
6
|
+
readFileSync,
|
|
7
|
+
renameSync,
|
|
8
|
+
rmSync,
|
|
9
|
+
writeFileSync,
|
|
10
|
+
} from "node:fs";
|
|
3
11
|
import { tmpdir } from "node:os";
|
|
4
12
|
import { join, resolve } from "node:path";
|
|
13
|
+
import { describeFailure } from "./failure.mjs";
|
|
14
|
+
import { assertSameNamespaces } from "./identity.mjs";
|
|
5
15
|
import {
|
|
6
16
|
arrayOf,
|
|
7
17
|
decode,
|
|
@@ -19,10 +29,12 @@ const ID_BYTES = 3;
|
|
|
19
29
|
const TIMESTAMP_LENGTH = 15;
|
|
20
30
|
|
|
21
31
|
/**
|
|
22
|
-
* @typedef {{ name: string, pid: number, command: string, cwd: string, log: string, startedAt: string, port?: number }} ProcessRecord
|
|
32
|
+
* @typedef {{ name: string, pid: number, startTime?: number | null, command: string, cwd: string, log: string, startedAt: string, port?: number }} ProcessRecord A process the run started. `startTime` is when the kernel started it, in clock ticks since boot: `null` where /proc could not tell, absent in a record an earlier dms-control wrote. Without it, `startedAt` tells the process apart.
|
|
23
33
|
* @typedef {ProcessRecord & { port: number }} ServerRecord
|
|
34
|
+
* @typedef {ServerRecord & { database: string }} BackendRecord A backend the run started, with the database it serves
|
|
24
35
|
* @typedef {{ email: string, password: string, userId?: string | null }} Account
|
|
25
|
-
* @typedef {{ dir: string, root: string, home: string, logs: string, mongo: string, browser?: ServerRecord, evidence: string, cache: string, id: string, createdAt: string, environment: string, playground: string, moduleRoot: string, repo: string | null, headSha: string | null, renderer?: string, baseRef: string, skill: string | null, apiPort: number, webPort: number, smtpPort: number, mail: string, apiUrl: string, webUrl: string, databasePrefix: string, database: string, mongoUrl: string, ownsMongo: boolean, secrets: Record<string, string>, adminPassword: string, extraEnv: Record<string, string>, processes: ProcessRecord[], accounts: Record<string, Account>, tokens: Record<string, string>, ready: boolean, error?: string, runPlayground?: string, baseRoot?: string, resetCount?: number, journeyCount?: number }} RunState
|
|
36
|
+
* @typedef {{ dir: string, root: string, home: string, logs: string, mongo: string, browser?: ServerRecord, evidence: string, cache: string, id: string, createdAt: string, environment: string, playground: string, moduleRoot: string, repo: string | null, headSha: string | null, renderer?: string, baseRef: string, skill: string | null, apiPort: number, webPort: number, smtpPort: number, mail: string, apiUrl: string, webUrl: string, databasePrefix: string, database: string, mongoUrl: string, ownsMongo: boolean, secrets: Record<string, string>, adminPassword: string, extraEnv: Record<string, string>, processes: ProcessRecord[], namespaces?: import("./identity.mjs").Namespaces, noStandby?: boolean, buildRoots?: string[], accounts: Record<string, Account>, tokens: Record<string, string>, ready: boolean, error?: string, runPlayground?: string, baseRoot?: string, resetCount?: number, journeyCount?: number, frontendBuild?: FrontendBuild, bakedEnv?: Record<string, string> }} RunState
|
|
37
|
+
* @typedef {{ key: string | null, cached: boolean, reason?: string }} FrontendBuild The key of the inputs of the run's frontend build (null when it was not keyed), whether the build came from the build cache, and why the cache was not used or not filled when it should have been
|
|
26
38
|
*/
|
|
27
39
|
|
|
28
40
|
/**
|
|
@@ -102,28 +114,32 @@ export function createRunFolders(dir) {
|
|
|
102
114
|
|
|
103
115
|
const isStrings = recordOf(isString);
|
|
104
116
|
|
|
105
|
-
const
|
|
106
|
-
{
|
|
107
|
-
name: isString,
|
|
108
|
-
pid: isNumber,
|
|
109
|
-
command: isString,
|
|
110
|
-
cwd: isString,
|
|
111
|
-
log: isString,
|
|
112
|
-
startedAt: isString,
|
|
113
|
-
},
|
|
114
|
-
{ port: isNumber },
|
|
115
|
-
);
|
|
116
|
-
|
|
117
|
-
const isServerRecord = objectOf({
|
|
117
|
+
const PROCESS_FIELDS = {
|
|
118
118
|
name: isString,
|
|
119
119
|
pid: isNumber,
|
|
120
120
|
command: isString,
|
|
121
121
|
cwd: isString,
|
|
122
122
|
log: isString,
|
|
123
123
|
startedAt: isString,
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const START_TIME = { startTime: nullable(isNumber) };
|
|
127
|
+
|
|
128
|
+
const isProcessRecord = objectOf(PROCESS_FIELDS, {
|
|
124
129
|
port: isNumber,
|
|
130
|
+
...START_TIME,
|
|
125
131
|
});
|
|
126
132
|
|
|
133
|
+
const isServerRecord = objectOf(
|
|
134
|
+
{ ...PROCESS_FIELDS, port: isNumber },
|
|
135
|
+
START_TIME,
|
|
136
|
+
);
|
|
137
|
+
|
|
138
|
+
const isBackendRecord = objectOf(
|
|
139
|
+
{ ...PROCESS_FIELDS, port: isNumber, database: isString },
|
|
140
|
+
START_TIME,
|
|
141
|
+
);
|
|
142
|
+
|
|
127
143
|
const isAccount = objectOf(
|
|
128
144
|
{ email: isString },
|
|
129
145
|
{ password: isString, userId: nullable(isString) },
|
|
@@ -168,12 +184,20 @@ const isStoredRun = objectOf(
|
|
|
168
184
|
},
|
|
169
185
|
{
|
|
170
186
|
browser: either(isString, isServerRecord),
|
|
187
|
+
namespaces: objectOf({}, { pid: isString, time: isString }),
|
|
188
|
+
noStandby: isBoolean,
|
|
189
|
+
buildRoots: arrayOf(isString),
|
|
171
190
|
error: isString,
|
|
172
191
|
renderer: isString,
|
|
173
192
|
runPlayground: isString,
|
|
174
193
|
baseRoot: isString,
|
|
175
194
|
resetCount: isNumber,
|
|
176
195
|
journeyCount: isNumber,
|
|
196
|
+
frontendBuild: objectOf(
|
|
197
|
+
{ key: nullable(isString), cached: isBoolean },
|
|
198
|
+
{ reason: isString },
|
|
199
|
+
),
|
|
200
|
+
bakedEnv: isStrings,
|
|
177
201
|
},
|
|
178
202
|
);
|
|
179
203
|
|
|
@@ -181,10 +205,14 @@ const isStoredRun = objectOf(
|
|
|
181
205
|
* Reads the state `up` recorded for a run. A folder an earlier release stored
|
|
182
206
|
* in `browser` is dropped, since nothing creates or reads that folder. An
|
|
183
207
|
* account that `down` left without its `password` reads with an empty one.
|
|
208
|
+
* Every command opens its run here, so a command run outside the PID or
|
|
209
|
+
* time namespace `up` ran in refuses before it reads a process or changes
|
|
210
|
+
* anything.
|
|
184
211
|
*
|
|
185
212
|
* @param {string} dir The run root, as `up` printed it
|
|
186
213
|
* @returns {RunState} The run state
|
|
187
214
|
* @throws When the folder holds no run.json
|
|
215
|
+
* @throws When the run was started in another PID or time namespace
|
|
188
216
|
*/
|
|
189
217
|
export function loadRun(dir) {
|
|
190
218
|
const file = join(resolve(dir), RUN_FILE);
|
|
@@ -196,6 +224,7 @@ export function loadRun(dir) {
|
|
|
196
224
|
/** @type {unknown} */
|
|
197
225
|
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
198
226
|
const stored = decode(isStoredRun, parsed, file, "a run state");
|
|
227
|
+
assertSameNamespaces(stored.namespaces);
|
|
199
228
|
const { browser, accounts, ...rest } = stored;
|
|
200
229
|
const run = {
|
|
201
230
|
...rest,
|
|
@@ -222,3 +251,100 @@ export function saveRun(state) {
|
|
|
222
251
|
{ mode: 0o600 },
|
|
223
252
|
);
|
|
224
253
|
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* The folder that holds one file per backend the run started.
|
|
257
|
+
*
|
|
258
|
+
* @param {string} dir The run root
|
|
259
|
+
* @returns {string} The folder
|
|
260
|
+
*/
|
|
261
|
+
export function backendsFolder(dir) {
|
|
262
|
+
return join(dir, "backends");
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Records a backend the run started in a file of its own,
|
|
267
|
+
* `backends/<pid>.json`. No other write of the run replaces that file, so
|
|
268
|
+
* `down` finds the backend even after a command saved a run.json it had
|
|
269
|
+
* loaded before the backend started.
|
|
270
|
+
*
|
|
271
|
+
* @param {string} dir The run root
|
|
272
|
+
* @param {BackendRecord} backend The backend
|
|
273
|
+
*/
|
|
274
|
+
export function recordBackend(dir, backend) {
|
|
275
|
+
const folder = backendsFolder(dir);
|
|
276
|
+
mkdirSync(folder, { recursive: true });
|
|
277
|
+
const file = join(folder, `${backend.pid}.json`);
|
|
278
|
+
writeFileSync(`${file}.next`, `${JSON.stringify(backend)}\n`);
|
|
279
|
+
renameSync(`${file}.next`, file);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* @param {unknown} error
|
|
284
|
+
* @returns {boolean}
|
|
285
|
+
*/
|
|
286
|
+
function isMissing(error) {
|
|
287
|
+
return error instanceof Error && "code" in error && error.code === "ENOENT";
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* @param {string} folder
|
|
292
|
+
* @returns {string[]}
|
|
293
|
+
*/
|
|
294
|
+
function namesIn(folder) {
|
|
295
|
+
try {
|
|
296
|
+
return readdirSync(folder);
|
|
297
|
+
} catch (error) {
|
|
298
|
+
if (isMissing(error)) return [];
|
|
299
|
+
throw error;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** @type {Set<string>} The records already named on stderr, so a command names each once. */
|
|
304
|
+
const reported = new Set();
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* @param {string} file
|
|
308
|
+
* @returns {BackendRecord[]}
|
|
309
|
+
*/
|
|
310
|
+
function readBackend(file) {
|
|
311
|
+
try {
|
|
312
|
+
/** @type {unknown} */
|
|
313
|
+
const parsed = JSON.parse(readFileSync(file, "utf8"));
|
|
314
|
+
return [decode(isBackendRecord, parsed, file, "a backend record")];
|
|
315
|
+
} catch (error) {
|
|
316
|
+
if (!isMissing(error) && !reported.has(file)) {
|
|
317
|
+
reported.add(file);
|
|
318
|
+
process.stderr.write(
|
|
319
|
+
`dms-control: skipped ${file}, which holds no backend record: ${describeFailure(error)}\n`,
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
return [];
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Every backend the run recorded, whether or not it still runs. A record
|
|
328
|
+
* removed while the folder is read is gone. A file that holds no backend
|
|
329
|
+
* record, such as a truncated one, is skipped and named on stderr, so it
|
|
330
|
+
* stops no command.
|
|
331
|
+
*
|
|
332
|
+
* @param {string} dir The run root
|
|
333
|
+
* @returns {BackendRecord[]} The backends
|
|
334
|
+
*/
|
|
335
|
+
export function recordedBackends(dir) {
|
|
336
|
+
const folder = backendsFolder(dir);
|
|
337
|
+
return namesIn(folder)
|
|
338
|
+
.filter((name) => name.endsWith(".json"))
|
|
339
|
+
.flatMap((name) => readBackend(join(folder, name)));
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Removes the record of a backend that no longer runs.
|
|
344
|
+
*
|
|
345
|
+
* @param {string} dir The run root
|
|
346
|
+
* @param {number} pid Its process id
|
|
347
|
+
*/
|
|
348
|
+
export function forgetBackend(dir, pid) {
|
|
349
|
+
rmSync(join(backendsFolder(dir), `${pid}.json`), { force: true });
|
|
350
|
+
}
|