fix(hopper): recover provenance-verified Spotlight exits

Verify the native helper's sealed identifier and pinned Apple signing team in BridgeLauncher, then carry that provenance to HopperClient. Only a verified native helper's complete not-found exit confirms no document handoff, allowing runtime and target-lease cleanup and retry; wrappers retain unknown document state.

Share publisher identity with setup through the domain contract. Correct fixture teardown ordering and parameterized MCP coverage. Validation: 54 focused tests passed across boundary, CLI, MCP acceptance, diagnostics and installer coverage (one Linux-only case skipped); check:fast passed. Real codesign verification accepted the installed official Hopper helper; Spotlight failures and bridge recovery used process fixtures.
This commit is contained in:
morluto
2026-10-14 06:19:47 +08:00
parent 4d98123279
commit 4acda94b76
13 changed files with 286 additions and 112 deletions
@@ -1,6 +1,6 @@
import { chmod, copyFile, rm } from "node:fs/promises";
import { describe, expect, onTestFinished } from "vitest";
import { describe, expect } from "vitest";
import { z } from "zod";
import { workspaceCliTest } from "../../support/cli/workspaceCliFixture.js";
@@ -39,7 +39,57 @@ describe("Hopper launcher failure through the one-shot CLI", () => {
const targetPath = workspace.path("probe");
await copyFile("/usr/bin/true", targetPath);
const loaderArgs = ["--probe"];
onTestFinished(async () => {
try {
for (const attempt of ["first", "retry"]) {
const result = await cli.run({
arguments: [
"analyze",
targetPath,
"--provider",
"hopper",
"--json",
],
environment: {
...workspace.environment,
HOPPER_LAUNCHER_PATH: launcherPath,
HOPPER_LOADER_ARGS_JSON: JSON.stringify(loaderArgs),
REA_LOG_LEVEL: "silent",
},
timeoutMs: 45_000,
});
const retained = RetainedRuntime.safeParse(result.json);
if (retained.success)
await rm(
retained.data.details.diagnostics.cleanup_error.details
.diagnostics.runtime_root,
{ recursive: true, force: true },
);
expect(result.exitCode, attempt).toBe(1);
expect(result.json, attempt).toMatchObject({
code: "cleanup_incomplete",
details: {
cleanup: "incomplete",
diagnostics: {
primary_error: {
code: "provider_unavailable",
details: {
stage: "launch",
launcher: {
exit_code: 2,
stderr: {
text: expect.stringContaining(
"Document state is unknown",
),
},
},
},
},
},
},
});
}
} finally {
const recovered = await acquireHopperTargetLease({
targetPath,
targetKind: "executable",
@@ -48,50 +98,6 @@ describe("Hopper launcher failure through the one-shot CLI", () => {
});
expect(recovered.acquired).toBe(true);
if (recovered.acquired) await recovered.lease.release();
});
for (const attempt of ["first", "retry"]) {
const result = await cli.run({
arguments: ["analyze", targetPath, "--provider", "hopper", "--json"],
environment: {
...workspace.environment,
HOPPER_LAUNCHER_PATH: launcherPath,
HOPPER_LOADER_ARGS_JSON: JSON.stringify(loaderArgs),
REA_LOG_LEVEL: "silent",
},
timeoutMs: 45_000,
});
const retained = RetainedRuntime.safeParse(result.json);
if (retained.success)
await rm(
retained.data.details.diagnostics.cleanup_error.details.diagnostics
.runtime_root,
{ recursive: true, force: true },
);
expect(result.exitCode, attempt).toBe(1);
expect(result.json, attempt).toMatchObject({
code: "cleanup_incomplete",
details: {
cleanup: "incomplete",
diagnostics: {
primary_error: {
code: "provider_unavailable",
details: {
stage: "launch",
launcher: {
exit_code: 2,
stderr: {
text: expect.stringContaining(
"Document state is unknown",
),
},
},
},
},
},
},
});
}
},
);
@@ -15,6 +15,7 @@ import type {
} from "../../../../src/hopper/BridgeLauncher.js";
import { HopperApplicationLauncher } from "../../../../src/hopper/BridgeLauncher.js";
import { HopperClient } from "../../../../src/hopper/HopperClient.js";
import { acquireHopperTargetLease } from "../../../../src/hopper/HopperTargetLease.js";
import { HopperFixtureLauncher } from "./hopperClient.fixture.js";
// Failure assertions keep the external document unknown. Teardown then publishes
@@ -118,6 +119,86 @@ it("does not equate an owned native helper exit with an external GUI exit", asyn
});
});
it.skipIf(process.platform !== "darwin")(
"releases the runtime and target lease after a provenance-verified Spotlight exit, then retries",
async () => {
const directory = await createTestTempDirectory(
"rea-hopper-native-not-found-",
);
const targetPath = join(directory, "target");
await writeFile(targetPath, "fixture target");
const loaderArgs = [
"-e",
'process.stderr.write("Hopper has not been found.\\nPlease make sure that Spotlight is active in order for this tool to find Hopper\'s executable.\\n", () => process.exit(3))',
"--",
];
// Simulate a verified native helper at the launcher provenance boundary;
// publisher verification itself has real codesign boundary coverage.
const native = new HopperApplicationLauncher(
{
environment: {},
launcherPath: process.execPath,
targetPath,
targetKind: "executable",
loaderArgs,
bridgeScriptPath: "/rea/hopper_bridge.py",
launchMode: "native",
},
{ verifyNativeHelper: async () => "verified" },
);
const retry = new HopperFixtureLauncher();
let session: BridgeSession | undefined;
const client = new HopperClient({
launcher: {
async launch(input, options) {
if (session !== undefined) return retry.launch(input);
session = input;
const launched = await native.launch(input, options);
expect(launched).toMatchObject({
ok: true,
value: { nativeHelperVerified: true },
});
return launched;
},
},
startupTimeoutMs: 10_000,
});
onTestFinished(async () => {
const closed = await client.close();
if (!closed.ok) throw closed.error;
});
const failed = await client.start();
expect(failed).toMatchObject({
ok: false,
error: { _tag: "HopperStartError", cleanupIncomplete: false },
});
if (failed.ok) throw new Error("Expected Spotlight failure");
expect(projectAnalysisError(failed.error)).toMatchObject({
code: "provider_unavailable",
message: expect.stringContaining("Spotlight"),
details: { launcher: { exit_code: 3 } },
});
if (session === undefined) throw new Error("Missing launch session");
await expect(access(session.directory)).rejects.toMatchObject({
code: "ENOENT",
});
const lease = await acquireHopperTargetLease({
targetPath,
targetKind: "executable",
loaderArgs,
runId: "spotlight-retry-proof",
});
if (!lease.acquired)
throw new Error("Spotlight failure retained the target lease");
await lease.lease.release();
expect(await client.start()).toMatchObject({ ok: true });
expect(await client.callTool("echo", { value: "retried" })).toEqual({
ok: true,
value: { value: "retried" },
});
},
);
it.each(["exit", "signal", "exit75"] as const)(
"reports a failed non-owning launcher %s without waiting for bridge timeout or inventing a GUI exit",
async (termination) => {
@@ -6,10 +6,11 @@ import { promisify } from "node:util";
import { describe, expect, it } from "vitest";
import {
HOPPER_PUBLISHER,
macHopperInstallDisclosure,
systemMacHopperInstallHost,
} from "../../../src/application/MacHopper.js";
import { verifyMacHopperNativeHelper } from "../../../src/hopper/HopperMacPublisher.js";
import { HOPPER_PUBLISHER } from "../../../src/domain/hopperPublisher.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
const run = promisify(execFile);
@@ -94,6 +95,24 @@ describe("macOS Hopper publisher verification", () => {
);
});
itDarwin(
"withholds native-helper provenance from custom signed or modified executables",
async () => {
const root = await createTestTempDirectory(
"rea-hopper-helper-publisher-",
);
const app = await buildBundle(root, "hopper");
const executable = join(app, ...executableSegments);
await expect(
verifyMacHopperNativeHelper(executable, process.env),
).resolves.toBe("unknown");
await appendFile(executable, "modified\n");
await expect(
verifyMacHopperNativeHelper(executable, process.env),
).resolves.toBe("unknown");
},
);
// Opt-in positive control: point this at a mounted official Hopper bundle,
// for example `hdiutil attach -readonly -nobrowse Hopper-6.6.0-demo.dmg`.
// It proves the pinned requirement is satisfiable by the real publisher, so
@@ -103,6 +122,12 @@ describe("macOS Hopper publisher verification", () => {
"accepts the officially signed Hopper application",
async () => {
await expect(host().validBundle(realApp ?? "")).resolves.toBe("verified");
await expect(
verifyMacHopperNativeHelper(
join(realApp ?? "", ...executableSegments),
process.env,
),
).resolves.toBe("verified");
},
);
});