feat(cli): improve setup onboarding

This commit is contained in:
morluto
2026-07-18 11:59:16 +08:00
parent 7bccb7c6ae
commit 16002ff3c2
70 changed files with 2043 additions and 228 deletions
+18 -8
View File
@@ -195,15 +195,16 @@ export const runDoctor = async (
SYSTEM_LINUX_HOPPER,
linuxHopperLauncherPath(homedir()),
...(await host.manualHopperPaths()),
await host.brewHopperPath(),
].filter((value): value is string => value !== undefined)
]
: [host.configuredHopperPath];
let hopperPath: string | undefined;
for (const candidate of new Set(candidates))
if (await host.executable(candidate)) {
hopperPath = candidate;
break;
}
let hopperPath = await firstExecutable(host, candidates);
if (hopperPath === undefined && host.configuredHopperPath === undefined) {
const brewCandidate = await host.brewHopperPath();
hopperPath = await firstExecutable(
host,
brewCandidate === undefined ? [] : [brewCandidate],
);
}
checks.push(
check(
"hopper",
@@ -562,6 +563,15 @@ const uniqueLines = (value: string): string[] => [
),
];
const firstExecutable = async (
host: DoctorHost,
candidates: readonly string[],
): Promise<string | undefined> => {
for (const candidate of new Set(candidates))
if (await host.executable(candidate)) return candidate;
return undefined;
};
const firstIlspyVersionLine = (value: string): string | undefined =>
uniqueLines(value).find((line) => line.startsWith("ilspycmd:"));
+26
View File
@@ -38,6 +38,32 @@ const SUPPORTED_RELEASES: Readonly<
export type LinuxPackageFamily = "deb" | "rpm" | "arch";
/** Exact stable external effects disclosed before a Linux Hopper installation. */
export interface LinuxHopperInstallDisclosure {
readonly downloadUrl: string;
readonly expectedBytes: number;
readonly expectedSha1: string;
readonly commands: readonly string[];
}
/** Describe the release and package-manager commands without downloading or writing. */
export const linuxHopperInstallDisclosure = (
family: LinuxPackageFamily,
isRoot: boolean,
): LinuxHopperInstallDisclosure => {
const release = SUPPORTED_RELEASES[family];
return {
downloadUrl: release.filename,
expectedBytes: Number(release.file_length),
expectedSha1: release.file_hash,
commands: linuxPackageManagerCommands(
family,
`<verified-hopper.${family}>`,
isRoot,
).map(({ executable, args }) => `${executable} ${args.join(" ")}`),
};
};
/** Parsed Linux distribution information used for support and package selection. */
export interface LinuxDistribution {
readonly id: string;
+11
View File
@@ -22,6 +22,17 @@ 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";
/** Stable network and command effects disclosed before macOS Hopper setup. */
export const macHopperInstallDisclosure = {
networkOrigins: [RELEASES_URL, DOWNLOAD_PREFIX] as const,
commands: [
"hdiutil attach -readonly -nobrowse -mountpoint <private-mount> <verified-hopper.dmg>",
"codesign --verify --deep --strict <mounted-hopper.app>",
"ditto <mounted-hopper.app> <user-Applications-stage>",
"open <installed-hopper.app>",
] as const,
};
/** Result of installing the official Hopper macOS application. */
export type MacHopperInstallResult =
| { readonly status: "installed"; readonly launcherPath: string }
+270 -24
View File
@@ -66,6 +66,18 @@ export type ClientConfigurationResult =
readonly status: "failed";
readonly reason: "path" | "backup" | "write" | "readback";
};
/** Read-only state used to block unsafe setup plans before any mutation. */
export type ClientConfigurationInspection =
| { readonly status: "already_current" }
| {
readonly status: "create" | "update";
readonly backupPath?: string;
}
| {
readonly status: "invalid";
readonly remediation: string;
};
/**
* Effects required by the idempotent setup workflow.
* Implementations report interrupted installers as values: setup must not wait
@@ -90,6 +102,11 @@ export interface SetupHost {
providerEnvironment: SetupProviderEnvironment,
command: readonly string[],
): Promise<boolean>;
inspectClientConfiguration?(
client: SetupClient,
providerEnvironment: SetupProviderEnvironment,
command: readonly string[],
): Promise<ClientConfigurationInspection>;
skillNeedsInstall(): Promise<boolean>;
installSkill(): Promise<"installed" | "unchanged" | "failed">;
doctor(): Promise<Awaited<ReturnType<typeof runDoctor>>>;
@@ -111,10 +128,17 @@ export interface SetupResult {
/** One concrete setup mutation disclosed before approval. */
export interface SetupAction {
readonly id: string;
readonly kind: "install_hopper" | "configure_client" | "install_skill";
readonly label: string;
readonly target: string;
readonly detail: string;
readonly external: boolean;
readonly operation: "create" | "update" | "install";
readonly backupPath?: string;
readonly networkOrigins?: readonly string[];
readonly commands?: readonly string[];
readonly integrity?: string;
}
/** Explicit authorization supplied by an interactive or unattended CLI adapter. */
@@ -122,12 +146,30 @@ export interface SetupOptions {
readonly approved: boolean;
readonly installHopper: boolean;
readonly structured: boolean;
readonly proposeHopper?: boolean;
readonly clientIds?: readonly string[];
readonly installSkill?: boolean;
readonly onProgress?: (event: SetupProgressEvent) => void;
}
/** One settled or active setup operation projected to the interactive adapter. */
export interface SetupProgressEvent {
readonly actionId: string;
readonly label: string;
readonly state: "started" | "completed" | "warning" | "failed";
readonly detail?: string;
}
/** Interactive selection and final consent returned by the CLI adapter. */
export interface SetupConfirmationDecision {
readonly approved: boolean;
readonly selectedActionIds: readonly string[];
}
/** Adapter-owned interactive confirmation for a fully discovered setup plan. */
export type SetupConfirmation = (
actions: readonly SetupAction[],
) => Promise<boolean>;
) => Promise<boolean | SetupConfirmationDecision>;
/**
* Install prerequisites and configure detected clients idempotently.
@@ -154,19 +196,40 @@ export const runSetup = async (
if (unsupported !== undefined) return fail(unsupported);
let hopperPath = await host.hopperPath();
let providerEnvironment = await initialProviderEnvironment(host, hopperPath);
const discovery = await discoverSetupActions(
const discovery = await discoverSetupActions({
host,
providerEnvironment,
options.installHopper,
);
const { installHopper, installSkill } = discovery;
let { detectedClients } = discovery;
forceHopperInstall: options.installHopper,
proposeHopper: options.proposeHopper ?? true,
selectedClientIds: options.clientIds,
installSkillSelection: options.installSkill,
});
if (discovery.blocker !== undefined) return fail(discovery.blocker);
let { installHopper, installSkill } = discovery;
let detectedClients: readonly SetupClient[] = discovery.detectedClients;
plannedActions = discovery.plannedActions;
let approved = options.approved || plannedActions.length === 0;
let interactiveApproval = false;
if (!approved && confirm !== undefined && !options.structured) {
interactiveApproval = await confirm(plannedActions);
approved = interactiveApproval;
const decision = await confirm(plannedActions);
if (typeof decision === "boolean") {
interactiveApproval = decision;
approved = decision;
} else {
const selected = new Set(decision.selectedActionIds);
plannedActions = plannedActions.filter(({ id }) => selected.has(id));
installHopper = plannedActions.some(
({ kind }) => kind === "install_hopper",
);
installSkill = plannedActions.some(
({ kind }) => kind === "install_skill",
);
detectedClients = detectedClients.filter((client) =>
selected.has(`configure_client:${client.name}`),
);
interactiveApproval = decision.approved;
approved = decision.approved || plannedActions.length === 0;
}
}
if (!approved)
return {
@@ -180,8 +243,21 @@ export const runSetup = async (
};
if (installHopper && (interactiveApproval || options.installHopper)) {
emitProgress(
options,
"install_hopper",
"Hopper deep-analysis provider",
"started",
);
const installed = await host.installHopper(options.installHopper);
if (installed.status === "failed")
if (installed.status === "failed") {
emitProgress(
options,
"install_hopper",
"Hopper deep-analysis provider",
"failed",
installed.remediation,
);
return {
status: "needs_human",
plannedActions,
@@ -191,12 +267,20 @@ export const runSetup = async (
code: installed.code,
remediation: installed.remediation,
};
}
hopperPath = installed.launcherPath;
providerEnvironment = {
...providerEnvironment,
HOPPER_LAUNCHER_PATH: installed.launcherPath,
};
appliedActions.push("installed_hopper");
emitProgress(
options,
"install_hopper",
"Hopper deep-analysis provider",
"completed",
installed.launcherPath,
);
detectedClients = await filterClientsNeedingConfigure(
host,
detectedClients,
@@ -211,15 +295,38 @@ export const runSetup = async (
command: registrationCommand(),
clients,
appliedActions,
...(options.onProgress === undefined
? {}
: { onProgress: options.onProgress }),
});
if (clientFailure !== undefined) return fail(clientFailure);
if (installSkill) {
emitProgress(
options,
"install_skill",
"REA reverse-engineering skill",
"started",
);
const skill = await host.installSkill();
if (skill === "failed")
if (skill === "failed") {
emitProgress(
options,
"install_skill",
"REA reverse-engineering skill",
"failed",
);
return fail(
"REA analysis skill could not be installed or verified. Check permissions for `~/.agents/skills`, then rerun setup.",
);
}
if (skill === "installed") appliedActions.push("installed_skill");
emitProgress(
options,
"install_skill",
"REA reverse-engineering skill",
skill === "installed" ? "completed" : "warning",
skill === "unchanged" ? "Already current" : undefined,
);
}
const doctor = await host.doctor();
const remediation = finalSetupRemediation(
@@ -238,11 +345,15 @@ export const runSetup = async (
};
};
const discoverSetupActions = async (
host: SetupHost,
providerEnvironment: SetupProviderEnvironment,
forceHopperInstall: boolean,
) => {
const discoverSetupActions = async (input: {
readonly host: SetupHost;
readonly providerEnvironment: SetupProviderEnvironment;
readonly forceHopperInstall: boolean;
readonly proposeHopper: boolean;
readonly selectedClientIds: readonly string[] | undefined;
readonly installSkillSelection: boolean | undefined;
}) => {
const { host, providerEnvironment } = input;
const initialDoctor = await host.doctor();
const linuxHopperRepairNeeded = initialDoctor.checks.some(
({ name, ok, detail }) =>
@@ -251,13 +362,24 @@ const discoverSetupActions = async (
(name === "hopper-version" && detail === "/opt/hopper/bin/Hopper")),
);
const installHopper =
forceHopperInstall ||
Object.keys(providerEnvironment).length === 0 ||
linuxHopperRepairNeeded;
const [detectedClients, installSkill] = await Promise.all([
input.forceHopperInstall ||
(input.proposeHopper &&
(Object.keys(providerEnvironment).length === 0 ||
linuxHopperRepairNeeded));
const [allDetectedClients, skillNeedsInstall] = await Promise.all([
host.detectedClients(),
host.skillNeedsInstall(),
]);
const selectedClientIdSet =
input.selectedClientIds === undefined
? undefined
: new Set(input.selectedClientIds);
const detectedClients = allDetectedClients.filter(
({ name }) =>
selectedClientIdSet === undefined || selectedClientIdSet.has(name),
);
const installSkill =
input.installSkillSelection === false ? false : skillNeedsInstall;
const command = registrationCommand();
const clients = installHopper
? detectedClients
@@ -267,21 +389,91 @@ const discoverSetupActions = async (
providerEnvironment,
command,
);
const plannedActions = setupPlan(
host.platform,
installHopper,
installSkill,
const inspections = await inspectClients(
host,
clients,
providerEnvironment,
command,
);
const invalid = inspections.find(
({ inspection }) => inspection.status === "invalid",
);
const blocker =
invalid?.inspection.status === "invalid"
? `${invalid.client.displayName ?? invalid.client.name}: ${invalid.inspection.remediation}`
: undefined;
const clientPlans = inspections.flatMap(({ client, inspection }) =>
inspection.status === "create" ||
inspection.status === "update" ||
(installHopper && inspection.status === "already_current")
? [
{
client,
operation:
inspection.status === "already_current"
? ("update" as const)
: inspection.status,
...(inspection.status !== "already_current" &&
inspection.backupPath !== undefined
? { backupPath: inspection.backupPath }
: {}),
},
]
: [],
);
const linuxDistribution =
host.platform === "linux" ? await host.linuxDistribution() : undefined;
const plannedActions = setupPlan({
platform: host.platform,
installHopper,
installSkill,
clients: clientPlans,
providerEnvironment,
...(linuxDistribution?.packageFamily === undefined
? {}
: { linuxPackageFamily: linuxDistribution.packageFamily }),
});
return {
installHopper,
installSkill,
detectedClients: clients,
detectedClients: clientPlans.map(({ client }) => client),
plannedActions,
blocker,
};
};
const inspectClients = async (
host: SetupHost,
clients: readonly SetupClient[],
providerEnvironment: SetupProviderEnvironment,
command: readonly string[],
) =>
Promise.all(
clients.map(async (client) => ({
client,
inspection:
(await host.inspectClientConfiguration?.(
client,
providerEnvironment,
command,
)) ?? ({ status: "update" } as const),
})),
);
const emitProgress = (
options: SetupOptions,
actionId: string,
label: string,
state: SetupProgressEvent["state"],
detail?: string,
): void =>
options.onProgress?.({
actionId,
label,
state,
...(detail === undefined ? {} : { detail }),
});
const filterClientsNeedingConfigure = async (
host: SetupHost,
detectedClients: readonly SetupClient[],
@@ -381,6 +573,7 @@ export const systemSetupHost = (
clientConfigurationAligned(client, providerEnvironment, command).then(
(aligned) => !aligned,
),
inspectClientConfiguration: inspectClientConfiguration,
skillNeedsInstall: () => canonicalSkillNeedsInstall(homedir()),
installSkill: () => installCanonicalSkill(homedir()),
doctor: () => runDoctor(undefined, doctorHost),
@@ -636,6 +829,59 @@ const clientConfigurationAligned = async (
}
};
const inspectClientConfiguration = async (
client: SetupClient,
providerEnvironment: SetupProviderEnvironment,
command: readonly string[],
): Promise<ClientConfigurationInspection> => {
if (client.format === "unsupported") return { status: "already_current" };
const transactionPath = await resolveClientConfigTransactionPath(
client.configPath,
);
if (transactionPath === undefined)
return {
status: "invalid",
remediation:
"The configuration path is unsafe or unresolved. Check ownership and symbolic links before rerunning setup.",
};
let original: string;
try {
original = await readFile(transactionPath, "utf8");
} catch (cause: unknown) {
if (isMissing(cause)) return { status: "create" };
return {
status: "invalid",
remediation:
"The configuration file could not be read. Check its permissions before rerunning setup.",
};
}
const desired = clientConfigurationDesired(providerEnvironment, command);
try {
const document =
client.format === "toml"
? objectSchema.parse(parseToml(original))
: parseObject(original);
const servers = parseOptionalObject(
client.format === "toml" ? document.mcp_servers : document.mcpServers,
);
if (
JSON.stringify(servers[PRODUCT_IDENTITY.mcpServerKey]) ===
JSON.stringify(desired)
)
return { status: "already_current" };
} catch {
return {
status: "invalid",
remediation:
"The existing configuration is malformed. Repair it before setup applies any other changes.",
};
}
return {
status: "update",
backupPath: `${client.configPath}.rea.backup`,
};
};
const normalizeProviderEnvironment = (
environment: SetupProviderEnvironment | string,
): SetupProviderEnvironment =>
+25 -2
View File
@@ -3,6 +3,7 @@ import type {
SetupClient,
SetupHost,
SetupProviderEnvironment,
SetupProgressEvent,
} from "./Setup.js";
const failedConfigurationMessage = (
@@ -25,19 +26,41 @@ export const configureDetectedClients = async (options: {
readonly command: readonly string[];
readonly clients: Record<string, ClientConfigurationResult>;
readonly appliedActions: string[];
readonly onProgress?: (event: SetupProgressEvent) => void;
}): Promise<string | undefined> => {
let failure: string | undefined;
for (const client of options.detectedClients) {
const actionId = `configure_client:${client.name}`;
const label = client.displayName ?? client.name;
options.onProgress?.({ actionId, label, state: "started" });
const result = await options.host.configureClient(
client,
options.providerEnvironment,
options.command,
);
options.clients[client.name] = result;
if (result.status === "failed")
if (result.status === "failed") {
failure ??= failedConfigurationMessage(result.reason);
else if (result.status === "configured")
options.onProgress?.({
actionId,
label,
state: "failed",
detail: failedConfigurationMessage(result.reason),
});
} else if (result.status === "configured") {
options.appliedActions.push(`configured_${client.name}`);
options.onProgress?.({ actionId, label, state: "completed" });
} else {
options.onProgress?.({
actionId,
label,
state: "warning",
detail:
result.status === "unchanged"
? "Already current"
: "Manual configuration required",
});
}
}
return failure;
};
+70 -17
View File
@@ -4,54 +4,107 @@ import type {
SetupProviderEnvironment,
} from "./Setup.js";
import { PRODUCT_IDENTITY } from "../identity.js";
import { homedir } from "node:os";
import { join } from "node:path";
import {
linuxHopperInstallDisclosure,
type LinuxPackageFamily,
} from "./LinuxHopper.js";
import { macHopperInstallDisclosure } from "./MacHopper.js";
/** Preflighted client mutation admitted into the setup plan. */
export type SetupClientPlan = Readonly<{
client: SetupClient;
operation: "create" | "update";
backupPath?: string;
}>;
/** Build the complete setup mutation plan before approval. */
export const setupPlan = (
platform: NodeJS.Platform,
installHopper: boolean,
installSkill: boolean,
clients: readonly SetupClient[],
providerEnvironment: SetupProviderEnvironment,
): readonly SetupAction[] => [
...(installHopper
export const setupPlan = (input: {
readonly platform: NodeJS.Platform;
readonly installHopper: boolean;
readonly installSkill: boolean;
readonly clients: readonly SetupClientPlan[];
readonly providerEnvironment: SetupProviderEnvironment;
readonly linuxPackageFamily?: LinuxPackageFamily;
}): readonly SetupAction[] => [
...(input.installHopper
? [
{
id: "install_hopper",
kind: "install_hopper" as const,
label: "Hopper deep-analysis provider",
target:
platform === "darwin"
? "~/Applications/Hopper Disassembler.app"
input.platform === "darwin"
? join(homedir(), "Applications/Hopper Disassembler.app")
: "system package manager",
detail:
platform === "linux"
input.platform === "linux"
? "Download, verify, and install Hopper plus its Xvfb demo-session dependencies. For the supported demo build, REA uses a private display and selects Hopper's offered demo mode for each analysis session."
: "Download the official Hopper package, verify it, and install it. Hopper may show its demo or license prompt when first opened.",
external: true,
operation: "install" as const,
...(input.platform === "linux"
? input.linuxPackageFamily === undefined
? {}
: linuxDisclosure(input.linuxPackageFamily)
: {
networkOrigins: macHopperInstallDisclosure.networkOrigins,
commands: macHopperInstallDisclosure.commands,
}),
},
]
: []),
...clients
.filter(({ format }) => format !== "unsupported")
...input.clients
.filter(({ client }) => client.format !== "unsupported")
.map(
(client): SetupAction => ({
({ client, operation, backupPath }): SetupAction => ({
id: `configure_client:${client.name}`,
kind: "configure_client",
label: client.displayName ?? client.name,
target: client.configPath,
detail: clientConfigurationDetail(client.name, providerEnvironment),
detail: clientConfigurationDetail(
client.displayName ?? client.name,
input.providerEnvironment,
),
external: false,
operation,
...(backupPath === undefined ? {} : { backupPath }),
}),
),
...(installSkill
...(input.installSkill
? [
{
id: "install_skill",
kind: "install_skill" as const,
target: `~/.agents/skills/${PRODUCT_IDENTITY.skillName}/SKILL.md`,
label: "REA reverse-engineering skill",
target: join(
homedir(),
".agents/skills",
PRODUCT_IDENTITY.skillName,
"SKILL.md",
),
detail:
"Install or update the bundled REA reverse-engineering skill.",
external: false,
operation: "install" as const,
},
]
: []),
];
const linuxDisclosure = (family: LinuxPackageFamily) => {
const disclosure = linuxHopperInstallDisclosure(
family,
process.getuid?.() === 0,
);
return {
networkOrigins: [disclosure.downloadUrl],
commands: disclosure.commands,
integrity: `${String(disclosure.expectedBytes)} bytes · SHA-1 ${disclosure.expectedSha1}`,
};
};
const clientConfigurationDetail = (
client: string,
environment: SetupProviderEnvironment,
+2
View File
@@ -3,6 +3,7 @@ import { join } from "node:path";
/** One supported client configuration location. */
export interface SetupClient {
readonly name: string;
readonly displayName?: string;
readonly configPath: string;
readonly markerPath?: string;
readonly format?: "json" | "toml" | "unsupported";
@@ -70,6 +71,7 @@ export const SUPPORTED_CLIENT_DEFINITIONS = [
export const supportedClients = (home: string): readonly SetupClient[] =>
SUPPORTED_CLIENT_DEFINITIONS.map((definition) => ({
name: definition.name,
displayName: definition.displayName,
configPath: join(home, ...definition.configPath),
markerPath: join(home, ...definition.markerPath),
format: definition.format,