Files
reverse-engineering-anything/src/cli/browserCommands.ts
T
Adolanium 7e01f1df85 fix(browser): bound armed timeline rows by the retained-event allowance (#14799)
* fix(browser): bound armed timeline rows by the retained-event allowance

Armed timeline rows were appended with no byte budget. Pre-arm
messages and document request identities already stop at the
retained-event allowance.

Count each armed row before keeping it. A row that does not fit fails
the observation with payload_limit and does not return a shorter
timeline. Event count has no separate ceiling.

* chore(browser): format the timeline allowance check

oxfmt --check rejects the wrapped comparison in TimelineCapture.#add.

* fix(browser): give timeline retention a caller control

Armed timeline rows use browser.timeline_retained_bytes. A call can pass
timeline_retained_bytes. Request identities keep a separate counter.
A row that does not fit returns payload_limit with details.limit, including
a row admitted during terminal progress.

* fix(browser): refresh advertised schemas for the timeline allowance

The new setting and per-call field change the compact input hashes for
observe_web_session, inspect_analysis_settings, and update_analysis_settings.

* fix(browser): report measured timeline bytes without required_value

Admission stops at the first row that does not fit, so later rows are
unknown. measured_timeline_bytes records the rows already measured.
The budget test waits until all four lifecycle events are delivered.
2026-11-27 08:28:44 +08:00

237 lines
7.8 KiB
TypeScript

import { deliverCliAnalysisEvidence } from "./analysisEvidenceDelivery.js";
import type { CliInstance } from "./types.js";
import { browserCliError } from "./browserCliError.js";
import { createBrowserObservationProvider } from "../composition/browserObservation.js";
import {
analyzeWebBundle,
inspectWebPage,
listBrowserTargets,
observeWebSession,
} from "../application/BrowserObservationService.js";
import { analyzeWebBundleInputSchema } from "../domain/webBundleAnalysis.js";
import { inspectWebPageInputSchema } from "../domain/browserObservation.js";
import { analysisInputErrorFromIssues } from "../domain/inputIssueProjection.js";
import type { Logger } from "pino";
import { contractNamed } from "./contractCliDescription.js";
import { registerContractCommand } from "./contractCommand.js";
import { BROWSER_TOOL_CONTRACTS } from "../contracts/browserToolContracts.js";
import {
browserTargetProjection,
observeThroughBrowser,
} from "./browserContractCommands.js";
import { cliProgressReporter } from "./cliProgress.js";
/** Register CLI equivalents of the passive browser MCP tools. */
export const registerBrowserCommands = (
cli: CliInstance,
logger: Logger,
environment: Readonly<Record<string, string | undefined>>,
): void => {
const listTargets = contractNamed(
BROWSER_TOOL_CONTRACTS,
"list_browser_targets",
);
registerContractCommand(
cli,
logger,
listTargets,
{
description:
"List page targets from a selected loopback CDP browser, optionally filtered by origin",
positionals: ["cdp_endpoint"],
fields: {
cdp_endpoint: { name: "endpoint" },
allowed_origins: { description: "Optional exact-origin filter" },
},
},
observeThroughBrowser(listTargets, listBrowserTargets),
);
const inspectPage = contractNamed(BROWSER_TOOL_CONTRACTS, "inspect_web_page");
registerContractCommand(
cli,
logger,
inspectPage,
{
description:
"Passively inspect one page through CDP, defaulting to its current origin",
...browserTargetProjection,
fields: {
...browserTargetProjection.fields,
observation_ms: {
description: "Observation duration in milliseconds",
},
include_accessibility_text: {
description: "Include accessibility text",
},
include_console_text: { description: "Include console message text" },
include_json_body_shapes: {
description: "Include structural shapes of JSON response bodies",
},
include_websocket_shapes: {
description: "Include structural shapes of WebSocket payloads",
},
include_script_sources: {
description: "Include JavaScript source text",
},
include_storage_keys: {
description: "Include storage key names without values",
},
include_storage_fingerprints: {
description: "Include content-derived storage fingerprints",
},
},
evidenceDelivery: true,
},
async (input, { signal, delivery }) => {
// Delivery fields select the reply; the capture takes the rest.
const parsed = inspectWebPageInputSchema.safeParse(input);
if (!parsed.success)
return browserCliError(
analysisInputErrorFromIssues(
inspectPage.name,
parsed.error.issues,
input,
),
);
const provider = createBrowserObservationProvider();
return deliverCliAnalysisEvidence({
...delivery!,
run: () => inspectWebPage(provider, parsed.data, { signal }),
service: {
close: async () => {
await provider.close?.();
},
},
providerId: provider.identity().id,
operation: inspectPage.name,
signal,
});
},
);
registerBundleAnalysis(cli, logger, environment);
const observeSession = contractNamed(
BROWSER_TOOL_CONTRACTS,
"observe_web_session",
);
registerContractCommand(
cli,
logger,
observeSession,
{
description: "Observe user-driven page navigation during a chosen window",
...browserTargetProjection,
fields: {
...browserTargetProjection.fields,
observation_ms: {
description: "Observation duration in milliseconds",
},
timeline_retained_bytes: {
description:
"Byte allowance for armed timeline rows on this call. Omitted uses browser.timeline_retained_bytes.",
},
},
},
observeThroughBrowser(observeSession, (provider, input, options) =>
observeWebSession(provider, input, {
...options,
// Report the armed observation on stderr, as MCP does.
progress: cliProgressReporter,
}),
),
);
};
const registerBundleAnalysis = (
cli: CliInstance,
logger: Logger,
environment: Readonly<Record<string, string | undefined>>,
): void => {
const contract = contractNamed(BROWSER_TOOL_CONTRACTS, "analyze_web_bundle");
const pageInspectionOnly =
"page inspection extras are not part of bundle analysis on the CLI; use inspect-web-page";
registerContractCommand(
cli,
logger,
contract,
{
description:
"Capture and statically analyze a page bundle, defaulting to the selected target origin",
...browserTargetProjection,
evidenceDelivery: true,
viewDelivery: { invalidJson: "view must be valid JSON" },
jsonInputs: {
semantic_precision:
"semanticPrecision must be inline JSON or a JSON file path",
},
flatten: {
source_map_limits: {
output_bytes: { name: "sourceMapOutputBytes" },
decoded_rows: { name: "sourceMapDecodedRows" },
decoded_segments: { name: "sourceMapDecodedSegments" },
decoded_records: { name: "sourceMapDecodedRecords" },
section_depth: { name: "sourceMapSectionDepth" },
fetch_bytes: { name: "sourceMapFetchBytes" },
},
},
fields: {
...browserTargetProjection.fields,
observation_ms: {
description: "Observation duration in milliseconds",
},
semantic_precision: {
description:
"Inline JSON or JSON file of semantic_precision evaluator budgets for each inert script.",
},
fetch_source_maps: {
description: "Fetch source maps referenced by captured scripts",
},
view: {
description:
"JSON web-bundle (endpoints, inferences, summary) or javascript-routes view; requires --detail summary and --evidence_output",
},
},
omit: {
include_script_sources:
"bundle analysis always reads script sources, the contract default",
include_accessibility_text: pageInspectionOnly,
include_console_text: pageInspectionOnly,
include_json_body_shapes: pageInspectionOnly,
include_websocket_shapes: pageInspectionOnly,
include_storage_keys: pageInspectionOnly,
include_storage_fingerprints: pageInspectionOnly,
},
},
async (input, { signal, delivery }) => {
// Delivery fields select the reply; the analysis takes the rest.
const parsed = analyzeWebBundleInputSchema.safeParse(input);
if (!parsed.success)
return browserCliError(
analysisInputErrorFromIssues(
contract.name,
parsed.error.issues,
input,
),
);
const provider = createBrowserObservationProvider(undefined, environment);
return deliverCliAnalysisEvidence({
...delivery!,
run: () => analyzeWebBundle(provider, parsed.data, { signal }),
service: {
close: async () => {
await provider.close?.();
},
},
providerId: provider.identity().id,
operation: contract.name,
signal,
});
},
);
};