fix!: repair boundary contracts across CLI and MCP (#597)
* fix(comparisons)!: preserve unknowns across partial observations Share absence and digest semantics, keep unobserved managed bodies unknown, and match unique managed type/name/signatures before structural fallback. BREAKING CHANGE: Managed comparison metadata adds exact-signature matching and its count, and name_matching now reports exact-signature-fallback. * fix(javascript): preserve projected result binding semantics Share syntax-aware property reads and assigned-result resolution. Retain destructuring paths and downgrade downstream whole-result relationships instead of resolving nested or wrapped calls as aliases. * fix(native)!: admit complete parser values and preserve typed failures Decode Mach headers by named columns, validate complete slice keys and lipo alignment, and preserve codesign runtime flags and tagged parser errors. Rename the Ghidra test dossier fixture and connect fake-backed suites to real verification lanes. BREAKING CHANGE: Native load-command results no longer contain the unused, always-null file_offset field. * fix(process): preserve host identity and actionable boundary failures Thread selected platform and environment through process and Doctor seams, stamp observed producer identity in Evidence, and retain PTY, filesystem, lease and lazy-provider failure causes. Drain cancelled lazy loads and omit exited descendants from verified lineage. * fix(browser): preserve cleanup and document-transition evidence Make V8 and Playwright teardown cancellation-aware, expose primary failures alongside incomplete cleanup, share frame-tree and navigation rules, and clear stale WebMCP registrations without pruning unrelated child frames. * fix(adapters): align managed targets and typed CLI outcomes Inspect managed PE/CLI targets independently of native provider selection, reuse successful managed selections, and keep evidence bound to the actual inspected target. Classify canonical CLI error envelopes and exhaustive setup/update/uninstall outcomes, audit registered CLI coverage, and assert platform-specific runtime results. * docs: refresh boundary contracts and shipped roadmap behavior
This commit is contained in:
@@ -67,4 +67,71 @@ describe("owned process-group cleanup discovery", () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
it("omits a descendant that exits while lineage is validated", async () => {
|
||||
const launcher = {
|
||||
pid: 100,
|
||||
parentPid: 1,
|
||||
processGroupId: 100,
|
||||
state: "S",
|
||||
command: "root",
|
||||
};
|
||||
const child = {
|
||||
pid: 101,
|
||||
parentPid: 100,
|
||||
processGroupId: 101,
|
||||
state: "S",
|
||||
command: "child",
|
||||
};
|
||||
const processes = [launcher, child];
|
||||
const listProcesses = vi
|
||||
.fn<ProcessOwnershipHost["listProcesses"]>()
|
||||
.mockResolvedValueOnce(processes)
|
||||
.mockResolvedValueOnce([launcher]);
|
||||
const host: ProcessOwnershipHost = {
|
||||
listProcesses,
|
||||
environment: (pid) =>
|
||||
pid === 101
|
||||
? Promise.reject(new Error("process exited during environment read"))
|
||||
: Promise.resolve({ REA_PROCESS_RUN_ID: "run-token" }),
|
||||
signalGroup: vi.fn(),
|
||||
};
|
||||
|
||||
await expect(observeOwnedProcessLineage(ownership, host)).resolves.toEqual({
|
||||
status: "verified",
|
||||
observedAt: expect.any(String),
|
||||
lineage: {
|
||||
runId: "run-token",
|
||||
launcherPid: 100,
|
||||
launcherParentPid: 1,
|
||||
processGroupId: 100,
|
||||
descendants: [],
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("does not publish lineage when the launcher exits during validation", async () => {
|
||||
const launcher = {
|
||||
pid: 100,
|
||||
parentPid: 1,
|
||||
processGroupId: 100,
|
||||
state: "S",
|
||||
command: "root",
|
||||
};
|
||||
const listProcesses = vi
|
||||
.fn<ProcessOwnershipHost["listProcesses"]>()
|
||||
.mockResolvedValueOnce([launcher])
|
||||
.mockResolvedValueOnce([]);
|
||||
const host: ProcessOwnershipHost = {
|
||||
listProcesses,
|
||||
environment: () => Promise.reject(new Error("launcher exited")),
|
||||
signalGroup: vi.fn(),
|
||||
};
|
||||
|
||||
await expect(
|
||||
observeOwnedProcessLineage(ownership, host),
|
||||
).resolves.toMatchObject({
|
||||
status: "unavailable",
|
||||
reason: "owned launcher exited during lineage validation",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -65,6 +65,7 @@ export type ProcessLineageObservation =
|
||||
|
||||
/** Narrow operating-system seam used to inspect processes and signal groups. */
|
||||
export interface ProcessOwnershipHost {
|
||||
readonly platform?: NodeJS.Platform;
|
||||
listProcesses(): Promise<readonly ProcessTableEntry[]>;
|
||||
environment(pid: number): Promise<Readonly<Record<string, string>>>;
|
||||
signalGroup(processGroupId: number, signal: NodeJS.Signals): void;
|
||||
@@ -79,6 +80,7 @@ export interface WindowsProcessTreeHost {
|
||||
interface ProcessOwnershipValidationFailure {
|
||||
readonly pid: number;
|
||||
readonly reason: "environment-unreadable" | "run-token-mismatch";
|
||||
readonly diagnostic?: string;
|
||||
}
|
||||
|
||||
/** Cleanup outcome with per-member diagnostics when ownership is uncertain. */
|
||||
@@ -90,6 +92,7 @@ export type ProcessCleanupResult =
|
||||
readonly failures?: readonly {
|
||||
readonly pid: number;
|
||||
readonly reason: "environment-unreadable" | "run-token-mismatch";
|
||||
readonly diagnostic?: string;
|
||||
}[];
|
||||
};
|
||||
|
||||
@@ -157,13 +160,19 @@ export const parseProcessEnvironment = (
|
||||
}),
|
||||
);
|
||||
|
||||
const systemHost: ProcessOwnershipHost = {
|
||||
/** Create the operating-system process inspector for an explicit host context. */
|
||||
export const createSystemProcessOwnershipHost = (
|
||||
platform: NodeJS.Platform = process.platform,
|
||||
hostEnvironment: NodeJS.ProcessEnv = process.env,
|
||||
): ProcessOwnershipHost => ({
|
||||
platform,
|
||||
async listProcesses() {
|
||||
if (process.platform === "win32") return [];
|
||||
const { stdout } = await execFileOutput("ps", [
|
||||
"-axo",
|
||||
"pid=,ppid=,pgid=,stat=,command=",
|
||||
]);
|
||||
if (platform === "win32") return [];
|
||||
const { stdout } = await execFileOutput(
|
||||
"ps",
|
||||
["-axo", "pid=,ppid=,pgid=,stat=,command="],
|
||||
{ env: hostEnvironment },
|
||||
);
|
||||
return stdout
|
||||
.split("\n")
|
||||
.map((line) => /\s*(\d+)\s+(\d+)\s+(\d+)\s+(\S+)\s+(.*)/u.exec(line))
|
||||
@@ -177,25 +186,28 @@ const systemHost: ProcessOwnershipHost = {
|
||||
}));
|
||||
},
|
||||
async environment(pid) {
|
||||
if (process.platform === "linux") {
|
||||
if (platform === "linux")
|
||||
return parseProcessEnvironment(
|
||||
await readFile(`/proc/${pid}/environ`, "utf8"),
|
||||
);
|
||||
}
|
||||
const { stdout } = await execFileOutput("ps", ["eww", "-p", String(pid)]);
|
||||
const environment: Record<string, string> = {};
|
||||
const { stdout } = await execFileOutput("ps", ["eww", "-p", String(pid)], {
|
||||
env: hostEnvironment,
|
||||
});
|
||||
const observedEnvironment: Record<string, string> = {};
|
||||
for (const match of stdout.matchAll(
|
||||
/(?:^|\s)([A-Za-z_][A-Za-z0-9_]*)=([^\s]*)/gu,
|
||||
)) {
|
||||
const name = match[1];
|
||||
if (name !== undefined) environment[name] = match[2] ?? "";
|
||||
if (name !== undefined) observedEnvironment[name] = match[2] ?? "";
|
||||
}
|
||||
return environment;
|
||||
return observedEnvironment;
|
||||
},
|
||||
signalGroup(processGroupId, signal) {
|
||||
process.kill(-processGroupId, signal);
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
const systemHost = createSystemProcessOwnershipHost();
|
||||
|
||||
const systemWindowsProcessTreeHost: WindowsProcessTreeHost = {
|
||||
async terminateTree(rootPid) {
|
||||
@@ -230,10 +242,17 @@ export const cleanupOwnedProcessGroup = async (
|
||||
ownership: OwnedProcessGroup,
|
||||
host: ProcessOwnershipHost = systemHost,
|
||||
): Promise<ProcessCleanupResult> => {
|
||||
const processes = await readLiveProcessTable(host);
|
||||
if (processes === undefined)
|
||||
return { cleaned: false, reason: "process table could not be inspected" };
|
||||
const plan = await createOwnedCleanupPlan(ownership, processes, host);
|
||||
const processTable = await readLiveProcessTable(host);
|
||||
if (!processTable.available)
|
||||
return {
|
||||
cleaned: false,
|
||||
reason: `process table could not be inspected: ${processTable.reason}`,
|
||||
};
|
||||
const plan = await createOwnedCleanupPlan(
|
||||
ownership,
|
||||
processTable.processes,
|
||||
host,
|
||||
);
|
||||
if ("cleaned" in plan) return plan;
|
||||
let signaled = false;
|
||||
for (const processGroupId of plan.signalOrder) {
|
||||
@@ -251,7 +270,10 @@ export const cleanupOwnedProcessGroup = async (
|
||||
const code =
|
||||
cause instanceof Error && "code" in cause ? cause.code : undefined;
|
||||
if (code !== "ESRCH")
|
||||
return { cleaned: false, reason: "owned process group signal failed" };
|
||||
return {
|
||||
cleaned: false,
|
||||
reason: `owned process group signal failed: ${errorMessage(cause)}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
return { cleaned: true, signaled };
|
||||
@@ -262,10 +284,17 @@ export const verifyNoTokenOwnedProcesses = async (
|
||||
runId: string,
|
||||
host: ProcessOwnershipHost = systemHost,
|
||||
): Promise<ProcessCleanupResult> => {
|
||||
const processes = await readLiveProcessTable(host);
|
||||
if (processes === undefined)
|
||||
return { cleaned: false, reason: "process table could not be inspected" };
|
||||
const scan = await scanTokenOwnedProcesses(processes, runId, host);
|
||||
const processTable = await readLiveProcessTable(host);
|
||||
if (!processTable.available)
|
||||
return {
|
||||
cleaned: false,
|
||||
reason: `process table could not be inspected: ${processTable.reason}`,
|
||||
};
|
||||
const scan = await scanTokenOwnedProcesses(
|
||||
processTable.processes,
|
||||
runId,
|
||||
host,
|
||||
);
|
||||
if (scan.failures.length > 0) return cleanupValidationFailure(scan.failures);
|
||||
return scan.owned.length === 0
|
||||
? { cleaned: true, signaled: false }
|
||||
@@ -274,11 +303,20 @@ export const verifyNoTokenOwnedProcesses = async (
|
||||
|
||||
const readLiveProcessTable = async (
|
||||
host: ProcessOwnershipHost,
|
||||
): Promise<readonly ProcessTableEntry[] | undefined> => {
|
||||
): Promise<
|
||||
| {
|
||||
readonly available: true;
|
||||
readonly processes: readonly ProcessTableEntry[];
|
||||
}
|
||||
| { readonly available: false; readonly reason: string }
|
||||
> => {
|
||||
try {
|
||||
return liveProcesses(await host.listProcesses());
|
||||
} catch {
|
||||
return undefined;
|
||||
return {
|
||||
available: true,
|
||||
processes: liveProcesses(await host.listProcesses()),
|
||||
};
|
||||
} catch (cause: unknown) {
|
||||
return { available: false, reason: errorMessage(cause) };
|
||||
}
|
||||
};
|
||||
|
||||
@@ -368,10 +406,10 @@ const revalidateOwnedProcessGroup = async (
|
||||
({ processGroupId: observedGroupId }) =>
|
||||
observedGroupId === processGroupId,
|
||||
);
|
||||
} catch {
|
||||
} catch (cause: unknown) {
|
||||
return {
|
||||
cleaned: false,
|
||||
reason: "process ownership could not be revalidated",
|
||||
reason: `process ownership could not be revalidated: ${errorMessage(cause)}`,
|
||||
};
|
||||
}
|
||||
const failures = await processOwnershipFailures(
|
||||
@@ -422,12 +460,23 @@ const processOwnershipFailures = async (
|
||||
try {
|
||||
if ((await host.environment(member.pid)).REA_PROCESS_RUN_ID !== runId)
|
||||
failures.push({ pid: member.pid, reason: "run-token-mismatch" });
|
||||
} catch {
|
||||
} catch (cause: unknown) {
|
||||
try {
|
||||
const live = liveProcesses(await host.listProcesses());
|
||||
if (!live.some(({ pid }) => pid === member.pid)) continue;
|
||||
} catch {}
|
||||
failures.push({ pid: member.pid, reason: "environment-unreadable" });
|
||||
} catch (recheckCause: unknown) {
|
||||
failures.push({
|
||||
pid: member.pid,
|
||||
reason: "environment-unreadable",
|
||||
diagnostic: `${errorMessage(cause)}; process liveness recheck failed: ${errorMessage(recheckCause)}`,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
failures.push({
|
||||
pid: member.pid,
|
||||
reason: "environment-unreadable",
|
||||
diagnostic: errorMessage(cause),
|
||||
});
|
||||
}
|
||||
}
|
||||
return failures;
|
||||
@@ -449,17 +498,31 @@ const scanTokenOwnedProcesses = async (
|
||||
try {
|
||||
if ((await host.environment(process.pid)).REA_PROCESS_RUN_ID === runId)
|
||||
owned.push(process);
|
||||
} catch {
|
||||
} catch (cause: unknown) {
|
||||
try {
|
||||
const live = liveProcesses(await host.listProcesses());
|
||||
if (!live.some(({ pid }) => pid === process.pid)) continue;
|
||||
} catch {}
|
||||
failures.push({ pid: process.pid, reason: "environment-unreadable" });
|
||||
} catch (recheckCause: unknown) {
|
||||
failures.push({
|
||||
pid: process.pid,
|
||||
reason: "environment-unreadable",
|
||||
diagnostic: `${errorMessage(cause)}; process liveness recheck failed: ${errorMessage(recheckCause)}`,
|
||||
});
|
||||
continue;
|
||||
}
|
||||
failures.push({
|
||||
pid: process.pid,
|
||||
reason: "environment-unreadable",
|
||||
diagnostic: errorMessage(cause),
|
||||
});
|
||||
}
|
||||
}
|
||||
return { owned, failures };
|
||||
};
|
||||
|
||||
const errorMessage = (cause: unknown): string =>
|
||||
cause instanceof Error ? cause.message : String(cause);
|
||||
|
||||
/** Observe one group without signaling it, failing closed on identity doubt. */
|
||||
export const observeOwnedProcessGroup = async (
|
||||
ownership: OwnedProcessGroup,
|
||||
|
||||
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from "vitest";
|
||||
import {
|
||||
cleanupOwnedProcessGroup,
|
||||
cleanupWindowsProcessTree,
|
||||
createSystemProcessOwnershipHost,
|
||||
observeOwnedProcessGroup,
|
||||
observeOwnedProcessLineage,
|
||||
type ProcessOwnershipHost,
|
||||
@@ -10,6 +11,14 @@ import {
|
||||
import { host, ownership } from "./ProcessOwnership.fixture.js";
|
||||
|
||||
describe("owned process-group cleanup validation: ownership and lineage", () => {
|
||||
it("builds a host seam from the injected platform and environment", async () => {
|
||||
const host = createSystemProcessOwnershipHost("win32", {
|
||||
PATH: "/injected/path",
|
||||
});
|
||||
expect(host.platform).toBe("win32");
|
||||
await expect(host.listProcesses()).resolves.toEqual([]);
|
||||
});
|
||||
|
||||
it("fails closed when a descendant in another process group lacks the token", async () => {
|
||||
const adapter: ProcessOwnershipHost = {
|
||||
listProcesses: () =>
|
||||
@@ -103,7 +112,11 @@ describe("owned process-group cleanup validation: ownership and lineage", () =>
|
||||
cleaned: false,
|
||||
reason: "process tree contains an unowned or PID-reused process",
|
||||
failures: [
|
||||
{ pid: 100, reason: "environment-unreadable" },
|
||||
expect.objectContaining({
|
||||
pid: 100,
|
||||
reason: "environment-unreadable",
|
||||
diagnostic: "transient procfs read",
|
||||
}),
|
||||
{ pid: 101, reason: "run-token-mismatch" },
|
||||
],
|
||||
});
|
||||
|
||||
@@ -18,10 +18,10 @@ export const observeOwnedProcessGroupWithHost = async (
|
||||
members = (await host.listProcesses()).filter(
|
||||
({ processGroupId }) => processGroupId === ownership.processGroupId,
|
||||
);
|
||||
} catch {
|
||||
} catch (cause: unknown) {
|
||||
return {
|
||||
state: "unverifiable",
|
||||
reason: "process group could not be inspected",
|
||||
reason: `process group could not be inspected: ${errorMessage(cause)}`,
|
||||
};
|
||||
}
|
||||
const liveMembers = liveProcesses(members);
|
||||
@@ -36,11 +36,13 @@ export const observeOwnedProcessGroupWithHost = async (
|
||||
state: "unverifiable",
|
||||
reason: "process ownership did not match",
|
||||
};
|
||||
} catch {
|
||||
return {
|
||||
state: "unverifiable",
|
||||
reason: "process ownership could not be revalidated",
|
||||
};
|
||||
} catch (cause: unknown) {
|
||||
if (!(await processIsGone(host, member.pid))) {
|
||||
return {
|
||||
state: "unverifiable",
|
||||
reason: `process ownership could not be revalidated for PID ${member.pid}: ${errorMessage(cause)}`,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
return { state: "alive" };
|
||||
@@ -54,10 +56,10 @@ export const observeOwnedProcessLineageWithHost = async (
|
||||
let processes: readonly ProcessTableEntry[];
|
||||
try {
|
||||
processes = liveProcesses(await host.listProcesses());
|
||||
} catch {
|
||||
} catch (cause: unknown) {
|
||||
return unavailableLineage(
|
||||
ownership,
|
||||
"process table could not be inspected",
|
||||
`process table could not be inspected: ${errorMessage(cause)}`,
|
||||
);
|
||||
}
|
||||
const launcher = processes.find(({ pid }) => pid === ownership.leaderPid);
|
||||
@@ -67,6 +69,7 @@ export const observeOwnedProcessLineageWithHost = async (
|
||||
if (identityFailure !== null)
|
||||
return unavailableLineage(ownership, identityFailure);
|
||||
const descendants = descendantsOf(launcher.pid, processes);
|
||||
const verifiedDescendants: ProcessTableEntry[] = [];
|
||||
for (const member of [launcher, ...descendants]) {
|
||||
try {
|
||||
if (
|
||||
@@ -77,10 +80,19 @@ export const observeOwnedProcessLineageWithHost = async (
|
||||
ownership,
|
||||
"process lineage contains an unowned or PID-reused process",
|
||||
);
|
||||
} catch {
|
||||
if (member.pid !== launcher.pid) verifiedDescendants.push(member);
|
||||
} catch (cause: unknown) {
|
||||
if (await processIsGone(host, member.pid)) {
|
||||
if (member.pid === launcher.pid)
|
||||
return unavailableLineage(
|
||||
ownership,
|
||||
"owned launcher exited during lineage validation",
|
||||
);
|
||||
continue;
|
||||
}
|
||||
return unavailableLineage(
|
||||
ownership,
|
||||
"process ownership could not be revalidated",
|
||||
`process ownership could not be revalidated for PID ${member.pid}: ${errorMessage(cause)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -92,7 +104,7 @@ export const observeOwnedProcessLineageWithHost = async (
|
||||
launcherPid: launcher.pid,
|
||||
launcherParentPid: launcher.parentPid,
|
||||
processGroupId: launcher.processGroupId,
|
||||
descendants: descendants
|
||||
descendants: verifiedDescendants
|
||||
.sort((left, right) => left.pid - right.pid)
|
||||
.map(({ pid, parentPid, processGroupId }) => ({
|
||||
pid,
|
||||
@@ -114,3 +126,19 @@ const unavailableLineage = (
|
||||
processGroupId: ownership.processGroupId,
|
||||
reason,
|
||||
});
|
||||
|
||||
const processIsGone = async (
|
||||
host: ProcessOwnershipHost,
|
||||
pid: number,
|
||||
): Promise<boolean> => {
|
||||
try {
|
||||
return !liveProcesses(await host.listProcesses()).some(
|
||||
(process) => process.pid === pid,
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
const errorMessage = (cause: unknown): string =>
|
||||
cause instanceof Error ? cause.message : String(cause);
|
||||
|
||||
@@ -20,6 +20,10 @@ export interface OwnedProviderProcessSpawnOptions {
|
||||
readonly windowsVerbatimArguments?: boolean;
|
||||
readonly cwd?: string;
|
||||
readonly env?: NodeJS.ProcessEnv;
|
||||
/** Host platform used to choose detached process-group behavior. */
|
||||
readonly platform?: NodeJS.Platform;
|
||||
/** Host environment used as the base for the child environment. */
|
||||
readonly hostEnvironment?: NodeJS.ProcessEnv;
|
||||
}
|
||||
|
||||
/** Spawned process paired with the identity proof required for group cleanup. */
|
||||
@@ -106,14 +110,16 @@ export type ProviderProcessStopResult =
|
||||
export const spawnOwnedProviderProcess = async (
|
||||
options: OwnedProviderProcessSpawnOptions,
|
||||
): Promise<SpawnedOwnedProviderProcess> => {
|
||||
const platform = options.platform ?? process.platform;
|
||||
const hostEnvironment = options.hostEnvironment ?? process.env;
|
||||
const child = spawn(options.command, [...options.arguments], {
|
||||
shell: false,
|
||||
windowsHide: true,
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
detached: process.platform !== "win32",
|
||||
detached: platform !== "win32",
|
||||
windowsVerbatimArguments: options.windowsVerbatimArguments ?? false,
|
||||
env: {
|
||||
...process.env,
|
||||
...hostEnvironment,
|
||||
...options.env,
|
||||
REA_PROCESS_RUN_ID: options.runId,
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user