fix(javascript): integrate current provider timeout recovery

Include the main-branch Hopper startup timeout normalization already merged
in #1866 so combined CI retains launcher diagnostics when either timer fires.

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
N0zoM1z0
2026-10-11 14:53:16 +00:00
co-authored by factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> Kaoru0822
59 changed files with 2090 additions and 300 deletions
@@ -1,4 +1,5 @@
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { mkdir, writeFile } from "node:fs/promises";
import { createRequire } from "node:module";
import { join, resolve } from "node:path";
@@ -770,6 +771,173 @@ describe("export writes in function evaluation contexts", () => {
});
});
it("retains namespace re-export bindings and provenance through stdio MCP and CLI", async () => {
const root = await createTestTempDirectory("rea-namespace-reexports-");
const declarations = [
{ clause: "* as ns", name: "ns", imported: "*" },
{
clause: '* as "namespace value"',
name: "namespace value",
imported: "*",
},
{ clause: "{ value as forwarded }", name: "forwarded", imported: "value" },
{ clause: "*", name: "*", imported: "*" },
];
const source = declarations
.map(({ clause }) => `export ${clause} from "./dependency.mjs";`)
.join("\n");
const path = join(root, "barrel.mjs");
await Promise.all([
writeFile(path, source),
writeFile(join(root, "dependency.mjs"), "export const value = 42;"),
]);
// Node executes only this test-owned fixture to establish its actual exports.
for (const name of ["ns", "namespace value"])
expect(await runtimeExport(path, name)).toEqual({ value: 42 });
for (const name of ["forwarded", "value"])
expect(await runtimeExport(path, name)).toBe(42);
const documents = [
await analyzeThroughMcp(root),
await runCli(["analyze-javascript-application", root, "--json"]),
];
const digest = createHash("sha256").update(source).digest("hex");
for (const document of documents) {
const { graph } = applicationEnvelope.parse(document).normalized_result;
const sourceModule = graph.nodes.find(({ observations }) =>
observations.some(
({ properties }) =>
properties.semantic_role === "source-module" &&
properties.path === "barrel.mjs",
),
);
expect(sourceModule?.observations[0]?.properties).toMatchObject({
export_relationships: 4,
export_names: ["*", "forwarded", "namespace value", "ns"],
omitted_export_names: 0,
});
for (const [index, { clause, name, imported }] of declarations.entries()) {
const exported = graph.nodes.find(({ observations }) =>
observations.some(
({ properties }) =>
properties.semantic_role === "export-binding" &&
properties.module_path === "barrel.mjs" &&
properties.exported_name === name,
),
);
const observation = exported?.observations[0];
expect(observation?.properties).toMatchObject({
relationship_kind: "re-export",
declared_specifier: "./dependency.mjs",
imported_name: imported,
local_name: null,
});
if (imported === "*" && name !== "*")
expect(observation?.evidence).toMatchObject({
artifact: { available: true, sha256: digest },
location: {
available: true,
value: {
kind: "source-range",
source: "barrel.mjs",
start: { line: index + 1, column: 7 },
end: { line: index + 1, column: 7 + clause.length },
},
},
});
expect(graph.edges).toContainEqual(
expect.objectContaining({
source_node_id: sourceModule?.node_id,
target_node_id: exported?.node_id,
relation: "exposes",
properties: expect.objectContaining({ exported_name: name }),
evidence: expect.objectContaining({
artifact: observation?.evidence.artifact,
location: observation?.evidence.location,
}),
}),
);
expect(graph.edges).toContainEqual(
expect.objectContaining({
source_node_id: exported?.node_id,
relation: "imports",
properties: expect.objectContaining({
module_link_kind: "re-export",
imported_name: imported,
exported_name: name,
resolved_path: "dependency.mjs",
resolution_status: "resolved",
}),
}),
);
}
expect(graph.coverage).toMatchObject({
status: "complete",
omitted_count: 0,
truncated: false,
});
}
});
it("excludes erased namespace type exports through stdio MCP and CLI", async () => {
const root = await createTestTempDirectory("rea-namespace-type-exports-");
const path = join(root, "barrel.mts");
await Promise.all([
writeFile(
path,
'export type * as Types from "./types.mjs";\nexport * as Values from "./values.mjs";',
),
writeFile(
join(root, "types.mjs"),
'throw new Error("Type-only dependency must not execute");',
),
writeFile(join(root, "values.mjs"), "export const value = 42;"),
]);
// Node strips the type-only declaration without loading its throwing target.
expect(await runtimeExport(path, "Types")).toBeUndefined();
expect(await runtimeExport(path, "Values")).toEqual({ value: 42 });
const documents = [
await analyzeThroughMcp(root),
await runCli(["analyze-javascript-application", root, "--json"]),
];
for (const document of documents) {
const { graph } = applicationEnvelope.parse(document).normalized_result;
const observations = graph.nodes.flatMap(
({ observations }) => observations,
);
expect(
observations.find(
({ properties }) =>
properties.semantic_role === "source-module" &&
properties.path === "barrel.mts",
)?.properties,
).toMatchObject({
export_relationships: 1,
export_names: ["Values"],
omitted_export_names: 0,
});
expect(
observations.filter(
({ properties }) =>
properties.semantic_role === "export-binding" &&
properties.module_path === "barrel.mts" &&
properties.exported_name === "Types",
),
).toEqual([]);
expect(
graph.edges.filter(
({ properties }) => properties.exported_name === "Types",
),
).toEqual([]);
expect(graph.coverage).toMatchObject({
status: "complete",
omitted_count: 0,
truncated: false,
});
}
});
const expectRuntimeExports = async (
fixture: Awaited<ReturnType<typeof analyzeVersions>>,
expected: (count: number) => unknown,
@@ -0,0 +1,333 @@
import { spawn, type ChildProcess } from "node:child_process";
import { access, copyFile, readFile, rm, writeFile } from "node:fs/promises";
import { dirname, resolve } from "node:path";
import { setTimeout as delay } from "node:timers/promises";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { expect, onTestFinished, vi } from "vitest";
import { z } from "zod";
import { parseMcpToolError } from "../../fixtures/mcpToolError.js";
import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js";
import { acquireHopperTargetLease } from "../../../src/hopper/HopperTargetLease.js";
const statusSchema = z.object({ result: z.object({ open: z.boolean() }) });
const test = workspaceCliTest.skipIf(process.platform !== "darwin");
test(
"retains a denial-shaped failure when caller arguments can alter the helper's AppleScript",
{ timeout: 90_000 },
async ({ workspace }) => {
for (const source of ["target", "loader"] as const) {
const target = workspace.path(
source === "target" ? 'quoted"target' : "loader-target",
);
await copyFile("/usr/bin/true", target);
const mode = await workspace.write(
`${source}-mode`,
"denied-after-handoff",
);
const receipt = workspace.path(`${source}-bootstrap`);
const loaderArgs = [
resolve("tests/fixtures/hopperLaunchAdmission.mjs"),
mode,
receipt,
...(source === "loader" ? ['caller"loader'] : []),
];
const transport = new StdioClientTransport({
command: process.execPath,
args: [resolve("dist/main.js")],
stderr: "pipe",
env: {
...workspace.environment,
PATH: process.env.PATH ?? "",
REA_ANALYSIS_PROVIDER: "hopper",
HOPPER_LAUNCHER_PATH: process.execPath,
HOPPER_LOADER_ARGS_JSON: JSON.stringify(loaderArgs),
REA_LOG_LEVEL: "silent",
},
});
const mcp = new Client({ name: "hopper-literal-boundary", version: "1" });
let runtime: string | undefined;
onTestFinished(async () => {
// SDK close can return immediately after SIGKILL. Wait for its actual
// child-close event before recovering this process's retained lease.
let serverExited: Promise<void> | undefined;
if (transport.pid !== null) {
const onclose = transport.onclose;
serverExited = new Promise<void>((resolveExit) => {
transport.onclose = () => {
onclose?.();
resolveExit();
};
});
}
await mcp.close();
await transport.close();
await serverExited;
// The fixture created only this marker, never a native GUI document.
if (runtime !== undefined)
await rm(runtime, { recursive: true, force: true });
const acquired = await acquireHopperTargetLease({
targetPath: target,
targetKind: "executable",
loaderArgs,
runId: "literal-boundary-test-cleanup",
});
if (!acquired.acquired) throw new Error("Fixture lease remained live");
await acquired.lease.release();
});
await mcp.connect(transport);
const call = (name: string, arguments_: Record<string, string> = {}) =>
mcp.callTool({ name, arguments: arguments_ });
expect((await call("open_binary", { path: target })).isError).not.toBe(
true,
);
const first = parseMcpToolError(await call("current_document"));
runtime = dirname(await readFile(receipt, "utf8"));
expect(first.error).toMatchObject({
code: "cleanup_incomplete",
details: {
diagnostics: {
primary_error: {
code: "provider_unavailable",
details: {
launcher: {
exit_code: 2,
stderr: {
text: expect.stringContaining(
"Not authorized to send Apple events",
),
},
},
},
},
},
},
});
await access(`${receipt}.document-created`);
await access(runtime);
expect(parseMcpToolError(await call("list_procedures"))).toEqual(first);
expect(parseMcpToolError(await call("close_binary")).error.code).toBe(
"cleanup_incomplete",
);
}
},
);
test("recovers a denied Hopper launch through the same stdio MCP session and CLI", async ({
workspace,
cli,
}) => {
const target = workspace.path("target");
await copyFile("/usr/bin/true", target);
const mode = await workspace.write("mode", "denied");
const receipt = workspace.path("bootstrap-path");
const environment = {
...workspace.environment,
PATH: process.env.PATH ?? "",
REA_ANALYSIS_PROVIDER: "hopper",
HOPPER_LAUNCHER_PATH: process.execPath,
HOPPER_LOADER_ARGS_JSON: JSON.stringify([
resolve("tests/fixtures/hopperLaunchAdmission.mjs"),
mode,
receipt,
]),
REA_LOG_LEVEL: "silent",
};
const transport = new StdioClientTransport({
command: process.execPath,
args: [resolve("dist/main.js")],
env: environment,
stderr: "pipe",
});
const mcp = new Client({ name: "hopper-startup-recovery", version: "1" });
let stderr = "";
transport.stderr?.on("data", (chunk: Buffer) => {
stderr += chunk.toString();
});
onTestFinished(async () => {
await mcp.close();
await transport.close();
});
await mcp.connect(transport);
const call = (name: string, arguments_: Record<string, string> = {}) =>
mcp.callTool({ name, arguments: arguments_ }).catch((cause: unknown) => {
throw new Error(`${name}: ${stderr}`, { cause });
});
expect((await call("open_binary", { path: target })).isError).not.toBe(true);
for (const operation of ["current_document", "list_procedures"]) {
const { error } = parseMcpToolError(await call(operation));
expect(error).toMatchObject({
code: "provider_unavailable",
details: {
stage: "launch",
launcher: {
exit_code: 2,
stderr: {
text: expect.stringContaining(
"Not authorized to send Apple events",
),
},
},
},
});
expect(error.message).toContain("Automation");
const bootstrap = await readFile(receipt, "utf8");
await expect(access(dirname(bootstrap))).rejects.toMatchObject({
code: "ENOENT",
});
}
expect((await call("close_binary")).isError).not.toBe(true);
const deniedCli = await cli.run({
arguments: ["analyze", target, "--provider", "hopper", "--json"],
environment,
timeoutMs: 15_000,
});
expect(deniedCli.exitCode).toBe(1);
expect(deniedCli.json).toMatchObject({
code: "provider_unavailable",
details: { launcher: { exit_code: 2 } },
});
await writeFile(mode, "bridge");
expect((await call("open_binary", { path: target })).isError).not.toBe(true);
expect((await call("current_document")).isError).not.toBe(true);
expect((await call("close_binary")).isError).not.toBe(true);
expect(
statusSchema.parse((await call("binary_session")).structuredContent).result
.open,
).toBe(false);
});
test(
"preserves a timed-out handoff and closes only a late bridge with matching run identity",
{ timeout: 60_000 },
async ({ workspace }) => {
const target = workspace.path("target");
await copyFile("/usr/bin/true", target);
const mode = await workspace.write("mode", "handoff");
const receipt = workspace.path("bootstrap-path");
const transport = new StdioClientTransport({
command: process.execPath,
args: [resolve("dist/main.js")],
stderr: "pipe",
env: {
...workspace.environment,
PATH: process.env.PATH ?? "",
REA_ANALYSIS_PROVIDER: "hopper",
HOPPER_LAUNCHER_PATH: process.execPath,
HOPPER_LOADER_ARGS_JSON: JSON.stringify([
resolve("tests/fixtures/hopperLaunchAdmission.mjs"),
mode,
receipt,
]),
REA_HOPPER_STARTUP_TIMEOUT_MS: "2000",
REA_LOG_LEVEL: "silent",
},
});
const mcp = new Client({ name: "hopper-late-readiness", version: "1" });
const children: ChildProcess[] = [];
let runtime: string | undefined;
const stop = async (child: ChildProcess) => {
if (child.exitCode !== null || child.signalCode !== null) return;
const exited = new Promise<void>((resolveExit) =>
child.once("exit", () => resolveExit()),
);
child.kill("SIGKILL");
await exited;
};
onTestFinished(async () => {
await Promise.all(children.map(stop));
await mcp.close();
await transport.close();
if (runtime !== undefined)
await rm(runtime, { recursive: true, force: true });
});
await mcp.connect(transport);
const call = (name: string, arguments_: Record<string, string> = {}) =>
mcp.callTool({ name, arguments: arguments_ });
expect((await call("open_binary", { path: target })).isError).not.toBe(
true,
);
const initial = parseMcpToolError(await call("current_document"));
expect(initial.error).toMatchObject({
code: "cleanup_incomplete",
details: {
diagnostics: {
primary_error: {
code: "provider_timeout",
details: { timeout_ms: 2000, launcher: { exit_code: 0 } },
},
},
},
});
expect(parseMcpToolError(await call("list_procedures"))).toEqual(initial);
const status = z.object({
result: z.object({
provider_operation_health: z.object({
state: z.literal("not_started"),
stage: z.literal("launch"),
}),
}),
});
status.parse((await call("binary_session")).structuredContent);
expect(parseMcpToolError(await call("close_binary")).error.code).toBe(
"cleanup_incomplete",
);
const bootstrap = await readFile(await readFile(receipt, "utf8"), "utf8");
const setting = (name: string): string => {
const line = bootstrap
.split("\n")
.find((value) => value.startsWith(`${name} = `));
if (line === undefined)
throw new Error(`Missing bootstrap setting ${name}`);
return z.string().parse(JSON.parse(line.slice(name.length + 3)));
};
const socket = setting("REA_SOCKET");
const token = setting("REA_TOKEN");
const runId = setting("REA_RUN_ID");
runtime = dirname(socket);
const lateBridge = async (identity: string, credential = token) => {
const child = spawn(
process.execPath,
[
resolve("tests/fixtures/fakeHopper.mjs"),
socket,
credential,
identity,
],
{ stdio: "ignore" },
);
children.push(child);
await vi.waitFor(() => access(socket));
return child;
};
for (const [identity, credential] of [
["different-run", token],
[runId, "different-token"],
] as const) {
const wrong = await lateBridge(identity, credential);
expect(parseMcpToolError(await call("close_binary")).error.code).toBe(
"cleanup_incomplete",
);
expect(wrong.exitCode).toBeNull();
await access(runtime);
await stop(wrong);
await rm(socket, { force: true });
}
const closing = call("close_binary");
await delay(150);
const owned = await lateBridge(runId);
expect((await closing).isError).not.toBe(true);
await vi.waitFor(() => expect(owned.exitCode).toBe(0));
await expect(access(runtime)).rejects.toMatchObject({ code: "ENOENT" });
await writeFile(mode, "bridge");
expect((await call("open_binary", { path: target })).isError).not.toBe(
true,
);
expect((await call("current_document")).isError).not.toBe(true);
expect((await call("close_binary")).isError).not.toBe(true);
},
);