Files
rea/src/cli/interactiveSetup.ts
T
morluto 2bf347dee6 fix(cli): reject invalid inline values and honor requested output on terminals (#14763)
* fix(cli): reject invalid inline values and honor requested output on terminals

- Reject inline boolean and empty number values the schema cannot accept
  instead of coercing --skill=yes to false or --limit= to 0.
- Give captured mcp add output the terminal presentation of every other
  command through one shared result destination.
- Show the unapproved setup plan whose remediation asks a terminal reader to
  review it, and open the setup rail once when a scoped run starts at approval.
- Treat --full-output and --token-count as requesting the result document, so
  setup and argument errors no longer add terminal presentation beside it.
- Answer --schema for an unknown command in the selected format, and set the
  exit code instead of exiting so piped stdout drains.

* fix(cli): keep rejected boolean and number literals across repeated flags

Binding stored one scalar value, so a later valid flag replaced an earlier
literal before the schema could reject it. Boolean and number literals the
field rejects now stay bound, including decimal unions and count options,
while repeated valid flags still use the last value. --dry-run keeps its
own error.

* test(cli): assert the terminal full-output failure envelope

The terminal --full-output regression only checked that usage text was
absent. Decode the default document and the --json envelope and require
VALIDATION_ERROR with the skill field's invalid_type error.

* test(cli): accept a counted setup approval prompt

A scoped terminal plan names every eligible action, so a Linux host that
can install Hopper asks "Apply these N changes?" rather than the singular
question. The consent helper now answers either prompt, and the rail test
finds that question before checking the intro was rendered once.

---------

Co-authored-by: Cursor Agent <cursoragent@cursor.com>
2026-11-26 12:15:17 -05:00

379 lines
13 KiB
TypeScript

import { HOST_PLATFORM } from "../platform/HostPlatform.js";
import {
cancel,
confirm,
intro,
isCancel,
multiselect,
outro,
} from "@clack/prompts";
import type {
SetupAction,
SetupConfirmationDecision,
SetupConfirmationContext,
SetupClientState,
SetupProgressEvent,
SetupResult,
} from "../application/SetupTypes.js";
import { SUPPORTED_CLIENT_DEFINITIONS } from "../application/SupportedClients.js";
import { invokingPackageRunner } from "../application/ClientRegistrationIdentity.js";
const clientDisplayNames: ReadonlyMap<string, string> = new Map(
SUPPORTED_CLIENT_DEFINITIONS.map(({ name, displayName }) => [
name,
displayName,
]),
);
const promptStreams = {
input: process.stdin,
output: process.stderr,
withGuide: true,
} as const;
/** One setup invocation's terminal rail: it opens exactly once. */
export interface SetupJourney {
introduced: boolean;
}
export const createSetupJourney = (): SetupJourney => ({ introduced: false });
/** Select agent targets, then review the resolved plan before consent. */
export const confirmInteractiveSetup = async (
actions: readonly SetupAction[],
accessible: boolean,
context?: SetupConfirmationContext,
signal?: AbortSignal,
journey: SetupJourney = createSetupJourney(),
): Promise<SetupConfirmationDecision> => {
// A scoped run skips selection and starts at confirmation; either stage
// opens the rail when it is the first to render.
if (!journey.introduced) {
journey.introduced = true;
intro("REA setup", promptStreams);
writeLine(
"│ Understand local apps and binaries from your terminal or agent.",
);
renderDetectedSummary(context?.clientStates ?? []);
renderKeyboardHelp(accessible);
}
if (context?.stage === "select") {
const selected = await selectSetupActions(
actions,
accessible,
context,
signal,
);
return selected === undefined
? cancelledDecision()
: { approved: false, selectedActionIds: selected };
}
if (actions.length === 0) return { approved: true, selectedActionIds: [] };
renderPreflight(actions);
if (context?.remediation !== undefined) {
writeLine("│");
writeLine(`│ ${context.remediation}`);
}
const approved = await confirm({
...promptStreams,
...(signal === undefined ? {} : { signal }),
message:
actions.length === 1
? "Apply this change?"
: `Apply these ${String(actions.length)} changes?`,
initialValue: true,
active: "Yes, apply",
inactive: "No, cancel",
vertical: accessible,
});
if (isCancel(approved) || !approved) return cancelledDecision();
return { approved: true, selectedActionIds: actions.map(({ id }) => id) };
};
/** Render stable, append-only progress for real setup operations. */
export const renderSetupProgress = (event: SetupProgressEvent): void => {
const symbol =
event.state === "started"
? "◇"
: event.state === "completed"
? "◆"
: event.state === "warning"
? "!"
: "✗";
const suffix = event.detail === undefined ? "" : ` · ${event.detail}`;
writeLine(`${symbol} ${event.label}${suffix}`);
};
/**
* Show a resolved plan that was not applied to a human at a terminal: a dry
* run, or a plan that still needs approval the terminal could not prompt for.
*/
export const renderSetupPlan = (
actions: readonly SetupAction[],
remediation: string | undefined,
closing: string,
): void => {
intro("REA setup plan", promptStreams);
renderPreflight(actions);
// A known manual step belongs to the plan, as it does at approval.
if (remediation !== undefined) {
writeLine("│");
writeLine(`│ ${remediation}`);
}
outro(closing, promptStreams);
};
/** Finish an interactive setup journey without exposing the full doctor catalog. */
export const renderInteractiveSetupResult = (
result: SetupResult,
environment: Readonly<NodeJS.ProcessEnv> = process.env,
journey?: SetupJourney,
): void => {
if (result.status === "planned" || result.status === "cancelled") return;
if (journey !== undefined && !journey.introduced) {
journey.introduced = true;
intro("REA setup", promptStreams);
}
if (result.status === "ready") {
const changedClients = Object.entries(result.clients)
.filter(([, client]) => client.status === "configured")
.map(([client]) => clientDisplayNames.get(client) ?? client);
const readyClients = readyAgentClients(result, changedClients);
renderReadyCapabilities(result, readyClients, environment);
outro(
changedClients.length === 0
? "REA is ready for local app analysis."
: `Restart ${changedClients.join(", ")} to load REA.`,
promptStreams,
);
return;
}
writeLine(`! ${result.remediation ?? "Setup needs attention."}`);
for (const resource of result.cleanup?.resources ?? [])
writeLine(` Pending cleanup: ${resource}`);
outro(
`Run \`${cliInvocation(environment)} doctor\` for the remaining checks.`,
promptStreams,
);
};
const selectSetupActions = async (
actions: readonly SetupAction[],
accessible: boolean,
context: SetupConfirmationContext,
signal?: AbortSignal,
): Promise<readonly string[] | undefined> => {
const clientStates = context.clientStates.filter(
({ client }) => client.format !== "unsupported",
);
const initial = new Set(context.selectedClientIds);
const selectedClientIds = accessible
? await selectClientsAccessibly(clientStates, initial, signal)
: await selectClients(clientStates, initial, signal);
if (selectedClientIds === undefined) return undefined;
const selectedActionIds = selectedClientIds.map(
(id) => `configure_client:${id}`,
);
const skill = actions.find(({ kind }) => kind === "install_skill");
if (skill !== undefined) {
if (selectedClientIds.length > 0) selectedActionIds.push(skill.id);
else {
const included = await confirm({
...promptStreams,
...(signal === undefined ? {} : { signal }),
message: "Install the guided REA skill for CLI use?",
initialValue: false,
vertical: accessible,
});
if (isCancel(included)) return undefined;
if (included) selectedActionIds.push(skill.id);
}
}
const hopper = actions.find(({ kind }) => kind === "install_hopper");
if (hopper !== undefined) {
const included = await confirm({
...promptStreams,
...(signal === undefined ? {} : { signal }),
message: "Install Hopper for deep binary analysis?",
initialValue: false,
vertical: accessible,
});
if (isCancel(included)) return undefined;
if (included) selectedActionIds.push(hopper.id);
}
return selectedActionIds;
};
const clientHint = (state: SetupClientState): string =>
state.configured
? "REA already configured · selected from existing configuration"
: state.client.configPathError !== undefined
? state.client.configPathError
: state.status === "invalid"
? "Existing configuration needs repair before setup"
: state.detected
? "Detected · choose to configure"
: "Not detected · configuration is still available";
const selectClients = async (
states: readonly SetupClientState[],
initial: ReadonlySet<string>,
signal?: AbortSignal,
): Promise<readonly string[] | undefined> => {
if (states.length === 0) return [];
const selection = await multiselect({
...promptStreams,
...(signal === undefined ? {} : { signal }),
message: "Which agents should use REA?",
options: states.map((state) => ({
value: state.client.name,
label: clientDisplayNames.get(state.client.name) ?? state.client.name,
hint: clientHint(state),
})),
initialValues: states
.filter(({ client }) => initial.has(client.name))
.map(({ client }) => client.name),
required: false,
maxItems: Math.max(
3,
Math.min(states.length, (process.stderr.rows ?? 24) - 8),
),
showInstructions: true,
});
return isCancel(selection) ? undefined : selection;
};
const selectClientsAccessibly = async (
states: readonly SetupClientState[],
initial: ReadonlySet<string>,
signal?: AbortSignal,
): Promise<readonly string[] | undefined> => {
const selected: string[] = [];
for (const state of states) {
const included = await confirm({
...promptStreams,
...(signal === undefined ? {} : { signal }),
message: `Configure ${clientDisplayNames.get(state.client.name) ?? state.client.name}? ${clientHint(state)}`,
initialValue: initial.has(state.client.name),
vertical: true,
});
if (isCancel(included)) return undefined;
if (included) selected.push(state.client.name);
}
return selected;
};
const renderDetectedSummary = (states: readonly SetupClientState[]): void => {
const detected = states.filter(({ detected }) => detected);
writeLine("│");
writeLine(
detected.length === 0
? "◆ No agents detected. Choose any supported agent below."
: `◆ Detected: ${humanList(detected.map(({ client }) => clientDisplayNames.get(client.name) ?? client.name))}`,
);
};
const renderKeyboardHelp = (accessible: boolean): void => {
writeLine("│");
writeLine(
accessible
? "│ Keys: Enter answer · Ctrl-C cancel"
: "│ Keys: ↑/↓ navigate · Space toggle · Enter confirm · Ctrl-C cancel",
);
};
const cliInvocation = (environment: Readonly<NodeJS.ProcessEnv>): string => {
const runner = invokingPackageRunner(environment, HOST_PLATFORM.platform);
return runner === undefined ? "rea" : `${runner} rea-agents`;
};
const renderReadyCapabilities = (
result: SetupResult,
readyClients: readonly string[],
environment: Readonly<NodeJS.ProcessEnv>,
): void => {
writeLine("◆ What you can do now");
const providers = readyProviders(result);
if (providers.length > 0)
writeLine(`│ Deep analysis: ${humanList(providers)}`);
if (readyClients.length > 0)
writeLine(`│ Agent access: ${humanList(readyClients)}`);
if (result.doctor.identity?.skill.state === "aligned")
writeLine("│ Guided reverse-engineering workflows: installed");
writeLine(
`│ CLI: ${cliInvocation(environment)} ${providers.length > 0 ? "analyze /path/to/app" : "capabilities"}`,
);
const firstClient = readyClients[0];
if (firstClient !== undefined)
writeLine(
`│ Try in ${firstClient}: "Explain how a feature works in /path/to/app and show the evidence."`,
);
};
const readyAgentClients = (
result: SetupResult,
changedClients: readonly string[],
): readonly string[] => {
const alignedClients = (result.doctor.identity?.registrations ?? [])
.filter(({ state }) => state === "aligned")
.map(({ client }) => clientDisplayNames.get(client) ?? client);
return [...new Set([...alignedClients, ...changedClients])];
};
const readyProviders = (result: SetupResult): readonly string[] =>
result.doctor.availableProviders.map(displayProviderId);
/** Product spellings that title case would distort. Lookup ids stay lowercase. */
const PROVIDER_DISPLAY_NAMES: Readonly<Record<string, string>> = {
ida: "IDA",
jadx: "JADX",
};
const displayProviderId = (id: string): string =>
PROVIDER_DISPLAY_NAMES[id] ??
(id.length === 0 ? id : `${id[0]?.toUpperCase() ?? ""}${id.slice(1)}`);
const humanList = (values: readonly string[]): string => {
if (values.length < 2) return values[0] ?? "";
if (values.length === 2) return `${values[0]} and ${values[1]}`;
return `${values.slice(0, -1).join(", ")}, and ${values.at(-1)}`;
};
const renderPreflight = (actions: readonly SetupAction[]): void => {
writeLine("│");
writeLine("◆ Ready to review");
for (const action of actions) {
writeLine("│");
writeLine(`│ ${action.operation.toUpperCase()} ${action.label}`);
writeLine(`│ ${action.target}`);
if (action.backupPath !== undefined)
writeLine(`│ backup: ${action.backupPath}`);
for (const origin of action.networkOrigins ?? [])
writeLine(`│ network: ${origin}`);
if (action.integrity !== undefined)
writeLine(`│ integrity: ${action.integrity}`);
for (const command of action.commands ?? [])
writeLine(`│ command: ${command}`);
writeLine(`│ ${action.detail}`);
if (action.external) writeLine("│ external software");
}
writeLine("│");
writeLine(
"│ REA will preserve unrelated configuration and will not install or upgrade Node.js, npm, Homebrew, Java, or Ghidra.",
);
};
const cancelledDecision = (): SetupConfirmationDecision => {
cancel("Setup cancelled. No changes were made.", promptStreams);
return {
approved: false,
selectedActionIds: [],
cancelled: true,
};
};
const writeLine = (line: string): void => {
process.stderr.write(`${line}\n`);
};