refactor(observation): internalize capture budgets

This commit is contained in:
morluto
2026-09-29 12:16:10 +08:00
parent 8c24272ee7
commit 7b3c797237
24 changed files with 202 additions and 466 deletions
+8
View File
@@ -101,6 +101,14 @@ describe("browser observation contracts", () => {
max_total_websocket_shape_bytes: 1_048_576,
},
});
expect(
inspectWebPageInputSchema.safeParse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: ["https://app.example.test"],
target_id: "page-1",
limits: {},
}).success,
).toBe(false);
});
});
+13 -126
View File
@@ -102,82 +102,8 @@ export const listBrowserTargetsInputSchema = z.object({
...browserInput,
});
const browserInspectionLimitsSchema = z.object({
max_frames: z.number().int().min(1).max(1_000).default(200),
max_dom_nodes: z.number().int().min(1).max(10_000).default(2_000),
max_ax_nodes: z.number().int().min(1).max(10_000).default(2_000),
max_ax_text_field_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024)
.default(1_024),
max_total_ax_text_bytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(64 * 1_024),
max_scripts: z.number().int().min(1).max(1_000).default(200),
max_resources: z.number().int().min(1).max(10_000).default(2_000),
max_workers: z.number().int().min(1).max(5_000).default(500),
max_storage_keys: z.number().int().min(1).max(10_000).default(1_000),
max_script_source_bytes: z
.number()
.int()
.min(1)
.max(4 * 1_024 * 1_024)
.default(1_024 * 1_024),
max_total_script_source_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
max_network_events: z.number().int().min(1).max(10_000).default(1_000),
max_console_events: z.number().int().min(1).max(2_000).default(200),
max_console_text_field_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024)
.default(1_024),
max_total_console_text_bytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(64 * 1_024),
max_json_body_bytes: z
.number()
.int()
.min(1)
.max(4 * 1_024 * 1_024)
.default(1_024 * 1_024),
max_total_json_body_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
max_json_shape_nodes: z.number().int().min(1).max(100_000).default(5_000),
max_json_shape_depth: z.number().int().min(1).max(100).default(20),
max_websocket_events: z.number().int().min(1).max(5_000).default(500),
max_websocket_shape_bytes: z
.number()
.int()
.min(1)
.max(1_024 * 1_024)
.default(64 * 1_024),
max_total_websocket_shape_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(1_024 * 1_024),
});
const DEFAULT_BROWSER_INSPECTION_LIMITS = {
/** Collection bounds for passive browser capture, applied internally. */
export const DEFAULT_BROWSER_INSPECTION_LIMITS = {
max_frames: 200,
max_dom_nodes: 2_000,
max_ax_nodes: 2_000,
@@ -200,7 +126,7 @@ const DEFAULT_BROWSER_INSPECTION_LIMITS = {
max_websocket_events: 500,
max_websocket_shape_bytes: 64 * 1_024,
max_total_websocket_shape_bytes: 1_024 * 1_024,
} as const;
};
const inspectWebPageInputFacts = {
...browserInput,
@@ -212,9 +138,6 @@ const inspectWebPageInputFacts = {
include_websocket_shapes: z.boolean().default(false),
include_storage_keys: z.boolean().default(false),
include_storage_fingerprints: z.boolean().default(false),
limits: browserInspectionLimitsSchema.default(
DEFAULT_BROWSER_INSPECTION_LIMITS,
),
} as const;
const inspectWebPageWithoutSourceSchema = z.strictObject({
@@ -241,61 +164,25 @@ const refineInspectWebPageInput = (
path: ["include_storage_fingerprints"],
message: "Storage fingerprints require storage key capture",
});
if (
input.limits.max_script_source_bytes >
input.limits.max_total_script_source_bytes
)
context.addIssue({
code: "custom",
path: ["limits", "max_script_source_bytes"],
message: "Per-script source limit cannot exceed the total source limit",
});
if (
input.limits.max_console_text_field_bytes >
input.limits.max_total_console_text_bytes
)
context.addIssue({
code: "custom",
path: ["limits", "max_console_text_field_bytes"],
message:
"Per-field console text limit cannot exceed the total text limit",
});
if (input.limits.max_json_body_bytes > input.limits.max_total_json_body_bytes)
context.addIssue({
code: "custom",
path: ["limits", "max_json_body_bytes"],
message: "Per-body JSON limit cannot exceed the total body limit",
});
if (
input.limits.max_websocket_shape_bytes >
input.limits.max_total_websocket_shape_bytes
)
context.addIssue({
code: "custom",
path: ["limits", "max_websocket_shape_bytes"],
message: "Per-frame WebSocket limit cannot exceed the total frame limit",
});
if (
input.limits.max_ax_text_field_bytes > input.limits.max_total_ax_text_bytes
)
context.addIssue({
code: "custom",
path: ["limits", "max_ax_text_field_bytes"],
message:
"Per-field accessibility text limit cannot exceed the total text limit",
});
};
/** Source-capturing inspection input, reusable by static bundle analysis. */
/** Caller schema for source-capturing inspection and bundle analysis. */
export const inspectWebPageWithSourceInputSchema =
inspectWebPageWithSourceShapeSchema.superRefine(refineInspectWebPageInput);
/** Public input for one passive, bounded inspection of an existing page. */
export const inspectWebPageInputSchema = z.union([
/** Caller-visible schema for one passive inspection. */
export const inspectWebPageToolInputSchema = z.union([
inspectWebPageWithoutSourceSchema.superRefine(refineInspectWebPageInput),
inspectWebPageWithSourceInputSchema,
]);
/** Internal parsed input with REA's fixed collection bounds applied. */
export const inspectWebPageInputSchema =
inspectWebPageToolInputSchema.transform((input) => ({
...input,
limits: DEFAULT_BROWSER_INSPECTION_LIMITS,
}));
export type ListBrowserTargetsInput = z.infer<
typeof listBrowserTargetsInputSchema
>;
+25 -40
View File
@@ -24,6 +24,17 @@ const approvedElectronInput = {
allowed_file_roots: electronFileRootsSchema,
};
/** Collection bounds for passive Electron capture, applied internally. */
export const DEFAULT_ELECTRON_INSPECTION_LIMITS = {
max_frames: 200,
max_dom_nodes: 2_000,
max_scripts: 500,
max_resources: 2_000,
max_workers: 500,
max_script_source_bytes: 1_024 * 1_024,
max_total_script_source_bytes: 4 * 1_024 * 1_024,
};
/** Input for listing root-confined file:// page targets from Electron CDP. */
export const listElectronTargetsInputSchema = z.strictObject({
...approvedElectronInput,
@@ -37,35 +48,6 @@ const inspectElectronPageFacts = {
...approvedElectronInput,
target_id: z.string().trim().min(1).max(256),
observation_ms: z.number().int().min(0).max(10_000).default(100),
limits: z
.strictObject({
max_frames: z.number().int().min(1).max(1_000).default(200),
max_dom_nodes: z.number().int().min(1).max(10_000).default(2_000),
max_scripts: z.number().int().min(1).max(2_000).default(500),
max_resources: z.number().int().min(1).max(10_000).default(2_000),
max_workers: z.number().int().min(1).max(5_000).default(500),
max_script_source_bytes: z
.number()
.int()
.min(1)
.max(4 * 1_024 * 1_024)
.default(1_024 * 1_024),
max_total_script_source_bytes: z
.number()
.int()
.min(1)
.max(16 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
})
.default({
max_frames: 200,
max_dom_nodes: 2_000,
max_scripts: 500,
max_resources: 2_000,
max_workers: 500,
max_script_source_bytes: 1_024 * 1_024,
max_total_script_source_bytes: 4 * 1_024 * 1_024,
}),
} as const;
export const inspectElectronPageInputSchema = z
.union([
@@ -78,17 +60,20 @@ export const inspectElectronPageInputSchema = z
include_script_sources: z.literal(true),
}),
])
.superRefine((input, context) => {
if (
input.limits.max_script_source_bytes >
input.limits.max_total_script_source_bytes
)
context.addIssue({
code: "custom",
path: ["limits", "max_script_source_bytes"],
message: "Per-script source limit cannot exceed the total source limit",
});
});
.transform((input) => ({
...input,
limits: DEFAULT_ELECTRON_INSPECTION_LIMITS,
}));
export const inspectElectronPageToolInputSchema = z.union([
z.strictObject({
...inspectElectronPageFacts,
include_script_sources: z.literal(false).default(false),
}),
z.strictObject({
...inspectElectronPageFacts,
include_script_sources: z.literal(true),
}),
]);
export type InspectElectronPageInput = z.infer<
typeof inspectElectronPageInputSchema
>;
+16 -20
View File
@@ -72,35 +72,31 @@ const observedJavaScriptRuntimeKindSchema = javascriptRuntimeKindSchema.or(
z.literal("unknown"),
);
export const javascriptRuntimeObservationLimitsSchema = z.strictObject({
max_events: z.number().int().min(1).max(50_000).default(10_000),
max_scripts: z.number().int().min(1).max(10_000).default(2_000),
max_execution_contexts: z.number().int().min(1).max(5_000).default(1_000),
max_location_bytes: z.number().int().min(64).max(65_536).default(16_384),
max_total_metadata_bytes: z
.number()
.int()
.min(1_024)
.max(32 * 1_024 * 1_024)
.default(4 * 1_024 * 1_024),
});
/** Collection bounds for passive V8 Inspector capture, applied internally. */
export const DEFAULT_JAVASCRIPT_RUNTIME_OBSERVATION_LIMITS = {
max_events: 10_000,
max_scripts: 2_000,
max_execution_contexts: 1_000,
max_location_bytes: 16_384,
max_total_metadata_bytes: 4 * 1_024 * 1_024,
};
/** Input for one bounded, attach-only V8 Inspector observation. */
export const observeJavaScriptRuntimeInputSchema = z
export const observeJavaScriptRuntimeToolInputSchema = z
.strictObject({
...runtimeScope,
target_id: z.string().trim().min(1).max(256),
runtime_kind: javascriptRuntimeKindSchema.optional(),
observation_ms: z.number().int().min(0).max(10_000).default(100),
limits: javascriptRuntimeObservationLimitsSchema.default({
max_events: 10_000,
max_scripts: 2_000,
max_execution_contexts: 1_000,
max_location_bytes: 16_384,
max_total_metadata_bytes: 4 * 1_024 * 1_024,
}),
})
.superRefine(requireRuntimeScope);
/** Internal parsed input with REA's fixed collection bounds applied. */
export const observeJavaScriptRuntimeInputSchema =
observeJavaScriptRuntimeToolInputSchema.transform((input) => ({
...input,
limits: DEFAULT_JAVASCRIPT_RUNTIME_OBSERVATION_LIMITS,
}));
export type ObserveJavaScriptRuntimeInput = z.infer<
typeof observeJavaScriptRuntimeInputSchema
>;
+6 -1
View File
@@ -1,6 +1,9 @@
import { z } from "zod";
import { inspectWebPageWithSourceInputSchema } from "./browserObservation.js";
import {
DEFAULT_BROWSER_INSPECTION_LIMITS,
inspectWebPageWithSourceInputSchema,
} from "./browserObservation.js";
import { webTextArtifactSchema } from "./webContentArtifact.js";
/** Provider-owned parser and network safety bounds, never caller settings. */
@@ -20,11 +23,13 @@ export const analyzeWebBundleToolInputSchema =
export const analyzeWebBundleInputSchema =
analyzeWebBundleToolInputSchema.transform((input) => ({
...input,
limits: DEFAULT_BROWSER_INSPECTION_LIMITS,
analysis_limits: WEB_BUNDLE_ANALYSIS_LIMITS,
})) as z.ZodType<AnalyzeWebBundleInput>;
export type AnalyzeWebBundleInput = z.infer<
typeof inspectWebPageWithSourceInputSchema
> & {
readonly limits: typeof DEFAULT_BROWSER_INSPECTION_LIMITS;
readonly fetch_source_maps: boolean;
readonly analysis_limits: typeof WEB_BUNDLE_ANALYSIS_LIMITS;
};
+2 -2
View File
@@ -1,7 +1,7 @@
import { describe, expect, it } from "vitest";
import {
inspectWebPageInputSchema,
inspectWebPageToolInputSchema,
webPageInspectionSchema,
} from "./browserObservation.js";
import { analyzeCapturedWebBundle } from "./webBundleAnalyzer.js";
@@ -142,7 +142,7 @@ describe("web bundle analyzer", () => {
const input = (overrides: Record<string, unknown> = {}) =>
analyzeWebBundleInputSchema.parse({
...inspectWebPageInputSchema.parse({
...inspectWebPageToolInputSchema.parse({
cdp_endpoint: "http://127.0.0.1:9222",
allowed_origins: [origin],
target_id: "page-1",