fix(cli): harden setup and destructive workflows
This commit is contained in:
@@ -25,7 +25,10 @@ import { installMacHopper } from "./MacHopper.js";
|
||||
import { configureDetectedClients } from "./SetupClients.js";
|
||||
import { supportedClients, type SetupClient } from "./SupportedClients.js";
|
||||
export type { SetupClient } from "./SupportedClients.js";
|
||||
import { installCanonicalSkill } from "./SetupSkill.js";
|
||||
import {
|
||||
canonicalSkillInstallationNeeded,
|
||||
installCanonicalSkill,
|
||||
} from "./SetupSkill.js";
|
||||
import { setupPlan } from "./SetupPlan.js";
|
||||
import {
|
||||
setupInstallFailure,
|
||||
@@ -69,6 +72,7 @@ export interface SetupHost {
|
||||
hopperPath: string | undefined,
|
||||
command: readonly string[],
|
||||
): Promise<ClientConfigurationResult>;
|
||||
skillInstallationNeeded(): Promise<boolean>;
|
||||
installSkill(): Promise<"installed" | "unchanged" | "failed">;
|
||||
doctor(): Promise<Awaited<ReturnType<typeof runDoctor>>>;
|
||||
}
|
||||
@@ -140,7 +144,13 @@ export const runSetup = async (
|
||||
);
|
||||
const installHopper = hopperPath === undefined || linuxHopperRepairNeeded;
|
||||
const detectedClients = await host.detectedClients();
|
||||
plannedActions = setupPlan(host.platform, installHopper, detectedClients);
|
||||
const installSkill = await host.skillInstallationNeeded();
|
||||
plannedActions = setupPlan(
|
||||
host.platform,
|
||||
installHopper,
|
||||
detectedClients,
|
||||
installSkill,
|
||||
);
|
||||
let approved = options.approved;
|
||||
let interactiveApproval = false;
|
||||
if (!approved && confirm !== undefined && !options.structured) {
|
||||
@@ -182,12 +192,14 @@ export const runSetup = async (
|
||||
appliedActions,
|
||||
});
|
||||
if (clientFailure !== undefined) return fail(clientFailure);
|
||||
const skill = await host.installSkill();
|
||||
if (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");
|
||||
if (installSkill) {
|
||||
const skill = await host.installSkill();
|
||||
if (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");
|
||||
}
|
||||
const doctor = await host.doctor();
|
||||
const remediation = finalSetupRemediation(
|
||||
host.platform,
|
||||
@@ -261,6 +273,7 @@ const systemSetupHost = (): SetupHost => {
|
||||
: client.format === "toml"
|
||||
? configureTomlClient(client, hopperPath, command)
|
||||
: configureJsonClient(client, hopperPath, command),
|
||||
skillInstallationNeeded: () => canonicalSkillInstallationNeeded(homedir()),
|
||||
installSkill: () => installCanonicalSkill(homedir()),
|
||||
doctor: () => runDoctor(undefined, doctorHost),
|
||||
};
|
||||
|
||||
@@ -14,7 +14,7 @@ const failedConfigurationMessage = (
|
||||
return "Agent configuration could not be verified after writing. Repair the configuration file or restore its `.rea.backup`, then rerun setup.";
|
||||
};
|
||||
|
||||
/** Configure each detected agent, stopping after the first failed transaction. */
|
||||
/** Configure every detected agent and report all failed transaction classes. */
|
||||
export const configureDetectedClients = async (options: {
|
||||
readonly host: SetupHost;
|
||||
readonly detectedClients: readonly SetupClient[];
|
||||
@@ -23,6 +23,7 @@ export const configureDetectedClients = async (options: {
|
||||
readonly clients: Record<string, ClientConfigurationResult>;
|
||||
readonly appliedActions: string[];
|
||||
}): Promise<string | undefined> => {
|
||||
const failures = new Set<"backup" | "write" | "readback">();
|
||||
for (const client of options.detectedClients) {
|
||||
const result = await options.host.configureClient(
|
||||
client,
|
||||
@@ -30,10 +31,11 @@ export const configureDetectedClients = async (options: {
|
||||
options.command,
|
||||
);
|
||||
options.clients[client.name] = result;
|
||||
if (result.status === "failed")
|
||||
return failedConfigurationMessage(result.reason);
|
||||
if (result.status === "failed") failures.add(result.reason);
|
||||
if (result.status === "configured")
|
||||
options.appliedActions.push(`configured_${client.name}`);
|
||||
}
|
||||
return undefined;
|
||||
return failures.size === 0
|
||||
? undefined
|
||||
: [...failures].map(failedConfigurationMessage).join(" ");
|
||||
};
|
||||
|
||||
@@ -5,6 +5,7 @@ export const setupPlan = (
|
||||
platform: NodeJS.Platform,
|
||||
installHopper: boolean,
|
||||
clients: readonly SetupClient[],
|
||||
installSkill: boolean,
|
||||
): readonly SetupAction[] => [
|
||||
...(installHopper
|
||||
? [
|
||||
@@ -32,10 +33,14 @@ export const setupPlan = (
|
||||
external: false,
|
||||
}),
|
||||
),
|
||||
{
|
||||
kind: "install_skill",
|
||||
target: "~/.agents/skills/rea-analysis/SKILL.md",
|
||||
detail: "Install or update the bundled REA analysis skill.",
|
||||
external: false,
|
||||
},
|
||||
...(installSkill
|
||||
? [
|
||||
{
|
||||
kind: "install_skill" as const,
|
||||
target: "~/.agents/skills/rea-analysis/SKILL.md",
|
||||
detail: "Install or update the bundled REA analysis skill.",
|
||||
external: false,
|
||||
},
|
||||
]
|
||||
: []),
|
||||
];
|
||||
|
||||
@@ -4,26 +4,43 @@ import writeFileAtomic from "write-file-atomic";
|
||||
|
||||
import { PRODUCT_IDENTITY } from "../identity.js";
|
||||
|
||||
const canonicalSkillDestination = (home: string): string =>
|
||||
join(home, ".agents/skills", PRODUCT_IDENTITY.skillName, "SKILL.md");
|
||||
|
||||
const readCanonicalSkill = (): Promise<string> =>
|
||||
readFile(
|
||||
new URL(
|
||||
`../../skills/${PRODUCT_IDENTITY.skillName}/SKILL.md`,
|
||||
import.meta.url,
|
||||
),
|
||||
"utf8",
|
||||
);
|
||||
|
||||
/** Determine whether setup would need to install or update the canonical skill. */
|
||||
export const canonicalSkillInstallationNeeded = async (
|
||||
home: string,
|
||||
): Promise<boolean> => {
|
||||
try {
|
||||
const [canonical, installed] = await Promise.all([
|
||||
readCanonicalSkill(),
|
||||
readFile(canonicalSkillDestination(home), "utf8").catch(() => undefined),
|
||||
]);
|
||||
return installed !== canonical;
|
||||
} catch {
|
||||
// Disclose the action when setup cannot prove the installed copy is aligned.
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
/** Transactionally install or upgrade the versioned canonical REA skill. */
|
||||
export const installCanonicalSkill = async (
|
||||
home: string,
|
||||
): Promise<"installed" | "unchanged" | "failed"> => {
|
||||
const destination = join(
|
||||
home,
|
||||
".agents/skills",
|
||||
PRODUCT_IDENTITY.skillName,
|
||||
"SKILL.md",
|
||||
);
|
||||
const destination = canonicalSkillDestination(home);
|
||||
const backup = `${destination}.rea.backup`;
|
||||
let original: string | undefined;
|
||||
try {
|
||||
const content = await readFile(
|
||||
new URL(
|
||||
`../../skills/${PRODUCT_IDENTITY.skillName}/SKILL.md`,
|
||||
import.meta.url,
|
||||
),
|
||||
"utf8",
|
||||
);
|
||||
const content = await readCanonicalSkill();
|
||||
original = await readFile(destination, "utf8").catch(() => undefined);
|
||||
if (original === content) return "unchanged";
|
||||
await mkdir(dirname(destination), { recursive: true });
|
||||
|
||||
@@ -194,6 +194,8 @@ const runCommand = (
|
||||
): Promise<boolean> =>
|
||||
new Promise((resolveResult) => {
|
||||
const child = spawn(command, [...arguments_], {
|
||||
// Reserve parent stdout for Incur's structured result. Array positions
|
||||
// are child fds 0/1/2, so npm stdout and stderr both reach parent stderr.
|
||||
stdio:
|
||||
output === "human" ? "inherit" : ["inherit", process.stderr, "inherit"],
|
||||
});
|
||||
|
||||
+3
-1
@@ -160,7 +160,9 @@ const registerSetupCommands = (
|
||||
installHopper: z
|
||||
.boolean()
|
||||
.default(false)
|
||||
.describe("Also approve Hopper installation with --yes"),
|
||||
.describe(
|
||||
"Approve Hopper installation when missing or repair is needed (requires --yes)",
|
||||
),
|
||||
}),
|
||||
alias: { yes: "y", installHopper: "install-hopper" },
|
||||
run: ({ options, formatExplicit }) =>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Cli, z } from "incur";
|
||||
import { createInterface } from "node:readline/promises";
|
||||
|
||||
import { loadConfiguredPermissionAuthority } from "./application/PermissionConfiguration.js";
|
||||
import {
|
||||
@@ -49,9 +50,13 @@ export const registerPolicyCommands = (
|
||||
network: z.enum(["none", "loopback", "external"]).default("none"),
|
||||
mount: z.boolean().default(false),
|
||||
write: z.boolean().default(false),
|
||||
yes: z
|
||||
.boolean()
|
||||
.default(false)
|
||||
.describe("Confirm a destructive policy revocation without prompting"),
|
||||
}),
|
||||
alias: { environmentNames: "environment-names" },
|
||||
run: ({ args, options }) =>
|
||||
alias: { environmentNames: "environment-names", yes: "y" },
|
||||
run: ({ args, options, formatExplicit }) =>
|
||||
logCliCommand(logger, "policy", async () => {
|
||||
const config = parseConfig(process.env);
|
||||
if (!config.ok) return failure(config.error);
|
||||
@@ -76,6 +81,22 @@ export const registerPolicyCommands = (
|
||||
permissionProjectStore === undefined
|
||||
)
|
||||
return { error: "Project policy is not configured" };
|
||||
if (!options.yes) {
|
||||
if (formatExplicit || process.stdin.isTTY !== true)
|
||||
return {
|
||||
error: "ConfirmationRequired",
|
||||
message: "Policy grant revocation requires confirmation",
|
||||
grant_id: args.value,
|
||||
remediation:
|
||||
"Verify the grant ID, then rerun the same command with --yes.",
|
||||
};
|
||||
if (!(await confirmRevocation(args.value)))
|
||||
return {
|
||||
error: "Cancelled",
|
||||
message: "Policy grant revocation was cancelled",
|
||||
grant_id: args.value,
|
||||
};
|
||||
}
|
||||
const revoked = await revokeProjectPermissionGrant(
|
||||
permissionProjectStore,
|
||||
permissionProjectRoot,
|
||||
@@ -100,6 +121,23 @@ export const registerPolicyCommands = (
|
||||
});
|
||||
};
|
||||
|
||||
const confirmRevocation = async (grantId: string): Promise<boolean> => {
|
||||
const prompt = createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
});
|
||||
try {
|
||||
const answer = (
|
||||
await prompt.question(`Revoke policy grant "${grantId}"? [y/N] `)
|
||||
)
|
||||
.trim()
|
||||
.toLowerCase();
|
||||
return answer === "y" || answer === "yes";
|
||||
} finally {
|
||||
prompt.close();
|
||||
}
|
||||
};
|
||||
|
||||
const explain = async (
|
||||
config: AppConfig,
|
||||
capability: PermissionCapability,
|
||||
|
||||
@@ -187,9 +187,7 @@ const linuxDemoLaunch = (
|
||||
};
|
||||
|
||||
const usesLinuxDemo = (options: HopperApplicationLauncherOptions): boolean =>
|
||||
process.platform === "linux" &&
|
||||
basename(options.launcherPath) === "Hopper" &&
|
||||
options.demoHelperPath !== undefined;
|
||||
process.platform === "linux" && options.demoHelperPath !== undefined;
|
||||
|
||||
const spawnLauncher = async (
|
||||
command: string,
|
||||
|
||||
Reference in New Issue
Block a user