perf(browser): normalize precise coverage once by script identity

Pass validated coverage into normalization and index retained sources by script ID instead of scanning them for every script. Preserve source revalidation, UTF-16 bounds, nested counts, and unavailable-source uncertainty.

Validation: 285 tests across 34 focused suites, check:fast, and architecture guards passed. Flameox-guided 8,000-script normalization benchmark improved from 101.6 ms to 5.5 ms median with identical full-result digests; this is normalization CPU, not end-to-end browser timing.
This commit is contained in:
morluto
2026-10-11 02:48:59 +08:00
parent dce81ca400
commit 3d79903fb1
4 changed files with 166 additions and 16 deletions
@@ -5,6 +5,59 @@ import { observeWebExecutionInputSchema } from "../../../src/domain/webExecution
import { inspectWebEventListenersInputSchema } from "../../../src/domain/webEventListeners.js";
import { startRuntimeBrowser } from "../../fixtures/webRuntime.js";
it("invalidates retained sources when their execution context is reused during another source read", async () => {
let emit: ((event: CdpEvent) => void) | undefined;
const browser = await startRuntimeBrowser({
commandResult: (command, origin) => {
if (
command.method === "Debugger.getScriptSource" &&
command.params.scriptId === "script-b"
)
emit?.({
sessionId: "session-1",
method: "Runtime.executionContextCreated",
params: {
context: {
id: 1,
uniqueId: "replacement-context",
origin,
name: "",
auxData: {
frameId: "runtime-main",
isDefault: true,
type: "default",
},
},
},
});
return undefined;
},
});
emit = browser.emitEvent;
onTestFinished(() => browser.close());
const result = await new CdpWebRuntimeProvider().observeExecution(
observeWebExecutionInputSchema.parse({
cdp_endpoint: browser.endpoint,
target_id: "allowed-page",
observation_ms: 5,
}),
);
if (!result.ok) throw result.error;
expect(result.value.coverage.scripts).toEqual([]);
expect(result.value.coverage.excluded_scripts).toBe(3);
expect(result.value.script_inventory.main_document_scripts).toBe(0);
for (const scriptId of ["script-a", "script-b"])
expect(
result.value.sources.find(({ script_id }) => script_id === scriptId),
).toMatchObject({ frame_id: null, source: { state: "excluded" } });
expect(browser.commands.map(({ method }) => method)).toEqual(
expect.arrayContaining([
"Profiler.stopPreciseCoverage",
"Target.detachFromTarget",
]),
);
});
it.each(
["listeners", "execution"].flatMap((operation) =>
["source_read", "other_source_read", "final_assertion"].map((phase) => ({