mirror of
https://github.com/morluto/rea.git
synced 2026-11-14 18:52:48 +00:00
* 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
220 lines
7.9 KiB
TypeScript
220 lines
7.9 KiB
TypeScript
import { randomUUID } from "node:crypto";
|
|
import { rm } from "node:fs/promises";
|
|
|
|
import type { ArtifactResourceScope } from "../artifacts/ArtifactResourceScope.js";
|
|
import { AnalysisCleanupLease } from "../artifacts/AnalysisCleanupLease.js";
|
|
import { AnalysisError } from "../domain/analysisErrorBase.js";
|
|
import { analysisErrorWithCancellation } from "../domain/analysisErrorCancellation.js";
|
|
import {
|
|
retainProcessInspectionCleanup,
|
|
processInspectionCancellationError,
|
|
} from "../providers/OwnedCommandCleanup.js";
|
|
import {
|
|
OwnedCommandFailure,
|
|
runOwnedCommand,
|
|
} from "../process/OwnedCommand.js";
|
|
import { DarwinProcessOwnershipInspectionError } from "../process/DarwinProcessRunTokenReader.js";
|
|
import { artifactCommandOutput } from "../process/ArtifactAnalysisResources.js";
|
|
import {
|
|
AnalysisCancelledError,
|
|
AnalysisOutputError,
|
|
} from "../domain/analysisErrorCore.js";
|
|
|
|
/** Run a setup command with ownership of its entire process family and partial output. */
|
|
export const runDesktopBuildOwnedCommand = async (input: {
|
|
readonly command: string;
|
|
readonly arguments: readonly string[];
|
|
readonly environment: Readonly<NodeJS.ProcessEnv>;
|
|
readonly timeoutMs: number;
|
|
readonly maxOutputBytes: number;
|
|
readonly resources: ArtifactResourceScope;
|
|
readonly partialPath?: string;
|
|
readonly workspace?: string;
|
|
readonly signal?: AbortSignal;
|
|
}): Promise<void> => {
|
|
let retained = false;
|
|
let cleanupReason: string | undefined;
|
|
const paths = [input.partialPath, input.workspace].filter(
|
|
(path): path is string => path !== undefined,
|
|
);
|
|
const remainingPaths = new Set(paths);
|
|
const removeOwnedPaths = async () => {
|
|
const attempted = [...remainingPaths];
|
|
const outcomes = await Promise.allSettled(
|
|
attempted.map((path) =>
|
|
rm(path, { recursive: path === input.workspace, force: true }),
|
|
),
|
|
);
|
|
outcomes.forEach((outcome, index) => {
|
|
if (outcome.status === "fulfilled")
|
|
remainingPaths.delete(attempted[index]!);
|
|
});
|
|
const failures = outcomes.flatMap((outcome) =>
|
|
outcome.status === "rejected" ? [outcome.reason] : [],
|
|
);
|
|
if (failures.length > 0)
|
|
throw new AggregateError(
|
|
failures,
|
|
"Could not remove setup command outputs",
|
|
);
|
|
};
|
|
try {
|
|
await runOwnedCommand(
|
|
{
|
|
command: input.command,
|
|
arguments: input.arguments,
|
|
runId: randomUUID(),
|
|
expectedCommand: null,
|
|
hostEnvironment: input.environment,
|
|
sweepTokenOwnedProcesses: true,
|
|
},
|
|
{ timeoutMs: input.timeoutMs, diagnosticBytes: input.maxOutputBytes },
|
|
{
|
|
signal: input.signal,
|
|
retainCleanup: (owner, result) => {
|
|
retained = true;
|
|
cleanupReason = result.reason;
|
|
const process = `setup process ${owner.launch.ownership?.runId ?? "unknown"}`;
|
|
let processPending = true;
|
|
input.resources.retain({
|
|
kind: "lease",
|
|
resource:
|
|
input.partialPath ??
|
|
`setup process ${owner.launch.ownership?.runId ?? "unknown"}`,
|
|
lease: new AnalysisCleanupLease(
|
|
async () => {
|
|
if (processPending) {
|
|
const stopped = await owner.stop();
|
|
if (stopped.status === "incomplete")
|
|
throw new Error(stopped.reason);
|
|
processPending = false;
|
|
}
|
|
await removeOwnedPaths();
|
|
},
|
|
{
|
|
get cleanupResources() {
|
|
return [
|
|
...remainingPaths,
|
|
...(processPending ? [process] : []),
|
|
];
|
|
},
|
|
},
|
|
),
|
|
});
|
|
},
|
|
},
|
|
);
|
|
if (retained)
|
|
throw new Error(
|
|
`Setup command cleanup incomplete: ${cleanupReason ?? "unknown reason"}`,
|
|
);
|
|
} catch (cause: unknown) {
|
|
let failure: unknown = cause;
|
|
if (!retained && paths.length > 0) {
|
|
try {
|
|
await removeOwnedPaths();
|
|
} catch (cleanup: unknown) {
|
|
input.resources.retain({
|
|
kind: "lease",
|
|
resource: paths[0]!,
|
|
lease: new AnalysisCleanupLease(removeOwnedPaths, {
|
|
get cleanupResources() {
|
|
return [...remainingPaths];
|
|
},
|
|
}),
|
|
});
|
|
failure = new AggregateError(
|
|
[cause, cleanup],
|
|
"Setup command failed and its partial output could not be removed",
|
|
);
|
|
}
|
|
}
|
|
if (retained)
|
|
failure = new Error(
|
|
`${desktopBuildOwnedCommandReason(cause)}; cleanup is incomplete: ${cleanupReason ?? "unknown reason"}`,
|
|
{ cause },
|
|
);
|
|
const command = cause instanceof OwnedCommandFailure ? cause : undefined;
|
|
const preparation = command?.preparationFailure ?? cause;
|
|
const output =
|
|
command?.snapshot == null
|
|
? preparation instanceof DarwinProcessOwnershipInspectionError
|
|
? preparation.capturedOutput
|
|
: undefined
|
|
: artifactCommandOutput(command.snapshot);
|
|
const reason = desktopBuildOwnedCommandReason(failure);
|
|
const physical =
|
|
failure instanceof AnalysisError
|
|
? failure
|
|
: new AnalysisOutputError("setup", reason, {
|
|
cause: failure,
|
|
...(command === undefined
|
|
? {}
|
|
: { diagnostics: { command_failure: command.reason } }),
|
|
...(output === undefined ? {} : { capturedOutput: output }),
|
|
});
|
|
const inspectionCancellation =
|
|
command === undefined || command.preparationFailure !== undefined
|
|
? processInspectionCancellationError(cause, "setup")
|
|
: undefined;
|
|
let primary: AnalysisError = physical;
|
|
if (command?.reason === "cancelled" && command.cancellation !== undefined) {
|
|
primary =
|
|
inspectionCancellation ??
|
|
new AnalysisCancelledError("setup", {
|
|
cause: command.cancellation.reason,
|
|
diagnostics: {
|
|
setup_failure: reason,
|
|
command_failure: command.reason,
|
|
},
|
|
...(output === undefined ? {} : { capturedOutput: output }),
|
|
});
|
|
if (failure !== cause) primary.retainSecondaryFailure(physical);
|
|
} else if (inspectionCancellation !== undefined) {
|
|
primary = inspectionCancellation;
|
|
}
|
|
// Executed command cleanup is already retained with its partial outputs
|
|
// by retainCleanup above. Only preparation owners transfer here.
|
|
primary =
|
|
command === undefined || command.preparationFailure !== undefined
|
|
? retainProcessInspectionCleanup(
|
|
command?.preparationFailure ?? cause,
|
|
primary,
|
|
input.resources,
|
|
"setup",
|
|
)
|
|
: command.cancellation === undefined
|
|
? primary
|
|
: analysisErrorWithCancellation(
|
|
primary,
|
|
new AnalysisCancelledError("setup", {
|
|
cause: command.cancellation.reason,
|
|
}),
|
|
);
|
|
// This finalization checkpoint records a later caller cancellation without
|
|
// reclassifying the established physical command or cleanup outcome.
|
|
if (input.signal?.aborted)
|
|
primary = analysisErrorWithCancellation(
|
|
primary,
|
|
new AnalysisCancelledError("setup", {
|
|
cause: input.signal.reason,
|
|
}),
|
|
);
|
|
throw primary;
|
|
}
|
|
};
|
|
|
|
/** Keep the bounded command's stderr with its failure reason. */
|
|
export const desktopBuildOwnedCommandReason = (cause: unknown): string => {
|
|
const command = cause instanceof OwnedCommandFailure ? cause : undefined;
|
|
const preparation = command?.preparationFailure ?? cause;
|
|
const stderr =
|
|
command?.snapshot?.stderr.text.trim() ??
|
|
(preparation instanceof DarwinProcessOwnershipInspectionError
|
|
? preparation.capturedOutput?.stderr.trim()
|
|
: undefined) ??
|
|
"";
|
|
const message = cause instanceof Error ? cause.message : String(cause);
|
|
return stderr === "" ? message : `${message}: ${stderr.slice(-2000)}`;
|
|
};
|