Files
reverse-engineering-anything/src/domain/webDeadlineControls.test.ts
T
0155b89953 fix(browser): name web-bundle budgets and recover web deadlines (#6036)
* fix(browser): name web-bundle budgets and recover web deadlines

Name the existing heap, per-script, and total web-bundle phases, and reject a per-script deadline above the total. Promote capture, CDP command, source-map decode, and source-map fetch deadlines at today's defaults. Name DevTools timeoutMs, and state that discovery, cleanup grace, byte budgets, and Electron hook retention have no runtime override.

Refs #5910

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(browser): apply selected web deadlines to the owning phase

Keep HTTP and browser-socket discovery on the fixed discovery deadline so a
short command_timeout_ms starts at the handshake. Apply scenario
command_timeout_ms to launch startup and the primary connect session, and
keep service-worker restoration on the fixed 5000 ms grace. Omit
command_timeout_ms from recovery text when the operation does not accept it.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(browser): retain scenario commands that finish after their deadline

A timed-out scenario command stays owned so cleanup can close a late
browser or context and restore a mutation that lands after the deadline.
Child storage replies use that deadline, scenario recovery names
command_timeout_ms in the capture JSON, and the session test connection
implements beginCleanupGrace.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(browser): keep child command timeouts as the public failure

A nested command such as Page.enable that is delivered but never answered
was stored as protocol_error, hiding the command name, selected deadline,
and recovery. Store the typed timeout and assert that public error.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(browser): keep runtime cleanup on the fixed grace

Switch runtime restoration to the cleanup grace before the first send so a
short command_timeout_ms cannot fail an otherwise successful observation.
Stop advertising command_timeout_ms on list_browser_targets, which never
bounds a post-discovery command.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* test(browser): prove a short command deadline cannot fail runtime cleanup

Drive restoration through the runtime fixture. A cleanup command that
outlives command_timeout_ms still succeeds, and a stalled restoration
names the fixed 5000 ms grace.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(browser): name the effective source-map decode deadline

codecBudgetLimitation reported a fixed 20-second deadline after a
caller selected decode_timeout_ms. Result and Evidence limitations
now name that effective millisecond deadline.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(browser): separate handshake and target-attach deadlines

A browser-level Target.attachToTarget no longer shares the WebSocket
handshake budget. Blocked service-worker attachment keeps discovery on
its fixed deadline and applies command_timeout_ms to the later handshake
and attach.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(browser): name the web-bundle resource each runtime exhausts

Bun ignores the V8 old-space flag. A Windows Job reports the process
commit limit, and other Bun hosts no longer suggest raising max_heap_mb.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(browser): cite the browser-socket discovery message budget

WebSocket discovery enforces the 64 MiB protocol budget. Timeout
recovery no longer advertises the 8 MiB HTTP document budget.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(browser): bound the blocked service-worker check before discovery

The first registration check has its own fixed 10s deadline. Discovery,
handshake, and Target.attachToTarget stay on their separate deadlines.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(browser): name the operand that stopped web-bundle admission

Host heap pressure no longer claims the fixed result budget is exhausted.
A Windows Bun allocation before the commit limit still names that limit.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-10-22 05:47:38 +08:00

233 lines
7.7 KiB
TypeScript

import { describe, expect, it } from "vitest";
import {
cdpCleanupGraceExpired,
cdpCommandDeadlineRecovery,
} from "./browserDeadline.js";
import { listBrowserTargetsInputSchema } from "./browserObservation.js";
import {
analyzeWebBundleInputSchema,
webBundleHeapExhaustedReason,
webBundleHeapExhaustedRemediation,
webBundleMemoryExhaustion,
webBundleResultAdmissionFailure,
webBundleScriptTimeoutRecovery,
} from "./webBundleAnalysis.js";
import {
inspectWebNetworkCaptureInputSchema,
webNetworkCaptureTimeoutRecovery,
} from "./webNetworkCapture.js";
import {
webSourceLocationInputSchema,
webSourceMapDecodeTimeoutRecovery,
} from "./webSourceLocation.js";
const bundle = {
cdp_endpoint: "http://127.0.0.1:9222",
target_id: "page",
};
describe("web deadline controls", () => {
it("rejects a per-script deadline above the total", () => {
const parsed = analyzeWebBundleInputSchema.safeParse({
...bundle,
analysis_timeout_ms: 40_000,
total_analysis_timeout_ms: 30_000,
});
expect(parsed.success).toBe(false);
if (parsed.success) return;
expect(
parsed.error.issues.map((issue) => issue.message).join(" "),
).toContain("analysis_timeout_ms");
});
it("keeps omitted web-bundle deadlines at today's defaults", () => {
const parsed = analyzeWebBundleInputSchema.parse(bundle);
expect(parsed.analysis_timeout_ms).toBeUndefined();
expect(parsed.total_analysis_timeout_ms).toBeUndefined();
expect(parsed.max_heap_mb).toBeUndefined();
expect(parsed.source_map_fetch_timeout_ms).toBeUndefined();
expect(parsed.command_timeout_ms).toBeUndefined();
});
it("accepts the promoted deadlines inside 1..2147483647", () => {
expect(
analyzeWebBundleInputSchema.safeParse({
...bundle,
command_timeout_ms: 2_147_483_647,
}).success,
).toBe(true);
expect(
listBrowserTargetsInputSchema.safeParse({
cdp_endpoint: "http://127.0.0.1:9222",
command_timeout_ms: 1_500,
}).success,
).toBe(false);
expect(
inspectWebNetworkCaptureInputSchema.safeParse({
capture_path: "/tmp/capture.har",
format: "har",
capture_timeout_ms: 0,
}).success,
).toBe(false);
expect(
webSourceLocationInputSchema.safeParse({
manifest_path: "/tmp/manifest.json",
script_index: 0,
source_map: {
path: "/tmp/app.js.map",
url: "https://example.test/app.js.map",
},
generated_position: { line: 1, column: 0 },
decode_timeout_ms: 20_000,
}).success,
).toBe(true);
});
it("names each deadline and the fixed budgets it does not raise", () => {
expect(webBundleScriptTimeoutRecovery(30_000).userMessage).toContain(
"analysis_timeout_ms",
);
expect(
webNetworkCaptureTimeoutRecovery(30_000).remediationAction,
).toContain("no runtime override");
expect(webSourceMapDecodeTimeoutRecovery(20_000).userMessage).toContain(
"decode_timeout_ms",
);
expect(
cdpCommandDeadlineRecovery(20_000, "handshake").userMessage,
).toContain("command_timeout_ms");
expect(cdpCleanupGraceExpired()).toContain("5000 ms");
expect(cdpCleanupGraceExpired()).toContain("no runtime override");
const inspector = cdpCommandDeadlineRecovery(
20_000,
"handshake",
undefined,
false,
);
expect(inspector.userMessage).not.toContain("command_timeout_ms");
expect(inspector.remediationAction).not.toContain("--command-timeout-ms");
expect(inspector.remediationAction).toContain("no runtime override");
});
});
it("names web-bundle exhaustion by the runtime that enforced it", () => {
const node = webBundleMemoryExhaustion({
runtime: "node",
heapMb: 1024,
memoryLimitReached: true,
processMemoryLimitBytes: 99,
});
expect(node.kind).toBe("v8-heap");
expect(webBundleHeapExhaustedReason(node)).toContain(
"exhausted its max_heap_mb V8 old-space heap of 1024 MiB",
);
expect(webBundleHeapExhaustedRemediation(node)).toContain(
"Increase max_heap_mb above 1024",
);
const commitBytes = 1024 * 1024 * 1024 + 512 * 1024 * 1024;
const windows = webBundleMemoryExhaustion({
runtime: "bun",
heapMb: 1024,
memoryLimitReached: true,
processMemoryLimitBytes: commitBytes,
});
const windowsReason = webBundleHeapExhaustedReason(windows);
expect(windows.kind).toBe("process-commit");
expect(windowsReason).toContain(
`${String(commitBytes)}-byte process commit limit`,
);
expect(windowsReason).toContain("512 MiB Bun runtime reserve");
expect(windowsReason).not.toContain("V8 old-space heap of");
const windowsRemediation = webBundleHeapExhaustedRemediation(windows);
expect(windowsRemediation).toContain("Increase max_heap_mb above 1024");
expect(windowsRemediation).toContain("Windows process commit limit");
expect(windowsRemediation).toContain("not a V8 old-space heap");
expect(windowsRemediation).not.toContain(
"exhausted its max_heap_mb V8 old-space heap",
);
const bun = webBundleMemoryExhaustion({
runtime: "bun",
heapMb: 1024,
memoryLimitReached: false,
processMemoryLimitBytes: null,
});
expect(bun.kind).toBe("bun-allocation");
expect(webBundleHeapExhaustedReason(bun)).toContain(
"Bun ignores the V8 old-space flag",
);
expect(webBundleHeapExhaustedReason(bun)).not.toContain(
"V8 old-space heap of",
);
expect(webBundleHeapExhaustedRemediation(bun)).not.toContain(
"Increase max_heap_mb",
);
expect(webBundleHeapExhaustedRemediation(bun)).toContain(
"does not change this exit",
);
const beforeLimit = webBundleMemoryExhaustion({
runtime: "bun",
heapMb: 1024,
memoryLimitReached: false,
processMemoryLimitBytes: commitBytes,
});
expect(beforeLimit.kind).toBe("bun-allocation");
const beforeLimitReason = webBundleHeapExhaustedReason(beforeLimit);
expect(beforeLimitReason).toContain(
`${String(commitBytes)}-byte process commit limit was reported as reached`,
);
expect(beforeLimitReason).not.toContain(
"enforces no analyzer process commit limit",
);
expect(webBundleHeapExhaustedRemediation(beforeLimit)).toContain(
"was not reported as reached",
);
expect(webBundleHeapExhaustedRemediation(beforeLimit)).not.toContain(
"Increase max_heap_mb",
);
expect(webBundleHeapExhaustedReason(bun)).toContain(
"enforces no analyzer process commit limit",
);
expect(
webBundleHeapExhaustedRemediation({
kind: "v8-heap",
heapMb: 16_384,
}),
).toContain("already at the maximum 16384 MiB");
});
it("names host heap pressure only while the fixed result budget still has room", () => {
const host = webBundleResultAdmissionFailure({
fixedBudgetBytes: 8_388_608,
fixedRemainingBytes: 8_388_608,
hostCapacityBytes: 0,
});
expect(host.kind).toBe("host-capacity");
expect(host.reason).toContain("host result capacity 0 bytes");
expect(host.reason).toContain("still has 8388608 bytes available");
expect(host.reason).not.toContain("has no further room");
if (host.kind !== "host-capacity") return;
expect(host.remediationAction).not.toContain("has no further room");
expect(host.remediationAction).toContain("NODE_OPTIONS");
expect(host.reportedLimits).toEqual({
result_bytes: 8_388_608,
result_budget_remaining_bytes: 8_388_608,
host_result_capacity_bytes: 0,
});
const fixed = webBundleResultAdmissionFailure({
fixedBudgetBytes: 8_388_608,
fixedRemainingBytes: 0,
hostCapacityBytes: 0,
});
expect(fixed.kind).toBe("fixed-budget");
expect(fixed.reason).toContain(
"8388608-byte web-bundle result budget has no further room",
);
expect(fixed.reason).toContain("no runtime override");
expect(fixed.reason).not.toContain("host result capacity");
});