Files
rea/scripts/lib/browser-runtime-termination-e2e.mjs
Ysopwnableandmorluto b5afa5f91e feat(browser): attribute native execution and listener sources (#917)
* feat(browser): attribute native execution and listener sources

Integrate finite native precise-coverage windows and direct DOM listener inspection through shared CLI/MCP workflows, exact session script identities, inline source evidence and owned instrumentation cleanup.

Refs #881, #774

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): preserve runtime identity and settle fixture cleanup

Retain complete reported initiator objects, reject changing loader availability, propagate the selected operation through main-frame authorization and complete effect/help inventories. Close only the synthetic owned Chrome via its native endpoint, await process-group settlement and preserve primary verification errors during cleanup.

Refs #881

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): settle invalid events and preserve document evidence

Accept same-document URL changes with proven stable loader identity, retain metadata without inventing ownership, terminate malformed event collection immediately, and sanitize known transport URLs without erasing sourceURL declarations.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): end detached target windows and sanitize coverage identities

Reuse the existing flat-session event matcher, retain prearming termination, and apply the source identity policy to coverage URLs. Add real MCP target-close verification and producer regressions for detachment orderings.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): retain final sample metadata and settle fatal protocol failures

Keep source inventory open through the resetting sample, propagate fatal wire failures without outstanding commands, and check listener collection failures after final document validation.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)

Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): require default execution worlds for website attribution

Preserve native context and world metadata without treating same-frame isolated or unknown contexts as website sources. Verify actual Chrome isolated function execution before excluding its coverage, and cover ambiguous world metadata with producer regressions.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)

Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): revalidate runtime source joins and selector failures

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

---------

Co-authored-by: morluto <[email protected]>
2026-10-07 21:45:07 +08:00

105 lines
3.1 KiB
JavaScript

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",
);
}