fix(process)!: retain observed cleanup intervention (#10481)

* fix(process)!: retain observed cleanup intervention

Process capture derived `settlement.cleanup_outcome` from the settlement
state alone in three places, so a quiesced capture reported
`not_required` even after final owned-process cleanup SIGKILLed a live
token-owned descendant. `cleanupOwnedProcessGroup` already knew it had
signaled, but the capture lifecycle discarded that fact.

Carry the observed signal out of ownership cleanup (also on failure
paths), accumulate it across cleanup retries as true/false/unknown, and
derive settlement and the intervention residual unknown once in
`processCaptureCleanupFacts` for full, partial and harness results.

BREAKING CHANGE: a quiesced settlement may now report
`cleanup_outcome: "cleaned"`. Saved captures stay valid; readers that
accepted only `not_required` for quiesced settlements must accept
`cleaned`.

Fixes #8032

Squashed from #8043 and rebased onto main (adapted to the resource-scope
release API and the inspection-failure cleanup path).

Co-Authored-By: gensokyo-dev <gensokyo-dev@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(process): keep a sent signal when cleanup throws an inspection failure

Final ownership verification can rethrow a typed inspection failure (for
example cancellation) after SIGKILL succeeded. The capture owner then
recorded the attempt as unknown and dropped the intervention fact.

cleanupOwnedProcessGroup now marks a failure it throws after signaling,
without changing the failure's identity, and the capture owner keeps
the signal for such failures.

Co-Authored-By: gensokyo-dev <gensokyo-dev@users.noreply.github.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: wunianze666-netizen <wunianze666@gmail.com>
Co-authored-by: gensokyo-dev <gensokyo-dev@users.noreply.github.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-11-14 08:48:13 +01:00
1 parent 1569186aa6
commit 672e214963
11 files changed
+812 -144

No files matched your search

+93
View File
@@ -1,10 +1,12 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import {
cleanupFailureFollowedSignal,
cleanupOwnedProcessGroup,
type ProcessIdentityObservation,
type ProcessOwnershipHost,
type ProcessTableEntry,
} from "./ProcessOwnership.js";
import { ProcessOwnershipInspectionCancelled } from "./ProcessInspectionCancellation.js";
import { parseProcessEnvironment } from "./ProcessOwnershipObservation.js";
import { selectCapturedProcessGroupIds } from "./ProcessOwnershipProcessTree.js";
import { matchesOwnedProcessCommand } from "./ProcessCommandIdentity.js";
@@ -439,6 +441,7 @@ describe("withheld process evidence during cleanup", () => {
expect(result.reason).toBe(
"owned process group signal failed: signal denied",
);
if (mode === "signal-error") expect(result.signaled).not.toBe(true);
},
);
@@ -611,6 +614,96 @@ describe("sampled detached process groups during cleanup", () => {
});
describe("rooted process-group cleanup", () => {
it("retains an earlier signal when a later owned group cannot be signaled", async () => {
const processes = [
{ pid: 100, parentPid: 1, processGroupId: 100 },
{ pid: 101, parentPid: 100, processGroupId: 101 },
{ pid: 900, parentPid: 1, processGroupId: 900 },
].map((entry) => ({ ...entry, state: "S", command: "fixture" }));
const signalGroup = vi.fn((group: number) => {
if (group === 101)
throw Object.assign(new Error("second group denied"), {
code: "EACCES",
});
});
const result = await cleanupOwnedProcessGroup(ownership, {
listProcesses: async () => processes,
environment: async (pid) => ({
REA_PROCESS_RUN_ID: pid === 900 ? "foreign" : "run-token",
}),
signalGroup,
});
expect(signalGroup.mock.calls).toEqual([
[100, "SIGKILL"],
[101, "SIGKILL"],
]);
expect(result).toMatchObject({ cleaned: false, signaled: true });
if (result.cleaned) throw new Error("Expected failed cleanup");
expect(result.reason).toContain("second group denied");
});
it("retains a signal when final ownership verification becomes unavailable", async () => {
const fixture = host({ 100: { REA_PROCESS_RUN_ID: "run-token" } });
let sent = false;
const result = await cleanupOwnedProcessGroup(
{ ...ownership, sweepTokenOwnedProcesses: true, captureBaseline: [] },
{
...fixture.adapter,
listProcesses: async () => {
if (sent) throw new Error("post-signal inspection unavailable");
return fixture.adapter.listProcesses();
},
signalGroup: (group, signal) => {
fixture.adapter.signalGroup(group, signal);
sent = true;
},
},
);
expect(fixture.signalGroup.mock.calls).toEqual([[100, "SIGKILL"]]);
expect(result).toMatchObject({ cleaned: false, signaled: true });
if (result.cleaned) throw new Error("Expected failed verification");
expect(result.reason).toContain("post-signal inspection unavailable");
});
it("marks an inspection failure thrown after a signal so callers keep the intervention", async () => {
const fixture = host({ 100: { REA_PROCESS_RUN_ID: "run-token" } });
let sent = false;
const cancelled = new ProcessOwnershipInspectionCancelled("stopped");
const attempt = cleanupOwnedProcessGroup(
{ ...ownership, sweepTokenOwnedProcesses: true, captureBaseline: [] },
{
...fixture.adapter,
listProcesses: async () => {
if (sent) throw cancelled;
return fixture.adapter.listProcesses();
},
signalGroup: (group, signal) => {
fixture.adapter.signalGroup(group, signal);
sent = true;
},
},
);
await expect(attempt).rejects.toBe(cancelled);
expect(fixture.signalGroup.mock.calls).toEqual([[100, "SIGKILL"]]);
expect(cleanupFailureFollowedSignal(cancelled)).toBe(true);
});
it("does not mark an inspection failure thrown before any signal", async () => {
const cancelled = new ProcessOwnershipInspectionCancelled("stopped");
const signalGroup = vi.fn();
await expect(
cleanupOwnedProcessGroup(ownership, {
listProcesses: async () => {
throw cancelled;
},
environment: async () => ({}),
signalGroup,
}),
).rejects.toBe(cancelled);
expect(signalGroup).not.toHaveBeenCalled();
expect(cleanupFailureFollowedSignal(cancelled)).toBe(false);
});
it("validates and signals rooted descendant groups once, root first", async () => {
const processes = [
{ pid: 100, parentPid: 1, processGroupId: 100 },