Files
reverse-engineering-anything/src/process/capture/ProcessSamplingInspection.test.ts
T
morluto ab84ba6839 fix(process)!: retain inspection cancellation and cleanup ownership (#10113)
* fix(process): preserve producer-owned inspection cancellation

Prototype the explicit inspection origin and retained file/batch owner protocol. Downstream adapter migration and lease binding remain required before integration.

* fix(process): type inspection ownership handoffs precisely

* fix(process): retain plain inspection preparation cancellation

* fix(process): retain explicit inspection cleanup and cancellation facts

* fix(process): require explicit inspection facts and cleanup scope

* refactor(process): remove redundant cleanup forwarding wrapper

* fix(firmware): retain inspection cancellation and cleanup ownership

* fix(wakaru): retain typed inspection outcomes and caller scope

* fix(ghidra): preserve inspection facts and launch cleanup scope

* fix(providers): preserve typed process inspection outcomes

* refactor(process): remove obsolete inspection cancellation re-export

* fix(native): preserve inspection preparation cleanup and cancellation

* fix(registration): retain typed inspection cleanup during Git discovery

* fix(process): preserve inspection deadline origin in adapter handoff

* fix(process): consume the canonical inspection selector in binding

* fix(native): preserve inspection deadlines and cleanup retry completion

* test(process): narrow the retained Darwin preparation owner

* fix(providers): preserve preparation ownership and timeout policy

Narrow preparation facts through their canonical owner, preserve actual cleanup capabilities and recorded cancellation, and keep enclosing command deadlines authoritative. Retain raw inspection records through Java refusal and configuration projections without changing availability fallback policy.

* refactor(process): use one inspection failure type owner

* fix(providers): retain recorded secondary failures when rebinding

* fix(ghidra): preserve normalized inspection facts through public projection

* fix(process): normalize native process listing cancellation

* docs(development): clarify failure provenance and cleanup verification

* fix(process): preserve direct inspection cancellation facts

* fix(process): retain inspection owners through termination

* fix(process): preserve command inspection cleanup provenance

* fix(desktop): preserve inspection facts after cleanup retry

* refactor(process)!: migrate desktop capture with inspection producers

* fix(desktop)!: preserve capture failure origins and cleanup owners (#10115)

* fix(desktop): preserve capture failure origins and cleanup

* fix(desktop): type keyring cleanup cancellation

* fix(kuna)!: retain copy cleanup before retiring workspaces (#10114)

* fix(kuna): own copy cancellation and descriptor cleanup

* fix(kuna): close copy descriptors before workspace removal

* test(kuna): type copy lifecycle gates for project library

* docs(kuna): explain retained copy-file cleanup and staging ownership

* docs(kuna): separate cache publication, integrity and cleanup guidance

* fix(desktop): retain sampling inspection owners before release

* fix(process): preserve capture file read cancellation origin

* fix(process): retain capture inspection cancellation and cleanup owners

* fix(process): retain sampling inspection failures in capture scope

* fix(process): retain inspection failures through mandatory cleanup

* fix(process): distinguish sampler shutdown from capture cancellation

* fix(browser): retain startup EOF before later cancellation

* fix(process): bind capture inspection cleanup to the owning run

* test(process): migrate cancellation fixtures to typed owners

Record explicit preparation and command cancellation facts, retain exact opaque reasons and physical causes, and guard the mocked version probe from native preparation. Keep the live Wakaru command control while adding a recording equivalent for bounded source verification.

* fix(process): own sampling reads and settle inspection peers

* fix(errors): retain authored recovery through cleanup composition

* fix(process): retain all sampling inspection leaves for final cleanup

* test(browser): report recording cleanup signal outcome

* fix(process): keep inspection error context out of implicit JSON

* fix(desktop): retain normalized sampling cleanup ownership

* test(process): assert tagged startup cancellation
2026-11-11 16:37:48 -05:00

162 lines
5.4 KiB
TypeScript

import { readFile } from "node:fs/promises";
import { join } from "node:path";
import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js";
import { expect, it, onTestFinished, vi } from "vitest";
import { failedInspectionDescriptor } from "../../../tests/fixtures/processInspectionCleanup.js";
import {
readProcessRunId,
type ProcessOwnershipHost,
} from "../ProcessOwnership.js";
import {
ProcessOwnershipIdentityBatchFailure,
ProcessOwnershipInspectionCancelled,
} from "../ProcessInspectionCancellation.js";
import { ProcessCaptureInspectionFailure } from "./ProcessCaptureError.js";
import { ProcessCaptureResourceScope } from "./ProcessCaptureLifecycle.js";
import { readSamplingSource } from "./ProcessSamplingObservation.js";
const recordingHost = (cause: unknown): ProcessOwnershipHost => ({
platform: "linux",
environment: vi.fn(async () => {
throw cause;
}),
listProcesses: async () => {
throw new Error("Unexpected process inspection");
},
signalGroup: () => {
throw new Error("Unexpected process signal");
},
});
it.each(["descriptor", "batch"] as const)(
"retains the actual run-token %s failure through sampling and capture-scope retry",
async (kind) => {
const file = await failedInspectionDescriptor();
const observations = new Map([
[
44,
{
state: "unavailable" as const,
reason: "retained sampling observation",
},
],
]);
const reason = { caller: "opaque cancellation" };
const cancellation = new ProcessOwnershipInspectionCancelled(reason);
const cause =
kind === "descriptor"
? file.failure
: new ProcessOwnershipIdentityBatchFailure(
[
{ pid: 42, requestIndex: 0, cause: file.failure },
{ pid: 44, requestIndex: 1, cause: cancellation },
],
observations,
);
const scope = new ProcessCaptureResourceScope();
onTestFinished(async () => {
file.allow();
await scope.close();
});
const host = recordingHost(cause);
let failure: unknown;
try {
await readSamplingSource(
"authored run token",
new AbortController().signal,
() => readProcessRunId(42, host),
);
} catch (error) {
failure = error;
}
const retained = scope.retainInspectionFailure(failure);
expect(retained).toBeInstanceOf(ProcessCaptureInspectionFailure);
if (retained === undefined)
throw new Error("Sampling lost its inspection owner");
expect(retained.inspectionFailure).toBe(cause);
expect(retained.cleanupIncomplete).toBe(true);
await expect(scope.retryCleanup()).rejects.toBeDefined();
expect(file.handle.fd).toBeGreaterThanOrEqual(0);
let nextCapture = false;
await expect(
scope.run(async () => {
nextCapture = true;
}),
).rejects.toBeDefined();
expect(nextCapture).toBe(false);
file.allow();
await scope.retryCleanup();
expect(file.handle.fd).toBe(-1);
expect(retained.cleanupIncomplete).toBe(false);
expect(retained.inspectionFailure).toBe(cause);
if (cause instanceof ProcessOwnershipIdentityBatchFailure) {
expect(cause.observations).toBe(observations);
expect(cause.cancellations[0]?.cancellation.reason).toBe(reason);
expect(retained.userCategory).toBeUndefined();
}
await scope.run(async () => {
nextCapture = true;
});
expect(nextCapture).toBe(true);
expect(host.environment).toHaveBeenCalledOnce();
},
);
it("propagates actual typed cancellation while keeping ordinary EIO optional and unknown", async () => {
const reason = { opaque: true };
const cancellation = new ProcessOwnershipInspectionCancelled(reason);
const scope = new ProcessCaptureResourceScope();
onTestFinished(() => scope.close());
let failure: unknown;
try {
await readSamplingSource("token", new AbortController().signal, () =>
readProcessRunId(42, recordingHost(cancellation)),
);
} catch (error) {
failure = error;
}
const retained = scope.retainInspectionFailure(failure);
expect(retained?.inspectionFailure).toBe(cancellation);
expect(retained?.cause).toBe(reason);
expect(retained?.userCategory).toBe("cancelled");
const physical = Object.assign(new Error("optional read failed"), {
code: "EIO",
});
const unknown = await readSamplingSource(
"token",
new AbortController().signal,
() => readProcessRunId(42, recordingHost(physical)),
);
expect(unknown).toMatchObject({
ok: false,
error: { kind: "unavailable", systemCode: "EIO" },
});
expect(scope.retainInspectionFailure(physical)).toBeUndefined();
});
it("keeps an actual physical filesystem rejection when it is reused as the sampling abort reason", async () => {
const directory = await createTestTempDirectory("rea-sampling-origin-");
const controller = new AbortController();
let physical: unknown;
const result = await readSamplingSource(
"authored optional file",
controller.signal,
async () => {
try {
return await readFile(join(directory, "absent"), {
signal: controller.signal,
});
} catch (cause) {
physical = cause;
controller.abort(cause);
throw cause;
}
},
);
expect(controller.signal.reason).toBe(physical);
expect(result).toMatchObject({
ok: false,
error: { kind: "unavailable", systemCode: "ENOENT" },
});
});