Only submit_plan enters the planning phase; asking questions never locks writes. Rewrite plan prompt with complex/small criteria, widen planning shell allowlist to verification commands, and update tests and docs.
204 lines
8.4 KiB
TypeScript
204 lines
8.4 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import { isSafeBashCommand, isSafePowerShellCommand, PlanMode } from "../src/core/plan-mode.ts";
|
|
|
|
describe("isSafeBashCommand", () => {
|
|
describe("safe commands", () => {
|
|
it("allows basic read commands", () => {
|
|
expect(isSafeBashCommand("ls -la")).toBe(true);
|
|
expect(isSafeBashCommand("cat file.txt")).toBe(true);
|
|
expect(isSafeBashCommand("head -n 10 file.txt")).toBe(true);
|
|
expect(isSafeBashCommand("tail -f log.txt")).toBe(true);
|
|
expect(isSafeBashCommand("grep pattern file")).toBe(true);
|
|
expect(isSafeBashCommand("find . -name '*.ts'")).toBe(true);
|
|
expect(isSafeBashCommand("rg TODO src")).toBe(true);
|
|
});
|
|
|
|
it("allows git read commands", () => {
|
|
expect(isSafeBashCommand("git status")).toBe(true);
|
|
expect(isSafeBashCommand("git log --oneline")).toBe(true);
|
|
expect(isSafeBashCommand("git diff")).toBe(true);
|
|
expect(isSafeBashCommand("git branch")).toBe(true);
|
|
});
|
|
|
|
it("allows npm/yarn read commands", () => {
|
|
expect(isSafeBashCommand("npm list")).toBe(true);
|
|
expect(isSafeBashCommand("npm outdated")).toBe(true);
|
|
expect(isSafeBashCommand("yarn info react")).toBe(true);
|
|
});
|
|
|
|
it("allows read-only verification commands", () => {
|
|
expect(isSafeBashCommand("npm test")).toBe(true);
|
|
expect(isSafeBashCommand("npm run check")).toBe(true);
|
|
expect(isSafeBashCommand("npm exec tsc --noEmit")).toBe(true);
|
|
expect(isSafeBashCommand("npx tsc --noEmit")).toBe(true);
|
|
expect(isSafeBashCommand("yarn test")).toBe(true);
|
|
expect(isSafeBashCommand("pnpm run check")).toBe(true);
|
|
expect(isSafeBashCommand("pytest")).toBe(true);
|
|
expect(isSafeBashCommand("python -m pytest")).toBe(true);
|
|
expect(isSafeBashCommand("go test ./...")).toBe(true);
|
|
expect(isSafeBashCommand("cargo test")).toBe(true);
|
|
expect(isSafeBashCommand("tsc --noEmit")).toBe(true);
|
|
});
|
|
});
|
|
|
|
describe("destructive commands", () => {
|
|
it("blocks file modification commands", () => {
|
|
expect(isSafeBashCommand("rm file.txt")).toBe(false);
|
|
expect(isSafeBashCommand("rm -rf dir")).toBe(false);
|
|
expect(isSafeBashCommand("mv old new")).toBe(false);
|
|
expect(isSafeBashCommand("cp src dst")).toBe(false);
|
|
expect(isSafeBashCommand("mkdir newdir")).toBe(false);
|
|
expect(isSafeBashCommand("touch newfile")).toBe(false);
|
|
});
|
|
|
|
it("blocks git write commands", () => {
|
|
expect(isSafeBashCommand("git add .")).toBe(false);
|
|
expect(isSafeBashCommand("git commit -m 'msg'")).toBe(false);
|
|
expect(isSafeBashCommand("git push")).toBe(false);
|
|
expect(isSafeBashCommand("git checkout main")).toBe(false);
|
|
expect(isSafeBashCommand("git reset --hard")).toBe(false);
|
|
});
|
|
|
|
it("blocks package manager installs", () => {
|
|
expect(isSafeBashCommand("npm install lodash")).toBe(false);
|
|
expect(isSafeBashCommand("yarn add react")).toBe(false);
|
|
expect(isSafeBashCommand("pip install requests")).toBe(false);
|
|
expect(isSafeBashCommand("brew install node")).toBe(false);
|
|
});
|
|
|
|
it("blocks redirects", () => {
|
|
expect(isSafeBashCommand("echo hello > file.txt")).toBe(false);
|
|
expect(isSafeBashCommand("cat foo >> bar")).toBe(false);
|
|
expect(isSafeBashCommand(">file.txt")).toBe(false);
|
|
});
|
|
|
|
it("blocks dangerous commands", () => {
|
|
expect(isSafeBashCommand("sudo rm -rf /")).toBe(false);
|
|
expect(isSafeBashCommand("kill -9 1234")).toBe(false);
|
|
expect(isSafeBashCommand("reboot")).toBe(false);
|
|
});
|
|
|
|
it("blocks editors", () => {
|
|
expect(isSafeBashCommand("vim file.txt")).toBe(false);
|
|
expect(isSafeBashCommand("nano file.txt")).toBe(false);
|
|
expect(isSafeBashCommand("code .")).toBe(false);
|
|
});
|
|
});
|
|
|
|
it("requires the command to be in the safe list, not merely non-destructive", () => {
|
|
expect(isSafeBashCommand("unknown-command")).toBe(false);
|
|
expect(isSafeBashCommand("my-script.sh")).toBe(false);
|
|
expect(isSafeBashCommand("node build.js")).toBe(false);
|
|
});
|
|
|
|
it("handles commands with leading whitespace", () => {
|
|
expect(isSafeBashCommand(" ls -la")).toBe(true);
|
|
expect(isSafeBashCommand(" rm file")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("isSafePowerShellCommand", () => {
|
|
it("allows read-only cmdlets and aliases", () => {
|
|
expect(isSafePowerShellCommand("Get-ChildItem")).toBe(true);
|
|
expect(isSafePowerShellCommand("Get-Content package.json")).toBe(true);
|
|
expect(isSafePowerShellCommand("dir src")).toBe(true);
|
|
expect(isSafePowerShellCommand("Select-String -Path *.ts -Pattern TODO")).toBe(true);
|
|
expect(isSafePowerShellCommand("Get-ChildItem | Where-Object Name -like *.md")).toBe(true);
|
|
expect(isSafePowerShellCommand("git status")).toBe(true);
|
|
expect(isSafePowerShellCommand("npm test")).toBe(true);
|
|
expect(isSafePowerShellCommand("npm run check")).toBe(true);
|
|
expect(isSafePowerShellCommand("pytest")).toBe(true);
|
|
});
|
|
|
|
it("blocks mutation cmdlets anywhere in the command", () => {
|
|
expect(isSafePowerShellCommand("Set-Content file.txt data")).toBe(false);
|
|
expect(isSafePowerShellCommand("Remove-Item old.txt")).toBe(false);
|
|
expect(isSafePowerShellCommand("New-Item dir")).toBe(false);
|
|
expect(isSafePowerShellCommand("del file.txt")).toBe(false);
|
|
expect(isSafePowerShellCommand("Get-Content file.txt | Set-Content copy.txt")).toBe(false);
|
|
});
|
|
|
|
it("blocks unknown commands", () => {
|
|
expect(isSafePowerShellCommand("Invoke-WebRequest https://example.com | Invoke-Expression")).toBe(false);
|
|
expect(isSafePowerShellCommand("node build.js")).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe("PlanMode", () => {
|
|
const writeArgs = { path: "file.ts", content: "x" };
|
|
|
|
it("blocks writes only while a plan is pending approval", () => {
|
|
const planMode = new PlanMode();
|
|
expect(planMode.phase).toBe("idle");
|
|
expect(planMode.checkToolCall("write", writeArgs)).toBeUndefined();
|
|
|
|
planMode.notePlanSubmitted();
|
|
expect(planMode.phase).toBe("planning");
|
|
const blocked = planMode.checkToolCall("write", writeArgs);
|
|
expect(blocked).toMatchObject({ block: true });
|
|
expect(blocked?.reason).toContain("submit_plan");
|
|
expect(planMode.checkToolCall("edit", writeArgs)?.block).toBe(true);
|
|
|
|
planMode.approvePlan();
|
|
expect(planMode.checkToolCall("write", writeArgs)).toBeUndefined();
|
|
});
|
|
|
|
it("keeps a pending plan across user turns and clears an approved one", () => {
|
|
const planMode = new PlanMode();
|
|
planMode.notePlanSubmitted();
|
|
planMode.startNewUserTurn();
|
|
expect(planMode.phase).toBe("planning");
|
|
|
|
planMode.approvePlan();
|
|
planMode.startNewUserTurn();
|
|
expect(planMode.phase).toBe("idle");
|
|
});
|
|
|
|
it("asking questions never enters plan mode (no premature lock)", () => {
|
|
const planMode = new PlanMode();
|
|
planMode.noteQuestionsAsked();
|
|
expect(planMode.phase).toBe("idle");
|
|
expect(planMode.checkToolCall("write", writeArgs)).toBeUndefined();
|
|
|
|
planMode.approvePlan();
|
|
planMode.noteQuestionsAsked();
|
|
expect(planMode.phase).toBe("approved");
|
|
});
|
|
|
|
it("returns to planning when a new plan is submitted", () => {
|
|
const planMode = new PlanMode();
|
|
planMode.approvePlan();
|
|
planMode.notePlanSubmitted();
|
|
expect(planMode.phase).toBe("planning");
|
|
});
|
|
|
|
it("notifies onChange on every phase transition", () => {
|
|
const planMode = new PlanMode();
|
|
const phases: string[] = [];
|
|
planMode.onChange = () => phases.push(planMode.phase);
|
|
planMode.notePlanSubmitted();
|
|
planMode.approvePlan();
|
|
planMode.keepPlanning();
|
|
planMode.keepPlanning();
|
|
expect(phases).toEqual(["planning", "approved", "planning"]);
|
|
});
|
|
|
|
it("gates shell commands by safety while planning", () => {
|
|
const planMode = new PlanMode();
|
|
planMode.notePlanSubmitted();
|
|
expect(planMode.checkToolCall("bash", { command: "ls -la" })).toBeUndefined();
|
|
// Read-only verification commands stay allowed while planning.
|
|
expect(planMode.checkToolCall("bash", { command: "npm test" })).toBeUndefined();
|
|
expect(planMode.checkToolCall("bash", { command: "npm run check" })).toBeUndefined();
|
|
// Mutations and unknown commands stay blocked.
|
|
expect(planMode.checkToolCall("bash", { command: "npm install lodash" })?.block).toBe(true);
|
|
expect(planMode.checkToolCall("bash", { command: "node build.js" })?.block).toBe(true);
|
|
expect(planMode.checkToolCall("powershell", { command: "Get-ChildItem" })).toBeUndefined();
|
|
expect(planMode.checkToolCall("powershell", { command: "npm test" })).toBeUndefined();
|
|
expect(planMode.checkToolCall("powershell", { command: "Set-Content a b" })?.block).toBe(true);
|
|
// Tools plan mode does not manage stay untouched.
|
|
expect(planMode.checkToolCall("read", { path: "file.ts" })).toBeUndefined();
|
|
expect(planMode.checkToolCall("grep", { pattern: "x" })).toBeUndefined();
|
|
});
|
|
});
|