fix(inspector): preserve lossy Node discovery locations (#542)
Keep explicitly selected Node Inspector targets observable when discovery cannot verify their file location. Resolve literal POSIX discovery paths without URI decoding, and retain lossy Windows or underscore paths as unresolved metadata. Validate scriptParsed file URLs independently. Preserve unknown target identity through runtime reconciliation, regenerate contracts, and verify real CLI/MCP behavior on Linux and Windows. Closes #530 Assisted-by: Kogasa (gensokyo-skills:kogasa-surprise-testing)
This commit is contained in:
@@ -1,82 +1,180 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile, spawn } from "node:child_process";
|
||||
import { once } from "node:events";
|
||||
import { dirname, resolve } from "node:path";
|
||||
import { mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { dirname, join, resolve } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { Client } from "@modelcontextprotocol/client";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
||||
|
||||
const execute = promisify(execFile);
|
||||
const root = resolve(
|
||||
dirname(fileURLToPath(import.meta.url)),
|
||||
"../tests/conformance/runtime-inspector",
|
||||
);
|
||||
const targetPath = resolve(root, "node-target.mjs");
|
||||
const child = spawn(process.execPath, ["--inspect=127.0.0.1:0", targetPath], {
|
||||
stdio: ["ignore", "ignore", "pipe"],
|
||||
const repository = resolve(dirname(fileURLToPath(import.meta.url)), "..");
|
||||
const root = await realpath(await mkdtemp(join(tmpdir(), "rea-inspector-")));
|
||||
const environment = {
|
||||
...process.env,
|
||||
REA_V8_INSPECTOR_OBSERVE_ENABLED: "true",
|
||||
};
|
||||
const client = new Client({
|
||||
name: "rea-inspector-conformance",
|
||||
version: "1.0.0",
|
||||
});
|
||||
const transport = new StdioClientTransport({
|
||||
command: process.execPath,
|
||||
args: ["scripts/rea.mjs", "mcp"],
|
||||
cwd: repository,
|
||||
env: environment,
|
||||
stderr: "pipe",
|
||||
});
|
||||
const names = [
|
||||
"node-target.mjs",
|
||||
"plain.cjs",
|
||||
"with space.cjs",
|
||||
"with_under_score.cjs",
|
||||
"with#hash.cjs",
|
||||
"with%percent.cjs",
|
||||
"with%23hash.cjs",
|
||||
...(process.platform === "win32" ? [] : ['with"quote.cjs']),
|
||||
];
|
||||
|
||||
try {
|
||||
const webSocketUrl = await inspectorUrl(child);
|
||||
const socket = new URL(webSocketUrl);
|
||||
await client.connect(transport);
|
||||
for (const name of names) {
|
||||
const targetPath =
|
||||
name === "node-target.mjs"
|
||||
? await realpath(
|
||||
join(repository, "tests/conformance/runtime-inspector", name),
|
||||
)
|
||||
: join(root, name);
|
||||
if (name !== "node-target.mjs")
|
||||
await writeFile(targetPath, "setInterval(() => {}, 1000);\n");
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
["--inspect=127.0.0.1:0", targetPath],
|
||||
{
|
||||
stdio: ["ignore", "ignore", "pipe"],
|
||||
},
|
||||
);
|
||||
try {
|
||||
await verifyTarget(child, targetPath, name);
|
||||
} finally {
|
||||
await terminate(child);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
await client.close();
|
||||
} finally {
|
||||
await rm(root, { recursive: true, force: true });
|
||||
}
|
||||
}
|
||||
|
||||
async function verifyTarget(child, targetPath, name) {
|
||||
const socket = new URL(await inspectorUrl(child));
|
||||
const endpoint = `http://127.0.0.1:${socket.port}`;
|
||||
const environment = {
|
||||
...process.env,
|
||||
REA_V8_INSPECTOR_OBSERVE_ENABLED: "true",
|
||||
};
|
||||
const listed = await runCli(
|
||||
["list-javascript-runtime-targets", endpoint, "--json"],
|
||||
environment,
|
||||
);
|
||||
const target = listed.normalized_result?.targets?.[0];
|
||||
if (
|
||||
listed.operation !== "list_javascript_runtime_targets" ||
|
||||
typeof target?.target_id !== "string" ||
|
||||
target.location?.kind !== "file" ||
|
||||
target.location.file_path !== targetPath
|
||||
)
|
||||
throw new Error("Real Node Inspector target discovery did not match");
|
||||
const observed = await runCli(
|
||||
[
|
||||
"observe-javascript-runtime",
|
||||
endpoint,
|
||||
target.target_id,
|
||||
"--runtime-kind",
|
||||
"node",
|
||||
"--observation-ms",
|
||||
"100",
|
||||
"--json",
|
||||
],
|
||||
environment,
|
||||
);
|
||||
const result = observed.normalized_result;
|
||||
if (
|
||||
observed.operation !== "observe_javascript_runtime" ||
|
||||
observed.provider?.id !== "rea-v8-inspector" ||
|
||||
result?.target?.target_id !== target.target_id ||
|
||||
result?.target?.runtime_kind !== "node" ||
|
||||
!Array.isArray(result?.scripts?.items) ||
|
||||
result.scripts.items.length === 0
|
||||
)
|
||||
throw new Error("Real Node Inspector observation did not match");
|
||||
if (
|
||||
!Array.isArray(result.directly_observed) ||
|
||||
!Array.isArray(result.unavailable_without_instrumentation) ||
|
||||
result.capture?.truncated !== false
|
||||
)
|
||||
throw new Error("Real Node Inspector capture was incomplete");
|
||||
const response = await fetch(`${endpoint}/json/list`);
|
||||
assert.equal(response.ok, true);
|
||||
const discovery = await response.json();
|
||||
assert.equal(discovery.length, 1);
|
||||
const raw = discovery[0];
|
||||
assert.equal(typeof raw.id, "string");
|
||||
// Independent oracle: the fixture owns the pathname, and Node's discovery
|
||||
// serialization loses quotes/backslashes. Never decode underscores into it.
|
||||
const unresolved = process.platform === "win32" || /[_"\\]/u.test(targetPath);
|
||||
const expectedLocation = unresolved
|
||||
? {
|
||||
kind: "unresolved",
|
||||
reported_url: raw.url,
|
||||
reason: "unverifiable-file-location",
|
||||
}
|
||||
: { kind: "file", file_path: targetPath };
|
||||
const listed = await runCli([
|
||||
"list-javascript-runtime-targets",
|
||||
endpoint,
|
||||
"--json",
|
||||
]);
|
||||
assert.equal(listed.operation, "list_javascript_runtime_targets");
|
||||
const inventory = listed.normalized_result;
|
||||
assert.equal(inventory.targets.length, 1);
|
||||
assert.equal(inventory.targets[0].target_id, raw.id);
|
||||
assert.deepEqual(inventory.targets[0].location, expectedLocation);
|
||||
assert.equal(inventory.excluded.unsupported_location, 0);
|
||||
const mcpList = await runMcp("list_javascript_runtime_targets", {
|
||||
inspector_endpoint: endpoint,
|
||||
});
|
||||
assert.deepEqual(mcpList, inventory);
|
||||
|
||||
const observed = await runCli([
|
||||
"observe-javascript-runtime",
|
||||
endpoint,
|
||||
raw.id,
|
||||
"--runtime-kind",
|
||||
"node",
|
||||
"--observation-ms",
|
||||
"100",
|
||||
"--json",
|
||||
]);
|
||||
assert.equal(observed.operation, "observe_javascript_runtime");
|
||||
assert.equal(observed.provider.id, "rea-v8-inspector");
|
||||
const mcpObservation = await runMcp("observe_javascript_runtime", {
|
||||
inspector_endpoint: endpoint,
|
||||
target_id: raw.id,
|
||||
runtime_kind: "node",
|
||||
observation_ms: 100,
|
||||
});
|
||||
for (const result of [observed.normalized_result, mcpObservation])
|
||||
verifyObservation(result, raw.id, expectedLocation, targetPath);
|
||||
assert.deepEqual(mcpObservation.target, observed.normalized_result.target);
|
||||
process.stdout.write(
|
||||
`${JSON.stringify({
|
||||
status: "pass",
|
||||
provider: observed.provider.id,
|
||||
target_id: target.target_id,
|
||||
platform: process.platform,
|
||||
node: process.version,
|
||||
fixture: name,
|
||||
target_id: raw.id,
|
||||
discovery_url: raw.url,
|
||||
target_location: expectedLocation,
|
||||
cli_scripts: observed.normalized_result.scripts.items.length,
|
||||
mcp_scripts: mcpObservation.scripts.items.length,
|
||||
listed_evidence_id: listed.evidence_id,
|
||||
observation_evidence_id: observed.evidence_id,
|
||||
scripts: result.scripts.items.length,
|
||||
contexts: result.execution_contexts.length,
|
||||
endpoint,
|
||||
})}\n`,
|
||||
);
|
||||
} finally {
|
||||
await terminate(child);
|
||||
}
|
||||
|
||||
function verifyObservation(result, targetId, expectedLocation, targetPath) {
|
||||
assert.equal(result.target.target_id, targetId);
|
||||
assert.equal(result.target.runtime_kind, "node");
|
||||
assert.deepEqual(result.target.location, expectedLocation);
|
||||
assert.equal(result.capture.truncated, false);
|
||||
assert.ok(Array.isArray(result.directly_observed));
|
||||
assert.ok(Array.isArray(result.unavailable_without_instrumentation));
|
||||
assert.ok(
|
||||
result.scripts.items.some(
|
||||
({ location }) =>
|
||||
location.kind === "file" && location.file_path === targetPath,
|
||||
),
|
||||
`Actual entry script was not independently resolved: ${targetPath}`,
|
||||
);
|
||||
if (expectedLocation.kind === "unresolved")
|
||||
assert.ok(
|
||||
result.unknowns.includes(
|
||||
"The discovery-reported file location cannot be verified; loaded script locations are resolved independently from Debugger.scriptParsed.",
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
async function runMcp(name, arguments_) {
|
||||
const response = await client.callTool({ name, arguments: arguments_ });
|
||||
assert.notEqual(
|
||||
response.isError,
|
||||
true,
|
||||
JSON.stringify(response.structuredContent),
|
||||
);
|
||||
assert.ok(response.structuredContent?.result);
|
||||
return response.structuredContent.result;
|
||||
}
|
||||
|
||||
async function inspectorUrl(process_) {
|
||||
@@ -91,30 +189,41 @@ async function inspectorUrl(process_) {
|
||||
throw new Error("Node exited before reporting its Inspector URL");
|
||||
}
|
||||
|
||||
async function runCli(arguments_, environment) {
|
||||
async function runCli(arguments_) {
|
||||
const { stdout } = await execute(
|
||||
process.execPath,
|
||||
["scripts/rea.mjs", ...arguments_],
|
||||
{
|
||||
cwd: resolve(dirname(fileURLToPath(import.meta.url)), ".."),
|
||||
cwd: repository,
|
||||
env: environment,
|
||||
maxBuffer: 16 * 1_024 * 1_024,
|
||||
timeout: 30_000,
|
||||
},
|
||||
);
|
||||
return JSON.parse(stdout);
|
||||
}
|
||||
|
||||
async function terminate(process_) {
|
||||
if (process_.exitCode !== null) return;
|
||||
if (process_.exitCode !== null || process_.signalCode !== null) return;
|
||||
const exited = once(process_, "exit");
|
||||
process_.kill("SIGTERM");
|
||||
const terminated = await Promise.race([
|
||||
exited.then(() => true),
|
||||
new Promise((resolveTimeout) =>
|
||||
setTimeout(() => resolveTimeout(false), 2_000),
|
||||
),
|
||||
]);
|
||||
if (terminated || process_.exitCode !== null) return;
|
||||
process_.kill("SIGKILL");
|
||||
await exited;
|
||||
let timer;
|
||||
try {
|
||||
const terminated = await Promise.race([
|
||||
exited.then(() => true),
|
||||
new Promise((resolveTimeout) => {
|
||||
timer = setTimeout(() => resolveTimeout(false), 2_000);
|
||||
}),
|
||||
]);
|
||||
if (
|
||||
terminated ||
|
||||
process_.exitCode !== null ||
|
||||
process_.signalCode !== null
|
||||
)
|
||||
return;
|
||||
process_.kill("SIGKILL");
|
||||
await exited;
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user