fix(electron): bound hook retention and report dropped coverage

This commit is contained in:
morluto
2026-10-09 05:15:23 +08:00
parent e714fb027a
commit 02d0702761
11 changed files with 658 additions and 192 deletions
@@ -9,6 +9,19 @@ import {
import { createElectronActiveObservationFixtureResult } from "../domain/javascript/electronActiveObservation.fixture.js";
import { runElectronActions } from "./PlaywrightElectronActiveActions.js";
it("keeps legacy Electron results explicit about unavailable retention data", () => {
const { retention: _retention, ...legacyResult } =
createElectronActiveObservationFixtureResult("/opt/app");
const parsed = electronActiveObservationResultSchema.parse({
...legacyResult,
ipc: { ...legacyResult.ipc, dropped: undefined },
timeline: { ...legacyResult.timeline, dropped: undefined },
});
expect(parsed.retention).toBeNull();
expect(parsed.ipc.dropped).toBeNull();
expect(parsed.timeline.dropped).toBeNull();
});
it("parses window, renderer, and deep-link actions for agents", () => {
const input = electronActiveObservationInputSchema.parse({
executable_path: "/opt/electron",
@@ -68,6 +68,25 @@ const hookEventSchema = z.strictObject({
const hookSnapshotSchema = z.strictObject({
events: z.array(hookEventSchema),
retention_budget_bytes: z.number().int().min(0),
estimated_retained_bytes: z.number().int().min(0),
event_serialized_byte_upper_bound: z.number().int().min(0),
retained: z.number().int().min(0),
dropped: z.number().int().min(0),
dropped_ipc: z.number().int().min(0),
dropped_runtime: z.number().int().min(0),
dropped_event_families: z.array(
z.strictObject({
family: z.string().min(1),
count: z.number().int().min(1),
}),
),
dropped_event_roles: z.array(
z.strictObject({
role: z.string().min(1),
count: z.number().int().min(1),
}),
),
observed: z.number().int().min(0),
observed_ipc: z.number().int().min(0),
observed_runtime: z.number().int().min(0),
@@ -357,6 +376,15 @@ export const readApplicationState = async (
? candidate()
: {
events: [],
retention_budget_bytes: 0,
estimated_retained_bytes: 0,
event_serialized_byte_upper_bound: 0,
retained: 0,
dropped: 0,
dropped_ipc: 0,
dropped_runtime: 0,
dropped_event_families: [],
dropped_event_roles: [],
observed: 0,
observed_ipc: 0,
observed_runtime: 0,
@@ -0,0 +1,129 @@
import { expect, it } from "vitest";
import { projectElectronActiveCapture } from "./PlaywrightElectronActiveProjection.js";
it("projects hook retention and dropped event families through the result schema", () => {
const event = {
sequence: 1,
correlation_id: "capture:123:1",
kind: "main-handler-invocation" as const,
event: null,
phase: "observed" as const,
channel: "readiness:echo",
direction: "renderer-to-main" as const,
sender: null,
receiver: "main",
frame: null,
target: null,
argument_shapes: [],
result_shape: null,
process_type: "main",
source: "electron-active-hook",
capture_method: "api-wrapper" as const,
artifact_path: null,
artifact_sha256: null,
error: false,
};
const result = projectElectronActiveCapture(
{
executable: "/opt/electron",
application: "/opt/app/main.js",
},
[],
{
windows: [],
metrics: [],
electronVersion: "test-electron",
hookSnapshot: {
events: [event],
retention_budget_bytes: 16 * 1024 * 1024,
estimated_retained_bytes: 1_024,
event_serialized_byte_upper_bound: 512,
retained: 1,
dropped: 1,
dropped_ipc: 0,
dropped_runtime: 1,
dropped_event_families: [{ family: "renderer-ipc", count: 1 }],
dropped_event_roles: [{ role: "renderer", count: 1 }],
observed: 2,
observed_ipc: 1,
observed_runtime: 1,
hook_error: false,
},
},
);
expect(result.retention).toMatchObject({
budget_bytes: 16 * 1024 * 1024,
estimated_retained_bytes: 1_024,
event_serialized_byte_upper_bound: 512,
retained: 1,
dropped: 1,
dropped_event_families: [{ family: "renderer-ipc", count: 1 }],
dropped_event_roles: [{ role: "renderer", count: 1 }],
});
expect(result.ipc).toMatchObject({ observed: 1, dropped: 0 });
expect(result.timeline).toMatchObject({ observed: 2, dropped: 1 });
expect(result.coverage.observed_event_families).toContain("renderer-ipc");
expect(result.coverage.unavailable_event_families).not.toContain(
"renderer-ipc",
);
expect(result.coverage.observed_roles).toContain("renderer");
expect(result.coverage.unavailable_event_families).toContain("preload");
});
it("keeps renderer coverage unavailable when only the main process was observed", () => {
const result = projectElectronActiveCapture(
{
executable: "/opt/electron",
application: "/opt/app/main.js",
},
[],
{
windows: [],
metrics: [],
electronVersion: "test-electron",
hookSnapshot: {
events: [
{
sequence: 1,
correlation_id: "capture:123:1",
kind: "app-lifecycle",
event: "ready",
phase: "observed",
channel: null,
direction: null,
sender: null,
receiver: null,
frame: null,
target: "app",
argument_shapes: [],
result_shape: null,
process_type: "main",
source: "electron-active-hook",
capture_method: "event-emitter",
artifact_path: null,
artifact_sha256: null,
error: false,
},
],
retention_budget_bytes: 16 * 1024 * 1024,
estimated_retained_bytes: 0,
event_serialized_byte_upper_bound: 0,
retained: 1,
dropped: 0,
dropped_ipc: 0,
dropped_runtime: 0,
dropped_event_families: [],
dropped_event_roles: [],
observed: 1,
observed_ipc: 0,
observed_runtime: 1,
hook_error: false,
},
},
);
expect(result.coverage.observed_roles).not.toContain("renderer");
expect(result.coverage.unavailable_event_families).toContain("renderer-ipc");
});
@@ -0,0 +1,178 @@
import {
electronActiveObservationResultSchema,
type ElectronActiveObservationResult,
} from "../domain/javascript/electronActiveObservation.js";
import type {
ElectronHookEvent,
ElectronHookSnapshot,
ElectronMetrics,
} from "./PlaywrightElectronActiveActions.js";
type ElectronCapturePaths = {
readonly executable: string;
readonly application: string;
};
type ElectronCaptureState = {
readonly windows: ReadonlyArray<
ElectronActiveObservationResult["windows"][number]
>;
readonly metrics: ElectronMetrics;
readonly electronVersion: string;
readonly hookSnapshot: ElectronHookSnapshot;
};
const observableEventFamilies = [
"app-lifecycle",
"window-lifecycle",
"web-contents-lifecycle",
"navigation",
"shell-attempt",
"process-lifecycle",
"permission",
"popup-attempt",
"download",
"protocol",
"preload",
"preload-configuration",
"renderer-ipc",
"native-addon",
"updater",
"error",
"ipc",
] as const;
const ipcEventKinds = new Set<ElectronHookEvent["kind"]>([
"main-handler-invocation",
"main-event-invocation",
"utility-process-fork",
"utility-process-message",
"ipc-main-to-renderer",
"ipc-utility-to-main",
"ipc-renderer-send",
"ipc-renderer-invoke",
"ipc-renderer-post-message",
]);
const eventFamily = (
kind: ElectronHookEvent["kind"],
processType: string | null,
): string => {
if (kind === "preload" && processType === "main")
return "preload-configuration";
if (kind.startsWith("ipc-renderer"))
return processType === "renderer" ? "renderer-ipc" : "ipc";
return ipcEventKinds.has(kind) ? "ipc" : kind;
};
const createCoverage = (hookSnapshot: ElectronHookSnapshot) => {
const observedEventFamilies: string[] = [
...new Set([
...hookSnapshot.events.map(({ kind, process_type }) =>
eventFamily(kind, process_type),
),
...hookSnapshot.dropped_event_families.map(({ family }) => family),
]),
].sort();
const unavailableEventFamilies = new Set<string>(
observableEventFamilies.filter(
(family) => !observedEventFamilies.includes(family),
),
);
const observedRoles = [
...new Set([
...hookSnapshot.events.flatMap(({ process_type, kind }) => [
...(process_type === null ? [] : [process_type]),
...(kind === "window-lifecycle" ? ["window"] : []),
...(kind === "web-contents-lifecycle" || kind === "navigation"
? ["web_contents"]
: []),
...(kind === "preload" && process_type === "preload"
? ["preload"]
: []),
...(kind.startsWith("ipc-renderer") && process_type === "renderer"
? ["renderer"]
: []),
]),
...hookSnapshot.dropped_event_roles.map(({ role }) => role),
]),
].sort();
if (!observedRoles.includes("preload"))
unavailableEventFamilies.add("preload");
if (!observedRoles.includes("renderer"))
unavailableEventFamilies.add("renderer-ipc");
return {
status: hookSnapshot.hook_error ? "hook_conflict" : "partial_attach",
observed_event_families: observedEventFamilies,
unavailable_event_families: [...unavailableEventFamilies].sort(),
observed_roles: observedRoles,
pre_capture_activity: "unavailable",
} as const;
};
/** Project one provider hook snapshot into the canonical public result. */
export const projectElectronActiveCapture = (
paths: ElectronCapturePaths,
actions: ElectronActiveObservationResult["actions"],
state: ElectronCaptureState,
): ElectronActiveObservationResult => {
const { hookSnapshot } = state;
const ipcEvents = hookSnapshot.events.filter(({ kind }) =>
ipcEventKinds.has(kind),
);
return electronActiveObservationResultSchema.parse({
application: {
executable_path: paths.executable,
application_path: paths.application,
electron_version: state.electronVersion,
process_ownership: "provider-owned",
cleanup: "terminated-owned-process",
},
actions,
windows: state.windows,
processes: {
items: state.metrics,
},
ipc: {
events: ipcEvents,
observed: hookSnapshot.observed_ipc,
dropped: hookSnapshot.dropped_ipc,
},
timeline: {
events: hookSnapshot.events,
observed: hookSnapshot.observed,
dropped: hookSnapshot.dropped,
},
retention: {
budget_bytes: hookSnapshot.retention_budget_bytes,
estimated_retained_bytes: hookSnapshot.estimated_retained_bytes,
event_serialized_byte_upper_bound:
hookSnapshot.event_serialized_byte_upper_bound,
retained: hookSnapshot.retained,
dropped: hookSnapshot.dropped,
dropped_event_families: hookSnapshot.dropped_event_families,
dropped_event_roles: hookSnapshot.dropped_event_roles,
observed_ipc: hookSnapshot.observed_ipc,
dropped_ipc: hookSnapshot.dropped_ipc,
observed_runtime: hookSnapshot.observed_runtime,
dropped_runtime: hookSnapshot.dropped_runtime,
},
coverage: createCoverage(hookSnapshot),
limitations: [
"IPC payloads are represented by value shapes; payload values are never retained.",
"IPC direction and sender/receiver identifiers are observed only where Electron exposes them at the hooked boundary.",
"The runtime timeline records lifecycle, navigation, shell, permission, popup, download, protocol, preload, native-addon, process, and IPC events; activity before hook installation is unavailable.",
"The preload and renderer process contexts are not instrumented by the main-process -r hook; preload configuration and contextBridge API-shape events are main-boundary observations, not proof of renderer-side execution.",
"Process metrics are an Electron API snapshot and do not prove hostile-local-user isolation.",
...(hookSnapshot.dropped > 0
? [
`The active hook retained ${hookSnapshot.retained} of ${hookSnapshot.observed} observed events within a ${hookSnapshot.retention_budget_bytes}-byte retained-data budget; ${hookSnapshot.dropped} events were dropped (${hookSnapshot.dropped_ipc} IPC, ${hookSnapshot.dropped_runtime} runtime).`,
]
: []),
"External shell opens, external navigation, permission grants, downloads, popup windows, updater relaunches, and OS integration are blocked and recorded by the active hook; other application filesystem and network behavior is not sandboxed by this provider.",
...(hookSnapshot.hook_error
? ["The active IPC hook could not be installed."]
: []),
],
});
};
+5 -149
View File
@@ -10,10 +10,9 @@ import type {
ProviderIdentity,
} from "../application/AnalysisProvider.js";
import type { ElectronActiveObservationPort } from "../application/javascript/ElectronActiveObservationPort.js";
import {
electronActiveObservationResultSchema,
type ElectronActiveObservationInput,
type ElectronActiveObservationResult,
import type {
ElectronActiveObservationInput,
ElectronActiveObservationResult,
} from "../domain/javascript/electronActiveObservation.js";
import { AnalysisError } from "../domain/analysisErrorBase.js";
import { BrowserObservationError } from "../domain/browserObservationError.js";
@@ -38,10 +37,8 @@ import {
runElectronActions,
readApplicationState,
runWithExecutionLimits,
type ElectronHookEvent,
type ElectronHookSnapshot,
type ElectronMetrics,
} from "./PlaywrightElectronActiveActions.js";
import { projectElectronActiveCapture } from "./PlaywrightElectronActiveProjection.js";
const OPERATION = "capture_electron_scenario" as const;
const STARTUP_TIMEOUT_MS = 60_000;
@@ -61,89 +58,6 @@ type ElectronPaths = {
readonly root: string;
};
const observableEventFamilies = [
"app-lifecycle",
"window-lifecycle",
"web-contents-lifecycle",
"navigation",
"shell-attempt",
"process-lifecycle",
"permission",
"popup-attempt",
"download",
"protocol",
"preload",
"preload-configuration",
"renderer-ipc",
"native-addon",
"updater",
"error",
"ipc",
] as const;
const ipcEventKinds = new Set<ElectronHookEvent["kind"]>([
"main-handler-invocation",
"main-event-invocation",
"utility-process-fork",
"utility-process-message",
"ipc-main-to-renderer",
"ipc-utility-to-main",
"ipc-renderer-send",
"ipc-renderer-invoke",
"ipc-renderer-post-message",
]);
const createCoverage = (hookSnapshot: ElectronHookSnapshot) => {
const observedEventFamilies: string[] = [
...new Set(
hookSnapshot.events.map(({ kind, process_type }) => {
if (kind === "preload" && process_type === "main")
return "preload-configuration";
if (kind.startsWith("ipc-renderer") && process_type !== "renderer")
return "ipc";
return ipcEventKinds.has(kind) ? "ipc" : kind;
}),
),
].sort();
const unavailableEventFamilies = new Set(
observableEventFamilies.filter(
(family) => !observedEventFamilies.includes(family),
),
);
const observedRoles = [
...new Set(
hookSnapshot.events.flatMap(({ process_type, kind }) => [
...(process_type === null ? [] : [process_type]),
...(kind === "window-lifecycle" ? ["window"] : []),
...(kind === "web-contents-lifecycle" || kind === "navigation"
? ["web_contents"]
: []),
...(kind === "preload" && process_type === "preload"
? ["preload"]
: []),
...(kind.startsWith("ipc-renderer") && process_type === "renderer"
? ["renderer"]
: []),
]),
),
].sort();
const rendererContextObserved = hookSnapshot.events.some(
({ process_type }) =>
process_type === "renderer" || process_type === "preload",
);
if (!rendererContextObserved) {
unavailableEventFamilies.add("preload");
unavailableEventFamilies.add("renderer-ipc");
}
return {
status: hookSnapshot.hook_error ? "hook_conflict" : "partial_attach",
observed_event_families: observedEventFamilies,
unavailable_event_families: [...unavailableEventFamilies].sort(),
observed_roles: observedRoles,
pre_capture_activity: "unavailable",
} as const;
};
/** Launch an owned Electron application through the official Playwright API. */
export class PlaywrightElectronActiveProvider implements ElectronActiveObservationPort {
identity(): ProviderIdentity {
@@ -200,7 +114,7 @@ export class PlaywrightElectronActiveProvider implements ElectronActiveObservati
readApplicationState(application),
options.signal,
);
outcome = ok(createResult(paths, input, actions, state));
outcome = ok(projectElectronActiveCapture(paths, actions, state));
} catch (cause: unknown) {
outcome = err(
options.signal?.aborted === true &&
@@ -339,64 +253,6 @@ const cleanupElectronProcesses = async (
};
};
const createResult = (
paths: ElectronPaths,
input: ElectronActiveObservationInput,
actions: ElectronActiveObservationResult["actions"],
state: {
readonly windows: ReadonlyArray<{
readonly window_id: string;
readonly web_contents_id: string;
readonly url: string;
readonly title: string;
readonly visible: boolean | null;
readonly destroyed: boolean;
}>;
readonly metrics: ElectronMetrics;
readonly electronVersion: string;
readonly hookSnapshot: ElectronHookSnapshot;
},
): ElectronActiveObservationResult => {
const { hookSnapshot } = state;
const ipcEvents = hookSnapshot.events.filter(({ kind }) =>
ipcEventKinds.has(kind),
);
return electronActiveObservationResultSchema.parse({
application: {
executable_path: paths.executable,
application_path: paths.application,
electron_version: state.electronVersion,
process_ownership: "provider-owned",
cleanup: "terminated-owned-process",
},
actions,
windows: state.windows,
processes: {
items: state.metrics,
},
ipc: {
events: ipcEvents,
observed: hookSnapshot.observed_ipc,
},
timeline: {
events: hookSnapshot.events,
observed: hookSnapshot.observed,
},
coverage: createCoverage(hookSnapshot),
limitations: [
"IPC payloads are represented by value shapes; payload values are never retained.",
"IPC direction and sender/receiver identifiers are observed only where Electron exposes them at the hooked boundary.",
"The runtime timeline records lifecycle, navigation, shell, permission, popup, download, protocol, preload, native-addon, process, and IPC events; activity before hook installation is unavailable.",
"The preload and renderer process contexts are not instrumented by the main-process -r hook; preload configuration and contextBridge API-shape events are main-boundary observations, not proof of renderer-side execution.",
"Process metrics are an Electron API snapshot and do not prove hostile-local-user isolation.",
"External shell opens, external navigation, permission grants, downloads, popup windows, updater relaunches, and OS integration are blocked and recorded by the active hook; other application filesystem and network behavior is not sandboxed by this provider.",
...(hookSnapshot.hook_error
? ["The active IPC hook could not be installed."]
: []),
],
});
};
const canonicalPaths = async (
input: ElectronActiveObservationInput,
): Promise<ElectronPaths> => {
@@ -49,6 +49,20 @@ export const createElectronActiveObservationFixtureResult = (
},
],
observed: 1,
dropped: 0,
},
retention: {
budget_bytes: 16 * 1024 * 1024,
estimated_retained_bytes: 768,
event_serialized_byte_upper_bound: 512,
retained: 1,
dropped: 0,
dropped_event_families: [],
dropped_event_roles: [],
observed_ipc: 1,
dropped_ipc: 0,
observed_runtime: 0,
dropped_runtime: 0,
},
limitations: [
"IPC payloads are represented by value shapes; payload values are never retained.",
@@ -262,13 +262,41 @@ export const electronActiveObservationResultSchema = z.strictObject({
ipc: z.strictObject({
events: z.array(ipcEventSchema),
observed: z.number().int().min(0),
dropped: z.number().int().min(0).nullable().default(null),
}),
timeline: z
.strictObject({
events: z.array(timelineEventSchema),
observed: z.number().int().min(0),
dropped: z.number().int().min(0).nullable().default(null),
})
.default({ events: [], observed: 0 }),
.default({ events: [], observed: 0, dropped: null }),
retention: z
.strictObject({
budget_bytes: z.number().int().min(0),
estimated_retained_bytes: z.number().int().min(0),
event_serialized_byte_upper_bound: z.number().int().min(0),
retained: z.number().int().min(0),
dropped: z.number().int().min(0),
dropped_event_families: z.array(
z.strictObject({
family: z.string().min(1),
count: z.number().int().min(1),
}),
),
dropped_event_roles: z.array(
z.strictObject({
role: z.string().min(1),
count: z.number().int().min(1),
}),
),
observed_ipc: z.number().int().min(0),
dropped_ipc: z.number().int().min(0),
observed_runtime: z.number().int().min(0),
dropped_runtime: z.number().int().min(0),
})
.nullable()
.default(null),
coverage: coverageSchema.default({
status: "partial_attach",
observed_event_families: [],