Files
reverse-engineering-anything/src/browser/PlaywrightElectronActiveProvider.test.ts
T
morluto cb49383269 refactor(browser): take the host in Chromium, Electron and capture adapters (#11130)
* refactor(browser): take the host in Chromium, Electron and capture adapters

chromiumSandboxEnabled and safeElectronEnvironment take a required platform. The scenario browser, native module resolver, Electron provider and capture cleanup, mitmproxy adapter and web script artifacts take an optional platform seam (default HOST_PLATFORM); Electron fuses read the host architecture from HOST_PLATFORM. Their tests inject the platform instead of redefining process.platform. src/browser no longer reads process.platform or process.arch.

* refactor(pcap): take the host platform in the packet tools, native bound and batch launch (#11141)

* refactor(pcap): take the host platform in the packet tools, native bound and batch launch

* test(pcap): cover the selected host in batch launch and capture-host support

* refactor(apktool): take the host platform in launcher selection, version probe and decode (#11164)

* refactor(apktool): take the host platform in launcher selection, version probe and decode

* test(apktool): cover the provider's selected host in launcher selection

* refactor(hopper): give the launcher the provider's host platform

* refactor(native): inject the host into the pwntools decoders and native capability defaults (#11173)

* refactor(native): inject the host into the pwntools decoders and native capability defaults

* fix(native): give composed pwntools services the capability host and drop empty test hooks

* refactor(adb): take the host platform in binary selection and pulled file names (#11176)

* refactor(adb): take the host platform in binary selection and pulled file names

* refactor(cli): pass the resolved host platform through rea mcp add (#11179)

* refactor(cli): pass the resolved host platform through rea mcp add

* refactor(composition): default composition entry points to HOST_PLATFORM and pass it to Hopper and IDA (#11183)

* refactor(kuna): require the host platform in admission and installation, and give it to the session client (#11186)

* refactor(kuna): require the host platform in admission and installation, and give it to the session client

* fix(kuna): pass the host platform from the JavaScript installation callers

verify-real-kuna, both native oracle helpers and the command-lifetime child script call inspectKunaInstallation from dist, where TypeScript cannot see the now-required platform. They passed undefined and got unsupported_host on supported hosts.

* refactor(server): give createServer a host option and pass it to session tools, sigrok and EVM (#11203)

* refactor(server): give createServer a host option and pass it to session tools, sigrok and EVM

createServer read process.platform and process.arch live for its capability services, and registerSessionTools read process.platform for process capture. A host option (default HOST_PLATFORM) now selects both, and composition passes the capability host to the sigrok provider and the EVM provider (execution gate, limiter probe and environment rules), as it does for pwntools.

hostRestrictedAvailabilityMcp selects its host through the option instead of redefining process.platform and process.arch, and moves to the MCP boundary lane since it no longer touches process-global state. evmCleanupRetry passes SIMULATED_LINUX_X64 to the provider instead of 52 property redefinitions; the fixture moves to tests/fixtures/simulatedHost.ts.

* refactor(desktop): require the host platform in build computer and secret folder paths (#11210)

* refactor(desktop): require the host platform in build computer and secret folder paths

buildComputerArchiveDirectory, defaultBuildComputerArchive, resolveBuildComputerEnvironment and buildSecretsDirectory defaulted to process.platform. Every caller but DesktopBuildComputers' secret folder already passed the platform; it now passes HOST_PLATFORM.platform, which it reads for the build environment.

* refactor(filesystem): default caller paths and ChangeTime settling to HOST_PLATFORM (#11212)

appendCallerPath, absoluteCallerPath and the ChangeTime settle seams defaulted to a live process.platform read. Their defaults now come from HOST_PLATFORM: the 59 callers that omit the platform join or settle paths they open on this host, and callers describing another host's layout already pass it.

The webScriptExport case that redefined process.platform to reach the Windows settle budget moves to its own file, which selects Windows by mocking HostPlatform.

* fix(server): report process capture availability for the server's host

sessionAvailabilityPolicy imported platform from node:process, so with a selected host binary_session reported capture_process_scenario from the running host while planning and capture used the selected one. It takes the platform from createServer and registerSessionTools. hostRestrictedAvailabilityMcp checks discovery against a dry-run plan on win32 and linux hosts.

* build(guards): count platform and arch imports from node:process and node:os as ambient host reads

sessionAvailabilityPolicy's named import of platform from node:process escaped the ambient-host-platform guard. The guards now also match named platform/arch imports from node:process or node:os and os.platform()/os.arch(). The one other site, DesktopApplicationCapture's os.arch(), records the capturing host and now reads HOST_PLATFORM.architecture.

* refactor(config): take the host platform in parseConfig and give debugger managers the capability host (#11237)

* refactor(config): take the host platform in parseConfig and give debugger managers the capability host

parseConfig read process.platform for the default Hopper launcher; it takes the platform (default HOST_PLATFORM). GdbSessionManager and RizinDebugSessionManager default their platform to HOST_PLATFORM, and composition passes the capability host that already decides their availability. GDB's /proc descriptor check reads HOST_PLATFORM explicitly: it inspects this host's /proc whatever host the manager models.

* refactor(firmware): give the firmware and Wakaru providers the capability host (#11239)

* refactor(firmware): give the firmware and Wakaru providers the capability host

resolveFirmwareCommand and resolveWakaruCommand read process.platform (and process.arch) for their host gate. They take the platform or host as a parameter; FirmwareProvider and WakaruProvider take a host (default HOST_PLATFORM) for the gate and their environment snapshots, and composition passes the capability host that already decides their availability. The JavaScript analysis source transfer defaults to HOST_PLATFORM.

artifactSelectedCommand passes SIMULATED_LINUX_X64 to both providers instead of redefining process.platform and process.arch, and the Wakaru limiter configuration cases now run on any POSIX host.

* refactor(captures): give tshark the capability host and default Cutter to HOST_PLATFORM (#11240)

* refactor(captures): give tshark the capability host and default Cutter to HOST_PLATFORM

TsharkCaptureProvider and CutterBridgeClient defaulted their platform to process.platform. Both default to HOST_PLATFORM now, and composition passes the capability host to the packet capture service, which already decides its availability.

* refactor(platform): read HOST_PLATFORM explicitly in host-only path checks (#11241)

* refactor(platform): read HOST_PLATFORM explicitly in host-only path checks

Electron's readlink target split, IDA's host path comparison, the inspector's reported runtime path, the server identity check and WABT's executable names all describe paths on the running host. They read HOST_PLATFORM instead of process.platform.

* test(native): select the native UI verifier's host through HostPlatform (#11242)

verify-native-ui.mjs reads its macOS requirement from HOST_PLATFORM, and its action test resolves the script's HostPlatform import to a simulated darwin host instead of redefining process.platform in the child preload. Simulating linux there fails the macOS requirement, so the redirect is effective.

* test(mcp): check that composition forwards the selected host to execution

hostRestrictedAvailabilityMcp ran execution only on darwin, where the runner's own host also refuses. The new case calls the firmware, Wakaru, tshark, sigrok and EVM tools under a darwin and a linux x64 host and requires different outcomes, so dropping a provider's host forwarding fails on any runner (checked by removing the firmware forwarding).

* fix(composition): give the pcap, Apktool and ADB providers the capability host

createServer's selected host decided pcap capture discovery, but the default pcap, Apktool and ADB factories built their providers with HOST_PLATFORM. The factories take the platform (default HOST_PLATFORM) and capabilityServices passes its context platform, so availability and execution follow one host.

* test: import moved resources owners in host-platform fixtures

* refactor(config): drop the unused host parameter from parseConfig
2026-11-16 04:27:07 +08:00

1610 lines
51 KiB
TypeScript

import {
ProcessOwnershipInspectionCancelled,
ProcessOwnershipIdentityBatchFailure,
} from "../process/ProcessInspectionCancellation.js";
import { projectAnalysisError } from "../domain/analysisErrorProjection.js";
import { primaryAnalysisError } from "../domain/analysisErrorBase.js";
import { AnalysisOutputError } from "../domain/analysisErrorCore.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
import { ExecFileOutputCancelledError } from "../process/ExecFileOutput.js";
import * as processOutput from "../process/ExecFileOutput.js";
import { cleanupElectronProcesses } from "./ElectronProcessCleanup.js";
import { afterEach, beforeEach, expect, it, vi } from "vitest";
import { writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
import { createHash } from "node:crypto";
import { fileURLToPath } from "node:url";
import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
import {
OBSIDIAN_FUSE_BYTES,
thinFuseImage,
} from "../../tests/fixtures/electron/fuseImage.js";
import * as processOwnership from "../process/ProcessOwnership.js";
import { ProcessOwnershipEvidenceOwner } from "../process/ProcessOwnershipEvidence.js";
import * as ownershipObservation from "../process/ProcessOwnershipObservation.js";
import * as electronActions from "./PlaywrightElectronActiveActions.js";
import { electronActiveObservationInputSchema } from "../domain/javascript/electronActiveObservation.js";
import type { PersistentOwnedProcessGroup } from "../process/ProcessOwnership.js";
import {
PlaywrightElectronActiveProvider,
safeElectronEnvironment,
} from "./PlaywrightElectronActiveProvider.js";
import { waitFor } from "../../tests/support/testWait.js";
import { releaseOwnedCleanup } from "../../tests/support/cleanupRelease.js";
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllEnvs();
});
it.each([false, true])(
"retains late cancellation beside a failed capture (cleanup fails: %s)",
async (cleanupFails) => {
stubVerifiedElectronCleanup();
stubCompletedCapture();
const controller = new AbortController();
const reason = { caller: "cancel during final cleanup" };
const failure = new AnalysisOutputError(
"capture_electron_scenario",
"final snapshot unavailable",
);
vi.spyOn(electronActions, "readFinalApplicationState").mockRejectedValue(
failure,
);
let denied = cleanupFails;
const application = {
process: () => ({ pid: 424242 }),
windows: () => [],
close: vi.fn(async () => {
controller.abort(reason);
if (denied) throw new Error("transport cleanup denied");
}),
};
const provider = new PlaywrightElectronActiveProvider(
{},
async () =>
application as unknown as Awaited<
ReturnType<typeof import("playwright-core")._electron.launch>
>,
);
try {
const result = await provider.capture(
{
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
}),
application_root: dirname(process.execPath),
},
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected capture failure");
expect(primaryAnalysisError(result.error)).toBe(failure);
expect(
failure.secondaryFailures.some(
(secondary) => secondary.cause === reason,
),
).toBe(true);
expect(result.error.partialObservation).toMatchObject({
kind: "electron-active-observation",
});
denied = false;
await provider.close();
expect(result.error.cleanupIncomplete).toBe(false);
expect(primaryAnalysisError(result.error)).toBe(failure);
} finally {
denied = false;
await provider.close();
}
},
);
it.each(["prepare", "baseline"] as const)(
"classifies cancellation from ownership %s without masking physical failure",
async (stage) => {
for (const kind of [
"reason",
"normalized-exec",
"physical",
"same-reason",
] as const) {
const controller = new AbortController();
const physical = new Error("ownership inspection failed");
const reason = kind === "same-reason" ? physical : { cancelled: stage };
const launch = vi.fn(async () => {
throw new Error("must not launch");
});
const fail = async () => {
controller.abort(reason);
if (kind === "normalized-exec")
throw ProcessOwnershipInspectionCancelled.from(
new ExecFileOutputCancelledError(reason),
);
throw kind === "reason"
? new ProcessOwnershipInspectionCancelled(reason)
: physical;
};
const preparation =
stage === "baseline"
? vi
.spyOn(ownershipObservation, "prepareProcessOwnershipInspection")
.mockResolvedValue()
: undefined;
const spy =
stage === "prepare"
? vi
.spyOn(ownershipObservation, "prepareProcessOwnershipInspection")
.mockImplementation(fail)
: vi
.spyOn(
ownershipObservation.systemProcessOwnershipHost,
"captureBaseline",
)
.mockImplementation(fail);
const provider = new PlaywrightElectronActiveProvider({}, launch);
try {
const result = await provider.capture(
{
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
}),
application_root: dirname(process.execPath),
},
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected inspection failure");
expect(launch).not.toHaveBeenCalled();
if (kind === "physical" || kind === "same-reason") {
expect(result.error.userCategory).not.toBe("cancelled");
expect(primaryAnalysisError(result.error).cause).toBe(physical);
expect(
primaryAnalysisError(result.error).secondaryFailures.some(
(failure) => failure.cause === reason,
),
).toBe(true);
} else {
expect(projectAnalysisError(result.error).code).toBe("cancelled");
expect(result.error.cause).toBe(reason);
}
} finally {
spy.mockRestore();
preparation?.mockRestore();
await provider.close();
}
}
},
);
it("preserves an independent AbortError from actual ownership baseline inspection", async () => {
const controller = new AbortController();
const reason = { caller: "cancel" };
const physical = new DOMException(
"independent inspector failure",
"AbortError",
);
vi.spyOn(processOutput, "execFileOutput").mockImplementation(async () => {
controller.abort(reason);
throw physical;
});
const host = ownershipObservation.createSystemProcessOwnershipHost(
"linux",
{},
);
const prepare = vi
.spyOn(ownershipObservation, "prepareProcessOwnershipInspection")
.mockResolvedValue();
const baseline = vi
.spyOn(ownershipObservation.systemProcessOwnershipHost, "captureBaseline")
.mockImplementation((signal) => host.captureBaseline!(signal));
const provider = new PlaywrightElectronActiveProvider({});
try {
const result = await provider.capture(
{
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
}),
application_root: dirname(process.execPath),
},
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected inspection failure");
const primary = primaryAnalysisError(result.error);
expect(primary.userCategory).not.toBe("cancelled");
expect(primary.cause).toBe(physical);
expect(
primary.secondaryFailures.some((failure) => failure.cause === reason),
).toBe(true);
} finally {
baseline.mockRestore();
prepare.mockRestore();
await provider.close();
}
});
it("retains failed ownership-helper cleanup until provider close retries it", async () => {
const controller = new AbortController();
const reason = { caller: "cancel" };
const cleanup = new ProviderCleanupError(
"process-ownership",
["helper-root"],
{},
{ operation: "prepare_owned_process" },
);
const owner = {
close: vi.fn(async () => {
releaseOwnedCleanup(cleanup);
}),
};
const prepare = vi
.spyOn(ownershipObservation, "prepareProcessOwnershipInspection")
.mockImplementation(async () => {
controller.abort(reason);
throw new DarwinProcessOwnershipInspectionError("helper cleanup failed", {
cancellation: { reason },
cleanupFailure: cleanup,
cleanupOwner: owner,
});
});
const launch = vi.fn(async () => {
throw new Error("must not launch");
});
const provider = new PlaywrightElectronActiveProvider({}, launch);
try {
const result = await provider.capture(
{
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
}),
application_root: dirname(process.execPath),
},
{ signal: controller.signal },
);
if (result.ok) throw new Error("Expected failed cleanup");
expect(result.error.cleanupIncomplete).toBe(true);
expect(result.error.cleanupResources).toContain("helper-root");
await provider.close();
expect(owner.close).toHaveBeenCalledOnce();
expect(result.error.cleanupIncomplete).toBe(false);
} finally {
prepare.mockRestore();
await provider.close();
}
});
it("joins a late launch acknowledgement and its cleanup after shutdown", async () => {
stubVerifiedElectronCleanup();
stubCompletedCapture();
type Application = Awaited<
ReturnType<typeof import("playwright-core")._electron.launch>
>;
let acknowledge = (_application: Application): void => undefined;
let started = (): void => undefined;
const launching = new Promise<Application>((resolve) => {
acknowledge = resolve;
});
const launchStarted = new Promise<void>((resolve) => {
started = resolve;
});
const application = {
process: () => ({ pid: 424242 }),
windows: () => [],
close: vi.fn(async () => undefined),
};
const provider = new PlaywrightElectronActiveProvider({}, async () => {
started();
return launching;
});
const controller = new AbortController();
const reason = { caller: "cancel pending launch" };
const capture = provider.capture(
{
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
}),
application_root: dirname(process.execPath),
},
{ signal: controller.signal },
);
await launchStarted;
controller.abort(reason);
let closed = false;
const closing = provider.close().then(() => {
closed = true;
});
await new Promise<void>((resolve) => setImmediate(resolve));
expect(closed).toBe(false);
acknowledge(application as unknown as Application);
const result = await capture;
await closing;
expect(application.close).toHaveBeenCalledOnce();
expect(closed).toBe(true);
if (result.ok) throw new Error("Expected launch cancellation");
expect(projectAnalysisError(result.error).code).toBe("cancelled");
expect(result.error.cause).toBe(reason);
});
it("passes the selected environment and ownership token to Electron launch", async () => {
vi.stubEnv("LANG", "ambient-language");
vi.stubEnv("XAUTHORITY", "/ambient/.Xauthority");
let launchedEnvironment: unknown;
const callerArguments = [
"--",
"/caller/plugin.asar",
"-r",
"caller-hook.cjs",
"\\server\\share\\".repeat(150),
];
const launchPlan = [
"-r",
fileURLToPath(
new URL("../../scripts/electron-active-hook.cjs", import.meta.url),
),
process.execPath,
...callerArguments,
];
const handoff = JSON.stringify({
length: launchPlan.length,
sha256: createHash("sha256")
.update(JSON.stringify(launchPlan))
.digest("hex"),
});
const provider = new PlaywrightElectronActiveProvider(
{
LANG: "selected-language",
PATH: "/selected/bin",
DISPLAY: ":selected-display",
XAUTHORITY: "/selected/display-authorization",
WAYLAND_DISPLAY: "selected-wayland",
REA_PRIVATE_VALUE: "not-a-launch-setting",
REA_ELECTRON_CAPTURE_ARGV: "forged ambient launch plan",
},
async (options) => {
launchedEnvironment = options?.env;
throw new Error("launch boundary reached");
},
);
const result = await provider.capture({
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
args: callerArguments,
}),
application_root: dirname(process.execPath),
});
expect(result.ok).toBe(false);
expect(launchedEnvironment).toEqual({
LANG: "selected-language",
PATH: "/selected/bin",
DISPLAY: ":selected-display",
XAUTHORITY: "/selected/display-authorization",
WAYLAND_DISPLAY: "selected-wayland",
REA_PROCESS_RUN_ID: expect.any(String),
REA_ELECTRON_CAPTURE_ARGV: handoff,
});
expect(handoff.length).toBeLessThan(128);
});
it("passes required Windows runtime variables without inheriting unrelated values", () => {
expect(
safeElectronEnvironment(
{
SYSTEMROOT: "C:\\Windows",
LOCALAPPDATA: "C:\\Users\\analyst\\AppData\\Local",
USERPROFILE: "C:\\Users\\analyst",
APP_SECRET: "must-not-pass",
},
"run-id",
"win32",
),
).toEqual({
SYSTEMROOT: "C:\\Windows",
LOCALAPPDATA: "C:\\Users\\analyst\\AppData\\Local",
USERPROFILE: "C:\\Users\\analyst",
REA_PROCESS_RUN_ID: "run-id",
});
});
let ownership: PersistentOwnedProcessGroup;
beforeEach(() => {
// Fake applications have no hook to announce a refused lock.
vi.spyOn(electronActions, "watchSingleInstanceLockRefusal").mockReturnValue(
new AbortController().signal,
);
ownership = {
runId: "run",
leaderPid: 4242,
processGroupId: 4242,
expectedParentPid: 1,
evidenceOwner: new ProcessOwnershipEvidenceOwner(),
};
});
const emptyHookSnapshot = () => ({
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,
hook_error: false,
single_instance_lock_refused: false,
});
it.each([
"owned launcher is not live",
"process table permission denied",
"process ownership did not match",
])(
"does not signal a Windows Electron PID when lineage is unavailable: %s",
async (reason) => {
vi.spyOn(processOwnership, "verifyNoTokenOwnedProcesses").mockResolvedValue(
{ cleaned: false, reason: "authored census unavailable" },
);
const terminateTree = vi.fn(async () => ({
cleaned: true as const,
signaled: true,
}));
const result = await cleanupElectronProcesses(
ownership,
{
status: "unavailable",
observedAt: "2026-10-10T00:00:00.000Z",
runId: ownership.runId,
launcherPid: ownership.leaderPid,
processGroupId: ownership.processGroupId,
reason,
},
{ platform: "win32", terminateTree },
);
expect(terminateTree).not.toHaveBeenCalled();
expect(result).toEqual({
cleaned: false,
reason: expect.stringContaining(
`${reason}; helper cleanup was not proven`,
),
});
},
);
it("does not signal an unobserved Windows Electron lineage", async () => {
vi.spyOn(processOwnership, "verifyNoTokenOwnedProcesses").mockResolvedValue({
cleaned: false,
reason: "authored census unavailable",
});
const terminateTree = vi.fn();
expect(
await cleanupElectronProcesses(ownership, undefined, {
platform: "win32",
terminateTree,
}),
).toEqual({
cleaned: false,
reason: expect.stringContaining(
"owned Electron lineage was not observed; helper cleanup was not proven",
),
});
expect(terminateTree).not.toHaveBeenCalled();
});
it("preserves a Windows root termination failure without signaling cached descendants", async () => {
const failure = { cleaned: false as const, reason: "termination denied" };
const terminateTree = vi
.fn<typeof processOwnership.cleanupWindowsProcessTree>()
.mockResolvedValue({ cleaned: true, signaled: true });
terminateTree.mockResolvedValueOnce(failure);
const result = await cleanupElectronProcesses(
ownership,
{
status: "verified",
observedAt: "2026-10-10T00:00:00.000Z",
lineage: {
runId: ownership.runId,
launcherPid: ownership.leaderPid,
launcherParentPid: 1,
processGroupId: ownership.processGroupId,
descendants: [
{
pid: 4343,
parentPid: ownership.leaderPid,
processGroupId: ownership.processGroupId,
},
],
},
},
{ platform: "win32", terminateTree },
);
expect(result).toBe(failure);
expect(terminateTree).toHaveBeenCalledOnce();
});
it("signals a Windows Electron tree only after lineage is verified", async () => {
vi.spyOn(processOwnership, "verifyNoTokenOwnedProcesses").mockResolvedValue({
cleaned: true,
signaled: false,
});
const terminateTree = vi.fn(async () => ({
cleaned: true as const,
signaled: true,
}));
const result = await cleanupElectronProcesses(
ownership,
{
status: "verified",
observedAt: "2026-10-10T00:00:00.000Z",
lineage: {
runId: ownership.runId,
launcherPid: ownership.leaderPid,
launcherParentPid: 1,
processGroupId: ownership.processGroupId,
descendants: [
{
pid: 4343,
parentPid: ownership.leaderPid,
processGroupId: ownership.processGroupId,
},
],
},
},
{ platform: "win32", terminateTree },
);
expect(terminateTree).toHaveBeenNthCalledWith(1, 4242);
expect(terminateTree).toHaveBeenCalledOnce();
expect(result).toEqual({ cleaned: true, signaled: true });
});
it("retains the partial capture without taskkill when Windows lineage is unavailable", async () => {
// Exercise the Windows decision branch; this is not native Windows coverage.
// The injected unavailable observation keeps this independent of host PID reuse.
vi.spyOn(
ownershipObservation,
"observeOwnedProcessLineage",
).mockResolvedValue({
status: "unavailable",
observedAt: "2026-10-10T00:00:00.000Z",
runId: "fixture-run",
launcherPid: 424242,
processGroupId: 424242,
reason: "process table permission denied",
});
vi.spyOn(processOwnership, "verifyNoTokenOwnedProcesses").mockResolvedValue({
cleaned: false,
reason: "authored census unavailable",
});
const terminateTree = vi
.spyOn(processOwnership, "cleanupWindowsProcessTree")
.mockResolvedValue({ cleaned: true, signaled: true });
const state = {
windows: [],
metrics: [],
electronVersion: "44.0.0",
hookSnapshot: emptyHookSnapshot(),
};
vi.spyOn(electronActions, "readApplicationState").mockResolvedValue(state);
vi.spyOn(electronActions, "readFinalApplicationState").mockResolvedValue(
state,
);
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => undefined),
windows: () => [],
};
const provider = new PlaywrightElectronActiveProvider(
{},
async () =>
application as unknown as Awaited<
ReturnType<typeof import("playwright-core")._electron.launch>
>,
undefined,
"win32",
);
const result = await provider.capture({
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
}),
application_root: dirname(process.execPath),
});
if (result.ok) throw new Error("Expected unverified cleanup to fail");
expect(terminateTree).not.toHaveBeenCalled();
expect(result.error).toMatchObject({
_tag: "BrowserObservationError",
reason: "cleanup_failed",
cleanup: {
reason: expect.stringContaining(
"process table permission denied; helper cleanup was not proven",
),
},
partialObservation: {
kind: "electron-active-observation",
capture: {
application: {
cleanup: "unverified",
},
},
},
});
});
it("retains the initial snapshot when the final state read fails", async () => {
const initialState = {
windows: [],
metrics: [],
electronVersion: "44.0.0",
hookSnapshot: emptyHookSnapshot(),
};
vi.spyOn(electronActions, "readApplicationState").mockResolvedValueOnce(
initialState,
);
vi.spyOn(electronActions, "readFinalApplicationState").mockRejectedValueOnce(
new Error("final renderer state unavailable"),
);
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => undefined),
windows: () => [],
};
const provider = new PlaywrightElectronActiveProvider(
{},
async () =>
application as unknown as Awaited<
ReturnType<typeof import("playwright-core")._electron.launch>
>,
);
const result = await provider.capture({
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
actions: [],
}),
application_root: dirname(process.execPath),
});
if (result.ok) throw new Error("Expected final state capture to fail");
const partialObservation = result.error.partialObservation;
expect(partialObservation).toMatchObject({
kind: "electron-active-observation",
capture: {
application: { electron_version: "44.0.0" },
limitations: expect.arrayContaining([
expect.stringContaining("initial pre-action snapshot"),
]),
},
});
});
it.each(["initial", "final"] as const)(
"refuses an application handed off to a running instance at the %s snapshot",
async (stage) => {
const state = (refused: boolean) => ({
windows: [],
metrics: [],
electronVersion: "44.0.0",
hookSnapshot: {
...emptyHookSnapshot(),
single_instance_lock_refused: refused,
},
});
vi.spyOn(electronActions, "readApplicationState").mockResolvedValueOnce(
state(stage === "initial"),
);
vi.spyOn(
electronActions,
"readFinalApplicationState",
).mockResolvedValueOnce(state(stage === "final"));
const runActions = vi
.spyOn(electronActions, "runElectronActions")
.mockResolvedValue({ actions: [] });
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => undefined),
windows: () => [],
};
const provider = new PlaywrightElectronActiveProvider(
{},
async () =>
application as unknown as Awaited<
ReturnType<typeof import("playwright-core")._electron.launch>
>,
);
const result = await provider.capture({
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
}),
application_root: dirname(process.execPath),
});
if (result.ok) throw new Error("Expected the handed-off launch to fail");
expect(runActions).toHaveBeenCalledTimes(stage === "initial" ? 0 : 1);
expect(application.close).toHaveBeenCalled();
expect(result.error).toMatchObject({
_tag: "BrowserObservationError",
reason: "application_already_running",
});
expect(projectAnalysisError(result.error)).toMatchObject({
code: "target_unavailable",
category: "unavailable",
message: expect.stringContaining("The application is already running"),
remediation: {
action: expect.stringContaining("Close the running application"),
},
});
// A refusal first seen in the final snapshot keeps that snapshot, which
// holds the blocked lock request and the action effects.
expect(result.error.partialObservation).toMatchObject({
kind: "electron-active-observation",
capture: {
limitations: expect.arrayContaining([
expect.stringContaining(
stage === "initial"
? "initial pre-action snapshot"
: "failed after its final snapshot",
),
]),
},
});
},
);
it("stops pending actions when the lock is refused while they wait", async () => {
const state = (refused: boolean) => ({
windows: [],
metrics: [],
electronVersion: "44.0.0",
hookSnapshot: {
...emptyHookSnapshot(),
single_instance_lock_refused: refused,
},
});
vi.spyOn(electronActions, "readApplicationState").mockResolvedValueOnce(
state(false),
);
vi.spyOn(electronActions, "readFinalApplicationState").mockResolvedValueOnce(
state(true),
);
// The refused path never opens the window the action waits for.
const refusal = new AbortController();
vi.spyOn(electronActions, "watchSingleInstanceLockRefusal").mockReturnValue(
refusal.signal,
);
const windows = vi.fn(() => []);
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => undefined),
windows,
};
const provider = new PlaywrightElectronActiveProvider(
{},
async () =>
application as unknown as Awaited<
ReturnType<typeof import("playwright-core")._electron.launch>
>,
);
const capture = provider.capture({
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
actions: [
{ step_id: "open", kind: "click", window_index: 0, selector: "#go" },
],
}),
application_root: dirname(process.execPath),
});
let result: Awaited<typeof capture>;
try {
await waitFor(() => expect(windows).toHaveBeenCalled());
refusal.abort();
result = await capture;
} finally {
// A failed wait still stops and joins the capture before mocks restore.
await provider.close();
await capture;
}
if (result.ok) throw new Error("Expected the handed-off launch to fail");
expect(application.close).toHaveBeenCalled();
expect(result.error).toMatchObject({
_tag: "BrowserObservationError",
reason: "application_already_running",
partialObservation: {
capture: { actions: [{ step_id: "open", status: "cancelled" }] },
},
});
});
it("rejects active capture before launch when EnableNodeCliInspectArguments fuse is disabled", async () => {
const root = await createTestTempDirectory("rea-active-provider-fuse-");
const disabledBinary = join(root, "disabled-electron");
await writeFile(disabledBinary, thinFuseImage(OBSIDIAN_FUSE_BYTES).bytes);
const launchSpy = vi.fn(async () => {
throw new Error("launch should not be called when fuse is disabled");
});
const provider = new PlaywrightElectronActiveProvider({}, launchSpy);
const result = await provider.capture({
...electronActiveObservationInputSchema.parse({
executable_path: disabledBinary,
application_path: disabledBinary,
}),
application_root: root,
});
if (result.ok) throw new Error("Expected capture to fail");
expect(launchSpy).not.toHaveBeenCalled();
expect(result.error).toMatchObject({
_tag: "AnalysisUnsupportedTargetError",
operation: "capture_electron_scenario",
path: disabledBinary,
message: expect.stringContaining("EnableNodeCliInspectArguments"),
remediationAction: expect.stringContaining("inspect_electron_page"),
});
});
const stubVerifiedElectronCleanup = () => {
vi.spyOn(
ownershipObservation,
"observeOwnedProcessLineage",
).mockImplementation(async (group) => ({
status: "verified",
observedAt: "2026-10-10T00:00:00.000Z",
lineage: {
runId: group.runId,
launcherPid: group.leaderPid,
launcherParentPid: process.pid,
processGroupId: group.processGroupId,
descendants: [],
},
}));
return [
vi
.spyOn(processOwnership, "cleanupWindowsProcessTree")
.mockResolvedValue({ cleaned: true, signaled: true }),
vi
.spyOn(processOwnership, "cleanupOwnedProcessGroup")
.mockResolvedValue({ cleaned: true, signaled: true }),
vi
.spyOn(processOwnership, "verifyNoTokenOwnedProcesses")
.mockResolvedValue({ cleaned: true, signaled: false }),
];
};
/** Run a capture whose test doubles abort `controller` at a chosen phase. */
const captureWithController = async (
controller: AbortController,
application: Record<string, unknown>,
actions: unknown[],
) => {
const provider = new PlaywrightElectronActiveProvider(
{},
async () =>
application as unknown as Awaited<
ReturnType<typeof import("playwright-core")._electron.launch>
>,
);
return provider.capture(
{
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
actions,
}),
application_root: dirname(process.execPath),
},
{ signal: controller.signal },
);
};
it("cancels a windowless wait action and cleans up the owned application", async () => {
const cleanupSpies = stubVerifiedElectronCleanup();
const state = {
windows: [],
metrics: [],
electronVersion: "44.0.0",
hookSnapshot: emptyHookSnapshot(),
};
vi.spyOn(electronActions, "readApplicationState").mockResolvedValue(state);
vi.spyOn(electronActions, "readFinalApplicationState").mockResolvedValue(
state,
);
const controller = new AbortController();
const reason = new Error("caller-reason");
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => undefined),
windows: () => [],
firstWindow: vi.fn(() => {
queueMicrotask(() => controller.abort(reason));
return new Promise<never>(() => undefined);
}),
};
const result = await captureWithController(controller, application, [
{ step_id: "settle", kind: "wait", duration_ms: 10 },
]);
expect(application.firstWindow).toHaveBeenCalledOnce();
expect(application.close).toHaveBeenCalledOnce();
expect(cleanupSpies.some((spy) => spy.mock.calls.length > 0)).toBe(true);
if (result.ok) throw new Error("Expected the capture to be cancelled");
expect(result.error).toMatchObject({
_tag: "BrowserObservationError",
reason: "cancelled",
partialObservation: {
kind: "electron-active-observation",
capture: {
actions: [{ step_id: "settle", kind: "wait", status: "cancelled" }],
},
},
});
expect(result.error.cause).toBe(reason);
});
it("records actions after a failed action as not executed and keeps the capture", async () => {
stubVerifiedElectronCleanup();
const state = {
windows: [],
metrics: [],
electronVersion: "44.0.0",
hookSnapshot: emptyHookSnapshot(),
};
vi.spyOn(electronActions, "readApplicationState").mockResolvedValue(state);
vi.spyOn(electronActions, "readFinalApplicationState").mockResolvedValue(
state,
);
const click = vi.fn(async () => {
throw new Error("strict mode violation");
});
const waitForTimeout = vi.fn(async () => undefined);
const page = { locator: () => ({ click }), waitForTimeout };
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => undefined),
windows: () => [page],
browserWindow: async () => ({
evaluate: async (read: (window: { id: number }) => unknown) =>
read({ id: 42 }),
dispose: async () => {},
}),
firstWindow: async () => page,
};
const result = await captureWithController(
new AbortController(),
application,
[
{ step_id: "fail", kind: "click", selector: "#duplicate" },
{ step_id: "settle", kind: "wait", duration_ms: 0, window_index: 0 },
{
step_id: "link",
kind: "deep-link",
delivery: "open-url",
url: "rea-test://open",
},
],
);
if (!result.ok) throw result.error;
expect(click).toHaveBeenCalledOnce();
expect(waitForTimeout).not.toHaveBeenCalled();
expect(application.close).toHaveBeenCalledOnce();
expect(result.value.actions).toEqual([
{
step_id: "fail",
kind: "click",
window_index: 0,
target: "window:42",
status: "failed",
elapsed_ms: expect.any(Number),
error: "strict mode violation",
},
{
step_id: "settle",
kind: "wait",
window_index: 0,
target: null,
status: "cancelled",
elapsed_ms: 0,
error: "not executed after an earlier action failure",
},
{
step_id: "link",
kind: "deep-link",
window_index: null,
target: null,
status: "cancelled",
elapsed_ms: 0,
error: "not executed after an earlier action failure",
},
]);
});
it("cancels a pending initial state read and cleans up the owned application", async () => {
const cleanupSpies = stubVerifiedElectronCleanup();
const controller = new AbortController();
const reason = new Error("caller-reason");
vi.spyOn(electronActions, "readApplicationState").mockImplementation(() => {
queueMicrotask(() => controller.abort(reason));
return new Promise<never>(() => undefined);
});
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => undefined),
windows: () => [],
};
const result = await captureWithController(controller, application, []);
expect(application.close).toHaveBeenCalledOnce();
expect(cleanupSpies.some((spy) => spy.mock.calls.length > 0)).toBe(true);
if (result.ok) throw new Error("Expected the capture to be cancelled");
expect(result.error).toMatchObject({
_tag: "BrowserObservationError",
reason: "cancelled",
});
expect(result.error.cause).toBe(reason);
});
it("observes an initial state read started after cancellation and rejected by cleanup", async () => {
const cleanupSpies = stubVerifiedElectronCleanup();
const unhandled: unknown[] = [];
const recordUnhandled = (reason: unknown) => {
unhandled.push(reason);
};
process.on("unhandledRejection", recordUnhandled);
try {
const controller = new AbortController();
const reason = new Error("caller-reason");
let rejectReads = (_cause: Error): void => undefined;
const closed = new Promise<never>((_, reject) => {
rejectReads = reject;
});
// Plain functions keep spy bookkeeping from observing the abandoned read.
const application = {
process: () => ({ pid: 424242 }),
evaluate: () => {
controller.abort(reason);
return closed;
},
close: vi.fn(async () => {
rejectReads(
new Error("Target page, context or browser has been closed"),
);
}),
windows: () => [],
};
const result = await captureWithController(controller, application, []);
await new Promise((resolve) => setImmediate(resolve));
await new Promise((resolve) => setImmediate(resolve));
expect(application.close).toHaveBeenCalledOnce();
expect(cleanupSpies.some((spy) => spy.mock.calls.length > 0)).toBe(true);
expect(unhandled).toEqual([]);
if (result.ok) throw new Error("Expected the capture to be cancelled");
expect(result.error).toMatchObject({
_tag: "BrowserObservationError",
reason: "cancelled",
});
expect(result.error.cause).toBe(reason);
} finally {
process.off("unhandledRejection", recordUnhandled);
}
});
it("keeps a producer failure and its output beside concurrent cancellation", async () => {
stubVerifiedElectronCleanup();
const state = {
windows: [],
metrics: [],
electronVersion: "44.0.0",
hookSnapshot: emptyHookSnapshot(),
};
vi.spyOn(electronActions, "readApplicationState").mockResolvedValue(state);
const controller = new AbortController();
const failure = new AnalysisOutputError(
"capture_electron_scenario",
"final state read failed",
{
diagnostics: { producer: "final snapshot" },
capturedOutput: {
stdout: "retained renderer output",
stderr: "renderer diagnostic",
exit_code: null,
signal: null,
truncated: false,
},
},
);
const reason = { caller: "cancel during final read" };
vi.spyOn(electronActions, "readFinalApplicationState").mockImplementation(
async () => {
controller.abort(reason);
throw failure;
},
);
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => undefined),
windows: () => [],
};
const result = await captureWithController(controller, application, []);
if (result.ok) throw new Error("Expected the capture to fail");
expect(result.error).toBe(failure);
expect(result.error).toMatchObject({
_tag: "AnalysisOutputError",
diagnostics: { producer: "final snapshot" },
capturedOutput: { stdout: "retained renderer output" },
partialObservation: { kind: "electron-active-observation" },
});
expect(
result.error.secondaryFailures.some(
(secondary) => secondary.cause === reason,
),
).toBe(true);
});
it("reports an expired final state grace period as the caller's cancellation", async () => {
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
try {
stubVerifiedElectronCleanup();
const controller = new AbortController();
const reason = new Error("caller-reason");
vi.spyOn(electronActions, "readApplicationState").mockResolvedValueOnce({
windows: [],
metrics: [],
electronVersion: "44.0.0",
hookSnapshot: emptyHookSnapshot(),
});
// The real final read reaches these evaluations, which never settle.
const evaluate = vi.fn(() => {
queueMicrotask(() => controller.abort(reason));
return new Promise<never>(() => undefined);
});
const application = {
process: () => ({ pid: 424242 }),
evaluate,
close: vi.fn(async () => undefined),
windows: () => [],
};
const capture = captureWithController(controller, application, []);
await waitFor(() => expect(evaluate).toHaveBeenCalled());
await vi.advanceTimersByTimeAsync(5_000);
const result = await capture;
if (result.ok) throw new Error("Expected the capture to be cancelled");
expect(result.error).toMatchObject({
_tag: "BrowserObservationError",
reason: "cancelled",
diagnostics: {
final_state_read: "cancellation_grace_expired",
cancellation_grace_ms: 5_000,
},
partialObservation: {
kind: "electron-active-observation",
capture: {
limitations: expect.arrayContaining([
expect.stringContaining("initial pre-action snapshot"),
]),
},
},
});
expect(result.error.cause).toBe(reason);
} finally {
vi.useRealTimers();
}
});
const stubCompletedCapture = () => {
const state = {
windows: [],
metrics: [],
electronVersion: "44.0.0",
hookSnapshot: emptyHookSnapshot(),
};
vi.spyOn(electronActions, "readApplicationState").mockResolvedValue(state);
vi.spyOn(electronActions, "readFinalApplicationState").mockResolvedValue(
state,
);
};
it("cancels a completed capture when cancellation arrives during application close", async () => {
stubVerifiedElectronCleanup();
stubCompletedCapture();
const controller = new AbortController();
const reason = new Error("SIGTERM during close");
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => {
controller.abort(reason);
}),
windows: () => [],
};
const result = await captureWithController(controller, application, []);
expect(application.close).toHaveBeenCalledOnce();
if (result.ok) throw new Error("Expected the capture to be cancelled");
expect(result.error).toMatchObject({
_tag: "BrowserObservationError",
reason: "cancelled",
partialObservation: {
kind: "electron-active-observation",
capture: { application: { electron_version: "44.0.0" } },
},
});
expect(result.error.cause).toBe(reason);
});
it("cancels a completed capture when cancellation arrives during process cleanup", async () => {
stubVerifiedElectronCleanup();
stubCompletedCapture();
const controller = new AbortController();
const reason = new Error("SIGTERM during process cleanup");
// POSIX cleanup owns the final sweep; Windows uses tree termination.
vi.spyOn(processOwnership, "cleanupOwnedProcessGroup").mockImplementation(
async () => {
controller.abort(reason);
return {
cleaned: true,
signaled: false,
unverified: [{ pid: 777, diagnostic: "command line unavailable" }],
};
},
);
vi.spyOn(processOwnership, "cleanupWindowsProcessTree").mockImplementation(
async () => {
controller.abort(reason);
return { cleaned: true, signaled: true };
},
);
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => undefined),
windows: () => [],
};
const result = await captureWithController(controller, application, []);
if (result.ok) throw new Error("Expected the capture to be cancelled");
expect(result.error).toMatchObject({
_tag: "BrowserObservationError",
reason: "cancelled",
partialObservation: {
kind: "electron-active-observation",
capture: { application: { electron_version: "44.0.0" } },
},
});
expect(result.error.cause).toBe(reason);
});
it("keeps cleanup failure precedence over cancellation observed during cleanup", async () => {
stubVerifiedElectronCleanup();
stubCompletedCapture();
const controller = new AbortController();
const failed = async () => {
controller.abort(new Error("SIGTERM during process cleanup"));
return { cleaned: false as const, reason: "process group still alive" };
};
vi.spyOn(processOwnership, "cleanupOwnedProcessGroup").mockImplementation(
failed,
);
vi.spyOn(processOwnership, "cleanupWindowsProcessTree").mockImplementation(
failed,
);
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => undefined),
windows: () => [],
};
const result = await captureWithController(controller, application, []);
if (result.ok) throw new Error("Expected cleanup to fail");
expect(result.error).toMatchObject({
cleanupIncomplete: true,
partialObservation: {
kind: "electron-active-observation",
capture: { application: { cleanup: "unverified" } },
},
});
expect(result.error.cleanupPrimary).toMatchObject({
_tag: "BrowserObservationError",
reason: "cancelled",
});
});
it.each(["process", "transport"] as const)(
"preserves cancellation that preceded a %s cleanup failure",
async (failure) => {
stubVerifiedElectronCleanup();
stubCompletedCapture();
const controller = new AbortController();
const reason = new Error("SIGTERM before cleanup");
vi.spyOn(electronActions, "runElectronActions").mockImplementation(
async () => {
controller.abort(reason);
return { actions: [] };
},
);
if (failure === "process") {
const failed = async () => ({
cleaned: false as const,
reason: "process group still alive",
});
vi.spyOn(processOwnership, "cleanupOwnedProcessGroup").mockImplementation(
failed,
);
vi.spyOn(
processOwnership,
"cleanupWindowsProcessTree",
).mockImplementation(failed);
}
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => {
if (failure === "transport")
throw new Error("transport still connected");
}),
windows: () => [],
};
const result = await captureWithController(controller, application, []);
if (result.ok) throw new Error("Expected incomplete cleanup");
expect(result.error).toMatchObject({
cleanupIncomplete: true,
cleanupPrimary: {
_tag: "BrowserObservationError",
reason: "cancelled",
cause: reason,
},
partialObservation: {
kind: "electron-active-observation",
},
});
expect(JSON.stringify(result.error.diagnostics)).toContain(
failure === "process"
? "process group still alive"
: "transport still connected",
);
expect(application.close).toHaveBeenCalledOnce();
},
);
it.each(["process", "transport", "both"] as const)(
"retains %s cleanup and blocks replacement capture until recovery",
async (failure) => {
stubVerifiedElectronCleanup();
stubCompletedCapture();
let denyProcess = failure !== "transport";
let denyTransport = failure !== "process";
const processCleanup = vi.fn(async () =>
denyProcess
? { cleaned: false as const, reason: "authored process cleanup denial" }
: { cleaned: true as const, signaled: false },
);
vi.spyOn(processOwnership, "cleanupOwnedProcessGroup").mockImplementation(
processCleanup,
);
vi.spyOn(processOwnership, "cleanupWindowsProcessTree").mockImplementation(
processCleanup,
);
const application = {
process: () => ({ pid: 424242 }),
close: vi.fn(async () => {
if (denyTransport) throw new Error("authored transport cleanup denial");
}),
windows: () => [],
};
const launch = vi.fn(
async () =>
application as unknown as Awaited<
ReturnType<typeof import("playwright-core")._electron.launch>
>,
);
const provider = new PlaywrightElectronActiveProvider({}, launch);
const input = {
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
}),
application_root: dirname(process.execPath),
};
try {
const first = await provider.capture(input);
expect(first).toMatchObject({
ok: false,
error: { cleanupIncomplete: true },
});
const blocked = await provider.capture(input);
expect(blocked).toMatchObject({
ok: false,
error: { cleanupIncomplete: true },
});
expect(launch).toHaveBeenCalledOnce();
denyProcess = false;
denyTransport = false;
expect((await provider.capture(input)).ok).toBe(true);
expect(first).toMatchObject({
error: { cleanupIncomplete: false, cleanupResources: [] },
});
expect(blocked).toMatchObject({
error: { cleanupIncomplete: false, cleanupResources: [] },
});
expect(launch).toHaveBeenCalledTimes(2);
} finally {
denyProcess = false;
denyTransport = false;
await provider.close();
}
},
);
it.each(["process", "transport"] as const)(
"retains cancellation first observed during failed %s cleanup through retry",
async (phase) => {
stubVerifiedElectronCleanup();
stubCompletedCapture();
const controller = new AbortController();
const reason = { caller: "during final cleanup", phase };
let denied = true;
const processCleanup = async () => {
if (phase === "process" && denied) {
controller.abort(reason);
return {
cleaned: false as const,
reason: "authored late process failure",
};
}
return { cleaned: true as const, signaled: false };
};
vi.spyOn(processOwnership, "cleanupOwnedProcessGroup").mockImplementation(
processCleanup,
);
vi.spyOn(processOwnership, "cleanupWindowsProcessTree").mockImplementation(
processCleanup,
);
const application = {
process: () => ({ pid: 424242 }),
windows: () => [],
close: vi.fn(async () => {
if (phase === "transport" && denied) {
controller.abort(reason);
throw new Error("authored late transport failure");
}
}),
};
const provider = new PlaywrightElectronActiveProvider(
{},
async () =>
application as unknown as Awaited<
ReturnType<typeof import("playwright-core")._electron.launch>
>,
);
try {
const result = await provider.capture(
{
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
}),
application_root: dirname(process.execPath),
},
{ signal: controller.signal },
);
if (result.ok)
throw new Error("Expected cleanup failure over cancellation");
expect(projectAnalysisError(result.error).code).toBe(
"cleanup_incomplete",
);
expect(result.error.cleanupPrimary?.cause).toBe(reason);
expect(result.error.partialObservation).toMatchObject({
kind: "electron-active-observation",
});
denied = false;
await provider.close();
expect(result.error.cleanupIncomplete).toBe(false);
expect(projectAnalysisError(result.error).code).toBe("cancelled");
expect(result.error.cleanupPrimary?.cause).toBe(reason);
} finally {
denied = false;
await provider.close();
}
},
);
it.each([false, true])(
"retains baseline batch observations and cancellation without ambient abort (mixed: %s)",
async (mixed) => {
const reason = { caller: "cancel native identity request" };
const observations = new Map([
[401, { state: "readable", identity: "recorded-native-start" } as const],
]);
const batch = new ProcessOwnershipIdentityBatchFailure(
[
{
pid: 402,
requestIndex: 1,
cause: new ProcessOwnershipInspectionCancelled(reason),
},
...(mixed
? [
{
pid: 403,
requestIndex: 2,
cause: new Error("physical native read failure"),
},
]
: []),
],
observations,
);
const prepare = vi
.spyOn(ownershipObservation, "prepareProcessOwnershipInspection")
.mockResolvedValue();
const baseline = vi
.spyOn(ownershipObservation.systemProcessOwnershipHost, "captureBaseline")
.mockRejectedValue(batch);
const launch = vi.fn(async () => {
throw new Error("must not launch");
});
const provider = new PlaywrightElectronActiveProvider({}, launch);
try {
const result = await provider.capture({
...electronActiveObservationInputSchema.parse({
executable_path: process.execPath,
application_path: process.execPath,
}),
application_root: dirname(process.execPath),
});
if (result.ok) throw new Error("Expected failed native identity batch");
const error = primaryAnalysisError(result.error);
if (mixed) {
expect(error.userCategory).not.toBe("cancelled");
expect(error.cause).toBe(batch);
expect(
error.secondaryFailures.some((failure) => failure.cause === reason),
).toBe(true);
} else {
expect(projectAnalysisError(error).code).toBe("cancelled");
expect(error.cause).toBe(reason);
expect(error).toHaveProperty("inspectionFailure", batch);
}
expect(batch.observations).toBe(observations);
expect(launch).not.toHaveBeenCalled();
} finally {
prepare.mockRestore();
baseline.mockRestore();
await provider.close();
}
},
);