fix(cli): harden setup and destructive workflows
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import { HopperApplicationLauncher } from "../src/hopper/BridgeLauncher.js";
|
||||
|
||||
const directories: string[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
directories
|
||||
.splice(0)
|
||||
.map((path) => rm(path, { recursive: true, force: true })),
|
||||
);
|
||||
});
|
||||
|
||||
describe("Hopper application launcher", () => {
|
||||
it.skipIf(process.platform !== "linux")(
|
||||
"uses the Linux demo helper for a non-canonical launcher path",
|
||||
async () => {
|
||||
const directory = await mkdtemp(join(tmpdir(), "rea-launcher-test-"));
|
||||
directories.push(directory);
|
||||
const helper = join(directory, "demo-helper.py");
|
||||
await writeFile(helper, "import sys\nsys.exit(0)\n");
|
||||
const launcher = new HopperApplicationLauncher({
|
||||
launcherPath: "/usr/bin/true",
|
||||
targetPath: "/tmp/fixture",
|
||||
targetKind: "executable",
|
||||
loaderArgs: [],
|
||||
bridgeScriptPath: "/tmp/hopper_bridge.py",
|
||||
demoHelperPath: helper,
|
||||
});
|
||||
|
||||
const launched = await launcher.launch({
|
||||
directory,
|
||||
socketPath: join(directory, "bridge.sock"),
|
||||
token: "token",
|
||||
runId: "run-wrapper",
|
||||
});
|
||||
|
||||
expect(launched.ok).toBe(true);
|
||||
if (!launched.ok) return;
|
||||
expect(launched.value.process.spawnfile).toBe("/usr/bin/python3");
|
||||
expect(launched.value.process.spawnargs).toContain(helper);
|
||||
expect(launched.value.shutdownByCleanup).toBe(true);
|
||||
expect(await readFile(join(directory, "bootstrap.py"), "utf8")).toContain(
|
||||
"REA_OWNS_PROCESS_LIFETIME = True",
|
||||
);
|
||||
if (launched.value.process.exitCode === null)
|
||||
await new Promise<void>((resolve) => {
|
||||
launched.value.process.once("exit", () => resolve());
|
||||
});
|
||||
},
|
||||
);
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdtemp, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
readProjectPermissionStore,
|
||||
writeProjectPermissionStore,
|
||||
} from "../src/application/ProjectPermissionStore.js";
|
||||
|
||||
const execFileAsync = promisify(execFile);
|
||||
const CLI_INTEGRATION_TIMEOUT_MS = 60_000;
|
||||
const roots: string[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
await Promise.all(
|
||||
roots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
|
||||
);
|
||||
});
|
||||
|
||||
describe("policy CLI revocation", () => {
|
||||
it(
|
||||
"requires confirmation in structured mode and accepts explicit approval",
|
||||
async () => {
|
||||
const project = await mkdtemp(join(tmpdir(), "rea-policy-cli-"));
|
||||
roots.push(project);
|
||||
const store = join(project, ".rea", "permissions.json");
|
||||
const grant = {
|
||||
grant_id: "project:evidence-read",
|
||||
capability: "evidence_read" as const,
|
||||
roots: [project],
|
||||
executables: [],
|
||||
environment_names: [],
|
||||
network: "none" as const,
|
||||
mount: false,
|
||||
lifetime: "project" as const,
|
||||
operation_identity: null,
|
||||
expires_at: null,
|
||||
};
|
||||
expect(
|
||||
(await writeProjectPermissionStore(store, project, [grant])).ok,
|
||||
).toBe(true);
|
||||
const environment = {
|
||||
...process.env,
|
||||
REA_PERMISSION_PROJECT_ROOT: project,
|
||||
REA_PERMISSION_PROJECT_STORE: store,
|
||||
};
|
||||
|
||||
const unapproved = await execFileAsync(
|
||||
process.execPath,
|
||||
["scripts/rea.mjs", "--json", "policy", "revoke", grant.grant_id],
|
||||
{ cwd: process.cwd(), env: environment },
|
||||
).catch((cause: unknown) => cause);
|
||||
expect(unapproved).toMatchObject({ code: 1, stderr: "" });
|
||||
expect(
|
||||
JSON.parse((unapproved as { readonly stdout: string }).stdout),
|
||||
).toEqual({
|
||||
error: "ConfirmationRequired",
|
||||
message: "Policy grant revocation requires confirmation",
|
||||
grant_id: grant.grant_id,
|
||||
remediation:
|
||||
"Verify the grant ID, then rerun the same command with --yes.",
|
||||
});
|
||||
expect(await readProjectPermissionStore(store, project)).toMatchObject({
|
||||
ok: true,
|
||||
value: { grants: [grant] },
|
||||
});
|
||||
|
||||
const approved = await execFileAsync(
|
||||
process.execPath,
|
||||
[
|
||||
"scripts/rea.mjs",
|
||||
"--json",
|
||||
"policy",
|
||||
"revoke",
|
||||
grant.grant_id,
|
||||
"--yes",
|
||||
],
|
||||
{ cwd: process.cwd(), env: environment },
|
||||
);
|
||||
expect(JSON.parse(approved.stdout)).toEqual({
|
||||
revoked: grant.grant_id,
|
||||
reload: "Send SIGHUP to the REA MCP process",
|
||||
});
|
||||
expect(await readProjectPermissionStore(store, project)).toMatchObject({
|
||||
ok: true,
|
||||
value: { grants: [] },
|
||||
});
|
||||
},
|
||||
CLI_INTEGRATION_TIMEOUT_MS,
|
||||
);
|
||||
});
|
||||
+64
-2
@@ -19,9 +19,11 @@ class FakeSetupHost implements SetupHost {
|
||||
hopper: string | undefined;
|
||||
hopperInstallSucceeds = true;
|
||||
skill: "installed" | "unchanged" | "failed" = "installed";
|
||||
skillNeedsInstallation = true;
|
||||
clients: readonly SetupClient[] = [];
|
||||
clientResults = new Map<string, ClientConfigurationResult>();
|
||||
hopperInstalls = 0;
|
||||
skillInstallations = 0;
|
||||
configurations = 0;
|
||||
doctorHealthy: boolean | undefined;
|
||||
linuxDemoRuntimeMissing = false;
|
||||
@@ -62,8 +64,12 @@ class FakeSetupHost implements SetupHost {
|
||||
this.clientResults.get(client.name) ?? { status: "configured" },
|
||||
);
|
||||
};
|
||||
installSkill = (): Promise<"installed" | "unchanged" | "failed"> =>
|
||||
Promise.resolve(this.skill);
|
||||
skillInstallationNeeded = (): Promise<boolean> =>
|
||||
Promise.resolve(this.skillNeedsInstallation);
|
||||
installSkill = (): Promise<"installed" | "unchanged" | "failed"> => {
|
||||
this.skillInstallations += 1;
|
||||
return Promise.resolve(this.skill);
|
||||
};
|
||||
doctor = (): Promise<{
|
||||
healthy: boolean;
|
||||
hopperPath?: string;
|
||||
@@ -126,6 +132,21 @@ describe("setup workflow", () => {
|
||||
);
|
||||
});
|
||||
|
||||
it("omits an aligned skill from the plan and does not reinstall it", async () => {
|
||||
const host = new FakeSetupHost();
|
||||
host.hopper = "/Applications/Hopper";
|
||||
host.skillNeedsInstallation = false;
|
||||
|
||||
const result = await runSetup(options(true), host);
|
||||
|
||||
expect(result.status).toBe("ready");
|
||||
expect(result.plannedActions.map(({ kind }) => kind)).not.toContain(
|
||||
"install_skill",
|
||||
);
|
||||
expect(result.appliedActions).toEqual([]);
|
||||
expect(host.skillInstallations).toBe(0);
|
||||
});
|
||||
|
||||
it("reports ready after an accepted Linux plan installs healthy Hopper", async () => {
|
||||
const host = new FakeSetupHost("linux");
|
||||
host.distribution = {
|
||||
@@ -218,6 +239,19 @@ describe("setup workflow", () => {
|
||||
expect(host.hopperInstalls).toBe(0);
|
||||
});
|
||||
|
||||
it("treats --install-hopper as approval, not a healthy reinstall request", async () => {
|
||||
const host = new FakeSetupHost();
|
||||
host.hopper = "/Applications/Hopper";
|
||||
|
||||
const result = await runSetup(options(true, true), host);
|
||||
|
||||
expect(result.status).toBe("ready");
|
||||
expect(result.plannedActions.map(({ kind }) => kind)).not.toContain(
|
||||
"install_hopper",
|
||||
);
|
||||
expect(host.hopperInstalls).toBe(0);
|
||||
});
|
||||
|
||||
it("installs missing Linux demo dependencies for existing Hopper", async () => {
|
||||
const host = new FakeSetupHost("linux");
|
||||
host.distribution = {
|
||||
@@ -297,6 +331,34 @@ describe("setup workflow", () => {
|
||||
expect(result.remediation).not.toContain("internal_client_key");
|
||||
});
|
||||
|
||||
it("records later client outcomes after an earlier configuration fails", async () => {
|
||||
const host = new FakeSetupHost();
|
||||
host.hopper = "/Applications/Hopper";
|
||||
host.clients = [
|
||||
{ name: "before", configPath: "/before.json" },
|
||||
{ name: "broken", configPath: "/broken.json" },
|
||||
{ name: "after", configPath: "/after.json" },
|
||||
];
|
||||
host.clientResults.set("broken", { status: "failed", reason: "write" });
|
||||
|
||||
const result = await runSetup(options(true), host);
|
||||
|
||||
expect(result.status).toBe("needs_human");
|
||||
expect(host.configurations).toBe(3);
|
||||
expect(result.clients).toEqual({
|
||||
before: { status: "configured" },
|
||||
broken: { status: "failed", reason: "write" },
|
||||
after: { status: "configured" },
|
||||
});
|
||||
expect(result.appliedActions).toEqual([
|
||||
"configured_before",
|
||||
"configured_after",
|
||||
]);
|
||||
expect(result.remediation).toBe(
|
||||
"Agent configuration could not be updated. Check file permissions, then rerun setup.",
|
||||
);
|
||||
});
|
||||
|
||||
it("explains skill installation recovery", async () => {
|
||||
const host = new FakeSetupHost();
|
||||
host.hopper = "/Applications/Hopper";
|
||||
|
||||
@@ -3,6 +3,7 @@ import { tmpdir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
import { afterEach, describe, expect, it } from "vitest";
|
||||
import { installCanonicalSkill } from "../src/application/Setup.js";
|
||||
import { canonicalSkillInstallationNeeded } from "../src/application/SetupSkill.js";
|
||||
import { TOOL_CONTRACTS } from "../src/contracts/toolContracts.js";
|
||||
|
||||
const roots: string[] = [];
|
||||
@@ -32,6 +33,18 @@ describe("canonical skill transaction", () => {
|
||||
`tool_count: ${String(TOOL_CONTRACTS.length)}`,
|
||||
);
|
||||
expect(await readFile(sibling, "utf8")).toBe("unrelated skill\n");
|
||||
expect(await canonicalSkillInstallationNeeded(home)).toBe(false);
|
||||
expect(await installCanonicalSkill(home)).toBe("unchanged");
|
||||
});
|
||||
|
||||
it("reports a missing or stale canonical skill as needing installation", async () => {
|
||||
const home = await mkdtemp(join(tmpdir(), "rea-skill-plan-test-"));
|
||||
roots.push(home);
|
||||
const destination = join(home, ".agents/skills/rea-analysis/SKILL.md");
|
||||
|
||||
expect(await canonicalSkillInstallationNeeded(home)).toBe(true);
|
||||
await mkdir(dirname(destination), { recursive: true });
|
||||
await writeFile(destination, "stale managed skill\n");
|
||||
expect(await canonicalSkillInstallationNeeded(home)).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user