Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(process): retain owned command cancellation provenance

morluto408704827057
diff --git a/docs/tool-design.md b/docs/tool-design.md
index 055daf148..c085fff20 100644
--- a/docs/tool-design.md
+++ b/docs/tool-design.md
@@ -95,6 +95,9 @@ providers need not expose identical state machines.
primary failure and let incomplete cleanup retain its own classification.
Preserve the caller's abort reason as the typed cancellation error's cause,
including when cancellation is observed only after provider completion.
+ Shared command owners must retain the observed cancellation reason separately
+ from physical failures and cleanup ownership. Carry that fact through failure
+ wrapping so adapters can preserve the exact reason without interpreting it.
Client cancellation alone does not prove native work stopped.
For atomic publication, document the cancellation cutoff and preserve any
committed output's publication receipt.
diff --git a/src/process/OwnedCommand.ts b/src/process/OwnedCommand.ts
index baf22c2b4..06f59a303 100644
--- a/src/process/OwnedCommand.ts
+++ b/src/process/OwnedCommand.ts
@@ -25,6 +25,12 @@ import {
/** A short-lived command failed its lifecycle or complete-output contract. */
export class OwnedCommandFailure extends Error {
+ readonly #cancellation: { readonly reason: unknown } | undefined;
+
+ /** Opaque caller reason observed by the owner, distinct from physical causes. */
+ get cancellation(): { readonly reason: unknown } | undefined {
+ return this.#cancellation;
+ }
readonly cleanupResult: ProcessCleanupFailure | undefined;
readonly collectionFailure: boolean;
readonly preparationFailure:
@@ -37,6 +43,7 @@ export class OwnedCommandFailure extends Error {
readonly snapshot: ProviderProcessSnapshot | null = null,
readonly cleanupFailure: string | null = null,
options?: ErrorOptions & {
+ readonly cancellation?: { readonly reason: unknown } | undefined;
readonly cleanupResult?: ProcessCleanupFailure;
readonly collectionFailure?: boolean;
readonly preparationFailure?: DarwinProcessOwnershipInspectionError;
@@ -45,6 +52,10 @@ export class OwnedCommandFailure extends Error {
readonly cleanupOwner?: ProviderProcessSupervisor,
) {
super(message, options);
+ this.#cancellation =
+ options?.cancellation === undefined
+ ? undefined
+ : { reason: options.cancellation.reason };
this.cleanupResult = options?.cleanupResult;
this.collectionFailure = options?.collectionFailure ?? false;
this.preparationFailure = options?.preparationFailure;
@@ -98,6 +109,9 @@ export const runOwnedCommand = async (
throw new OwnedCommandFailure(
"cancelled",
"Command cancelled before launch.",
+ null,
+ null,
+ { cancellation: { reason: callerSignal.reason } },
);
const deadline = new ProviderStartupDeadline(limits.timeoutMs, callerSignal);
try {
@@ -158,6 +172,11 @@ const collectOwnedCommand = async (
null,
{
cause,
+ ...(interruption === "cancelled"
+ ? {
+ cancellation: { reason: signal.reason },
+ }
+ : {}),
...(preparationFailure === undefined ? {} : { preparationFailure }),
},
);
@@ -203,6 +222,11 @@ const collectOwnedCommand = async (
interruption === "cancelled"
? "Command cancelled."
: "Command deadline elapsed.",
+ null,
+ null,
+ interruption === "cancelled"
+ ? { cancellation: { reason: signal.reason } }
+ : undefined,
);
if (exceeded)
throw new OwnedCommandFailure(
@@ -227,6 +251,10 @@ const collectOwnedCommand = async (
? "Command cancelled."
: "Command deadline elapsed.",
snapshot,
+ null,
+ interruption === "cancelled"
+ ? { cancellation: { reason: signal.reason } }
+ : undefined,
);
if (exceeded)
throw new OwnedCommandFailure(
@@ -278,7 +306,9 @@ const collectOwnedCommand = async (
snapshot,
stopped.reason,
{
- ...(failure === undefined ? {} : { cause: failure }),
+ ...(failure === undefined
+ ? {}
+ : { cause: failure, cancellation: failure.cancellation }),
...(stopped.cleanupResult === undefined
? {}
: { cleanupResult: stopped.cleanupResult }),
@@ -297,6 +327,7 @@ const collectOwnedCommand = async (
failure.cleanupFailure,
{
cause: failure,
+ cancellation: failure.cancellation,
...(failure.cleanupResult === undefined
? {}
: { cleanupResult: failure.cleanupResult }),
@@ -305,7 +336,15 @@ const collectOwnedCommand = async (
failure.resources,
);
if (callerSignal?.aborted)
- throw new OwnedCommandFailure("cancelled", "Command cancelled.", snapshot);
+ throw new OwnedCommandFailure(
+ "cancelled",
+ "Command cancelled.",
+ snapshot,
+ null,
+ {
+ cancellation: { reason: callerSignal.reason },
+ },
+ );
return snapshot;
};
diff --git a/tests/boundary/process/ownedCommand.test.ts b/tests/boundary/process/ownedCommand.test.ts
index faa4d7a29..cb4a326f8 100644
--- a/tests/boundary/process/ownedCommand.test.ts
+++ b/tests/boundary/process/ownedCommand.test.ts
@@ -349,6 +349,10 @@ it.skipIf(process.platform === "win32")(
it("cancels a real acquired process and independently releases it", async () => {
const controller = new AbortController();
+ const callerReason = {
+ request: "active cancellation",
+ cleanup: "caller metadata",
+ };
let pid: number | undefined;
const result = runOwnedCommand(
command(
@@ -361,7 +365,7 @@ it("cancels a real acquired process and independently releases it", async () =>
const process = await spawnOwnedProviderProcess(input);
pid = process.process.pid;
await waitForProviderProcessReady(process.process);
- setImmediate(() => controller.abort());
+ setImmediate(() => controller.abort(callerReason));
return process;
},
},
@@ -369,6 +373,7 @@ it("cancels a real acquired process and independently releases it", async () =>
await expect(result).rejects.toMatchObject({
reason: "cancelled",
cleanupFailure: null,
+ cancellation: { reason: callerReason },
});
if (pid === undefined) throw new Error("Process was not acquired");
const acquiredPid = pid;
@@ -446,6 +451,9 @@ it.skipIf(process.platform === "win32").each(["cancelled", "timeout"] as const)(
"The acquired command must retain its failed cleanup owner",
);
expect(failure.reason).toBe(reason);
+ if (reason === "cancelled")
+ expect(failure.cancellation?.reason).toBe(controller.signal.reason);
+ else expect(failure.cancellation).toBeUndefined();
expect(failure.snapshot?.stdout.text).toContain("child-ready");
expect(cleanup).toMatchObject({
cleaned: false,
@@ -481,6 +489,7 @@ it.skipIf(process.platform === "win32").each(["cancelled", "timeout"] as const)(
it("honors cancellation during cleanup after a successful command", async () => {
const controller = new AbortController();
+ const callerReason = new Error("cancel during final process cleanup");
await expect(
runOwnedCommand(
command("process.exit(0)"),
@@ -493,14 +502,18 @@ it("honors cancellation during cleanup after a successful command", async () =>
...launched,
cleanup: async () => {
const result = await cleanupOwnedProcessGroup(launched.ownership);
- controller.abort();
+ controller.abort(callerReason);
return result;
},
};
},
},
),
- ).rejects.toMatchObject({ reason: "cancelled", cleanupFailure: null });
+ ).rejects.toMatchObject({
+ reason: "cancelled",
+ cleanupFailure: null,
+ cancellation: { reason: callerReason },
+ });
});
it("rejects oversized diagnostics even when the child exits immediately", async () => {
diff --git a/tests/boundary/process/ownedCommandCancellation.test.ts b/tests/boundary/process/ownedCommandCancellation.test.ts
new file mode 100644
index 000000000..8db51f4ed
--- /dev/null
+++ b/tests/boundary/process/ownedCommandCancellation.test.ts
@@ -0,0 +1,53 @@
+import { expect, it } from "vitest";
+import {
+ OwnedCommandFailure,
+ runOwnedCommand,
+} from "../../../src/process/OwnedCommand.js";
+import { ProviderCleanupError } from "../../../src/domain/providerCleanupError.js";
+
+it.each(
+ (["before", "preparation"] as const).flatMap((phase) =>
+ [
+ null,
+ "opaque caller reason",
+ new ProviderCleanupError("caller-owned", ["caller metadata"], {
+ reason: "opaque reason",
+ }),
+ ].map((reason) => ({ phase, reason })),
+ ),
+)(
+ "retains exact caller cancellation during $phase without assigning cleanup ownership",
+ async ({ phase, reason }) => {
+ const controller = new AbortController();
+ let launches = 0;
+ if (phase === "before") controller.abort(reason);
+ const result = await runOwnedCommand(
+ {
+ command: process.execPath,
+ arguments: ["-e", "throw new Error('Must not launch')"],
+ runId: "owned-command-cancellation-control",
+ },
+ { timeoutMs: 1000, diagnosticBytes: 1024 },
+ {
+ signal: controller.signal,
+ launcher: async (options) => {
+ launches++;
+ expect(options.signal?.aborted).toBe(false);
+ controller.abort(reason);
+ options.signal?.throwIfAborted();
+ throw new Error("Cancelled preparation must not launch");
+ },
+ },
+ ).catch((cause: unknown) => cause);
+ if (!(result instanceof OwnedCommandFailure))
+ throw new Error("Expected owned command cancellation");
+ expect(result.reason).toBe("cancelled");
+ expect(result.cancellation?.reason).toBe(reason);
+ expect(Object.hasOwn(result, "cancellation")).toBe(false);
+ expect(JSON.stringify(result)).not.toContain("opaque caller reason");
+ expect(result.cleanupFailure).toBeNull();
+ expect(result.resources).toEqual([]);
+ expect(result.snapshot).toBeNull();
+ expect(launches).toBe(phase === "before" ? 0 : 1);
+ },
+);