agentpath 0.0.0-stage → 0.1.0-alpha.2
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/LICENSE.md +105 -0
- package/README.md +101 -2
- package/THIRD_PARTY_NOTICES +12 -0
- package/apps/demo-site/dist/site.js +92 -0
- package/apps/studio/dist/assets/geist-cyrillic-ext-wght-normal-DjL33-gN.woff2 +0 -0
- package/apps/studio/dist/assets/geist-cyrillic-wght-normal-BEAKL7Jp.woff2 +0 -0
- package/apps/studio/dist/assets/geist-latin-ext-wght-normal-DC-KSUi6.woff2 +0 -0
- package/apps/studio/dist/assets/geist-latin-wght-normal-BgDaEnEv.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-cyrillic-ext-wght-normal-X_5orZeX.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-cyrillic-wght-normal-DiZS0aHC.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-latin-ext-wght-normal-Bwz-egvJ.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-latin-wght-normal-XN7g48iV.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-symbols2-wght-normal-CO5SzqOn.woff2 +0 -0
- package/apps/studio/dist/assets/geist-mono-vietnamese-wght-normal-DadHysG0.woff2 +0 -0
- package/apps/studio/dist/assets/geist-vietnamese-wght-normal-6IgcOCM7.woff2 +0 -0
- package/apps/studio/dist/assets/index-DH4wJPHm.js +67 -0
- package/apps/studio/dist/assets/index-DrnNh2ip.css +1 -0
- package/apps/studio/dist/index.html +14 -0
- package/audit.md +173 -0
- package/autonomous-mode.md +104 -0
- package/ci.md +118 -0
- package/coding-agents.md +143 -0
- package/concepts.md +145 -0
- package/contracts.md +356 -0
- package/examples/tasks/ambiguous.json +98 -0
- package/examples/tasks/assertion-failure.json +98 -0
- package/examples/tasks/broken.json +98 -0
- package/examples/tasks/fixed.json +98 -0
- package/examples/tasks/recovery.json +19 -0
- package/images/compare-recovery-desktop.jpg +0 -0
- package/images/compare-variants-desktop.jpg +0 -0
- package/images/overview-desktop.jpg +0 -0
- package/images/run-autonomous-passed-desktop.jpg +0 -0
- package/images/run-failed-desktop.jpg +0 -0
- package/images/run-failed-mobile.jpg +0 -0
- package/images/run-missing-artifact-desktop.jpg +0 -0
- package/images/task-editor-laptop.jpg +0 -0
- package/installation.md +102 -0
- package/ledger.md +62 -0
- package/licenses/-fontsource-variable-geist-mono.txt +93 -0
- package/licenses/-fontsource-variable-geist.txt +93 -0
- package/licenses/lucide-react.txt +43 -0
- package/licenses/react-dom.txt +21 -0
- package/licenses/react.txt +21 -0
- package/licenses/scheduler.txt +21 -0
- package/licenses/tailwindcss.txt +21 -0
- package/licenses/zod.txt +21 -0
- package/package.json +165 -4
- package/packages/cli/bin/agentpath.js +39 -0
- package/packages/cli/dist/audit-rerun.js +384 -0
- package/packages/cli/dist/audit.js +387 -0
- package/packages/cli/dist/change-summaries.js +87 -0
- package/packages/cli/dist/ci-report.js +88 -0
- package/packages/cli/dist/demo.js +88 -0
- package/packages/cli/dist/doctor.js +265 -0
- package/packages/cli/dist/explain.js +89 -0
- package/packages/cli/dist/git.js +7 -0
- package/packages/cli/dist/inspect.js +118 -0
- package/packages/cli/dist/ledger.js +509 -0
- package/packages/cli/dist/local.js +102 -0
- package/packages/cli/dist/main.js +708 -0
- package/packages/cli/dist/mcp.js +231 -0
- package/packages/cli/dist/paths.js +6 -0
- package/packages/cli/dist/playwright-fixture.js +185 -0
- package/packages/cli/dist/playwright-reporter.js +49 -0
- package/packages/cli/dist/tasks.js +37 -0
- package/packages/cli/dist/test-source.js +283 -0
- package/packages/cli/dist/watch.js +96 -0
- package/packages/contracts/dist/agent-settings.js +18 -0
- package/packages/contracts/dist/agent.js +175 -0
- package/packages/contracts/dist/audit.js +115 -0
- package/packages/contracts/dist/guidance.js +339 -0
- package/packages/contracts/dist/index.js +14 -0
- package/packages/contracts/dist/inspect.js +63 -0
- package/packages/contracts/dist/ledger.js +80 -0
- package/packages/contracts/dist/local.js +171 -0
- package/packages/contracts/dist/outcomes.js +94 -0
- package/packages/contracts/dist/platform.js +105 -0
- package/packages/contracts/dist/playwright-live.js +76 -0
- package/packages/contracts/dist/policy.js +93 -0
- package/packages/contracts/dist/run.js +178 -0
- package/packages/contracts/dist/task-issues.js +39 -0
- package/packages/contracts/dist/task.js +185 -0
- package/packages/contracts/dist/watch.js +57 -0
- package/packages/core/dist/action-feedback.js +103 -0
- package/packages/core/dist/actions.js +160 -0
- package/packages/core/dist/agent-loop.js +122 -0
- package/packages/core/dist/artifacts.js +156 -0
- package/packages/core/dist/assertions.js +120 -0
- package/packages/core/dist/autonomous.js +1 -0
- package/packages/core/dist/browser.js +196 -0
- package/packages/core/dist/compare.js +141 -0
- package/packages/core/dist/control-state.js +62 -0
- package/packages/core/dist/diagnostics.js +134 -0
- package/packages/core/dist/evidence-diff.js +93 -0
- package/packages/core/dist/evidence.js +203 -0
- package/packages/core/dist/index.js +22 -0
- package/packages/core/dist/inspect.js +257 -0
- package/packages/core/dist/integrity.js +176 -0
- package/packages/core/dist/liveness.js +114 -0
- package/packages/core/dist/local-files.js +61 -0
- package/packages/core/dist/locators.js +214 -0
- package/packages/core/dist/model-provider.js +88 -0
- package/packages/core/dist/observation.js +257 -0
- package/packages/core/dist/playwright-live.js +519 -0
- package/packages/core/dist/playwright-results.js +543 -0
- package/packages/core/dist/policy.js +182 -0
- package/packages/core/dist/privacy.js +93 -0
- package/packages/core/dist/recovery.js +54 -0
- package/packages/core/dist/retention.js +204 -0
- package/packages/core/dist/run-index.js +177 -0
- package/packages/core/dist/run-plan.js +28 -0
- package/packages/core/dist/run-repository.js +518 -0
- package/packages/core/dist/runner.js +913 -0
- package/packages/core/dist/storage.js +63 -0
- package/packages/core/dist/task-fingerprint.js +36 -0
- package/packages/core/dist/test-change-rerun.js +84 -0
- package/packages/core/dist/test-changes.js +896 -0
- package/packages/core/dist/timing.js +41 -0
- package/packages/core/dist/watch.js +165 -0
- package/packages/server/dist/index.js +265 -0
- package/packages/server/dist/task-store.js +103 -0
- package/quickstart.md +85 -0
- package/security.md +136 -0
- package/try-on-your-repo.md +54 -0
- package/understanding-results.md +159 -0
- package/writing-tasks.md +196 -0
|
@@ -0,0 +1,384 @@
|
|
|
1
|
+
// `agentpath audit --rerun` (docs/audit.md): runs the base version of each flagged test against the head build,
|
|
2
|
+
// next to the head version, with the project's own Playwright. This executes the project's tests, configuration and
|
|
3
|
+
// web server, as `npx playwright test` would; it is opt-in on the command line only.
|
|
4
|
+
import { spawn } from "node:child_process";
|
|
5
|
+
import { randomBytes } from "node:crypto";
|
|
6
|
+
import { rmSync, unlinkSync } from "node:fs";
|
|
7
|
+
import { lstat, mkdtemp, readFile, realpath, stat, writeFile } from "node:fs/promises";
|
|
8
|
+
import { createRequire } from "node:module";
|
|
9
|
+
import { tmpdir } from "node:os";
|
|
10
|
+
import { dirname, join, posix, resolve, sep } from "node:path";
|
|
11
|
+
import { auditReportSchema } from "#agentpath/contracts";
|
|
12
|
+
import { classifyRerun, controlTestLine, explainFailedTest, isParameterised, MAX_SOURCE_BYTES, plainText, readTests, RERUN_COPY, rerunCopyName, rerunSide, runsSerially, tokenize, usesSnapshotFiles } from "#agentpath/core";
|
|
13
|
+
import { git } from "./git.js";
|
|
14
|
+
export class RerunUsageError extends Error {
|
|
15
|
+
}
|
|
16
|
+
const MAX_RERUN_TESTS = 50;
|
|
17
|
+
const RESULTS_LIMIT = 64 * 1024 * 1024;
|
|
18
|
+
const CONTEXT_LIMIT = 2 * 1024 * 1024;
|
|
19
|
+
const TRACE_LIMIT = 512 * 1024 * 1024;
|
|
20
|
+
const KILL_GRACE_MS = 5000;
|
|
21
|
+
const CONFIG_NAMES = ["playwright.config.ts", "playwright.config.mts", "playwright.config.cts", "playwright.config.js", "playwright.config.mjs", "playwright.config.cjs"];
|
|
22
|
+
const HOOK_TITLE = /^\[(beforeEach|afterEach|beforeAll|afterAll) \d+\]$/;
|
|
23
|
+
/** `git status` entries (tracked changes and untracked files, not ignored ones), keyed by path. */
|
|
24
|
+
async function treeState(root) {
|
|
25
|
+
const fields = (await git(root, ["status", "--porcelain=v1", "-z", "--untracked-files=all"])).split("\0");
|
|
26
|
+
const entries = new Map();
|
|
27
|
+
for (let index = 0; index < fields.length; index++) {
|
|
28
|
+
const field = fields[index];
|
|
29
|
+
if (field.length < 4)
|
|
30
|
+
continue;
|
|
31
|
+
entries.set(field.slice(3), field.slice(0, 2));
|
|
32
|
+
if (field[0] === "R" || field[0] === "C")
|
|
33
|
+
index++;
|
|
34
|
+
}
|
|
35
|
+
return entries;
|
|
36
|
+
}
|
|
37
|
+
async function exists(path) {
|
|
38
|
+
return stat(path).then(() => true, () => false);
|
|
39
|
+
}
|
|
40
|
+
async function findConfig(root, from) {
|
|
41
|
+
for (let directory = from;; directory = dirname(directory)) {
|
|
42
|
+
for (const name of CONFIG_NAMES)
|
|
43
|
+
if (await exists(join(directory, name)))
|
|
44
|
+
return join(directory, name);
|
|
45
|
+
if (directory === root || dirname(directory) === directory || !directory.startsWith(root))
|
|
46
|
+
return undefined;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
/** The project's own Playwright command-line entry, resolved from the configuration's directory. */
|
|
50
|
+
function playwrightCli(configDirectory) {
|
|
51
|
+
const require = createRequire(join(configDirectory, "noop.js"));
|
|
52
|
+
for (const name of ["playwright/package.json", "@playwright/test/package.json"]) {
|
|
53
|
+
try {
|
|
54
|
+
return join(dirname(require.resolve(name)), "cli.js");
|
|
55
|
+
}
|
|
56
|
+
catch { /* try the next */ }
|
|
57
|
+
}
|
|
58
|
+
return undefined;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Runs Playwright in its own process group. On timeout the group gets SIGINT, the signal Playwright cleans up on (it
|
|
62
|
+
* ends its web server, which runs in a group of its own; SIGTERM would leave it running), then SIGKILL.
|
|
63
|
+
*/
|
|
64
|
+
function runPlaywright(cli, args, cwd, resultsFile, timeoutMs, track) {
|
|
65
|
+
return new Promise((done) => {
|
|
66
|
+
const env = { ...process.env, PLAYWRIGHT_JSON_OUTPUT_FILE: resultsFile, PLAYWRIGHT_JSON_OUTPUT_NAME: resultsFile };
|
|
67
|
+
const child = spawn(process.execPath, [cli, ...args], { cwd, env, detached: process.platform !== "win32", stdio: "ignore" });
|
|
68
|
+
track(child);
|
|
69
|
+
let timedOut = false;
|
|
70
|
+
const signal = (name) => {
|
|
71
|
+
try {
|
|
72
|
+
if (process.platform === "win32")
|
|
73
|
+
child.kill(name);
|
|
74
|
+
else
|
|
75
|
+
process.kill(-child.pid, name);
|
|
76
|
+
}
|
|
77
|
+
catch { /* already gone */ }
|
|
78
|
+
};
|
|
79
|
+
const timer = setTimeout(() => {
|
|
80
|
+
timedOut = true;
|
|
81
|
+
signal("SIGINT");
|
|
82
|
+
setTimeout(() => signal("SIGKILL"), KILL_GRACE_MS).unref();
|
|
83
|
+
}, Math.max(1, timeoutMs));
|
|
84
|
+
const finish = () => {
|
|
85
|
+
clearTimeout(timer);
|
|
86
|
+
// Anything the run left in its own group (a web server, a browser) is ended with it.
|
|
87
|
+
signal("SIGTERM");
|
|
88
|
+
track(undefined);
|
|
89
|
+
done(timedOut);
|
|
90
|
+
};
|
|
91
|
+
child.once("exit", finish);
|
|
92
|
+
child.once("error", finish);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* Per test location (`realpath:line`), per project: the side result. Failed results are read with explain's engine.
|
|
97
|
+
* `loadErrors` says Playwright reported errors outside any test, such as a file that could not be imported.
|
|
98
|
+
*/
|
|
99
|
+
async function readResults(resultsFile, outputDirectory) {
|
|
100
|
+
const results = new Map();
|
|
101
|
+
let parsed;
|
|
102
|
+
try {
|
|
103
|
+
if ((await lstat(resultsFile)).size > RESULTS_LIMIT)
|
|
104
|
+
return { results, loadErrors: false };
|
|
105
|
+
parsed = JSON.parse(await readFile(resultsFile, "utf8"));
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
return { results, loadErrors: false };
|
|
109
|
+
}
|
|
110
|
+
const loadErrors = Array.isArray(parsed.errors) && parsed.errors.length > 0;
|
|
111
|
+
const rootDir = typeof parsed.config?.rootDir === "string" ? parsed.config.rootDir : undefined;
|
|
112
|
+
if (!rootDir)
|
|
113
|
+
return { results, loadErrors };
|
|
114
|
+
const inside = (path) => typeof path === "string" && resolve(path).startsWith(outputDirectory + sep);
|
|
115
|
+
const pending = Array.isArray(parsed.suites) ? [...parsed.suites] : [];
|
|
116
|
+
for (let visited = 0; pending.length && visited < 100_000; visited++) {
|
|
117
|
+
const suite = pending.pop();
|
|
118
|
+
if (!suite || typeof suite !== "object")
|
|
119
|
+
continue;
|
|
120
|
+
if (Array.isArray(suite.suites))
|
|
121
|
+
pending.push(...suite.suites);
|
|
122
|
+
for (const spec of Array.isArray(suite.specs) ? suite.specs : []) {
|
|
123
|
+
if (typeof spec?.file !== "string" || typeof spec.line !== "number" || !Array.isArray(spec.tests))
|
|
124
|
+
continue;
|
|
125
|
+
const file = await realpath(resolve(rootDir, spec.file)).catch(() => resolve(rootDir, spec.file));
|
|
126
|
+
const key = `${file}:${spec.line}`;
|
|
127
|
+
for (const test of spec.tests) {
|
|
128
|
+
const runs = Array.isArray(test?.results) ? test.results : [];
|
|
129
|
+
const last = runs.at(-1);
|
|
130
|
+
const outcome = typeof test?.status === "string" ? test.status : "";
|
|
131
|
+
let explanation;
|
|
132
|
+
if (outcome === "unexpected" && last) {
|
|
133
|
+
const attachments = Array.isArray(last.attachments) ? last.attachments : [];
|
|
134
|
+
const context = attachments.find((entry) => entry?.name === "error-context")?.path;
|
|
135
|
+
const trace = attachments.find((entry) => entry?.name === "trace")?.path;
|
|
136
|
+
if (inside(context)) {
|
|
137
|
+
try {
|
|
138
|
+
if ((await lstat(context)).size <= CONTEXT_LIMIT) {
|
|
139
|
+
let traceBytes;
|
|
140
|
+
if (inside(trace))
|
|
141
|
+
traceBytes = (await lstat(trace)).size > TRACE_LIMIT ? "unreadable" : await readFile(trace);
|
|
142
|
+
explanation = explainFailedTest(dirname(context), await readFile(context, "utf8"), traceBytes);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
catch { /* unread: the failure stays outside a locator or assertion */ }
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
const side = rerunSide(outcome, typeof last?.status === "string" ? last.status : "", explanation);
|
|
149
|
+
const project = typeof test?.projectName === "string" ? test.projectName : "";
|
|
150
|
+
if (!results.has(key))
|
|
151
|
+
results.set(key, new Map());
|
|
152
|
+
results.get(key).set(project, side);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return { results, loadErrors };
|
|
157
|
+
}
|
|
158
|
+
/** Playwright reads each path argument as a regular expression, so a literal path is escaped. */
|
|
159
|
+
const target = (path, line) => `${path.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}:${line}`;
|
|
160
|
+
const notRerun = (reason, old = "not-run", newSide = "not-run") => ({ rerun: { outcome: "inconclusive", old, new: newSide, reason } });
|
|
161
|
+
/**
|
|
162
|
+
* Reruns the flagged tests of a static report and returns the report with `rerun` (and `direction` where the change
|
|
163
|
+
* was needed) on each flagged test. Copies are removed in every case; git status is compared before and after.
|
|
164
|
+
*/
|
|
165
|
+
export async function rerunAudit(cwd, report, options) {
|
|
166
|
+
const root = (await git(cwd, ["rev-parse", "--show-toplevel"])).trim();
|
|
167
|
+
if ((await git(root, ["rev-parse", "HEAD"])).trim() !== report.head) {
|
|
168
|
+
throw new RerunUsageError("--rerun runs the tests in this checkout, so the checkout must be at --head. Check it out first, or leave --head out.");
|
|
169
|
+
}
|
|
170
|
+
if ((await git(root, ["status", "--porcelain=v1", "-z", "--untracked-files=no"])).length) {
|
|
171
|
+
throw new RerunUsageError("--rerun needs a checkout without changes to tracked files, so that the build under test is --head.");
|
|
172
|
+
}
|
|
173
|
+
const before = await treeState(root);
|
|
174
|
+
const stale = [...before.keys()].filter((path) => RERUN_COPY.test(path));
|
|
175
|
+
if (stale.length)
|
|
176
|
+
throw new RerunUsageError(`Remove the copies an earlier rerun left behind first: ${stale.slice(0, 10).map(plainText).join(", ")}.`);
|
|
177
|
+
const config = options.config ? resolve(cwd, options.config) : undefined;
|
|
178
|
+
if (config && !(await exists(config)))
|
|
179
|
+
throw new RerunUsageError(`--config ${JSON.stringify(options.config)} does not exist.`);
|
|
180
|
+
const outcomes = new Map();
|
|
181
|
+
const planned = [];
|
|
182
|
+
const copies = [];
|
|
183
|
+
const baseSources = new Map();
|
|
184
|
+
const headOnly = new Map();
|
|
185
|
+
let count = 0;
|
|
186
|
+
for (const file of report.files) {
|
|
187
|
+
if (file.unreadable)
|
|
188
|
+
continue;
|
|
189
|
+
const basePath = file.previousPath ?? file.path;
|
|
190
|
+
for (const test of file.tests) {
|
|
191
|
+
if (!test.findings.some((finding) => finding.effect !== "neutral"))
|
|
192
|
+
continue;
|
|
193
|
+
// test.only focuses the whole Playwright run, so it cannot run beside the others; --fail-on-change-needed fails on it.
|
|
194
|
+
if (test.findings.some((finding) => finding.kind === "only-added")) {
|
|
195
|
+
outcomes.set(test, { rerun: { outcome: "not-applicable", old: "not-run", new: "not-run", reason: "test.only focuses the whole Playwright run, so it is not rerun beside other tests." } });
|
|
196
|
+
continue;
|
|
197
|
+
}
|
|
198
|
+
if (HOOK_TITLE.test(test.title.at(-1))) {
|
|
199
|
+
outcomes.set(test, notRerun("A hook cannot be rerun on its own; rerun the tests it covers."));
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (!test.baseLine) {
|
|
203
|
+
outcomes.set(test, { rerun: { outcome: "not-applicable", old: "absent", new: "not-run", reason: "A new test has no old version." } });
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
if (++count > MAX_RERUN_TESTS) {
|
|
207
|
+
outcomes.set(test, notRerun(`More than ${MAX_RERUN_TESTS} tests were flagged; this one was not rerun.`));
|
|
208
|
+
continue;
|
|
209
|
+
}
|
|
210
|
+
if (!baseSources.has(basePath)) {
|
|
211
|
+
const source = await git(root, ["cat-file", "blob", `${report.base}:${basePath}`]).catch(() => undefined);
|
|
212
|
+
baseSources.set(basePath, source !== undefined && Buffer.byteLength(source) <= MAX_SOURCE_BYTES ? source : undefined);
|
|
213
|
+
}
|
|
214
|
+
const source = baseSources.get(basePath);
|
|
215
|
+
if (source === undefined) {
|
|
216
|
+
outcomes.set(test, notRerun("The old version could not be read again."));
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
const read = readTests(source);
|
|
220
|
+
if (isParameterised(read, test.baseLine)) {
|
|
221
|
+
outcomes.set(test, notRerun("The test's title is built when the tests run, so its line may stand for several tests; it is not rerun on its own."));
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
if ([...read.tests, ...read.parameterised].some((entry) => entry.modifiers.has("only"))) {
|
|
225
|
+
outcomes.set(test, notRerun("The old file marks a test only, which would focus the whole run."));
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
if (runsSerially(tokenize(source))) {
|
|
229
|
+
outcomes.set(test, notRerun("The old file runs tests in serial mode, so one test cannot be rerun on its own."));
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
if (usesSnapshotFiles(read, test.baseLine)) {
|
|
233
|
+
outcomes.set(test, notRerun("The old version compares snapshot files, which are found by the test file's name, so a copy cannot use them."));
|
|
234
|
+
continue;
|
|
235
|
+
}
|
|
236
|
+
const directory = join(root, ...posix.dirname(basePath).split("/"));
|
|
237
|
+
if (!(await exists(directory))) {
|
|
238
|
+
outcomes.set(test, notRerun("The old version's directory does not exist at head."));
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (file.status !== "deleted" && !headOnly.has(file.path)) {
|
|
242
|
+
const current = await git(root, ["cat-file", "blob", `${report.head}:${file.path}`]).catch(() => "");
|
|
243
|
+
headOnly.set(file.path, Buffer.byteLength(current) > MAX_SOURCE_BYTES || (() => { try {
|
|
244
|
+
const now = readTests(current);
|
|
245
|
+
return [...now.tests, ...now.parameterised].some((entry) => entry.modifiers.has("only"));
|
|
246
|
+
}
|
|
247
|
+
catch {
|
|
248
|
+
return true;
|
|
249
|
+
} })());
|
|
250
|
+
}
|
|
251
|
+
// A head file with test.only would focus the run on itself: its tests' new versions are not run.
|
|
252
|
+
const atHead = file.status !== "deleted" && test.headLine !== undefined;
|
|
253
|
+
const head = atHead && !headOnly.get(file.path) ? { path: join(root, ...file.path.split("/")), line: test.headLine } : undefined;
|
|
254
|
+
const control = controlTestLine(read, new Set(file.tests.flatMap((entry) => entry.baseLine ? [entry.baseLine] : [])));
|
|
255
|
+
planned.push({ test, copy: basePath, baseLine: test.baseLine, ...(control ? { control } : {}), ...(head ? { head } : atHead ? { headHeld: true } : {}) });
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
let child;
|
|
259
|
+
const output = await mkdtemp(join(tmpdir(), "agentpath-rerun-"));
|
|
260
|
+
const cleanup = () => {
|
|
261
|
+
for (const copy of copies) {
|
|
262
|
+
try {
|
|
263
|
+
unlinkSync(copy);
|
|
264
|
+
}
|
|
265
|
+
catch { /* already gone */ }
|
|
266
|
+
}
|
|
267
|
+
// The output holds traces of the pages; it goes with the copies, also on Ctrl-C.
|
|
268
|
+
try {
|
|
269
|
+
rmSync(output, { recursive: true, force: true });
|
|
270
|
+
}
|
|
271
|
+
catch { /* best effort */ }
|
|
272
|
+
// SIGINT lets Playwright end its web server and browsers after this process has gone.
|
|
273
|
+
if (child?.pid) {
|
|
274
|
+
try {
|
|
275
|
+
if (process.platform === "win32")
|
|
276
|
+
child.kill("SIGINT");
|
|
277
|
+
else
|
|
278
|
+
process.kill(-child.pid, "SIGINT");
|
|
279
|
+
}
|
|
280
|
+
catch { /* gone */ }
|
|
281
|
+
}
|
|
282
|
+
};
|
|
283
|
+
const onSignal = (signal) => { cleanup(); process.exit(signal === "SIGINT" ? 130 : 143); };
|
|
284
|
+
process.once("SIGINT", onSignal);
|
|
285
|
+
process.once("SIGTERM", onSignal);
|
|
286
|
+
const deadline = Date.now() + options.timeoutMs;
|
|
287
|
+
let timedOut = false;
|
|
288
|
+
try {
|
|
289
|
+
// One copy per base file, written beside it with exclusive creation; never over an existing file.
|
|
290
|
+
const copyOf = new Map();
|
|
291
|
+
for (const entry of planned) {
|
|
292
|
+
if (!copyOf.has(entry.copy)) {
|
|
293
|
+
const name = rerunCopyName(posix.basename(entry.copy), randomBytes(4).toString("hex"));
|
|
294
|
+
const path = join(root, ...posix.dirname(entry.copy).split("/"), name);
|
|
295
|
+
await writeFile(path, baseSources.get(entry.copy), { flag: "wx" });
|
|
296
|
+
copies.push(path);
|
|
297
|
+
copyOf.set(entry.copy, await realpath(path));
|
|
298
|
+
}
|
|
299
|
+
entry.copy = copyOf.get(entry.copy);
|
|
300
|
+
}
|
|
301
|
+
// Group by configuration: the nearest playwright.config above the copy, unless --config was given.
|
|
302
|
+
const groups = new Map();
|
|
303
|
+
for (const entry of planned) {
|
|
304
|
+
const found = config ?? await findConfig(root, dirname(entry.copy));
|
|
305
|
+
if (!found) {
|
|
306
|
+
outcomes.set(entry.test, notRerun("No playwright.config was found above the test file. Pass --config."));
|
|
307
|
+
continue;
|
|
308
|
+
}
|
|
309
|
+
if (!groups.has(found))
|
|
310
|
+
groups.set(found, []);
|
|
311
|
+
groups.get(found).push(entry);
|
|
312
|
+
}
|
|
313
|
+
let run = 0;
|
|
314
|
+
for (const [configFile, entries] of groups) {
|
|
315
|
+
const cli = playwrightCli(dirname(configFile));
|
|
316
|
+
if (!cli) {
|
|
317
|
+
for (const entry of entries)
|
|
318
|
+
outcomes.set(entry.test, notRerun("Playwright is not installed for this configuration."));
|
|
319
|
+
continue;
|
|
320
|
+
}
|
|
321
|
+
const common = ["--config", configFile, "--reporter=json", "--retries=0", "--trace=retain-on-failure", "--update-snapshots=none", ...(options.project ? ["--project", options.project] : [])];
|
|
322
|
+
const once = async (targets) => {
|
|
323
|
+
const resultsFile = join(output, `results-${++run}.json`);
|
|
324
|
+
const directory = join(output, `run-${run}`);
|
|
325
|
+
timedOut = (await runPlaywright(cli, ["test", ...new Set(targets), ...common, `--output=${directory}`], dirname(configFile), resultsFile, deadline - Date.now(), (value) => { child = value; })) || timedOut;
|
|
326
|
+
return readResults(resultsFile, directory);
|
|
327
|
+
};
|
|
328
|
+
// Head files by their real path, as Playwright's results name them.
|
|
329
|
+
const headFile = new Map();
|
|
330
|
+
for (const entry of entries)
|
|
331
|
+
if (entry.head)
|
|
332
|
+
headFile.set(entry, await realpath(entry.head.path).catch(() => entry.head.path));
|
|
333
|
+
const headKey = (entry) => `${headFile.get(entry)}:${entry.head.line}`;
|
|
334
|
+
const targetsOf = (group) => [
|
|
335
|
+
...group.flatMap((entry) => [target(entry.copy, entry.baseLine), ...(entry.control ? [target(entry.copy, entry.control)] : [])]),
|
|
336
|
+
...group.flatMap((entry) => entry.head ? [target(headFile.get(entry), entry.head.line)] : []),
|
|
337
|
+
];
|
|
338
|
+
const combined = timedOut ? { results: new Map(), loadErrors: false } : await once(targetsOf(entries));
|
|
339
|
+
const first = combined.results;
|
|
340
|
+
// A file Playwright cannot load stops the whole run, so each copy left without results is run on its own.
|
|
341
|
+
const unloaded = new Set();
|
|
342
|
+
if (combined.loadErrors) {
|
|
343
|
+
for (const copy of new Set(entries.filter((entry) => !first.has(`${entry.copy}:${entry.baseLine}`)).map((entry) => entry.copy))) {
|
|
344
|
+
if (timedOut)
|
|
345
|
+
break;
|
|
346
|
+
const alone = await once(targetsOf(entries.filter((entry) => entry.copy === copy)));
|
|
347
|
+
for (const [key, value] of alone.results)
|
|
348
|
+
first.set(key, value);
|
|
349
|
+
if (alone.loadErrors)
|
|
350
|
+
unloaded.add(copy);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
// A failure counts only once a second run of the old version fails the same way.
|
|
354
|
+
const failed = entries.filter((entry) => [...(first.get(`${entry.copy}:${entry.baseLine}`)?.values() ?? [])].some((side) => side.status === "failed" && side.pageFailure));
|
|
355
|
+
const confirm = failed.length && !timedOut ? (await once(failed.map((entry) => target(entry.copy, entry.baseLine)))).results : new Map();
|
|
356
|
+
for (const entry of entries) {
|
|
357
|
+
if (timedOut && !first.size) {
|
|
358
|
+
outcomes.set(entry.test, notRerun("The rerun ran out of time."));
|
|
359
|
+
continue;
|
|
360
|
+
}
|
|
361
|
+
const old = first.get(`${entry.copy}:${entry.baseLine}`) ?? new Map();
|
|
362
|
+
if (!old.size && unloaded.has(entry.copy)) {
|
|
363
|
+
outcomes.set(entry.test, notRerun("Playwright could not load the old version or found no test in it: an import it uses may have changed at head, or testMatch or testIgnore may exclude the copy."));
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
const head = entry.head ? first.get(headKey(entry)) ?? new Map() : entry.headHeld ? new Map() : "absent";
|
|
367
|
+
const control = entry.control ? first.get(`${entry.copy}:${entry.control}`) : undefined;
|
|
368
|
+
outcomes.set(entry.test, classifyRerun(old, confirm.get(`${entry.copy}:${entry.baseLine}`) ?? new Map(), head, control ?? new Map()));
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
finally {
|
|
373
|
+
process.removeListener("SIGINT", onSignal);
|
|
374
|
+
process.removeListener("SIGTERM", onSignal);
|
|
375
|
+
cleanup();
|
|
376
|
+
}
|
|
377
|
+
const after = await treeState(root);
|
|
378
|
+
const changed = [...new Set([...before.keys(), ...after.keys()])].filter((path) => before.get(path) !== after.get(path)).sort();
|
|
379
|
+
const files = report.files.map((file) => ({ ...file, tests: file.tests.map((test) => {
|
|
380
|
+
const outcome = outcomes.get(test);
|
|
381
|
+
return outcome ? { ...test, rerun: outcome.rerun, ...(outcome.direction ? { direction: outcome.direction } : {}) } : test;
|
|
382
|
+
}) }));
|
|
383
|
+
return auditReportSchema.parse({ ...report, files, rerun: { treeChanged: changed.slice(0, 200).map((path) => plainText(path).slice(0, 2000)), timedOut } });
|
|
384
|
+
}
|