feat(browser): attribute native execution and listener sources (#917)

* feat(browser): attribute native execution and listener sources

Integrate finite native precise-coverage windows and direct DOM listener inspection through shared CLI/MCP workflows, exact session script identities, inline source evidence and owned instrumentation cleanup.

Refs #881, #774

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): preserve runtime identity and settle fixture cleanup

Retain complete reported initiator objects, reject changing loader availability, propagate the selected operation through main-frame authorization and complete effect/help inventories. Close only the synthetic owned Chrome via its native endpoint, await process-group settlement and preserve primary verification errors during cleanup.

Refs #881

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): settle invalid events and preserve document evidence

Accept same-document URL changes with proven stable loader identity, retain metadata without inventing ownership, terminate malformed event collection immediately, and sanitize known transport URLs without erasing sourceURL declarations.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): end detached target windows and sanitize coverage identities

Reuse the existing flat-session event matcher, retain prearming termination, and apply the source identity policy to coverage URLs. Add real MCP target-close verification and producer regressions for detachment orderings.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): retain final sample metadata and settle fatal protocol failures

Keep source inventory open through the resetting sample, propagate fatal wire failures without outstanding commands, and check listener collection failures after final document validation.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)

Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): require default execution worlds for website attribution

Preserve native context and world metadata without treating same-frame isolated or unknown contexts as website sources. Verify actual Chrome isolated function execution before excluding its coverage, and cover ambiguous world metadata with producer regressions.

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)

Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

* fix(browser): revalidate runtime source joins and selector failures

Assisted-by: Yukari (gensokyo-skills:yukari-boundary-analysis)
Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)

