Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(browser)!: preserve unknown console timestamps

morluto690fc518540b
diff --git a/docs/browser-observation.md b/docs/browser-observation.md
index 5439244bf..64823a472 100644
--- a/docs/browser-observation.md
+++ b/docs/browser-observation.md
@@ -345,7 +345,7 @@ REA retains local observation data and removes only URL userinfo credentials, ex
WebSocket stream inventories and connection counts likewise require complete
WebSocket coverage. Redirect resource types use only shared known metadata:
an omitted type leaves equality unknown rather than proving a modification.
-- Console observations with a stack source retain call type, argument types, timestamp, and source location. When console text capture is selected, already-delivered primitive values are retained verbatim; objects, getters, and remote properties are never expanded.
+- Console observations with a stack source retain call type, argument types, the CDP `Runtime.Timestamp` in milliseconds since Unix epoch, and source location. An absent or invalid timestamp is `null` and makes `console_events` incomplete; an observed zero remains zero. This epoch-millisecond clock is separate from the monotonic-second timestamps in `observe-web-session`. When console text capture is selected, already-delivered primitive values are retained verbatim; objects, getters, and remote properties are never expanded.
- WebSocket observations retain direction, opcode, and payload byte length. When shape capture is selected, text frames are classified as text or complete value-free JSON shape; binary bytes, hashes, prefixes, and raw frames are never retained.
- Storage observations always redact values. Key names, IndexedDB names, and cache names require `include_storage_keys`; stable content fingerprints require the additional `include_storage_fingerprints` selection. Cookie fingerprints cover cookies applicable to the current main-frame URL, including its path, rather than every cookie path on that origin. Other storage remains scoped to the origin. Cache fingerprints hash all decoded response bytes, including bodies above 64 KiB, subject to the [64 MiB per-message CDP limit](#request-boundary). Missing or undecodable cache bodies and partial IndexedDB remote objects make fingerprint coverage incomplete, so identical observations remain `unknown`.
- Unavailable or malformed storage inventories mark `storage_keys` unavailable, even without fingerprints. Comparisons suppress inferred key additions and removals while preserving modifications proven by complete fingerprints on both sides. Previously exported captures are not retroactively corrected; recapture them to obtain these coverage markers.
diff --git a/src/browser/CdpCaptureEventHandlers.ts b/src/browser/CdpCaptureEventHandlers.ts
index 0f16bd0a7..5d0792da5 100644
--- a/src/browser/CdpCaptureEventHandlers.ts
+++ b/src/browser/CdpCaptureEventHandlers.ts
@@ -452,9 +452,12 @@ export const handleConsoleAPICalled = (
return;
}
const arguments_ = recordsValue(params.args);
+ const timestamp = numberValue(params.timestamp);
+ if (timestamp === undefined)
+ state.completeness.exclude("console_events", "invalid_protocol_value");
state.console.push({
type: cdpStringValue(params.type) ?? "unknown",
- timestamp: numberValue(params.timestamp) ?? 0,
+ timestamp: timestamp ?? null,
argument_types: arguments_.map(
(argument) => cdpStringValue(argument.type) ?? "unknown",
),
diff --git a/src/browser/CdpCaptureEvents.consoleTimestamp.test.ts b/src/browser/CdpCaptureEvents.consoleTimestamp.test.ts
new file mode 100644
index 000000000..fd5604d69
--- /dev/null
+++ b/src/browser/CdpCaptureEvents.consoleTimestamp.test.ts
@@ -0,0 +1,68 @@
+import { expect, it } from "vitest";
+
+import { CdpCaptureEvents } from "./CdpCaptureEvents.js";
+import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
+import { webPageInspectionSchema } from "../domain/browserObservationSchemas.js";
+
+const origin = "http://127.0.0.1:43127";
+
+const captureWithConsoleTimestamp = (timestamp: unknown): CdpCaptureEvents => {
+ const capture = new CdpCaptureEvents(
+ inspectWebPageInputSchema.parse({
+ cdp_endpoint: origin,
+ allowed_origins: [origin],
+ target_id: "allowed-page",
+ }),
+ new Set([origin]),
+ );
+ const params: Record<string, unknown> = {
+ type: "log",
+ args: [],
+ stackTrace: {
+ callFrames: [{ url: `${origin}/app.js`, lineNumber: 0, columnNumber: 0 }],
+ },
+ };
+ if (timestamp !== undefined) params.timestamp = timestamp;
+ capture.ingest({ method: "Runtime.consoleAPICalled", params });
+ return capture;
+};
+
+it.each([
+ ["an omitted timestamp", undefined],
+ ["a non-finite timestamp", Number.NaN],
+ ["a string timestamp", "123"],
+ ["an explicit null timestamp", null],
+])(
+ "retains %s as unknown and marks console coverage invalid",
+ (_label, timestamp) => {
+ const capture = captureWithConsoleTimestamp(timestamp);
+ const event = capture.console[0];
+
+ expect(event?.timestamp).toBeNull();
+ expect(capture.completeness.snapshot().excluded).toContainEqual({
+ section: "console_events",
+ reason: "invalid_protocol_value",
+ count: 1,
+ });
+ expect(
+ webPageInspectionSchema.shape.console.shape.events.element.safeParse(
+ event,
+ ).success,
+ ).toBe(true);
+ },
+);
+
+it.each([
+ ["Unix epoch", 0],
+ ["pre-epoch", -1],
+])("preserves a finite %s console timestamp", (_label, timestamp) => {
+ const capture = captureWithConsoleTimestamp(timestamp);
+
+ expect(capture.console[0]?.timestamp).toBe(timestamp);
+ expect(capture.completeness.snapshot().excluded).not.toContainEqual(
+ expect.objectContaining({
+ section: "console_events",
+ reason: "invalid_protocol_value",
+ }),
+ );
+});
diff --git a/src/domain/browserObservationSchemas.ts b/src/domain/browserObservationSchemas.ts
index e79a90328..53834085a 100644
--- a/src/domain/browserObservationSchemas.ts
+++ b/src/domain/browserObservationSchemas.ts
@@ -161,7 +161,12 @@ const browserNetworkRequestSchema = z.object({
});
const browserConsoleEventSchema = z.object({
type: z.string(),
- timestamp: z.number(),
+ timestamp: z
+ .number()
+ .nullable()
+ .describe(
+ "CDP Runtime.Timestamp in milliseconds since Unix epoch; null when the producer value is absent or invalid.",
+ ),
argument_types: z.array(z.string()),
url: z.string().nullable(),
line: z.number().int().min(0).nullable(),
diff --git a/tests/acceptance/browser/consoleTimestampCliMcp.test.ts b/tests/acceptance/browser/consoleTimestampCliMcp.test.ts
new file mode 100644
index 000000000..ea32cacf2
--- /dev/null
+++ b/tests/acceptance/browser/consoleTimestampCliMcp.test.ts
@@ -0,0 +1,155 @@
+import { expect } from "vitest";
+
+import { toolContract } from "../../../src/contracts/toolContracts.js";
+import { parseEvidence } from "../../../src/domain/evidence.js";
+import { webPageInspectionSchema } from "../../../src/domain/browserObservationSchemas.js";
+import { connectLocalToolsMcp } from "../../fixtures/localToolsMcp.js";
+import { startFakeCdpBrowser } from "../../fixtures/fakeCdpBrowser.js";
+import { cliTest } from "../../support/cli/cliFixture.js";
+
+const consoleTimestampCases = [
+ { label: "missing", timestamp: undefined, expected: null, incomplete: true },
+ { label: "wrong type", timestamp: "123", expected: null, incomplete: true },
+ { label: "actual zero", timestamp: 0, expected: 0, incomplete: false },
+] as const;
+
+cliTest(
+ "preserves missing and observed console timestamps through built CLI and stdio MCP schemas",
+ async ({ cli }) => {
+ const { call } = await connectLocalToolsMcp();
+
+ for (const { timestamp, expected, incomplete } of consoleTimestampCases) {
+ const browser = await startFakeCdpBrowser({
+ eventsBeforeReply: ["Runtime.enable"],
+ commandEvents: (command, origin) => {
+ if (command.method !== "Runtime.enable") return undefined;
+ const session =
+ command.sessionId === undefined
+ ? {}
+ : { sessionId: command.sessionId };
+ return [
+ {
+ ...session,
+ method: "Runtime.executionContextCreated",
+ params: {
+ context: {
+ id: 1,
+ origin,
+ auxData: { frameId: "frame-main", isDefault: true },
+ },
+ },
+ },
+ {
+ ...session,
+ method: "Runtime.consoleAPICalled",
+ params: {
+ type: "log",
+ ...(timestamp === undefined ? {} : { timestamp }),
+ args: [{ type: "string", value: "in-scope-console" }],
+ stackTrace: {
+ callFrames: [
+ {
+ url: `${origin}/app.js`,
+ lineNumber: 0,
+ columnNumber: 0,
+ },
+ ],
+ },
+ },
+ },
+ {
+ ...session,
+ method: "Runtime.consoleAPICalled",
+ params: {
+ type: "log",
+ timestamp: 124,
+ args: [{ type: "string", value: "out-of-scope-console" }],
+ stackTrace: {
+ callFrames: [
+ {
+ url: "https://outside.invalid/foreign.js",
+ lineNumber: 0,
+ columnNumber: 0,
+ },
+ ],
+ },
+ },
+ },
+ ];
+ },
+ });
+
+ try {
+ const cliResponse = await cli.run({
+ arguments: [
+ "inspect-web-page",
+ browser.endpoint,
+ "allowed-page",
+ "--allowed-origins",
+ browser.allowedOrigin,
+ "--observation-ms",
+ "0",
+ "--json",
+ ],
+ });
+ expect(cliResponse.exitCode, cliResponse.stdout).toBe(0);
+ const cliContract = toolContract("inspect_web_page").outputSchema.parse(
+ cliResponse.json,
+ );
+ const cliEvidence = parseEvidence(cliContract);
+
+ const mcpResponse = await call("inspect_web_page", {
+ cdp_endpoint: browser.endpoint,
+ target_id: "allowed-page",
+ allowed_origins: [browser.allowedOrigin],
+ observation_ms: 0,
+ });
+ expect(mcpResponse.isError).not.toBe(true);
+ const mcpEvidence = parseEvidence(
+ toolContract("inspect_web_page").outputSchema.parse(
+ mcpResponse.structuredContent,
+ ),
+ );
+
+ for (const evidence of [cliEvidence, mcpEvidence]) {
+ const page = webPageInspectionSchema.parse(
+ evidence.normalized_result,
+ );
+ expect(page.console.events).toEqual([
+ expect.objectContaining({
+ timestamp: expected,
+ url: `${browser.allowedOrigin}/app.js`,
+ argument_types: ["string"],
+ }),
+ ]);
+ expect(JSON.stringify(evidence)).not.toContain("outside.invalid");
+ expect(JSON.stringify(evidence)).not.toContain(
+ "out-of-scope-console",
+ );
+ if (incomplete) {
+ expect(page.completeness.unavailable_sections).toContain(
+ "console_events",
+ );
+ expect(page.completeness.excluded).toContainEqual({
+ section: "console_events",
+ reason: "invalid_protocol_value",
+ count: 1,
+ });
+ } else {
+ expect(page.completeness.unavailable_sections).not.toContain(
+ "console_events",
+ );
+ expect(page.completeness.excluded).not.toContainEqual(
+ expect.objectContaining({
+ section: "console_events",
+ reason: "invalid_protocol_value",
+ }),
+ );
+ }
+ }
+ } finally {
+ await browser.close();
+ }
+ }
+ },
+);