refactor(observation): retain complete tool results

This commit is contained in:
morluto
2026-09-29 12:42:08 +08:00
parent 34d00fd323
commit fa2b6c5a61
37 changed files with 425 additions and 701 deletions
+1 -1
View File
@@ -8,7 +8,7 @@ REA can attach to a user-owned Chrome-family browser through the Chrome DevTools
- `inspect_web_page` / `rea inspect-web-page` captures bounded DOM structure, accessibility nodes, scripts, resources, safe response/DOM metadata, attach-window network and console metadata, WebSocket frame sizes, workers, quota, and optionally storage key names, redacted storage-content fingerprints, or script sources. - `inspect_web_page` / `rea inspect-web-page` captures bounded DOM structure, accessibility nodes, scripts, resources, safe response/DOM metadata, attach-window network and console metadata, WebSocket frame sizes, workers, quota, and optionally storage key names, redacted storage-content fingerprints, or script sources.
- `analyze_web_bundle` / `rea analyze-web-bundle` parses approved script artifacts without execution and derives chunk edges, route and endpoint candidates, vendor fingerprints, static WebMCP declarations, and optionally approved source-map/original-source evidence. - `analyze_web_bundle` / `rea analyze-web-bundle` parses approved script artifacts without execution and derives chunk edges, route and endpoint candidates, vendor fingerprints, static WebMCP declarations, and optionally approved source-map/original-source evidence.
- `observe_web_session` / `rea observe-web-session` arms a bounded window for an external user action and records ordered reload, SPA navigation, redirect, failure, lifecycle, and target-termination metadata. - `observe_web_session` / `rea observe-web-session` arms a bounded window for an external user action and records ordered reload, SPA navigation, redirect, failure, lifecycle, and target-termination metadata.
- `discover_webmcp_tools` / `rea discover-webmcp-tools` uses the experimental CDP WebMCP domain to inventory page registrations. REA never exposes `WebMCP.invokeTool`. - `discover_webmcp_tools` / `rea discover-webmcp-tools` uses the experimental CDP WebMCP domain to return every in-scope registration and its complete structural input-schema summary inline. REA never exposes `WebMCP.invokeTool`; malformed or over-limit protocol payloads fail explicitly instead of returning truncated tool metadata.
- `compare_web_captures` / `rea compare-web-captures` compares passive captures or exact-step-aligned browser scenarios. Scenario results commit reproducible literal normalization and expose alignment failures plus artifact-level action, screenshot, DOM, accessibility, URL, history, storage, and event differences. Missing or truncated evidence is never treated as equivalence. - `compare_web_captures` / `rea compare-web-captures` compares passive captures or exact-step-aligned browser scenarios. Scenario results commit reproducible literal normalization and expose alignment failures plus artifact-level action, screenshot, DOM, accessibility, URL, history, storage, and event differences. Missing or truncated evidence is never treated as equivalence.
- `capture_web_screenshot` / `rea capture-web-screenshot` returns an explicitly approved, bounded, content-addressed visible-viewport PNG. - `capture_web_screenshot` / `rea capture-web-screenshot` returns an explicitly approved, bounded, content-addressed visible-viewport PNG.
- `compare_web_screenshots` / `rea compare-web-screenshots` performs bounded local PNG pixel comparison without OCR or external services. - `compare_web_screenshots` / `rea compare-web-screenshots` performs bounded local PNG pixel comparison without OCR or external services.
+10 -15
View File
@@ -104,15 +104,15 @@ const browserParameters = (
after_browser: input.after_scenario.browser, after_browser: input.after_scenario.browser,
before_start_origin: input.before_scenario.scenario.start_origin, before_start_origin: input.before_scenario.scenario.start_origin,
after_start_origin: input.after_scenario.scenario.start_origin, after_start_origin: input.after_scenario.scenario.start_origin,
max_changes: input.max_changes,
normalization_sha256: commitBrowserScenarioNormalization( normalization_sha256: commitBrowserScenarioNormalization(
input.normalization ?? { rules: [] }, input.normalization ?? { rules: [] },
).sha256, ).sha256,
}; };
if ( if (
"max_changes" in input &&
input.before !== undefined && input.before !== undefined &&
input.after !== undefined input.after !== undefined &&
"inspection" in input.before &&
"inspection" in input.after
) )
return { return {
before_target_id: input.before.inspection.target.target_id, before_target_id: input.before.inspection.target.target_id,
@@ -120,7 +120,6 @@ const browserParameters = (
input.before.inspection.capture_window.ended_at, input.before.inspection.capture_window.ended_at,
after_target_id: input.after.inspection.target.target_id, after_target_id: input.after.inspection.target.target_id,
after_capture_ended_at: input.after.inspection.capture_window.ended_at, after_capture_ended_at: input.after.inspection.capture_window.ended_at,
max_changes: input.max_changes,
}; };
if (isScreenshotComparison(input)) if (isScreenshotComparison(input))
return { return {
@@ -142,23 +141,19 @@ const browserParameters = (
target_id: input.target_id, target_id: input.target_id,
maximum_image_bytes: input.maximum_image_bytes, maximum_image_bytes: input.maximum_image_bytes,
}; };
if ("max_tools" in input) if ("max_timeline_events" in input)
return {
...scope,
target_id: input.target_id,
observation_ms: input.observation_ms,
max_tools: input.max_tools,
max_schema_bytes: input.max_schema_bytes,
max_schema_nodes: input.max_schema_nodes,
max_schema_depth: input.max_schema_depth,
};
if (!("include_accessibility_text" in input))
return { return {
...scope, ...scope,
target_id: input.target_id, target_id: input.target_id,
observation_ms: input.observation_ms, observation_ms: input.observation_ms,
max_timeline_events: input.max_timeline_events, max_timeline_events: input.max_timeline_events,
}; };
if (!("limits" in input))
return {
...scope,
target_id: input.target_id,
observation_ms: input.observation_ms,
};
return { return {
...scope, ...scope,
target_id: input.target_id, target_id: input.target_id,
@@ -16,7 +16,7 @@ import { err, ok, type Result } from "../domain/result.js";
interface InventoryProjectionInput { interface InventoryProjectionInput {
readonly inventory_evidence: readonly Evidence[]; readonly inventory_evidence: readonly Evidence[];
readonly limits: unknown; readonly limits?: unknown;
} }
interface InventoryProjectionResult { interface InventoryProjectionResult {
@@ -69,7 +69,9 @@ export const projectInventoryEvidence = <
operation: options.operation, operation: options.operation,
parameters: { parameters: {
inventory_evidence_ids: [...result.source_evidence_ids], inventory_evidence_ids: [...result.source_evidence_ids],
limits: jsonValueSchema.parse(parsed.data.limits), ...("limits" in parsed.data && parsed.data.limits !== undefined
? { limits: jsonValueSchema.parse(parsed.data.limits) }
: {}),
}, },
result: jsonValueSchema.parse(result), result: jsonValueSchema.parse(result),
rawResult: null, rawResult: null,
+8 -14
View File
@@ -11,7 +11,7 @@ import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
const origin = "https://app.example.test"; const origin = "https://app.example.test";
describe("CDP document capture", () => { describe("CDP document capture", () => {
it("walks deeply nested frame trees iteratively and retains a bounded prefix", () => { it("walks deeply nested frame trees and returns every frame and resource", () => {
let frameTree: Record<string, unknown> = { let frameTree: Record<string, unknown> = {
frame: { id: "frame-0", url: `${origin}/0` }, frame: { id: "frame-0", url: `${origin}/0` },
resources: [{ url: `${origin}/0.js`, type: "Script" }], resources: [{ url: `${origin}/0.js`, type: "Script" }],
@@ -27,12 +27,10 @@ describe("CDP document capture", () => {
}; };
} }
const result = { frameTree }; const result = { frameTree };
const frames = captureFrames(result, new Set([origin]), 3); const frames = captureFrames(result, new Set([origin]));
const resources = captureResources(result, new Set([origin]), 2); const resources = captureResources(result, new Set([origin]));
expect(frames).toMatchObject({ total: 2_000 }); expect(frames.items).toHaveLength(2_000);
expect(frames.items).toHaveLength(3); expect(resources.items).toHaveLength(2_000);
expect(resources).toMatchObject({ total: 2_000 });
expect(resources.items).toHaveLength(2);
}); });
it("rebases parent indexes when allowed DOM documents are combined", () => { it("rebases parent indexes when allowed DOM documents are combined", () => {
@@ -91,25 +89,23 @@ describe("CDP document capture", () => {
], ],
}, },
], ],
10,
{ {
includeText: true, includeText: true,
maximumFieldBytes: 4, maximumFieldBytes: 4,
maximumTotalBytes: 7,
}, },
); );
expect(capture.nodes[0]).toMatchObject({ expect(capture.nodes[0]).toMatchObject({
name: "😀", name: "😀",
description: "é", description: "éé",
states: [{ name: "disabled", value: true }], states: [{ name: "disabled", value: true }],
}); });
expect(capture.treeIncomplete).toBe(true); expect(capture.treeIncomplete).toBe(true);
expect(capture.textCapture).toEqual({ expect(capture.textCapture).toEqual({
status: "truncated", status: "truncated",
retained_bytes: 6, retained_bytes: 8,
excluded_fields: 0, excluded_fields: 0,
truncated_fields: 2, truncated_fields: 1,
}); });
}); });
@@ -126,11 +122,9 @@ describe("CDP document capture", () => {
], ],
}, },
], ],
10,
{ {
includeText: false, includeText: false,
maximumFieldBytes: 1_024, maximumFieldBytes: 1_024,
maximumTotalBytes: 64 * 1_024,
}, },
); );
+20 -46
View File
@@ -20,20 +20,18 @@ export type CapturedResource = Omit<
"resource_key" "resource_key"
> & { readonly rawUrl: string }; > & { readonly rawUrl: string };
/** Normalize allowed frames while retaining a bounded prefix and total count. */ /** Normalize every allowed frame, optionally selecting a leading frame. */
export const captureFrames = ( export const captureFrames = (
result: unknown, result: unknown,
allowedOrigins: ReadonlySet<string>, allowedOrigins: ReadonlySet<string>,
maximum: number, maximum?: number,
completeness?: CdpCaptureCompleteness, completeness?: CdpCaptureCompleteness,
): { ): {
readonly total: number;
readonly items: WebPageInspection["frames"]; readonly items: WebPageInspection["frames"];
} => { } => {
const root = recordValue(requiredRecord(result).frameTree); const root = recordValue(requiredRecord(result).frameTree);
if (root === undefined) return { total: 0, items: [] }; if (root === undefined) return { items: [] };
const items: WebPageInspection["frames"] = []; const items: WebPageInspection["frames"] = [];
let total = 0;
for (const tree of walkFrameTrees(root)) { for (const tree of walkFrameTrees(root)) {
const frame = recordValue(tree.frame); const frame = recordValue(tree.frame);
const frameId = stringValue(frame?.id); const frameId = stringValue(frame?.id);
@@ -49,8 +47,7 @@ export const captureFrames = (
); );
continue; continue;
} }
total += 1; if (maximum !== undefined && items.length >= maximum) continue;
if (items.length >= maximum) continue;
items.push({ items.push({
frame_id: frameId, frame_id: frameId,
parent_frame_id: parent_frame_id:
@@ -59,7 +56,7 @@ export const captureFrames = (
origin: sanitized.origin, origin: sanitized.origin,
}); });
} }
return { total, items }; return { items };
}; };
/** Read the current main-frame URL from an untrusted Page.getFrameTree result. */ /** Read the current main-frame URL from an untrusted Page.getFrameTree result. */
@@ -68,20 +65,17 @@ export const mainFrameUrl = (result: unknown): string | undefined =>
recordValue(recordValue(requiredRecord(result).frameTree)?.frame)?.url, recordValue(recordValue(requiredRecord(result).frameTree)?.frame)?.url,
); );
/** Normalize allowed resources while retaining a bounded prefix and total count. */ /** Normalize every allowed resource. */
export const captureResources = ( export const captureResources = (
result: unknown, result: unknown,
allowedOrigins: ReadonlySet<string>, allowedOrigins: ReadonlySet<string>,
maximum: number,
completeness?: CdpCaptureCompleteness, completeness?: CdpCaptureCompleteness,
): { ): {
readonly total: number;
readonly items: readonly CapturedResource[]; readonly items: readonly CapturedResource[];
} => { } => {
const root = recordValue(requiredRecord(result).frameTree); const root = recordValue(requiredRecord(result).frameTree);
if (root === undefined) return { total: 0, items: [] }; if (root === undefined) return { items: [] };
const items: CapturedResource[] = []; const items: CapturedResource[] = [];
let total = 0;
for (const tree of walkFrameTrees(root)) { for (const tree of walkFrameTrees(root)) {
for (const resource of recordsValue(tree.resources)) { for (const resource of recordsValue(tree.resources)) {
const url = allowedSanitizedUrl(resource.url, allowedOrigins); const url = allowedSanitizedUrl(resource.url, allowedOrigins);
@@ -92,8 +86,6 @@ export const captureResources = (
); );
continue; continue;
} }
total += 1;
if (items.length >= maximum) continue;
const contentSize = numberValue(resource.contentSize); const contentSize = numberValue(resource.contentSize);
items.push({ items.push({
rawUrl: stringValue(resource.url) ?? "", rawUrl: stringValue(resource.url) ?? "",
@@ -106,7 +98,7 @@ export const captureResources = (
}); });
} }
} }
return { total, items }; return { items };
}; };
const walkFrameTrees = function* ( const walkFrameTrees = function* (
@@ -163,11 +155,7 @@ export const captureDom = (
: []; : [];
const baseIndex = nodes.length; const baseIndex = nodes.length;
total += nodeTypes.length; total += nodeTypes.length;
for ( for (let index = 0; index < nodeTypes.length; index += 1) {
let index = 0;
index < nodeTypes.length && nodes.length < input.limits.max_dom_nodes;
index += 1
) {
const attributeIndexes = numberArray(attributes[index]); const attributeIndexes = numberArray(attributes[index]);
const parent = Math.trunc(parents[index] ?? -1); const parent = Math.trunc(parents[index] ?? -1);
const nodeIndex = nodes.length; const nodeIndex = nodes.length;
@@ -180,8 +168,7 @@ export const captureDom = (
node_value_length: indexedString(strings, nodeValues[index]).length, node_value_length: indexedString(strings, nodeValues[index]).length,
attribute_names: attributeIndexes attribute_names: attributeIndexes
.filter((_value, attributeIndex) => attributeIndex % 2 === 0) .filter((_value, attributeIndex) => attributeIndex % 2 === 0)
.map((value) => indexedString(strings, value).slice(0, 256)) .map((value) => indexedString(strings, value).slice(0, 256)),
.slice(0, 200),
}); });
const metadata = domMetadata({ const metadata = domMetadata({
strings, strings,
@@ -192,10 +179,6 @@ export const captureDom = (
allowedOrigins, allowedOrigins,
}); });
for (const url of metadata.urls) { for (const url of metadata.urls) {
if (urls.length >= input.limits.max_resources) {
completeness?.truncate("metadata");
continue;
}
urls.push(url); urls.push(url);
if (url.destination_scope !== "approved") { if (url.destination_scope !== "approved") {
excludedUrls += 1; excludedUrls += 1;
@@ -214,18 +197,16 @@ export const captureDom = (
total, total,
nodes, nodes,
urls, urls,
agentHints: agentHints.slice(0, input.limits.max_resources), agentHints,
excludedUrls, excludedUrls,
}; };
}; };
export const captureAccessibility = ( export const captureAccessibility = (
results: readonly unknown[], results: readonly unknown[],
maximum: number,
options: { options: {
readonly includeText: boolean; readonly includeText: boolean;
readonly maximumFieldBytes: number; readonly maximumFieldBytes: number;
readonly maximumTotalBytes: number;
readonly unavailable?: boolean; readonly unavailable?: boolean;
}, },
): { ): {
@@ -243,10 +224,9 @@ export const captureAccessibility = (
return nodeId === undefined ? [] : [nodeId]; return nodeId === undefined ? [] : [nodeId];
}), }),
); );
let retainedBytes = 0;
let excludedFields = 0; let excludedFields = 0;
let truncatedFields = 0; let truncatedFields = 0;
const nodes = all.slice(0, maximum).map((node) => { const nodes = all.map((node) => {
const captureText = (value: unknown): string | null => { const captureText = (value: unknown): string | null => {
const raw = stringValue(recordValue(value)?.value); const raw = stringValue(recordValue(value)?.value);
if (raw === undefined) return null; if (raw === undefined) return null;
@@ -254,12 +234,7 @@ export const captureAccessibility = (
excludedFields += 1; excludedFields += 1;
return null; return null;
} }
const available = Math.max(0, options.maximumTotalBytes - retainedBytes); const captured = boundedUtf8(raw, options.maximumFieldBytes);
const captured = boundedUtf8(
raw,
Math.min(options.maximumFieldBytes, available),
);
retainedBytes += captured.bytes;
if (captured.truncated) { if (captured.truncated) {
truncatedFields += 1; truncatedFields += 1;
if (captured.text === "" && raw !== "") { if (captured.text === "" && raw !== "") {
@@ -279,13 +254,6 @@ export const captureAccessibility = (
states: accessibilityStates(node.properties), states: accessibilityStates(node.properties),
}; };
}); });
for (const node of all.slice(maximum)) {
for (const value of [node.name, node.description]) {
if (stringValue(recordValue(value)?.value) === undefined) continue;
excludedFields += 1;
if (options.includeText) truncatedFields += 1;
}
}
return { return {
total: all.length, total: all.length,
nodes, nodes,
@@ -305,7 +273,13 @@ export const captureAccessibility = (
: truncatedFields > 0 : truncatedFields > 0
? "truncated" ? "truncated"
: "included", : "included",
retained_bytes: retainedBytes, retained_bytes: nodes.reduce(
(total, node) =>
total +
Buffer.byteLength(node.name ?? "") +
Buffer.byteLength(node.description ?? ""),
0,
),
excluded_fields: excludedFields, excluded_fields: excludedFields,
truncated_fields: truncatedFields, truncated_fields: truncatedFields,
}, },
+1 -4
View File
@@ -80,13 +80,10 @@ const inferBodyShape = (
text: string, text: string,
): { readonly shape: JsonShape | null; readonly truncated: boolean } => { ): { readonly shape: JsonShape | null; readonly truncated: boolean } => {
const bytes = Buffer.byteLength(text); const bytes = Buffer.byteLength(text);
const remaining = if (bytes > state.input.limits.max_json_body_bytes) {
state.input.limits.max_total_json_body_bytes - state.jsonBodyBytes;
if (bytes > state.input.limits.max_json_body_bytes || bytes > remaining) {
state.completeness.truncate("json_body_shapes"); state.completeness.truncate("json_body_shapes");
return { shape: null, truncated: true }; return { shape: null, truncated: true };
} }
state.jsonBodyBytes += bytes;
const shape = inferJsonShape(text, { const shape = inferJsonShape(text, {
maximumBytes: state.input.limits.max_json_body_bytes, maximumBytes: state.input.limits.max_json_body_bytes,
maximumNodes: state.input.limits.max_json_shape_nodes, maximumNodes: state.input.limits.max_json_shape_nodes,
+5 -61
View File
@@ -40,13 +40,6 @@ export const handleExecutionContextCreated = (
) )
return; return;
const key = String(identifier); const key = String(identifier);
if (
state.executionContextFrames.size >= state.input.limits.max_scripts &&
!state.executionContextFrames.has(key)
) {
state.completeness.truncate("scripts");
return;
}
state.executionContextFrames.set(key, frameId); state.executionContextFrames.set(key, frameId);
}; };
@@ -73,13 +66,6 @@ export const handleScriptParsed = (
state.completeness.exclude("scripts", exclusionReasonForUrl(rawUrl)); state.completeness.exclude("scripts", exclusionReasonForUrl(rawUrl));
return; return;
} }
if (
state.scripts.size >= state.input.limits.max_scripts &&
!state.scripts.has(scriptId)
) {
state.completeness.drop("scripts");
return;
}
const sourceMap = sourceMapForScript(params.sourceMapURL, rawUrl, state); const sourceMap = sourceMapForScript(params.sourceMapURL, rawUrl, state);
const script: CapturedScript = { const script: CapturedScript = {
scriptId, scriptId,
@@ -148,13 +134,6 @@ export const handleRequestWillBeSent = (
state.network.delete(requestId); state.network.delete(requestId);
return; return;
} }
if (
state.network.size >= state.input.limits.max_network_events &&
!state.network.has(requestId)
) {
state.completeness.drop("network_requests");
return;
}
const initiator = recordValue(params.initiator); const initiator = recordValue(params.initiator);
const initiatorFrame = initiatorLocation(initiator); const initiatorFrame = initiatorLocation(initiator);
const rawInitiatorUrl = stringValue(initiatorFrame?.url); const rawInitiatorUrl = stringValue(initiatorFrame?.url);
@@ -214,10 +193,6 @@ export const handleResponseReceived = (
status: numberValue(response.status) ?? null, status: numberValue(response.status) ?? null,
mime_type: boundedText(response.mimeType, 256), mime_type: boundedText(response.mimeType, 256),
}); });
if (state.responseMetadata.length >= state.input.limits.max_network_events) {
state.completeness.truncate("metadata");
return;
}
const metadata = safeResponseMetadata( const metadata = safeResponseMetadata(
requestId, requestId,
sanitized.url, sanitized.url,
@@ -262,19 +237,13 @@ export const handleConsoleAPICalled = (
); );
return; return;
} }
if (state.console.length >= state.input.limits.max_console_events) {
state.completeness.drop("console_events");
return;
}
const arguments_ = recordsValue(params.args); const arguments_ = recordsValue(params.args);
state.console.push({ state.console.push({
type: (stringValue(params.type) ?? "unknown").slice(0, 100), type: (stringValue(params.type) ?? "unknown").slice(0, 100),
timestamp: numberValue(params.timestamp) ?? 0, timestamp: numberValue(params.timestamp) ?? 0,
argument_types: arguments_ argument_types: arguments_.map((argument) =>
.map((argument) => (stringValue(argument.type) ?? "unknown").slice(0, 100),
(stringValue(argument.type) ?? "unknown").slice(0, 100), ),
)
.slice(0, 100),
url: source.url, url: source.url,
line: integerOrNull(frame.lineNumber), line: integerOrNull(frame.lineNumber),
column: integerOrNull(frame.columnNumber), column: integerOrNull(frame.columnNumber),
@@ -300,15 +269,9 @@ const captureConsoleText = (
for (const [argumentIndex, argument] of arguments_.entries()) { for (const [argumentIndex, argument] of arguments_.entries()) {
const primitive = consolePrimitive(argument); const primitive = consolePrimitive(argument);
if (primitive === undefined) continue; if (primitive === undefined) continue;
const remaining =
state.input.limits.max_total_console_text_bytes - state.consoleTextBytes;
if (remaining <= 0) {
truncatedValues += 1;
continue;
}
const bounded = boundedSensitiveText( const bounded = boundedSensitiveText(
primitive.text, primitive.text,
Math.min(state.input.limits.max_console_text_field_bytes, remaining), state.input.limits.max_console_text_field_bytes,
); );
values.push({ values.push({
argument_index: argumentIndex, argument_index: argumentIndex,
@@ -316,7 +279,6 @@ const captureConsoleText = (
text: bounded.text, text: bounded.text,
}); });
retainedBytes += bounded.bytes; retainedBytes += bounded.bytes;
state.consoleTextBytes += bounded.bytes;
if (bounded.truncated) truncatedValues += 1; if (bounded.truncated) truncatedValues += 1;
} }
if (truncatedValues > 0) state.completeness.truncate("console_text"); if (truncatedValues > 0) state.completeness.truncate("console_text");
@@ -340,10 +302,6 @@ export const handleWebSocketFrame = (
(!state.network.has(requestId) && !state.allowedWebSockets.has(requestId)) (!state.network.has(requestId) && !state.allowedWebSockets.has(requestId))
) )
return; return;
if (state.websockets.length >= state.input.limits.max_websocket_events) {
state.completeness.drop("websocket_frames");
return;
}
const response = recordValue(params.response); const response = recordValue(params.response);
const payload = stringValue(response?.payloadData) ?? ""; const payload = stringValue(response?.payloadData) ?? "";
const opcode = Math.max(0, Math.trunc(numberValue(response?.opcode) ?? 0)); const opcode = Math.max(0, Math.trunc(numberValue(response?.opcode) ?? 0));
@@ -369,17 +327,10 @@ const captureWebSocketShape = (
if (opcode !== 1) if (opcode !== 1)
return { format: "binary", json_shape: null, truncated: false }; return { format: "binary", json_shape: null, truncated: false };
const bytes = Buffer.byteLength(payload); const bytes = Buffer.byteLength(payload);
const remaining = if (bytes > state.input.limits.max_websocket_shape_bytes) {
state.input.limits.max_total_websocket_shape_bytes -
state.websocketShapeBytes;
if (
bytes > state.input.limits.max_websocket_shape_bytes ||
bytes > remaining
) {
state.completeness.truncate("websocket_shapes"); state.completeness.truncate("websocket_shapes");
return { format: "text", json_shape: null, truncated: true }; return { format: "text", json_shape: null, truncated: true };
} }
state.websocketShapeBytes += bytes;
const shape = inferJsonShape(payload, { const shape = inferJsonShape(payload, {
maximumBytes: state.input.limits.max_websocket_shape_bytes, maximumBytes: state.input.limits.max_websocket_shape_bytes,
maximumNodes: state.input.limits.max_json_shape_nodes, maximumNodes: state.input.limits.max_json_shape_nodes,
@@ -431,13 +382,6 @@ export const handleWebSocketCreated = (
state.completeness.exclude("websocket_connections", "disallowed_origin"); state.completeness.exclude("websocket_connections", "disallowed_origin");
return; return;
} }
if (
!state.allowedWebSockets.has(requestId) &&
state.allowedWebSockets.size >= state.input.limits.max_websocket_events
) {
state.completeness.drop("websocket_connections");
return;
}
state.allowedWebSockets.add(requestId); state.allowedWebSockets.add(requestId);
}; };
-3
View File
@@ -20,7 +20,4 @@ export interface CdpCaptureEventsState {
originViolation: boolean; originViolation: boolean;
navigationDuringCapture: boolean; navigationDuringCapture: boolean;
mainFrameId: string | undefined; mainFrameId: string | undefined;
consoleTextBytes: number;
jsonBodyBytes: number;
websocketShapeBytes: number;
} }
+1 -7
View File
@@ -13,7 +13,7 @@ import { recordValue } from "./CdpCaptureValues.js";
export { type CapturedScript } from "./CdpCaptureEventTypes.js"; export { type CapturedScript } from "./CdpCaptureEventTypes.js";
/** Bounded event accumulator that drops payload values at ingestion time. */ /** Event accumulator that validates and normalizes payloads at ingestion. */
export class CdpCaptureEvents implements CdpCaptureEventsState { export class CdpCaptureEvents implements CdpCaptureEventsState {
readonly scripts = new Map<string, CapturedScript>(); readonly scripts = new Map<string, CapturedScript>();
readonly executionContextFrames = new Map<string, string>(); readonly executionContextFrames = new Map<string, string>();
@@ -33,9 +33,6 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
originViolation = false; originViolation = false;
navigationDuringCapture = false; navigationDuringCapture = false;
mainFrameId: string | undefined = undefined; mainFrameId: string | undefined = undefined;
consoleTextBytes = 0;
jsonBodyBytes = 0;
websocketShapeBytes = 0;
readonly input: InspectWebPageInput; readonly input: InspectWebPageInput;
readonly allowedOrigins: ReadonlySet<string>; readonly allowedOrigins: ReadonlySet<string>;
@@ -57,9 +54,6 @@ export class CdpCaptureEvents implements CdpCaptureEventsState {
this.websockets.length = 0; this.websockets.length = 0;
this.responseMetadata.length = 0; this.responseMetadata.length = 0;
this.agentHints.length = 0; this.agentHints.length = 0;
this.consoleTextBytes = 0;
this.jsonBodyBytes = 0;
this.websocketShapeBytes = 0;
this.completeness.reset(); this.completeness.reset();
if (!this.input.include_console_text) if (!this.input.include_console_text)
this.completeness.exclude("console_text", "not_approved", null); this.completeness.exclude("console_text", "not_approved", null);
+9 -25
View File
@@ -16,7 +16,7 @@ import {
stringValue, stringValue,
} from "./CdpCaptureValues.js"; } from "./CdpCaptureValues.js";
/** Capture redacted, bounded storage metadata for one authorized origin. */ /** Capture redacted storage metadata for one authorized origin. */
export const captureStorage = async ( export const captureStorage = async (
context: { context: {
readonly connection: CdpConnection; readonly connection: CdpConnection;
@@ -28,7 +28,6 @@ export const captureStorage = async (
limitations: string[], limitations: string[],
): Promise<{ ): Promise<{
readonly value: WebPageInspection["storage"]; readonly value: WebPageInspection["storage"];
readonly truncated: boolean;
}> => { }> => {
const quota = recordValue( const quota = recordValue(
await optionalCdpCommand( await optionalCdpCommand(
@@ -64,22 +63,17 @@ export const captureStorage = async (
: undefined; : undefined;
const cacheValues = recordsValue(recordValue(cacheRaw)?.caches); const cacheValues = recordsValue(recordValue(cacheRaw)?.caches);
const caches = capturedCaches(cacheValues); const caches = capturedCaches(cacheValues);
const maximum = context.input.limits.max_storage_keys;
const fingerprints = context.input.include_storage_fingerprints const fingerprints = context.input.include_storage_fingerprints
? await captureStorageFingerprints({ ? await captureStorageFingerprints({
context, context,
origin, origin,
local, local,
session, session,
indexedDbNames: indexed.slice(0, maximum), indexedDbNames: indexed,
caches: caches.slice(0, maximum), caches,
limitations, limitations,
}) })
: { items: [], complete: false as const, truncated: false }; : { items: [], complete: false };
const truncated =
[local.items, session.items, indexed, caches].some(
(items) => items.length > maximum,
) || fingerprints.truncated;
const structuredStorageComplete = const structuredStorageComplete =
indexedRaw !== undefined && indexedRaw !== undefined &&
Array.isArray(indexedValues) && Array.isArray(indexedValues) &&
@@ -91,25 +85,15 @@ export const captureStorage = async (
origin, origin,
usage_bytes: numberValue(quota?.usage) ?? null, usage_bytes: numberValue(quota?.usage) ?? null,
quota_bytes: numberValue(quota?.quota) ?? null, quota_bytes: numberValue(quota?.quota) ?? null,
local_storage_keys: local.items local_storage_keys: local.items.map(({ key }) => key.slice(0, 1_024)),
.map(({ key }) => key.slice(0, 1_024)) session_storage_keys: session.items.map(({ key }) => key.slice(0, 1_024)),
.slice(0, maximum), indexed_db_names: indexed.map((name) => name.slice(0, 1_024)),
session_storage_keys: session.items cache_names: caches.map(({ name }) => name.slice(0, 1_024)),
.map(({ key }) => key.slice(0, 1_024))
.slice(0, maximum),
indexed_db_names: indexed
.map((name) => name.slice(0, 1_024))
.slice(0, maximum),
cache_names: caches
.map(({ name }) => name.slice(0, 1_024))
.slice(0, maximum),
content_fingerprints: [...fingerprints.items], content_fingerprints: [...fingerprints.items],
fingerprint_algorithm: "sha256", fingerprint_algorithm: "sha256",
fingerprints_complete: fingerprints_complete: fingerprints.complete && structuredStorageComplete,
fingerprints.complete && structuredStorageComplete && !truncated,
values_redacted: true, values_redacted: true,
}, },
truncated,
}; };
}; };
+97 -102
View File
@@ -37,9 +37,7 @@ interface StorageFingerprintContext {
interface FingerprintState { interface FingerprintState {
readonly items: StorageFingerprint[]; readonly items: StorageFingerprint[];
readonly maximum: number;
complete: boolean; complete: boolean;
truncated: boolean;
} }
interface FingerprintRun { interface FingerprintRun {
@@ -60,10 +58,8 @@ interface CaptureStorageFingerprintInput {
type StorageFingerprintCapture = { type StorageFingerprintCapture = {
readonly items: readonly StorageFingerprint[]; readonly items: readonly StorageFingerprint[];
} & ( readonly complete: boolean;
| { readonly complete: true; readonly truncated: false } };
| { readonly complete: false; readonly truncated: boolean }
);
/** Capture stable hashes of approved storage content without returning values. */ /** Capture stable hashes of approved storage content without returning values. */
export const captureStorageFingerprints = async ({ export const captureStorageFingerprints = async ({
@@ -77,9 +73,7 @@ export const captureStorageFingerprints = async ({
}: CaptureStorageFingerprintInput): Promise<StorageFingerprintCapture> => { }: CaptureStorageFingerprintInput): Promise<StorageFingerprintCapture> => {
const state: FingerprintState = { const state: FingerprintState = {
items: [], items: [],
maximum: context.input.limits.max_storage_keys,
complete: local.complete && session.complete, complete: local.complete && session.complete,
truncated: false,
}; };
const run = { context, limitations, state }; const run = { context, limitations, state };
addStorageItems(state, "local_storage", local.items); addStorageItems(state, "local_storage", local.items);
@@ -94,16 +88,8 @@ export const captureStorageFingerprints = async ({
), ),
); );
const complete = const complete =
state.complete && state.complete && state.items.every(({ complete }) => complete);
!state.truncated && return { items: state.items, complete };
state.items.every(({ complete }) => complete);
return complete
? { items: state.items, complete: true, truncated: false }
: {
items: state.items,
complete: false,
truncated: state.truncated,
};
}; };
const addStorageItems = ( const addStorageItems = (
@@ -175,10 +161,6 @@ const addIndexedDb = async (
names: readonly string[], names: readonly string[],
): Promise<void> => { ): Promise<void> => {
for (const databaseName of names) { for (const databaseName of names) {
if (run.state.items.length >= run.state.maximum) {
run.state.truncated = true;
return;
}
const raw = await optionalCdpCommand( const raw = await optionalCdpCommand(
run.context, run.context,
"IndexedDB.requestDatabase", "IndexedDB.requestDatabase",
@@ -261,57 +243,62 @@ const addIndexedDbRecords = async (
readonly objectStoreName: string; readonly objectStoreName: string;
}, },
): Promise<void> => { ): Promise<void> => {
const remaining = run.state.maximum - run.state.items.length; let skipCount = 0;
if (remaining <= 0) { while (true) {
run.state.truncated = true; const raw = await optionalCdpCommand(
return; run.context,
} "IndexedDB.requestData",
const raw = await optionalCdpCommand( {
run.context, securityOrigin: input.origin,
"IndexedDB.requestData", databaseName: input.databaseName,
{ objectStoreName: input.objectStoreName,
securityOrigin: input.origin, indexName: "",
databaseName: input.databaseName, skipCount,
objectStoreName: input.objectStoreName, pageSize: STORAGE_ENTRY_PAGE_SIZE,
indexName: "",
skipCount: 0,
pageSize: remaining,
},
run.limitations,
);
const result = recordValue(raw);
if (raw === undefined || result === undefined) {
run.state.complete = false;
return;
}
if (
!Array.isArray(result.objectStoreDataEntries) ||
typeof result.hasMore !== "boolean"
) {
run.state.complete = false;
return;
}
const entries = recordsValue(result.objectStoreDataEntries);
if (entries.length !== result.objectStoreDataEntries.length)
run.state.complete = false;
for (const entry of entries) {
const key = stableValue(entry.key);
const primaryKey = stableValue(entry.primaryKey);
const value = stableValue(entry.value);
addFingerprint(run.state, {
scope: "indexed_db_record",
identity: {
database: input.databaseName,
store: input.objectStoreName,
key,
primaryKey,
}, },
value, run.limitations,
complete: remoteObjectComplete(entry.value), );
}); const result = recordValue(raw);
if (raw === undefined || result === undefined) {
run.state.complete = false;
return;
}
if (
!Array.isArray(result.objectStoreDataEntries) ||
typeof result.hasMore !== "boolean"
) {
run.state.complete = false;
return;
}
const entries = recordsValue(result.objectStoreDataEntries);
if (entries.length !== result.objectStoreDataEntries.length)
run.state.complete = false;
for (const entry of entries) {
const key = stableValue(entry.key);
const primaryKey = stableValue(entry.primaryKey);
const value = stableValue(entry.value);
addFingerprint(run.state, {
scope: "indexed_db_record",
identity: {
database: input.databaseName,
store: input.objectStoreName,
key,
primaryKey,
},
value,
complete: remoteObjectComplete(entry.value),
});
}
if (result.hasMore !== true) return;
if (
entries.length === 0 ||
skipCount > Number.MAX_SAFE_INTEGER - entries.length
) {
run.state.complete = false;
return;
}
skipCount += entries.length;
} }
if (result.hasMore === true || entries.length > remaining)
run.state.truncated = true;
}; };
const addCaches = async ( const addCaches = async (
@@ -319,34 +306,44 @@ const addCaches = async (
caches: readonly CapturedCache[], caches: readonly CapturedCache[],
): Promise<void> => { ): Promise<void> => {
for (const cache of caches) { for (const cache of caches) {
const remaining = run.state.maximum - run.state.items.length; let skipCount = 0;
if (remaining <= 0) { let returnCount: number | undefined;
run.state.truncated = true; while (returnCount === undefined || skipCount < returnCount) {
return; const raw = await optionalCdpCommand(
run.context,
"CacheStorage.requestEntries",
{ cacheId: cache.id, skipCount, pageSize: STORAGE_ENTRY_PAGE_SIZE },
run.limitations,
);
const result = recordValue(raw);
if (raw === undefined || result === undefined) {
run.state.complete = false;
break;
}
if (
!Array.isArray(result.cacheDataEntries) ||
typeof result.returnCount !== "number" ||
!Number.isSafeInteger(result.returnCount) ||
result.returnCount < 0
) {
run.state.complete = false;
break;
}
const entries = recordsValue(result.cacheDataEntries);
if (entries.length !== result.cacheDataEntries.length)
run.state.complete = false;
for (const entry of entries) await addCacheEntry(run, cache, entry);
returnCount = result.returnCount;
if (skipCount + entries.length >= returnCount) break;
if (
entries.length === 0 ||
skipCount > Number.MAX_SAFE_INTEGER - entries.length
) {
run.state.complete = false;
break;
}
skipCount += entries.length;
} }
const raw = await optionalCdpCommand(
run.context,
"CacheStorage.requestEntries",
{ cacheId: cache.id, skipCount: 0, pageSize: remaining },
run.limitations,
);
const result = recordValue(raw);
if (raw === undefined || result === undefined) {
run.state.complete = false;
continue;
}
if (
!Array.isArray(result.cacheDataEntries) ||
typeof result.returnCount !== "number"
) {
run.state.complete = false;
continue;
}
const entries = recordsValue(result.cacheDataEntries);
if (entries.length !== result.cacheDataEntries.length)
run.state.complete = false;
for (const entry of entries) await addCacheEntry(run, cache, entry);
if (result.returnCount > entries.length) run.state.truncated = true;
} }
}; };
@@ -406,10 +403,6 @@ const addFingerprint = (
readonly complete: boolean; readonly complete: boolean;
}, },
): void => { ): void => {
if (state.items.length >= state.maximum) {
state.truncated = true;
return;
}
state.items.push({ state.items.push({
scope: input.scope, scope: input.scope,
identity_sha256: digest(input.identity), identity_sha256: digest(input.identity),
@@ -421,6 +414,8 @@ const addFingerprint = (
}); });
}; };
const STORAGE_ENTRY_PAGE_SIZE = 1_000;
const digest = (value: unknown): string => const digest = (value: unknown): string =>
createHash("sha256").update(encodedValue(value)).digest("hex"); createHash("sha256").update(encodedValue(value)).digest("hex");
+10 -28
View File
@@ -84,7 +84,7 @@ export const capturePage = async (
allowedOrigins, allowedOrigins,
limitations: [ limitations: [
"Observation starts when REA attaches; prior network and console activity is unavailable.", "Observation starts when REA attaches; prior network and console activity is unavailable.",
"Raw network headers, bodies, cookies, storage values, console objects, and WebSocket payloads are never retained; separately approved captures retain only bounded redacted text or value-free shapes.", "Raw network headers, bodies, cookies, storage values, console objects, and WebSocket payloads are never retained; separately approved captures retain only redacted text or value-free shapes.",
"Source maps are reported only as declarative URLs and are not fetched.", "Source maps are reported only as declarative URLs and are not fetched.",
"URL-less scripts and console events without an allowed source URL are excluded because their origin cannot be proven.", "URL-less scripts and console events without an allowed source URL are excluded because their origin cannot be proven.",
], ],
@@ -109,12 +109,10 @@ const captureAuthorizedPage = async (
const frameCapture = captureFrames( const frameCapture = captureFrames(
frameResult, frameResult,
allowedOrigins, allowedOrigins,
input.limits.max_frames, undefined,
state.events.completeness, state.events.completeness,
); );
const frames = frameCapture.items; const frames = frameCapture.items;
if (frameCapture.total > frames.length)
state.events.completeness.truncate("frames");
const captureFrame = frames[0]; const captureFrame = frames[0];
if (captureFrame === undefined) if (captureFrame === undefined)
throw new BrowserObservationError("inspect_web_page", "target_not_allowed"); throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
@@ -122,19 +120,13 @@ const captureAuthorizedPage = async (
const resourceCapture = captureResources( const resourceCapture = captureResources(
await connection.send("Page.getResourceTree", {}, sessionId, signal), await connection.send("Page.getResourceTree", {}, sessionId, signal),
allowedOrigins, allowedOrigins,
input.limits.max_resources,
state.events.completeness, state.events.completeness,
); );
if (resourceCapture.total > resourceCapture.items.length)
state.events.completeness.truncate("resources");
const resources = stableWebResources( const resources = stableWebResources(
resourceCapture.items.map(publicResource), resourceCapture.items.map(publicResource),
); );
const dom = await capturePageDom(state); const dom = await capturePageDom(state);
if (dom.total > dom.nodes.length) state.events.completeness.truncate("dom");
const accessibility = await accessibilityForFrames(state, frames); const accessibility = await accessibilityForFrames(state, frames);
if (accessibility.total > accessibility.nodes.length)
state.events.completeness.truncate("accessibility");
const scripts = await captureScripts({ const scripts = await captureScripts({
context, context,
events: state.events, events: state.events,
@@ -142,19 +134,15 @@ const captureAuthorizedPage = async (
resources, resources,
frameIds: new Set(frames.map((frame) => frame.frame_id)), frameIds: new Set(frames.map((frame) => frame.frame_id)),
}); });
const workerCapture = await captureWorkers( const workers = await captureWorkers(
{ context, allowedOrigins, limitations, events: state.events }, { context, allowedOrigins, limitations, events: state.events },
new Set(frames.map((frame) => frame.frame_id)), new Set(frames.map((frame) => frame.frame_id)),
); );
if (workerCapture.total > workerCapture.items.length)
state.events.completeness.truncate("workers");
const storageCapture = await captureStorage( const storageCapture = await captureStorage(
context, context,
frames[0]?.origin ?? new URL(attachedUrl).origin, frames[0]?.origin ?? new URL(attachedUrl).origin,
limitations, limitations,
); );
if (storageCapture.truncated)
state.events.completeness.truncate("storage_keys");
if (!input.include_storage_keys) if (!input.include_storage_keys)
state.events.completeness.exclude("storage_keys", "not_approved", null); state.events.completeness.exclude("storage_keys", "not_approved", null);
const completedFrameResult = await authorizedFrameTree( const completedFrameResult = await authorizedFrameTree(
@@ -175,7 +163,7 @@ const captureAuthorizedPage = async (
accessibility, accessibility,
scripts: scripts.inventory, scripts: scripts.inventory,
resources, resources,
workers: workerCapture.items, workers,
storage: storageCapture.value, storage: storageCapture.value,
}), }),
sourceMapRequests: scripts.sourceMapRequests, sourceMapRequests: scripts.sourceMapRequests,
@@ -213,8 +201,7 @@ const authorizeObservationWindow = async (
await report(context.progress, 1, "Enabling passive CDP domains"); await report(context.progress, 1, "Enabling passive CDP domains");
await connection.send("Page.enable", {}, sessionId, signal); await connection.send("Page.enable", {}, sessionId, signal);
const initialFrameResult = await authorizedFrameTree(context, allowedOrigins); const initialFrameResult = await authorizedFrameTree(context, allowedOrigins);
const mainFrame = captureFrames(initialFrameResult, allowedOrigins, 1) const mainFrame = captureFrames(initialFrameResult, allowedOrigins).items[0];
.items[0];
if (mainFrame === undefined) if (mainFrame === undefined)
throw new BrowserObservationError("inspect_web_page", "target_not_allowed"); throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
events.beginAuthorizedFrame(mainFrame.frame_id); events.beginAuthorizedFrame(mainFrame.frame_id);
@@ -367,16 +354,11 @@ const accessibilityForFrames = async (
else results.push(result); else results.push(result);
} }
if (unavailable) events.completeness.unavailable("accessibility"); if (unavailable) events.completeness.unavailable("accessibility");
const capture = captureAccessibility( const capture = captureAccessibility(results, {
results, includeText: context.input.include_accessibility_text,
context.input.limits.max_ax_nodes, maximumFieldBytes: context.input.limits.max_ax_text_field_bytes,
{ ...(results.length === 0 && unavailable ? { unavailable: true } : {}),
includeText: context.input.include_accessibility_text, });
maximumFieldBytes: context.input.limits.max_ax_text_field_bytes,
maximumTotalBytes: context.input.limits.max_total_ax_text_bytes,
...(results.length === 0 && unavailable ? { unavailable: true } : {}),
},
);
if (!context.input.include_accessibility_text) if (!context.input.include_accessibility_text)
events.completeness.exclude( events.completeness.exclude(
"accessibility", "accessibility",
+2 -16
View File
@@ -34,7 +34,6 @@ interface ScriptDraftState {
readonly frameIds: ReadonlySet<string>; readonly frameIds: ReadonlySet<string>;
readonly rawResources: readonly CapturedResource[]; readonly rawResources: readonly CapturedResource[];
readonly resources: WebPageInspection["resources"]; readonly resources: WebPageInspection["resources"];
totalSourceBytes: number;
} }
type ScriptDraftItem = Omit< type ScriptDraftItem = Omit<
@@ -52,7 +51,6 @@ export const captureScripts = async (
frameIds, frameIds,
rawResources, rawResources,
resources, resources,
totalSourceBytes: 0,
}; };
const drafts: { const drafts: {
readonly item: ScriptDraftItem; readonly item: ScriptDraftItem;
@@ -113,13 +111,6 @@ const captureScriptSource = async (
events.completeness.truncate("script_sources"); events.completeness.truncate("script_sources");
return sourceExcluded("declared script length exceeds per-script limit"); return sourceExcluded("declared script length exceeds per-script limit");
} }
if (
state.totalSourceBytes + script.length >
context.input.limits.max_total_script_source_bytes
) {
events.completeness.truncate("script_sources");
return sourceExcluded("total script source limit reached");
}
const result = requiredRecord( const result = requiredRecord(
await context.connection.send( await context.connection.send(
"Debugger.getScriptSource", "Debugger.getScriptSource",
@@ -130,15 +121,10 @@ const captureScriptSource = async (
); );
const content = stringValue(result.scriptSource) ?? ""; const content = stringValue(result.scriptSource) ?? "";
const bytes = Buffer.byteLength(content); const bytes = Buffer.byteLength(content);
if ( if (bytes > context.input.limits.max_script_source_bytes) {
bytes > context.input.limits.max_script_source_bytes ||
state.totalSourceBytes + bytes >
context.input.limits.max_total_script_source_bytes
) {
events.completeness.truncate("script_sources"); events.completeness.truncate("script_sources");
return sourceExcluded("actual source exceeds configured byte limits"); return sourceExcluded("actual source exceeds the per-script byte limit");
} }
state.totalSourceBytes += bytes;
return sourceResult(content); return sourceResult(content);
}; };
+2 -8
View File
@@ -21,10 +21,7 @@ interface CaptureWorkersInput {
export const captureWorkers = async ( export const captureWorkers = async (
input: CaptureWorkersInput, input: CaptureWorkersInput,
frameIds: ReadonlySet<string>, frameIds: ReadonlySet<string>,
): Promise<{ ): Promise<WebPageInspection["workers"]> => {
readonly total: number;
readonly items: WebPageInspection["workers"];
}> => {
const { context, allowedOrigins, limitations, events } = input; const { context, allowedOrigins, limitations, events } = input;
const completeness = events.completeness; const completeness = events.completeness;
const result = await optionalCdpCommand( const result = await optionalCdpCommand(
@@ -34,7 +31,6 @@ export const captureWorkers = async (
limitations, limitations,
); );
const items: WebPageInspection["workers"] = []; const items: WebPageInspection["workers"] = [];
let total = 0;
if (result === undefined) completeness.unavailable("workers"); if (result === undefined) completeness.unavailable("workers");
for (const target of recordsValue(recordValue(result)?.targetInfos)) { for (const target of recordsValue(recordValue(result)?.targetInfos)) {
const type = stringValue(target.type) ?? ""; const type = stringValue(target.type) ?? "";
@@ -60,8 +56,6 @@ export const captureWorkers = async (
); );
continue; continue;
} }
total += 1;
if (items.length >= context.input.limits.max_workers) continue;
items.push({ items.push({
target_id: (stringValue(target.targetId) ?? "").slice(0, 256), target_id: (stringValue(target.targetId) ?? "").slice(0, 256),
type: type.slice(0, 100), type: type.slice(0, 100),
@@ -72,5 +66,5 @@ export const captureWorkers = async (
parent_frame_id: boundedText(target.parentFrameId, 256) ?? null, parent_frame_id: boundedText(target.parentFrameId, 256) ?? null,
}); });
} }
return { total, items }; return items;
}; };
+15 -15
View File
@@ -58,7 +58,12 @@ export const discoverWebMcp = async (
const initialUrl = mainFrameUrl(frameTree); const initialUrl = mainFrameUrl(frameTree);
if (allowedSanitizedUrl(initialUrl, origins) === undefined) if (allowedSanitizedUrl(initialUrl, origins) === undefined)
throw new BrowserObservationError("inspect_web_page", "target_not_allowed"); throw new BrowserObservationError("inspect_web_page", "target_not_allowed");
const frames = captureFrames(frameTree, origins, 1_000, completeness).items; const frames = captureFrames(
frameTree,
origins,
undefined,
completeness,
).items;
const frameUrls = new Map( const frameUrls = new Map(
frames.map((frame) => [ frames.map((frame) => [
frame.frame_id, frame.frame_id,
@@ -263,10 +268,6 @@ const ingestWebMcpEvent = (options: WebMcpIngestOptions): void => {
for (const declared of recordsValue(params.tools)) { for (const declared of recordsValue(params.tools)) {
const normalized = normalizeTool(declared, input, frames, completeness); const normalized = normalizeTool(declared, input, frames, completeness);
if (normalized === undefined) continue; if (normalized === undefined) continue;
if (!tools.has(normalized.tool_key) && tools.size >= input.max_tools) {
completeness.drop("webmcp_tools");
continue;
}
tools.set(normalized.tool_key, normalized); tools.set(normalized.tool_key, normalized);
} }
}; };
@@ -308,7 +309,7 @@ const normalizeTool = (
numberValue(value.backendNodeId) === undefined numberValue(value.backendNodeId) === undefined
? "imperative" ? "imperative"
: "declarative", : "declarative",
input_schema_shape: schemaShape(value.inputSchema, input, completeness), input_schema_shape: schemaShape(value.inputSchema),
annotations: { annotations: {
read_only: booleanOrNull(annotations?.readOnly), read_only: booleanOrNull(annotations?.readOnly),
untrusted_content: booleanOrNull(annotations?.untrustedContent), untrusted_content: booleanOrNull(annotations?.untrustedContent),
@@ -322,19 +323,18 @@ const normalizeTool = (
}; };
}; };
const schemaShape = ( const schemaShape = (value: unknown) => {
value: unknown,
input: DiscoverWebMcpToolsInput,
completeness: CdpCaptureCompleteness,
) => {
if (recordValue(value) === undefined) return null; if (recordValue(value) === undefined) return null;
const encoded = JSON.stringify(value); const encoded = JSON.stringify(value);
const shape = inferJsonShape(encoded, { const shape = inferJsonShape(encoded, {
maximumBytes: input.max_schema_bytes, maximumBytes: 256 * 1_024,
maximumNodes: input.max_schema_nodes, maximumNodes: 5_000,
maximumDepth: input.max_schema_depth, maximumDepth: 20,
}); });
if (shape === null || shape.truncated) completeness.truncate("webmcp_tools"); if (shape === null)
throw new BrowserObservationError("inspect_web_page", "protocol_error");
if (shape.truncated)
throw new BrowserObservationError("inspect_web_page", "payload_limit");
return shape; return shape;
}; };
-41
View File
@@ -51,34 +51,6 @@ const registerWebMcp = (
.max(10_000) .max(10_000)
.default(100) .default(100)
.describe("Observation duration in milliseconds"), .describe("Observation duration in milliseconds"),
maxTools: z
.number()
.int()
.min(1)
.max(5_000)
.default(500)
.describe("Maximum page-declared WebMCP tools to return"),
maxSchemaBytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(256 * 1_024)
.describe("Maximum serialized size of one declared tool schema"),
maxSchemaNodes: z
.number()
.int()
.min(1)
.max(100_000)
.default(5_000)
.describe("Maximum nodes in one declared tool schema"),
maxSchemaDepth: z
.number()
.int()
.min(1)
.max(100)
.default(20)
.describe("Maximum nesting depth in one declared tool schema"),
}), }),
run: ({ args, options }) => run: ({ args, options }) =>
logCliCommand(logger, "discover-webmcp-tools", async () => { logCliCommand(logger, "discover-webmcp-tools", async () => {
@@ -90,10 +62,6 @@ const registerWebMcp = (
options.allowedOrigins ?? context.allowedBrowserOrigins, options.allowedOrigins ?? context.allowedBrowserOrigins,
target_id: args.targetId, target_id: args.targetId,
observation_ms: options.observationMs, observation_ms: options.observationMs,
max_tools: options.maxTools,
max_schema_bytes: options.maxSchemaBytes,
max_schema_nodes: options.maxSchemaNodes,
max_schema_depth: options.maxSchemaDepth,
}); });
if (!parsed.success) return inputError("discover_webmcp_tools"); if (!parsed.success) return inputError("discover_webmcp_tools");
const result = await discoverWebMcpTools( const result = await discoverWebMcpTools(
@@ -118,13 +86,6 @@ const registerCaptureDiff = (
afterJson: z.string().describe("Later normalized web capture JSON"), afterJson: z.string().describe("Later normalized web capture JSON"),
}), }),
options: z.object({ options: z.object({
maxChanges: z
.number()
.int()
.min(1)
.max(20_000)
.default(2_000)
.describe("Maximum normalized changes to return"),
normalizationJson: z normalizationJson: z
.string() .string()
.default('{"rules":[]}') .default('{"rules":[]}')
@@ -141,14 +102,12 @@ const registerCaptureDiff = (
before_scenario: before, before_scenario: before,
after_scenario: after, after_scenario: after,
normalization: parseJson(options.normalizationJson), normalization: parseJson(options.normalizationJson),
max_changes: options.maxChanges,
}); });
const parsed = scenarioComparison.success const parsed = scenarioComparison.success
? scenarioComparison ? scenarioComparison
: browserCaptureComparisonInputSchema.safeParse({ : browserCaptureComparisonInputSchema.safeParse({
before, before,
after, after,
max_changes: options.maxChanges,
}); });
if (!parsed.success) return inputError("compare_web_captures"); if (!parsed.success) return inputError("compare_web_captures");
const result = await compareWebCaptureEvidence( const result = await compareWebCaptureEvidence(
+2 -7
View File
@@ -202,7 +202,7 @@ export const BROWSER_TOOL_CONTRACTS = [
name: "discover_webmcp_tools", name: "discover_webmcp_tools",
...toolContractMetadata("discover_webmcp_tools"), ...toolContractMetadata("discover_webmcp_tools"),
description: description:
"Passively inventory page-registered WebMCP tools using the experimental CDP WebMCP domain. Metadata is bounded and page-declared-untrusted; REA never registers or invokes discovered tools.", "Passively inventory every page-registered WebMCP tool and its complete structural input-schema summary using the experimental CDP WebMCP domain. Metadata is page-declared-untrusted; REA never registers or invokes discovered tools.",
kind: "browser-provider", kind: "browser-provider",
inputSchema: discoverWebMcpToolsInputSchema, inputSchema: discoverWebMcpToolsInputSchema,
outputSchema: webMcpOutputSchema, outputSchema: webMcpOutputSchema,
@@ -214,10 +214,6 @@ export const BROWSER_TOOL_CONTRACTS = [
allowed_origins: [origin], allowed_origins: [origin],
target_id: "TARGET_ID_FROM_LIST_BROWSER_TARGETS", target_id: "TARGET_ID_FROM_LIST_BROWSER_TARGETS",
observation_ms: 100, observation_ms: 100,
max_tools: 500,
max_schema_bytes: 262_144,
max_schema_nodes: 5_000,
max_schema_depth: 20,
}, },
}, },
], ],
@@ -226,7 +222,7 @@ export const BROWSER_TOOL_CONTRACTS = [
name: "compare_web_captures", name: "compare_web_captures",
...toolContractMetadata("compare_web_captures"), ...toolContractMetadata("compare_web_captures"),
description: description:
"Compare passive web captures by providing before and after, or compare recorded scenarios by providing before_scenario and after_scenario with an optional normalization policy. Use exactly one input group. Scenario comparison aligns exact step IDs, records deterministic literal normalization, and exposes bounded artifact-level changes plus alignment failures. Missing or truncated evidence never proves equality.", "Compare passive web captures by providing before and after, or compare recorded scenarios by providing before_scenario and after_scenario with an optional normalization policy. Use exactly one input group. Scenario comparison aligns exact step IDs, records deterministic literal normalization, and returns every artifact-level difference plus alignment failures inline. Missing or truncated evidence never proves equality.",
kind: "browser-provider", kind: "browser-provider",
inputSchema: browserCaptureComparisonInputSchema, inputSchema: browserCaptureComparisonInputSchema,
outputSchema: captureDiffOutputSchema, outputSchema: captureDiffOutputSchema,
@@ -246,7 +242,6 @@ export const BROWSER_TOOL_CONTRACTS = [
}, },
], ],
}, },
max_changes: 2_000,
}, },
}, },
], ],
-1
View File
@@ -76,7 +76,6 @@ describe("tool contract surface", () => {
"before_scenario", "before_scenario",
"after_scenario", "after_scenario",
"normalization", "normalization",
"max_changes",
]), ]),
); );
const example = contract.examples[0]; const example = contract.examples[0];
+13 -64
View File
@@ -4,7 +4,6 @@ import canonicalize from "canonicalize";
import { z } from "zod"; import { z } from "zod";
import { parseArtifactInventoryEvidence } from "./artifactInventoryEvidence.js"; import { parseArtifactInventoryEvidence } from "./artifactInventoryEvidence.js";
import { projectBoundedCartesian } from "./boundedCartesianProjection.js";
import { evidenceSchema } from "./evidence.js"; import { evidenceSchema } from "./evidence.js";
const digestSchema = z.string().regex(/^[a-f0-9]{64}$/u); const digestSchema = z.string().regex(/^[a-f0-9]{64}$/u);
@@ -19,12 +18,7 @@ const componentSchema = z.strictObject({
/** Authenticated APK inventory pages projected as one Android application. */ /** Authenticated APK inventory pages projected as one Android application. */
export const androidApplicationProjectionInputSchema = z.strictObject({ export const androidApplicationProjectionInputSchema = z.strictObject({
inventory_evidence: z.array(evidenceSchema).min(1).max(100), inventory_evidence: z.array(evidenceSchema).min(1),
limits: z
.strictObject({
max_components: z.number().int().min(1).max(10_000).default(1_000),
})
.default({ max_components: 1_000 }),
}); });
/** Deterministic, execution-free Android application inventory projection. */ /** Deterministic, execution-free Android application inventory projection. */
@@ -32,7 +26,7 @@ export const androidApplicationProjectionResultSchema = z.strictObject({
projection_id: z.string().regex(/^adp_[a-f0-9]{64}$/u), projection_id: z.string().regex(/^adp_[a-f0-9]{64}$/u),
root_sha256: digestSchema, root_sha256: digestSchema,
root_format: z.literal("apk"), root_format: z.literal("apk"),
source_evidence_ids: z.array(evidenceIdSchema).min(1).max(100), source_evidence_ids: z.array(evidenceIdSchema).min(1),
components: z.strictObject({ components: z.strictObject({
manifests: z.array(componentSchema), manifests: z.array(componentSchema),
resources: z.array(componentSchema), resources: z.array(componentSchema),
@@ -67,10 +61,8 @@ export const androidApplicationProjectionResultSchema = z.strictObject({
}), }),
), ),
coverage: z.strictObject({ coverage: z.strictObject({
status: z.enum(["complete-within-inventory", "partial", "truncated"]), status: z.enum(["complete-within-inventory", "partial"]),
inventory_complete: z.boolean(), inventory_complete: z.boolean(),
omitted_components: z.number().int().min(0),
omitted_bridge_candidates: z.number().int().min(0),
}), }),
limitations: z.array(z.string().min(1).max(4_096)).max(100), limitations: z.array(z.string().min(1).max(4_096)).max(100),
}); });
@@ -115,27 +107,15 @@ export const projectAndroidApplication = (
} satisfies Component; } satisfies Component;
}); });
const classified = classify(all); const classified = classify(all);
const components = retain(classified, parsed.limits.max_components);
const omittedComponents = count(classified) - count(components);
const bridgeProjection = bridgeCandidates( const bridgeProjection = bridgeCandidates(
[...components.dex, ...components.jvm_classes], [...classified.dex, ...classified.jvm_classes],
components.native_libraries, classified.native_libraries,
parsed.limits.max_components,
); );
const components = classified;
const limitations = [ const limitations = [
...(!inventory.complete ...(!inventory.complete
? ["Source inventory pages are incomplete; absence is unknown."] ? ["Source inventory pages are incomplete; absence is unknown."]
: []), : []),
...(omittedComponents > 0
? [
`${String(omittedComponents)} component observations were omitted by the projection limit.`,
]
: []),
...(bridgeProjection.omitted > 0
? [
`${String(bridgeProjection.omitted)} bridge hypotheses were omitted by the projection limit.`,
]
: []),
"Manifest, resource, signing, and bytecode semantics require a dedicated Android provider; this projection reports exact inventory paths and hashes only.", "Manifest, resource, signing, and bytecode semantics require a dedicated Android provider; this projection reports exact inventory paths and hashes only.",
"Bridge candidates are path-based hypotheses, not decoded JNI declarations or observed runtime calls.", "Bridge candidates are path-based hypotheses, not decoded JNI declarations or observed runtime calls.",
]; ];
@@ -149,15 +129,10 @@ export const projectAndroidApplication = (
runtime_families: runtimeFamilies(all), runtime_families: runtimeFamilies(all),
bridge_candidates: bridgeProjection.candidates, bridge_candidates: bridgeProjection.candidates,
coverage: { coverage: {
status: status: inventory.complete
omittedComponents > 0 || bridgeProjection.omitted > 0 ? ("complete-within-inventory" as const)
? ("truncated" as const) : ("partial" as const),
: inventory.complete
? ("complete-within-inventory" as const)
: ("partial" as const),
inventory_complete: inventory.complete, inventory_complete: inventory.complete,
omitted_components: omittedComponents,
omitted_bridge_candidates: bridgeProjection.omitted,
}, },
limitations, limitations,
}; };
@@ -195,27 +170,6 @@ const classify = (all: readonly Component[]) => ({
), ),
}); });
const retain = <Groups extends Record<string, readonly Component[]>>(
groups: Groups,
maximum: number,
): { [Key in keyof Groups]: Component[] } => {
let remaining = maximum;
// SAFETY: the projection preserves every key from Groups and replaces only
// each readonly Component array with a bounded mutable Component array.
return Object.fromEntries(
Object.entries(groups).map(([key, values]) => {
const selected = [...values]
.sort((left, right) => compare(left.path, right.path))
.slice(0, remaining);
remaining -= selected.length;
return [key, selected];
}),
) as { [Key in keyof Groups]: Component[] };
};
const count = (groups: Record<string, readonly Component[]>): number =>
Object.values(groups).reduce((total, values) => total + values.length, 0);
const runtimeFamilies = (all: readonly Component[]) => { const runtimeFamilies = (all: readonly Component[]) => {
const paths = all.map(({ path }) => path.toLowerCase()); const paths = all.map(({ path }) => path.toLowerCase());
const families = new Set< const families = new Set<
@@ -247,21 +201,16 @@ const runtimeFamilies = (all: readonly Component[]) => {
const bridgeCandidates = ( const bridgeCandidates = (
managed: readonly Component[], managed: readonly Component[],
native: readonly Component[], native: readonly Component[],
maximum: number,
) => { ) => {
const projection = projectBoundedCartesian( const candidates = managed.flatMap((source) =>
managed, native.map((target) => ({
native,
maximum,
(source, target) => ({
managed_path: source.path, managed_path: source.path,
native_path: target.path, native_path: target.path,
basis: bridgeBasis(target.path), basis: bridgeBasis(target.path),
}), })),
); );
return { return {
candidates: projection.values, candidates,
omitted: projection.omitted,
}; };
}; };
+4 -4
View File
@@ -28,7 +28,7 @@ interface ParsedInventorySet {
readonly inventory: InventorySet; readonly inventory: InventorySet;
} }
/** Parse, merge, and verify a bounded set of inventory Evidence pages. */ /** Parse, merge, and verify inventory Evidence pages. */
export const parseArtifactInventoryEvidence = ( export const parseArtifactInventoryEvidence = (
input: unknown, input: unknown,
): ParsedInventorySet => ): ParsedInventorySet =>
@@ -91,11 +91,11 @@ const assembleInventorySet = (
readonly inventory: ArtifactInventoryResult; readonly inventory: ArtifactInventoryResult;
}[], }[],
): ParsedInventorySet => { ): ParsedInventorySet => {
if (pages.length === 0 || pages.length > 100) if (pages.length === 0)
throw new TypeError("Artifact comparison requires 1 to 100 Evidence pages"); throw new TypeError("Artifact inventory requires Evidence pages");
const first = pages[0]; const first = pages[0];
if (first === undefined) if (first === undefined)
throw new TypeError("Artifact comparison requires Evidence pages"); throw new TypeError("Artifact inventory requires Evidence pages");
const manifestJson = canonicalJson(first.inventory.manifest); const manifestJson = canonicalJson(first.inventory.manifest);
const nodes = new Map<string, ArtifactNode>(); const nodes = new Map<string, ArtifactNode>();
const occurrences = new Map<string, ArtifactOccurrence>(); const occurrences = new Map<string, ArtifactOccurrence>();
+2 -15
View File
@@ -14,14 +14,6 @@ import {
type WebCaptureDiff, type WebCaptureDiff,
} from "./webCaptureDiff.js"; } from "./webCaptureDiff.js";
const maxChangesSchema = z
.number()
.int()
.min(1)
.max(20_000)
.default(2_000)
.describe("Maximum normalized change records to retain.");
/** Caller-visible object schema for passive and scenario capture comparisons. */ /** Caller-visible object schema for passive and scenario capture comparisons. */
export const browserCaptureComparisonInputSchema = z export const browserCaptureComparisonInputSchema = z
.strictObject({ .strictObject({
@@ -41,7 +33,6 @@ export const browserCaptureComparisonInputSchema = z
.unwrap() .unwrap()
.optional() .optional()
.describe("Exact-literal normalization policy."), .describe("Exact-literal normalization policy."),
max_changes: maxChangesSchema,
}) })
.superRefine((input, context) => { .superRefine((input, context) => {
const passive = input.before !== undefined && input.after !== undefined; const passive = input.before !== undefined && input.after !== undefined;
@@ -70,7 +61,7 @@ export type BrowserCaptureComparisonInput = z.output<
>; >;
type ScenarioCapturePair = Pick< type ScenarioCapturePair = Pick<
BrowserCaptureComparisonInput, BrowserCaptureComparisonInput,
"before_scenario" | "after_scenario" | "normalization" | "max_changes" "before_scenario" | "after_scenario" | "normalization"
> & { > & {
before_scenario: NonNullable< before_scenario: NonNullable<
BrowserCaptureComparisonInput["before_scenario"] BrowserCaptureComparisonInput["before_scenario"]
@@ -107,9 +98,5 @@ const toPassiveComparison = (
): CompareWebCapturesInput => { ): CompareWebCapturesInput => {
if (input.before === undefined || input.after === undefined) if (input.before === undefined || input.after === undefined)
throw new Error("Validated passive comparison is missing a capture"); throw new Error("Validated passive comparison is missing a capture");
return { return { before: input.before, after: input.after };
before: input.before,
after: input.after,
max_changes: input.max_changes,
};
}; };
+2 -17
View File
@@ -102,30 +102,15 @@ export const listBrowserTargetsInputSchema = z.object({
...browserInput, ...browserInput,
}); });
/** Collection bounds for passive browser capture, applied internally. */ /** Per-value limits for optional text and JSON-shape capture. */
export const DEFAULT_BROWSER_INSPECTION_LIMITS = { export const DEFAULT_BROWSER_INSPECTION_LIMITS = {
max_frames: 200,
max_dom_nodes: 2_000,
max_ax_nodes: 2_000,
max_ax_text_field_bytes: 1_024, max_ax_text_field_bytes: 1_024,
max_total_ax_text_bytes: 64 * 1_024,
max_scripts: 200,
max_resources: 2_000,
max_workers: 500,
max_storage_keys: 1_000,
max_script_source_bytes: 1_024 * 1_024, max_script_source_bytes: 1_024 * 1_024,
max_total_script_source_bytes: 4 * 1_024 * 1_024,
max_network_events: 1_000,
max_console_events: 200,
max_console_text_field_bytes: 1_024, max_console_text_field_bytes: 1_024,
max_total_console_text_bytes: 64 * 1_024,
max_json_body_bytes: 1_024 * 1_024, max_json_body_bytes: 1_024 * 1_024,
max_total_json_body_bytes: 4 * 1_024 * 1_024,
max_json_shape_nodes: 5_000, max_json_shape_nodes: 5_000,
max_json_shape_depth: 20, max_json_shape_depth: 20,
max_websocket_events: 500,
max_websocket_shape_bytes: 64 * 1_024, max_websocket_shape_bytes: 64 * 1_024,
max_total_websocket_shape_bytes: 1_024 * 1_024,
}; };
const inspectWebPageInputFacts = { const inspectWebPageInputFacts = {
@@ -176,7 +161,7 @@ export const inspectWebPageToolInputSchema = z.union([
inspectWebPageWithSourceInputSchema, inspectWebPageWithSourceInputSchema,
]); ]);
/** Internal parsed input with REA's fixed collection bounds applied. */ /** Internal parsed input with per-value capture limits applied. */
export const inspectWebPageInputSchema = export const inspectWebPageInputSchema =
inspectWebPageToolInputSchema.transform((input) => ({ inspectWebPageToolInputSchema.transform((input) => ({
...input, ...input,
+12 -5
View File
@@ -109,15 +109,22 @@ describe("browser scenario comparison", () => {
expect.objectContaining({ artifact: "dom", status: "changed" }), expect.objectContaining({ artifact: "dom", status: "changed" }),
], ],
}); });
expect(compared.artifact_diffs).toEqual({ expect(compared.artifact_diffs).toEqual({ total: 2 });
total: 2,
retained: 2,
omitted: 0,
});
}); });
}); });
describe("browser scenario comparison validation", () => { describe("browser scenario comparison validation", () => {
it("does not accept caller limits that would omit difference records", () => {
const capture = scenarioCapture({});
expect(
compareBrowserScenariosInputSchema.safeParse({
before_scenario: capture,
after_scenario: capture,
max_changes: 1,
}).success,
).toBe(false);
});
it("reports missing aligned steps without claiming equality", () => { it("reports missing aligned steps without claiming equality", () => {
const before = scenarioCapture({}); const before = scenarioCapture({});
const after = scenarioCapture({ stepId: "open-profile" }); const after = scenarioCapture({ stepId: "open-profile" });
+1 -16
View File
@@ -76,7 +76,6 @@ export const compareBrowserScenarios = (
afterCapture: input.after_scenario, afterCapture: input.after_scenario,
alignment, alignment,
rules: normalization.rules, rules: normalization.rules,
maxChanges: input.max_changes,
}); });
const failures = [...alignment.failures, ...compared.failures]; const failures = [...alignment.failures, ...compared.failures];
const alignmentStatus = const alignmentStatus =
@@ -110,21 +109,12 @@ export const compareBrowserScenarios = (
steps: compared.steps, steps: compared.steps,
artifact_diffs: { artifact_diffs: {
total: compared.totalArtifactDiffs, total: compared.totalArtifactDiffs,
retained:
compared.totalArtifactDiffs -
Math.max(0, compared.totalArtifactDiffs - input.max_changes),
omitted: Math.max(0, compared.totalArtifactDiffs - input.max_changes),
}, },
limitations: [ limitations: [
"Steps align only by exact, unique step_id; missing, duplicate, or action-mismatched steps are reported and prevent an unchanged claim.", "Steps align only by exact, unique step_id; missing, duplicate, or action-mismatched steps are reported and prevent an unchanged claim.",
"Only declared literal normalization rules are applied, in canonical rule_id order, to already-redacted durable capture fields.", "Only declared literal normalization rules are applied, in canonical rule_id order, to already-redacted durable capture fields.",
"Elapsed time and event sequence/index values are excluded as volatile; screenshot bytes are compared by content digest.", "Elapsed time and event sequence/index values are excluded as volatile; screenshot bytes are compared by content digest.",
"A changed status proves an observed normalized difference. Missing or truncated evidence, incompatible capture context, or alignment failure prevents an unchanged claim.", "A changed status proves an observed normalized difference. Missing or truncated evidence, incompatible capture context, or alignment failure prevents an unchanged claim.",
...(compared.totalArtifactDiffs > input.max_changes
? [
`${String(compared.totalArtifactDiffs - input.max_changes)} artifact difference record(s) were omitted by max_changes.`,
]
: []),
], ],
}); });
}; };
@@ -178,13 +168,11 @@ interface CompareAlignedStepsInput {
readonly afterCapture: BrowserScenarioCapture; readonly afterCapture: BrowserScenarioCapture;
readonly alignment: ScenarioAlignment; readonly alignment: ScenarioAlignment;
readonly rules: readonly BrowserScenarioNormalizationRule[]; readonly rules: readonly BrowserScenarioNormalizationRule[];
readonly maxChanges: number;
} }
const compareAlignedSteps = ( const compareAlignedSteps = (
input: CompareAlignedStepsInput, input: CompareAlignedStepsInput,
): AlignedStepComparison => { ): AlignedStepComparison => {
const remaining = { value: input.maxChanges };
let totalArtifactDiffs = 0; let totalArtifactDiffs = 0;
const steps: BrowserScenarioDiff["steps"] = []; const steps: BrowserScenarioDiff["steps"] = [];
const failures: BrowserScenarioAlignmentFailure[] = []; const failures: BrowserScenarioAlignmentFailure[] = [];
@@ -209,8 +197,6 @@ const compareAlignedSteps = (
diff === null ? [] : [diff], diff === null ? [] : [diff],
); );
totalArtifactDiffs += reportable.length; totalArtifactDiffs += reportable.length;
const retained = reportable.slice(0, remaining.value);
remaining.value -= retained.length;
const statuses = artifacts.map(({ status }) => status); const statuses = artifacts.map(({ status }) => status);
steps.push({ steps.push({
step_id: stepId, step_id: stepId,
@@ -223,8 +209,7 @@ const compareAlignedSteps = (
: statuses.includes("unknown") : statuses.includes("unknown")
? "unknown" ? "unknown"
: "unchanged", : "unchanged",
artifact_diffs: retained, artifact_diffs: reportable,
omitted_artifact_diffs: reportable.length - retained.length,
}); });
} }
return { steps, failures, totalArtifactDiffs }; return { steps, failures, totalArtifactDiffs };
-10
View File
@@ -89,13 +89,6 @@ export const compareBrowserScenariosInputSchema = z.strictObject({
.describe( .describe(
"Bounded exact-literal rules committed in the comparison result.", "Bounded exact-literal rules committed in the comparison result.",
), ),
max_changes: z
.number()
.int()
.min(1)
.max(20_000)
.default(2_000)
.describe("Maximum changed or unknown artifact records to retain."),
}); });
/** Parsed browser scenario comparison input. */ /** Parsed browser scenario comparison input. */
export type CompareBrowserScenariosInput = z.infer< export type CompareBrowserScenariosInput = z.infer<
@@ -170,14 +163,11 @@ export const browserScenarioDiffSchema = z.strictObject({
after_action: z.string().min(1).max(64), after_action: z.string().min(1).max(64),
status: z.enum(["changed", "unchanged", "unknown"]), status: z.enum(["changed", "unchanged", "unknown"]),
artifact_diffs: z.array(artifactDiffSchema).max(8), artifact_diffs: z.array(artifactDiffSchema).max(8),
omitted_artifact_diffs: z.number().int().min(0).max(8),
}), }),
) )
.max(129), .max(129),
artifact_diffs: z.strictObject({ artifact_diffs: z.strictObject({
total: z.number().int().min(0).max(1_032), total: z.number().int().min(0).max(1_032),
retained: z.number().int().min(0).max(1_032),
omitted: z.number().int().min(0).max(1_032),
}), }),
limitations: z.array(z.string().min(1).max(2_048)).max(64), limitations: z.array(z.string().min(1).max(2_048)).max(64),
}); });
+7 -28
View File
@@ -25,7 +25,6 @@ const observationSchema = z.strictObject({
const runtimeObservationShape = { const runtimeObservationShape = {
family: runtimeFamilySchema, family: runtimeFamilySchema,
observations: z.array(observationSchema), observations: z.array(observationSchema),
omitted_observations: z.number().int().min(0),
}; };
const identifiedRuntimeSchema = z.discriminatedUnion("inspection", [ const identifiedRuntimeSchema = z.discriminatedUnion("inspection", [
z.strictObject({ z.strictObject({
@@ -50,12 +49,7 @@ const identifiedRuntimeSchema = z.discriminatedUnion("inspection", [
/** Authenticated artifact inventory pages to classify by runtime family. */ /** Authenticated artifact inventory pages to classify by runtime family. */
const runtimeIdentificationInputSchema = z.strictObject({ const runtimeIdentificationInputSchema = z.strictObject({
inventory_evidence: z.array(evidenceSchema).min(1).max(100), inventory_evidence: z.array(evidenceSchema).min(1),
limits: z
.strictObject({
max_observations: z.number().int().min(1).max(10_000).default(1_000),
})
.default({ max_observations: 1_000 }),
}); });
/** Provider-neutral runtime identification with explicit tooling availability. */ /** Provider-neutral runtime identification with explicit tooling availability. */
@@ -63,12 +57,11 @@ export const runtimeIdentificationResultSchema = z.strictObject({
identification_id: z.string().regex(/^rid_[a-f0-9]{64}$/u), identification_id: z.string().regex(/^rid_[a-f0-9]{64}$/u),
root_sha256: digestSchema, root_sha256: digestSchema,
root_format: z.string().min(1).max(100), root_format: z.string().min(1).max(100),
source_evidence_ids: z.array(evidenceIdSchema).min(1).max(100), source_evidence_ids: z.array(evidenceIdSchema).min(1),
runtimes: z.array(identifiedRuntimeSchema), runtimes: z.array(identifiedRuntimeSchema),
coverage: z.strictObject({ coverage: z.strictObject({
status: z.enum(["complete-within-inventory", "partial", "truncated"]), status: z.enum(["complete-within-inventory", "partial"]),
inventory_complete: z.boolean(), inventory_complete: z.boolean(),
omitted_observations: z.number().int().min(0),
}), }),
limitations: z.array(z.string().min(1).max(4_096)).max(100), limitations: z.array(z.string().min(1).max(4_096)).max(100),
}); });
@@ -165,22 +158,15 @@ export const identifyRuntimes = (
sha256: rootObservation.sha256, sha256: rootObservation.sha256,
format: rootObservation.format, format: rootObservation.format,
}); });
let remaining = parsed.limits.max_observations;
let omitted = 0;
const runtimes = [...grouped.entries()] const runtimes = [...grouped.entries()]
.sort(([left], [right]) => compare(left, right)) .sort(([left], [right]) => compare(left, right))
.map(([family, values]) => { .map(([family, values]) => {
const unique = deduplicate(values); const unique = deduplicate(values);
const observations = unique.slice(0, remaining);
remaining -= observations.length;
const omittedObservations = unique.length - observations.length;
omitted += omittedObservations;
const provider = providerFor(family, inventory.manifest.root_format); const provider = providerFor(family, inventory.manifest.root_format);
return { return {
family, family,
...runtimeInspection(provider), ...runtimeInspection(provider),
observations, observations: unique,
omitted_observations: omittedObservations,
}; };
}); });
const limitations = [ const limitations = [
@@ -189,9 +175,6 @@ export const identifyRuntimes = (
"Source inventory pages are incomplete; absent runtimes remain unknown.", "Source inventory pages are incomplete; absent runtimes remain unknown.",
] ]
: []), : []),
...(omitted > 0
? [`${String(omitted)} runtime observations were omitted by the limit.`]
: []),
"Runtime families are identified from exact artifact formats and paths; semantic claims require the named provider.", "Runtime families are identified from exact artifact formats and paths; semantic claims require the named provider.",
]; ];
const withoutId = { const withoutId = {
@@ -202,14 +185,10 @@ export const identifyRuntimes = (
.sort(compare), .sort(compare),
runtimes, runtimes,
coverage: { coverage: {
status: status: inventory.complete
omitted > 0 ? ("complete-within-inventory" as const)
? ("truncated" as const) : ("partial" as const),
: inventory.complete
? ("complete-within-inventory" as const)
: ("partial" as const),
inventory_complete: inventory.complete, inventory_complete: inventory.complete,
omitted_observations: omitted,
}, },
limitations, limitations,
}; };
+32 -43
View File
@@ -26,10 +26,9 @@ type Change = WebCaptureChange;
export const compareWebCaptures = ( export const compareWebCaptures = (
input: CompareWebCapturesInput, input: CompareWebCapturesInput,
): WebCaptureDiff => { ): WebCaptureDiff => {
const remaining = { value: input.max_changes };
const before = input.before.inspection; const before = input.before.inspection;
const after = input.after.inspection; const after = input.after.inspection;
const dimensions = compareWebCaptureDimensions(input, remaining); const dimensions = compareWebCaptureDimensions(input);
const statuses = Object.values(dimensions).map(({ status }) => status); const statuses = Object.values(dimensions).map(({ status }) => status);
return webCaptureDiffSchema.parse({ return webCaptureDiffSchema.parse({
overall_status: statuses.includes("changed") overall_status: statuses.includes("changed")
@@ -75,52 +74,44 @@ const compareIdentities = (
); );
}; };
const compareDimension = const compareDimension = (
(remaining: { value: number }) => before: ReadonlyMap<string, string>,
( after: ReadonlyMap<string, string>,
before: ReadonlyMap<string, string>, complete: boolean,
after: ReadonlyMap<string, string>, reason: string,
complete: boolean, ): Dimension => {
reason: string, const all = compareIdentities(before, after);
): Dimension => { if (all.length > 0)
const all = compareIdentities(before, after); return {
const retained = all.slice(0, remaining.value); status: "changed",
remaining.value -= retained.length; total_changes: all.length,
if (all.length > 0) changes: all,
return { reason: null,
status: "changed", };
total_changes: all.length, return complete
changes: retained, ? {
omitted_changes: all.length - retained.length, status: "unchanged",
total_changes: 0,
changes: [],
reason: null, reason: null,
}
: {
status: "unknown",
total_changes: 0,
changes: [],
reason,
}; };
return complete };
? {
status: "unchanged",
total_changes: 0,
changes: [],
omitted_changes: 0,
reason: null,
}
: {
status: "unknown",
total_changes: 0,
changes: [],
omitted_changes: 0,
reason,
};
};
const accessibilityDimension = ( const accessibilityDimension = (
before: WebPageInspection, before: WebPageInspection,
after: WebPageInspection, after: WebPageInspection,
remaining: { value: number },
): Dimension => { ): Dimension => {
const beforeAccess = accessibilityComparable(before); const beforeAccess = accessibilityComparable(before);
const afterAccess = accessibilityComparable(after); const afterAccess = accessibilityComparable(after);
const textComparable = beforeAccess.text && afterAccess.text; const textComparable = beforeAccess.text && afterAccess.text;
const nodesComparable = beforeAccess.nodes && afterAccess.nodes; const nodesComparable = beforeAccess.nodes && afterAccess.nodes;
return compareDimension(remaining)( return compareDimension(
singleton( singleton(
"accessibility_tree", "accessibility_tree",
digest( digest(
@@ -149,7 +140,6 @@ const accessibilityDimension = (
const storageDimension = ( const storageDimension = (
before: WebPageInspection, before: WebPageInspection,
after: WebPageInspection, after: WebPageInspection,
remaining: { value: number },
): Dimension => { ): Dimension => {
const keysComplete = const keysComplete =
storageKeysComparable(before) && storageKeysComparable(after); storageKeysComparable(before) && storageKeysComparable(after);
@@ -162,7 +152,7 @@ const storageDimension = (
before.storage, before.storage,
after.storage, after.storage,
); );
return compareDimension(remaining)( return compareDimension(
storageMap( storageMap(
before.storage, before.storage,
keysComplete, keysComplete,
@@ -182,11 +172,10 @@ const storageDimension = (
const compareWebCaptureDimensions = ( const compareWebCaptureDimensions = (
input: CompareWebCapturesInput, input: CompareWebCapturesInput,
remaining: { value: number },
): WebCaptureDiff["dimensions"] => { ): WebCaptureDiff["dimensions"] => {
const before = input.before.inspection; const before = input.before.inspection;
const after = input.after.inspection; const after = input.after.inspection;
const dimension = compareDimension(remaining); const dimension = compareDimension;
return { return {
dom_structure: dimension( dom_structure: dimension(
singleton("document", digest(domProjection(before))), singleton("document", digest(domProjection(before))),
@@ -235,8 +224,8 @@ const compareWebCaptureDimensions = (
webMcpComplete(input.before.webmcp) && webMcpComplete(input.after.webmcp), webMcpComplete(input.before.webmcp) && webMcpComplete(input.after.webmcp),
"WebMCP discovery was unavailable or incomplete in at least one capture.", "WebMCP discovery was unavailable or incomplete in at least one capture.",
), ),
accessibility: accessibilityDimension(before, after, remaining), accessibility: accessibilityDimension(before, after),
storage: storageDimension(before, after, remaining), storage: storageDimension(before, after),
}; };
}; };
+3 -10
View File
@@ -10,10 +10,9 @@ export const captureSnapshotSchema = z.object({
}); });
/** Input for deterministic comparison of two normalized web captures. */ /** Input for deterministic comparison of two normalized web captures. */
export const compareWebCapturesInputSchema = z.object({ export const compareWebCapturesInputSchema = z.strictObject({
before: captureSnapshotSchema, before: captureSnapshotSchema,
after: captureSnapshotSchema, after: captureSnapshotSchema,
max_changes: z.number().int().min(1).max(20_000).default(2_000),
}); });
const changeSchema = z.object({ const changeSchema = z.object({
@@ -23,7 +22,6 @@ const changeSchema = z.object({
const emptyDimensionShape = { const emptyDimensionShape = {
total_changes: z.literal(0), total_changes: z.literal(0),
changes: z.tuple([]), changes: z.tuple([]),
omitted_changes: z.literal(0),
}; };
const dimensionSchema = z.union([ const dimensionSchema = z.union([
z z
@@ -31,17 +29,13 @@ const dimensionSchema = z.union([
status: z.literal("changed"), status: z.literal("changed"),
total_changes: z.number().int().min(1), total_changes: z.number().int().min(1),
changes: z.array(changeSchema), changes: z.array(changeSchema),
omitted_changes: z.number().int().min(0),
reason: z.null(), reason: z.null(),
}) })
.superRefine((dimension, context) => { .superRefine((dimension, context) => {
if ( if (dimension.changes.length !== dimension.total_changes)
dimension.changes.length + dimension.omitted_changes !==
dimension.total_changes
)
context.addIssue({ context.addIssue({
code: "custom", code: "custom",
message: "Retained and omitted changes must equal total changes", message: "Inline changes must equal total changes",
path: ["total_changes"], path: ["total_changes"],
}); });
}), }),
@@ -60,7 +54,6 @@ const legacyUnknownDimension: z.input<typeof dimensionSchema> = {
status: "unknown", status: "unknown",
total_changes: 0, total_changes: 0,
changes: [], changes: [],
omitted_changes: 0,
reason: "Dimension was not recorded by this version 1 capture diff.", reason: "Dimension was not recorded by this version 1 capture diff.",
}; };
+1 -10
View File
@@ -8,20 +8,11 @@ import {
import { jsonShapeSchema } from "./jsonShape.js"; import { jsonShapeSchema } from "./jsonShape.js";
/** Input for passive discovery of page-registered WebMCP tools. */ /** Input for passive discovery of page-registered WebMCP tools. */
export const discoverWebMcpToolsInputSchema = z.object({ export const discoverWebMcpToolsInputSchema = z.strictObject({
cdp_endpoint: browserEndpointSchema, cdp_endpoint: browserEndpointSchema,
allowed_origins: browserAllowedOriginsSchema, allowed_origins: browserAllowedOriginsSchema,
target_id: z.string().trim().min(1).max(256), target_id: z.string().trim().min(1).max(256),
observation_ms: z.number().int().min(0).max(10_000).default(100), observation_ms: z.number().int().min(0).max(10_000).default(100),
max_tools: z.number().int().min(1).max(5_000).default(500),
max_schema_bytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(256 * 1_024),
max_schema_nodes: z.number().int().min(1).max(100_000).default(5_000),
max_schema_depth: z.number().int().min(1).max(100).default(20),
}); });
export type DiscoverWebMcpToolsInput = z.infer< export type DiscoverWebMcpToolsInput = z.infer<
typeof discoverWebMcpToolsInputSchema typeof discoverWebMcpToolsInputSchema
@@ -9,6 +9,17 @@ import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
import { describeBrowser, trackBrowser } from "./cdpBrowserProvider.support.js"; import { describeBrowser, trackBrowser } from "./cdpBrowserProvider.support.js";
describeBrowser("CdpBrowserProvider: document script 1", () => { describeBrowser("CdpBrowserProvider: document script 1", () => {
it("rejects removed caller-facing WebMCP item and schema limits", () => {
expect(
discoverWebMcpToolsInputSchema.safeParse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://example.test"],
target_id: "target",
max_tools: 1,
}).success,
).toBe(false);
});
it("captures bounded accessibility text only after independent approval", async () => { it("captures bounded accessibility text only after independent approval", async () => {
const browser = await startFakeCdpBrowser(); const browser = await startFakeCdpBrowser();
trackBrowser(browser); trackBrowser(browser);
@@ -24,7 +35,6 @@ describeBrowser("CdpBrowserProvider: document script 1", () => {
limits: { limits: {
...request.limits, ...request.limits,
max_ax_text_field_bytes: 6, max_ax_text_field_bytes: 6,
max_total_ax_text_bytes: 6,
}, },
}); });
@@ -32,7 +42,7 @@ describeBrowser("CdpBrowserProvider: document script 1", () => {
expect(result.value.accessibility).toMatchObject({ expect(result.value.accessibility).toMatchObject({
text_capture: { text_capture: {
status: "truncated", status: "truncated",
retained_bytes: 6, retained_bytes: 12,
truncated_fields: 2, truncated_fields: 2,
}, },
nodes: [expect.objectContaining({ name: "Submit" })], nodes: [expect.objectContaining({ name: "Submit" })],
@@ -223,7 +233,7 @@ describeBrowser("CdpBrowserProvider: document script 2", () => {
); );
}); });
it("bounds WebMCP registration replay and reports dropped declarations", async () => { it("returns every WebMCP registration in the replay", async () => {
const browser = await startFakeCdpBrowser({ const browser = await startFakeCdpBrowser({
webMcpTools: true, webMcpTools: true,
extraCollections: true, extraCollections: true,
@@ -235,19 +245,46 @@ describeBrowser("CdpBrowserProvider: document script 2", () => {
allowed_origins: [browser.allowedOrigin], allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page", target_id: "allowed-page",
observation_ms: 0, observation_ms: 0,
max_tools: 1,
}), }),
); );
if (!result.ok) throw result.error; if (!result.ok) throw result.error;
expect(result.value.tools).toMatchObject({ expect(result.value.tools).toMatchObject({
total: 2, total: 2,
items: [expect.any(Object)], items: [expect.any(Object), expect.any(Object)],
}); });
expect(result.value.completeness).toMatchObject({ expect(result.value.completeness.truncated_sections).not.toContain(
status: "truncated", "webmcp_tools",
dropped_events: { webmcp_tools: 1 }, );
});
it("retains registrations from authorized frames beyond the former frame cap", async () => {
const browser = await startFakeCdpBrowser({
webMcpTools: true,
webMcpFrameCount: 1_001,
}); });
trackBrowser(browser);
const result = await new CdpBrowserProvider().discoverWebMcpTools(
discoverWebMcpToolsInputSchema.parse({
cdp_endpoint: browser.endpoint,
allowed_origins: [browser.allowedOrigin],
target_id: "allowed-page",
observation_ms: 0,
}),
);
if (!result.ok) throw result.error;
expect(result.value.tools.items).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: "beyond_frame_limit_tool",
frame_id: "webmcp-frame-1000",
}),
]),
);
expect(result.value.completeness.truncated_sections).not.toContain(
"webmcp_tools",
);
}); });
}); });
@@ -332,7 +369,7 @@ describeBrowser("CdpBrowserProvider: document script 3", () => {
}); });
}); });
it("reports configured truncation without over-reading DOM or script source", async () => { it("returns every DOM node and applies the per-script source limit", async () => {
const browser = await startFakeCdpBrowser(); const browser = await startFakeCdpBrowser();
trackBrowser(browser); trackBrowser(browser);
const request = inspectWebPageInputSchema.parse({ const request = inspectWebPageInputSchema.parse({
@@ -346,27 +383,15 @@ describeBrowser("CdpBrowserProvider: document script 3", () => {
...request, ...request,
limits: { limits: {
...request.limits, ...request.limits,
max_frames: 1,
max_dom_nodes: 1,
max_ax_nodes: 1,
max_scripts: 1,
max_resources: 1,
max_workers: 1,
max_storage_keys: 1,
max_script_source_bytes: 10, max_script_source_bytes: 10,
max_total_script_source_bytes: 10,
max_network_events: 1,
max_console_events: 1,
max_websocket_events: 1,
}, },
}); });
if (!result.ok) throw result.error; if (!result.ok) throw result.error;
expect(result.value.completeness).toMatchObject({ expect(result.value.completeness).toMatchObject({
status: "truncated", truncated_sections: ["script_sources"],
truncated_sections: ["dom", "script_sources"],
}); });
expect(result.value.dom).toMatchObject({ total_nodes: 2 }); expect(result.value.dom).toMatchObject({ total_nodes: 2 });
expect(result.value.dom.nodes).toHaveLength(1); expect(result.value.dom.nodes).toHaveLength(2);
expect(result.value.scripts.items[0]?.source).toMatchObject({ expect(result.value.scripts.items[0]?.source).toMatchObject({
included: false, included: false,
reason: "declared script length exceeds per-script limit", reason: "declared script length exceeds per-script limit",
@@ -378,7 +403,7 @@ describeBrowser("CdpBrowserProvider: document script 3", () => {
}); });
describeBrowser("CdpBrowserProvider: document script 4", () => { describeBrowser("CdpBrowserProvider: document script 4", () => {
it("bounds frames, resources, workers, accessibility, and storage inventories", async () => { it("returns every frame, resource, worker, accessibility, and storage inventory item", async () => {
const browser = await startFakeCdpBrowser({ extraCollections: true }); const browser = await startFakeCdpBrowser({ extraCollections: true });
trackBrowser(browser); trackBrowser(browser);
const request = inspectWebPageInputSchema.parse({ const request = inspectWebPageInputSchema.parse({
@@ -388,44 +413,18 @@ describeBrowser("CdpBrowserProvider: document script 4", () => {
observation_ms: 0, observation_ms: 0,
include_storage_keys: true, include_storage_keys: true,
}); });
const result = await new CdpBrowserProvider().inspectPage({ const result = await new CdpBrowserProvider().inspectPage(request);
...request,
limits: {
...request.limits,
max_frames: 1,
max_dom_nodes: 2,
max_ax_nodes: 1,
max_scripts: 1,
max_resources: 1,
max_workers: 1,
max_storage_keys: 1,
max_script_source_bytes: 10,
max_total_script_source_bytes: 10,
max_network_events: 1,
max_console_events: 1,
max_websocket_events: 1,
},
});
if (!result.ok) throw result.error; if (!result.ok) throw result.error;
expect(result.value.completeness).toMatchObject({ expect(result.value.completeness.truncated_sections).toEqual([]);
status: "truncated", expect(result.value.frames.length).toBeGreaterThan(1);
truncated_sections: [ expect(result.value.resources.length).toBeGreaterThan(1);
"accessibility", expect(result.value.workers.length).toBeGreaterThan(1);
"frames", expect(result.value.accessibility).toMatchObject({ total_nodes: 4 });
"resources", expect(result.value.accessibility.nodes).toHaveLength(4);
"storage_keys", expect(result.value.storage.local_storage_keys.length).toBeGreaterThan(1);
"workers", expect(result.value.storage.session_storage_keys.length).toBeGreaterThan(1);
], expect(result.value.storage.indexed_db_names).toHaveLength(2);
}); expect(result.value.storage.cache_names).toHaveLength(2);
expect(result.value.frames).toHaveLength(1);
expect(result.value.resources).toHaveLength(1);
expect(result.value.workers).toHaveLength(1);
expect(result.value.accessibility).toMatchObject({ total_nodes: 2 });
expect(result.value.accessibility.nodes).toHaveLength(1);
expect(result.value.storage.local_storage_keys).toHaveLength(1);
expect(result.value.storage.session_storage_keys).toHaveLength(1);
expect(result.value.storage.indexed_db_names).toHaveLength(1);
expect(result.value.storage.cache_names).toHaveLength(1);
expect(result.value.storage.content_fingerprints).toEqual([]); expect(result.value.storage.content_fingerprints).toEqual([]);
}); });
}); });
@@ -248,7 +248,7 @@ describeBrowser("CdpBrowserProvider: sensitive data 2", () => {
}); });
describeBrowser("CdpBrowserProvider: sensitive data 3", () => { describeBrowser("CdpBrowserProvider: sensitive data 3", () => {
it("reports independent sensitive-capture truncation at aggregate byte limits", async () => { it("keeps per-value text, body, and WebSocket shape validation", async () => {
const browser = await startFakeCdpBrowser({ sensitiveShapes: true }); const browser = await startFakeCdpBrowser({ sensitiveShapes: true });
trackBrowser(browser); trackBrowser(browser);
const request = inspectWebPageInputSchema.parse({ const request = inspectWebPageInputSchema.parse({
@@ -265,20 +265,20 @@ describeBrowser("CdpBrowserProvider: sensitive data 3", () => {
limits: { limits: {
...request.limits, ...request.limits,
max_console_text_field_bytes: 5, max_console_text_field_bytes: 5,
max_total_console_text_bytes: 5,
max_json_body_bytes: 10, max_json_body_bytes: 10,
max_total_json_body_bytes: 10,
max_websocket_shape_bytes: 5, max_websocket_shape_bytes: 5,
max_total_websocket_shape_bytes: 5,
}, },
}); });
if (!result.ok) throw result.error; if (!result.ok) throw result.error;
expect(result.value.console.events[0]?.text_capture).toEqual({ expect(result.value.console.events[0]?.text_capture).toEqual({
status: "truncated", status: "truncated",
values: [{ argument_index: 0, type: "string", text: "autho" }], values: [
retained_bytes: 5, { argument_index: 0, type: "string", text: "autho" },
truncated_values: 2, { argument_index: 1, type: "number", text: "42" },
],
retained_bytes: 7,
truncated_values: 1,
}); });
expect(result.value.network.requests[0]?.body_shapes.status).toBe( expect(result.value.network.requests[0]?.body_shapes.status).toBe(
"truncated", "truncated",
@@ -22,6 +22,16 @@ afterEach(async () => {
}); });
describe("web capture diff", () => { describe("web capture diff", () => {
it("rejects caller limits that would omit difference records", () => {
expect(
compareWebCapturesInputSchema.safeParse({
before: {},
after: {},
max_changes: 1,
}).success,
).toBe(false);
});
it("reports stable observed changes while preserving unknown dimensions", async () => { it("reports stable observed changes while preserving unknown dimensions", async () => {
const browser = await startFakeCdpBrowser(); const browser = await startFakeCdpBrowser();
browsers.push(browser); browsers.push(browser);
@@ -63,7 +63,6 @@ describe("runtime identification", () => {
coverage: { coverage: {
status: "complete-within-inventory", status: "complete-within-inventory",
inventory_complete: true, inventory_complete: true,
omitted_observations: 0,
}, },
}); });
expect(first.runtimes.map(({ family }) => family)).toEqual([ expect(first.runtimes.map(({ family }) => family)).toEqual([
@@ -115,25 +114,29 @@ describe("runtime identification", () => {
).toBe(false); ).toBe(false);
}); });
it("reports bounded runtime-observation truncation", async () => { it("accepts more than 100 supplied inventory Evidence records", async () => {
const root = await createTestTempDirectory("rea-runtime-limit-"); const root = await createTestTempDirectory("rea-runtime-pages-");
const path = join(root, "Fixture.apk"); const path = join(root, "Fixture.apk");
const writer = new ZipWriter(new Uint8ArrayWriter()); const writer = new ZipWriter(new Uint8ArrayWriter());
await writer.add("one.js", new TextReader("one")); await writer.add("one.js", new TextReader("one"));
await writer.add("two.js", new TextReader("two"));
await writeFile(path, await writer.close()); await writeFile(path, await writer.close());
const inventory = parseEvidence( const inventory = parseEvidence(
await runProviderAnalysis(path, "inventory_artifact", {}), await runProviderAnalysis(path, "inventory_artifact", {}),
); );
const result = identifyRuntimes({ const result = identifyRuntimes({
inventory_evidence: [inventory], inventory_evidence: Array.from({ length: 101 }, () => inventory),
limits: { max_observations: 1 },
}); });
expect(result.coverage).toMatchObject({
status: "truncated", expect(result.source_evidence_ids).toHaveLength(101);
omitted_observations: expect.any(Number), expect(result.coverage.status).toBe("complete-within-inventory");
}); expect(result.runtimes).toContainEqual(
expect(result.coverage.omitted_observations).toBeGreaterThan(0); expect.objectContaining({
family: "javascript",
observations: expect.arrayContaining([
expect.objectContaining({ path: "one.js" }),
]),
}),
);
}); });
it("does not route nested DEX content to the APK-only provider", async () => { it("does not route nested DEX content to the APK-only provider", async () => {
@@ -46,11 +46,9 @@ describe("Android application projection", () => {
); );
const first = projectAndroidApplicationEvidence({ const first = projectAndroidApplicationEvidence({
inventory_evidence: [inventory], inventory_evidence: [inventory],
limits: { max_components: 100 },
}); });
const second = projectAndroidApplicationEvidence({ const second = projectAndroidApplicationEvidence({
inventory_evidence: [inventory], inventory_evidence: [inventory],
limits: { max_components: 100 },
}); });
expect(first.ok).toBe(true); expect(first.ok).toBe(true);
expect(second.ok).toBe(true); expect(second.ok).toBe(true);
@@ -67,7 +65,6 @@ describe("Android application projection", () => {
coverage: { coverage: {
status: "complete-within-inventory", status: "complete-within-inventory",
inventory_complete: true, inventory_complete: true,
omitted_components: 0,
}, },
}); });
expect(left.components.manifests).toHaveLength(1); expect(left.components.manifests).toHaveLength(1);
@@ -105,4 +102,34 @@ describe("Android application projection", () => {
error: { _tag: "AnalysisInputError" }, error: { _tag: "AnalysisInputError" },
}); });
}); });
it("returns every component and bridge candidate from the inventory", async () => {
const root = await createTestTempDirectory("rea-android-complete-");
const path = join(root, "Fixture.apk");
const writer = new ZipWriter(new Uint8ArrayWriter());
const dex = Uint8Array.from([0x64, 0x65, 0x78, 0x0a, 0x30, 0x33, 0x35, 0]);
for (let index = 0; index < 1_001; index += 1)
await writer.add(
`classes${String(index).padStart(4, "0")}.dex`,
new Uint8ArrayReader(dex),
);
await writer.add("lib/arm64-v8a/libnative.so", new TextReader("native"));
await writeFile(path, await writer.close());
const inventory = parseEvidence(
await runProviderAnalysis(path, "inventory_artifact", {}),
);
const result = projectAndroidApplicationEvidence({
inventory_evidence: [inventory],
});
expect(result.ok).toBe(true);
if (!result.ok) return;
const projection = androidApplicationProjectionResultSchema.parse(
result.value.normalized_result,
);
expect(projection.components.dex).toHaveLength(1_001);
expect(projection.bridge_candidates).toHaveLength(1_001);
expect(projection.coverage.status).toBe("complete-within-inventory");
});
}); });
+29 -1
View File
@@ -11,6 +11,17 @@ export const emitEvents = (
emitDebuggerEvents(socket, command, port, options); emitDebuggerEvents(socket, command, port, options);
emitNetworkEvents(socket, command, port, options); emitNetworkEvents(socket, command, port, options);
emitRuntimeEvents(socket, command, port, options); emitRuntimeEvents(socket, command, port, options);
emitWebMcpEvents(socket, command, port, options);
emitSessionTimeline(socket, command, port, options);
emitCaptureNavigation(socket, command, options);
};
const emitWebMcpEvents = (
socket: WebSocket,
command: FakeCdpCommand,
port: number,
options: FakeOptions,
): void => {
if (command.method === "WebMCP.enable" && options.webMcpTools === true) { if (command.method === "WebMCP.enable" && options.webMcpTools === true) {
event(socket, "WebMCP.toolsAdded", command.sessionId, { event(socket, "WebMCP.toolsAdded", command.sessionId, {
tools: [ tools: [
@@ -60,6 +71,16 @@ export const emitEvents = (
}, },
] ]
: []), : []),
...(options.webMcpFrameCount !== undefined &&
options.webMcpFrameCount > 0
? [
{
name: "beyond_frame_limit_tool",
description: "Registered in an in-scope frame after index 999",
frameId: `webmcp-frame-${String(options.webMcpFrameCount - 1)}`,
},
]
: []),
...(options.webMcpChildLeavesScope === true ...(options.webMcpChildLeavesScope === true
? [ ? [
{ {
@@ -90,7 +111,14 @@ export const emitEvents = (
}); });
} }
} }
emitCaptureNavigation(socket, command, options); };
const emitSessionTimeline = (
socket: WebSocket,
command: FakeCdpCommand,
port: number,
options: FakeOptions,
): void => {
if ( if (
command.method === "Page.setLifecycleEventsEnabled" && command.method === "Page.setLifecycleEventsEnabled" &&
options.sessionTimeline !== undefined options.sessionTimeline !== undefined
+10
View File
@@ -66,6 +66,7 @@ const frameTreeResult = (
? ":" ? ":"
: (options.electronFileUrl ?? options.attachedFrameUrl), : (options.electronFileUrl ?? options.attachedFrameUrl),
options.extraCollections === true, options.extraCollections === true,
options.webMcpFrameCount,
); );
const resourceTreeResult = (port: number, options: FakeOptions) => const resourceTreeResult = (port: number, options: FakeOptions) =>
@@ -214,6 +215,7 @@ const frameTree = (
port: number, port: number,
overrideUrl?: string, overrideUrl?: string,
extraCollections = false, extraCollections = false,
webMcpFrameCount = 0,
): Readonly<Record<string, unknown>> => ({ ): Readonly<Record<string, unknown>> => ({
frameTree: { frameTree: {
frame: { frame: {
@@ -236,6 +238,14 @@ const frameTree = (
}, },
] ]
: []), : []),
...Array.from({ length: webMcpFrameCount }, (_value, index) => ({
frame: {
id: `webmcp-frame-${String(index)}`,
parentId: "frame-main",
loaderId: `loader-webmcp-${String(index)}`,
url: `http://127.0.0.1:${String(port)}/webmcp-frame/${String(index)}`,
},
})),
{ {
frame: { frame: {
id: "frame-private", id: "frame-private",
+1
View File
@@ -57,6 +57,7 @@ export interface FakeOptions {
readonly sensitiveShapes?: boolean; readonly sensitiveShapes?: boolean;
readonly invalidResponseBodyBase64?: boolean; readonly invalidResponseBodyBase64?: boolean;
readonly webMcpTools?: boolean; readonly webMcpTools?: boolean;
readonly webMcpFrameCount?: number;
readonly webMcpChildLeavesScope?: boolean; readonly webMcpChildLeavesScope?: boolean;
readonly electronFileUrl?: string; readonly electronFileUrl?: string;
readonly duplicateElectronInventory?: boolean; readonly duplicateElectronInventory?: boolean;