feat(setup): make installation explicit and safe

This commit is contained in:
morluto
2026-07-13 15:13:59 +08:00
parent 1d1fe7b212
commit 3793889ce8
12 changed files with 793 additions and 497 deletions
+4 -4
View File
@@ -7,6 +7,7 @@ import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { parseBinaryTarget } from "../domain/binaryTarget.js";
import { supportsNodeVersion } from "../domain/runtimeVersion.js";
import { probeHomebrew } from "./homebrew.js";
import {
linuxHopperLauncherPath,
@@ -60,10 +61,9 @@ export const runDoctor = async (
readonly checks: readonly DoctorCheck[];
}> => {
const checks: DoctorCheck[] = [];
const nodeMajor = parseMajor(host.nodeVersion);
checks.push(
check("node", nodeMajor >= 24, host.nodeVersion, {
remediation: "Install Node.js 24.18 or newer.",
check("node", supportsNodeVersion(host.nodeVersion), host.nodeVersion, {
remediation: "Install Node.js 22.19+ or 24.11+.",
classification: "missing_dependency",
}),
);
@@ -107,7 +107,7 @@ export const runDoctor = async (
checks.push(
check("hopper", hopperPath !== undefined, hopperPath, {
remediation:
"Run rea setup --yes to install Hopper, or set HOPPER_LAUNCHER_PATH.",
"Run rea setup to install Hopper, or set HOPPER_LAUNCHER_PATH.",
classification: "missing_analysis_engine",
}),
);
+236
View File
@@ -0,0 +1,236 @@
import { createHash } from "node:crypto";
import { execFile } from "node:child_process";
import {
access,
mkdir,
mkdtemp,
readdir,
rename,
rm,
writeFile,
} from "node:fs/promises";
import { homedir, tmpdir } from "node:os";
import { join } from "node:path";
import { promisify } from "node:util";
import { z } from "zod";
const execFileAsync = promisify(execFile);
const RELEASES_URL =
"https://www.hopperapp.com/include/files-api.php?request=releases&public=true";
const DOWNLOAD_PREFIX = "https://www.hopperapp.com:443/downloader/public/";
const MAX_PACKAGE_BYTES = 100_000_000;
const BUNDLE_IDENTIFIER = "com.cryptic-apps.hopper-web-4";
/** Result of installing the official Hopper macOS application. */
export type MacHopperInstallResult =
| { readonly status: "installed"; readonly launcherPath: string }
| {
readonly status: "failed";
readonly reason:
| "release_metadata"
| "download"
| "integrity"
| "mount"
| "bundle"
| "destination_exists"
| "copy"
| "launcher_missing";
};
/** External effects used by the bounded macOS Hopper installer. */
export interface MacHopperInstallHost {
download(
url: string,
): Promise<{ readonly ok: boolean; readonly bytes: Uint8Array }>;
createTemporaryDirectory(): Promise<string>;
createMountDirectory(path: string): Promise<void>;
writePackage(path: string, bytes: Uint8Array): Promise<void>;
mount(packagePath: string, mountPath: string): Promise<boolean>;
unmount(mountPath: string): Promise<void>;
appBundle(mountPath: string): Promise<string | undefined>;
validBundle(appPath: string): Promise<boolean>;
destinationExists(path: string): Promise<boolean>;
installBundle(
source: string,
destination: string,
stage: string,
): Promise<boolean>;
launcherReady(path: string): Promise<boolean>;
openApplication(path: string): Promise<void>;
cleanup(path: string): Promise<void>;
destination(): string;
}
/** Download, verify, and install Hopper into the current user's Applications directory. */
export const installMacHopper = async (
host: MacHopperInstallHost = systemMacHopperInstallHost(),
): Promise<MacHopperInstallResult> => {
let temporary: string | undefined;
let mounted = false;
try {
const metadata = await host.download(RELEASES_URL);
if (!metadata.ok) return { status: "failed", reason: "release_metadata" };
const parsed = releasesSchema.safeParse(parseJson(metadata.bytes));
if (!parsed.success)
return { status: "failed", reason: "release_metadata" };
const release = parsed.data["OS X"];
if (!release.filename.startsWith(DOWNLOAD_PREFIX))
return { status: "failed", reason: "release_metadata" };
const archive = await host.download(release.filename);
if (!archive.ok) return { status: "failed", reason: "download" };
if (!integrityMatches(archive.bytes, release))
return { status: "failed", reason: "integrity" };
const destination = host.destination();
if (await host.destinationExists(destination))
return { status: "failed", reason: "destination_exists" };
temporary = await host.createTemporaryDirectory();
const packagePath = join(temporary, "hopper.dmg");
const mountPath = join(temporary, "mounted");
await host.createMountDirectory(mountPath);
await host.writePackage(packagePath, archive.bytes);
if (!(await host.mount(packagePath, mountPath)))
return { status: "failed", reason: "mount" };
mounted = true;
const source = await host.appBundle(mountPath);
if (source === undefined || !(await host.validBundle(source)))
return { status: "failed", reason: "bundle" };
const stage = `${destination}.rea-stage`;
if (!(await host.installBundle(source, destination, stage)))
return { status: "failed", reason: "copy" };
const launcherPath = join(destination, "Contents/MacOS/hopper");
if (!(await host.launcherReady(launcherPath)))
return { status: "failed", reason: "launcher_missing" };
await host.openApplication(destination);
return { status: "installed", launcherPath };
} catch {
return { status: "failed", reason: "download" };
} finally {
if (temporary !== undefined) {
const mountPath = join(temporary, "mounted");
if (mounted) await host.unmount(mountPath);
await host.cleanup(temporary);
}
}
};
const systemMacHopperInstallHost = (): MacHopperInstallHost => ({
async download(url) {
const response = await fetch(url, {
headers: { accept: "application/json", "user-agent": "rea-installer" },
signal: AbortSignal.timeout(120_000),
});
const length = Number(response.headers.get("content-length") ?? "0");
if (length > MAX_PACKAGE_BYTES)
return { ok: false, bytes: new Uint8Array() };
const bytes = new Uint8Array(await response.arrayBuffer());
return { ok: response.ok && bytes.byteLength <= MAX_PACKAGE_BYTES, bytes };
},
createTemporaryDirectory: () => mkdtemp(join(tmpdir(), "rea-hopper-mac-")),
createMountDirectory: (path) => mkdir(path),
writePackage: (path, bytes) => writeFile(path, bytes, { mode: 0o600 }),
async mount(packagePath, mountPath) {
return commandSucceeds("hdiutil", [
"attach",
"-readonly",
"-nobrowse",
"-mountpoint",
mountPath,
packagePath,
]);
},
async unmount(mountPath) {
await commandSucceeds("hdiutil", ["detach", mountPath, "-force"]);
},
async appBundle(mountPath) {
const entries = await readdir(mountPath, { withFileTypes: true });
const app = entries.find(
(entry) => entry.isDirectory() && /^Hopper.*\.app$/iu.test(entry.name),
);
return app === undefined ? undefined : join(mountPath, app.name);
},
async validBundle(appPath) {
if (
!(await commandSucceeds("codesign", [
"--verify",
"--deep",
"--strict",
appPath,
]))
)
return false;
try {
const identifier = (
await execFileAsync("/usr/libexec/PlistBuddy", [
"-c",
"Print :CFBundleIdentifier",
join(appPath, "Contents/Info.plist"),
])
).stdout.trim();
return identifier === BUNDLE_IDENTIFIER;
} catch {
return false;
}
},
async destinationExists(path) {
try {
await access(path);
return true;
} catch {
return false;
}
},
async installBundle(source, destination, stage) {
try {
await mkdir(join(homedir(), "Applications"), { recursive: true });
await rm(stage, { recursive: true, force: true });
await execFileAsync("ditto", [source, stage]);
await rename(stage, destination);
return true;
} catch {
await rm(stage, { recursive: true, force: true });
return false;
}
},
async launcherReady(path) {
try {
await access(path);
return true;
} catch {
return false;
}
},
async openApplication(path) {
await execFileAsync("open", [path]);
},
cleanup: (path) => rm(path, { recursive: true, force: true }),
destination: () => join(homedir(), "Applications/Hopper Disassembler.app"),
});
const parseJson = (bytes: Uint8Array): unknown =>
JSON.parse(new TextDecoder().decode(bytes));
const integrityMatches = (
bytes: Uint8Array,
release: z.infer<typeof releaseSchema>,
): boolean =>
bytes.byteLength === Number(release.file_length) &&
bytes.byteLength <= MAX_PACKAGE_BYTES &&
createHash("sha1").update(bytes).digest("hex") === release.file_hash;
const commandSucceeds = async (
command: string,
args: readonly string[],
): Promise<boolean> => {
try {
await execFileAsync(command, args);
return true;
} catch {
return false;
}
};
const releaseSchema = z.object({
filename: z.string().url(),
file_length: z.string().regex(/^\d+$/u),
file_hash: z.string().regex(/^[a-f0-9]{40}$/u),
});
const releasesSchema = z.object({ "OS X": releaseSchema });
+120 -76
View File
@@ -9,22 +9,19 @@ import {
} from "node:fs/promises";
import { homedir } from "node:os";
import { dirname, join, resolve } from "node:path";
import { execFile } from "node:child_process";
import { promisify } from "node:util";
import { z } from "zod";
import writeFileAtomic from "write-file-atomic";
import { parse as parseToml, stringify as stringifyToml } from "smol-toml";
import { PRODUCT_IDENTITY } from "../identity.js";
import { supportsNodeVersion } from "../domain/runtimeVersion.js";
import { runDoctor, systemDoctorHost } from "./Doctor.js";
import { probeHomebrew } from "./homebrew.js";
import {
installLinuxHopper,
readLinuxDistribution,
type LinuxDistribution,
} from "./LinuxHopper.js";
const execFileAsync = promisify(execFile);
import { installMacHopper } from "./MacHopper.js";
const registrationCommand = (): readonly string[] =>
process.env.npm_command === "exec"
@@ -58,14 +55,12 @@ export interface SetupHost {
readonly nodeVersion: string;
macosVersion(): Promise<string | undefined>;
linuxDistribution(): Promise<LinuxDistribution | undefined>;
hasHomebrew(): Promise<boolean>;
installHomebrew(): Promise<boolean>;
hopperPath(): Promise<string | undefined>;
installHopper(): Promise<boolean>;
installHopper(): Promise<string | undefined>;
detectedClients(): Promise<readonly SetupClient[]>;
configureClient(
client: SetupClient,
hopperPath: string,
hopperPath: string | undefined,
command: readonly string[],
): Promise<ClientConfigurationResult>;
installSkill(): Promise<"installed" | "unchanged" | "failed">;
@@ -77,56 +72,126 @@ export interface SetupHost {
* prompt, avoiding deadlocks in stdio and other unattended environments.
*/
export interface SetupResult {
readonly status: "ready" | "needs_human";
readonly actions: readonly string[];
readonly status: "planned" | "needs_confirmation" | "ready" | "needs_human";
readonly plannedActions: readonly SetupAction[];
readonly appliedActions: readonly string[];
readonly clients: Readonly<Record<string, ClientConfigurationResult>>;
readonly doctor: Awaited<ReturnType<typeof runDoctor>>;
readonly remediation?: string;
}
/** One concrete setup mutation disclosed before approval. */
export interface SetupAction {
readonly kind: "install_hopper" | "configure_client" | "install_skill";
readonly target: string;
readonly detail: string;
readonly external: boolean;
}
/** Explicit authorization supplied by an interactive or unattended CLI adapter. */
export interface SetupOptions {
readonly approved: boolean;
readonly installHopper: boolean;
readonly structured: boolean;
}
/** Adapter-owned interactive confirmation for a fully discovered setup plan. */
export type SetupConfirmation = (
actions: readonly SetupAction[],
) => Promise<boolean>;
/**
* Install prerequisites and configure detected clients idempotently.
* With `yes` false, mutations requiring consent are reported as `needs_human`;
* with `yes` true, external installers still fail closed if they require UI.
* Discovery always precedes mutation. Interactive confirmation or explicit
* unattended options authorize only the actions represented in the result.
*/
export const runSetup = async (
yes: boolean,
options: SetupOptions,
host: SetupHost = systemSetupHost(),
confirm?: SetupConfirmation,
): Promise<SetupResult> => {
const actions: string[] = [];
const appliedActions: string[] = [];
let plannedActions: readonly SetupAction[] = [];
const clients: Record<string, ClientConfigurationResult> = {};
const fail = async (remediation: string): Promise<SetupResult> => ({
status: "needs_human",
actions,
plannedActions,
appliedActions,
clients,
doctor: await host.doctor(),
remediation,
});
const unsupported = await hostRemediation(host);
if (unsupported !== undefined) return fail(unsupported);
if (host.platform === "darwin" && !(await host.hasHomebrew())) {
if (!yes) return fail("Re-run with --yes to install Homebrew.");
if (!(await host.installHomebrew()))
return fail(
"Homebrew installation was interrupted; resolve the installer prompt and re-run setup.",
);
actions.push("installed_homebrew");
}
let hopperPath = await host.hopperPath();
if (hopperPath === undefined) {
if (!yes) return fail("Re-run with --yes to install Hopper.");
if (!(await host.installHopper()))
return fail(
"Hopper installation was interrupted; resolve the system prompt and re-run setup.",
);
actions.push("installed_hopper");
hopperPath = await host.hopperPath();
if (hopperPath === undefined)
return fail(
"Hopper installation completed but its launcher was not found.",
);
const detectedClients = await host.detectedClients();
plannedActions = [
...(hopperPath === undefined
? [
{
kind: "install_hopper" as const,
target:
host.platform === "darwin"
? "~/Applications/Hopper Disassembler.app"
: "system package manager",
detail:
"Download the official Hopper package, verify it, install it, and open Hopper for activation.",
external: true,
},
]
: []),
...detectedClients
.filter(({ format }) => format !== "unsupported")
.map(
(client): SetupAction => ({
kind: "configure_client",
target: client.configPath,
detail: `Add the REA MCP registration for ${client.name}; preserve unrelated configuration.`,
external: false,
}),
),
{
kind: "install_skill",
target: "~/.agents/skills/rea-analysis/SKILL.md",
detail: "Install or update the bundled REA analysis skill.",
external: false,
},
];
let approved = options.approved;
let interactiveApproval = false;
if (!approved && confirm !== undefined && !options.structured) {
interactiveApproval = await confirm(plannedActions);
approved = interactiveApproval;
}
for (const client of await host.detectedClients()) {
if (!approved)
return {
status: confirm === undefined ? "needs_confirmation" : "planned",
plannedActions,
appliedActions,
clients,
doctor: await host.doctor(),
remediation:
"Review the setup plan, then rerun interactively or with --yes.",
};
if (
hopperPath === undefined &&
(interactiveApproval || options.installHopper)
) {
hopperPath = await host.installHopper();
if (hopperPath === undefined)
return {
status: "needs_human",
plannedActions,
appliedActions,
clients,
doctor: await host.doctor(),
remediation:
"Hopper installation failed; install Hopper manually or rerun setup after resolving the reported system error.",
};
appliedActions.push("installed_hopper");
}
for (const client of detectedClients) {
const result = await host.configureClient(
client,
hopperPath,
@@ -138,18 +203,19 @@ export const runSetup = async (
`${client.name} configuration ${result.reason} verification failed; no successful configuration was reported.`,
);
if (result.status === "configured")
actions.push(`configured_${client.name}`);
appliedActions.push(`configured_${client.name}`);
}
const skill = await host.installSkill();
if (skill === "failed")
return fail("Agent skill installation or readback failed.");
if (skill === "installed") actions.push("installed_skill");
if (skill === "installed") appliedActions.push("installed_skill");
const doctor = await host.doctor();
const activationRequired = actions.includes("installed_hopper");
const activationRequired = appliedActions.includes("installed_hopper");
const ready = doctor.healthy && !activationRequired;
return {
status: ready ? "ready" : "needs_human",
actions,
plannedActions,
appliedActions,
clients,
doctor,
...(ready
@@ -157,7 +223,9 @@ export const runSetup = async (
: {
remediation: activationRequired
? "Open Hopper, complete its one-time activation, then rerun rea doctor --json."
: "Run rea doctor and apply each reported remediation.",
: hopperPath === undefined
? "Hopper is optional for non-Hopper providers. Rerun with --yes --install-hopper for deep native analysis."
: "Run rea doctor and apply each reported remediation.",
}),
};
};
@@ -167,7 +235,8 @@ const hostRemediation = async (
): Promise<string | undefined> => {
if (host.platform !== "darwin" && host.platform !== "linux")
return "REA supports Hopper on macOS and selected 64-bit Linux distributions.";
if (major(host.nodeVersion) < 24) return "Install Node.js 24.18 or newer.";
if (!supportsNodeVersion(host.nodeVersion))
return "Install Node.js 22.19+ or 24.11+ and rerun setup.";
if (host.platform === "darwin") {
const version = await host.macosVersion();
return version === undefined || major(version) < 12
@@ -179,7 +248,7 @@ const hostRemediation = async (
: "REA supports Hopper on Ubuntu 24.04+, Fedora 41+, and 64-bit Arch Linux.";
};
/** Production setup effects for macOS, Homebrew, JSON MCP clients, and the canonical skill directory. */
/** Production setup effects for Hopper, agent configuration, and the canonical skill directory. */
const systemSetupHost = (): SetupHost => {
const doctorHost = systemDoctorHost();
return {
@@ -187,21 +256,14 @@ const systemSetupHost = (): SetupHost => {
nodeVersion: process.versions.node,
macosVersion: () => doctorHost.macosVersion(),
linuxDistribution: readLinuxDistribution,
hasHomebrew: () => brewSucceeds(["--version"]),
installHomebrew: () =>
commandSucceeds(
"/bin/bash",
[
"-c",
"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)",
],
{ ...process.env, NONINTERACTIVE: "1" },
),
hopperPath: async () => (await runDoctor(undefined, doctorHost)).hopperPath,
installHopper: () =>
process.platform === "linux"
? installLinuxHopper().then(({ status }) => status === "installed")
: brewSucceeds(["install", "--cask", "hopper-disassembler"]),
installHopper: async () => {
const result =
process.platform === "linux"
? await installLinuxHopper()
: await installMacHopper();
return result.status === "installed" ? result.launcherPath : undefined;
},
detectedClients: () => detectClients(homedir()),
configureClient: (client, hopperPath, command) =>
client.format === "unsupported"
@@ -473,24 +535,6 @@ export const installCanonicalSkill = async (
}
};
const commandSucceeds = async (
command: string,
args: readonly string[],
env?: NodeJS.ProcessEnv,
): Promise<boolean> => {
try {
await execFileAsync(command, args, env === undefined ? {} : { env });
return true;
} catch {
return false;
}
};
const brewSucceeds = async (args: readonly string[]): Promise<boolean> => {
const result = await probeHomebrew(async (command) =>
(await commandSucceeds(command, args)) ? true : undefined,
);
return result === true;
};
const major = (version: string): number =>
Number.parseInt(version.split(".")[0] ?? "0", 10);
const exists = async (path: string): Promise<boolean> => {