refactor(browser): return complete scenario observations

This commit is contained in:
morluto
2026-09-29 13:02:20 +08:00
parent e8c56e0c0a
commit e80fe414b1
16 changed files with 351 additions and 459 deletions
+73
View File
@@ -0,0 +1,73 @@
import { expect, it } from "vitest";
import { browserScenarioSchema } from "./browserScenario.js";
it("accepts scenarios beyond former action, secret, storage, and replay-route counts", () => {
const actions = Array.from({ length: 129 }, (_, index) => ({
step_id: `fill_${index}`,
action: "fill",
locator: { kind: "css", selector: "#field" },
value: { source: "secret", secret_id: `secret_${index}` },
}));
const secrets = Array.from({ length: actions.length }, (_, index) => ({
secret_id: `secret_${index}`,
environment_variable: `SECRET_${index}`,
purpose: "input",
redaction: "replace-with-secret-reference",
}));
const cookies = Array.from({ length: 129 }, (_, index) => ({
name: `cookie_${index}`,
value: { source: "literal", value: "value", classification: "public" },
destination: { url: "https://app.example.test/" },
http_only: false,
secure: true,
same_site: "Lax",
}));
const storageEntries = Array.from({ length: 129 }, (_, index) => ({
name: `entry_${index}`,
value: { source: "literal", value: "value", classification: "public" },
}));
const routes = Array.from({ length: 257 }, (_, index) => ({
route_id: `route_${index}`,
method: "GET",
request: { url: `https://app.example.test/${index}` },
response: { kind: "response", status: 200 },
}));
const result = browserScenarioSchema.parse({
browser: {
mode: "launch",
executable_path: "/opt/chromium",
headless: true,
user_data: "temporary-owned",
cleanup: "close-and-delete-profile",
},
start_url: { url: "https://app.example.test/" },
allowed_origins: ["https://app.example.test"],
actions,
secrets,
storage: {
cookies,
local_storage: [
{ origin: "https://app.example.test", entries: storageEntries },
],
session_storage: [
{ origin: "https://app.example.test", entries: storageEntries },
],
},
request_replay: {
mode: "exact",
unmatched: "abort",
routes,
},
});
expect(result.actions).toHaveLength(129);
expect(result.secrets).toHaveLength(129);
expect(result.storage.cookies).toHaveLength(129);
expect(result.storage.local_storage[0]?.entries).toHaveLength(129);
expect(result.storage.session_storage[0]?.entries).toHaveLength(129);
expect(
result.request_replay.mode === "exact" && result.request_replay.routes,
).toHaveLength(257);
});
+1 -4
View File
@@ -1,7 +1,6 @@
import { z } from "zod";
import {
BROWSER_SCENARIO_LIMITS,
BROWSER_SCENARIO_CAPTURE_LIMITS,
browserScenarioActionSchema,
browserScenarioAllowedOriginsSchema,
@@ -42,13 +41,11 @@ export const browserScenarioInputSchema = z.strictObject({
actions: z
.array(browserScenarioActionSchema)
.min(1)
.max(BROWSER_SCENARIO_LIMITS.actions)
.describe("Required bounded sequence of explicit browser actions."),
.describe("Required sequence of explicit browser actions."),
storage: browserScenarioStorageSchema,
request_replay: browserScenarioRequestReplaySchema,
secrets: z
.array(browserScenarioSecretSchema)
.max(BROWSER_SCENARIO_LIMITS.secrets)
.default([])
.describe(
"Optional references to approved environment variables. Secret values are never supplied inline; declarations are required only when an action, URL, storage value, or replay route references a secret.",
+54
View File
@@ -1,3 +1,5 @@
import { createHash } from "node:crypto";
import { expect, it } from "vitest";
import { sanitizeBrowserUrl } from "./browserObservation.js";
@@ -86,6 +88,58 @@ it("does not attach a content digest to redacted storage secrets", () => {
).toBe(false);
});
it("accepts complete storage, history, and text artifacts beyond former caps", () => {
const text = "x".repeat(1_048_577);
const fingerprint = {
name: "entry",
value_state: "hashed",
value_sha256: "a".repeat(64),
} as const;
const parsed = browserStepArtifactsSchema.safeParse({
screenshot: { state: "not_requested" },
dom: {
state: "captured",
value: {
sha256: createHash("sha256").update(text).digest("hex"),
bytes: Buffer.byteLength(text),
text,
},
},
accessibility: { state: "not_requested" },
url: { state: "not_requested" },
history: {
state: "captured",
value: {
length: 257,
current_url: sanitizeBrowserUrl("https://example.test/"),
navigation_entries: Array.from({ length: 257 }, () => ({
type: "navigate",
name: sanitizeBrowserUrl("https://example.test/"),
})),
},
},
storage: {
state: "captured",
value: {
cookies: Array.from({ length: 513 }, () => ({
name: fingerprint.name,
domain: "example.test",
path: "/",
secure: true,
http_only: false,
same_site: "Lax",
value_state: fingerprint.value_state,
value_sha256: fingerprint.value_sha256,
})),
local_storage: Array.from({ length: 513 }, () => fingerprint),
session_storage: Array.from({ length: 513 }, () => fingerprint),
},
},
});
expect(parsed.success).toBe(true);
});
it("derives equality eligibility from exact completeness state", () => {
expect(
browserScenarioCompletenessSchema.safeParse({
+4 -4
View File
@@ -34,15 +34,15 @@ export const browserScenarioCaptureSchema = z
scenario: z.strictObject({
start_origin: z.string().min(1).max(2_048),
allowed_origins: z.array(z.string().min(1).max(2_048)).min(1).max(32),
action_count: z.number().int().min(1).max(128),
secret_references: z.array(z.string().min(1).max(64)).max(64),
action_count: z.number().int().min(1),
secret_references: z.array(z.string().min(1).max(64)),
}),
duration_ms: z.number().int().min(0),
steps: z.array(browserScenarioStepSchema).min(1).max(129),
steps: z.array(browserScenarioStepSchema).min(1),
events: z.strictObject({
retained: z.number().int().min(0),
dropped: z.number().int().min(0),
items: z.array(browserScenarioEventSchema).max(100_000),
items: z.array(browserScenarioEventSchema),
}),
completeness: browserScenarioCompletenessSchema,
limitations: z.array(z.string().min(1).max(2_048)).max(64),
+39 -49
View File
@@ -6,17 +6,11 @@ import { sanitizedBrowserUrlSchema } from "./browserObservation.js";
import { webScreenshotArtifactSchema } from "./webScreenshot.js";
const digestSchema = z.string().regex(/^[a-f0-9]{64}$/u);
const boundedTextSchema = z.string().max(1_048_576);
const textArtifactSchema = z
.strictObject({
sha256: digestSchema,
bytes: z
.number()
.int()
.min(0)
.max(16 * 1_024 * 1_024),
text: boundedTextSchema,
bytes: z.number().int().min(0),
text: z.string(),
})
.superRefine((artifact, context) => {
const bytes = Buffer.from(artifact.text);
@@ -49,20 +43,18 @@ export const captureStateSchema = <Schema extends z.ZodType>(value: Schema) =>
const historySchema = z.strictObject({
length: z.number().int().min(0),
current_url: sanitizedBrowserUrlSchema,
navigation_entries: z
.array(
z.strictObject({
type: z.enum([
"navigate",
"reload",
"back_forward",
"prerender",
"unknown",
]),
name: sanitizedBrowserUrlSchema,
}),
)
.max(256),
navigation_entries: z.array(
z.strictObject({
type: z.enum([
"navigate",
"reload",
"back_forward",
"prerender",
"unknown",
]),
name: sanitizedBrowserUrlSchema,
}),
),
});
const hashedStorageValueShape = {
@@ -83,35 +75,33 @@ export type BrowserStorageValueFingerprint = z.infer<
const storageValueSchema = z.discriminatedUnion("value_state", [
z.strictObject({
name: z.string().max(1_024),
name: z.string(),
...hashedStorageValueShape,
}),
z.strictObject({
name: z.string().max(1_024),
name: z.string(),
...redactedStorageValueShape,
}),
]);
const cookieShape = {
name: z.string().max(1_024),
domain: z.string().max(2_048),
path: z.string().max(4_096),
name: z.string(),
domain: z.string(),
path: z.string(),
secure: z.boolean(),
http_only: z.boolean(),
same_site: z.enum(["Strict", "Lax", "None"]),
};
const storageSnapshotSchema = z.strictObject({
cookies: z
.array(
z.discriminatedUnion("value_state", [
z.strictObject({ ...cookieShape, ...hashedStorageValueShape }),
z.strictObject({ ...cookieShape, ...redactedStorageValueShape }),
]),
)
.max(512),
local_storage: z.array(storageValueSchema).max(512),
session_storage: z.array(storageValueSchema).max(512),
cookies: z.array(
z.discriminatedUnion("value_state", [
z.strictObject({ ...cookieShape, ...hashedStorageValueShape }),
z.strictObject({ ...cookieShape, ...redactedStorageValueShape }),
]),
),
local_storage: z.array(storageValueSchema),
session_storage: z.array(storageValueSchema),
});
export const browserStepArtifactsSchema = z.strictObject({
@@ -132,10 +122,10 @@ const eventBase = {
const eventUrl = sanitizedBrowserUrlSchema.nullable();
const networkEventShape = {
...eventBase,
method: z.string().min(1).max(32),
method: z.string().min(1),
url: sanitizedBrowserUrlSchema,
resource_type: z.string().min(1).max(64),
header_names: z.array(z.string().max(256)).max(256),
resource_type: z.string().min(1),
header_names: z.array(z.string()),
};
const webSocketFrameShape = {
...eventBase,
@@ -149,15 +139,15 @@ export const browserScenarioEventSchema = z.union([
z.strictObject({
...eventBase,
kind: z.literal("console"),
level: z.string().min(1).max(64),
text: z.string().max(65_536),
level: z.string().min(1),
text: z.string(),
url: eventUrl,
}),
z.strictObject({
...eventBase,
kind: z.literal("page-error"),
message: z.string().max(65_536),
stack: z.string().max(262_144).nullable(),
message: z.string(),
stack: z.string().nullable(),
}),
z.strictObject({
...networkEventShape,
@@ -175,7 +165,7 @@ export const browserScenarioEventSchema = z.union([
...networkEventShape,
kind: z.literal("request-failed"),
status: z.null(),
failure: z.string().min(1).max(1_024),
failure: z.string().min(1),
}),
z.strictObject({
...eventBase,
@@ -185,7 +175,7 @@ export const browserScenarioEventSchema = z.union([
z.strictObject({
...webSocketFrameShape,
payload_type: z.literal("text"),
payload_text: z.string().max(65_536),
payload_text: z.string(),
}),
z.strictObject({
...webSocketFrameShape,
@@ -204,12 +194,12 @@ export const browserScenarioEventSchema = z.union([
"popup-closed",
]),
url: eventUrl,
name: z.string().max(1_024).nullable(),
name: z.string().nullable(),
}),
z.strictObject({
...eventBase,
kind: z.literal("download-cancelled"),
suggested_filename: z.string().max(1_024),
suggested_filename: z.string(),
url: sanitizedBrowserUrlSchema,
}),
]);
@@ -310,7 +300,7 @@ export const browserScenarioStepSchema = z.union([
z.strictObject({
...browserScenarioStepShape,
status: z.enum(["failed", "cancelled"]),
error: z.string().max(4_096),
error: z.string(),
}),
]);
export type BrowserScenarioStep = z.infer<typeof browserScenarioStepSchema>;
+9 -43
View File
@@ -7,30 +7,14 @@ import {
} from "./browserObservation.js";
export const BROWSER_SCENARIO_LIMITS = {
actions: 128,
secrets: 64,
storageEntries: 512,
replayRoutes: 256,
durationMs: 300_000,
actionTimeoutMs: 60_000,
} as const;
/** Fixed internal budgets for provider-owned scenario capture. */
/** Fixed internal deadlines for provider-owned scenario capture. */
export interface BrowserScenarioCaptureLimits {
readonly max_duration_ms: number;
readonly action_timeout_ms: number;
readonly navigation_timeout_ms: number;
readonly max_events: number;
readonly max_frames: number;
readonly max_workers: number;
readonly max_popups: number;
readonly max_websockets: number;
readonly max_dom_nodes: number;
readonly max_accessibility_nodes: number;
readonly max_screenshots: number;
readonly max_screenshot_bytes: number;
readonly max_storage_entries: number;
readonly max_total_metadata_bytes: number;
}
export const BROWSER_SCENARIO_CAPTURE_LIMITS: BrowserScenarioCaptureLimits =
@@ -38,17 +22,6 @@ export const BROWSER_SCENARIO_CAPTURE_LIMITS: BrowserScenarioCaptureLimits =
max_duration_ms: 60_000,
action_timeout_ms: 5_000,
navigation_timeout_ms: 10_000,
max_events: 2_000,
max_frames: 100,
max_workers: 20,
max_popups: 10,
max_websockets: 100,
max_dom_nodes: 10_000,
max_accessibility_nodes: 10_000,
max_screenshots: 16,
max_screenshot_bytes: 4 * 1_024 * 1_024,
max_storage_entries: 256,
max_total_metadata_bytes: 4 * 1_024 * 1_024,
});
export const scenarioIdentifierSchema = z
@@ -117,7 +90,7 @@ const queryEntrySchema = z.strictObject({
/** URL whose values remain explicit public literals or declared secret references. */
export const browserScenarioUrlSchema = z.strictObject({
url: browserScenarioBaseUrlSchema,
query: z.array(queryEntrySchema).max(64).default([]),
query: z.array(queryEntrySchema).default([]),
});
export type BrowserScenarioUrl = z.infer<typeof browserScenarioUrlSchema>;
@@ -290,25 +263,22 @@ export const browserScenarioStorageSchema = z
same_site: z.enum(["Strict", "Lax", "None"]),
}),
)
.max(128)
.default([]),
local_storage: z
.array(
z.strictObject({
origin: browserOriginSchema,
entries: z.array(storageEntrySchema).max(128),
entries: z.array(storageEntrySchema),
}),
)
.max(32)
.default([]),
session_storage: z
.array(
z.strictObject({
origin: browserOriginSchema,
entries: z.array(storageEntrySchema).max(128),
entries: z.array(storageEntrySchema),
}),
)
.max(32)
.default([]),
})
.default({ cookies: [], local_storage: [], session_storage: [] })
@@ -332,7 +302,7 @@ const replayResponseSchema = z.discriminatedUnion("kind", [
z.strictObject({
kind: z.literal("response"),
status: z.number().int().min(100).max(599),
headers: z.array(replayHeaderSchema).max(64).default([]),
headers: z.array(replayHeaderSchema).default([]),
body: browserScenarioValueSchema.optional(),
}),
z.strictObject({
@@ -361,10 +331,7 @@ export const browserScenarioRequestReplaySchema = z
z.strictObject({
mode: z.literal("exact"),
unmatched: z.enum(["abort", "passthrough-approved-origins"]),
routes: z
.array(replayRouteSchema)
.min(1)
.max(BROWSER_SCENARIO_LIMITS.replayRoutes),
routes: z.array(replayRouteSchema).min(1),
}),
])
.default({ mode: "disabled" })
@@ -386,7 +353,7 @@ const REQUIRED_REDACTED_HEADERS = [
"set-cookie",
] as const;
const normalizedNames = (maximum: number) =>
const normalizedNames = () =>
z
.array(
z
@@ -396,7 +363,6 @@ const normalizedNames = (maximum: number) =>
.max(256)
.transform((value) => value.toLowerCase()),
)
.max(maximum)
.transform((values) => [...new Set(values)].sort());
export const browserScenarioRedactionSchema = z
@@ -404,8 +370,8 @@ export const browserScenarioRedactionSchema = z
secret_values: z
.literal("replace-with-secret-reference")
.default("replace-with-secret-reference"),
query_parameter_names: normalizedNames(128).default([]),
header_names: normalizedNames(128).default([...REQUIRED_REDACTED_HEADERS]),
query_parameter_names: normalizedNames().default([]),
header_names: normalizedNames().default([...REQUIRED_REDACTED_HEADERS]),
})
.superRefine(({ header_names: names }, context) => {
for (const required of REQUIRED_REDACTED_HEADERS)