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.
222 lines
8.0 KiB
TypeScript
222 lines
8.0 KiB
TypeScript
import { existsSync } from "node:fs";
|
|
import { join } from "node:path";
|
|
import { fauxAssistantMessage, fauxToolCall } from "@earendil-works/pi-ai";
|
|
import { beforeAll, describe, expect, test } from "vitest";
|
|
import type { ExtensionUIContext } from "../src/core/extensions/types.ts";
|
|
import { PLAN_MODE_PROMPT } from "../src/core/plan-mode.ts";
|
|
import { initTheme, theme } from "../src/modes/interactive/theme/theme.ts";
|
|
import { createHarness, type Harness } from "./suite/harness.ts";
|
|
|
|
beforeAll(() => {
|
|
initTheme("dark");
|
|
});
|
|
|
|
interface MockUI {
|
|
ui: ExtensionUIContext;
|
|
statusLog: Array<{ key: string; text: string | undefined }>;
|
|
}
|
|
|
|
/**
|
|
* Minimal ExtensionUIContext for driving dialogs from tests. `select` answers in order,
|
|
* `input` returns the next scripted value, everything else is a no-op.
|
|
*/
|
|
function createMockUI(options: { selectAnswers?: string[]; inputs?: string[] } = {}): MockUI {
|
|
const selectAnswers = [...(options.selectAnswers ?? [])];
|
|
const inputs = [...(options.inputs ?? [])];
|
|
const statusLog: Array<{ key: string; text: string | undefined }> = [];
|
|
const ui: ExtensionUIContext = {
|
|
select: async () => selectAnswers.shift(),
|
|
confirm: async () => true,
|
|
input: async () => inputs.shift(),
|
|
notify: () => {},
|
|
onTerminalInput: () => () => {},
|
|
setStatus: (key, text) => {
|
|
statusLog.push({ key, text });
|
|
},
|
|
setWorkingMessage: () => {},
|
|
setWorkingVisible: () => {},
|
|
setWorkingIndicator: () => {},
|
|
setHiddenThinkingLabel: () => {},
|
|
setWidget: () => {},
|
|
setFooter: () => {},
|
|
setHeader: () => {},
|
|
setTitle: () => {},
|
|
custom: async () => {
|
|
throw new Error("custom dialogs are not supported by the mock UI");
|
|
},
|
|
pasteToEditor: () => {},
|
|
setEditorText: () => {},
|
|
getEditorText: () => "",
|
|
editor: async () => undefined,
|
|
addAutocompleteProvider: () => {},
|
|
setEditorComponent: () => {},
|
|
getEditorComponent: () => undefined,
|
|
theme,
|
|
getAllThemes: () => [],
|
|
getTheme: () => undefined,
|
|
setTheme: () => ({ success: false, error: "not supported" }),
|
|
getToolsExpanded: () => false,
|
|
setToolsExpanded: () => {},
|
|
};
|
|
return { ui, statusLog };
|
|
}
|
|
|
|
function toolResults(harness: Harness): Array<{ toolName: string; isError: boolean; text: string }> {
|
|
return harness.session.messages
|
|
.filter((message) => message.role === "toolResult")
|
|
.map((message) => ({
|
|
toolName: message.toolName,
|
|
isError: message.isError ?? false,
|
|
text: message.content
|
|
.filter((part) => part.type === "text")
|
|
.map((part) => part.text ?? "")
|
|
.join("\n"),
|
|
}));
|
|
}
|
|
|
|
const questionnaireCall = fauxToolCall("questionnaire", {
|
|
questions: [{ id: "scope", prompt: "Which scope?", options: [{ value: "a", label: "Option A" }] }],
|
|
});
|
|
|
|
const toolUse = (call: ReturnType<typeof fauxToolCall>) => fauxAssistantMessage([call], { stopReason: "toolUse" });
|
|
|
|
describe("built-in plan mode", () => {
|
|
test("asking questions never locks writes; only a revised plan blocks until approved", async () => {
|
|
const harness = await createHarness();
|
|
try {
|
|
const { ui, statusLog } = createMockUI({
|
|
selectAnswers: ["Option A", "Revise the plan", "Execute the plan"],
|
|
inputs: ["please add tests"],
|
|
});
|
|
await harness.session.bindExtensions({ uiContext: ui, mode: "rpc" });
|
|
expect(harness.session.getActiveToolNames()).toEqual([
|
|
"read",
|
|
"bash",
|
|
"edit",
|
|
"write",
|
|
"subagent",
|
|
"questionnaire",
|
|
"submit_plan",
|
|
]);
|
|
expect(harness.session.systemPrompt).toContain(PLAN_MODE_PROMPT);
|
|
|
|
harness.setResponses([
|
|
toolUse(questionnaireCall),
|
|
toolUse(fauxToolCall("write", { path: "before-plan.txt", content: "not blocked" })),
|
|
toolUse(fauxToolCall("submit_plan", { plan: "1. Do the thing" })),
|
|
toolUse(fauxToolCall("write", { path: "plan.txt", content: "blocked" })),
|
|
toolUse(fauxToolCall("submit_plan", { plan: "1. Do the thing\n2. Add tests" })),
|
|
toolUse(fauxToolCall("write", { path: "plan.txt", content: "written" })),
|
|
fauxAssistantMessage("done"),
|
|
]);
|
|
await harness.session.prompt("implement the feature");
|
|
|
|
const results = toolResults(harness);
|
|
expect(results.map((result) => result.toolName)).toEqual([
|
|
"questionnaire",
|
|
"write",
|
|
"submit_plan",
|
|
"write",
|
|
"submit_plan",
|
|
"write",
|
|
]);
|
|
expect(results[0]?.isError).toBe(false);
|
|
expect(results[0]?.text).toContain("user selected: 1. Option A");
|
|
// A clarifying question alone never locks: the write right after it succeeds.
|
|
expect(results[1]?.isError).toBe(false);
|
|
// The revise outcome keeps writes blocked and names the next step.
|
|
expect(results[2]?.text).toContain("requested changes");
|
|
expect(results[3]?.isError).toBe(true);
|
|
expect(results[3]?.text).toContain("Plan mode: write is blocked");
|
|
expect(results[3]?.text).toContain("submit_plan");
|
|
// The approval dialog resolved to "Execute the plan".
|
|
expect(results[4]?.text).toContain("Plan approved by the user");
|
|
expect(results[5]?.isError).toBe(false);
|
|
expect(existsSync(join(harness.tempDir, "plan.txt"))).toBe(true);
|
|
|
|
// Planning shows a footer status; approval clears it again.
|
|
expect(statusLog.some((entry) => entry.key === "plan" && entry.text !== undefined)).toBe(true);
|
|
expect(statusLog.at(-1)).toEqual({ key: "plan", text: undefined });
|
|
} finally {
|
|
harness.cleanup();
|
|
}
|
|
});
|
|
|
|
test("keeps a revised plan pending across new user turns", async () => {
|
|
const harness = await createHarness();
|
|
try {
|
|
const { ui } = createMockUI({
|
|
selectAnswers: ["Revise the plan"],
|
|
inputs: ["add more detail"],
|
|
});
|
|
await harness.session.bindExtensions({ uiContext: ui, mode: "rpc" });
|
|
|
|
harness.setResponses([
|
|
toolUse(fauxToolCall("submit_plan", { plan: "1. Do the thing" })),
|
|
fauxAssistantMessage("waiting for a revised plan"),
|
|
]);
|
|
await harness.session.prompt("implement the feature");
|
|
// Replying after a revise must not clear the pending plan: writes stay blocked.
|
|
harness.setResponses([
|
|
toolUse(fauxToolCall("write", { path: "plan.txt", content: "blocked" })),
|
|
fauxAssistantMessage("still waiting"),
|
|
]);
|
|
await harness.session.prompt("yes, please revise");
|
|
|
|
const results = toolResults(harness);
|
|
expect(results.map((result) => result.toolName)).toEqual(["submit_plan", "write"]);
|
|
expect(results[0]?.text).toContain("requested changes");
|
|
expect(results[1]?.isError).toBe(true);
|
|
expect(results[1]?.text).toContain("Plan mode: write is blocked");
|
|
} finally {
|
|
harness.cleanup();
|
|
}
|
|
});
|
|
|
|
test("approves the plan automatically without an interactive UI", async () => {
|
|
const harness = await createHarness();
|
|
try {
|
|
await harness.session.bindExtensions({ mode: "print" });
|
|
|
|
harness.setResponses([
|
|
toolUse(questionnaireCall),
|
|
toolUse(fauxToolCall("submit_plan", { plan: "1. Do the thing" })),
|
|
toolUse(fauxToolCall("write", { path: "plan.txt", content: "written" })),
|
|
fauxAssistantMessage("done"),
|
|
]);
|
|
await harness.session.prompt("implement the feature");
|
|
|
|
const results = toolResults(harness);
|
|
expect(results[0]?.text).toContain("interactive UI is not available");
|
|
expect(results[1]?.text).toContain("Plan recorded and approved");
|
|
expect(results[2]?.isError).toBe(false);
|
|
expect(existsSync(join(harness.tempDir, "plan.txt"))).toBe(true);
|
|
} finally {
|
|
harness.cleanup();
|
|
}
|
|
});
|
|
|
|
test("stays out of the way when submit_plan is not active", async () => {
|
|
const harness = await createHarness({ initialActiveToolNames: ["read", "bash", "edit", "write"] });
|
|
try {
|
|
const { ui } = createMockUI({ selectAnswers: ["Option A"] });
|
|
await harness.session.bindExtensions({ uiContext: ui, mode: "rpc" });
|
|
expect(harness.session.systemPrompt).not.toContain("<plan_mode>");
|
|
|
|
harness.setResponses([
|
|
toolUse(questionnaireCall),
|
|
toolUse(fauxToolCall("write", { path: "plan.txt", content: "written" })),
|
|
fauxAssistantMessage("done"),
|
|
]);
|
|
await harness.session.prompt("implement the feature");
|
|
|
|
const results = toolResults(harness);
|
|
expect(results.map((result) => result.toolName)).toEqual(["questionnaire", "write"]);
|
|
expect(results[1]?.isError).toBe(false);
|
|
expect(existsSync(join(harness.tempDir, "plan.txt"))).toBe(true);
|
|
} finally {
|
|
harness.cleanup();
|
|
}
|
|
});
|
|
});
|