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
+3 -3
View File
@@ -94,9 +94,9 @@ export const captureWebExecution = async (
sources.belongsToDocument(id),
);
const sample =
raw === undefined
? undefined
: normalizePreciseCoverage(raw, sources, retained);
parsed?.success === true
? normalizePreciseCoverage(parsed.data, sources, retained)
: undefined;
await session.options.progress?.report({
phase: "browser_execution",
completed: 2,
+9 -13
View File
@@ -1,34 +1,30 @@
import type { z } from "zod";
import { AnalysisOutputError } from "../../domain/analysisErrorCore.js";
import type { WebExecution } from "../../domain/webExecution.js";
import type { WebRuntimeSource } from "../../domain/webRuntime.js";
import { preciseCoverageSchema } from "./CdpRuntimeProtocol.js";
import type { preciseCoverageSchema } from "./CdpRuntimeProtocol.js";
import type { CdpRuntimeSources } from "./CdpRuntimeSources.js";
/** Validate source-range bounds against exact retained UTF-16 text without flattening nested counts. */
export const normalizePreciseCoverage = (
raw: unknown,
parsed: z.infer<typeof preciseCoverageSchema>,
sources: CdpRuntimeSources,
retained: readonly WebRuntimeSource[],
): {
readonly coverage: WebExecution["coverage"];
readonly timestamp: number;
} => {
const parsed = preciseCoverageSchema.safeParse(raw);
if (!parsed.success)
throw new AnalysisOutputError(
sources.session.operation,
`Malformed precise coverage: ${parsed.error.message}`,
);
const retainedSources = new Map(
retained.map(({ script_id, source }) => [script_id, source]),
);
const scripts: WebExecution["coverage"]["scripts"] = [];
let excluded = 0;
for (const script of parsed.data.result) {
for (const script of parsed.result) {
if (!sources.belongsToDocument(script.scriptId)) {
excluded += 1;
continue;
}
const source = retained.find(
(item) => item.script_id === script.scriptId,
)?.source;
const source = retainedSources.get(script.scriptId);
scripts.push({
script_id: script.scriptId,
reported_url: sources.sourceUrl(script.scriptId, script.url),
@@ -57,7 +53,7 @@ export const normalizePreciseCoverage = (
});
}
return {
timestamp: parsed.data.timestamp,
timestamp: parsed.timestamp,
coverage: {
state: "captured",
reason: null,