feat: add evidence-backed process investigations

This commit is contained in:
morluto
2026-07-12 20:36:00 +08:00
parent 9f60348d1b
commit cc8681a856
53 changed files with 2774 additions and 290 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env node
// Route production MCP before importing Incur. Incur owns registration helpers
// such as `mcp add`, while only dist/main.js may serve the 46-tool stdio server.
// such as `mcp add`, while only dist/main.js may serve the 50-tool stdio server.
const args = process.argv.slice(2);
const isMcpMode =
args[0] === "--mcp" || (args.length === 1 && args[0] === "mcp");
+22
View File
@@ -0,0 +1,22 @@
#!/usr/bin/env node
import { spawn } from "node:child_process";
const npm = process.platform === "win32" ? "npm.cmd" : "npm";
const child = spawn(npm, ["rebuild", "node-pty"], {
stdio: "inherit",
env: { ...process.env, npm_config_build_from_source: "true" },
});
child.once("error", (error) => {
console.error(`Unable to rebuild node-pty: ${error.message}`);
process.exitCode = 1;
});
child.once("exit", (code, signal) => {
if (signal !== null) {
console.error(`node-pty rebuild terminated by ${signal}`);
process.exitCode = 1;
return;
}
process.exitCode = code ?? 1;
});
+3 -3
View File
@@ -161,8 +161,8 @@ try {
const client = new Client({ name: "package-smoke", version: "1.0.0" });
try {
await client.connect(transport);
if ((await client.listTools()).tools.length !== 46)
throw new Error("packaged MCP did not expose 46 tools");
if ((await client.listTools()).tools.length !== 50)
throw new Error("packaged MCP did not expose 50 tools");
const result = await client.callTool({
name: "current_document",
arguments: {},
@@ -177,7 +177,7 @@ try {
}
process.stdout.write(
`${JSON.stringify({ cli: true, incurMcpCommand: "npx -y @morluto/rea mcp", doctor: true, setup: "idempotent", clients: 2, backupReadback: true, failureRecovery: true, skill: true, mcpTools: 46, targetFree: true })}\n`,
`${JSON.stringify({ cli: true, incurMcpCommand: "npx -y @morluto/rea mcp", doctor: true, setup: "idempotent", clients: 2, backupReadback: true, failureRecovery: true, skill: true, mcpTools: 50, targetFree: true })}\n`,
);
} finally {
if (tarball) await rm(join(root, tarball), { force: true });
+25 -37
View File
@@ -9,10 +9,6 @@ import { TOOL_CONTRACTS } from "../dist/contracts/toolContracts.js";
const execFileAsync = promisify(execFile);
const timeout = 180_000;
const hopperProcessPrefix = "/Applications/Hopper Disassembler.app/Contents/";
// Snapshot bundle processes so cleanup terminates only Hopper processes this
// verifier caused to appear, never an instance the user already had running.
const hopperProcessesBefore = await hopperProcessIds();
const sessionsBefore = new Set(
(await readdir("/tmp")).filter((name) => name.startsWith("rea-")),
);
@@ -77,6 +73,9 @@ const requireFunctionDossier = (value, expectedAddress) => {
"callers",
"callees",
"incoming_references",
"outgoing_references",
"referenced_strings",
"referenced_names",
"basic_blocks",
]) {
const collection = value[field];
@@ -89,6 +88,27 @@ const requireFunctionDossier = (value, expectedAddress) => {
throw new Error(`analyze_function returned an invalid ${field} result`);
}
}
for (const field of ["callers", "callees"]) {
for (const identity of value[field].items) {
if (
typeof identity?.address !== "string" ||
typeof identity?.name !== "string"
) {
throw new Error(`analyze_function returned an untyped ${field} item`);
}
}
}
for (const block of value.basic_blocks.items) {
if (!Array.isArray(block?.successors)) {
throw new Error("analyze_function omitted CFG successor evidence");
}
}
if (
typeof value.instruction_scan?.scanned !== "number" ||
typeof value.instruction_scan?.truncated !== "boolean"
) {
throw new Error("analyze_function omitted instruction scan limitations");
}
return value;
};
@@ -156,7 +176,7 @@ try {
const expectedNames = TOOL_CONTRACTS.map(({ name }) => name).sort();
const actualNames = listed.tools.map(({ name }) => name).sort();
if (JSON.stringify(actualNames) !== JSON.stringify(expectedNames)) {
throw new Error("The real server did not expose the intended 46 tools");
throw new Error("The real server did not expose the intended 50 tools");
}
const options = { timeout };
@@ -314,7 +334,6 @@ try {
await transport.close();
} finally {
clearInterval(keepAlive);
await terminateNewHopperProcesses(hopperProcessesBefore);
}
}
@@ -350,34 +369,3 @@ await new Promise((resolve, reject) => {
},
);
});
async function hopperProcessIds() {
const result = await execFileAsync("ps", ["-ax", "-o", "pid=,command="]);
return new Set(
result.stdout
.split("\n")
.map((line) => line.trim().match(/^(\d+)\s+(.+)$/))
.filter((match) => match?.[2]?.startsWith(hopperProcessPrefix) === true)
.map((match) => Number(match[1])),
);
}
async function terminateNewHopperProcesses(previous) {
const current = await hopperProcessIds();
const owned = [...current].filter((pid) => !previous.has(pid));
for (const pid of owned) signalProcess(pid, "SIGTERM");
await new Promise((resolve) => setTimeout(resolve, 1_000));
for (const pid of owned)
if (signalProcess(pid, 0)) signalProcess(pid, "SIGKILL");
}
function signalProcess(pid, signal) {
try {
process.kill(pid, signal);
return true;
} catch (cause) {
if (cause instanceof Error && "code" in cause && cause.code === "ESRCH")
return false;
throw cause;
}
}