Files
reverse-engineering-anything/scripts/lib/real-hopper-owner-exit.mjs
T
2474e6ec6c refactor(verify): run Hopper and Kuna verifier commands through the supervised owner (#12718)
* refactor(verify): run Hopper and Kuna verifier commands through the supervised owner

Replace bare execFile in the Hopper and Kuna real-provider verifiers with
createVerifierCommands, and run pre-workspace toolchain probes through
runOwnedCommand. Close the owner before lineage verification and keep the
diagnostic workspace when cleanup, lineage, or the run fails.

Replace isError-only and FAT64 prose refusals with the projected error code
and structured issue or target details.

* fix(verify): release a clean Kuna workspace after a verification failure

A failed check whose provider, command, and lineage cleanup all completed
still removes the root. The workspace stays only when that cleanup is
unconfirmed, which is the contract the Kuna verifier failure-path tests
already require.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(verify): keep the Kuna report when workspace release fails

release() closes cleanup registration before it removes the root, so
retain() after a failed release throws and skips the JSON report. Register
retention only when release has not started; a failed release already
keeps the workspace.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(verify): keep both Hopper verification and lineage failures

When verification has already failed and lineage observation then fails,
the retained-workspace error carried only the earlier cause. Aggregate
both failures in the Hopper, FAT, and caller verifiers so the cleanup
reason stays with the workspace.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(verify): keep Hopper manifests inside the repository

realpath() accepted REA_HOPPER_CONFORMANCE_MANIFEST_PATH as any filesystem
path. Resolve the manifest against the repository and reject a lexical or
symlink escape before it is read.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(verify): acquire command owners before verifier workspaces

* fix(verify): create the command owner before the verifier workspace

If command-owner setup fails after a workspace is allocated, the temporary
root is never released. Create the owner first and close it when workspace
allocation fails.

Co-authored-by: morluto <morluto@users.noreply.github.com>

* fix(verify): supervise Hopper helper CLI processes

* fix(verify): run Hopper helper CLIs through the command owner

Boundary and Linux owner-exit checks still launched the REA CLI with
execFile, so a timeout could not stop those processes. Both helpers now
require the verifier command owner and run those CLIs through it.

Co-authored-by: morluto <morluto@users.noreply.github.com>

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
Co-authored-by: morluto <morluto@users.noreply.github.com>
2026-11-20 04:35:35 +00:00

171 lines
5.6 KiB
JavaScript

import assert from "node:assert/strict";
import { setTimeout as delay } from "node:timers/promises";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { verifyNoTokenOwnedProcesses } from "../../dist/process/ProcessOwnership.js";
import { ProcessOwnershipEvidenceOwner } from "../../dist/process/ProcessOwnershipEvidence.js";
import {
observeProcessStartIdentity,
signalProcessWithStartIdentity,
systemProcessOwnershipHost,
} from "../../dist/process/ProcessOwnershipObservation.js";
import { startUnrelatedHopperSentinel } from "./unrelated-hopper-sentinel.mjs";
import {
requireMcpEvidenceResult,
requireMcpLifecycleResult,
} from "./mcp-verifier-results.mjs";
/** Prove actual MCP owner exit releases the demo singleton for subsequent CLI/MCP sessions. */
export async function verifyLinuxHopperOwnerExit({
primary,
secondary,
serverCommand,
serverArgs,
environment,
commands,
}) {
if (commands === undefined)
throw new Error(
"Linux Hopper owner-exit verification requires the supervised command owner",
);
const sentinel = await startUnrelatedHopperSentinel();
const outcomes = [];
try {
for (const mode of ["graceful", "SIGKILL"]) {
const baseline = await systemProcessOwnershipHost.captureBaseline?.();
assert.ok(
baseline,
"Could not capture pre-launch process ownership baseline",
);
const transport = new StdioClientTransport({
command: serverCommand,
args: serverArgs,
env: environment,
stderr: "pipe",
});
// Drain launcher diagnostics even on the deliberately interrupted owner.
transport.stderr?.resume();
const client = new Client({ name: "hopper-owner-exit", version: "1" });
const options = { timeout: 180_000 };
try {
await client.connect(transport);
const ownerPid = transport.pid;
assert.ok(ownerPid !== null);
const owner = await observeProcessStartIdentity(ownerPid);
assert.equal(owner?.state, "readable");
const ownerProcess = (
await systemProcessOwnershipHost.listProcesses()
).find(({ pid }) => pid === ownerPid);
assert.ok(
ownerProcess,
"Could not observe the launched MCP process group",
);
assert.equal(typeof ownerProcess.processGroupId, "number");
const relation = {
leaderPid: ownerPid,
processGroupId: ownerProcess.processGroupId,
evidenceOwner: new ProcessOwnershipEvidenceOwner(),
};
requireMcpLifecycleResult(
await client.callTool(
{
name: "open_binary",
arguments: { path: primary },
},
options,
),
"open_binary",
);
const procedures = requireMcpEvidenceResult(
await client.callTool(
{ name: "list_procedures", arguments: {} },
options,
),
"list_procedures",
);
assert.ok(procedures.length > 0);
const status = requireMcpLifecycleResult(
await client.callTool(
{ name: "binary_session", arguments: {} },
options,
),
"binary_session",
);
const runId = status.analysis_run?.run_id;
assert.equal(typeof runId, "string");
assert.equal(status.analysis_provider_binding?.provider.id, "hopper");
const active = await verifyNoTokenOwnedProcesses(
runId,
systemProcessOwnershipHost,
baseline,
relation,
);
assert.equal(active.cleaned, false, JSON.stringify(active));
assert.ok(
relation.evidenceOwner.coverage.retainedOwned > 0,
`The active Hopper session has no token-verified process ownership evidence: ${JSON.stringify(active)}`,
);
if (mode === "graceful") await client.close();
else {
assert.equal(
await signalProcessWithStartIdentity(
ownerPid,
owner.identity,
"SIGKILL",
),
"signaled",
);
}
const deadline = Date.now() + 10_000;
let cleanup;
do {
cleanup = await verifyNoTokenOwnedProcesses(
runId,
systemProcessOwnershipHost,
baseline,
relation,
);
if (cleanup.cleaned) break;
await delay(100);
} while (Date.now() < deadline);
assert.equal(cleanup.cleaned, true, JSON.stringify(cleanup));
// The SIGKILL case must recover the stale lease for both target identities.
for (const target of [primary, secondary]) {
const result = await commands.exec(
process.execPath,
[
"scripts/rea.mjs",
"inspect",
target,
"--provider",
"hopper",
"--format",
"json",
],
{
env: environment,
timeout: 180_000,
maxBuffer: 16 * 1024 * 1024,
},
);
assert.ok(JSON.parse(result.stdout).normalized_result);
}
outcomes.push({
mode,
ownedProcessesStopped: true,
sameTargetReopened: true,
differentTargetReopened: true,
});
} finally {
await client.close();
await transport.close();
}
}
} finally {
assert.equal(await sentinel.verifyAndClose(), true);
}
return { outcomes, unrelatedHopperSurvived: true };
}