@mostra/runtime 0.0.0-stage → 0.2.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/LICENSE +202 -0
- package/README.md +39 -2
- package/dist/bin.d.ts +1 -0
- package/dist/bin.js +236 -0
- package/dist/bin.js.map +1 -0
- package/dist/builder/child/main.d.ts +1 -0
- package/dist/builder/child/main.js +1408 -0
- package/dist/builder/child/main.js.map +1 -0
- package/dist/child-xrHmytks.js +49 -0
- package/dist/child-xrHmytks.js.map +1 -0
- package/dist/deps/child/install.d.ts +1 -0
- package/dist/deps/child/install.js +33 -0
- package/dist/deps/child/install.js.map +1 -0
- package/dist/index.d.ts +221 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/instance-CzBkUyHu.js +15920 -0
- package/dist/instance-CzBkUyHu.js.map +1 -0
- package/dist/types-NjNy7gXX.js +57 -0
- package/dist/types-NjNy7gXX.js.map +1 -0
- package/package.json +87 -3
- package/src/actions/index.ts +622 -0
- package/src/addressing/index.ts +281 -0
- package/src/bin.ts +240 -0
- package/src/builder/child/dev.ts +383 -0
- package/src/builder/child/fields.ts +363 -0
- package/src/builder/child/main.ts +47 -0
- package/src/builder/child/plugins.ts +195 -0
- package/src/builder/child/scan.ts +459 -0
- package/src/builder/child/spa.ts +348 -0
- package/src/builder/coverage.ts +72 -0
- package/src/builder/index.ts +406 -0
- package/src/builder/types.ts +180 -0
- package/src/deps/child/install.ts +30 -0
- package/src/deps/index.ts +918 -0
- package/src/deps/installer.ts +321 -0
- package/src/deps/lock.ts +82 -0
- package/src/deps/scan.ts +266 -0
- package/src/deps/specifier.ts +64 -0
- package/src/deps/warm.ts +57 -0
- package/src/dev/index.ts +518 -0
- package/src/faults.ts +65 -0
- package/src/gate.ts +55 -0
- package/src/gateway/actions.ts +268 -0
- package/src/gateway/browser.ts +650 -0
- package/src/gateway/capabilities.ts +760 -0
- package/src/gateway/controls.ts +91 -0
- package/src/gateway/dev.ts +119 -0
- package/src/gateway/http.ts +229 -0
- package/src/gateway/identity.ts +101 -0
- package/src/gateway/index.ts +1299 -0
- package/src/gateway/integrity.ts +117 -0
- package/src/gateway/listener.ts +217 -0
- package/src/gateway/routes.ts +197 -0
- package/src/gateway/serve.ts +138 -0
- package/src/gc/index.ts +451 -0
- package/src/home/acquire.ts +276 -0
- package/src/home/index.ts +16 -0
- package/src/home/paths.ts +192 -0
- package/src/home/platform.ts +34 -0
- package/src/home/record.ts +76 -0
- package/src/home/settings.ts +113 -0
- package/src/index.ts +30 -0
- package/src/instance/index.ts +979 -0
- package/src/interaction/ephemeral.ts +300 -0
- package/src/interaction/events.ts +355 -0
- package/src/interaction/fields.ts +392 -0
- package/src/interaction/index.ts +1013 -0
- package/src/interaction/outbox.ts +493 -0
- package/src/interaction/policy.ts +26 -0
- package/src/interaction/results.ts +341 -0
- package/src/interaction/runs.ts +313 -0
- package/src/interaction/signals.ts +120 -0
- package/src/interaction/submissions.ts +585 -0
- package/src/interaction/subscriptions.ts +258 -0
- package/src/interaction/viewers.ts +187 -0
- package/src/interaction/wait.ts +201 -0
- package/src/ledger/index.ts +442 -0
- package/src/ledger/migrations.ts +596 -0
- package/src/ledger/requests.ts +105 -0
- package/src/ledger/types.ts +28 -0
- package/src/live/index.ts +573 -0
- package/src/log.ts +316 -0
- package/src/processes/child.ts +67 -0
- package/src/processes/environment.ts +64 -0
- package/src/processes/identity.ts +191 -0
- package/src/processes/index.ts +19 -0
- package/src/processes/manager.ts +543 -0
- package/src/protocol-server/client.ts +94 -0
- package/src/protocol-server/index.ts +91 -0
- package/src/protocol-server/server.ts +518 -0
- package/src/protocol-server/streams.ts +81 -0
- package/src/publish/checks.ts +92 -0
- package/src/publish/digest.ts +87 -0
- package/src/publish/index.ts +23 -0
- package/src/publish/manifest.ts +409 -0
- package/src/publish/methods.ts +96 -0
- package/src/publish/publisher.ts +913 -0
- package/src/publish/source.ts +272 -0
- package/src/publish/tar.ts +110 -0
- package/src/registry/index.ts +481 -0
- package/src/registry/store.ts +136 -0
- package/src/secrets/index.ts +223 -0
- package/src/service/idle.ts +64 -0
- package/src/service/index.ts +3 -0
- package/src/service/unit.ts +250 -0
- package/src/verify/fields.ts +125 -0
- package/src/verify/index.ts +913 -0
- package/src/verify/regression.ts +157 -0
- package/src/verify/runner.ts +230 -0
- package/src/verify/sandbox.ts +407 -0
- package/src/verify/typecheck.ts +371 -0
- package/src/workers/index.ts +17 -0
- package/src/workers/launcher.ts +260 -0
- package/src/workers/pool.ts +708 -0
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Regressions against the last passing version (AX-04, ROUND-3 §9, D138 item 4): contracts from
|
|
3
|
+
* the two versions' `registry.json`, the controls and landmarks of their ARIA snapshots, and the
|
|
4
|
+
* share of pixels that differ. Every regression is a warning that says what was observed (D72):
|
|
5
|
+
* a redesign may change any of them on purpose.
|
|
6
|
+
*/
|
|
7
|
+
import { createDiagnostic, type Diagnostic } from "@mostra/protocol";
|
|
8
|
+
import { coverageMatcher, type VersionRegistry } from "../builder/index.ts";
|
|
9
|
+
|
|
10
|
+
/** The share of differing pixels above which a viewport counts as changed. */
|
|
11
|
+
export const PIXEL_THRESHOLD = 0.01;
|
|
12
|
+
|
|
13
|
+
/** The roles a regression of the ARIA snapshot compares: controls and landmarks. */
|
|
14
|
+
const COMPARED_ROLES = new Set([
|
|
15
|
+
"button",
|
|
16
|
+
"link",
|
|
17
|
+
"textbox",
|
|
18
|
+
"searchbox",
|
|
19
|
+
"checkbox",
|
|
20
|
+
"radio",
|
|
21
|
+
"switch",
|
|
22
|
+
"combobox",
|
|
23
|
+
"listbox",
|
|
24
|
+
"slider",
|
|
25
|
+
"spinbutton",
|
|
26
|
+
"tab",
|
|
27
|
+
"menuitem",
|
|
28
|
+
"main",
|
|
29
|
+
"navigation",
|
|
30
|
+
"banner",
|
|
31
|
+
"contentinfo",
|
|
32
|
+
"form",
|
|
33
|
+
"search",
|
|
34
|
+
"region",
|
|
35
|
+
"dialog",
|
|
36
|
+
]);
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The controls and landmarks of an ARIA snapshot, as `role "name"` with digits folded, so a
|
|
40
|
+
* counter or a date in a name doesn't read as a removed control.
|
|
41
|
+
*/
|
|
42
|
+
export function ariaControls(snapshot: string): string[] {
|
|
43
|
+
const found: string[] = [];
|
|
44
|
+
for (const match of snapshot.matchAll(/^\s*- ([a-z]+)(?: "((?:[^"\\]|\\.)*)")?/gm)) {
|
|
45
|
+
const role = match[1] as string;
|
|
46
|
+
if (!COMPARED_ROLES.has(role)) continue;
|
|
47
|
+
const name = (match[2] ?? "").replace(/\d+/g, "#").trim();
|
|
48
|
+
found.push(name ? `${role} "${name}"` : role);
|
|
49
|
+
}
|
|
50
|
+
return found;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/** The controls and landmarks the baseline had and the candidate doesn't, counting repeats. */
|
|
54
|
+
export function removedControls(baseline: string, candidate: string): string[] {
|
|
55
|
+
const left = new Map<string, number>();
|
|
56
|
+
for (const control of ariaControls(candidate)) left.set(control, (left.get(control) ?? 0) + 1);
|
|
57
|
+
const removed: string[] = [];
|
|
58
|
+
for (const control of ariaControls(baseline)) {
|
|
59
|
+
const count = left.get(control) ?? 0;
|
|
60
|
+
if (count > 0) left.set(control, count - 1);
|
|
61
|
+
else removed.push(control);
|
|
62
|
+
}
|
|
63
|
+
return removed;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** The ARIA regression of one viewport, if the baseline had controls the candidate lacks. */
|
|
67
|
+
export function ariaRegression(
|
|
68
|
+
baseline: string,
|
|
69
|
+
candidate: string,
|
|
70
|
+
viewport: string,
|
|
71
|
+
): { readonly diagnostic: Diagnostic; readonly summary: string } | undefined {
|
|
72
|
+
const removed = removedControls(baseline, candidate);
|
|
73
|
+
if (removed.length === 0) return undefined;
|
|
74
|
+
const shown = removed.slice(0, 5).join(", ");
|
|
75
|
+
const more = removed.length > 5 ? ` and ${removed.length - 5} more` : "";
|
|
76
|
+
const summary = `${viewport}: ${shown}${more}`;
|
|
77
|
+
return {
|
|
78
|
+
summary,
|
|
79
|
+
diagnostic: createDiagnostic("MOSTRA_REGRESSION_ARIA", {
|
|
80
|
+
message: `Observed at ${viewport}: the last passing version showed ${shown}${more}, which this source doesn't.`,
|
|
81
|
+
location: { viewport },
|
|
82
|
+
}),
|
|
83
|
+
};
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/** The pixel regression of one viewport, when its difference passes {@link PIXEL_THRESHOLD}. */
|
|
87
|
+
export function pixelRegression(
|
|
88
|
+
ratio: number,
|
|
89
|
+
sizeChanged: boolean,
|
|
90
|
+
viewport: string,
|
|
91
|
+
diffPath: string,
|
|
92
|
+
): Diagnostic | undefined {
|
|
93
|
+
if (!sizeChanged && ratio <= PIXEL_THRESHOLD) return undefined;
|
|
94
|
+
return createDiagnostic("MOSTRA_REGRESSION_PIXELS", {
|
|
95
|
+
message: `Observed at ${viewport}: ${(ratio * 100).toFixed(1)} % of the pixels differ from the last passing version, rendered in the same run with the same clock and data. The difference is in ${diffPath}.`,
|
|
96
|
+
location: { viewport },
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* What the candidate's registry drops of the baseline's: literal field IDs it no longer covers,
|
|
102
|
+
* server functions, live sources and page routes. `savedAnswers` counts the answers the home holds
|
|
103
|
+
* under a field ID, which become orphans when viewers switch (D42).
|
|
104
|
+
*/
|
|
105
|
+
export function contractRegressions(
|
|
106
|
+
baseline: VersionRegistry,
|
|
107
|
+
candidate: VersionRegistry,
|
|
108
|
+
savedAnswers: (fieldId: string) => number,
|
|
109
|
+
): { readonly diagnostics: Diagnostic[]; readonly summary: string[] } {
|
|
110
|
+
const diagnostics: Diagnostic[] = [];
|
|
111
|
+
const summary: string[] = [];
|
|
112
|
+
const covers = coverageMatcher(candidate.fields);
|
|
113
|
+
const fields = new Set(
|
|
114
|
+
(baseline.fields?.entries ?? []).flatMap((entry) => (entry.id !== undefined ? [entry.id] : [])),
|
|
115
|
+
);
|
|
116
|
+
for (const id of [...fields].sort()) {
|
|
117
|
+
if (covers(id)) continue;
|
|
118
|
+
const saved = savedAnswers(id);
|
|
119
|
+
summary.push(`field ${id}`);
|
|
120
|
+
diagnostics.push(
|
|
121
|
+
createDiagnostic("MOSTRA_REGRESSION_FIELD_REMOVED", {
|
|
122
|
+
message: `Observed: the last passing version used the field ID "${id}" and this source doesn't${saved > 0 ? `; ${saved} saved ${saved === 1 ? "answer" : "answers"} under it won't carry over` : ""}.`,
|
|
123
|
+
}),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
const actions = new Set(candidate.actions.map((entry) => entry.id));
|
|
127
|
+
for (const entry of baseline.actions) {
|
|
128
|
+
if (actions.has(entry.id)) continue;
|
|
129
|
+
summary.push(`action ${entry.id}`);
|
|
130
|
+
diagnostics.push(
|
|
131
|
+
createDiagnostic("MOSTRA_REGRESSION_ACTION_REMOVED", {
|
|
132
|
+
message: `Observed: the last passing version had the server function ${entry.id} and this source doesn't.`,
|
|
133
|
+
}),
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
const sources = new Set(candidate.liveSources.map((entry) => entry.key));
|
|
137
|
+
for (const entry of baseline.liveSources) {
|
|
138
|
+
if (sources.has(entry.key)) continue;
|
|
139
|
+
summary.push(`live source ${entry.key}`);
|
|
140
|
+
diagnostics.push(
|
|
141
|
+
createDiagnostic("MOSTRA_REGRESSION_LIVE_REMOVED", {
|
|
142
|
+
message: `Observed: the last passing version had the live source ${entry.key} and this source doesn't.`,
|
|
143
|
+
}),
|
|
144
|
+
);
|
|
145
|
+
}
|
|
146
|
+
const pages = new Set(candidate.pages.map((page) => page.path));
|
|
147
|
+
for (const page of baseline.pages) {
|
|
148
|
+
if (pages.has(page.path)) continue;
|
|
149
|
+
summary.push(`page ${page.path}`);
|
|
150
|
+
diagnostics.push(
|
|
151
|
+
createDiagnostic("MOSTRA_REGRESSION_PAGE_REMOVED", {
|
|
152
|
+
message: `Observed: the last passing version had the page ${page.path} and this source doesn't.`,
|
|
153
|
+
}),
|
|
154
|
+
);
|
|
155
|
+
}
|
|
156
|
+
return { diagnostics, summary };
|
|
157
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The supervisor's side of the verify runner (ARCHITECTURE §1, FAILURE_MODEL §2): one runner child
|
|
3
|
+
* at a time, started through the process manager on the first render and kept while it lives (it
|
|
4
|
+
* exits by itself after 10 idle minutes, closing its warm Chromium). A runner that dies during a
|
|
5
|
+
* request fails it with `MOSTRA_VERIFY_CRASHED` and its last lines; the next request starts a
|
|
6
|
+
* fresh one. The supervisor never loads Playwright (RT-02).
|
|
7
|
+
*/
|
|
8
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
9
|
+
import { join, resolve } from "node:path";
|
|
10
|
+
import {
|
|
11
|
+
BUNDLE_NAME,
|
|
12
|
+
type CompareRequest,
|
|
13
|
+
type CompareResult,
|
|
14
|
+
createDiagnostic,
|
|
15
|
+
type RenderRequest,
|
|
16
|
+
type RenderResult,
|
|
17
|
+
RpcError,
|
|
18
|
+
VERIFY_RUNNER_METHODS,
|
|
19
|
+
} from "@mostra/protocol";
|
|
20
|
+
import type { Environment, Log } from "../log.ts";
|
|
21
|
+
import type { ChildHandle, ProcessManager } from "../processes/index.ts";
|
|
22
|
+
|
|
23
|
+
/** Where the runner child is and where its browser lives. */
|
|
24
|
+
export interface RunnerLocation {
|
|
25
|
+
readonly entry: string;
|
|
26
|
+
readonly nodeArgs: readonly string[];
|
|
27
|
+
/**
|
|
28
|
+
* The runtime bundle's own browsers (`<bundle>/browsers`, D142), when this runtime runs from a
|
|
29
|
+
* bundle: the runner uses exactly the browser the bundle's setup fetched and checked, whatever
|
|
30
|
+
* `PLAYWRIGHT_BROWSERS_PATH` the environment names.
|
|
31
|
+
*/
|
|
32
|
+
readonly browsers?: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** The runtime bundle this runtime sits in (four levels above its package), if any. */
|
|
36
|
+
export function enclosingBundle(runtimeRoot: string): string | undefined {
|
|
37
|
+
const root = resolve(runtimeRoot, "..", "..", "..", "..");
|
|
38
|
+
try {
|
|
39
|
+
const info = JSON.parse(readFileSync(join(root, "bundle.json"), "utf8")) as { name?: unknown };
|
|
40
|
+
return info.name === BUNDLE_NAME ? root : undefined;
|
|
41
|
+
} catch {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** The runner child's entry and Node arguments: `MOSTRA_VERIFY_ENTRY`, else beside the runtime. */
|
|
47
|
+
export function locateRunner(env: Environment, runtimeRoot: string): RunnerLocation {
|
|
48
|
+
const bundle = enclosingBundle(runtimeRoot);
|
|
49
|
+
const browsers = bundle !== undefined ? { browsers: join(bundle, "browsers") } : {};
|
|
50
|
+
const override = env["MOSTRA_VERIFY_ENTRY"];
|
|
51
|
+
if (override) return { entry: override, nodeArgs: [], ...browsers };
|
|
52
|
+
const fromSource = import.meta.url.endsWith(".ts");
|
|
53
|
+
const built = resolve(runtimeRoot, "..", "verify", "dist", "main.js");
|
|
54
|
+
if (!fromSource || existsSync(built)) return { entry: built, nodeArgs: [], ...browsers };
|
|
55
|
+
return {
|
|
56
|
+
entry: resolve(runtimeRoot, "..", "verify", "src", "main.ts"),
|
|
57
|
+
nodeArgs: ["--conditions=@mostra/source"],
|
|
58
|
+
...browsers,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* How long past its deadline a runner may take to answer: the runner abandons a render 1 s past
|
|
64
|
+
* its deadline itself (closing that render's context, which ends a page that blocks its main
|
|
65
|
+
* thread), so only a runner this late is stuck as a whole and is stopped.
|
|
66
|
+
*/
|
|
67
|
+
const OVERDUE_GRACE_MS = 8_000;
|
|
68
|
+
|
|
69
|
+
/** The runner as the verify pipeline uses it. */
|
|
70
|
+
export interface RunnerClient {
|
|
71
|
+
/**
|
|
72
|
+
* Renders one link. A runner that hasn't answered shortly after `request.deadline` (a page whose
|
|
73
|
+
* code blocks the browser, say) is stopped, browser and all, and the call fails with
|
|
74
|
+
* `MOSTRA_VERIFY_TIMEOUT` (FAILURE_MODEL §9).
|
|
75
|
+
*/
|
|
76
|
+
render(request: RenderRequest): Promise<RenderResult>;
|
|
77
|
+
/** Compares two screenshots, bounded by `deadline` like a render. */
|
|
78
|
+
compare(request: CompareRequest, deadline: number): Promise<CompareResult>;
|
|
79
|
+
/** Stops the runner child, if one runs. */
|
|
80
|
+
close(): Promise<void>;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** The variables the runner child gets: where Playwright's browsers are, and nothing secret. */
|
|
84
|
+
export function runnerEnvironment(env: Environment, browsers?: string): Environment {
|
|
85
|
+
const keep = [
|
|
86
|
+
"PATH",
|
|
87
|
+
"HOME",
|
|
88
|
+
"TMPDIR",
|
|
89
|
+
"LANG",
|
|
90
|
+
"LC_ALL",
|
|
91
|
+
"TZ",
|
|
92
|
+
"PLAYWRIGHT_BROWSERS_PATH",
|
|
93
|
+
"XDG_CACHE_HOME",
|
|
94
|
+
];
|
|
95
|
+
const out: Record<string, string> = {};
|
|
96
|
+
for (const name of keep) {
|
|
97
|
+
const value = env[name];
|
|
98
|
+
if (value !== undefined) out[name] = value;
|
|
99
|
+
}
|
|
100
|
+
if (browsers !== undefined) out["PLAYWRIGHT_BROWSERS_PATH"] = browsers;
|
|
101
|
+
const idle = env["MOSTRA_TEST_VERIFY_IDLE_MS"];
|
|
102
|
+
if (idle) out["MOSTRA_TEST_VERIFY_IDLE_MS"] = idle;
|
|
103
|
+
return out;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Creates the client; nothing starts until the first request. */
|
|
107
|
+
export function createRunnerClient(options: {
|
|
108
|
+
readonly processes: ProcessManager;
|
|
109
|
+
readonly log: Log;
|
|
110
|
+
readonly env: Environment;
|
|
111
|
+
readonly entry: string;
|
|
112
|
+
readonly nodeArgs: readonly string[];
|
|
113
|
+
/** The bundle's browsers directory, when the runtime runs from a bundle. */
|
|
114
|
+
readonly browsers?: string | undefined;
|
|
115
|
+
}): RunnerClient {
|
|
116
|
+
let child: Promise<ChildHandle> | undefined;
|
|
117
|
+
|
|
118
|
+
const start = (): Promise<ChildHandle> => {
|
|
119
|
+
child ??= options.processes
|
|
120
|
+
.spawn({
|
|
121
|
+
kind: "verify",
|
|
122
|
+
ownerRef: "verify-runner",
|
|
123
|
+
command: process.execPath,
|
|
124
|
+
args: [...options.nodeArgs, options.entry],
|
|
125
|
+
env: runnerEnvironment(options.env, options.browsers),
|
|
126
|
+
ipc: {},
|
|
127
|
+
})
|
|
128
|
+
.then((handle) => {
|
|
129
|
+
void handle.exited.then((exit) => {
|
|
130
|
+
options.log.info("verify.runner_exited", { code: exit.code, signal: exit.signal });
|
|
131
|
+
if (child) {
|
|
132
|
+
void child.then((current) => {
|
|
133
|
+
if (current === handle) child = undefined;
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
});
|
|
137
|
+
return handle;
|
|
138
|
+
})
|
|
139
|
+
.catch((error: unknown) => {
|
|
140
|
+
child = undefined;
|
|
141
|
+
throw error;
|
|
142
|
+
});
|
|
143
|
+
return child;
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
/** Runners this client stopped because a run was overdue: the others' renders are retried. */
|
|
147
|
+
const recycled = new WeakSet<object>();
|
|
148
|
+
|
|
149
|
+
const ask = async (
|
|
150
|
+
method: string,
|
|
151
|
+
params: object,
|
|
152
|
+
deadline: number,
|
|
153
|
+
retried = false,
|
|
154
|
+
): Promise<unknown> => {
|
|
155
|
+
const handle = await start();
|
|
156
|
+
let timer: ReturnType<typeof setTimeout> | undefined;
|
|
157
|
+
const overdue = new Promise<{ kind: "overdue" }>((done) => {
|
|
158
|
+
timer = setTimeout(
|
|
159
|
+
() => done({ kind: "overdue" }),
|
|
160
|
+
Math.max(0, deadline - Date.now()) + OVERDUE_GRACE_MS,
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
const outcome = await Promise.race([
|
|
164
|
+
handle.request(method, params as Record<string, unknown>).then(
|
|
165
|
+
(result) => ({ kind: "result" as const, result }),
|
|
166
|
+
(error: unknown) => ({ kind: "error" as const, error }),
|
|
167
|
+
),
|
|
168
|
+
handle.exited.then((exit) => ({ kind: "exit" as const, exit })),
|
|
169
|
+
overdue,
|
|
170
|
+
]).finally(() => clearTimeout(timer));
|
|
171
|
+
if (outcome.kind === "result") return outcome.result;
|
|
172
|
+
if (outcome.kind === "overdue") {
|
|
173
|
+
// The runner is stuck (artifact code blocking the page's main thread keeps every browser
|
|
174
|
+
// call waiting): stop it and its browser; the next run starts a fresh one.
|
|
175
|
+
options.log.warn("verify.runner_overdue", { method });
|
|
176
|
+
if (child) {
|
|
177
|
+
const stuck = child;
|
|
178
|
+
child = undefined;
|
|
179
|
+
const stopped = await stuck.catch(() => undefined);
|
|
180
|
+
if (stopped) recycled.add(stopped);
|
|
181
|
+
await stopped?.stop(2000);
|
|
182
|
+
}
|
|
183
|
+
throw new RpcError(
|
|
184
|
+
createDiagnostic("MOSTRA_VERIFY_TIMEOUT", {
|
|
185
|
+
message:
|
|
186
|
+
"Verify ran out of time: the browser stopped answering while it checked the page.",
|
|
187
|
+
}),
|
|
188
|
+
);
|
|
189
|
+
}
|
|
190
|
+
if (outcome.kind === "error" && outcome.error instanceof RpcError) {
|
|
191
|
+
// The runner answered with a diagnostic of its own (no browser, say): it stands. A closed
|
|
192
|
+
// channel or an internal error means the runner itself failed, reported as a crash.
|
|
193
|
+
const code = outcome.error.diagnostic.code;
|
|
194
|
+
if (code !== "MOSTRA_HOST_INTERNAL_ERROR" && !code.startsWith("MOSTRA_HOST_CONNECTION"))
|
|
195
|
+
throw outcome.error;
|
|
196
|
+
}
|
|
197
|
+
// Another run's recovery stopped the runner under this request: it isn't this page's fault,
|
|
198
|
+
// so the request runs once more on a fresh runner (Opus review finding 5). The retry keeps the
|
|
199
|
+
// request's deadline, so one cut off with no time left ends as a timeout, never as a crash.
|
|
200
|
+
if (recycled.has(handle) && !retried) {
|
|
201
|
+
options.log.info("verify.runner_retry", { method });
|
|
202
|
+
return ask(method, params, deadline, true);
|
|
203
|
+
}
|
|
204
|
+
const exit =
|
|
205
|
+
outcome.kind === "exit"
|
|
206
|
+
? outcome.exit
|
|
207
|
+
: await Promise.race([
|
|
208
|
+
handle.exited,
|
|
209
|
+
new Promise<undefined>((done) => setTimeout(() => done(undefined), 1000)),
|
|
210
|
+
]);
|
|
211
|
+
const tail = exit?.stderrTail.slice(-10).join("\n") ?? "";
|
|
212
|
+
throw new RpcError(
|
|
213
|
+
createDiagnostic("MOSTRA_VERIFY_CRASHED", {
|
|
214
|
+
message: `The verify runner stopped before it answered${exit ? ` (${exit.signal ?? `code ${exit.code}`})` : ""}.${tail ? `\n${tail}` : ""}`,
|
|
215
|
+
}),
|
|
216
|
+
);
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
return {
|
|
220
|
+
render: (request) =>
|
|
221
|
+
ask(VERIFY_RUNNER_METHODS.render, request, request.deadline) as Promise<RenderResult>,
|
|
222
|
+
compare: (request, deadline) =>
|
|
223
|
+
ask(VERIFY_RUNNER_METHODS.compare, request, deadline) as Promise<CompareResult>,
|
|
224
|
+
async close() {
|
|
225
|
+
const running = child;
|
|
226
|
+
child = undefined;
|
|
227
|
+
if (running) await (await running.catch(() => undefined))?.stop(2000);
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
}
|