Merge remote-tracking branch 'origin/main' into feat/pkg-inventory
# Conflicts: # docs/product-catalog.json # docs/verification/managed-conformance-manifest.json # skills/reverse-engineer-anything/SKILL.md # src/generatedMcpToolCatalog.ts
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
import { createServer } from "node:http";
|
||||
|
||||
/** Owned synthetic site keeps source bytes, repeated URLs and expected execution independent of REA. */
|
||||
export async function startBrowserRuntimeSite() {
|
||||
let origin;
|
||||
let evidenceRequests = 0;
|
||||
const sources = () => ({
|
||||
selected: `// 🔥 UTF-16 prefix\r\nfunction chosen() {\r\n history.pushState({}, '', '/same-document?caller=value#fragment');\r\n fetch('/evidence?marker=chosen');\r\n if (document.body.dataset.branch === 'unused') { fetch('/never'); }\r\n}\r\ndocument.querySelector('#run').addEventListener('click', chosen, { passive: true });\r\n//# sourceURL=${origin.replace("http://", "http://declared:label@")}/same.js\r\n`,
|
||||
other: `function untouched() { return 'other source'; }\r\ndocument.querySelector('#child').addEventListener('click', untouched);\r\n//# sourceURL=${origin.replace("http://", "http://declared:label@")}/same.js\r\n`,
|
||||
});
|
||||
const server = createServer((request, response) => {
|
||||
response.setHeader("content-type", "text/html; charset=utf-8");
|
||||
if (request.url?.startsWith("/evidence")) {
|
||||
evidenceRequests += 1;
|
||||
response.end("ok");
|
||||
return;
|
||||
}
|
||||
if (request.url === "/a.js" || request.url === "/b.js") {
|
||||
response.setHeader(
|
||||
"content-type",
|
||||
"application/javascript; charset=utf-8",
|
||||
);
|
||||
response.end(
|
||||
request.url === "/a.js" ? sources().selected : sources().other,
|
||||
);
|
||||
return;
|
||||
}
|
||||
response.end(
|
||||
'<!doctype html><meta charset="utf-8"><button id="run"><span id="child">Run</span></button><script src="/a.js"></script><script src="/b.js"></script>',
|
||||
);
|
||||
});
|
||||
await new Promise((resolve, reject) => {
|
||||
server.once("error", reject);
|
||||
server.listen(0, "127.0.0.1", resolve);
|
||||
});
|
||||
const address = server.address();
|
||||
if (address === null || typeof address === "string")
|
||||
throw new Error("Runtime fixture did not bind TCP");
|
||||
origin = `http://127.0.0.1:${address.port}`;
|
||||
return {
|
||||
origin,
|
||||
sources: sources(),
|
||||
evidenceRequests: () => evidenceRequests,
|
||||
close: () =>
|
||||
new Promise((resolve, reject) =>
|
||||
server.close((error) =>
|
||||
error === undefined ? resolve() : reject(error),
|
||||
),
|
||||
),
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,445 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn, execFile } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { once } from "node:events";
|
||||
import { access } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
import { Client } from "@modelcontextprotocol/client";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
||||
import { CdpBrowserProvider } from "../../dist/browser/CdpBrowserProvider.js";
|
||||
import { CdpConnection } from "../../dist/browser/CdpConnection.js";
|
||||
import { waitForBrowserDevtoolsPort } from "../../dist/browser/BrowserProcessStartup.js";
|
||||
import { parseEvidence } from "../../dist/domain/evidence.js";
|
||||
import { webExecutionSchema } from "../../dist/domain/webExecution.js";
|
||||
import { webEventListenersSchema } from "../../dist/domain/webEventListeners.js";
|
||||
import { startBrowserRuntimeSite } from "../fixtures/browser-runtime-site.mjs";
|
||||
import { mcpTextValue } from "./mcp-verifier-results.mjs";
|
||||
import {
|
||||
startRuntimeFixtureBrowser,
|
||||
closeRuntimeFixtureResources,
|
||||
} from "./browser-runtime-fixture-lifecycle.mjs";
|
||||
import { verifyRuntimeTargetTermination } from "./browser-runtime-termination-e2e.mjs";
|
||||
import { verifyRuntimeInvalidSelectors } from "./browser-runtime-selector-e2e.mjs";
|
||||
import {
|
||||
createRuntimeIsolatedWorld,
|
||||
triggerRuntimeFixtureAction,
|
||||
isolatedFixtureUrl,
|
||||
} from "./browser-runtime-world-e2e.mjs";
|
||||
|
||||
/** Exercise native browser runtime attribution through public CLI and stdio MCP, including arming. */
|
||||
export async function verifyBrowserRuntime(
|
||||
executable,
|
||||
entrypoint = fileURLToPath(new URL("../rea.mjs", import.meta.url)),
|
||||
) {
|
||||
await access(executable);
|
||||
const site = await startBrowserRuntimeSite();
|
||||
let fixture;
|
||||
let primaryError;
|
||||
let stderr = "";
|
||||
let action;
|
||||
let client;
|
||||
try {
|
||||
fixture = await startRuntimeFixtureBrowser(executable, site.origin);
|
||||
const { browser, profile } = fixture;
|
||||
browser.stderr.on("data", (chunk) => {
|
||||
if (stderr.length < 65536) stderr += chunk;
|
||||
});
|
||||
const port = await waitForBrowserDevtoolsPort({
|
||||
child: browser,
|
||||
executable,
|
||||
activePortPath: join(profile, "DevToolsActivePort"),
|
||||
stderr: () => stderr,
|
||||
timeoutMs: 20_000,
|
||||
});
|
||||
const endpoint = `http://127.0.0.1:${port}`;
|
||||
const targets = await new CdpBrowserProvider().listTargets({
|
||||
cdp_endpoint: endpoint,
|
||||
allowed_origins: [site.origin],
|
||||
});
|
||||
if (!targets.ok) throw targets.error;
|
||||
const target = targets.value.targets[0];
|
||||
assert.ok(target, "owned fixture page target missing");
|
||||
const pages = await (await fetch(`${endpoint}/json/list`)).json();
|
||||
const page = pages.find((candidate) => candidate.id === target.target_id);
|
||||
action = await CdpConnection.connect(
|
||||
page.webSocketDebuggerUrl,
|
||||
"inspect_web_page",
|
||||
);
|
||||
await ready(action);
|
||||
let isolatedContext = await createRuntimeIsolatedWorld(action);
|
||||
const env = {
|
||||
...process.env,
|
||||
REA_LOG_LEVEL: "silent",
|
||||
HOPPER_LAUNCHER_PATH: "/rea-unconfigured-provider/hopper",
|
||||
};
|
||||
const input = { cdp_endpoint: endpoint, target_id: target.target_id };
|
||||
const cliListener = await promisify(execFile)(
|
||||
process.execPath,
|
||||
[
|
||||
entrypoint,
|
||||
"inspect-web-event-listeners",
|
||||
endpoint,
|
||||
target.target_id,
|
||||
"#run",
|
||||
"--json",
|
||||
],
|
||||
{ env, timeout: 40_000, maxBuffer: 64 * 1024 * 1024 },
|
||||
);
|
||||
const cliListenerProof = assertListeners(
|
||||
parseEvidence(JSON.parse(cliListener.stdout)),
|
||||
site,
|
||||
);
|
||||
const cliExecution = await executionCli(
|
||||
entrypoint,
|
||||
input,
|
||||
env,
|
||||
action,
|
||||
isolatedContext,
|
||||
);
|
||||
const cliProof = assertExecution(
|
||||
parseEvidence(JSON.parse(cliExecution)),
|
||||
site,
|
||||
);
|
||||
const transport = new StdioClientTransport({
|
||||
command: process.execPath,
|
||||
args: [entrypoint, "mcp"],
|
||||
env,
|
||||
stderr: "pipe",
|
||||
});
|
||||
client = new Client({ name: "web-runtime-real-e2e", version: "1" });
|
||||
await client.connect(transport);
|
||||
const invalidSelectors = await verifyRuntimeInvalidSelectors(
|
||||
client,
|
||||
entrypoint,
|
||||
input,
|
||||
env,
|
||||
);
|
||||
const listenerResponse = await client.callTool({
|
||||
name: "inspect_web_event_listeners",
|
||||
arguments: { ...input, selector: "#run" },
|
||||
});
|
||||
assert.notEqual(
|
||||
listenerResponse.isError,
|
||||
true,
|
||||
mcpTextValue(listenerResponse),
|
||||
);
|
||||
const mcpListenerProof = assertListeners(
|
||||
parseEvidence(JSON.parse(mcpTextValue(listenerResponse)).evidence),
|
||||
site,
|
||||
);
|
||||
const mcpProof = await executionMcp(
|
||||
client,
|
||||
input,
|
||||
action,
|
||||
site,
|
||||
isolatedContext,
|
||||
false,
|
||||
);
|
||||
assert.equal(cliProof.selected_sha256, mcpProof.selected_sha256);
|
||||
assert.equal(cliListenerProof.sha256, mcpListenerProof.sha256);
|
||||
await reloadFixture(action);
|
||||
isolatedContext = await createRuntimeIsolatedWorld(action);
|
||||
const freshMcpProof = await executionMcp(
|
||||
client,
|
||||
input,
|
||||
action,
|
||||
site,
|
||||
isolatedContext,
|
||||
);
|
||||
assert.equal(site.evidenceRequests(), 3);
|
||||
const termination = await verifyRuntimeTargetTermination(
|
||||
client,
|
||||
input,
|
||||
site.origin,
|
||||
);
|
||||
const stillOpen = await action.send("Runtime.evaluate", {
|
||||
expression: "Boolean(document.querySelector('#run'))",
|
||||
returnByValue: true,
|
||||
});
|
||||
assert.equal(
|
||||
stillOpen.result.value,
|
||||
true,
|
||||
"REA must preserve the externally owned page",
|
||||
);
|
||||
const location = await action.send("Runtime.evaluate", {
|
||||
expression: "location.href",
|
||||
returnByValue: true,
|
||||
});
|
||||
assert.equal(
|
||||
location.result.value,
|
||||
`${site.origin}/same-document?caller=value#fragment`,
|
||||
);
|
||||
return {
|
||||
product: target,
|
||||
cli: cliProof,
|
||||
mcp: mcpProof,
|
||||
listener: cliListenerProof,
|
||||
public_cases: 8,
|
||||
invalid_selectors: invalidSelectors,
|
||||
termination,
|
||||
fresh_mcp: freshMcpProof,
|
||||
page_remained_open: true,
|
||||
};
|
||||
} catch (cause) {
|
||||
primaryError = cause;
|
||||
throw cause;
|
||||
} finally {
|
||||
await closeRuntimeFixtureResources(
|
||||
[
|
||||
() => client?.close(),
|
||||
() => action?.close(),
|
||||
() => fixture?.close(),
|
||||
() => site.close(),
|
||||
],
|
||||
primaryError,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function executionMcp(
|
||||
client,
|
||||
input,
|
||||
action,
|
||||
site,
|
||||
isolatedContext,
|
||||
requireBlock = true,
|
||||
) {
|
||||
let actionPromise;
|
||||
const response = await client.callTool(
|
||||
{
|
||||
name: "observe_web_execution",
|
||||
arguments: { ...input, observation_ms: 300 },
|
||||
},
|
||||
{
|
||||
timeout: 40_000,
|
||||
onprogress: (notification) => {
|
||||
if (
|
||||
actionPromise === undefined &&
|
||||
notification.message?.includes(
|
||||
"browser_execution: Browser execution observation armed",
|
||||
)
|
||||
) {
|
||||
actionPromise = triggerRuntimeFixtureAction(action, isolatedContext);
|
||||
void actionPromise.catch(() => undefined);
|
||||
}
|
||||
},
|
||||
},
|
||||
);
|
||||
assert.ok(actionPromise, "MCP did not emit an actual armed notification");
|
||||
await actionPromise;
|
||||
assert.notEqual(response.isError, true, mcpTextValue(response));
|
||||
return assertExecution(
|
||||
parseEvidence(JSON.parse(mcpTextValue(response)).evidence),
|
||||
site,
|
||||
requireBlock,
|
||||
);
|
||||
}
|
||||
|
||||
async function ready(connection) {
|
||||
for (let attempt = 0; attempt < 100; attempt += 1) {
|
||||
const result = await connection.send("Runtime.evaluate", {
|
||||
expression:
|
||||
"typeof chosen === 'function' && typeof untouched === 'function'",
|
||||
returnByValue: true,
|
||||
});
|
||||
if (result.result.value === true) return;
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
}
|
||||
throw new Error("Runtime fixture sources did not load");
|
||||
}
|
||||
|
||||
async function executionCli(entrypoint, input, env, action, isolatedContext) {
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
entrypoint,
|
||||
"observe-web-execution",
|
||||
input.cdp_endpoint,
|
||||
input.target_id,
|
||||
"--observation-ms",
|
||||
"300",
|
||||
"--json",
|
||||
],
|
||||
{ env, stdio: ["ignore", "pipe", "pipe"] },
|
||||
);
|
||||
let stdout = "";
|
||||
let stderr = "";
|
||||
let actionPromise;
|
||||
const timeout = setTimeout(() => child.kill("SIGTERM"), 40_000);
|
||||
child.stdout.on("data", (chunk) => {
|
||||
stdout += chunk;
|
||||
});
|
||||
child.stderr.on("data", (chunk) => {
|
||||
stderr += chunk;
|
||||
if (actionPromise === undefined && stderr.includes('"completed":1')) {
|
||||
actionPromise = triggerRuntimeFixtureAction(action, isolatedContext);
|
||||
void actionPromise.catch(() => undefined);
|
||||
}
|
||||
});
|
||||
try {
|
||||
const [code] = await once(child, "close");
|
||||
assert.equal(code, 0, stderr);
|
||||
assert.ok(actionPromise, "CLI did not report an actual armed point");
|
||||
await actionPromise;
|
||||
return stdout;
|
||||
} finally {
|
||||
clearTimeout(timeout);
|
||||
if (child.exitCode === null && child.signalCode === null)
|
||||
child.kill("SIGTERM");
|
||||
}
|
||||
}
|
||||
|
||||
function sourceByText(result, text) {
|
||||
const source = result.sources.find(
|
||||
(item) => item.source.state === "captured" && item.source.text === text,
|
||||
);
|
||||
assert.ok(source, "Exact independently known source text must be retained");
|
||||
assert.equal(
|
||||
source.source.sha256,
|
||||
createHash("sha256").update(text).digest("hex"),
|
||||
);
|
||||
assert.equal(source.source.utf16_units, text.length);
|
||||
assert.equal(source.source.utf8_bytes, Buffer.byteLength(text));
|
||||
return source;
|
||||
}
|
||||
function assertListeners(evidence, site) {
|
||||
const result = webEventListenersSchema.parse(evidence.normalized_result);
|
||||
assert.equal(
|
||||
result.listeners.length,
|
||||
1,
|
||||
"The selected node's child listener must not be attributed to the selected node",
|
||||
);
|
||||
const listener = result.listeners.find((item) => item.type === "click");
|
||||
assert.ok(listener);
|
||||
assert.equal(listener.passive, true);
|
||||
const source = sourceByText(result, site.sources.selected);
|
||||
assert.equal(listener.location.script_id, source.script_id);
|
||||
assert.equal(listener.location.source_association, "script_id");
|
||||
assert.equal(listener.execution, "unknown");
|
||||
return {
|
||||
sha256: source.source.sha256,
|
||||
line: listener.location.line_number,
|
||||
column: listener.location.column_number,
|
||||
};
|
||||
}
|
||||
function assertExecution(evidence, site, requireBlock = true) {
|
||||
const result = webExecutionSchema.parse(evidence.normalized_result);
|
||||
assert.ok(
|
||||
result.coverage.scripts.every(
|
||||
(item) => item.reported_url !== isolatedFixtureUrl,
|
||||
),
|
||||
);
|
||||
assert.ok(
|
||||
result.sources.every(
|
||||
(item) =>
|
||||
item.url !== isolatedFixtureUrl || item.source.state !== "captured",
|
||||
),
|
||||
);
|
||||
const selected = sourceByText(result, site.sources.selected);
|
||||
const other = sourceByText(result, site.sources.other);
|
||||
assert.equal(selected.url, other.url);
|
||||
assert.equal(selected.has_source_url, true);
|
||||
assert.equal(
|
||||
selected.url,
|
||||
`${site.origin.replace("http://", "http://declared:label@")}/same.js`,
|
||||
);
|
||||
assert.notEqual(selected.script_id, other.script_id);
|
||||
assert.ok(
|
||||
selected.source.utf8_bytes > selected.source.utf16_units,
|
||||
"Unicode oracle must distinguish UTF-8 bytes from UTF-16 units",
|
||||
);
|
||||
const coverage = result.coverage.scripts.find(
|
||||
(script) => script.script_id === selected.script_id,
|
||||
);
|
||||
const fn = coverage?.functions.find((item) => item.name === "chosen");
|
||||
assert.ok(
|
||||
fn,
|
||||
"Externally invoked callback must have precise function evidence",
|
||||
);
|
||||
assert.ok(fn.ranges.some((range) => range.count === 1));
|
||||
if (requireBlock)
|
||||
assert.equal(
|
||||
fn.is_block_coverage,
|
||||
true,
|
||||
"Fresh fixture function must supply block granularity",
|
||||
);
|
||||
if (fn.is_block_coverage)
|
||||
assert.ok(
|
||||
fn.ranges.some((range) => range.count === 0),
|
||||
`Unexecuted branch must remain zero within block coverage: ${JSON.stringify(fn)}`,
|
||||
);
|
||||
assert.equal(
|
||||
fn.ranges[0].start_offset,
|
||||
site.sources.selected.indexOf("function chosen"),
|
||||
"Coverage offsets must index the retained UTF-16 source, including the astral prefix",
|
||||
);
|
||||
for (const range of fn.ranges) {
|
||||
assert.equal(range.source_bounds, "verified");
|
||||
assert.ok(range.end_offset <= selected.source.utf16_units);
|
||||
}
|
||||
const request = result.requests.find((item) =>
|
||||
item.url.endsWith("/evidence?marker=chosen"),
|
||||
);
|
||||
assert.ok(request, "The selected request initiator must be retained");
|
||||
assert.ok(
|
||||
request.callsites.some(
|
||||
(site) =>
|
||||
site.script_id === selected.script_id &&
|
||||
site.source_association === "script_id",
|
||||
),
|
||||
);
|
||||
assert.equal(request.causal_attribution, "unknown");
|
||||
assert.ok(
|
||||
request.callsites.some(
|
||||
(site) =>
|
||||
site.script_id === selected.script_id && site.url === selected.url,
|
||||
),
|
||||
);
|
||||
assert.equal(result.script_inventory.coverage_absence, "unknown");
|
||||
assert.equal(result.instrumentation.cleanup, "confirmed");
|
||||
return {
|
||||
selected_sha256: selected.source.sha256,
|
||||
other_sha256: other.source.sha256,
|
||||
zero_branch: fn.is_block_coverage
|
||||
? "observed-zero"
|
||||
: "unknown-function-only",
|
||||
granularity: fn.is_block_coverage ? "block" : "function",
|
||||
repeated_url_identity: true,
|
||||
same_document_navigation: true,
|
||||
declared_source_url_preserved: true,
|
||||
request_script_id: selected.script_id,
|
||||
isolated_execution_proved_and_excluded: true,
|
||||
};
|
||||
}
|
||||
|
||||
async function reloadFixture(connection) {
|
||||
await connection.send("Page.enable");
|
||||
let dispose;
|
||||
const committed = new Promise((resolve, reject) => {
|
||||
const timer = setTimeout(
|
||||
() => reject(new Error("Fixture reload did not commit")),
|
||||
5000,
|
||||
);
|
||||
const remove = connection.onEvent((event) => {
|
||||
if (
|
||||
event.method === "Page.frameNavigated" &&
|
||||
!event.params.frame?.parentId
|
||||
)
|
||||
resolve();
|
||||
});
|
||||
dispose = () => {
|
||||
clearTimeout(timer);
|
||||
remove();
|
||||
};
|
||||
});
|
||||
void committed.catch(() => undefined);
|
||||
try {
|
||||
await connection.send("Page.reload", { ignoreCache: true });
|
||||
await committed;
|
||||
await ready(connection);
|
||||
} finally {
|
||||
dispose();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { once } from "node:events";
|
||||
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { spawnOwnedProviderProcess } from "../../dist/process/ProviderProcess.js";
|
||||
import { createSystemProcessOwnershipHost } from "../../dist/process/ProcessOwnershipObservation.js";
|
||||
import { liveProcesses } from "../../dist/process/ProcessOwnershipProcessTree.js";
|
||||
import { CdpConnection } from "../../dist/browser/CdpConnection.js";
|
||||
|
||||
/** Launch only the synthetic fixture browser in its own verified process group. */
|
||||
export async function startRuntimeFixtureBrowser(executable, origin) {
|
||||
const profile = await mkdtemp(join(tmpdir(), "rea-real-web-runtime-"));
|
||||
let owned;
|
||||
try {
|
||||
owned = await spawnOwnedProviderProcess({
|
||||
command: executable,
|
||||
arguments: [
|
||||
"--headless=new",
|
||||
...(process.env.REA_BROWSER_NO_SANDBOX === "true"
|
||||
? ["--no-sandbox"]
|
||||
: []),
|
||||
"--remote-debugging-address=127.0.0.1",
|
||||
"--remote-debugging-port=0",
|
||||
`--user-data-dir=${profile}`,
|
||||
"--no-first-run",
|
||||
"--no-default-browser-check",
|
||||
"--disable-background-networking",
|
||||
"--disable-component-update",
|
||||
"--disable-dev-shm-usage",
|
||||
"--disable-sync",
|
||||
origin,
|
||||
],
|
||||
runId: randomUUID(),
|
||||
// Installed Chrome launch scripts exec another binary; do not require an argv prefix.
|
||||
expectedCommand: null,
|
||||
});
|
||||
owned.process.stdout?.resume();
|
||||
return {
|
||||
browser: owned.process,
|
||||
profile,
|
||||
close: async () => {
|
||||
await stopOwnedBrowser(owned, profile);
|
||||
await rm(profile, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: 5,
|
||||
retryDelay: 100,
|
||||
});
|
||||
},
|
||||
};
|
||||
} catch (cause) {
|
||||
if (owned !== undefined) await stopOwnedBrowser(owned, profile);
|
||||
await rm(profile, {
|
||||
recursive: true,
|
||||
force: true,
|
||||
maxRetries: 5,
|
||||
retryDelay: 100,
|
||||
});
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
|
||||
async function stopOwnedBrowser(owned, profile) {
|
||||
const child = owned.process;
|
||||
const exited =
|
||||
child.exitCode === null && child.signalCode === null
|
||||
? once(child, "exit")
|
||||
: Promise.resolve();
|
||||
void exited.catch(() => undefined);
|
||||
let connection;
|
||||
let timer;
|
||||
const escalation = setTimeout(() => {
|
||||
if (child.exitCode === null && child.signalCode === null)
|
||||
child.kill("SIGKILL");
|
||||
}, 2_000);
|
||||
try {
|
||||
if (child.exitCode === null && child.signalCode === null) {
|
||||
try {
|
||||
const [port, browserPath] = (
|
||||
await readFile(join(profile, "DevToolsActivePort"), "utf8")
|
||||
)
|
||||
.trim()
|
||||
.split("\n");
|
||||
assert.match(port, /^\d+$/);
|
||||
assert.match(browserPath, /^\/devtools\/browser\/[^\s]+$/);
|
||||
const signal = AbortSignal.timeout(2_000);
|
||||
connection = await CdpConnection.connect(
|
||||
`ws://127.0.0.1:${port}${browserPath}`,
|
||||
"inspect_web_page",
|
||||
signal,
|
||||
);
|
||||
// This browser belongs to the verifier; public REA operations never close it.
|
||||
await connection.send("Browser.close", {}, undefined, signal);
|
||||
} catch {
|
||||
if (child.exitCode === null && child.signalCode === null)
|
||||
child.kill("SIGTERM");
|
||||
}
|
||||
}
|
||||
if (owned.cleanup !== undefined) {
|
||||
const result = await owned.cleanup();
|
||||
assert.equal(result.cleaned, true, JSON.stringify(result));
|
||||
}
|
||||
await Promise.race([
|
||||
exited,
|
||||
new Promise((_, reject) => {
|
||||
timer = setTimeout(
|
||||
() =>
|
||||
reject(
|
||||
new Error("Owned fixture browser did not exit after shutdown"),
|
||||
),
|
||||
5_000,
|
||||
);
|
||||
}),
|
||||
]);
|
||||
} finally {
|
||||
clearTimeout(escalation);
|
||||
clearTimeout(timer);
|
||||
await connection?.close();
|
||||
}
|
||||
if (process.platform === "win32") return;
|
||||
const host = createSystemProcessOwnershipHost();
|
||||
const deadline = Date.now() + 5_000;
|
||||
while (true) {
|
||||
const members = liveProcesses(await host.listProcesses()).filter(
|
||||
(member) => member.processGroupId === owned.ownership.processGroupId,
|
||||
);
|
||||
if (members.length === 0) return;
|
||||
if (Date.now() >= deadline)
|
||||
throw new Error(
|
||||
`Owned fixture group remains: ${JSON.stringify(members.map(({ pid }) => pid))}`,
|
||||
);
|
||||
await new Promise((resolve) => setTimeout(resolve, 25));
|
||||
}
|
||||
}
|
||||
|
||||
/** Attempt every owned cleanup and retain the original verification failure if cleanup also fails. */
|
||||
export async function closeRuntimeFixtureResources(tasks, primaryError) {
|
||||
const failures = [];
|
||||
for (const task of tasks) {
|
||||
try {
|
||||
await task();
|
||||
} catch (cause) {
|
||||
failures.push(cause);
|
||||
}
|
||||
}
|
||||
if (failures.length > 0)
|
||||
throw new AggregateError(
|
||||
primaryError === undefined ? failures : [primaryError, ...failures],
|
||||
"Runtime fixture cleanup failed",
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { mcpTextValue } from "./mcp-verifier-results.mjs";
|
||||
|
||||
/** Verify native malformed-selector classification through the public CLI and MCP. */
|
||||
export async function verifyRuntimeInvalidSelectors(
|
||||
client,
|
||||
entrypoint,
|
||||
input,
|
||||
env,
|
||||
) {
|
||||
const cli = await new Promise((resolve) => {
|
||||
execFile(
|
||||
process.execPath,
|
||||
[
|
||||
entrypoint,
|
||||
"inspect-web-event-listeners",
|
||||
input.cdp_endpoint,
|
||||
input.target_id,
|
||||
"[",
|
||||
"--json",
|
||||
],
|
||||
{ env, timeout: 40_000, maxBuffer: 64 * 1024 * 1024 },
|
||||
(error, stdout, stderr) => resolve({ error, stdout, stderr }),
|
||||
);
|
||||
});
|
||||
assert.ok(cli.error, "Invalid native CSS must fail in the CLI");
|
||||
assert.equal(typeof cli.error.code, "number", cli.stderr);
|
||||
assertInputFailure(JSON.parse(cli.stdout), input.target_id);
|
||||
const response = await client.callTool({
|
||||
name: "inspect_web_event_listeners",
|
||||
arguments: { ...input, selector: "[" },
|
||||
});
|
||||
assert.equal(response.isError, true, mcpTextValue(response));
|
||||
assertInputFailure(JSON.parse(mcpTextValue(response)).error, input.target_id);
|
||||
return { cli: "invalid_input", mcp: "invalid_input", selector: "[" };
|
||||
}
|
||||
|
||||
function assertInputFailure(failure, targetId) {
|
||||
assert.equal(failure.code, "invalid_request");
|
||||
assert.equal(failure.category, "invalid_input");
|
||||
assert.equal(failure.details.operation, "inspect_web_event_listeners");
|
||||
assert.ok(
|
||||
failure.details.issues.some(
|
||||
(issue) =>
|
||||
issue.path[0] === "selector" && issue.message.includes(targetId),
|
||||
),
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { CdpConnection } from "../../dist/browser/CdpConnection.js";
|
||||
import { mcpTextValue } from "./mcp-verifier-results.mjs";
|
||||
import { closeRuntimeFixtureResources } from "./browser-runtime-fixture-lifecycle.mjs";
|
||||
|
||||
/** Close a separately owned fixture page after actual MCP arming and inspect native cleanup diagnostics. */
|
||||
export async function verifyRuntimeTargetTermination(client, input, origin) {
|
||||
const version = await (
|
||||
await fetch(`${input.cdp_endpoint}/json/version`)
|
||||
).json();
|
||||
const root = await CdpConnection.connect(
|
||||
version.webSocketDebuggerUrl,
|
||||
"observe_web_execution",
|
||||
);
|
||||
let targetId;
|
||||
let closed = false;
|
||||
let primaryError;
|
||||
try {
|
||||
({ targetId } = await root.send("Target.createTarget", { url: origin }));
|
||||
assert.equal(typeof targetId, "string");
|
||||
await waitForTarget(input.cdp_endpoint, targetId, origin);
|
||||
let closePromise;
|
||||
const response = await client.callTool(
|
||||
{
|
||||
name: "observe_web_execution",
|
||||
arguments: {
|
||||
...input,
|
||||
target_id: targetId,
|
||||
observation_ms: 2_147_483_647,
|
||||
},
|
||||
},
|
||||
{
|
||||
timeout: 20_000,
|
||||
onprogress: (notification) => {
|
||||
if (
|
||||
closePromise === undefined &&
|
||||
notification.message?.includes(
|
||||
"browser_execution: Browser execution observation armed",
|
||||
)
|
||||
) {
|
||||
closePromise = root
|
||||
.send("Target.closeTarget", { targetId })
|
||||
.then((result) => {
|
||||
assert.equal(result.success, true);
|
||||
closed = true;
|
||||
});
|
||||
void closePromise.catch(() => undefined);
|
||||
}
|
||||
},
|
||||
},
|
||||
);
|
||||
assert.ok(
|
||||
closePromise,
|
||||
"The separate page must be closed after actual arming",
|
||||
);
|
||||
await closePromise;
|
||||
assert.equal(
|
||||
response.isError,
|
||||
true,
|
||||
"Detached instrumentation cannot claim confirmed cleanup",
|
||||
);
|
||||
const failure = mcpTextValue(response);
|
||||
assert.match(failure, /cleanup.*confirmed/u);
|
||||
assert.ok(
|
||||
failure.includes(targetId),
|
||||
"Failure must retain the selected target identity",
|
||||
);
|
||||
assert.match(failure, /Profiler\.stopPreciseCoverage/u);
|
||||
return {
|
||||
target_id: targetId,
|
||||
page_closed_after_arming: true,
|
||||
failure_inline: true,
|
||||
cleanup: "unconfirmed_reported",
|
||||
};
|
||||
} catch (cause) {
|
||||
primaryError = cause;
|
||||
throw cause;
|
||||
} finally {
|
||||
await closeRuntimeFixtureResources(
|
||||
[
|
||||
() =>
|
||||
targetId === undefined || closed
|
||||
? undefined
|
||||
: root.send("Target.closeTarget", { targetId }),
|
||||
() => root.close(),
|
||||
],
|
||||
primaryError,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function waitForTarget(endpoint, targetId, origin) {
|
||||
for (let attempt = 0; attempt < 100; attempt += 1) {
|
||||
const pages = await (await fetch(`${endpoint}/json/list`)).json();
|
||||
if (
|
||||
pages.some((page) => page.id === targetId && page.url.startsWith(origin))
|
||||
)
|
||||
return;
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
}
|
||||
throw new Error(
|
||||
"Separate fixture target did not settle on its selected origin",
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import assert from "node:assert/strict";
|
||||
|
||||
export const isolatedFixtureUrl = "rea-isolated-fixture.js";
|
||||
|
||||
/** Create an explicitly owned native isolated-world fixture outside REA's public operations. */
|
||||
export async function createRuntimeIsolatedWorld(connection) {
|
||||
const tree = await connection.send("Page.getFrameTree");
|
||||
const world = await connection.send("Page.createIsolatedWorld", {
|
||||
frameId: tree.frameTree.frame.id,
|
||||
worldName: "rea-verifier-isolated",
|
||||
});
|
||||
const contextId = world.executionContextId;
|
||||
assert.ok(Number.isInteger(contextId));
|
||||
const initialized = await connection.send("Runtime.evaluate", {
|
||||
contextId,
|
||||
expression: `function reaIsolatedFixture() { return 'isolated-proof'; }
|
||||
//# sourceURL=${isolatedFixtureUrl}`,
|
||||
});
|
||||
assert.equal(initialized.exceptionDetails, undefined);
|
||||
return contextId;
|
||||
}
|
||||
|
||||
/** Invoke a known isolated callback and the selected site's callback after actual REA arming. */
|
||||
export async function triggerRuntimeFixtureAction(connection, contextId) {
|
||||
const proof = await connection.send("Runtime.evaluate", {
|
||||
contextId,
|
||||
expression: "reaIsolatedFixture()",
|
||||
returnByValue: true,
|
||||
});
|
||||
assert.equal(proof.result.value, "isolated-proof");
|
||||
return connection.send("Runtime.evaluate", {
|
||||
expression: "document.querySelector('#run').click()",
|
||||
});
|
||||
}
|
||||
@@ -46,6 +46,7 @@ export const toolFamilyCatalog = (sources) => {
|
||||
surface: "browser-provider",
|
||||
contracts: [
|
||||
...sources.browserContracts.BROWSER_TOOL_CONTRACTS,
|
||||
...sources.webRuntimeContracts.WEB_RUNTIME_TOOL_CONTRACTS,
|
||||
...sources.browserScenarioContracts.BROWSER_SCENARIO_TOOL_CONTRACTS,
|
||||
],
|
||||
},
|
||||
@@ -171,7 +172,10 @@ export const providerCatalog = (sources) => {
|
||||
},
|
||||
{
|
||||
identity: sources.browserProvider.CDP_BROWSER_PROVIDER_IDENTITY,
|
||||
contracts: sources.browserContracts.BROWSER_TOOL_CONTRACTS,
|
||||
contracts: [
|
||||
...sources.browserContracts.BROWSER_TOOL_CONTRACTS,
|
||||
...sources.webRuntimeContracts.WEB_RUNTIME_TOOL_CONTRACTS,
|
||||
],
|
||||
},
|
||||
{
|
||||
identity:
|
||||
|
||||
@@ -50,6 +50,7 @@ const SOURCE_PATHS = {
|
||||
managedWorkflowContracts:
|
||||
"dist/contracts/managed/managedWorkflowToolContracts.js",
|
||||
browserContracts: "dist/contracts/browserToolContracts.js",
|
||||
webRuntimeContracts: "dist/contracts/webRuntimeToolContracts.js",
|
||||
browserScenarioContracts: "dist/contracts/browserScenarioToolContracts.js",
|
||||
electronContracts: "dist/contracts/javascript/electronToolContracts.js",
|
||||
javascriptRuntimeObservationContracts:
|
||||
|
||||
@@ -89,12 +89,14 @@ const failedBoundary = (file, target) => {
|
||||
)
|
||||
return "pure-layer";
|
||||
}
|
||||
if (file === "src/application/runtime.ts") return undefined;
|
||||
if (layer === "application" && targetLayer === "composition")
|
||||
return "application-composition";
|
||||
if (
|
||||
(layer === "application" || layer === "server") &&
|
||||
(MIGRATED_PROVIDER_ROOTS.has(targetLayer) ||
|
||||
/^src\/(?:artifacts\/ArtifactProvider|dotnet\/ManagedStaticProvider|native\/NativeMacOSProvider)\.[^/]+$/u.test(
|
||||
target,
|
||||
) ||
|
||||
/^src\/browser\/[^/]*Provider\.[^/]+$/u.test(target))
|
||||
)
|
||||
return "provider-construction";
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env node
|
||||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import { verifyBrowserRuntime } from "./lib/browser-runtime-e2e.mjs";
|
||||
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
|
||||
|
||||
const executable = process.env.REA_BROWSER_EXECUTABLE;
|
||||
if (executable === undefined)
|
||||
throw new Error(
|
||||
"verify:browser:runtime requires caller-supplied REA_BROWSER_EXECUTABLE",
|
||||
);
|
||||
if (process.platform !== "win32") {
|
||||
try {
|
||||
await promisify(execFile)("ps", ["-p", String(process.pid), "-o", "pid="], {
|
||||
timeout: 5_000,
|
||||
});
|
||||
} catch (cause) {
|
||||
throw new Error(
|
||||
"verify:browser:runtime requires the host ps command for owned fixture cleanup",
|
||||
{ cause },
|
||||
);
|
||||
}
|
||||
}
|
||||
const run = createVerifierRun();
|
||||
const result = await verifyBrowserRuntime(executable, process.argv[2]);
|
||||
console.log(
|
||||
JSON.stringify(
|
||||
{ status: "passed", ...result, verifier: await completeVerifierRun(run) },
|
||||
null,
|
||||
2,
|
||||
),
|
||||
);
|
||||
Reference in New Issue
Block a user