@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,394 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
copyFileSync,
|
|
4
|
+
existsSync,
|
|
5
|
+
lstatSync,
|
|
6
|
+
mkdirSync,
|
|
7
|
+
readdirSync,
|
|
8
|
+
readlinkSync,
|
|
9
|
+
renameSync,
|
|
10
|
+
rmSync,
|
|
11
|
+
statSync,
|
|
12
|
+
symlinkSync,
|
|
13
|
+
utimesSync,
|
|
14
|
+
writeFileSync,
|
|
15
|
+
} from "node:fs";
|
|
16
|
+
import { homedir } from "node:os";
|
|
17
|
+
import { isAbsolute, join } from "node:path";
|
|
18
|
+
import {
|
|
19
|
+
bakedVariables,
|
|
20
|
+
buildInputs,
|
|
21
|
+
buildKey,
|
|
22
|
+
sourceNames,
|
|
23
|
+
WORKSPACE_FOLDER,
|
|
24
|
+
} from "./build-key.mjs";
|
|
25
|
+
import { describeFailure } from "./failure.mjs";
|
|
26
|
+
import { PREPARED_WORKSPACE_REFUSED } from "./frontend-workspace.mjs";
|
|
27
|
+
import { ExitError } from "./processes.mjs";
|
|
28
|
+
|
|
29
|
+
const READY_MARK = ".dms-control-ready";
|
|
30
|
+
const OUTPUTS = "outputs";
|
|
31
|
+
const INPUTS = "inputs.json";
|
|
32
|
+
const PENDING_PREFIX = ".pending-";
|
|
33
|
+
const PENDING_AGE_MS = 3_600_000;
|
|
34
|
+
const SUFFIX_BYTES = 4;
|
|
35
|
+
|
|
36
|
+
/** How many frontend builds the cache keeps, the most recently used first. */
|
|
37
|
+
export const KEPT_BUILDS = 5;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* @typedef {import("./build-key.mjs").KeyedRun & Pick<import("./run-state.mjs").RunState, "runPlayground" | "frontendBuild">} BuildingRun The run whose frontend build is laid out
|
|
41
|
+
* @typedef {{ prepare: (workspace: string) => Promise<void>, build: (prepared: boolean) => Promise<void> }} FrontendSteps How a run sets its frontend workspace up (the setup and install `ajs dms build` runs first) and builds: in that prepared workspace, or with `ajs dms build` as it is
|
|
42
|
+
* @typedef {{ prepared: import("./build-key.mjs").PreparedWorkspace, inputs: import("./build-key.mjs").BuildInputs, key: string }} PreparedBuild A prepared workspace, what it hashes and the key
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Where frontend builds are cached: `dms-control/frontend` under
|
|
47
|
+
* `XDG_CACHE_HOME`, else `~/.cache`.
|
|
48
|
+
*
|
|
49
|
+
* @returns {string} The cache folder
|
|
50
|
+
*/
|
|
51
|
+
function frontendCache() {
|
|
52
|
+
const cacheHome = process.env.XDG_CACHE_HOME ?? join(homedir(), ".cache");
|
|
53
|
+
return join(cacheHome, "dms-control", "frontend");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Recreates `source` at `target`: folders and relative symbolic links anew,
|
|
58
|
+
* files copied, so a later write to either side leaves the other unchanged.
|
|
59
|
+
*
|
|
60
|
+
* @param {string} source The entry to recreate
|
|
61
|
+
* @param {string} target Where, which must not exist
|
|
62
|
+
* @throws On an absolute symbolic link, which would point into the run that built it, or when `target` exists
|
|
63
|
+
*/
|
|
64
|
+
function copyEntry(source, target) {
|
|
65
|
+
const stat = lstatSync(source);
|
|
66
|
+
if (stat.isSymbolicLink()) {
|
|
67
|
+
const link = readlinkSync(source);
|
|
68
|
+
if (isAbsolute(link)) throw new Error(`${source} links to ${link}`);
|
|
69
|
+
symlinkSync(link, target);
|
|
70
|
+
} else if (stat.isDirectory()) {
|
|
71
|
+
mkdirSync(target);
|
|
72
|
+
for (const name of readdirSync(source)) {
|
|
73
|
+
copyEntry(join(source, name), join(target, name));
|
|
74
|
+
}
|
|
75
|
+
} else {
|
|
76
|
+
if (existsSync(target)) throw new Error(`${target} exists`);
|
|
77
|
+
copyFileSync(source, target);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** @param {string} path */
|
|
82
|
+
function remove(path) {
|
|
83
|
+
try {
|
|
84
|
+
rmSync(path, { recursive: true, force: true });
|
|
85
|
+
} catch {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Marks an entry the most recently used. An entry another user owns, or one
|
|
92
|
+
* a prune removed meanwhile, stays as it is.
|
|
93
|
+
*
|
|
94
|
+
* @param {string} entry The entry
|
|
95
|
+
*/
|
|
96
|
+
function markUsed(entry) {
|
|
97
|
+
const now = new Date();
|
|
98
|
+
try {
|
|
99
|
+
utimesSync(join(entry, READY_MARK), now, now);
|
|
100
|
+
} catch {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Lays the output of the cached build of `key` into `workspace`, beside the
|
|
107
|
+
* sources its setup wrote, and marks the build the most recently used. A
|
|
108
|
+
* partial layout is removed. Never throws: a restore that fails, because
|
|
109
|
+
* the entry is incomplete or pruned while it is read, reads as a miss.
|
|
110
|
+
*
|
|
111
|
+
* @param {string} cache The cache folder
|
|
112
|
+
* @param {string} key The build key
|
|
113
|
+
* @param {string} workspace The run's prepared workspace
|
|
114
|
+
* @returns {boolean} Whether the cache held the build and it was laid out
|
|
115
|
+
*/
|
|
116
|
+
export function restoreBuild(cache, key, workspace) {
|
|
117
|
+
const entry = join(cache, key);
|
|
118
|
+
/** @type {string[]} */
|
|
119
|
+
const restored = [];
|
|
120
|
+
try {
|
|
121
|
+
if (!existsSync(join(entry, READY_MARK))) return false;
|
|
122
|
+
for (const name of readdirSync(join(entry, OUTPUTS))) {
|
|
123
|
+
if (existsSync(join(workspace, name))) {
|
|
124
|
+
throw new Error(`${name} exists`);
|
|
125
|
+
}
|
|
126
|
+
restored.push(name);
|
|
127
|
+
copyEntry(join(entry, OUTPUTS, name), join(workspace, name));
|
|
128
|
+
}
|
|
129
|
+
} catch {
|
|
130
|
+
for (const name of restored) remove(join(workspace, name));
|
|
131
|
+
return false;
|
|
132
|
+
}
|
|
133
|
+
markUsed(entry);
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* @param {string} path
|
|
139
|
+
* @returns {number}
|
|
140
|
+
*/
|
|
141
|
+
function modifiedAt(path) {
|
|
142
|
+
try {
|
|
143
|
+
return statSync(path, { throwIfNoEntry: false })?.mtimeMs ?? 0;
|
|
144
|
+
} catch {
|
|
145
|
+
return 0;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* @param {string} cache
|
|
151
|
+
* @param {string} name
|
|
152
|
+
*/
|
|
153
|
+
function discard(cache, name) {
|
|
154
|
+
const doomed = join(
|
|
155
|
+
cache,
|
|
156
|
+
`${PENDING_PREFIX}${name}-${randomBytes(SUFFIX_BYTES).toString("hex")}`,
|
|
157
|
+
);
|
|
158
|
+
try {
|
|
159
|
+
renameSync(join(cache, name), doomed);
|
|
160
|
+
} catch {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
remove(doomed);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Keeps the `limit` most recently used builds and removes the rest, and any
|
|
168
|
+
* store left pending for over an hour. A build being restored while it is
|
|
169
|
+
* removed fails its restore, which then builds instead. A cache that cannot
|
|
170
|
+
* be read or written is left as it is.
|
|
171
|
+
*
|
|
172
|
+
* @param {string} cache The cache folder
|
|
173
|
+
* @param {number} limit How many builds to keep
|
|
174
|
+
*/
|
|
175
|
+
function pruneBuilds(cache, limit) {
|
|
176
|
+
/** @type {string[]} */
|
|
177
|
+
let names = [];
|
|
178
|
+
try {
|
|
179
|
+
names = readdirSync(cache);
|
|
180
|
+
} catch {
|
|
181
|
+
return;
|
|
182
|
+
}
|
|
183
|
+
const pending = names.filter((name) => name.startsWith(PENDING_PREFIX));
|
|
184
|
+
const stale = pending.filter(
|
|
185
|
+
(name) => Date.now() - modifiedAt(join(cache, name)) > PENDING_AGE_MS,
|
|
186
|
+
);
|
|
187
|
+
const unused = names
|
|
188
|
+
.filter((name) => !name.startsWith(PENDING_PREFIX))
|
|
189
|
+
.map((name) => ({ name, used: modifiedAt(join(cache, name, READY_MARK)) }))
|
|
190
|
+
.sort((left, right) => right.used - left.used)
|
|
191
|
+
.slice(limit)
|
|
192
|
+
.map(({ name }) => name);
|
|
193
|
+
for (const name of [...stale, ...unused]) discard(cache, name);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* @param {string} cache
|
|
198
|
+
* @param {string} key
|
|
199
|
+
* @param {unknown} inputs
|
|
200
|
+
* @param {{ workspace: string, outputs: string[] }} build
|
|
201
|
+
* @param {string} pending
|
|
202
|
+
*/
|
|
203
|
+
function placeBuild(cache, key, inputs, build, pending) {
|
|
204
|
+
mkdirSync(join(pending, OUTPUTS), { recursive: true });
|
|
205
|
+
for (const name of build.outputs) {
|
|
206
|
+
copyEntry(join(build.workspace, name), join(pending, OUTPUTS, name));
|
|
207
|
+
}
|
|
208
|
+
writeFileSync(join(pending, INPUTS), `${JSON.stringify(inputs, null, 2)}\n`);
|
|
209
|
+
writeFileSync(join(pending, READY_MARK), `${key}\n`);
|
|
210
|
+
if (existsSync(join(cache, key))) return;
|
|
211
|
+
try {
|
|
212
|
+
renameSync(pending, join(cache, key));
|
|
213
|
+
} catch (error) {
|
|
214
|
+
if (!existsSync(join(cache, key, READY_MARK))) throw error;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Stores the output of the build in `workspace` (its `outputs` entries) as
|
|
220
|
+
* the build of `key`: laid out in a pending folder, then renamed into place,
|
|
221
|
+
* so a reader sees all of it or none. A build already stored under `key`
|
|
222
|
+
* stays. Then prunes to {@link KEPT_BUILDS}. Never throws: a cache that
|
|
223
|
+
* cannot be written leaves the run as it is.
|
|
224
|
+
*
|
|
225
|
+
* @param {string} cache The cache folder
|
|
226
|
+
* @param {string} key The build key
|
|
227
|
+
* @param {unknown} inputs What the key hashes, written beside the build
|
|
228
|
+
* @param {{ workspace: string, outputs: string[] }} build The run's workspace and the entries the build wrote
|
|
229
|
+
* @returns {string | null} Why the build is not in the cache, or null once it is
|
|
230
|
+
*/
|
|
231
|
+
export function storeBuild(cache, key, inputs, build) {
|
|
232
|
+
const pending = join(
|
|
233
|
+
cache,
|
|
234
|
+
`${PENDING_PREFIX}${key}-${randomBytes(SUFFIX_BYTES).toString("hex")}`,
|
|
235
|
+
);
|
|
236
|
+
/** @type {string | null} */
|
|
237
|
+
let failure = null;
|
|
238
|
+
try {
|
|
239
|
+
mkdirSync(cache, { recursive: true });
|
|
240
|
+
placeBuild(cache, key, inputs, build, pending);
|
|
241
|
+
} catch (error) {
|
|
242
|
+
failure = describeFailure(error);
|
|
243
|
+
}
|
|
244
|
+
remove(pending);
|
|
245
|
+
pruneBuilds(cache, KEPT_BUILDS);
|
|
246
|
+
return failure;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* @param {BuildingRun} state
|
|
251
|
+
* @param {string} frontendPackage
|
|
252
|
+
* @param {FrontendSteps} steps
|
|
253
|
+
* @param {string} workspace
|
|
254
|
+
* @returns {Promise<PreparedBuild | { reason: string }>}
|
|
255
|
+
*/
|
|
256
|
+
async function prepareBuild(state, frontendPackage, steps, workspace) {
|
|
257
|
+
try {
|
|
258
|
+
await steps.prepare(workspace);
|
|
259
|
+
} catch (error) {
|
|
260
|
+
return {
|
|
261
|
+
reason: `its workspace could not be prepared (${describeFailure(error)})`,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
try {
|
|
265
|
+
/** @type {import("./build-key.mjs").PreparedWorkspace} */
|
|
266
|
+
const prepared = {
|
|
267
|
+
project: state.runPlayground ?? "",
|
|
268
|
+
workspace,
|
|
269
|
+
names: sourceNames(workspace),
|
|
270
|
+
installed: workspace,
|
|
271
|
+
};
|
|
272
|
+
const inputs = buildInputs(state, frontendPackage, prepared);
|
|
273
|
+
return { prepared, inputs, key: buildKey(inputs) };
|
|
274
|
+
} catch (error) {
|
|
275
|
+
return {
|
|
276
|
+
reason: `its inputs could not be keyed (${describeFailure(error)})`,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* @param {BuildingRun} state
|
|
283
|
+
* @param {string} frontendPackage
|
|
284
|
+
* @param {PreparedBuild} build
|
|
285
|
+
* @returns {import("./run-state.mjs").FrontendBuild}
|
|
286
|
+
*/
|
|
287
|
+
function storeConsumed(state, frontendPackage, build) {
|
|
288
|
+
const { prepared, inputs, key } = build;
|
|
289
|
+
const cache = frontendCache();
|
|
290
|
+
try {
|
|
291
|
+
const consumed = buildKey(buildInputs(state, frontendPackage, prepared));
|
|
292
|
+
if (consumed !== key) {
|
|
293
|
+
return {
|
|
294
|
+
key: null,
|
|
295
|
+
cached: false,
|
|
296
|
+
reason: "its workspace sources changed while it built",
|
|
297
|
+
};
|
|
298
|
+
}
|
|
299
|
+
const outputs = sourceNames(prepared.workspace).filter(
|
|
300
|
+
(name) => !prepared.names.includes(name),
|
|
301
|
+
);
|
|
302
|
+
const failure = storeBuild(cache, key, inputs, {
|
|
303
|
+
workspace: prepared.workspace,
|
|
304
|
+
outputs,
|
|
305
|
+
});
|
|
306
|
+
if (failure === null) return { key, cached: false };
|
|
307
|
+
return { key, cached: false, reason: `${cache} refused it (${failure})` };
|
|
308
|
+
} catch (error) {
|
|
309
|
+
return {
|
|
310
|
+
key: null,
|
|
311
|
+
cached: false,
|
|
312
|
+
reason: `its workspace could not be read (${describeFailure(error)})`,
|
|
313
|
+
};
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* @param {unknown} error
|
|
319
|
+
* @returns {error is ExitError}
|
|
320
|
+
*/
|
|
321
|
+
function refusedPreparedWorkspace(error) {
|
|
322
|
+
return (
|
|
323
|
+
error instanceof ExitError && error.exitCode === PREPARED_WORKSPACE_REFUSED
|
|
324
|
+
);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* @param {BuildingRun} state
|
|
329
|
+
* @param {FrontendSteps} steps
|
|
330
|
+
* @param {string} workspace
|
|
331
|
+
* @param {string} reason
|
|
332
|
+
* @returns {Promise<void>}
|
|
333
|
+
*/
|
|
334
|
+
async function buildAsItIs(state, steps, workspace, reason) {
|
|
335
|
+
remove(workspace);
|
|
336
|
+
await steps.build(false);
|
|
337
|
+
state.frontendBuild = { key: null, cached: false, reason };
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Lays out the run's `.dms-frontend`. With `useCache`, records the `VITE_*`
|
|
342
|
+
* and `NODE_ENV` variables it keys on in `state.bakedEnv`, sets the
|
|
343
|
+
* workspace up (`steps.prepare`, the setup and install `ajs dms build` runs
|
|
344
|
+
* before it builds) and keys the build on what that workspace holds (see
|
|
345
|
+
* `buildInputs`). The cache's build of that key is laid into it; else
|
|
346
|
+
* `steps.build(true)` builds in that same workspace, without setting it up
|
|
347
|
+
* again, so the build reads the sources the key hashed and nothing read
|
|
348
|
+
* from the checkout later. The build is stored when the workspace's sources
|
|
349
|
+
* still hash to the key once it is done. Without `useCache`, when the
|
|
350
|
+
* workspace cannot be prepared, or when the build refuses it (it exits with
|
|
351
|
+
* `PREPARED_WORKSPACE_REFUSED`), or when its inputs cannot all be keyed
|
|
352
|
+
* (see `buildInputs`), `steps.build(false)` runs `ajs dms build` as it is
|
|
353
|
+
* and nothing is stored; any other failure of the build fails the
|
|
354
|
+
* layout. Records the key, where the build came from and why the cache was
|
|
355
|
+
* not used or filled in `state.frontendBuild`.
|
|
356
|
+
*
|
|
357
|
+
* @param {BuildingRun} state The run, after its project build
|
|
358
|
+
* @param {string} frontendPackage The `@antelopejs/dms-frontend` folder that builds
|
|
359
|
+
* @param {FrontendSteps} steps How the run prepares and builds
|
|
360
|
+
* @param {boolean} useCache Whether to read and write the cache
|
|
361
|
+
* @returns {Promise<void>} Resolves once the build is laid out
|
|
362
|
+
*/
|
|
363
|
+
export async function layOutFrontendBuild(
|
|
364
|
+
state,
|
|
365
|
+
frontendPackage,
|
|
366
|
+
steps,
|
|
367
|
+
useCache,
|
|
368
|
+
) {
|
|
369
|
+
const workspace = join(state.runPlayground ?? "", WORKSPACE_FOLDER);
|
|
370
|
+
if (!useCache) {
|
|
371
|
+
await steps.build(false);
|
|
372
|
+
state.frontendBuild = { key: null, cached: false };
|
|
373
|
+
return;
|
|
374
|
+
}
|
|
375
|
+
state.bakedEnv = bakedVariables(process.env);
|
|
376
|
+
const build = await prepareBuild(state, frontendPackage, steps, workspace);
|
|
377
|
+
if ("reason" in build) {
|
|
378
|
+
await buildAsItIs(state, steps, workspace, build.reason);
|
|
379
|
+
return;
|
|
380
|
+
}
|
|
381
|
+
if (restoreBuild(frontendCache(), build.key, workspace)) {
|
|
382
|
+
state.frontendBuild = { key: build.key, cached: true };
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
try {
|
|
386
|
+
await steps.build(true);
|
|
387
|
+
} catch (error) {
|
|
388
|
+
if (!refusedPreparedWorkspace(error)) throw error;
|
|
389
|
+
const reason = `the build refused its prepared workspace (${error.message})`;
|
|
390
|
+
await buildAsItIs(state, steps, workspace, reason);
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
state.frontendBuild = storeConsumed(state, frontendPackage, build);
|
|
394
|
+
}
|
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { execFile } from "node:child_process";
|
|
3
|
+
import {
|
|
4
|
+
existsSync,
|
|
5
|
+
mkdirSync,
|
|
6
|
+
mkdtempSync,
|
|
7
|
+
rmSync,
|
|
8
|
+
symlinkSync,
|
|
9
|
+
utimesSync,
|
|
10
|
+
writeFileSync,
|
|
11
|
+
} from "node:fs";
|
|
12
|
+
import { tmpdir } from "node:os";
|
|
13
|
+
import { join } from "node:path";
|
|
14
|
+
import test from "node:test";
|
|
15
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
16
|
+
import { isDeepStrictEqual, promisify } from "node:util";
|
|
17
|
+
import { KEPT_BUILDS, restoreBuild, storeBuild } from "./build-cache.mjs";
|
|
18
|
+
import { buildChangedSince, existingBuildRoots } from "./build-inputs.mjs";
|
|
19
|
+
import { write } from "./helpers/repo.mjs";
|
|
20
|
+
import { namesIn, treeOf } from "./helpers/tree.mjs";
|
|
21
|
+
|
|
22
|
+
const exec = promisify(execFile);
|
|
23
|
+
const FILES_PER_STORE = 400;
|
|
24
|
+
const START_DELAY_MS = 500;
|
|
25
|
+
const UNTOUCHABLE = "/proc/version";
|
|
26
|
+
const CLOCK_STEP_MS = 20;
|
|
27
|
+
|
|
28
|
+
const SOURCES = { "main.ts": "mount();\n" };
|
|
29
|
+
const BUILT = {
|
|
30
|
+
"dist/client/index.html": "<html>one</html>\n",
|
|
31
|
+
"dist/server.mjs": "serve();\n",
|
|
32
|
+
"dist/current": "-> client",
|
|
33
|
+
"components.d.ts": "declare const a: 1;\n",
|
|
34
|
+
};
|
|
35
|
+
const OUTPUTS = ["components.d.ts", "dist"];
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* A cache folder and a run's built workspace: the sources its setup wrote
|
|
39
|
+
* and what the build added, a relative link among them as Vite writes none
|
|
40
|
+
* but pnpm does.
|
|
41
|
+
*
|
|
42
|
+
* @param {import("node:test").TestContext} t The test
|
|
43
|
+
*/
|
|
44
|
+
function fixture(t) {
|
|
45
|
+
const dir = mkdtempSync(join(tmpdir(), "dms-control-build-cache-test-"));
|
|
46
|
+
t.after(() => rmSync(dir, { recursive: true, force: true }));
|
|
47
|
+
const built = join(dir, "built");
|
|
48
|
+
write(join(built, "main.ts"), SOURCES["main.ts"]);
|
|
49
|
+
write(
|
|
50
|
+
join(built, "dist", "client", "index.html"),
|
|
51
|
+
BUILT["dist/client/index.html"],
|
|
52
|
+
);
|
|
53
|
+
write(join(built, "dist", "server.mjs"), BUILT["dist/server.mjs"]);
|
|
54
|
+
write(join(built, "components.d.ts"), BUILT["components.d.ts"]);
|
|
55
|
+
symlinkSync("client", join(built, "dist", "current"));
|
|
56
|
+
/** @param {string} name */
|
|
57
|
+
const prepared = (name) => {
|
|
58
|
+
const workspace = join(dir, name);
|
|
59
|
+
write(join(workspace, "main.ts"), SOURCES["main.ts"]);
|
|
60
|
+
return workspace;
|
|
61
|
+
};
|
|
62
|
+
return {
|
|
63
|
+
dir,
|
|
64
|
+
cache: join(dir, "cache"),
|
|
65
|
+
build: { workspace: built, outputs: OUTPUTS },
|
|
66
|
+
prepared,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
await test("a stored build restores beside another run's sources, and outlives the run that built it", (t) => {
|
|
71
|
+
const { cache, build, prepared } = fixture(t);
|
|
72
|
+
assert.equal(storeBuild(cache, "k1", { inputs: 1 }, build), null);
|
|
73
|
+
rmSync(build.workspace, { recursive: true });
|
|
74
|
+
const workspace = prepared("next");
|
|
75
|
+
assert.equal(restoreBuild(cache, "k1", workspace), true);
|
|
76
|
+
assert.deepEqual(
|
|
77
|
+
treeOf(workspace),
|
|
78
|
+
{ ...SOURCES, ...BUILT },
|
|
79
|
+
"a link resolved into a copy, a link into the cache, or the builder's sources in place of the run's, serve another build",
|
|
80
|
+
);
|
|
81
|
+
assert.equal(
|
|
82
|
+
restoreBuild(cache, "k1", prepared("again")),
|
|
83
|
+
true,
|
|
84
|
+
"restoring by moving the entry leaves nothing for the next run",
|
|
85
|
+
);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
await test("a restore into a run's workspace is no change to the build its backends load; a rebuild after it still is", async (t) => {
|
|
89
|
+
const { dir, cache, build } = fixture(t);
|
|
90
|
+
assert.equal(storeBuild(cache, "k1", {}, build), null);
|
|
91
|
+
const moduleRoot = join(dir, "module");
|
|
92
|
+
const playground = join(moduleRoot, "playground");
|
|
93
|
+
write(join(moduleRoot, "dist", "index.js"), "serve();\n");
|
|
94
|
+
mkdirSync(join(playground, "node_modules"), { recursive: true });
|
|
95
|
+
const runPlayground = join(dir, "run", "playground");
|
|
96
|
+
write(join(runPlayground, "dist", "index.js"), "project();\n");
|
|
97
|
+
write(join(runPlayground, ".antelope", "build", "build.json"), "{}\n");
|
|
98
|
+
const workspace = join(runPlayground, ".dms-frontend");
|
|
99
|
+
write(join(workspace, "main.ts"), SOURCES["main.ts"]);
|
|
100
|
+
const run = { moduleRoot, playground, runPlayground, buildRoots: [""] };
|
|
101
|
+
run.buildRoots = existingBuildRoots(run);
|
|
102
|
+
await sleep(CLOCK_STEP_MS);
|
|
103
|
+
const standbyStartedAt = new Date().toISOString();
|
|
104
|
+
await sleep(CLOCK_STEP_MS);
|
|
105
|
+
assert.equal(restoreBuild(cache, "k1", workspace), true);
|
|
106
|
+
assert.equal(
|
|
107
|
+
buildChangedSince(run, standbyStartedAt),
|
|
108
|
+
null,
|
|
109
|
+
"a staleness check that reads the run's frontend workspace takes every restore for a rebuild, and no standby is ever promoted",
|
|
110
|
+
);
|
|
111
|
+
write(join(moduleRoot, "dist", "index.js"), "rebuilt();\n");
|
|
112
|
+
assert.equal(
|
|
113
|
+
buildChangedSince(run, standbyStartedAt),
|
|
114
|
+
`${join(moduleRoot, "dist", "index.js")} changed`,
|
|
115
|
+
"a hit that resets what the check compares against hides the module's rebuild, and the standby serves the old code",
|
|
116
|
+
);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
await test("only a complete entry restores, and a second store of a key keeps the first", (t) => {
|
|
120
|
+
const { cache, build, prepared } = fixture(t);
|
|
121
|
+
mkdirSync(join(cache, "partial", "outputs", "dist"), { recursive: true });
|
|
122
|
+
const workspace = prepared("next");
|
|
123
|
+
assert.equal(
|
|
124
|
+
restoreBuild(cache, "partial", workspace),
|
|
125
|
+
false,
|
|
126
|
+
"an entry without its ready mark is a store that did not finish",
|
|
127
|
+
);
|
|
128
|
+
assert.deepEqual(
|
|
129
|
+
treeOf(workspace),
|
|
130
|
+
SOURCES,
|
|
131
|
+
"a half restore serves no dashboard",
|
|
132
|
+
);
|
|
133
|
+
assert.equal(storeBuild(cache, "k1", {}, build), null);
|
|
134
|
+
write(join(build.workspace, "dist", "server.mjs"), "other();\n");
|
|
135
|
+
assert.equal(storeBuild(cache, "k1", {}, build), null);
|
|
136
|
+
assert.equal(restoreBuild(cache, "k1", workspace), true);
|
|
137
|
+
assert.deepEqual(
|
|
138
|
+
treeOf(workspace),
|
|
139
|
+
{ ...SOURCES, ...BUILT },
|
|
140
|
+
"a store over a live entry, or a file shared with the run that built it, changes a build under its reader",
|
|
141
|
+
);
|
|
142
|
+
assert.deepEqual(
|
|
143
|
+
namesIn(cache).filter((name) => name.startsWith(".pending-")),
|
|
144
|
+
[],
|
|
145
|
+
"a pending store left behind fills the disk",
|
|
146
|
+
);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
await test("a restore never replaces what the run's own setup wrote", (t) => {
|
|
150
|
+
const { cache, build, prepared } = fixture(t);
|
|
151
|
+
assert.equal(storeBuild(cache, "k1", {}, build), null);
|
|
152
|
+
const workspace = prepared("next");
|
|
153
|
+
write(join(workspace, "components.d.ts"), "mine\n");
|
|
154
|
+
assert.equal(restoreBuild(cache, "k1", workspace), false);
|
|
155
|
+
assert.deepEqual(
|
|
156
|
+
treeOf(workspace),
|
|
157
|
+
{ ...SOURCES, "components.d.ts": "mine\n" },
|
|
158
|
+
"a restore over the run's sources serves the builder's",
|
|
159
|
+
);
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
await test("a build holding an absolute link is not stored", (t) => {
|
|
163
|
+
const { cache, build, prepared } = fixture(t);
|
|
164
|
+
symlinkSync(build.workspace, join(build.workspace, "dist", "self"));
|
|
165
|
+
assert.match(
|
|
166
|
+
storeBuild(cache, "k1", {}, build) ?? "",
|
|
167
|
+
/links to/u,
|
|
168
|
+
"an absolute link points into the run that built it",
|
|
169
|
+
);
|
|
170
|
+
assert.equal(restoreBuild(cache, "k1", prepared("next")), false);
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
await test("a cache that cannot be written refuses the store without throwing", (t) => {
|
|
174
|
+
const { dir, build } = fixture(t);
|
|
175
|
+
writeFileSync(join(dir, "file"), "");
|
|
176
|
+
assert.match(
|
|
177
|
+
storeBuild(join(dir, "file", "cache"), "k1", {}, build) ?? "",
|
|
178
|
+
/ENOTDIR/u,
|
|
179
|
+
"a cache folder created outside a try fails the run after its build",
|
|
180
|
+
);
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
await test("the cache keeps the most recently used builds", (t) => {
|
|
184
|
+
const { cache, build, prepared } = fixture(t);
|
|
185
|
+
const old = new Date("2026-01-01T00:00:00Z");
|
|
186
|
+
for (let index = 0; index < KEPT_BUILDS; index += 1) {
|
|
187
|
+
assert.equal(storeBuild(cache, `k${index}`, {}, build), null);
|
|
188
|
+
utimesSync(join(cache, `k${index}`, ".dms-control-ready"), old, old);
|
|
189
|
+
}
|
|
190
|
+
assert.equal(restoreBuild(cache, "k0", prepared("used")), true);
|
|
191
|
+
assert.equal(storeBuild(cache, "new", {}, build), null);
|
|
192
|
+
const kept = namesIn(cache);
|
|
193
|
+
assert.equal(
|
|
194
|
+
kept.length,
|
|
195
|
+
KEPT_BUILDS,
|
|
196
|
+
"an unbounded cache fills the shared disk",
|
|
197
|
+
);
|
|
198
|
+
assert.ok(
|
|
199
|
+
kept.includes("k0"),
|
|
200
|
+
"pruning by creation drops the build every run uses",
|
|
201
|
+
);
|
|
202
|
+
assert.ok(kept.includes("new"));
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
await test("two stores of one key at once leave one whole entry", async (t) => {
|
|
206
|
+
const { dir, cache, prepared } = fixture(t);
|
|
207
|
+
/** @type {Record<string, string>} */
|
|
208
|
+
const sides = {};
|
|
209
|
+
for (const side of ["a", "b"]) {
|
|
210
|
+
sides[side] = join(dir, side);
|
|
211
|
+
for (let index = 0; index < FILES_PER_STORE; index += 1)
|
|
212
|
+
write(
|
|
213
|
+
join(sides[side], "dist", `chunk-${index}.js`),
|
|
214
|
+
`${side}${index}\n`,
|
|
215
|
+
);
|
|
216
|
+
}
|
|
217
|
+
const at = Date.now() + START_DELAY_MS;
|
|
218
|
+
const script = `const { storeBuild } = await import(${JSON.stringify(new URL("build-cache.mjs", import.meta.url).href)});
|
|
219
|
+
while (Date.now() < ${at});
|
|
220
|
+
storeBuild(process.argv[1], "k", {}, { workspace: process.argv[2], outputs: ["dist"] });`;
|
|
221
|
+
await Promise.all(
|
|
222
|
+
Object.values(sides).map((source) =>
|
|
223
|
+
exec(process.execPath, [
|
|
224
|
+
"--input-type=module",
|
|
225
|
+
"-e",
|
|
226
|
+
script,
|
|
227
|
+
cache,
|
|
228
|
+
source,
|
|
229
|
+
]),
|
|
230
|
+
),
|
|
231
|
+
);
|
|
232
|
+
assert.deepEqual(
|
|
233
|
+
namesIn(cache),
|
|
234
|
+
["k"],
|
|
235
|
+
"a store left pending fills the disk",
|
|
236
|
+
);
|
|
237
|
+
const workspace = prepared("restored");
|
|
238
|
+
assert.equal(restoreBuild(cache, "k", workspace), true);
|
|
239
|
+
assert.ok(
|
|
240
|
+
[treeOf(sides.a), treeOf(sides.b)].some((side) =>
|
|
241
|
+
isDeepStrictEqual({ ...side, ...SOURCES }, treeOf(workspace)),
|
|
242
|
+
),
|
|
243
|
+
"two runs writing one folder leave a mix of both builds",
|
|
244
|
+
);
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
await test("a restore that fails part-way leaves the run's sources alone, and one that cannot mark the entry used still lays it out", (t) => {
|
|
248
|
+
const { cache, build, prepared } = fixture(t);
|
|
249
|
+
assert.equal(storeBuild(cache, "k1", {}, build), null);
|
|
250
|
+
symlinkSync("/pruned", join(cache, "k1", "outputs", "dist", "gone"));
|
|
251
|
+
const workspace = prepared("half");
|
|
252
|
+
assert.equal(restoreBuild(cache, "k1", workspace), false);
|
|
253
|
+
assert.deepEqual(
|
|
254
|
+
treeOf(workspace),
|
|
255
|
+
SOURCES,
|
|
256
|
+
"a restore that stops part-way serves a dashboard missing chunks",
|
|
257
|
+
);
|
|
258
|
+
if (!existsSync(UNTOUCHABLE)) return;
|
|
259
|
+
assert.equal(storeBuild(cache, "k2", {}, build), null);
|
|
260
|
+
const mark = join(cache, "k2", ".dms-control-ready");
|
|
261
|
+
rmSync(mark);
|
|
262
|
+
symlinkSync(UNTOUCHABLE, mark);
|
|
263
|
+
const whole = prepared("whole");
|
|
264
|
+
assert.equal(
|
|
265
|
+
restoreBuild(cache, "k2", whole),
|
|
266
|
+
true,
|
|
267
|
+
"a use time touched outside the try throws on an entry another user owns or a prune removed",
|
|
268
|
+
);
|
|
269
|
+
assert.deepEqual(treeOf(whole), { ...SOURCES, ...BUILT });
|
|
270
|
+
});
|