/** * plan — a file-backed planning mode for Pi. * * While plan mode is on, tools that are not read-only are removed from the * active set or blocked: write and edit are hidden, bash is limited to a single * read-only git command, powershell is blocked, and everything else that mutates * (worktree, MCP tools) is blocked. `subagent` stays active but is forced * read-only. The one permitted write is the `plan` tool's `write` action, which * saves the plan in the agent directory. The model investigates, saves the plan, * and calls `plan` action `exit` to read the file back and ask for approval. * * Toggle with `/plan` (or Shift+Tab, which bytefly's editor binds), or start with * `--plan`. The state is * persisted as a custom session entry, so it follows the active branch. * * Load with: pi --extension ./extensions/plan */ import type { AgentMessage } from "@earendil-works/pi-agent-core"; import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; import { fileURLToPath } from "node:url"; import { answerOn } from "../../lib/bus-request.ts"; import { isExtensionEnabled } from "../../lib/env.ts"; import { requestMount, type MountSource } from "../../lib/http/mount.ts"; import { assertDeferredPresent } from "../../lib/lazy.ts"; import { sanitizeLine } from "../../lib/format.ts"; import { lastGoal, isPursuedGoal, type PublishedGoal } from "../../lib/goal-state.ts"; import { READ_ONLY_SUMMARY, PLAN_ENTER_REQUEST, PLAN_LEAVE_REQUEST, PLAN_TOGGLE_REQUEST, type PlanModeRequest, } from "../../lib/mode-entry.ts"; import { isBoardRead, pinReadOnlySubagent } from "../../lib/policy.ts"; import { boardWriteRefusal } from "../../lib/workflow/actions.ts"; import { BASH_TOOL, BOARD_TOOL, QUESTION_TOOL, SUBAGENT_TOOL } from "../../lib/tool-names.ts"; import { clip } from "../../lib/tui/columns.ts"; import { STATUS_KEYS } from "../../lib/tui/glyphs.ts"; import { planModeNotice, registerCommands } from "./commands.ts"; import { createPlanPolicy, BASH_BLOCKED_GUIDANCE, BLOCKED_GUIDANCE, PLAN_SAFE_TOOLS } from "./policy.ts"; import { createPlanRuntime } from "./runtime.ts"; import { registerTools } from "./tools.ts"; import { createLazyPlanWebReview } from "./web-lazy.ts"; /** Marker embedded in the injected prompt and used to filter stale context. */ export const PLAN_MODE_MARKER = "[PLAN MODE ACTIVE]"; /** * How the read-only git guard words a refusal, kept as plan mode has always said * it. `lib/git/read-command.ts` is mode-neutral, so its sentences are the * caller's: byte-identical here, and lead mode passes its own. */ const PLAN_PHRASING = { label: "plan mode", whileDoing: "while planning" }; /** * Longest goal text restated here. `goal` injects the full objective every turn * regardless of plan mode, so this line is a short pointer; the clip is what keeps * the whole context inside the prompt-budget ceiling. */ const GOAL_CONTEXT_CHARS = 64; /** * Injected each turn while planning: the facts and the next action, not the tool * contracts. A pursued session goal — being planned or already final — becomes one * extra bullet, so a plan is designed against the objective instead of beside it. */ export function planModeContext(goal?: PublishedGoal | null): string { const lines = [ PLAN_MODE_MARKER, "", `- Do not modify anything except the plan file: while planning, ${READ_ONLY_SUMMARY}.`, "- bash is git-only: read-only git commands chain with && or ; — no pipes, redirection, or substitution, and no powershell. The cwd is already the project root, so skip cd, and read files with read, grep, find, and ls.", "- Investigate and design a concrete plan, then save it with `plan` action `write` and call `plan` action `exit` with the returned plan_path for approval.", "- Delegate broad exploration with subagent.", `- To choose between approaches, ask with ${QUESTION_TOOL} before finalizing.`, ]; if (goal && isPursuedGoal(goal)) { lines.push(`- Current session goal: ${clip(sanitizeLine(goal.objective), GOAL_CONTEXT_CHARS)}`); } return lines.join("\n"); } /** This extension entry file, as Pi records it for our tool registrations. */ function extensionEntryPath(): string | undefined { try { return fileURLToPath(import.meta.url); } catch { return undefined; } } function isPlanModeContext(message: AgentMessage): boolean { // Only the injected message carries this custom type. Matching on the marker // text would also capture (and drop) a real user message that contains it. return (message as AgentMessage & { customType?: string }).customType === "plan-mode-context"; } /** What a caller may substitute for the shipped wiring; production passes nothing. */ export interface PlanDeps { /** * Where a review's prefix comes from: the session's one server, on `serve`. * * A review binds no socket of its own, so a suite substitutes this to see the spec the * review asks with — the session id it carries is the page's home — and answers a * refusal, which stops `start` before it would open a browser. */ mount?: MountSource; } export default function planMode(pi: ExtensionAPI, deps: PlanDeps = {}): void { if (!isExtensionEnabled("plan")) return; const policy = createPlanPolicy(pi); const runtime = createPlanRuntime(pi, policy, { entryPath: extensionEntryPath() }); // The retention rule, loaded by the first request that needs it: a `context` // filter is not a surface a session start should pay for. let retention: Promise | undefined; /** The session this review serves as; its prefix lands on that session's one server. */ let sessionId = ""; const web = createLazyPlanWebReview(runtime.plans, { mount: deps.mount ?? ((spec) => requestMount(pi, spec)), session: () => sessionId, }); registerTools(pi, runtime, web); registerCommands(pi, runtime, web); pi.registerFlag("plan", { description: "Start in plan mode (read-only exploration)", type: "boolean", default: false, }); // Another extension asks on these channels instead of calling a tool it cannot // reach from a command or editor context: `goal` offers plan mode as a phase, and // bytefly's editor binds Shift+Tab to the toggle. The asker owns the user's answer, // so enabling here must not ask again, and a toggle answers with the state it left. const unsubscribes: Array<() => void> = []; const listenForRequests = (): void => { for (const unsubscribe of unsubscribes) unsubscribe(); unsubscribes.length = 0; // The channel's own fact is the context it answers through; the rest of the guard — an object // with a `respond` — is `answerOn`'s, here because four other listeners needed it too. const answers = (request: PlanModeRequest): boolean => request.ctx !== undefined; unsubscribes.push( answerOn( pi, PLAN_ENTER_REQUEST, (request) => { if (!runtime.isEnabled()) runtime.enable(request.ctx); // What the mode is rather than what the call was: a branch whose mode refuses plan // answers `false`, so the asker proceeds without a phase it never entered. return runtime.isEnabled(); }, answers, ), ); unsubscribes.push( answerOn( pi, PLAN_LEAVE_REQUEST, (request) => { // Idempotent, and silent: a door entering another mode needs plan off, not a // notice about a mode it is leaving on its way somewhere else. if (runtime.isEnabled()) runtime.disable(request.ctx); return !runtime.isEnabled(); }, answers, ), ); unsubscribes.push( answerOn( pi, PLAN_TOGGLE_REQUEST, (request) => { runtime.toggle(request.ctx); request.ctx.ui.notify(planModeNotice(runtime.isEnabled()), "info"); return runtime.isEnabled(); }, answers, ), ); }; pi.on("session_start", (_event, ctx) => { // A module moved since this process loaded plan leaves a deferred specifier below // naming a file the tree no longer has: fail here, once, naming it, rather than in // the tool call that would have loaded it. assertDeferredPresent(import.meta.url, ["index.ts"]); // `serve` keys the socket, the remembered port, and the pin by this id, and draws // the port chip in the context it filed with it: a review that asks under another // one gets a port nobody names. sessionId = ctx.sessionManager.getSessionId(); listenForRequests(); runtime.restore(ctx); // The store's bound is applied once per session, in the background: a session that starts // must not wait for a directory it may not need, and a sweep has nothing to say when it // finds the store inside the bound — one `readdir` of the count, then nothing. const cwd = ctx.cwd; void import("./prune.ts").then((prune) => prune.sweepQuietly(pi, cwd)).catch(() => undefined); }); pi.on("session_tree", (_event, ctx) => runtime.restore(ctx)); pi.on("session_shutdown", async (_event, ctx) => { await web.closeAll(); for (const unsubscribe of unsubscribes) unsubscribe(); unsubscribes.length = 0; ctx.ui.setStatus(STATUS_KEYS.planMode, undefined); }); /** * Why a tool call is blocked while planning, or undefined when it may * proceed. Two layers: the policy (readers, `readOnlyHint`, deny list), then * the path backstop (a hint on a path-carrying tool is only a claim). */ const blockedReason = async (toolName: string, input: unknown): Promise => { // Plan's own tool is the sanctioned exception: it is the mode's vocabulary — // `write` saves the artifact, `exit` presents it, `cancel` restores access, and // `enter` answers "already" — so its actions decide for themselves whether they // apply, and each refuses outside its mode. The source-path check keeps a // same-named tool from another extension from borrowing the exemption. if (runtime.isOwnTool(toolName)) return undefined; // `subagent` stays active, but only as read-only delegation. The pin is one body with the // board's gate; plan mode adds the one rule that is its own below. if (toolName === SUBAGENT_TOOL) { const unpinned = pinReadOnlySubagent(pi, input); if (unpinned !== undefined) return `Plan mode: ${unpinned} ${BLOCKED_GUIDANCE}`; if (typeof input === "object" && input !== null) { // A background run outlives the call and is reached only through `/agents`, which // refuses while planning: starting one here would spend a child's budget on a // result nobody can collect until the plan is approved. if ((input as Record).background === true) { return `Plan mode: a background run cannot be inspected or collected while planning; run it in the foreground and wait for it. ${BLOCKED_GUIDANCE}`; } } return undefined; } // `board` stays active so planning can read the board it is about to add cards // to. The action is the truth here, not the annotation: the same tool writes, so // `readOnlyHint` cannot say it, and every verb but the reads are refused by name. if (toolName === BOARD_TOOL) { if (isBoardRead(input)) return undefined; return boardWriteRefusal((input as { action?: unknown } | undefined)?.action, BLOCKED_GUIDANCE); } const blocked = policy.check(toolName); if (blocked) return `Plan mode: ${blocked.reason}`; // `bash` is allowed by the policy so read-only git stays possible; this is // what actually restricts it to chained read-only git commands. The judge is one // body with the board's gate, so the grammar and the write-back stay in one place. // The Windows shell (`powershell`) is not in the policy allow list, so it is // blocked above, not here. if (toolName === BASH_TOOL) { const { readOnlyBashRefusal } = await import("../../lib/git/read-command.ts"); const reason = readOnlyBashRefusal(input, PLAN_PHRASING, BASH_BLOCKED_GUIDANCE); if (reason !== undefined) return `Plan mode: ${reason}`; } const { hasPathInput } = await import("./path-guard.ts"); if (hasPathInput(input) && !PLAN_SAFE_TOOLS.includes(toolName)) { return `Plan mode: "${toolName}" takes a file path and is not a known reader. ${BLOCKED_GUIDANCE}`; } return undefined; }; // Block anything that is not read-only while planning. The active-set gating // above hides most of these; this is the second layer for tools already // declared in an in-flight request, and the guard for tools we did not know // about when the extension loaded (MCP servers, future extensions). pi.on("tool_call", async (event) => { if (!runtime.isEnabled()) return undefined; const reason = await blockedReason(event.toolName, event.input); return reason ? { block: true, reason } : undefined; }); // Tell the model it is planning, and restate an active goal so the plan is // designed against it. Reads the branch, so no extra model call is involved. pi.on("before_agent_start", (_event, ctx) => { if (!runtime.isEnabled()) return undefined; const goal = lastGoal(ctx.sessionManager.getBranch()); return { message: { customType: "plan-mode-context", content: planModeContext(goal), display: false } }; }); // Keep stale plan-mode context out of later, non-plan turns (for example after /resume). pi.on("context", async (event) => { // The mode is read before the deferred load, so the flag describes the request being // filtered. Disabled with none of our own it answers `undefined` where this branch used to // hand back the list unchanged: the host sees the same messages, and the helper's one no-op // rule is what lets the five handlers share one body. const active = runtime.isEnabled(); const { retainNewest } = await (retention ??= import("../../lib/notice.ts")); const kept = retainNewest(event.messages, isPlanModeContext, { active }); return kept ? { messages: kept } : undefined; }); }