Files
rea/scripts/lib/browser-script-export-e2e.mjs
T
67a1e2c937 feat(browser)!: size inspect_web_page delivery and page captures by section (#14368)
* feat(browser)!: size inspect_web_page delivery and page captures by section

inspect_web_page returned the complete capture inline with no detail
choice. A local page with 2,000 list rows, 200 buttons and three bundled
libraries produced a 4.6 MB MCP result, mostly 6,423 DOM nodes and 10,614
accessibility nodes.

inspect_web_page now declares the size-aware detail. Omitted, a capture
up to 32 KiB is inline and a larger one is retained behind a summary of
its small sections and counts. A new web-page view pages dom (node_names),
accessibility (roles), console (types), network (methods), scripts and
resources. The shared Evidence handler applies the same size decision as
the other size-aware tools and keeps detail out of the capture parameters.

BREAKING CHANGE: an MCP inspect_web_page call that omits detail on a
capture above 32 KiB now returns the retained summary. Pass
detail: "complete" for the previous shape, for example before
compare_web_captures.

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* feat(cli): deliver inspect-web-page summaries through the Evidence bundle

The contract's size-aware detail also generated a --detail option for
rea inspect-web-page, but the command ignored it and always printed the
complete capture. The command now uses the shared CLI Evidence delivery,
as analyze-web-bundle does: omitted or complete detail prints the
capture, and --detail summary requires --evidence_output and prints the
summary with the bundle receipt.

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* fix(browser): bound web-page summaries and keep capture context on pages

Review found three gaps in the new web-page view:
- pages dropped the capture's completeness and the section's context, so
  an empty accessibility page looked like an exhausted one;
- script rows carried their complete captured source, so one large
  bundle could not be paged;
- the summary copied every frame and worker.

Every page now carries the capture's completeness and its section's own
context beside the page coverage. Script rows keep source metadata
without text, and a script-source collection pages one script's text by
UTF-16 offset. The summary counts frames and workers, which page through
their own collections.

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

* test(browser): request complete captures in the real-browser verifiers

The metadata-budget, URL userinfo, DOM destination and script export
verifiers read the complete inspect_web_page capture through MCP. With
size-aware delivery, an omitted detail above 32 KiB returns the summary,
and the metadata-budget verifier failed reading scripts.items from it.
These calls now request detail complete; their CLI calls already return
the complete capture.

Co-Authored-By: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>

---------

Co-authored-by: gensokyo-dev <gensokyo-dev@users.noreply.github.com>
Co-authored-by: gensokyo-dev <342580529+gensokyo-dev@users.noreply.github.com>
2026-11-24 10:08:21 -05:00

202 lines
6.0 KiB
JavaScript

import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { mkdtemp, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
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 { parseEvidence } from "../../dist/domain/evidence.js";
import { startBrowserScriptSite } from "../fixtures/browser-script-site.mjs";
import {
assertWebScriptExport,
assertExportedScriptAnalysis,
} from "./browser-script-export-assertions.mjs";
import { mcpTextValue } from "./mcp-verifier-results.mjs";
import { withVerifierCleanup } from "./verifier-cleanup.mjs";
/** Verify capture → export → existing analysis through real CLI and stdio MCP. */
export async function verifyBrowserScriptExport(
executable,
entrypoint = fileURLToPath(new URL("../rea.mjs", import.meta.url)),
attachment,
) {
let site;
let root;
let transport;
let client;
return withVerifierCleanup(async () => {
site = await startBrowserScriptSite();
root = await mkdtemp(join(tmpdir(), "rea-browser-script-export-"));
const env = {
...process.env,
REA_LOG_LEVEL: "silent",
HOPPER_LAUNCHER_PATH: "/rea-unconfigured-provider/hopper",
};
const cli = exportCli(entrypoint, env);
const scenario = scriptCaptureScenario(executable, site.origin);
const save = (name, capture) => saveCapture(root, name, capture);
transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
env,
stderr: "pipe",
});
client = new Client({ name: "web-script-real-e2e", version: "1" });
const cliCapture = await cli(
"capture-browser-scenario",
JSON.stringify(scenario),
);
const cliInput = await save("cli", cliCapture);
const counts = new Map(site.counts);
const cliExport = await cli(
"export-web-scripts",
cliInput.capture_path,
cliInput.output_directory,
);
const cliResult = await assertWebScriptExport(
cliExport,
cliCapture,
site.assets,
);
await cli(
"analyze-javascript-application",
cliResult.analysis_input.input_path,
).then((value) => assertExportedScriptAnalysis(value, 4, true));
assert.deepEqual(
site.counts,
counts,
"Export/analysis refetched a captured asset",
);
await client.connect(transport);
const call = async (name, args) => {
const result = await client.callTool({ name, arguments: args });
assert.notEqual(result.isError, true, mcpTextValue(result));
return parseEvidence(JSON.parse(mcpTextValue(result)));
};
const mcpCapture = await call("capture_browser_scenario", scenario);
const mcpInput = await save("mcp", mcpCapture);
const mcpCounts = new Map(site.counts);
const mcpResult = await assertWebScriptExport(
await call("export_web_scripts", mcpInput),
mcpCapture,
site.assets,
);
assertExportedScriptAnalysis(
await call("analyze_javascript_application", mcpResult.analysis_input),
4,
true,
);
assert.deepEqual(
site.counts,
mcpCounts,
"MCP export/analysis refetched a captured asset",
);
const passive = attachment
? await verifyPassiveExport(attachment, { cli, call, save })
: false;
return {
cli: true,
stdio_mcp: true,
active_scripts: 4,
passive,
exact_bytes: true,
manifest_readback: true,
relative_import_resolved: true,
query_variants_isolated: true,
implicit_refetches: 0,
};
}, [
() => client?.close(),
() => transport?.close(),
() => site?.close(),
() =>
root === undefined
? undefined
: rm(root, { recursive: true, force: true }),
]);
}
const verifyPassiveExport = async (attachment, { cli, call, save }) => {
const input = {
...attachment,
observation_ms: 100,
include_script_sources: true,
};
// Export reads the complete capture with its script sources.
const page = await call("inspect_web_page", { ...input, detail: "complete" });
const pageInput = await save("passive", page);
const exported = await assertWebScriptExport(
await call("export_web_scripts", pageInput),
page,
);
assertExportedScriptAnalysis(
await call("analyze_javascript_application", exported.analysis_input),
exported.scripts.length,
false,
);
const pageCli = await cli(
"inspect-web-page",
attachment.cdp_endpoint,
attachment.target_id,
"--include-script-sources",
"--observation-ms",
"100",
);
const pageCliInput = await save("passive-cli", pageCli);
const pageCliResult = await assertWebScriptExport(
await cli(
"export-web-scripts",
pageCliInput.capture_path,
pageCliInput.output_directory,
),
pageCli,
);
assertExportedScriptAnalysis(
await cli(
"analyze-javascript-application",
pageCliResult.analysis_input.input_path,
),
pageCliResult.scripts.length,
false,
);
return true;
};
const exportCli =
(entrypoint, env) =>
async (...args) => {
const { stdout } = await promisify(execFile)(
process.execPath,
[entrypoint, ...args, "--json"],
{ env, timeout: 60000, maxBuffer: 32 * 1024 * 1024 },
);
return parseEvidence(JSON.parse(stdout));
};
const saveCapture = async (root, name, capture) => {
const capturePath = join(root, `${name}.json`);
await writeFile(capturePath, JSON.stringify(capture));
return {
capture_path: capturePath,
output_directory: join(root, `${name}-export`),
};
};
const scriptCaptureScenario = (executable, origin) => ({
browser: { mode: "launch", executable_path: executable },
start_url: { url: origin },
actions: [
{
step_id: "ready",
action: "wait_for",
locator: { kind: "css", selector: '[data-ready="true"]' },
state: "visible",
timeout_ms: 10000,
},
],
capture: { network: { response_body: true } },
});