---------

Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
Ysopwnable
2026-10-07 21:45:07 +08:00
committed by GitHub
co-authored by morluto
parent ef1a0cc65a
commit b5afa5f91e
82 changed files with 5392 additions and 85 deletions
+3 -1
View File
@@ -11,13 +11,15 @@ export class BrowserObservationError extends AnalysisError {
override readonly cleanupIncomplete: boolean;
override readonly cleanupResources: readonly string[];
override readonly userCategory: "cancelled" | undefined;
override readonly userMessage: string | undefined;
constructor(
readonly operation: BrowserObservationOperation,
readonly reason: BrowserObservationFailureReason,
options?: ErrorOptions,
options?: ErrorOptions & { readonly detail?: string },
) {
super(`Browser observation ${operation} failed: ${reason}`, options);
this.userMessage = options?.detail;
this.cleanupIncomplete = reason === "cleanup_failed";
this.cleanupResources =
reason === "cleanup_failed" ? ["browser_transport"] : [];
+2
View File
@@ -5,6 +5,8 @@ export type BrowserObservationOperation =
| "analyze_web_bundle"
| "trace_web_module_imports"
| "observe_web_session"
| "observe_web_execution"
| "inspect_web_event_listeners"
| "discover_webmcp_tools"
| "compare_web_captures"
| "capture_web_screenshot"
+44
View File
@@ -0,0 +1,44 @@
import { z } from "zod";
import {
webRuntimeBrowserSchema,
webRuntimeLocationSchema,
webRuntimeScopeSchema,
webRuntimeSourceSchema,
webRuntimeTargetSchema,
} from "./webRuntime.js";
/** Inspect listeners registered directly on the first main-document CSS match. */
export const inspectWebEventListenersInputSchema = webRuntimeScopeSchema.extend(
{
selector: z.string().min(1),
},
);
export type InspectWebEventListenersInput = z.infer<
typeof inspectWebEventListenersInputSchema
>;
/** A listener declaration and its source location do not establish that it ran. */
export const webEventListenersSchema = z.object({
browser: webRuntimeBrowserSchema,
target: webRuntimeTargetSchema,
inspected_at: z.iso.datetime(),
selected_node: z.object({
selector: z.string(),
match: z.enum(["found", "not_found"]),
backend_node_id: z.number().int().min(1).nullable(),
}),
listeners: z.array(
z.object({
type: z.string(),
use_capture: z.boolean(),
passive: z.boolean(),
once: z.boolean(),
location: webRuntimeLocationSchema,
execution: z.literal("unknown"),
}),
),
sources: z.array(webRuntimeSourceSchema),
cleanup: z.literal("confirmed"),
limitations: z.array(z.string()),
});
export type WebEventListeners = z.infer<typeof webEventListenersSchema>;
+93
View File
@@ -0,0 +1,93 @@
import { z } from "zod";
import { jsonObjectSchema } from "./jsonValue.js";
import {
webRuntimeBrowserSchema,
webRuntimeLocationSchema,
webRuntimeScopeSchema,
webRuntimeSourceSchema,
webRuntimeTargetSchema,
} from "./webRuntime.js";
/** Explicit instrumentation request for one finite, externally driven execution window. */
export const observeWebExecutionInputSchema = webRuntimeScopeSchema.extend({
observation_ms: z.number().int().min(1).max(2_147_483_647).default(10_000),
});
export type ObserveWebExecutionInput = z.infer<
typeof observeWebExecutionInputSchema
>;
const rangeSchema = z.object({
start_offset: z.number().int().min(0),
end_offset: z.number().int().min(0),
count: z.number().int().min(0),
source_bounds: z.enum(["verified", "unknown"]),
});
const functionSchema = z.object({
name: z.string(),
is_block_coverage: z.boolean(),
ranges: z.array(rangeSchema),
});
/** Precise V8 ranges retain their nested structure and counts without summing them. */
export const webExecutionSchema = z.object({
browser: webRuntimeBrowserSchema,
target: webRuntimeTargetSchema,
window: z.object({
armed_at: z.iso.datetime(),
ended_at: z.iso.datetime(),
requested_ms: z.number().int().min(1),
end_reason: z.enum([
"window_elapsed",
"document_changed",
"target_terminated",
]),
producer_started_seconds: z.number().min(0),
producer_sampled_seconds: z.number().min(0).nullable(),
}),
coverage: z.object({
state: z.enum(["captured", "unavailable"]),
reason: z.string().nullable(),
offset_units: z.literal("utf16-code-units"),
end_offset: z.literal("exclusive"),
scripts: z.array(
z.object({
script_id: z.string().min(1),
reported_url: z.string(),
functions: z.array(functionSchema),
}),
),
excluded_scripts: z.number().int().min(0),
}),
requests: z.array(
z.object({
request_id: z.string().min(1),
url: z.string(),
method: z.string(),
timestamp_seconds: z.number().min(0),
initiator_type: z.string(),
reported_initiator: jsonObjectSchema,
callsites: z.array(webRuntimeLocationSchema),
async_parent_ids: z.array(
z.object({ id: z.string(), debugger_id: z.string().nullable() }),
),
causal_attribution: z.literal("unknown"),
}),
),
excluded_requests: z.number().int().min(0),
script_inventory: z.object({
main_document_scripts: z.number().int().min(0),
not_reported_script_ids: z.array(z.string().min(1)),
coverage_absence: z.literal("unknown"),
}),
sources: z.array(webRuntimeSourceSchema),
instrumentation: z.object({
resets_execution_counters: z.literal(true),
disables_optimized_execution: z.literal(true),
takes_one_resetting_sample: z.boolean(),
executes_selected_code: z.literal(false),
cleanup: z.literal("confirmed"),
page_ownership: z.literal("external"),
}),
limitations: z.array(z.string()),
});
export type WebExecution = z.infer<typeof webExecutionSchema>;
+79
View File
@@ -0,0 +1,79 @@
import { z } from "zod";
import {
browserAllowedOriginsSchema,
browserEndpointSchema,
} from "./browserObservation.js";
import { browserVersionSchema } from "./browserObservationSchemas.js";
import { jsonObjectSchema } from "./jsonValue.js";
/** Scope for one externally owned page; omitted origins select its current origin. */
export const webRuntimeScopeSchema = z.strictObject({
cdp_endpoint: browserEndpointSchema,
allowed_origins: browserAllowedOriginsSchema,
target_id: z.string().min(1),
});
/** Source evidence is bound by the selected CDP session's script ID, never URL equality. */
export const webRuntimeSourceSchema = z.object({
script_id: z.string().min(1),
url: z.string(),
execution_context_id: z.number().int().nullable(),
reported_execution_context: jsonObjectSchema.nullable(),
reported_script_context_aux_data: jsonObjectSchema.nullable(),
frame_id: z.string().nullable(),
producer_hash: z.string().nullable(),
source_map_url: z.string().nullable(),
has_source_url: z.boolean().nullable(),
language: z.string().nullable(),
resource_start: z
.object({
line_number: z.number().int().min(0),
column_number: z.number().int().min(0),
})
.nullable(),
source: z.discriminatedUnion("state", [
z.object({
state: z.literal("captured"),
text: z.string(),
sha256: z.string().regex(/^[a-f0-9]{64}$/u),
utf8_bytes: z.number().int().min(0),
utf16_units: z.number().int().min(0),
}),
z.object({
state: z.enum(["unavailable", "excluded"]),
reason: z.string().min(1),
}),
]),
});
export type WebRuntimeSource = z.infer<typeof webRuntimeSourceSchema>;
/** Producer callsite coordinates are zero-based resource lines and UTF-16 columns. */
export const webRuntimeLocationSchema = z.object({
script_id: z.string(),
url: z.string().nullable(),
line_number: z.number().int().min(0),
column_number: z.number().int().min(0).nullable(),
function_name: z.string().nullable(),
source_association: z.enum(["script_id", "unknown"]),
});
/** Common identity for a selected live page document. */
export const webRuntimeTargetSchema = z.object({
target_id: z.string().min(1),
origin: z.string().min(1),
initial_url: z.string(),
frame_id: z.string().min(1),
loader_id: z.string().nullable(),
});
/** Producer product identity is observed separately from the unknown host platform. */
export const webRuntimeBrowserSchema = browserVersionSchema;
/** Complete runtime evidence has explicit byte budgets rather than silent item truncation. */
export const WEB_RUNTIME_LIMITS = {
protocolBytes: 64 * 1024 * 1024,
retainedEventBytes: 8 * 1024 * 1024,
sourceBytes: 32 * 1024 * 1024,
commandTimeoutMs: 20_000,
cleanupTimeoutMs: 5_000,
} as const;