dsh-test-drive 0.1.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/CHANGELOG.md +19 -0
- package/LICENSE +201 -0
- package/README.es.md +229 -0
- package/README.hi.md +229 -0
- package/README.md +229 -0
- package/README.pt.md +229 -0
- package/README.zh.md +229 -0
- package/SECURITY.md +55 -0
- package/THIRD_PARTY_NOTICES.md +15 -0
- package/cordis.patch.yml +45 -0
- package/lib/index.js +1920 -0
- package/lib/types/batch.d.ts +50 -0
- package/lib/types/batch.d.ts.map +1 -0
- package/lib/types/command.d.ts +22 -0
- package/lib/types/command.d.ts.map +1 -0
- package/lib/types/config.d.ts +109 -0
- package/lib/types/config.d.ts.map +1 -0
- package/lib/types/domain.d.ts +133 -0
- package/lib/types/domain.d.ts.map +1 -0
- package/lib/types/drive.d.ts +65 -0
- package/lib/types/drive.d.ts.map +1 -0
- package/lib/types/driver.d.ts +153 -0
- package/lib/types/driver.d.ts.map +1 -0
- package/lib/types/index.d.ts +49 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/report.d.ts +18 -0
- package/lib/types/report.d.ts.map +1 -0
- package/lib/types/result.d.ts +254 -0
- package/lib/types/result.d.ts.map +1 -0
- package/lib/types/sanitize.d.ts +58 -0
- package/lib/types/sanitize.d.ts.map +1 -0
- package/lib/types/tools.d.ts +34 -0
- package/lib/types/tools.d.ts.map +1 -0
- package/lib/types/version.d.ts +8 -0
- package/lib/types/version.d.ts.map +1 -0
- package/lib/types/workspace.d.ts +103 -0
- package/lib/types/workspace.d.ts.map +1 -0
- package/package.json +146 -0
package/lib/index.js
ADDED
|
@@ -0,0 +1,1920 @@
|
|
|
1
|
+
import { randomBytes } from "node:crypto";
|
|
2
|
+
import { access, mkdir, mkdtemp, readFile, rename, rm, writeFile } from "node:fs/promises";
|
|
3
|
+
import { basename, dirname, isAbsolute, join, resolve } from "node:path";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import z$1 from "@deepseek-ai/schemastery";
|
|
6
|
+
import { defineDomain, domainTable } from "@deepseek-ai/dsh-storage-domain";
|
|
7
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
8
|
+
import { tmpdir } from "node:os";
|
|
9
|
+
//#region src/driver.ts
|
|
10
|
+
/**
|
|
11
|
+
* The `dsh` CLI driver: locates a spawnable dsh executable (config override,
|
|
12
|
+
* PATH lookup, or npm shim parsing on Windows), runs each stage as a managed
|
|
13
|
+
* subprocess with a throwaway DSH_HOME, and interprets the outputs the stages
|
|
14
|
+
* produce (the pnpm allowBuilds gate, the config-dump layer list, and boot
|
|
15
|
+
* failure markers). No shell is ever involved — argv is passed directly — so
|
|
16
|
+
* hostile target specs cannot inject commands.
|
|
17
|
+
*
|
|
18
|
+
* @module dsh-test-drive/driver
|
|
19
|
+
*/
|
|
20
|
+
/** Cap on collected output per stream (in-memory tail). */
|
|
21
|
+
const COLLECT_MAX_BYTES = 65536;
|
|
22
|
+
/** Cap on the complete spill file per stream. */
|
|
23
|
+
const SPILL_MAX_BYTES = 1048576;
|
|
24
|
+
/** Grace between SIGTERM and SIGKILL for a stage process. */
|
|
25
|
+
const STAGE_GRACE_MS = 5e3;
|
|
26
|
+
/** Bootstrap bundles the `dsh plugin` template may add; the installed plugin is any OTHER bundle. */
|
|
27
|
+
const TEMPLATE_BUNDLES = [
|
|
28
|
+
"@deepseek-ai/dsh-base",
|
|
29
|
+
"@deepseek-ai/dsh-headless",
|
|
30
|
+
"@deepseek-ai/dsh-web-app"
|
|
31
|
+
];
|
|
32
|
+
/** Loader-failure markers that make a boot smoke fail regardless of exit code. */
|
|
33
|
+
const BOOT_FAILURE_MARKERS = [
|
|
34
|
+
/plugin\(s\) failed to load/iu,
|
|
35
|
+
/did not activate/iu,
|
|
36
|
+
/\bFAILED\b/u,
|
|
37
|
+
/could not be resolved/iu,
|
|
38
|
+
/fiber state/iu
|
|
39
|
+
];
|
|
40
|
+
/** Parse the JS target out of an npm-generated `.cmd`/`.bat` shim. */
|
|
41
|
+
function parseNpmShim(shimPath, shimText) {
|
|
42
|
+
const rel = /"%dp0%\\(?<rel>node_modules\\[^"]+\.(?:js|cjs|mjs))"/u.exec(shimText)?.groups?.rel;
|
|
43
|
+
if (rel === void 0) throw new Error(`dsh-test-drive: cannot parse npm shim ${shimPath} (no dp0-relative JS target found)`);
|
|
44
|
+
return join(dirname(shimPath), rel);
|
|
45
|
+
}
|
|
46
|
+
/** Regex escape for layer/row matching against package names. */
|
|
47
|
+
function escapeRegex(source) {
|
|
48
|
+
return source.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&");
|
|
49
|
+
}
|
|
50
|
+
/** Whether a target spec names a git-hosted source (subject to pnpm's prepare gate). */
|
|
51
|
+
function isGitLike(target) {
|
|
52
|
+
return /^git\+|^github:|^git@|\.git(?:#|$)/u.test(target);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Classify a sanitized target spec by how `dsh plugin add` addresses it.
|
|
56
|
+
* Pure: no filesystem or network access.
|
|
57
|
+
*
|
|
58
|
+
* @param spec - the sanitized target.
|
|
59
|
+
* @returns the address kind.
|
|
60
|
+
*/
|
|
61
|
+
function classifyTarget(spec) {
|
|
62
|
+
if (/\.tgz$/u.test(spec)) return "tarball";
|
|
63
|
+
if (/^file:/u.test(spec)) return spec.endsWith(".tgz") ? "tarball" : "path";
|
|
64
|
+
if (/^git\+|^github:|^git@|\.git(?:#|$)/u.test(spec)) return "repo";
|
|
65
|
+
if (isAbsolute(spec) || spec.startsWith(".")) return "path";
|
|
66
|
+
if (/^(@[a-z0-9-~][a-z0-9-._~]*\/)?[a-z0-9-~][a-z0-9-._~]*(@.+)?$/u.test(spec)) return "npm";
|
|
67
|
+
return "repo";
|
|
68
|
+
}
|
|
69
|
+
/** Extract the package name pnpm reports as blocked: `Ignored build scripts: <name>.` */
|
|
70
|
+
function parseIgnoredBuildScript(output) {
|
|
71
|
+
return /ignored build scripts:?\s*([a-z0-9][a-z0-9@/._-]*?)\s*\./iu.exec(output)?.[1];
|
|
72
|
+
}
|
|
73
|
+
/** Extract `# == <label>` layer labels from a `--dump-config` output. */
|
|
74
|
+
function parseDumpLayers(dump) {
|
|
75
|
+
const layers = [];
|
|
76
|
+
for (const match of dump.matchAll(/^# == (.+)$/gmu)) {
|
|
77
|
+
const label = match[1];
|
|
78
|
+
if (label !== void 0) layers.push(label.trim());
|
|
79
|
+
}
|
|
80
|
+
return layers;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Whether the dump proves the installed package's patch took effect: a layer
|
|
84
|
+
* label mentions it, or a row names it.
|
|
85
|
+
*/
|
|
86
|
+
function dumpMentionsPackage(dump, packageName) {
|
|
87
|
+
const namePattern = new RegExp(`(^|[^a-z0-9@/._-])${escapeRegex(packageName)}([^a-z0-9@/._-]|$)`, "iu");
|
|
88
|
+
const rowPattern = new RegExp(`^\\s*name:\\s*["']?${escapeRegex(packageName)}["']?\\s*$`, "mu");
|
|
89
|
+
const layers = parseDumpLayers(dump).filter((label) => namePattern.test(label));
|
|
90
|
+
return {
|
|
91
|
+
mentioned: layers.length > 0 || rowPattern.test(dump),
|
|
92
|
+
layers
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
/** Whether the text contains any loader-failure marker. */
|
|
96
|
+
function hasBootFailure(text) {
|
|
97
|
+
return BOOT_FAILURE_MARKERS.some((marker) => marker.test(text));
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Locate and run the `dsh` CLI. The located argv prefix is cached per driver
|
|
101
|
+
* instance, so every stage reuses one resolution.
|
|
102
|
+
*/
|
|
103
|
+
var DshDriver = class {
|
|
104
|
+
deps;
|
|
105
|
+
located;
|
|
106
|
+
constructor(deps) {
|
|
107
|
+
this.deps = deps;
|
|
108
|
+
}
|
|
109
|
+
/** Resolve the spawn argv prefix `[program, ...args]` for the dsh CLI. */
|
|
110
|
+
locate() {
|
|
111
|
+
this.located ??= this.resolveLocation();
|
|
112
|
+
return this.located;
|
|
113
|
+
}
|
|
114
|
+
async resolveLocation() {
|
|
115
|
+
const { config, ctx, log } = this.deps;
|
|
116
|
+
if (config.dshBin !== "") {
|
|
117
|
+
if (!isAbsolute(config.dshBin)) throw new Error(`dsh-test-drive: config.dshBin must be an absolute path (got ${JSON.stringify(config.dshBin)})`);
|
|
118
|
+
if (/\.(?:[cm]?js)$/u.test(config.dshBin)) return [process.execPath, config.dshBin];
|
|
119
|
+
log(`test-drive: using config.dshBin ${config.dshBin}`);
|
|
120
|
+
return [config.dshBin];
|
|
121
|
+
}
|
|
122
|
+
const found = await ctx.subprocess.resolveExecutable("dsh");
|
|
123
|
+
if (process.platform === "win32" && /\.(?:cmd|bat)$/iu.test(found)) {
|
|
124
|
+
const target = parseNpmShim(found, await readFile(found, "utf8"));
|
|
125
|
+
await access(target);
|
|
126
|
+
log(`test-drive: dsh resolved to ${target} (via npm shim ${found})`);
|
|
127
|
+
return [process.execPath, target];
|
|
128
|
+
}
|
|
129
|
+
if (/\.ps1$/iu.test(found)) throw new Error(`dsh-test-drive: dsh resolved to the PowerShell shim ${found}; set config.dshBin to the JS entry (or the .cmd shim)`);
|
|
130
|
+
log(`test-drive: dsh resolved to ${found}`);
|
|
131
|
+
return [found];
|
|
132
|
+
}
|
|
133
|
+
/** Compile the child environment: throwaway home, redirected store, deliberate forwards only. */
|
|
134
|
+
buildEnv(home, storeDir) {
|
|
135
|
+
const env = {
|
|
136
|
+
DSH_HOME: home,
|
|
137
|
+
npm_config_store_dir: storeDir
|
|
138
|
+
};
|
|
139
|
+
for (const name of this.deps.config.forwardEnv) {
|
|
140
|
+
const value = process.env[name];
|
|
141
|
+
if (value !== void 0) env[name] = value;
|
|
142
|
+
}
|
|
143
|
+
return env;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* Run one dsh CLI invocation as a managed subprocess.
|
|
147
|
+
*
|
|
148
|
+
* @param args - dsh arguments (argv entries, never shell-interpreted).
|
|
149
|
+
* @param options - isolation facts and the stage deadline.
|
|
150
|
+
* @returns the collected outcome.
|
|
151
|
+
*/
|
|
152
|
+
async run(args, options) {
|
|
153
|
+
const started = Date.now();
|
|
154
|
+
const outer = options.signal;
|
|
155
|
+
const deadline = AbortSignal.timeout(options.timeoutMs);
|
|
156
|
+
const signal = outer === void 0 ? deadline : AbortSignal.any([outer, deadline]);
|
|
157
|
+
let handle;
|
|
158
|
+
try {
|
|
159
|
+
const [program, ...prefix] = await this.locate();
|
|
160
|
+
if (program === void 0) throw new Error("dsh location resolved to an empty argv");
|
|
161
|
+
const spec = {
|
|
162
|
+
argv: [
|
|
163
|
+
program,
|
|
164
|
+
...prefix,
|
|
165
|
+
...args
|
|
166
|
+
],
|
|
167
|
+
cwd: options.cwd,
|
|
168
|
+
stdio: {
|
|
169
|
+
stdin: "ignore",
|
|
170
|
+
stdout: {
|
|
171
|
+
maxBytes: COLLECT_MAX_BYTES,
|
|
172
|
+
spill: { maxBytes: SPILL_MAX_BYTES }
|
|
173
|
+
},
|
|
174
|
+
stderr: {
|
|
175
|
+
maxBytes: COLLECT_MAX_BYTES,
|
|
176
|
+
spill: { maxBytes: SPILL_MAX_BYTES }
|
|
177
|
+
}
|
|
178
|
+
},
|
|
179
|
+
graceMs: STAGE_GRACE_MS,
|
|
180
|
+
signal,
|
|
181
|
+
env: this.buildEnv(options.home, options.storeDir)
|
|
182
|
+
};
|
|
183
|
+
handle = this.deps.ctx.subprocess.spawn(spec);
|
|
184
|
+
} catch (error) {
|
|
185
|
+
return {
|
|
186
|
+
exitCode: null,
|
|
187
|
+
stdout: "",
|
|
188
|
+
stderr: `spawn failed: ${String(error)}`,
|
|
189
|
+
truncated: false,
|
|
190
|
+
durationMs: Date.now() - started,
|
|
191
|
+
timedOut: false
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
let outcome;
|
|
195
|
+
try {
|
|
196
|
+
outcome = await handle.done;
|
|
197
|
+
} catch (error) {
|
|
198
|
+
return {
|
|
199
|
+
exitCode: null,
|
|
200
|
+
stdout: "",
|
|
201
|
+
stderr: `spawn failed: ${String(error)}`,
|
|
202
|
+
truncated: false,
|
|
203
|
+
durationMs: Date.now() - started,
|
|
204
|
+
timedOut: false
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
const stdoutRead = handle.collected.stdout?.readFrom(0);
|
|
208
|
+
const stderrRead = handle.collected.stderr?.readFrom(0);
|
|
209
|
+
const stdout = stdoutRead?.text ?? "";
|
|
210
|
+
const stderr = stderrRead?.text ?? "";
|
|
211
|
+
const truncated = (stdoutRead?.lossy ?? false) || (stderrRead?.lossy ?? false);
|
|
212
|
+
return {
|
|
213
|
+
exitCode: outcome.exitCode,
|
|
214
|
+
stdout,
|
|
215
|
+
stderr,
|
|
216
|
+
truncated,
|
|
217
|
+
durationMs: Date.now() - started,
|
|
218
|
+
timedOut: deadline.aborted && (outer === void 0 || !outer.aborted)
|
|
219
|
+
};
|
|
220
|
+
}
|
|
221
|
+
/** Write the profile's pnpm-workspace.yaml allowBuilds entry for one package. */
|
|
222
|
+
async writeAllowBuilds(profileDir, packageName) {
|
|
223
|
+
await mkdir(profileDir, { recursive: true });
|
|
224
|
+
const key = packageName.replace(/'/gu, "''");
|
|
225
|
+
await writeFile(join(profileDir, "pnpm-workspace.yaml"), `allowBuilds:\n '${key}': true\n`, "utf8");
|
|
226
|
+
this.deps.log(`test-drive: allowlisted prepare build for ${packageName} in ${join(profileDir, "pnpm-workspace.yaml")}`);
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Run the install stage: `dsh plugin --profile <name> add <target>`, with the
|
|
230
|
+
* documented allowBuilds retry for git-hosted sources.
|
|
231
|
+
*/
|
|
232
|
+
async add(target, options) {
|
|
233
|
+
const { config } = this.deps;
|
|
234
|
+
const args = [
|
|
235
|
+
"plugin",
|
|
236
|
+
"--profile",
|
|
237
|
+
config.profileName,
|
|
238
|
+
"add",
|
|
239
|
+
target
|
|
240
|
+
];
|
|
241
|
+
const first = await this.run(args, options);
|
|
242
|
+
const combined = `${first.stdout}\n${first.stderr}`;
|
|
243
|
+
const blocked = config.allowBuilds ? parseIgnoredBuildScript(combined) : void 0;
|
|
244
|
+
if (first.exitCode === 0 || blocked === void 0) return {
|
|
245
|
+
run: first,
|
|
246
|
+
attempts: 1,
|
|
247
|
+
allowBuildsNeeded: false
|
|
248
|
+
};
|
|
249
|
+
await this.writeAllowBuilds(join(options.home, "profiles", config.profileName), blocked);
|
|
250
|
+
return {
|
|
251
|
+
run: await this.run(args, options),
|
|
252
|
+
attempts: 2,
|
|
253
|
+
allowBuildsNeeded: true
|
|
254
|
+
};
|
|
255
|
+
}
|
|
256
|
+
/** Run the config stage: `dsh --profile <name> --dump-config`. */
|
|
257
|
+
async dumpConfig(options) {
|
|
258
|
+
return this.run([
|
|
259
|
+
"--profile",
|
|
260
|
+
this.deps.config.profileName,
|
|
261
|
+
"--dump-config"
|
|
262
|
+
], options);
|
|
263
|
+
}
|
|
264
|
+
/** Run the boot-smoke stage: `dsh --profile <name> <task>`. */
|
|
265
|
+
async smokeTask(task, options) {
|
|
266
|
+
return this.run([
|
|
267
|
+
"--profile",
|
|
268
|
+
this.deps.config.profileName,
|
|
269
|
+
task
|
|
270
|
+
], options);
|
|
271
|
+
}
|
|
272
|
+
/** Run the uninstall stage: `dsh plugin --profile <name> remove <pkg>`. */
|
|
273
|
+
async remove(packageName, options) {
|
|
274
|
+
return this.run([
|
|
275
|
+
"plugin",
|
|
276
|
+
"--profile",
|
|
277
|
+
this.deps.config.profileName,
|
|
278
|
+
"remove",
|
|
279
|
+
packageName
|
|
280
|
+
], options);
|
|
281
|
+
}
|
|
282
|
+
/** Ask the CLI for its version (cached per driver instance). */
|
|
283
|
+
async version(options) {
|
|
284
|
+
return (await this.run(["--version"], options)).stdout.trim().split("\n").at(-1)?.trim() ?? "";
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Read the installed plugin's own manifest from the profile's node_modules.
|
|
288
|
+
*
|
|
289
|
+
* @param profileDir - the profile directory `dsh plugin` manages.
|
|
290
|
+
* @returns the package facts, or undefined when no out-of-tree bundle is installed.
|
|
291
|
+
*/
|
|
292
|
+
async readInstalledPackage(profileDir) {
|
|
293
|
+
let manifestPath;
|
|
294
|
+
let text;
|
|
295
|
+
try {
|
|
296
|
+
manifestPath = join(profileDir, "package.json");
|
|
297
|
+
text = await readFile(manifestPath, "utf8");
|
|
298
|
+
} catch {
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
const installed = (JSON.parse(text).dsh?.profile?.bundles ?? []).filter((name) => !TEMPLATE_BUNDLES.includes(name));
|
|
302
|
+
if (installed.length === 0) return void 0;
|
|
303
|
+
const name = installed[installed.length - 1];
|
|
304
|
+
if (name === void 0) return void 0;
|
|
305
|
+
const segments = name.split("/");
|
|
306
|
+
try {
|
|
307
|
+
const pkgPath = join(profileDir, "node_modules", ...segments, "package.json");
|
|
308
|
+
const pkg = JSON.parse(await readFile(pkgPath, "utf8"));
|
|
309
|
+
return {
|
|
310
|
+
packageName: name,
|
|
311
|
+
packageVersion: typeof pkg.version === "string" ? pkg.version : "unknown",
|
|
312
|
+
hasBundleManifest: typeof pkg.dsh?.bundle?.patch === "string"
|
|
313
|
+
};
|
|
314
|
+
} catch {
|
|
315
|
+
return {
|
|
316
|
+
packageName: name,
|
|
317
|
+
packageVersion: "unknown",
|
|
318
|
+
hasBundleManifest: false
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
/** Resolve a relative local path target against the host's invoking directory. */
|
|
324
|
+
function anchorLocalTarget(target) {
|
|
325
|
+
return resolve(process.cwd(), target);
|
|
326
|
+
}
|
|
327
|
+
//#endregion
|
|
328
|
+
//#region src/result.ts
|
|
329
|
+
/**
|
|
330
|
+
* The structured test-drive result: one record per driven target, plus the
|
|
331
|
+
* batch matrix that aggregates them. This is the machine-readable output
|
|
332
|
+
* contract that downstream consumers (dsh-score and other scoring pipelines)
|
|
333
|
+
* read from the `test-drive` storage domain — every field a scorer needs
|
|
334
|
+
* (install/smoke status, durations, failure summaries) is a first-class
|
|
335
|
+
* field, never prose to be parsed.
|
|
336
|
+
*
|
|
337
|
+
* The zod schemas are the single source of truth for both the domain record
|
|
338
|
+
* types (validated at the durable boundary by `dsh-storage-domain`) and the
|
|
339
|
+
* TypeScript record types (`z.infer`), so the written records and the typed
|
|
340
|
+
* runtime can never drift.
|
|
341
|
+
*
|
|
342
|
+
* @module dsh-test-drive/result
|
|
343
|
+
*/
|
|
344
|
+
/** Schema discriminator of every record this plugin writes (version 1). */
|
|
345
|
+
const RESULT_SCHEMA = "dsh-test-drive/v1";
|
|
346
|
+
/** How the target was addressed. */
|
|
347
|
+
const TargetKindSchema = z.union([
|
|
348
|
+
z.literal("repo"),
|
|
349
|
+
z.literal("npm"),
|
|
350
|
+
z.literal("path"),
|
|
351
|
+
z.literal("tarball")
|
|
352
|
+
]);
|
|
353
|
+
/** Outcome of an ordinary stage: it ran and passed, ran and failed, or was skipped by config. */
|
|
354
|
+
const StageStatusSchema = z.union([
|
|
355
|
+
z.literal("pass"),
|
|
356
|
+
z.literal("fail"),
|
|
357
|
+
z.literal("skipped")
|
|
358
|
+
]);
|
|
359
|
+
/** Boot-smoke outcome: clean boot plus completed task, failed boot, clean boot with an inconclusive task, or skipped. */
|
|
360
|
+
const SmokeStatusSchema = z.union([
|
|
361
|
+
z.literal("pass"),
|
|
362
|
+
z.literal("fail"),
|
|
363
|
+
z.literal("boot-ok"),
|
|
364
|
+
z.literal("skipped")
|
|
365
|
+
]);
|
|
366
|
+
/** Overall verdict of one drive run. */
|
|
367
|
+
const DriveVerdictSchema = z.union([
|
|
368
|
+
z.literal("pass"),
|
|
369
|
+
z.literal("fail"),
|
|
370
|
+
z.literal("partial"),
|
|
371
|
+
z.literal("unknown")
|
|
372
|
+
]);
|
|
373
|
+
/** Shared stage facts: exit, duration, attempts, and sanitized summary/tail. */
|
|
374
|
+
const StageResultSchema = z.object({
|
|
375
|
+
/** How the stage ended. */
|
|
376
|
+
status: StageStatusSchema,
|
|
377
|
+
/** Process exit code; null when the stage never produced a process outcome (timeout or spawn failure). */
|
|
378
|
+
exitCode: z.number().int().nullable(),
|
|
379
|
+
/** Wall-clock duration of the stage in milliseconds. */
|
|
380
|
+
durationMs: z.number().finite(),
|
|
381
|
+
/** Spawn attempts (the allowBuilds retry is attempt 2). */
|
|
382
|
+
attempts: z.number().int().min(1),
|
|
383
|
+
/** One-line sanitized verdict, e.g. `exit 0 after allowBuilds allowance`. */
|
|
384
|
+
summary: z.string(),
|
|
385
|
+
/** Sanitized output tail (secrets and temp paths redacted). */
|
|
386
|
+
outputTail: z.string()
|
|
387
|
+
});
|
|
388
|
+
/** Install stage extras: whether the pnpm allowBuilds gate was hit and allowed. */
|
|
389
|
+
const InstallStageResultSchema = StageResultSchema.extend({
|
|
390
|
+
/** True when pnpm blocked a git prepare build and the retry allowlisted it. */
|
|
391
|
+
allowBuildsNeeded: z.boolean() });
|
|
392
|
+
/** Config-dump stage extras: whether the bundle patch provably took effect. */
|
|
393
|
+
const ConfigStageResultSchema = StageResultSchema.extend({
|
|
394
|
+
/** True when the dump contains the installed package as a layer or a row. */
|
|
395
|
+
patchEffective: z.boolean(),
|
|
396
|
+
/** Sanitized layer labels in the dump that mention the package. */
|
|
397
|
+
layers: z.array(z.string())
|
|
398
|
+
});
|
|
399
|
+
/** Boot-smoke stage: loader-failure facts plus the optional task completion. */
|
|
400
|
+
const SmokeStageResultSchema = StageResultSchema.extend({
|
|
401
|
+
/** `pass`/`fail`/`boot-ok`/`skipped`, see {@link SmokeStatus}. */
|
|
402
|
+
status: SmokeStatusSchema,
|
|
403
|
+
/** True when loader-failure markers (FAILED / failed to load / did not activate) appeared. */
|
|
404
|
+
bootFailed: z.boolean(),
|
|
405
|
+
/** True when the headless one-shot task reported completion (exit 0). */
|
|
406
|
+
taskCompleted: z.boolean()
|
|
407
|
+
});
|
|
408
|
+
/** Uninstall stage result. */
|
|
409
|
+
const UninstallStageResultSchema = StageResultSchema;
|
|
410
|
+
/** Cleanup stage: quarantine/removal facts for the temp directory this run owned. */
|
|
411
|
+
const CleanupStageResultSchema = z.object({
|
|
412
|
+
/** `pass` when the owned temp root was removed; `skipped` when keepTempDirs was set. */
|
|
413
|
+
status: z.union([
|
|
414
|
+
z.literal("pass"),
|
|
415
|
+
z.literal("fail"),
|
|
416
|
+
z.literal("skipped")
|
|
417
|
+
]),
|
|
418
|
+
/** True when the root was renamed into the owned quarantine directory first. */
|
|
419
|
+
quarantined: z.boolean(),
|
|
420
|
+
/** True when the quarantined root was fully removed from disk. */
|
|
421
|
+
removed: z.boolean(),
|
|
422
|
+
/** One-line sanitized summary (dry-run plan on failure). */
|
|
423
|
+
summary: z.string()
|
|
424
|
+
});
|
|
425
|
+
/** Facts about the runner that produced the record. */
|
|
426
|
+
const RunMetaSchema = z.object({
|
|
427
|
+
/** Unique id of this drive run (also the `runs` table key). */
|
|
428
|
+
runId: z.string().min(1),
|
|
429
|
+
/** ISO-8601 start timestamp. */
|
|
430
|
+
startedAt: z.string().min(1),
|
|
431
|
+
/** ISO-8601 finish timestamp. */
|
|
432
|
+
finishedAt: z.string().min(1),
|
|
433
|
+
/** Total wall-clock duration in milliseconds. */
|
|
434
|
+
durationMs: z.number().finite(),
|
|
435
|
+
/** `dsh --version` of the CLI that ran the stages ('' when unknown). */
|
|
436
|
+
harnessVersion: z.string(),
|
|
437
|
+
/** dsh-test-drive's own version. */
|
|
438
|
+
pluginVersion: z.string().min(1),
|
|
439
|
+
/** Runner platform (`process.platform`). */
|
|
440
|
+
platform: z.string().min(1),
|
|
441
|
+
/** Runner Node version (`process.version`). */
|
|
442
|
+
node: z.string().min(1)
|
|
443
|
+
});
|
|
444
|
+
/** What was driven, and what the install revealed about it. */
|
|
445
|
+
const DriveTargetSchema = z.object({
|
|
446
|
+
/** Address kind, see {@link TargetKind}. */
|
|
447
|
+
kind: TargetKindSchema,
|
|
448
|
+
/** Sanitized target spec exactly as passed to `dsh plugin add`. */
|
|
449
|
+
spec: z.string().min(1),
|
|
450
|
+
/** Install-revealed package facts; absent when the install never materialized the package. */
|
|
451
|
+
resolved: z.object({
|
|
452
|
+
/** Installed package name (authoritative: read from the profile manifest). */
|
|
453
|
+
packageName: z.string().min(1),
|
|
454
|
+
/** Installed package version. */
|
|
455
|
+
packageVersion: z.string().min(1),
|
|
456
|
+
/** Whether the package declares `dsh.bundle.patch` (a bundle layer exists). */
|
|
457
|
+
hasBundleManifest: z.boolean()
|
|
458
|
+
}).optional()
|
|
459
|
+
});
|
|
460
|
+
/** Isolation facts asserted by construction: every path the child touched was inside the owned temp root. */
|
|
461
|
+
const IsolationFactsSchema = z.object({
|
|
462
|
+
/** The child processes used a throwaway DSH_HOME inside the owned temp root. */
|
|
463
|
+
tempDshHome: z.boolean(),
|
|
464
|
+
/** The child cwd was a throwaway workspace inside the owned temp root. */
|
|
465
|
+
tempWorkspace: z.boolean(),
|
|
466
|
+
/** The pnpm store was redirected inside the owned temp root. */
|
|
467
|
+
tempStore: z.boolean(),
|
|
468
|
+
/** Host home untouched — always true: no code path reads or writes the real profile home. */
|
|
469
|
+
hostHomeTouched: z.literal(false)
|
|
470
|
+
});
|
|
471
|
+
/** One drive run: install → dump-config → boot smoke → uninstall → cleanup. */
|
|
472
|
+
const DriveResultSchema = z.object({
|
|
473
|
+
/** Discriminator {@link RESULT_SCHEMA}. */
|
|
474
|
+
schema: z.literal(RESULT_SCHEMA),
|
|
475
|
+
/** Runner facts. */
|
|
476
|
+
run: RunMetaSchema,
|
|
477
|
+
/** Target facts. */
|
|
478
|
+
target: DriveTargetSchema,
|
|
479
|
+
/** Isolation facts. */
|
|
480
|
+
isolation: IsolationFactsSchema,
|
|
481
|
+
/** Stage outcomes in execution order. */
|
|
482
|
+
stages: z.object({
|
|
483
|
+
install: InstallStageResultSchema,
|
|
484
|
+
config: ConfigStageResultSchema,
|
|
485
|
+
smoke: SmokeStageResultSchema,
|
|
486
|
+
uninstall: UninstallStageResultSchema,
|
|
487
|
+
cleanup: CleanupStageResultSchema
|
|
488
|
+
}),
|
|
489
|
+
/** Overall verdict derived from the stages by {@link verdictOf}. */
|
|
490
|
+
verdict: DriveVerdictSchema,
|
|
491
|
+
/** One-line sanitized explanation of the verdict. */
|
|
492
|
+
verdictReason: z.string().min(1)
|
|
493
|
+
});
|
|
494
|
+
/** One row of a batch matrix: the per-target verdict plus the fields a matrix table needs. */
|
|
495
|
+
const MatrixRowSchema = z.object({
|
|
496
|
+
/** Sanitized target spec. */
|
|
497
|
+
target: z.string().min(1),
|
|
498
|
+
/** Address kind. */
|
|
499
|
+
kind: TargetKindSchema,
|
|
500
|
+
/** Per-target verdict. */
|
|
501
|
+
verdict: DriveVerdictSchema,
|
|
502
|
+
/** Install stage status. */
|
|
503
|
+
install: StageStatusSchema,
|
|
504
|
+
/** Smoke stage status. */
|
|
505
|
+
smoke: SmokeStatusSchema,
|
|
506
|
+
/** Per-target wall-clock duration in milliseconds. */
|
|
507
|
+
durationMs: z.number().finite(),
|
|
508
|
+
/** One-line sanitized summary (the target's verdict reason). */
|
|
509
|
+
summary: z.string()
|
|
510
|
+
});
|
|
511
|
+
/** Aggregated batch record keyed by the batch id. */
|
|
512
|
+
const MatrixRecordSchema = z.object({
|
|
513
|
+
/** Discriminator {@link RESULT_SCHEMA}. */
|
|
514
|
+
schema: z.literal(RESULT_SCHEMA),
|
|
515
|
+
/** Matrix id — named by the producing batch job's final output line. */
|
|
516
|
+
id: z.string().min(1),
|
|
517
|
+
/** ISO-8601 creation timestamp. */
|
|
518
|
+
createdAt: z.string().min(1),
|
|
519
|
+
/** Total wall-clock duration of the batch in milliseconds. */
|
|
520
|
+
durationMs: z.number().finite(),
|
|
521
|
+
/** Per-target rows in drive order. */
|
|
522
|
+
rows: z.array(MatrixRowSchema),
|
|
523
|
+
/** Verdict tallies over `rows`. */
|
|
524
|
+
totals: z.object({
|
|
525
|
+
total: z.number().int().min(0),
|
|
526
|
+
pass: z.number().int().min(0),
|
|
527
|
+
fail: z.number().int().min(0),
|
|
528
|
+
partial: z.number().int().min(0),
|
|
529
|
+
unknown: z.number().int().min(0)
|
|
530
|
+
})
|
|
531
|
+
});
|
|
532
|
+
/** Compile the tallies for one matrix record. */
|
|
533
|
+
function totalsOf(rows) {
|
|
534
|
+
const totals = {
|
|
535
|
+
total: rows.length,
|
|
536
|
+
pass: 0,
|
|
537
|
+
fail: 0,
|
|
538
|
+
partial: 0,
|
|
539
|
+
unknown: 0
|
|
540
|
+
};
|
|
541
|
+
for (const row of rows) switch (row.verdict) {
|
|
542
|
+
case "pass":
|
|
543
|
+
totals.pass += 1;
|
|
544
|
+
break;
|
|
545
|
+
case "fail":
|
|
546
|
+
totals.fail += 1;
|
|
547
|
+
break;
|
|
548
|
+
case "partial":
|
|
549
|
+
totals.partial += 1;
|
|
550
|
+
break;
|
|
551
|
+
default: totals.unknown += 1;
|
|
552
|
+
}
|
|
553
|
+
return totals;
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Derive the overall verdict of one drive run from its stage outcomes.
|
|
557
|
+
*
|
|
558
|
+
* Rules, in order:
|
|
559
|
+
* 1. A failed install or a failed boot (`smoke.fail`) is a hard `fail`.
|
|
560
|
+
* 2. `pass` needs install pass + patch effective + a clean boot (`pass` or
|
|
561
|
+
* `boot-ok`) + a successful uninstall.
|
|
562
|
+
* 3. `partial` covers everything that installed but missed a later assurance
|
|
563
|
+
* (patch not visible, smoke skipped by config, uninstall failed).
|
|
564
|
+
* 4. `unknown` is the honest answer when nothing decisive ran.
|
|
565
|
+
*
|
|
566
|
+
* @param result - the complete drive result.
|
|
567
|
+
* @returns `[verdict, reason]`.
|
|
568
|
+
*/
|
|
569
|
+
function verdictOf(result) {
|
|
570
|
+
const { install, config, smoke, uninstall } = result.stages;
|
|
571
|
+
if (install.status === "fail") return ["fail", `install failed: ${install.summary}`];
|
|
572
|
+
if (smoke.status === "fail") return ["fail", `boot smoke failed: ${smoke.summary}`];
|
|
573
|
+
if (install.status === "pass" && config.status === "pass" && config.patchEffective && (smoke.status === "pass" || smoke.status === "boot-ok") && uninstall.status === "pass") {
|
|
574
|
+
if (smoke.status === "boot-ok") return ["pass", "install, patch, boot, and uninstall verified; headless task inconclusive (see smoke.summary)"];
|
|
575
|
+
return ["pass", "install, patch, boot, task, and uninstall all verified"];
|
|
576
|
+
}
|
|
577
|
+
if (install.status !== "pass") return ["unknown", "install never ran to completion"];
|
|
578
|
+
if (config.status !== "pass" || !config.patchEffective) return ["partial", `install passed but the bundle patch was not verified (${config.summary})`];
|
|
579
|
+
if (smoke.status === "skipped") return ["partial", "install passed but the boot smoke was skipped by config"];
|
|
580
|
+
if (uninstall.status !== "pass") return ["partial", `install and smoke passed but uninstall failed (${uninstall.summary})`];
|
|
581
|
+
return ["unknown", "stage combination not covered by the verdict rules"];
|
|
582
|
+
}
|
|
583
|
+
//#endregion
|
|
584
|
+
//#region src/sanitize.ts
|
|
585
|
+
/**
|
|
586
|
+
* Pure sanitizers for every string that reaches a report, a log, or the model.
|
|
587
|
+
* Nothing here reads the environment or the clock: report content is the only
|
|
588
|
+
* caller, and the sanitizers must stay testable with extreme inputs (token
|
|
589
|
+
* literals, credential-bearing URLs, hostile repo specs, temp-root paths).
|
|
590
|
+
*
|
|
591
|
+
* @module dsh-test-drive/sanitize
|
|
592
|
+
*/
|
|
593
|
+
/** GitHub personal access tokens (`ghp_` prefix, ≥20 body chars). */
|
|
594
|
+
const GITHUB_TOKEN = /ghp_[A-Za-z0-9]{20,}/gu;
|
|
595
|
+
/** npm access tokens (`npm_` prefix, ≥20 body chars). */
|
|
596
|
+
const NPM_TOKEN = /npm_[A-Za-z0-9]{20,}/gu;
|
|
597
|
+
/** OpenAI-style keys (`sk-` prefix, ≥16 body chars) and common jwt/aws/twilio shapes. */
|
|
598
|
+
const API_KEY = /(?:sk-[A-Za-z0-9_-]{16,}|AKIA[0-9A-Z]{16}|xox[baprs]-[A-Za-z0-9-]{10,})/gu;
|
|
599
|
+
/** `scheme://user:pass@host` — keeps the scheme and host, drops the credentials. */
|
|
600
|
+
const URL_CREDENTIALS = /(\b[a-z][a-z0-9+.-]*:\/\/)[^/@\s:]+(?::[^/@\s]*)?@/giu;
|
|
601
|
+
/** `Authorization: Bearer <token>`-style header lines. */
|
|
602
|
+
const BEARER = /\b(Bearer\s+)[A-Za-z0-9._~+/=-]{8,}/giu;
|
|
603
|
+
/** Control characters that must never survive into a one-line report field. */
|
|
604
|
+
const CONTROL_CHARS = /[\u0000-\u001f\u007f]/gu;
|
|
605
|
+
/** Maximum length of a sanitized target spec recorded in a report. */
|
|
606
|
+
const MAX_TARGET_SPEC_LENGTH = 1e3;
|
|
607
|
+
/** Marker substituted for every redacted secret. */
|
|
608
|
+
const REDACTED = "***REDACTED***";
|
|
609
|
+
/**
|
|
610
|
+
* Replace credential-shaped substrings with {@link REDACTED}.
|
|
611
|
+
*
|
|
612
|
+
* @param text - any output or user-supplied string.
|
|
613
|
+
* @returns the same text with secrets redacted.
|
|
614
|
+
*/
|
|
615
|
+
function redactSecrets(text) {
|
|
616
|
+
return text.replace(GITHUB_TOKEN, REDACTED).replace(NPM_TOKEN, REDACTED).replace(API_KEY, REDACTED).replace(URL_CREDENTIALS, "$1***@").replace(BEARER, `$1${REDACTED}`);
|
|
617
|
+
}
|
|
618
|
+
/**
|
|
619
|
+
* Sanitize one user-supplied target spec for recording and display: strip
|
|
620
|
+
* control characters, redact embedded credentials, cap the length, and keep
|
|
621
|
+
* surrounding whitespace off the recorded value.
|
|
622
|
+
*
|
|
623
|
+
* @param spec - the raw target as the user supplied it.
|
|
624
|
+
* @returns the sanitized spec; never throws.
|
|
625
|
+
*/
|
|
626
|
+
function sanitizeTarget(spec) {
|
|
627
|
+
return redactSecrets(spec.replace(CONTROL_CHARS, " ").trim()).slice(0, MAX_TARGET_SPEC_LENGTH);
|
|
628
|
+
}
|
|
629
|
+
/**
|
|
630
|
+
* Bound a multi-line output stream to a short tail for one-line summaries.
|
|
631
|
+
* The tail keeps the END of the text (errors and final results cluster there).
|
|
632
|
+
*
|
|
633
|
+
* @param text - collected output.
|
|
634
|
+
* @param maxChars - retained character count.
|
|
635
|
+
* @returns the tail, prefixed with a truncation marker when shortened.
|
|
636
|
+
*/
|
|
637
|
+
function tailText(text, maxChars) {
|
|
638
|
+
const clean = text.replace(/\s+$/u, "");
|
|
639
|
+
if (clean.length <= maxChars) return clean;
|
|
640
|
+
return `…<truncated, showing last ${maxChars} chars>…${clean.slice(-maxChars)}`;
|
|
641
|
+
}
|
|
642
|
+
/**
|
|
643
|
+
* Replace every occurrence of one run's temp root with a stable placeholder,
|
|
644
|
+
* so recorded output never leaks machine-local absolute paths. Both
|
|
645
|
+
* separator styles are matched (Windows backslashes vs forward slashes).
|
|
646
|
+
*
|
|
647
|
+
* @param text - output to scrub.
|
|
648
|
+
* @param tempRoot - the exact temp root this run owns.
|
|
649
|
+
* @returns the scrubbed text.
|
|
650
|
+
*/
|
|
651
|
+
function redactTempPath(text, tempRoot) {
|
|
652
|
+
if (tempRoot.length === 0) return text;
|
|
653
|
+
const forward = tempRoot.replace(/\\/gu, "/");
|
|
654
|
+
const backward = tempRoot.replace(/\//gu, "\\");
|
|
655
|
+
return text.split(forward).join("<testdrive-temp>").split(backward).join("<testdrive-temp>");
|
|
656
|
+
}
|
|
657
|
+
/**
|
|
658
|
+
* Scrub one stage's collected output for a report field: redact the temp
|
|
659
|
+
* root, redact secrets, then keep only the bounded tail.
|
|
660
|
+
*
|
|
661
|
+
* @param text - raw collected output.
|
|
662
|
+
* @param tempRoot - this run's temp root ('' for none).
|
|
663
|
+
* @param maxChars - retained tail length.
|
|
664
|
+
* @returns the report-safe tail.
|
|
665
|
+
*/
|
|
666
|
+
function sanitizeOutput(text, tempRoot, maxChars) {
|
|
667
|
+
return tailText(redactSecrets(redactTempPath(text, tempRoot)), maxChars);
|
|
668
|
+
}
|
|
669
|
+
//#endregion
|
|
670
|
+
//#region src/version.ts
|
|
671
|
+
/**
|
|
672
|
+
* Producer version, kept in lockstep with `package.json` by
|
|
673
|
+
* `scripts/release.mjs`; `tests/version.spec.ts` trips when they drift.
|
|
674
|
+
* The structured report records it as `run.pluginVersion`.
|
|
675
|
+
* @module dsh-test-drive/version
|
|
676
|
+
*/
|
|
677
|
+
const VERSION = "0.1.0";
|
|
678
|
+
//#endregion
|
|
679
|
+
//#region src/drive.ts
|
|
680
|
+
/**
|
|
681
|
+
* The single-target drive pipeline: create an owned temp workspace, run
|
|
682
|
+
* install → dump-config → boot smoke → uninstall through the {@link DshDriver},
|
|
683
|
+
* and ALWAYS finish with the quarantine cleanup ladder — also on abort and
|
|
684
|
+
* unexpected pipeline errors (the `try/finally` discipline of §0.2). The
|
|
685
|
+
* resulting {@link DriveResult} is the structured record downstream consumers
|
|
686
|
+
* read.
|
|
687
|
+
*
|
|
688
|
+
* @module dsh-test-drive/drive
|
|
689
|
+
*/
|
|
690
|
+
/** id prefix of a single drive run. */
|
|
691
|
+
const RUN_ID_PREFIX = "tdr_";
|
|
692
|
+
/** id prefix of a batch matrix. */
|
|
693
|
+
const MATRIX_ID_PREFIX = "tdm_";
|
|
694
|
+
/** Fresh unique id with one of the plugin's prefixes. */
|
|
695
|
+
function freshId(prefix) {
|
|
696
|
+
return `${prefix}${randomBytes(12).toString("hex")}`;
|
|
697
|
+
}
|
|
698
|
+
/** Derive one line of batch progress from a settled result. */
|
|
699
|
+
function progressLine(index, total, result) {
|
|
700
|
+
return `[${index}/${total}] ${result.target.spec} → ${result.verdict} (${result.verdictReason})`;
|
|
701
|
+
}
|
|
702
|
+
/** Map one stage's process outcome to a plain stage record. */
|
|
703
|
+
function stageOf(run, tempRoot, outputTailBytes, okSummary) {
|
|
704
|
+
const outputTail = sanitizeOutput(`${run.stdout}\n${run.stderr}`, tempRoot, outputTailBytes);
|
|
705
|
+
if (run.timedOut) return {
|
|
706
|
+
status: "fail",
|
|
707
|
+
exitCode: run.exitCode,
|
|
708
|
+
durationMs: run.durationMs,
|
|
709
|
+
attempts: 1,
|
|
710
|
+
summary: `timed out after ${run.durationMs} ms`,
|
|
711
|
+
outputTail
|
|
712
|
+
};
|
|
713
|
+
if (run.exitCode === 0) return {
|
|
714
|
+
status: "pass",
|
|
715
|
+
exitCode: 0,
|
|
716
|
+
durationMs: run.durationMs,
|
|
717
|
+
attempts: 1,
|
|
718
|
+
summary: okSummary,
|
|
719
|
+
outputTail
|
|
720
|
+
};
|
|
721
|
+
const firstLine = tailText(run.stderr || run.stdout, 160).split("\n")[0] ?? "";
|
|
722
|
+
return {
|
|
723
|
+
status: "fail",
|
|
724
|
+
exitCode: run.exitCode,
|
|
725
|
+
durationMs: run.durationMs,
|
|
726
|
+
attempts: 1,
|
|
727
|
+
summary: `exit ${String(run.exitCode)}: ${firstLine || "no output"}`,
|
|
728
|
+
outputTail
|
|
729
|
+
};
|
|
730
|
+
}
|
|
731
|
+
/** Map the install stage, including the allowBuilds facts. */
|
|
732
|
+
function installStageOf(outcome, tempRoot, outputTailBytes) {
|
|
733
|
+
return {
|
|
734
|
+
...stageOf(outcome.run, tempRoot, outputTailBytes, outcome.allowBuildsNeeded ? "install ok after allowBuilds allowance" : "install ok (exit 0)"),
|
|
735
|
+
attempts: outcome.attempts,
|
|
736
|
+
allowBuildsNeeded: outcome.allowBuildsNeeded
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
/** The not-reached record for every stage skipped after a pipeline error. */
|
|
740
|
+
const UNREACHED = {
|
|
741
|
+
status: "skipped",
|
|
742
|
+
exitCode: null,
|
|
743
|
+
durationMs: 0,
|
|
744
|
+
attempts: 0,
|
|
745
|
+
outputTail: "",
|
|
746
|
+
summary: "not reached: earlier stage errored"
|
|
747
|
+
};
|
|
748
|
+
/** A failed stage record for an unexpected pipeline error (no process facts). */
|
|
749
|
+
function erroredStage(summary) {
|
|
750
|
+
return {
|
|
751
|
+
status: "fail",
|
|
752
|
+
exitCode: null,
|
|
753
|
+
durationMs: 0,
|
|
754
|
+
attempts: 1,
|
|
755
|
+
summary: tailText(summary, 160),
|
|
756
|
+
outputTail: ""
|
|
757
|
+
};
|
|
758
|
+
}
|
|
759
|
+
/**
|
|
760
|
+
* Drive one target through the complete install-smoke pipeline.
|
|
761
|
+
*
|
|
762
|
+
* @param target - raw target spec (repo, npm name, local path, tarball).
|
|
763
|
+
* @param options - per-drive overrides.
|
|
764
|
+
* @returns the structured result; never throws for expected stage failures.
|
|
765
|
+
*/
|
|
766
|
+
var DriveRunner = class {
|
|
767
|
+
deps;
|
|
768
|
+
harnessVersion;
|
|
769
|
+
constructor(deps) {
|
|
770
|
+
this.deps = deps;
|
|
771
|
+
}
|
|
772
|
+
/** Ask the CLI once per runner lifetime for its version. */
|
|
773
|
+
version(options) {
|
|
774
|
+
this.harnessVersion ??= this.deps.driver.version({
|
|
775
|
+
...options,
|
|
776
|
+
timeoutMs: this.deps.config.configTimeoutMs
|
|
777
|
+
}).catch(() => "");
|
|
778
|
+
return this.harnessVersion;
|
|
779
|
+
}
|
|
780
|
+
/** Persist one settled result; a failing medium is logged, never thrown. */
|
|
781
|
+
async record(result) {
|
|
782
|
+
try {
|
|
783
|
+
await (await this.deps.domain()).table("runs").put(result.run.runId, result);
|
|
784
|
+
} catch (error) {
|
|
785
|
+
this.deps.log(`test-drive: failed to persist run ${result.run.runId}: ${String(error)}`);
|
|
786
|
+
}
|
|
787
|
+
}
|
|
788
|
+
async drive(target, options = {}) {
|
|
789
|
+
const { config, driver, temp, log } = this.deps;
|
|
790
|
+
const startedAt = /* @__PURE__ */ new Date();
|
|
791
|
+
const startedMs = Date.now();
|
|
792
|
+
const runId = freshId(RUN_ID_PREFIX);
|
|
793
|
+
const spec = sanitizeTarget(target);
|
|
794
|
+
if (spec.length === 0) throw new Error("dsh-test-drive: target must be a non-empty string");
|
|
795
|
+
const kind = classifyTarget(spec);
|
|
796
|
+
const addSpec = kind === "path" ? anchorLocalTarget(spec) : spec;
|
|
797
|
+
const workspace = await temp.create(config.profileName);
|
|
798
|
+
const base = {
|
|
799
|
+
home: workspace.home,
|
|
800
|
+
cwd: workspace.workspace,
|
|
801
|
+
storeDir: workspace.store,
|
|
802
|
+
signal: options.signal
|
|
803
|
+
};
|
|
804
|
+
let install = {
|
|
805
|
+
...UNREACHED,
|
|
806
|
+
allowBuildsNeeded: false
|
|
807
|
+
};
|
|
808
|
+
let configStage = {
|
|
809
|
+
...UNREACHED,
|
|
810
|
+
patchEffective: false,
|
|
811
|
+
layers: []
|
|
812
|
+
};
|
|
813
|
+
let smoke = {
|
|
814
|
+
...UNREACHED,
|
|
815
|
+
bootFailed: false,
|
|
816
|
+
taskCompleted: false
|
|
817
|
+
};
|
|
818
|
+
let uninstall = { ...UNREACHED };
|
|
819
|
+
let resolved;
|
|
820
|
+
let cleanup = {
|
|
821
|
+
status: "fail",
|
|
822
|
+
quarantined: false,
|
|
823
|
+
removed: false,
|
|
824
|
+
summary: "cleanup not executed"
|
|
825
|
+
};
|
|
826
|
+
let harnessVersion = "";
|
|
827
|
+
try {
|
|
828
|
+
install = installStageOf(await driver.add(addSpec, {
|
|
829
|
+
...base,
|
|
830
|
+
timeoutMs: config.installTimeoutMs
|
|
831
|
+
}), workspace.root, config.outputTailBytes);
|
|
832
|
+
if (install.status === "pass") resolved = await driver.readInstalledPackage(workspace.profileDir).catch(() => void 0);
|
|
833
|
+
if (install.status === "pass" && !(options.signal?.aborted ?? false)) {
|
|
834
|
+
const dump = await driver.dumpConfig({
|
|
835
|
+
...base,
|
|
836
|
+
timeoutMs: config.configTimeoutMs
|
|
837
|
+
});
|
|
838
|
+
const stage = stageOf(dump, workspace.root, config.outputTailBytes, "dump ok (exit 0)");
|
|
839
|
+
const pkgName = resolved?.packageName ?? "";
|
|
840
|
+
const mention = pkgName === "" ? {
|
|
841
|
+
mentioned: false,
|
|
842
|
+
layers: []
|
|
843
|
+
} : dumpMentionsPackage(`${dump.stdout}\n${dump.stderr}`, pkgName);
|
|
844
|
+
configStage = {
|
|
845
|
+
...stage,
|
|
846
|
+
patchEffective: mention.mentioned,
|
|
847
|
+
layers: mention.layers
|
|
848
|
+
};
|
|
849
|
+
} else if (options.signal?.aborted ?? false) configStage = {
|
|
850
|
+
...configStage,
|
|
851
|
+
summary: "run aborted before this stage"
|
|
852
|
+
};
|
|
853
|
+
else configStage = {
|
|
854
|
+
...configStage,
|
|
855
|
+
summary: "install did not pass"
|
|
856
|
+
};
|
|
857
|
+
const task = options.headlessTask ?? config.headlessTask;
|
|
858
|
+
if (task === "") smoke = {
|
|
859
|
+
...smoke,
|
|
860
|
+
summary: "boot smoke disabled by config (headlessTask is empty)"
|
|
861
|
+
};
|
|
862
|
+
else if (install.status !== "pass") smoke = {
|
|
863
|
+
...smoke,
|
|
864
|
+
summary: "install did not pass"
|
|
865
|
+
};
|
|
866
|
+
else if (options.signal?.aborted ?? false) smoke = {
|
|
867
|
+
...smoke,
|
|
868
|
+
summary: "run aborted before this stage"
|
|
869
|
+
};
|
|
870
|
+
else {
|
|
871
|
+
const run = await driver.smokeTask(task, {
|
|
872
|
+
...base,
|
|
873
|
+
timeoutMs: config.smokeTimeoutMs
|
|
874
|
+
});
|
|
875
|
+
const outputTail = sanitizeOutput(`${run.stdout}\n${run.stderr}`, workspace.root, config.outputTailBytes);
|
|
876
|
+
const bootFailed = hasBootFailure(`${run.stdout}\n${run.stderr}`);
|
|
877
|
+
if (run.timedOut) smoke = {
|
|
878
|
+
status: "fail",
|
|
879
|
+
exitCode: run.exitCode,
|
|
880
|
+
durationMs: run.durationMs,
|
|
881
|
+
attempts: 1,
|
|
882
|
+
summary: `timed out after ${run.durationMs} ms`,
|
|
883
|
+
outputTail,
|
|
884
|
+
bootFailed: false,
|
|
885
|
+
taskCompleted: false
|
|
886
|
+
};
|
|
887
|
+
else if (bootFailed) smoke = {
|
|
888
|
+
status: "fail",
|
|
889
|
+
exitCode: run.exitCode,
|
|
890
|
+
durationMs: run.durationMs,
|
|
891
|
+
attempts: 1,
|
|
892
|
+
summary: "boot failure markers found (FAILED / failed to load / did not activate)",
|
|
893
|
+
outputTail,
|
|
894
|
+
bootFailed: true,
|
|
895
|
+
taskCompleted: false
|
|
896
|
+
};
|
|
897
|
+
else if (run.exitCode === 0) smoke = {
|
|
898
|
+
status: "pass",
|
|
899
|
+
exitCode: 0,
|
|
900
|
+
durationMs: run.durationMs,
|
|
901
|
+
attempts: 1,
|
|
902
|
+
summary: "booted cleanly and the headless task completed",
|
|
903
|
+
outputTail,
|
|
904
|
+
bootFailed: false,
|
|
905
|
+
taskCompleted: true
|
|
906
|
+
};
|
|
907
|
+
else smoke = {
|
|
908
|
+
status: "boot-ok",
|
|
909
|
+
exitCode: run.exitCode,
|
|
910
|
+
durationMs: run.durationMs,
|
|
911
|
+
attempts: 1,
|
|
912
|
+
summary: "booted without loader failures; headless task did not complete (credentials/model unreachable)",
|
|
913
|
+
outputTail,
|
|
914
|
+
bootFailed: false,
|
|
915
|
+
taskCompleted: false
|
|
916
|
+
};
|
|
917
|
+
}
|
|
918
|
+
if (resolved?.packageName === void 0) uninstall = {
|
|
919
|
+
...uninstall,
|
|
920
|
+
summary: "no installed package to remove"
|
|
921
|
+
};
|
|
922
|
+
else uninstall = stageOf(await driver.remove(resolved.packageName, {
|
|
923
|
+
...base,
|
|
924
|
+
timeoutMs: config.uninstallTimeoutMs
|
|
925
|
+
}), workspace.root, config.outputTailBytes, "remove ok (exit 0)");
|
|
926
|
+
harnessVersion = await this.version(base).catch(() => "");
|
|
927
|
+
} catch (error) {
|
|
928
|
+
const summary = `pipeline error: ${tailText(String(error), 160)}`;
|
|
929
|
+
if (install.status === "skipped") install = {
|
|
930
|
+
...erroredStage(summary),
|
|
931
|
+
allowBuildsNeeded: false
|
|
932
|
+
};
|
|
933
|
+
else if (configStage.status === "skipped") configStage = {
|
|
934
|
+
...erroredStage(summary),
|
|
935
|
+
patchEffective: false,
|
|
936
|
+
layers: []
|
|
937
|
+
};
|
|
938
|
+
else if (smoke.status === "skipped") smoke = {
|
|
939
|
+
...erroredStage(summary),
|
|
940
|
+
bootFailed: false,
|
|
941
|
+
taskCompleted: false
|
|
942
|
+
};
|
|
943
|
+
else uninstall = { ...erroredStage(summary) };
|
|
944
|
+
log(`test-drive: pipeline error for ${spec}: ${String(error)}`);
|
|
945
|
+
} finally {
|
|
946
|
+
try {
|
|
947
|
+
cleanup = config.keepTempDirs ? temp.keep(workspace.root) : await temp.quarantineAndRemove(workspace.root);
|
|
948
|
+
} catch (error) {
|
|
949
|
+
log(`test-drive: cleanup ladder failed for ${workspace.root}: ${String(error)}`);
|
|
950
|
+
cleanup = {
|
|
951
|
+
status: "fail",
|
|
952
|
+
quarantined: false,
|
|
953
|
+
removed: false,
|
|
954
|
+
summary: `cleanup refused: ${tailText(String(error), 120)}`
|
|
955
|
+
};
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
const finishedAt = /* @__PURE__ */ new Date();
|
|
959
|
+
const result = {
|
|
960
|
+
schema: RESULT_SCHEMA,
|
|
961
|
+
run: {
|
|
962
|
+
runId,
|
|
963
|
+
startedAt: startedAt.toISOString(),
|
|
964
|
+
finishedAt: finishedAt.toISOString(),
|
|
965
|
+
durationMs: Date.now() - startedMs,
|
|
966
|
+
harnessVersion,
|
|
967
|
+
pluginVersion: VERSION,
|
|
968
|
+
platform: process.platform,
|
|
969
|
+
node: process.version
|
|
970
|
+
},
|
|
971
|
+
target: {
|
|
972
|
+
kind,
|
|
973
|
+
spec: addSpec,
|
|
974
|
+
...resolved === void 0 ? {} : { resolved }
|
|
975
|
+
},
|
|
976
|
+
isolation: {
|
|
977
|
+
tempDshHome: true,
|
|
978
|
+
tempWorkspace: true,
|
|
979
|
+
tempStore: true,
|
|
980
|
+
hostHomeTouched: false
|
|
981
|
+
},
|
|
982
|
+
stages: {
|
|
983
|
+
install,
|
|
984
|
+
config: configStage,
|
|
985
|
+
smoke,
|
|
986
|
+
uninstall,
|
|
987
|
+
cleanup
|
|
988
|
+
},
|
|
989
|
+
verdict: "unknown",
|
|
990
|
+
verdictReason: ""
|
|
991
|
+
};
|
|
992
|
+
const [verdict, reason] = verdictOf(result);
|
|
993
|
+
result.verdict = verdict;
|
|
994
|
+
result.verdictReason = reason;
|
|
995
|
+
await this.record(result);
|
|
996
|
+
return result;
|
|
997
|
+
}
|
|
998
|
+
};
|
|
999
|
+
//#endregion
|
|
1000
|
+
//#region src/batch.ts
|
|
1001
|
+
/** The job kind this producer registers (declaration-merged into JobKindMap). */
|
|
1002
|
+
const DRIVE_BATCH_KIND = "drive-batch";
|
|
1003
|
+
/** The batch work function shared by the job producer and direct tests. */
|
|
1004
|
+
async function runBatch(deps, targets, signal, onProgress) {
|
|
1005
|
+
const { config, log } = deps;
|
|
1006
|
+
const runner = new DriveRunner(deps);
|
|
1007
|
+
const matrixId = freshId(MATRIX_ID_PREFIX);
|
|
1008
|
+
const startedMs = Date.now();
|
|
1009
|
+
const rows = [];
|
|
1010
|
+
const queue = [...targets];
|
|
1011
|
+
async function worker() {
|
|
1012
|
+
for (;;) {
|
|
1013
|
+
if (signal.aborted) return;
|
|
1014
|
+
const target = queue.shift();
|
|
1015
|
+
if (target === void 0) return;
|
|
1016
|
+
const result = await runner.drive(target, { signal });
|
|
1017
|
+
rows.push({
|
|
1018
|
+
target: result.target.spec,
|
|
1019
|
+
kind: result.target.kind,
|
|
1020
|
+
verdict: result.verdict,
|
|
1021
|
+
install: result.stages.install.status,
|
|
1022
|
+
smoke: result.stages.smoke.status,
|
|
1023
|
+
durationMs: result.run.durationMs,
|
|
1024
|
+
summary: result.verdictReason
|
|
1025
|
+
});
|
|
1026
|
+
onProgress(progressLine(rows.length, targets.length, result));
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
const workers = Array.from({ length: config.batchConcurrency }, () => worker());
|
|
1030
|
+
await Promise.all(workers);
|
|
1031
|
+
const matrix = {
|
|
1032
|
+
schema: RESULT_SCHEMA,
|
|
1033
|
+
id: matrixId,
|
|
1034
|
+
createdAt: (/* @__PURE__ */ new Date()).toISOString(),
|
|
1035
|
+
durationMs: Date.now() - startedMs,
|
|
1036
|
+
rows,
|
|
1037
|
+
totals: totalsOf(rows)
|
|
1038
|
+
};
|
|
1039
|
+
try {
|
|
1040
|
+
const domain = await deps.domain();
|
|
1041
|
+
await domain.table("matrices").put(matrixId, matrix);
|
|
1042
|
+
await domain.global.set({
|
|
1043
|
+
matrixId,
|
|
1044
|
+
createdAt: matrix.createdAt
|
|
1045
|
+
});
|
|
1046
|
+
} catch (error) {
|
|
1047
|
+
log(`test-drive: failed to persist matrix ${matrixId}: ${String(error)}`);
|
|
1048
|
+
}
|
|
1049
|
+
return {
|
|
1050
|
+
matrixId,
|
|
1051
|
+
matrix
|
|
1052
|
+
};
|
|
1053
|
+
}
|
|
1054
|
+
/** One-line settlement summary for notices and the final flush. */
|
|
1055
|
+
function matrixSummary(matrix) {
|
|
1056
|
+
const { totals } = matrix;
|
|
1057
|
+
return `matrix ${matrix.id}: ${totals.pass} pass, ${totals.fail} fail, ${totals.partial} partial, ${totals.unknown} unknown (of ${totals.total})`;
|
|
1058
|
+
}
|
|
1059
|
+
/**
|
|
1060
|
+
* Start one `drive-batch` background job. The returned id doubles as the
|
|
1061
|
+
* handle the model uses to read output and, via the final flush line, to find
|
|
1062
|
+
* the matrix record for `drive_report`.
|
|
1063
|
+
*
|
|
1064
|
+
* @param deps - batch dependencies.
|
|
1065
|
+
* @param targets - sanitized target specs.
|
|
1066
|
+
* @param owner - the owning agent (from the command invocation).
|
|
1067
|
+
* @param label - one-line model-facing label.
|
|
1068
|
+
* @returns the registry-issued job id.
|
|
1069
|
+
*/
|
|
1070
|
+
function startBatchJob(deps, targets, owner, label) {
|
|
1071
|
+
const sanitized = targets.map(sanitizeTarget).filter((spec) => spec.length > 0);
|
|
1072
|
+
const kind = DRIVE_BATCH_KIND;
|
|
1073
|
+
return deps.ctx.jobs.start({
|
|
1074
|
+
kind,
|
|
1075
|
+
label,
|
|
1076
|
+
owner,
|
|
1077
|
+
run: () => {
|
|
1078
|
+
const abort = new AbortController();
|
|
1079
|
+
const progress = [];
|
|
1080
|
+
const done = Promise.withResolvers();
|
|
1081
|
+
let settled = false;
|
|
1082
|
+
const settle = (outcome) => {
|
|
1083
|
+
if (settled) return outcome;
|
|
1084
|
+
settled = true;
|
|
1085
|
+
progress.push(`${outcome.status === "completed" ? "batch finished" : "batch stopped"}: ${outcome.detail ?? ""}`);
|
|
1086
|
+
done.resolve(outcome);
|
|
1087
|
+
return outcome;
|
|
1088
|
+
};
|
|
1089
|
+
runBatch(deps, sanitized, abort.signal, (line) => {
|
|
1090
|
+
progress.push(line);
|
|
1091
|
+
}).then(({ matrixId, matrix }) => {
|
|
1092
|
+
progress.push(`matrix ${matrixId} — fetch with drive_report("${matrixId}")`);
|
|
1093
|
+
settle({
|
|
1094
|
+
status: "completed",
|
|
1095
|
+
detail: matrixSummary(matrix)
|
|
1096
|
+
});
|
|
1097
|
+
}).catch((error) => {
|
|
1098
|
+
deps.log(`test-drive: batch job failed: ${String(error)}`);
|
|
1099
|
+
settle({
|
|
1100
|
+
status: "failed",
|
|
1101
|
+
detail: tailDetail(String(error))
|
|
1102
|
+
});
|
|
1103
|
+
});
|
|
1104
|
+
return {
|
|
1105
|
+
cancel(reason) {
|
|
1106
|
+
abort.abort(reason ?? "cancelled");
|
|
1107
|
+
progress.push(`cancelling (${reason ?? "no reason given"})`);
|
|
1108
|
+
settle({
|
|
1109
|
+
status: "killed",
|
|
1110
|
+
detail: `cancelled: ${reason ?? "no reason given"}`
|
|
1111
|
+
});
|
|
1112
|
+
},
|
|
1113
|
+
done: done.promise,
|
|
1114
|
+
readOutput: () => {
|
|
1115
|
+
if (progress.length === 0) return "";
|
|
1116
|
+
return `${progress.splice(0, progress.length).join("\n")}\n`;
|
|
1117
|
+
}
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
});
|
|
1121
|
+
}
|
|
1122
|
+
/** Bound an error message for job detail lines. */
|
|
1123
|
+
function tailDetail(text) {
|
|
1124
|
+
const clean = text.replace(/\s+/gu, " ").trim();
|
|
1125
|
+
return clean.length <= 200 ? clean : `${clean.slice(0, 197)}…`;
|
|
1126
|
+
}
|
|
1127
|
+
//#endregion
|
|
1128
|
+
//#region src/command.ts
|
|
1129
|
+
/** Split raw command input into target specs (whitespace and commas separate). */
|
|
1130
|
+
function parseTargets(rawInput) {
|
|
1131
|
+
return rawInput.split(/[\s,]+/u).map((token) => token.trim()).filter((token) => token.length > 0);
|
|
1132
|
+
}
|
|
1133
|
+
/**
|
|
1134
|
+
* Execute `/testdrive <targets...>`: validate the target list against the
|
|
1135
|
+
* configured batch cap and start the batch job for the invoking agent.
|
|
1136
|
+
*
|
|
1137
|
+
* @param deps - batch dependencies.
|
|
1138
|
+
* @param invocation - the command invocation.
|
|
1139
|
+
* @returns the command result naming the started job.
|
|
1140
|
+
*/
|
|
1141
|
+
function handleTestdrive(deps, invocation) {
|
|
1142
|
+
const targets = parseTargets(invocation.rawInput);
|
|
1143
|
+
if (targets.length === 0) return {
|
|
1144
|
+
kind: "error",
|
|
1145
|
+
text: "Usage: /testdrive <target> [<target> ...] — one or more plugin repos, npm packages, paths, or tarballs"
|
|
1146
|
+
};
|
|
1147
|
+
if (targets.length > deps.config.maxBatchTargets) return {
|
|
1148
|
+
kind: "error",
|
|
1149
|
+
text: `Too many targets: ${targets.length} exceeds the configured batch cap of ${deps.config.maxBatchTargets}`
|
|
1150
|
+
};
|
|
1151
|
+
const jobId = startBatchJob(deps, targets, invocation.agent, `/testdrive ${targets.join(" ")}`);
|
|
1152
|
+
return {
|
|
1153
|
+
kind: "success",
|
|
1154
|
+
text: `Started background batch job ${String(jobId)} for ${targets.length} target(s). Progress streams through the job output; the final line names the matrix id for drive_report.`
|
|
1155
|
+
};
|
|
1156
|
+
}
|
|
1157
|
+
//#endregion
|
|
1158
|
+
//#region src/config.ts
|
|
1159
|
+
/**
|
|
1160
|
+
* Plugin configuration and its explicit resolve step. `resolveConfig` re-judges
|
|
1161
|
+
* every default and bound so programmatic construction that bypasses
|
|
1162
|
+
* Schemastery normalization still fails loud instead of running with hidden
|
|
1163
|
+
* defaults (the explicit-resolve contract).
|
|
1164
|
+
*
|
|
1165
|
+
* @module dsh-test-drive/config
|
|
1166
|
+
*/
|
|
1167
|
+
/** Default profile template used inside each throwaway DSH_HOME. */
|
|
1168
|
+
const DEFAULT_PROFILE_NAME = "headless";
|
|
1169
|
+
/** Default headless task text; the boot smoke runs a one-shot task with it. */
|
|
1170
|
+
const DEFAULT_HEADLESS_TASK = "Reply with exactly: ok";
|
|
1171
|
+
/** Maximum length of the user-supplied headless task. */
|
|
1172
|
+
const MAX_HEADLESS_TASK_LENGTH = 2e3;
|
|
1173
|
+
/** Default per-stage timeout in milliseconds for the install (`dsh plugin add`). */
|
|
1174
|
+
const DEFAULT_INSTALL_TIMEOUT_MS = 6e5;
|
|
1175
|
+
/** Default per-stage timeout for `--dump-config`. */
|
|
1176
|
+
const DEFAULT_CONFIG_TIMEOUT_MS = 6e4;
|
|
1177
|
+
/** Default per-stage timeout for the headless boot smoke. */
|
|
1178
|
+
const DEFAULT_SMOKE_TIMEOUT_MS = 3e5;
|
|
1179
|
+
/** Default per-stage timeout for `dsh plugin remove`. */
|
|
1180
|
+
const DEFAULT_UNINSTALL_TIMEOUT_MS = 12e4;
|
|
1181
|
+
/** Bounds shared by every stage timeout. */
|
|
1182
|
+
const MIN_STAGE_TIMEOUT_MS = 1e3;
|
|
1183
|
+
const MAX_STAGE_TIMEOUT_MS = 36e5;
|
|
1184
|
+
/** Default cap on the sanitized output tail recorded per stage. */
|
|
1185
|
+
const DEFAULT_OUTPUT_TAIL_BYTES = 8e3;
|
|
1186
|
+
const MAX_OUTPUT_TAIL_BYTES = 256e3;
|
|
1187
|
+
/** Default: a forwarded environment-name list is empty, so child profiles never see host credentials. */
|
|
1188
|
+
const DEFAULT_FORWARD_ENV = [];
|
|
1189
|
+
/** Schemastery schema for loader-validated configuration. */
|
|
1190
|
+
const Config = z$1.object({
|
|
1191
|
+
profileName: z$1.string().default(DEFAULT_PROFILE_NAME),
|
|
1192
|
+
dshBin: z$1.string().default(""),
|
|
1193
|
+
headlessTask: z$1.string().default(DEFAULT_HEADLESS_TASK),
|
|
1194
|
+
forwardEnv: z$1.array(z$1.string()).default([...DEFAULT_FORWARD_ENV]),
|
|
1195
|
+
allowBuilds: z$1.boolean().default(true),
|
|
1196
|
+
installTimeoutMs: z$1.number().min(MIN_STAGE_TIMEOUT_MS).max(MAX_STAGE_TIMEOUT_MS).default(DEFAULT_INSTALL_TIMEOUT_MS),
|
|
1197
|
+
configTimeoutMs: z$1.number().min(MIN_STAGE_TIMEOUT_MS).max(MAX_STAGE_TIMEOUT_MS).default(DEFAULT_CONFIG_TIMEOUT_MS),
|
|
1198
|
+
smokeTimeoutMs: z$1.number().min(MIN_STAGE_TIMEOUT_MS).max(MAX_STAGE_TIMEOUT_MS).default(DEFAULT_SMOKE_TIMEOUT_MS),
|
|
1199
|
+
uninstallTimeoutMs: z$1.number().min(MIN_STAGE_TIMEOUT_MS).max(MAX_STAGE_TIMEOUT_MS).default(DEFAULT_UNINSTALL_TIMEOUT_MS),
|
|
1200
|
+
outputTailBytes: z$1.number().min(256).max(MAX_OUTPUT_TAIL_BYTES).default(DEFAULT_OUTPUT_TAIL_BYTES),
|
|
1201
|
+
keepTempDirs: z$1.boolean().default(false),
|
|
1202
|
+
maxBatchTargets: z$1.number().min(1).max(200).default(20),
|
|
1203
|
+
batchConcurrency: z$1.number().min(1).max(4).default(1)
|
|
1204
|
+
});
|
|
1205
|
+
/** Throw the standard fail-loud config error for one invalid field. */
|
|
1206
|
+
function invalid(field, detail) {
|
|
1207
|
+
throw new Error(`dsh-test-drive: config.${field} ${detail}`);
|
|
1208
|
+
}
|
|
1209
|
+
/**
|
|
1210
|
+
* Resolve raw config to the runtime policy, re-validating defaults and bounds.
|
|
1211
|
+
*
|
|
1212
|
+
* @param config - raw loader config; `undefined` for a bare row.
|
|
1213
|
+
* @returns the frozen resolved config.
|
|
1214
|
+
*/
|
|
1215
|
+
function resolveConfig(config) {
|
|
1216
|
+
const profileName = config?.profileName ?? "headless";
|
|
1217
|
+
if (profileName.length === 0) invalid("profileName", "must be a non-empty profile template name");
|
|
1218
|
+
if (!/^[a-zA-Z0-9._-]+$/.test(profileName)) invalid("profileName", `contains unsupported characters (got ${JSON.stringify(profileName)})`);
|
|
1219
|
+
const dshBin = config?.dshBin ?? "";
|
|
1220
|
+
if (typeof dshBin !== "string") invalid("dshBin", "must be a string path or empty");
|
|
1221
|
+
const headlessTask = config?.headlessTask ?? "Reply with exactly: ok";
|
|
1222
|
+
if (typeof headlessTask !== "string" || headlessTask.length > 2e3) invalid("headlessTask", `must be a string of at most ${MAX_HEADLESS_TASK_LENGTH} characters (empty skips the smoke stage)`);
|
|
1223
|
+
const forwardEnv = [...config?.forwardEnv ?? DEFAULT_FORWARD_ENV];
|
|
1224
|
+
for (const name of forwardEnv) if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) invalid("forwardEnv", `entry ${JSON.stringify(name)} is not a valid environment variable name`);
|
|
1225
|
+
const allowBuilds = config?.allowBuilds ?? true;
|
|
1226
|
+
if (typeof allowBuilds !== "boolean") invalid("allowBuilds", "must be a boolean");
|
|
1227
|
+
const installTimeoutMs = config?.installTimeoutMs ?? 6e5;
|
|
1228
|
+
if (!Number.isFinite(installTimeoutMs) || installTimeoutMs < 1e3 || installTimeoutMs > 36e5) invalid("installTimeoutMs", `must be a finite number between ${MIN_STAGE_TIMEOUT_MS} and ${MAX_STAGE_TIMEOUT_MS}`);
|
|
1229
|
+
const configTimeoutMs = config?.configTimeoutMs ?? 6e4;
|
|
1230
|
+
if (!Number.isFinite(configTimeoutMs) || configTimeoutMs < 1e3 || configTimeoutMs > 36e5) invalid("configTimeoutMs", `must be a finite number between ${MIN_STAGE_TIMEOUT_MS} and ${MAX_STAGE_TIMEOUT_MS}`);
|
|
1231
|
+
const smokeTimeoutMs = config?.smokeTimeoutMs ?? 3e5;
|
|
1232
|
+
if (!Number.isFinite(smokeTimeoutMs) || smokeTimeoutMs < 1e3 || smokeTimeoutMs > 36e5) invalid("smokeTimeoutMs", `must be a finite number between ${MIN_STAGE_TIMEOUT_MS} and ${MAX_STAGE_TIMEOUT_MS}`);
|
|
1233
|
+
const uninstallTimeoutMs = config?.uninstallTimeoutMs ?? 12e4;
|
|
1234
|
+
if (!Number.isFinite(uninstallTimeoutMs) || uninstallTimeoutMs < 1e3 || uninstallTimeoutMs > 36e5) invalid("uninstallTimeoutMs", `must be a finite number between ${MIN_STAGE_TIMEOUT_MS} and ${MAX_STAGE_TIMEOUT_MS}`);
|
|
1235
|
+
const outputTailBytes = config?.outputTailBytes ?? 8e3;
|
|
1236
|
+
if (!Number.isInteger(outputTailBytes) || outputTailBytes < 256 || outputTailBytes > 256e3) invalid("outputTailBytes", `must be an integer between 256 and ${MAX_OUTPUT_TAIL_BYTES}`);
|
|
1237
|
+
const keepTempDirs = config?.keepTempDirs ?? false;
|
|
1238
|
+
if (typeof keepTempDirs !== "boolean") invalid("keepTempDirs", "must be a boolean");
|
|
1239
|
+
const maxBatchTargets = config?.maxBatchTargets ?? 20;
|
|
1240
|
+
if (!Number.isInteger(maxBatchTargets) || maxBatchTargets < 1 || maxBatchTargets > 200) invalid("maxBatchTargets", `must be an integer between 1 and 200`);
|
|
1241
|
+
const batchConcurrency = config?.batchConcurrency ?? 1;
|
|
1242
|
+
if (!Number.isInteger(batchConcurrency) || batchConcurrency < 1 || batchConcurrency > 4) invalid("batchConcurrency", `must be an integer between 1 and 4`);
|
|
1243
|
+
return Object.freeze({
|
|
1244
|
+
profileName,
|
|
1245
|
+
dshBin,
|
|
1246
|
+
headlessTask,
|
|
1247
|
+
forwardEnv: Object.freeze(forwardEnv),
|
|
1248
|
+
allowBuilds,
|
|
1249
|
+
installTimeoutMs,
|
|
1250
|
+
configTimeoutMs,
|
|
1251
|
+
smokeTimeoutMs,
|
|
1252
|
+
uninstallTimeoutMs,
|
|
1253
|
+
outputTailBytes,
|
|
1254
|
+
keepTempDirs,
|
|
1255
|
+
maxBatchTargets,
|
|
1256
|
+
batchConcurrency
|
|
1257
|
+
});
|
|
1258
|
+
}
|
|
1259
|
+
//#endregion
|
|
1260
|
+
//#region src/domain.ts
|
|
1261
|
+
/**
|
|
1262
|
+
* The `test-drive` storage domain: the durable home of drive-run records and
|
|
1263
|
+
* batch matrices. Opened through `ctx.storageDomain` (a public service), with
|
|
1264
|
+
* record schemas validated at the durable boundary; the domain stays open for
|
|
1265
|
+
* the plugin's lifetime and closes through the registered effect.
|
|
1266
|
+
*
|
|
1267
|
+
* @module dsh-test-drive/domain
|
|
1268
|
+
*/
|
|
1269
|
+
/** Domain name (also the backend unit name; UNIT_NAME_RE forbids hyphens). */
|
|
1270
|
+
const DOMAIN_NAME = "test_drive";
|
|
1271
|
+
/** Domain format version; bump when a record format changes incompatibly. */
|
|
1272
|
+
const DOMAIN_VERSION = 1;
|
|
1273
|
+
/** Latest-matrix pointer value; `''` id means "no matrix yet". */
|
|
1274
|
+
const LatestMatrixSchema = z.object({
|
|
1275
|
+
matrixId: z.string(),
|
|
1276
|
+
createdAt: z.string()
|
|
1277
|
+
});
|
|
1278
|
+
/** Storage-domain spec: one table of per-run records, one of batch matrices, plus the latest pointer. */
|
|
1279
|
+
const driveDomainSpec = defineDomain({
|
|
1280
|
+
name: DOMAIN_NAME,
|
|
1281
|
+
version: 1,
|
|
1282
|
+
global: {
|
|
1283
|
+
schema: LatestMatrixSchema,
|
|
1284
|
+
initial: {
|
|
1285
|
+
matrixId: "",
|
|
1286
|
+
createdAt: ""
|
|
1287
|
+
}
|
|
1288
|
+
},
|
|
1289
|
+
tables: {
|
|
1290
|
+
runs: domainTable(DriveResultSchema),
|
|
1291
|
+
matrices: domainTable(MatrixRecordSchema)
|
|
1292
|
+
}
|
|
1293
|
+
});
|
|
1294
|
+
//#endregion
|
|
1295
|
+
//#region src/report.ts
|
|
1296
|
+
/** Emoji/symbol mark for a stage or verdict status. */
|
|
1297
|
+
function statusMark(status) {
|
|
1298
|
+
switch (status) {
|
|
1299
|
+
case "pass": return "✅";
|
|
1300
|
+
case "fail": return "❌";
|
|
1301
|
+
case "boot-ok": return "🟡";
|
|
1302
|
+
case "partial": return "🟠";
|
|
1303
|
+
case "skipped": return "⏭️";
|
|
1304
|
+
default: return "❓";
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
/** Human-duration rendering for one number of milliseconds. */
|
|
1308
|
+
function formatDuration(ms) {
|
|
1309
|
+
if (ms < 1e3) return `${ms} ms`;
|
|
1310
|
+
return `${(ms / 1e3).toFixed(1)} s`;
|
|
1311
|
+
}
|
|
1312
|
+
/** Render one drive result as a Markdown report block. */
|
|
1313
|
+
function renderDriveResult(result) {
|
|
1314
|
+
const { run, target, stages, verdict, verdictReason } = result;
|
|
1315
|
+
const lines = [
|
|
1316
|
+
`# Test drive ${run.runId}`,
|
|
1317
|
+
"",
|
|
1318
|
+
`- Target: \`${target.spec}\` (${target.kind})`,
|
|
1319
|
+
`- Verdict: ${statusMark(verdict)} ${verdict} — ${verdictReason}`,
|
|
1320
|
+
`- Duration: ${formatDuration(run.durationMs)} (harness ${run.harnessVersion || "unknown"}, plugin ${run.pluginVersion})`,
|
|
1321
|
+
...target.resolved != null ? [`- Installed: ${target.resolved.packageName}@${target.resolved.packageVersion}${target.resolved.hasBundleManifest ? "" : " (no dsh.bundle manifest)"}`] : [],
|
|
1322
|
+
`- Isolation: throwaway DSH_HOME + workspace + pnpm store; host profile untouched`,
|
|
1323
|
+
"",
|
|
1324
|
+
"| Stage | Result | Exit | Duration | Summary |",
|
|
1325
|
+
"|---|---|---|---|---|",
|
|
1326
|
+
`| install | ${statusMark(stages.install.status)} ${stages.install.status} | ${String(stages.install.exitCode)} | ${formatDuration(stages.install.durationMs)} | ${stages.install.summary} |`,
|
|
1327
|
+
`| config (--dump-config) | ${statusMark(stages.config.status)} ${stages.config.status} | ${String(stages.config.exitCode)} | ${formatDuration(stages.config.durationMs)} | ${stages.config.patchEffective ? `patch effective: ${stages.config.layers.join(", ")}` : stages.config.summary} |`,
|
|
1328
|
+
`| smoke (headless boot) | ${statusMark(stages.smoke.status)} ${stages.smoke.status} | ${String(stages.smoke.exitCode)} | ${formatDuration(stages.smoke.durationMs)} | ${stages.smoke.summary} |`,
|
|
1329
|
+
`| uninstall | ${statusMark(stages.uninstall.status)} ${stages.uninstall.status} | ${String(stages.uninstall.exitCode)} | ${formatDuration(stages.uninstall.durationMs)} | ${stages.uninstall.summary} |`,
|
|
1330
|
+
`| cleanup | ${statusMark(stages.cleanup.status)} ${stages.cleanup.status} | — | — | ${stages.cleanup.summary} |`,
|
|
1331
|
+
""
|
|
1332
|
+
];
|
|
1333
|
+
if (stages.install.status === "fail" && stages.install.outputTail !== "") lines.push("## Install output (tail, sanitized)", "", "```", stages.install.outputTail, "```", "");
|
|
1334
|
+
if ((stages.smoke.status === "fail" || stages.smoke.status === "boot-ok") && stages.smoke.outputTail !== "") lines.push("## Smoke output (tail, sanitized)", "", "```", stages.smoke.outputTail, "```", "");
|
|
1335
|
+
return lines.join("\n");
|
|
1336
|
+
}
|
|
1337
|
+
/** Render one batch matrix as a Markdown report. */
|
|
1338
|
+
function renderMatrix(matrix) {
|
|
1339
|
+
const lines = [
|
|
1340
|
+
`# Test drive matrix ${matrix.id}`,
|
|
1341
|
+
"",
|
|
1342
|
+
`- Created: ${matrix.createdAt}`,
|
|
1343
|
+
`- Duration: ${formatDuration(matrix.durationMs)}`,
|
|
1344
|
+
`- Totals: ${matrix.totals.pass} pass, ${matrix.totals.fail} fail, ${matrix.totals.partial} partial, ${matrix.totals.unknown} unknown (of ${matrix.totals.total})`,
|
|
1345
|
+
"",
|
|
1346
|
+
"| # | Target | Install | Smoke | Verdict | Duration | Summary |",
|
|
1347
|
+
"|---|---|---|---|---|---|---|"
|
|
1348
|
+
];
|
|
1349
|
+
matrix.rows.forEach((row, index) => {
|
|
1350
|
+
lines.push(`| ${index + 1} | \`${row.target}\` | ${statusMark(row.install)} ${row.install} | ${statusMark(row.smoke)} ${row.smoke} | ${statusMark(row.verdict)} ${row.verdict} | ${formatDuration(row.durationMs)} | ${row.summary} |`);
|
|
1351
|
+
});
|
|
1352
|
+
const failures = matrix.rows.filter((row) => row.verdict === "fail" || row.verdict === "partial");
|
|
1353
|
+
if (failures.length > 0) {
|
|
1354
|
+
lines.push("", "## Attention", "");
|
|
1355
|
+
for (const row of failures) lines.push(`- ${statusMark(row.verdict)} \`${row.target}\`: ${row.summary}`);
|
|
1356
|
+
}
|
|
1357
|
+
return lines.join("\n");
|
|
1358
|
+
}
|
|
1359
|
+
//#endregion
|
|
1360
|
+
//#region src/tools.ts
|
|
1361
|
+
/** Shared JSON-schema fragment for one stage record inside the drive result. */
|
|
1362
|
+
const stageRecordSchema = {
|
|
1363
|
+
type: "object",
|
|
1364
|
+
additionalProperties: false,
|
|
1365
|
+
properties: {
|
|
1366
|
+
status: { type: "string" },
|
|
1367
|
+
exitCode: { oneOf: [{ type: "integer" }, { type: "null" }] },
|
|
1368
|
+
durationMs: { type: "integer" },
|
|
1369
|
+
attempts: { type: "integer" },
|
|
1370
|
+
summary: { type: "string" },
|
|
1371
|
+
outputTail: { type: "string" }
|
|
1372
|
+
}
|
|
1373
|
+
};
|
|
1374
|
+
/**
|
|
1375
|
+
* Full JSON-schema fragment for the drive-result branch of both tool outputs.
|
|
1376
|
+
* Explicit properties with `additionalProperties: false` — the compiled type
|
|
1377
|
+
* doubles as the compile-time assertion that the canonical `DriveResult`
|
|
1378
|
+
* value matches the declared tool contract.
|
|
1379
|
+
*/
|
|
1380
|
+
const driveResultSchema = {
|
|
1381
|
+
type: "object",
|
|
1382
|
+
additionalProperties: false,
|
|
1383
|
+
properties: {
|
|
1384
|
+
schema: {
|
|
1385
|
+
type: "string",
|
|
1386
|
+
const: RESULT_SCHEMA
|
|
1387
|
+
},
|
|
1388
|
+
run: {
|
|
1389
|
+
type: "object",
|
|
1390
|
+
additionalProperties: false,
|
|
1391
|
+
properties: {
|
|
1392
|
+
runId: { type: "string" },
|
|
1393
|
+
startedAt: { type: "string" },
|
|
1394
|
+
finishedAt: { type: "string" },
|
|
1395
|
+
durationMs: { type: "integer" },
|
|
1396
|
+
harnessVersion: { type: "string" },
|
|
1397
|
+
pluginVersion: { type: "string" },
|
|
1398
|
+
platform: { type: "string" },
|
|
1399
|
+
node: { type: "string" }
|
|
1400
|
+
}
|
|
1401
|
+
},
|
|
1402
|
+
target: {
|
|
1403
|
+
type: "object",
|
|
1404
|
+
additionalProperties: false,
|
|
1405
|
+
properties: {
|
|
1406
|
+
kind: { type: "string" },
|
|
1407
|
+
spec: { type: "string" },
|
|
1408
|
+
resolved: { oneOf: [{
|
|
1409
|
+
type: "object",
|
|
1410
|
+
additionalProperties: false,
|
|
1411
|
+
properties: {
|
|
1412
|
+
packageName: { type: "string" },
|
|
1413
|
+
packageVersion: { type: "string" },
|
|
1414
|
+
hasBundleManifest: { type: "boolean" }
|
|
1415
|
+
}
|
|
1416
|
+
}, { type: "null" }] }
|
|
1417
|
+
}
|
|
1418
|
+
},
|
|
1419
|
+
isolation: {
|
|
1420
|
+
type: "object",
|
|
1421
|
+
additionalProperties: false,
|
|
1422
|
+
properties: {
|
|
1423
|
+
tempDshHome: { type: "boolean" },
|
|
1424
|
+
tempWorkspace: { type: "boolean" },
|
|
1425
|
+
tempStore: { type: "boolean" },
|
|
1426
|
+
hostHomeTouched: {
|
|
1427
|
+
type: "boolean",
|
|
1428
|
+
const: false
|
|
1429
|
+
}
|
|
1430
|
+
}
|
|
1431
|
+
},
|
|
1432
|
+
stages: {
|
|
1433
|
+
type: "object",
|
|
1434
|
+
additionalProperties: false,
|
|
1435
|
+
properties: {
|
|
1436
|
+
install: {
|
|
1437
|
+
...stageRecordSchema,
|
|
1438
|
+
properties: {
|
|
1439
|
+
...stageRecordSchema.properties,
|
|
1440
|
+
allowBuildsNeeded: { type: "boolean" }
|
|
1441
|
+
}
|
|
1442
|
+
},
|
|
1443
|
+
config: {
|
|
1444
|
+
...stageRecordSchema,
|
|
1445
|
+
properties: {
|
|
1446
|
+
...stageRecordSchema.properties,
|
|
1447
|
+
patchEffective: { type: "boolean" },
|
|
1448
|
+
layers: {
|
|
1449
|
+
type: "array",
|
|
1450
|
+
items: { type: "string" }
|
|
1451
|
+
}
|
|
1452
|
+
}
|
|
1453
|
+
},
|
|
1454
|
+
smoke: {
|
|
1455
|
+
...stageRecordSchema,
|
|
1456
|
+
properties: {
|
|
1457
|
+
...stageRecordSchema.properties,
|
|
1458
|
+
bootFailed: { type: "boolean" },
|
|
1459
|
+
taskCompleted: { type: "boolean" }
|
|
1460
|
+
}
|
|
1461
|
+
},
|
|
1462
|
+
uninstall: stageRecordSchema,
|
|
1463
|
+
cleanup: {
|
|
1464
|
+
type: "object",
|
|
1465
|
+
additionalProperties: false,
|
|
1466
|
+
properties: {
|
|
1467
|
+
status: { type: "string" },
|
|
1468
|
+
quarantined: { type: "boolean" },
|
|
1469
|
+
removed: { type: "boolean" },
|
|
1470
|
+
summary: { type: "string" }
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
}
|
|
1474
|
+
},
|
|
1475
|
+
verdict: { oneOf: [
|
|
1476
|
+
{
|
|
1477
|
+
type: "string",
|
|
1478
|
+
const: "pass"
|
|
1479
|
+
},
|
|
1480
|
+
{
|
|
1481
|
+
type: "string",
|
|
1482
|
+
const: "fail"
|
|
1483
|
+
},
|
|
1484
|
+
{
|
|
1485
|
+
type: "string",
|
|
1486
|
+
const: "partial"
|
|
1487
|
+
},
|
|
1488
|
+
{
|
|
1489
|
+
type: "string",
|
|
1490
|
+
const: "unknown"
|
|
1491
|
+
}
|
|
1492
|
+
] },
|
|
1493
|
+
verdictReason: { type: "string" }
|
|
1494
|
+
}
|
|
1495
|
+
};
|
|
1496
|
+
/**
|
|
1497
|
+
* Full JSON-schema fragment for the matrix-report branch of `drive_report`.
|
|
1498
|
+
*/
|
|
1499
|
+
const matrixReportSchema = {
|
|
1500
|
+
type: "object",
|
|
1501
|
+
additionalProperties: false,
|
|
1502
|
+
properties: {
|
|
1503
|
+
schema: {
|
|
1504
|
+
type: "string",
|
|
1505
|
+
const: RESULT_SCHEMA
|
|
1506
|
+
},
|
|
1507
|
+
id: { type: "string" },
|
|
1508
|
+
createdAt: { type: "string" },
|
|
1509
|
+
durationMs: { type: "integer" },
|
|
1510
|
+
rows: {
|
|
1511
|
+
type: "array",
|
|
1512
|
+
items: {
|
|
1513
|
+
type: "object",
|
|
1514
|
+
additionalProperties: false,
|
|
1515
|
+
properties: {
|
|
1516
|
+
target: { type: "string" },
|
|
1517
|
+
kind: { type: "string" },
|
|
1518
|
+
verdict: { type: "string" },
|
|
1519
|
+
install: { type: "string" },
|
|
1520
|
+
smoke: { type: "string" },
|
|
1521
|
+
durationMs: { type: "integer" },
|
|
1522
|
+
summary: { type: "string" }
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
},
|
|
1526
|
+
totals: {
|
|
1527
|
+
type: "object",
|
|
1528
|
+
additionalProperties: false,
|
|
1529
|
+
properties: {
|
|
1530
|
+
total: { type: "integer" },
|
|
1531
|
+
pass: { type: "integer" },
|
|
1532
|
+
fail: { type: "integer" },
|
|
1533
|
+
partial: { type: "integer" },
|
|
1534
|
+
unknown: { type: "integer" }
|
|
1535
|
+
}
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
};
|
|
1539
|
+
/** Render one `test_drive` result (drive record or background handle). */
|
|
1540
|
+
function renderDrive(value) {
|
|
1541
|
+
if (value.kind === "background") return [{
|
|
1542
|
+
type: "text",
|
|
1543
|
+
text: `Started background test drive job ${value.jobId}. Read its output for progress; the matrix id in the final line feeds drive_report.`
|
|
1544
|
+
}];
|
|
1545
|
+
return [{
|
|
1546
|
+
type: "text",
|
|
1547
|
+
text: renderDriveResult(value)
|
|
1548
|
+
}];
|
|
1549
|
+
}
|
|
1550
|
+
/** Render one `drive_report` result (matrix or run record). */
|
|
1551
|
+
function renderReport(value) {
|
|
1552
|
+
return [{
|
|
1553
|
+
type: "text",
|
|
1554
|
+
text: "totals" in value ? renderMatrix(value) : renderDriveResult(value)
|
|
1555
|
+
}];
|
|
1556
|
+
}
|
|
1557
|
+
/** The deadline for one foreground drive: the sum of every stage deadline plus a buffer. */
|
|
1558
|
+
function driveDeadlineMs(config) {
|
|
1559
|
+
return config.installTimeoutMs + config.configTimeoutMs + config.smokeTimeoutMs + config.uninstallTimeoutMs + 6e4;
|
|
1560
|
+
}
|
|
1561
|
+
/** Owner for a background branch; background work needs a live agent to collect it. */
|
|
1562
|
+
function requireOwner(exec) {
|
|
1563
|
+
if (exec.agent === void 0) throw new Error("dsh-test-drive: background test drive requires an owning agent");
|
|
1564
|
+
return exec.agent;
|
|
1565
|
+
}
|
|
1566
|
+
/** Build both tool definitions over the shared services. */
|
|
1567
|
+
function allTools(services) {
|
|
1568
|
+
return [defineTool({
|
|
1569
|
+
name: "test_drive",
|
|
1570
|
+
description: "Install-and-smoke test one DeepSeek Harness plugin target (a git repo spec, an npm package name, a local path, or a tarball) in an ISOLATED throwaway DSH_HOME: `dsh plugin add` → `--dump-config` patch check → headless boot smoke (FAILED-marker scan) → `dsh plugin remove` → quarantined cleanup. The host profile and real ~/.dsh are never touched. Returns a structured result (install/smoke pass/fail, durations, failure summaries) consumable by scoring pipelines. Use background: true for a long target.",
|
|
1571
|
+
parameters: {
|
|
1572
|
+
target: {
|
|
1573
|
+
type: "string",
|
|
1574
|
+
required: true,
|
|
1575
|
+
description: "Plugin target: a git spec (github:owner/repo#sha, git+https://...), an npm package name, a local path, or a .tgz tarball."
|
|
1576
|
+
},
|
|
1577
|
+
headlessTask: {
|
|
1578
|
+
type: "string",
|
|
1579
|
+
description: "One-shot task text for the boot-smoke stage; empty string skips the smoke stage. Defaults to the plugin config."
|
|
1580
|
+
},
|
|
1581
|
+
background: {
|
|
1582
|
+
type: "boolean",
|
|
1583
|
+
description: "Run the drive as a background job and return its job id instead of waiting."
|
|
1584
|
+
}
|
|
1585
|
+
},
|
|
1586
|
+
output: {
|
|
1587
|
+
schema: { oneOf: [driveResultSchema, {
|
|
1588
|
+
type: "object",
|
|
1589
|
+
additionalProperties: false,
|
|
1590
|
+
properties: {
|
|
1591
|
+
kind: {
|
|
1592
|
+
type: "string",
|
|
1593
|
+
const: "background"
|
|
1594
|
+
},
|
|
1595
|
+
jobId: { type: "string" }
|
|
1596
|
+
}
|
|
1597
|
+
}] },
|
|
1598
|
+
render: (_args, value) => renderDrive(value)
|
|
1599
|
+
},
|
|
1600
|
+
timeoutMs: driveDeadlineMs(services.config),
|
|
1601
|
+
async execute(args, exec) {
|
|
1602
|
+
const target = sanitizeTarget(args.target);
|
|
1603
|
+
if (target.length === 0) throw new Error("dsh-test-drive: target must be a non-empty string");
|
|
1604
|
+
const headlessTask = args.headlessTask;
|
|
1605
|
+
if (headlessTask !== void 0 && headlessTask.length > 2e3) throw new Error(`dsh-test-drive: headlessTask must be at most ${MAX_HEADLESS_TASK_LENGTH} characters`);
|
|
1606
|
+
if (args.background === true) {
|
|
1607
|
+
const owner = requireOwner(exec);
|
|
1608
|
+
const label = `test_drive ${target}`;
|
|
1609
|
+
const jobId = startBatchJob(services, [target], owner, label);
|
|
1610
|
+
return {
|
|
1611
|
+
kind: "background",
|
|
1612
|
+
jobId: String(jobId)
|
|
1613
|
+
};
|
|
1614
|
+
}
|
|
1615
|
+
const result = await services.runner.drive(target, {
|
|
1616
|
+
signal: exec.signal,
|
|
1617
|
+
...headlessTask === void 0 ? {} : { headlessTask }
|
|
1618
|
+
});
|
|
1619
|
+
return {
|
|
1620
|
+
...result,
|
|
1621
|
+
target: {
|
|
1622
|
+
...result.target,
|
|
1623
|
+
resolved: result.target.resolved ?? null
|
|
1624
|
+
}
|
|
1625
|
+
};
|
|
1626
|
+
}
|
|
1627
|
+
}), defineTool({
|
|
1628
|
+
name: "drive_report",
|
|
1629
|
+
description: "Fetch a stored test-drive record by id: a single run (tdr_...), a batch matrix (tdm_...), or — with no id — the latest matrix. Returns the structured record and renders it as Markdown.",
|
|
1630
|
+
parameters: { id: {
|
|
1631
|
+
type: "string",
|
|
1632
|
+
description: "Run id (tdr_...), matrix id (tdm_...); omitted = the latest matrix."
|
|
1633
|
+
} },
|
|
1634
|
+
output: {
|
|
1635
|
+
schema: { oneOf: [driveResultSchema, matrixReportSchema] },
|
|
1636
|
+
render: (_args, value) => renderReport(value)
|
|
1637
|
+
},
|
|
1638
|
+
async execute(args) {
|
|
1639
|
+
const domain = await services.domain();
|
|
1640
|
+
if (args.id === void 0 || args.id === "") {
|
|
1641
|
+
const latest = domain.global.get();
|
|
1642
|
+
if (latest.matrixId === "") throw new Error("dsh-test-drive: no matrix recorded yet — run /testdrive or test_drive first");
|
|
1643
|
+
const matrix = domain.table("matrices").get(latest.matrixId);
|
|
1644
|
+
if (matrix === void 0) throw new Error(`dsh-test-drive: latest matrix ${latest.matrixId} not found`);
|
|
1645
|
+
return matrix;
|
|
1646
|
+
}
|
|
1647
|
+
if (args.id.startsWith("tdr_")) {
|
|
1648
|
+
const run = domain.table("runs").get(args.id);
|
|
1649
|
+
if (run === void 0) throw new Error(`dsh-test-drive: no run recorded with id ${args.id}`);
|
|
1650
|
+
return {
|
|
1651
|
+
...run,
|
|
1652
|
+
target: {
|
|
1653
|
+
...run.target,
|
|
1654
|
+
resolved: run.target.resolved ?? null
|
|
1655
|
+
}
|
|
1656
|
+
};
|
|
1657
|
+
}
|
|
1658
|
+
if (args.id.startsWith("tdm_")) {
|
|
1659
|
+
const matrix = domain.table("matrices").get(args.id);
|
|
1660
|
+
if (matrix === void 0) throw new Error(`dsh-test-drive: no matrix recorded with id ${args.id}`);
|
|
1661
|
+
return matrix;
|
|
1662
|
+
}
|
|
1663
|
+
throw new Error(`dsh-test-drive: unknown report id ${args.id} (expected tdr_... run or tdm_... matrix)`);
|
|
1664
|
+
}
|
|
1665
|
+
})];
|
|
1666
|
+
}
|
|
1667
|
+
//#endregion
|
|
1668
|
+
//#region src/workspace.ts
|
|
1669
|
+
/**
|
|
1670
|
+
* Temporary-directory ownership for test drives. Every directory this plugin
|
|
1671
|
+
* ever touches under the OS temp dir is created here with {@link mkdtemp},
|
|
1672
|
+
* tracked in a live registry, and removed only through the
|
|
1673
|
+
* dry-run → quarantine-rename → delete ladder. Nothing in this module scans
|
|
1674
|
+
* the temp dir, guesses at foreign prefixes, or touches the real home:
|
|
1675
|
+
* ownership is a registry lookup plus a prefix and direct-child check, and a
|
|
1676
|
+
* path that fails any check is refused loudly.
|
|
1677
|
+
*
|
|
1678
|
+
* @module dsh-test-drive/workspace
|
|
1679
|
+
*/
|
|
1680
|
+
/** Prefix of every owned temp root (`mkdtemp` appends the random suffix). */
|
|
1681
|
+
const OWNED_PREFIX = "dsh-test-drive-";
|
|
1682
|
+
/** Prefix of quarantine directories an owned root is renamed into before removal. */
|
|
1683
|
+
const QUARANTINE_PREFIX = "dsh-test-drive-quarantine-";
|
|
1684
|
+
/** Compare two absolute paths with the platform's case semantics. */
|
|
1685
|
+
function samePath(a, b) {
|
|
1686
|
+
const left = resolve(a);
|
|
1687
|
+
const right = resolve(b);
|
|
1688
|
+
return process.platform === "win32" ? left.toLowerCase() === right.toLowerCase() : left === right;
|
|
1689
|
+
}
|
|
1690
|
+
/**
|
|
1691
|
+
* The registry of temp roots this plugin instance owns. All mutations are
|
|
1692
|
+
* synchronous so the ownership set can never observe a half-registered root.
|
|
1693
|
+
*/
|
|
1694
|
+
var TempWorkspaceRegistry = class {
|
|
1695
|
+
log;
|
|
1696
|
+
now;
|
|
1697
|
+
owned = /* @__PURE__ */ new Set();
|
|
1698
|
+
quarantined = /* @__PURE__ */ new Set();
|
|
1699
|
+
sequence = 0;
|
|
1700
|
+
/**
|
|
1701
|
+
* @param log - operator log sink (the plugin logger); receives the dry-run plan lines.
|
|
1702
|
+
* @param now - clock (test hook for deterministic quarantine names).
|
|
1703
|
+
*/
|
|
1704
|
+
constructor(log, now = Date.now) {
|
|
1705
|
+
this.log = log;
|
|
1706
|
+
this.now = now;
|
|
1707
|
+
}
|
|
1708
|
+
/** Owned roots currently registered (for tests and diagnostics). */
|
|
1709
|
+
get size() {
|
|
1710
|
+
return this.owned.size;
|
|
1711
|
+
}
|
|
1712
|
+
/** Create one owned temp root with the standard layout. */
|
|
1713
|
+
async create(profileName) {
|
|
1714
|
+
const root = await mkdtemp(join(tmpdir(), OWNED_PREFIX));
|
|
1715
|
+
const home = join(root, "home");
|
|
1716
|
+
const workspace = join(root, "workspace");
|
|
1717
|
+
const store = join(root, "pnpm-store");
|
|
1718
|
+
const profileDir = join(home, "profiles", profileName);
|
|
1719
|
+
this.owned.add(root);
|
|
1720
|
+
await mkdir(home, { recursive: true });
|
|
1721
|
+
await mkdir(workspace, { recursive: true });
|
|
1722
|
+
await mkdir(store, { recursive: true });
|
|
1723
|
+
this.log(`test-drive: created temp workspace ${root} (home=${home})`);
|
|
1724
|
+
return {
|
|
1725
|
+
root,
|
|
1726
|
+
home,
|
|
1727
|
+
workspace,
|
|
1728
|
+
store,
|
|
1729
|
+
profileDir
|
|
1730
|
+
};
|
|
1731
|
+
}
|
|
1732
|
+
/**
|
|
1733
|
+
* Assert that `path` is a directory this instance created and still owns.
|
|
1734
|
+
* Fails loud — never falls back to cwd, a parent, or a different prefix.
|
|
1735
|
+
*/
|
|
1736
|
+
assertOwned(path, what) {
|
|
1737
|
+
const normalized = resolve(path);
|
|
1738
|
+
if (!samePath(dirname(normalized), tmpdir())) throw new Error(`refusing temp cleanup: ${what} ${normalized} is not a direct child of the OS temp dir`);
|
|
1739
|
+
const name = basename(normalized);
|
|
1740
|
+
const prefix = what === "root" ? OWNED_PREFIX : QUARANTINE_PREFIX;
|
|
1741
|
+
if (!name.startsWith(prefix)) throw new Error(`refusing temp cleanup: ${what} ${normalized} does not carry the owned prefix ${prefix}`);
|
|
1742
|
+
if (![...what === "root" ? this.owned : this.quarantined].some((candidate) => samePath(candidate, normalized))) throw new Error(`refusing temp cleanup: ${what} ${normalized} is not registered with this plugin instance`);
|
|
1743
|
+
}
|
|
1744
|
+
/**
|
|
1745
|
+
* Compile and LOG the dry-run plan for one root before any mutation happens.
|
|
1746
|
+
* The returned quarantine path is the exact path the execution ladder uses,
|
|
1747
|
+
* so the logged plan always matches the performed operations.
|
|
1748
|
+
*/
|
|
1749
|
+
plan(root) {
|
|
1750
|
+
this.assertOwned(root, "root");
|
|
1751
|
+
const quarantine = join(tmpdir(), `${QUARANTINE_PREFIX}${this.now()}-${this.sequence += 1}`);
|
|
1752
|
+
this.log(`test-drive: cleanup dry-run — quarantine-rename: ${root} -> ${quarantine}`);
|
|
1753
|
+
this.log(`test-drive: cleanup dry-run — remove: ${quarantine} (owned quarantine dir, prefix ${QUARANTINE_PREFIX})`);
|
|
1754
|
+
return quarantine;
|
|
1755
|
+
}
|
|
1756
|
+
/**
|
|
1757
|
+
* Remove one owned root via the quarantine ladder: assert ownership, log the
|
|
1758
|
+
* dry-run plan, rename into an owned quarantine directory, then remove it.
|
|
1759
|
+
* A root that fails removal stays registered as quarantine and is retried
|
|
1760
|
+
* on the next teardown.
|
|
1761
|
+
*
|
|
1762
|
+
* @param root - the owned root to remove.
|
|
1763
|
+
* @returns the sanitized outcome.
|
|
1764
|
+
*/
|
|
1765
|
+
async quarantineAndRemove(root) {
|
|
1766
|
+
const normalized = resolve(root);
|
|
1767
|
+
const quarantine = this.plan(normalized);
|
|
1768
|
+
try {
|
|
1769
|
+
await rename(normalized, quarantine);
|
|
1770
|
+
} catch (error) {
|
|
1771
|
+
return {
|
|
1772
|
+
status: "fail",
|
|
1773
|
+
quarantined: false,
|
|
1774
|
+
removed: false,
|
|
1775
|
+
summary: `quarantine rename failed: ${String(error)}`
|
|
1776
|
+
};
|
|
1777
|
+
}
|
|
1778
|
+
this.owned.delete(normalized);
|
|
1779
|
+
this.quarantined.add(quarantine);
|
|
1780
|
+
try {
|
|
1781
|
+
await removeWithRetries(quarantine, 3, 100);
|
|
1782
|
+
} catch (error) {
|
|
1783
|
+
this.log(`test-drive: quarantine removal failed, left for retry: ${quarantine} (${String(error)})`);
|
|
1784
|
+
return {
|
|
1785
|
+
status: "fail",
|
|
1786
|
+
quarantined: true,
|
|
1787
|
+
removed: false,
|
|
1788
|
+
summary: "quarantined but removal failed; path left under the OS temp dir (see host log)"
|
|
1789
|
+
};
|
|
1790
|
+
}
|
|
1791
|
+
this.quarantined.delete(quarantine);
|
|
1792
|
+
this.log(`test-drive: removed ${quarantine}`);
|
|
1793
|
+
return {
|
|
1794
|
+
status: "pass",
|
|
1795
|
+
quarantined: true,
|
|
1796
|
+
removed: true,
|
|
1797
|
+
summary: "owned temp root quarantined and removed"
|
|
1798
|
+
};
|
|
1799
|
+
}
|
|
1800
|
+
/**
|
|
1801
|
+
* Keep a root for forensics (config `keepTempDirs`): drop ownership without
|
|
1802
|
+
* touching the directory. The caller owns later manual cleanup.
|
|
1803
|
+
*/
|
|
1804
|
+
keep(root) {
|
|
1805
|
+
const normalized = resolve(root);
|
|
1806
|
+
this.assertOwned(normalized, "root");
|
|
1807
|
+
this.owned.delete(normalized);
|
|
1808
|
+
this.log(`test-drive: keeping temp workspace for forensics (ownership dropped): ${normalized}`);
|
|
1809
|
+
return {
|
|
1810
|
+
status: "skipped",
|
|
1811
|
+
quarantined: false,
|
|
1812
|
+
removed: false,
|
|
1813
|
+
summary: `kept for forensics: ${basename(normalized)}`
|
|
1814
|
+
};
|
|
1815
|
+
}
|
|
1816
|
+
/** Best-effort teardown of every still-registered root and quarantine dir. */
|
|
1817
|
+
async disposeAll() {
|
|
1818
|
+
const roots = [...this.owned];
|
|
1819
|
+
for (const root of roots) try {
|
|
1820
|
+
await this.quarantineAndRemove(root);
|
|
1821
|
+
} catch (error) {
|
|
1822
|
+
this.log(`test-drive: teardown cleanup failed for ${root}: ${String(error)}`);
|
|
1823
|
+
}
|
|
1824
|
+
const quarantines = [...this.quarantined];
|
|
1825
|
+
for (const quarantine of quarantines) try {
|
|
1826
|
+
await removeWithRetries(quarantine, 3, 100);
|
|
1827
|
+
this.quarantined.delete(quarantine);
|
|
1828
|
+
} catch (error) {
|
|
1829
|
+
this.log(`test-drive: teardown quarantine retry failed for ${quarantine}: ${String(error)}`);
|
|
1830
|
+
}
|
|
1831
|
+
}
|
|
1832
|
+
};
|
|
1833
|
+
/** Remove a directory tree with bounded retries (Windows file-lock tolerance). */
|
|
1834
|
+
async function removeWithRetries(path, maxRetries, retryDelayMs) {
|
|
1835
|
+
let attempt = 0;
|
|
1836
|
+
for (;;) try {
|
|
1837
|
+
await rm(path, {
|
|
1838
|
+
recursive: true,
|
|
1839
|
+
force: true,
|
|
1840
|
+
maxRetries: 1
|
|
1841
|
+
});
|
|
1842
|
+
return;
|
|
1843
|
+
} catch (error) {
|
|
1844
|
+
attempt += 1;
|
|
1845
|
+
if (attempt >= maxRetries) throw error;
|
|
1846
|
+
await new Promise((resolveDelay) => setTimeout(resolveDelay, retryDelayMs));
|
|
1847
|
+
}
|
|
1848
|
+
}
|
|
1849
|
+
//#endregion
|
|
1850
|
+
//#region src/index.ts
|
|
1851
|
+
const name = "dsh-test-drive";
|
|
1852
|
+
/**
|
|
1853
|
+
* Public services only. `storageDomain` is deliberately OPTIONAL: the shipped
|
|
1854
|
+
* `dsh-base` bundle (headless profile) does not mount it, and the plugin must
|
|
1855
|
+
* still boot there — report persistence degrades to disabled with a logged
|
|
1856
|
+
* reason, tools keep working.
|
|
1857
|
+
*/
|
|
1858
|
+
const inject = [
|
|
1859
|
+
"tools",
|
|
1860
|
+
"commands",
|
|
1861
|
+
"subprocess",
|
|
1862
|
+
"jobs"
|
|
1863
|
+
];
|
|
1864
|
+
/**
|
|
1865
|
+
* Mount the plugin: resolve config, open the report domain (lazily), register
|
|
1866
|
+
* the tools and the `/testdrive` command as effects, and own every temp
|
|
1867
|
+
* directory through the quarantine registry (teardown sweeps leftovers).
|
|
1868
|
+
*
|
|
1869
|
+
* @param ctx - context carrying tools/commands/subprocess/jobs/storageDomain.
|
|
1870
|
+
* @param config - raw loader config; defaults applied through {@link resolveConfig}.
|
|
1871
|
+
*/
|
|
1872
|
+
function apply(ctx, config) {
|
|
1873
|
+
const resolved = resolveConfig(config);
|
|
1874
|
+
const log = (line) => {
|
|
1875
|
+
ctx.logger("test-drive").info(line);
|
|
1876
|
+
};
|
|
1877
|
+
const temp = new TempWorkspaceRegistry(log);
|
|
1878
|
+
ctx.effect(() => () => {
|
|
1879
|
+
temp.disposeAll().catch((error) => log(`teardown cleanup failed: ${String(error)}`));
|
|
1880
|
+
}, "dsh-test-drive temp registry teardown");
|
|
1881
|
+
const storageDomain = ctx.get("storageDomain");
|
|
1882
|
+
const domainPromise = storageDomain === void 0 ? Promise.reject(/* @__PURE__ */ new Error("dsh-test-drive: storageDomain service not mounted in this composition; reports disabled")) : (async () => storageDomain.open(driveDomainSpec))();
|
|
1883
|
+
domainPromise.catch((error) => {
|
|
1884
|
+
log(`report domain unavailable: ${String(error)}`);
|
|
1885
|
+
});
|
|
1886
|
+
const domain = () => domainPromise;
|
|
1887
|
+
ctx.effect(() => () => {
|
|
1888
|
+
domainPromise.then((handle) => handle.close()).catch(() => {});
|
|
1889
|
+
}, "dsh-test-drive report domain close");
|
|
1890
|
+
const driver = new DshDriver({
|
|
1891
|
+
ctx,
|
|
1892
|
+
config: resolved,
|
|
1893
|
+
log
|
|
1894
|
+
});
|
|
1895
|
+
const deps = {
|
|
1896
|
+
ctx,
|
|
1897
|
+
config: resolved,
|
|
1898
|
+
driver,
|
|
1899
|
+
temp,
|
|
1900
|
+
log,
|
|
1901
|
+
domain
|
|
1902
|
+
};
|
|
1903
|
+
const runner = new DriveRunner(deps);
|
|
1904
|
+
const services = {
|
|
1905
|
+
...deps,
|
|
1906
|
+
runner
|
|
1907
|
+
};
|
|
1908
|
+
for (const tool of allTools(services)) ctx.effect(() => ctx.tools.register(tool), `dsh-test-drive: ${tool.name} tool`);
|
|
1909
|
+
ctx.effect(() => ctx.commands.register({
|
|
1910
|
+
name: "testdrive",
|
|
1911
|
+
description: "Batch test-drive plugin targets in isolated throwaway profiles (background job + matrix report)",
|
|
1912
|
+
input: { hint: "repo or npm targets, space-separated" },
|
|
1913
|
+
handler: (invocation) => handleTestdrive(services, invocation)
|
|
1914
|
+
}), "dsh-test-drive: /testdrive command");
|
|
1915
|
+
driver.locate().catch((error) => {
|
|
1916
|
+
log(`dsh CLI unavailable: ${String(error)}`);
|
|
1917
|
+
});
|
|
1918
|
+
}
|
|
1919
|
+
//#endregion
|
|
1920
|
+
export { BOOT_FAILURE_MARKERS, Config, DOMAIN_NAME, DOMAIN_VERSION, DRIVE_BATCH_KIND, DriveResultSchema, DriveRunner, DshDriver, MATRIX_ID_PREFIX, MatrixRecordSchema, OWNED_PREFIX, QUARANTINE_PREFIX, REDACTED, RESULT_SCHEMA, RUN_ID_PREFIX, TEMPLATE_BUNDLES, TempWorkspaceRegistry, VERSION, anchorLocalTarget, apply, classifyTarget, driveDomainSpec, dumpMentionsPackage, formatDuration, freshId, hasBootFailure, inject, isGitLike, matrixSummary, name, parseDumpLayers, parseIgnoredBuildScript, parseNpmShim, parseTargets, progressLine, redactSecrets, redactTempPath, removeWithRetries, renderDriveResult, renderMatrix, resolveConfig, runBatch, sanitizeOutput, sanitizeTarget, startBatchJob, statusMark, tailText, totalsOf, verdictOf };
|