* fix(application): retain completed evidence through cancellation Validate completed provider reports before classifying cancellation and preserve Evidence and captured diagnostics. Share MCP recording and delivery across custom observation handlers. Add source-owned completion and public SDK recording controls. Refs #3923, #3927, #4261. * fix(wasm): retain final observations through cancellation Keep completed Evidence after finalization and through CLI/MCP delivery. Preserve output and cleanup failure precedence. Refs #4254. * fix(browser): retain final observations and forward cancellation Classify final CDP detach and completed scenario cleanup at the provider boundary. Translate completed partials into canonical Evidence for MCP recording, and forward CLI cancellation to passive Electron tools. Refs #4255, #4256, #4257. * fix(ghidra): isolate cancellation of shared startup waiters Keep startup group-owned while each caller has an independent abortable wait. Await final-owner cleanup and preserve cleanup facts before retrying. Reuse the shared abortable promise helper. Refs #3983. * fix(mcp): keep shutdown registrations until cleanup succeeds Retain repeated event requests while unresolved owners need cleanup, and unregister only after success. Verify coalescing and failed-owner retry. Refs #4253. * fix(adb): own transfer snapshots and stage screen publication Bind transfer digests to the exact private input bytes and preserve caller APK names. Retain failed descriptors and private-root owners with typed cleanup evidence; publish captures exclusively after the cancellation cutoff. Refs #4251, #4252. * fix(verification): report ADB cleanup failures Always close after attempted verification and keep unexpected primary failures and cleanup details visible. Exercise the production script with controlled imports, including null and undefined throws. Refs #4260. * fix(runner): stop owned Windows commands on cancellation Use the retained ChildProcess handle for IPC cancellation and parent disconnection. Keep the SQLite lock until observed child close, and include native Windows regressions in the curated CI lane. Native Windows results remain pending. Refs #4258. * fix(website): canonicalize link containment boundaries Compare resolved targets with resolved repository and site roots. Accept directory aliases while preserving escape checks and keep fixtures within their temporary owner. Refs #4264. * docs(testing): cover completion races and cleanup retry Clarify validation-before-cancellation, independent startup waiter controls, producer and adapter coverage, and repeated shutdown events. * fix(filesystem): retain file owners and completed reads after close failure * fix(session): report retained snapshot staging in cleanup status Fixes #4277. Include both snapshot and profile file-resource scopes in the owning status fact, preserving publication receipts and cleanup retry. * docs(mcp): clarify recovery of raw partial observations Document the observed SDK cancellation boundary and the open design decision in #4276 without promoting raw errors to Evidence. * fix(kuna): own the complete installation inspection lifetime * test(kuna): cover session cleanup of shared cache resources * fix(hopper): isolate publisher operation resource admission * fix(hopper): preserve launcher cleanup failure classification * fix(adb): retain ordered cleanup and screen publication outcomes * fix(runner): retain Windows job ownership through cleanup retries * docs: clarify inspection admission and ordered cleanup regressions * fix(ghidra): preserve startup cleanup and request deadlines * fix(runner): supervise stdin errors and bootstrap native controls * docs: require queued lifecycle controls to survive admission * fix(cli): leave cleanup resolution with resource owners * test(adb): verify owned install snapshots through CLI * fix(errors): exclude resolved primary cleanup resources * fix(adb): resolve publication cleanup after its handle closes * fix(android): resolve JADX hash cleanup after owned close * refactor(android): share hash failure cause options * fix(kuna): gate commands on retained descriptor cleanup * docs: cover cleanup admission and owner-confirmed recovery * refactor(artifacts): bind cleanup errors to confirmed release * fix(adb): resolve recovered cleanup at retained owners * fix(kuna): resolve installation cleanup after owned close * fix(kuna): gate cache reads on retained hash cleanup * fix(setup): preserve publisher cleanup admission failures * fix(android): resolve the reported retirement cleanup error * test(runner): scope post-spawn fault control to POSIX * test(adb): cover persistent snapshot owner retirement * fix(kuna): preserve file outcomes with explicit cleanup ownership * fix(artifacts): resolve errors through retained cleanup owners * docs(lifecycle): carry cleanup ownership in typed outcomes * fix(adb): bind staged screen errors to their cleanup lease * fix(kuna): bind profile cleanup through a close capability * fix(artifacts): retain descriptor resources in cleanup errors * fix(pcap): bind existing errors to snapshot retirement * fix(windows): load source owner through its file URL * test(build): copy the command runner helper into fixtures * fix(mcp): own shared observation providers in the transport * fix(jadx): resolve every retained cleanup retry error * fix(kuna): bind retry errors to every retained cleanup owner * fix(adb): bind admission and shutdown errors to exact cleanup leases * fix(windows): resolve command paths before native ownership * test(publisher): assert exact descriptor recovery without release metadata * docs(lifecycle): require every retry error to follow its owners * fix(kuna): keep hash cleanup state stable across concurrent retries * test(windows): exercise surviving descendants with scoped shutdown deadlines * fix(uninstall): preserve primary read errors through cleanup failure * test(publisher): assert cleanup Result and exact owner recovery * test(windows): observe supervisor resources through IPC disconnect * fix(browser): retain typed cancellation after interrupted actions * test(publisher): assert ownership phases without retry-count coupling * fix(lifecycle): preserve abort reasons through shared provider execution * fix(jadx): preserve execution and late hash cancellation causes * fix(adb): compose late cancellation with exact cleanup failures * test(mcp): use the shared budget for session workflows
423 lines
13 KiB
TypeScript
423 lines
13 KiB
TypeScript
import { EventEmitter, once } from "node:events";
|
|
import { readFile, writeFile } from "node:fs/promises";
|
|
import { createRequire } from "node:module";
|
|
import { join } from "node:path";
|
|
import { setTimeout as delay } from "node:timers/promises";
|
|
|
|
import { expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
createWindowsOwnedCommand,
|
|
loadWindowsOwnedProcessClass,
|
|
superviseWindowsOwnedProcess,
|
|
} from "../../scripts/lib/windows-owned-command.mjs";
|
|
import { requireWindowsNativeAuthority } from "../../src/windows/WindowsNativeLoader.js";
|
|
import { createTestTempDirectory } from "../fixtures/temporaryDirectory.js";
|
|
|
|
const require = createRequire(import.meta.url);
|
|
|
|
async function readOptional(path: string): Promise<string | undefined> {
|
|
return readFile(path, "utf8").catch(() => undefined);
|
|
}
|
|
|
|
it("imports the source Windows owner without dist using a fake native authority", async () => {
|
|
const calls: string[] = [];
|
|
const WindowsOwnedProcess = await loadWindowsOwnedProcessClass();
|
|
const child = new WindowsOwnedProcess(
|
|
"ignored.exe",
|
|
[],
|
|
undefined,
|
|
{},
|
|
false,
|
|
{
|
|
authority: {
|
|
inspection: {
|
|
abiVersion: 1,
|
|
nodeApiVersion: 8,
|
|
architecture: "x64",
|
|
filesystem: {
|
|
requestedPath: "C:\\probe",
|
|
finalPath: "C:\\probe",
|
|
filesystem: "NTFS",
|
|
volumeSerial: "0".repeat(16),
|
|
fileId: "0".repeat(32),
|
|
size: 0,
|
|
directory: true,
|
|
},
|
|
privateDacl: true,
|
|
privateBridgeDescriptorAccess: true,
|
|
atomicJobAssignment: true,
|
|
killOnOwnerClose: true,
|
|
},
|
|
call(operation: string) {
|
|
calls.push(operation);
|
|
if (operation === "process_spawn")
|
|
return {
|
|
pid: 42,
|
|
handle: {},
|
|
jobAssigned: true,
|
|
killOnOwnerClose: true,
|
|
};
|
|
if (operation === "process_poll")
|
|
return {
|
|
stdout: Buffer.alloc(0),
|
|
stderr: Buffer.alloc(0),
|
|
stdoutEnded: true,
|
|
stderrEnded: true,
|
|
exitCode: 0,
|
|
activeProcesses: 0,
|
|
};
|
|
if (operation === "process_close") return null;
|
|
throw new Error(`Unmodeled fake native operation ${operation}`);
|
|
},
|
|
},
|
|
},
|
|
);
|
|
|
|
await expect(once(child, "close")).resolves.toEqual([0, null]);
|
|
expect(calls).toEqual(["process_spawn", "process_poll", "process_close"]);
|
|
});
|
|
|
|
it("passes cross-spawn's resolved executable to the native owner", () => {
|
|
const parserPath = require.resolve("cross-spawn/lib/parse");
|
|
require(parserPath);
|
|
const parserModule = require.cache[parserPath];
|
|
if (parserModule === undefined)
|
|
throw new Error("cross-spawn parser was not loaded");
|
|
const originalParser = parserModule.exports;
|
|
let received: unknown[] | undefined;
|
|
class CaptureOwnedProcess {
|
|
constructor(...arguments_: unknown[]) {
|
|
received = arguments_;
|
|
}
|
|
}
|
|
parserModule.exports = () => ({
|
|
command: "node",
|
|
file: "C:\\runtime\\node.exe",
|
|
args: ["-e", "code"],
|
|
options: {},
|
|
});
|
|
try {
|
|
createWindowsOwnedCommand({
|
|
WindowsOwnedProcess: CaptureOwnedProcess as never,
|
|
command: "node",
|
|
arguments: ["-e", "code"],
|
|
cwd: undefined,
|
|
environment: {},
|
|
});
|
|
} finally {
|
|
parserModule.exports = originalParser;
|
|
}
|
|
|
|
expect(received?.[0]).toBe("C:\\runtime\\node.exe");
|
|
expect(received?.[1]).toEqual(["-e", "code"]);
|
|
});
|
|
|
|
it("supervises protocol stdin errors until the owned job and stream close", async () => {
|
|
const calls: string[] = [];
|
|
let activeProcesses = 1;
|
|
const writeFailure = new Error("controlled native stdin failure");
|
|
const WindowsOwnedProcess = await loadWindowsOwnedProcessClass();
|
|
const child = createWindowsOwnedCommand({
|
|
WindowsOwnedProcess,
|
|
command: "ignored.exe",
|
|
arguments: [],
|
|
cwd: undefined,
|
|
environment: {},
|
|
authority: {
|
|
inspection: {
|
|
abiVersion: 1,
|
|
nodeApiVersion: 8,
|
|
architecture: "x64",
|
|
filesystem: {
|
|
requestedPath: "C:\\probe",
|
|
finalPath: "C:\\probe",
|
|
filesystem: "NTFS",
|
|
volumeSerial: "0".repeat(16),
|
|
fileId: "0".repeat(32),
|
|
size: 0,
|
|
directory: true,
|
|
},
|
|
privateDacl: true,
|
|
privateBridgeDescriptorAccess: true,
|
|
atomicJobAssignment: true,
|
|
killOnOwnerClose: true,
|
|
protocolStdin: true,
|
|
},
|
|
call(operation: string) {
|
|
calls.push(operation);
|
|
if (operation === "process_spawn")
|
|
return {
|
|
pid: 42,
|
|
handle: {},
|
|
jobAssigned: true,
|
|
killOnOwnerClose: true,
|
|
};
|
|
if (operation === "process_stdin_write") throw writeFailure;
|
|
if (operation === "process_stdin_close") return null;
|
|
if (operation === "process_terminate") {
|
|
activeProcesses = 0;
|
|
return true;
|
|
}
|
|
if (operation === "process_poll")
|
|
return {
|
|
stdout: Buffer.alloc(0),
|
|
stderr: Buffer.alloc(0),
|
|
stdoutEnded: true,
|
|
stderrEnded: true,
|
|
exitCode: 0,
|
|
activeProcesses,
|
|
};
|
|
if (operation === "process_close") return null;
|
|
throw new Error(`Unmodeled fake native operation ${operation}`);
|
|
},
|
|
},
|
|
});
|
|
const lifecycle = superviseWindowsOwnedProcess(child, {
|
|
retryIntervalMs: 10,
|
|
});
|
|
if (child.stdin === null) throw new Error("fixture protocol stdin missing");
|
|
const stdin = child.stdin;
|
|
const write = new Promise<Error | null>((resolve) => {
|
|
stdin.write(Buffer.from("x"), (cause?: Error | null) =>
|
|
resolve(cause ?? null),
|
|
);
|
|
});
|
|
|
|
await expect(write).resolves.toBe(writeFailure);
|
|
await expect(lifecycle.finished).rejects.toBe(writeFailure);
|
|
expect(calls).toContain("process_terminate");
|
|
expect(calls).toContain("process_close");
|
|
});
|
|
|
|
it.runIf(process.platform === "win32")(
|
|
"supervises native protocol stdin through Job Object close",
|
|
async () => {
|
|
const directory = await createTestTempDirectory("rea-owned-stdin-probe-");
|
|
const WindowsOwnedProcess = await loadWindowsOwnedProcessClass();
|
|
const child = new WindowsOwnedProcess(
|
|
process.execPath,
|
|
["-e", "setInterval(() => {}, 1000)"],
|
|
directory,
|
|
process.env,
|
|
false,
|
|
{
|
|
protocolStdin: true,
|
|
authority: requireWindowsNativeAuthority(),
|
|
},
|
|
);
|
|
child.stdout.resume();
|
|
child.stderr.resume();
|
|
const lifecycle = superviseWindowsOwnedProcess(child);
|
|
const stdin = child.stdin;
|
|
if (stdin === null) throw new Error("fixture protocol stdin missing");
|
|
let stdinClosed = false;
|
|
stdin.once("close", () => {
|
|
stdinClosed = true;
|
|
});
|
|
process.stdin.pipe(stdin);
|
|
|
|
try {
|
|
lifecycle.requestCleanup();
|
|
let settled = false;
|
|
void lifecycle.finished.then(
|
|
() => {
|
|
settled = true;
|
|
},
|
|
() => {
|
|
settled = true;
|
|
},
|
|
);
|
|
await vi.waitFor(() => expect(settled).toBe(true), { timeout: 15_000 });
|
|
await expect(lifecycle.finished).resolves.toMatchObject({
|
|
code: expect.any(Number),
|
|
});
|
|
expect(stdinClosed).toBe(true);
|
|
} finally {
|
|
process.stdin.unpipe(stdin);
|
|
lifecycle.requestCleanup();
|
|
}
|
|
},
|
|
);
|
|
|
|
it.runIf(process.platform === "win32")(
|
|
"keeps native Job Object ownership after the leader exits",
|
|
async () => {
|
|
const directory = await createTestTempDirectory("rea-owned-tree-probe-");
|
|
const activityPath = join(directory, "descendant-activity.log");
|
|
const readyPath = join(directory, "descendant-ready");
|
|
const exitedPath = join(directory, "descendant-exited");
|
|
const errorPath = join(directory, "descendant-error");
|
|
const stopPath = join(directory, "descendant-stop");
|
|
const grandchild = `
|
|
const fs = require("node:fs");
|
|
process.on("exit", (code) => fs.writeFileSync(${JSON.stringify(exitedPath)}, String(code)));
|
|
process.on("uncaughtExceptionMonitor", (error) => fs.writeFileSync(${JSON.stringify(errorPath)}, String(error)));
|
|
fs.writeFileSync(${JSON.stringify(readyPath)}, "ready");
|
|
const timer = setInterval(() => {
|
|
if (fs.existsSync(${JSON.stringify(stopPath)})) {
|
|
clearInterval(timer);
|
|
process.exit(0);
|
|
}
|
|
fs.appendFileSync(${JSON.stringify(activityPath)}, "active\\n");
|
|
}, 20);
|
|
`;
|
|
const leader = `
|
|
const fs = require("node:fs");
|
|
const child = require("node:child_process").spawn(
|
|
process.execPath,
|
|
["-e", ${JSON.stringify(grandchild)}],
|
|
{ detached: true, stdio: "ignore" },
|
|
);
|
|
child.unref();
|
|
const waitForDescendant = setInterval(() => {
|
|
if (!fs.existsSync(${JSON.stringify(readyPath)})) return;
|
|
clearInterval(waitForDescendant);
|
|
process.exit(0);
|
|
}, 10);
|
|
`;
|
|
const WindowsOwnedProcess = await loadWindowsOwnedProcessClass();
|
|
const native = requireWindowsNativeAuthority();
|
|
const observedCounts: number[] = [];
|
|
const authority = {
|
|
inspection: native.inspection,
|
|
call(operation: string, arguments_: readonly unknown[]) {
|
|
const result = native.call(operation, arguments_);
|
|
if (
|
|
operation === "process_poll" &&
|
|
typeof result === "object" &&
|
|
result !== null &&
|
|
"activeProcesses" in result &&
|
|
typeof result.activeProcesses === "number"
|
|
)
|
|
observedCounts.push(result.activeProcesses);
|
|
return result;
|
|
},
|
|
};
|
|
const child = new WindowsOwnedProcess(
|
|
process.execPath,
|
|
["-e", leader],
|
|
directory,
|
|
process.env,
|
|
false,
|
|
{ authority },
|
|
);
|
|
child.stdout.resume();
|
|
child.stderr.resume();
|
|
const exited = once(child, "exit");
|
|
const closed = once(child, "close");
|
|
let hasClosed = false;
|
|
void closed.then(() => {
|
|
hasClosed = true;
|
|
});
|
|
|
|
try {
|
|
await exited;
|
|
await expect(readFile(readyPath, "utf8")).resolves.toBe("ready");
|
|
const before = await readOptional(activityPath);
|
|
await expect
|
|
.poll(async () => (await readFile(activityPath, "utf8")).length, {
|
|
timeout: 5_000,
|
|
})
|
|
.toBeGreaterThan(before?.length ?? 0);
|
|
expect(
|
|
hasClosed,
|
|
JSON.stringify({
|
|
activeProcessCounts: observedCounts,
|
|
descendantExitCode: await readOptional(exitedPath),
|
|
descendantError: await readOptional(errorPath),
|
|
activity: await readOptional(activityPath),
|
|
}),
|
|
).toBe(false);
|
|
await expect(child.cleanup()).resolves.toMatchObject({ cleaned: true });
|
|
await closed;
|
|
const activityAtCleanup = await readFile(activityPath, "utf8");
|
|
await delay(100);
|
|
expect(await readFile(activityPath, "utf8")).toBe(activityAtCleanup);
|
|
} finally {
|
|
await writeFile(stopPath, "stop");
|
|
if (!hasClosed) {
|
|
const cleanup = await child.cleanup();
|
|
if (cleanup.cleaned) await closed;
|
|
else child.unref();
|
|
}
|
|
await delay(100);
|
|
}
|
|
},
|
|
);
|
|
|
|
it("retries thrown and false cleanup results, then preserves the first error", async () => {
|
|
vi.useFakeTimers();
|
|
try {
|
|
class ControlledOwnedProcess extends EventEmitter {
|
|
attempts = 0;
|
|
|
|
cleanup() {
|
|
this.attempts += 1;
|
|
if (this.attempts === 1)
|
|
throw new Error("controlled synchronous cleanup throw");
|
|
if (this.attempts === 2)
|
|
return Promise.resolve({
|
|
cleaned: false,
|
|
reason: "controlled poll failure",
|
|
});
|
|
this.emit("close", 7, null);
|
|
return Promise.resolve({ cleaned: true, signaled: true });
|
|
}
|
|
}
|
|
|
|
const child = new ControlledOwnedProcess();
|
|
const diagnostics: { type: string; reason?: string }[] = [];
|
|
const lifecycle = superviseWindowsOwnedProcess(child, {
|
|
retryIntervalMs: 25,
|
|
onCleanupDiagnostic: (diagnostic) => diagnostics.push(diagnostic),
|
|
});
|
|
let completed = false;
|
|
const completion = lifecycle.finished
|
|
.catch((cause: unknown) => cause)
|
|
.then((cause) => {
|
|
completed = true;
|
|
return cause;
|
|
});
|
|
|
|
child.emit("error", undefined);
|
|
expect(child.attempts).toBe(1);
|
|
await vi.waitFor(() =>
|
|
expect(diagnostics).toEqual([
|
|
{
|
|
type: "cleanup-failed",
|
|
reason: "controlled synchronous cleanup throw",
|
|
},
|
|
]),
|
|
);
|
|
expect(completed).toBe(false);
|
|
|
|
child.emit("error", new Error("secondary native error"));
|
|
expect(child.attempts).toBe(2);
|
|
await vi.waitFor(() =>
|
|
expect(diagnostics).toEqual([
|
|
{
|
|
type: "cleanup-failed",
|
|
reason: "controlled synchronous cleanup throw",
|
|
},
|
|
{ type: "cleanup-failed", reason: "controlled poll failure" },
|
|
]),
|
|
);
|
|
expect(completed).toBe(false);
|
|
|
|
await vi.advanceTimersByTimeAsync(25);
|
|
await expect(completion).resolves.toBeUndefined();
|
|
expect(child.attempts).toBe(3);
|
|
expect(diagnostics).toEqual([
|
|
{
|
|
type: "cleanup-failed",
|
|
reason: "controlled synchronous cleanup throw",
|
|
},
|
|
{ type: "cleanup-failed", reason: "controlled poll failure" },
|
|
{ type: "cleanup-recovered" },
|
|
]);
|
|
} finally {
|
|
vi.useRealTimers();
|
|
}
|
|
});
|