Files
reverse-engineering-anything/src/process/ProcessInspectionCancellation.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

172 lines
6.0 KiB
TypeScript

import type { ProcessIdentityObservation } from "./ProcessOwnership.js";
import {
RegularFileCancelled,
RegularFileCleanupFailure,
} from "../filesystem/OwnedRegularFile.js";
import { RegularFileOperationCancelled } from "../filesystem/RegularFile.js";
import { FileReadCancelled } from "../filesystem/readFileHandleChunks.js";
import { ExecFileOutputCancelledError } from "./ExecFileOutput.js";
/** Cancellation observed by ownership inspection, separate from opaque caller values. */
export class ProcessOwnershipInspectionCancelled extends Error {
readonly _tag = "ProcessOwnershipInspectionCancelled";
constructor(
readonly reason: unknown,
options?: { readonly cause: unknown },
) {
super("Process ownership inspection cancelled", {
cause: options === undefined ? reason : options.cause,
});
this.name = this._tag;
}
/** Normalize only cancellation facts emitted by the actual lower-level owner. */
static from(
cause: unknown,
):
| ProcessOwnershipInspectionCancelled
| ProcessOwnershipIdentityBatchFailure
| undefined {
if (isProcessOwnershipInspectionCancellation(cause)) return cause;
if (cause instanceof ExecFileOutputCancelledError)
return new ProcessOwnershipInspectionCancelled(cause.cause, { cause });
if (
cause instanceof RegularFileOperationCancelled ||
cause instanceof RegularFileCancelled ||
cause instanceof FileReadCancelled
)
return new ProcessOwnershipInspectionCancelled(cause.reason, { cause });
return undefined;
}
}
export const assertProcessOwnershipInspectionActive = (
signal?: AbortSignal,
): void => {
if (signal?.aborted === true)
throw new ProcessOwnershipInspectionCancelled(signal.reason);
};
/** Neither signal state, error identity nor error names establish cancellation origin. */
export const isProcessOwnershipInspectionCancellation = (
cause: unknown,
): cause is
| ProcessOwnershipInspectionCancelled
| ProcessOwnershipIdentityBatchFailure =>
cause instanceof ProcessOwnershipInspectionCancelled ||
(cause instanceof ProcessOwnershipIdentityBatchFailure &&
cause.origin === "cancelled");
/** One native identity batch has settled every admitted read, retaining failed owners and completed facts. */
export class ProcessOwnershipIdentityBatchFailure extends AggregateError {
readonly _tag = "ProcessOwnershipIdentityBatchFailure";
readonly cancellations: readonly {
readonly pid: number;
readonly requestIndex: number;
readonly cancellation:
| ProcessOwnershipInspectionCancelled
| ProcessOwnershipIdentityBatchFailure;
}[];
readonly physicalFailures: readonly {
readonly pid: number;
readonly requestIndex: number;
readonly cause: unknown;
}[];
readonly origin: "cancelled" | "physical";
/** Single caller cancellation value; every request's explicit fact remains in cancellations. */
get reason(): unknown {
return this.cancellations[0]?.cancellation.reason;
}
constructor(
readonly failures: readonly {
readonly pid: number;
readonly requestIndex: number;
readonly cause: unknown;
}[],
readonly observations: ReadonlyMap<number, ProcessIdentityObservation>,
physicalReadFailures: readonly {
readonly pid: number;
readonly requestIndex: number;
readonly cause: unknown;
}[] = [],
) {
super(
[...physicalReadFailures, ...failures].map(({ cause }) => cause),
"Native process identity inspection failed",
);
this.name = this._tag;
const cancellations: Array<{
pid: number;
requestIndex: number;
cancellation:
| ProcessOwnershipInspectionCancelled
| ProcessOwnershipIdentityBatchFailure;
}> = [];
const physicalFailures = [...physicalReadFailures];
for (const { pid, requestIndex, cause } of failures) {
const cancellation =
cause instanceof RegularFileCleanupFailure
? cause.outcome.kind === "failed"
? ProcessOwnershipInspectionCancelled.from(cause.outcome.cause)
: cause.completionCancellation === undefined
? undefined
: new ProcessOwnershipInspectionCancelled(
cause.completionCancellation.reason,
{ cause },
)
: ProcessOwnershipInspectionCancelled.from(cause);
if (cancellation !== undefined)
cancellations.push({ pid, requestIndex, cancellation });
if (
cause instanceof RegularFileCleanupFailure ||
cancellation === undefined
)
physicalFailures.push({ pid, requestIndex, cause });
}
this.cancellations = cancellations;
this.physicalFailures = physicalFailures;
this.origin =
physicalFailures.length === 0 && cancellations.length > 0
? "cancelled"
: "physical";
}
}
/** Unavailable observations must not discard cancellation or a retained descriptor owner. */
export const rethrowProcessOwnershipInspectionFailure = (
cause: unknown,
): void => {
if (
cause instanceof RegularFileCleanupFailure ||
cause instanceof ProcessOwnershipIdentityBatchFailure
)
throw cause;
const cancellation = ProcessOwnershipInspectionCancelled.from(cause);
if (cancellation !== undefined) throw cancellation;
};
/** Cancellable inspection backoff owns its abort outcome rather than parsing a timer error. */
export const waitForProcessOwnershipInspection = async (
milliseconds: number,
signal?: AbortSignal,
): Promise<void> => {
assertProcessOwnershipInspectionActive(signal);
return new Promise((resolve, reject) => {
const finish = () => {
signal?.removeEventListener("abort", onAbort);
resolve();
};
const timer = setTimeout(finish, milliseconds);
const onAbort = () => {
clearTimeout(timer);
signal?.removeEventListener("abort", onAbort);
reject(new ProcessOwnershipInspectionCancelled(signal?.reason));
};
signal?.addEventListener("abort", onAbort, { once: true });
if (signal?.aborted === true) onAbort();
});
};