commit 711abdac783c18810b9ad32327b31eaa5f02fa7f Author: Alexander Kucera Date: Sun Nov 15 19:49:39 2026 +0100 feat: finish-audit-goal extension with optional Goal mode Pi extension command shadowing the finish_audit prompt template. /finish_audit runs the one-shot reviewer fanout audit; /finish_audit goal starts pi-goal via its managed-run RPC so the audit repeats until reviews come back clean. diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..646ac51 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.DS_Store +node_modules/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..20098c6 --- /dev/null +++ b/README.md @@ -0,0 +1,44 @@ +# Finish Audit Goal + +**A [Pi](https://github.com/earendil-works/pi) extension command that runs the finish-audit reviewer fanout — optionally on the [`pi-goal`](https://www.npmjs.com/package/@narumitw/pi-goal) continuation loop, so the audit repeats itself until the reviews come back clean.** + +## Why + +Pi's prompt templates and extension commands live in separate worlds. When you type a slash command, Pi dispatches *extension* commands first, then expands *prompt* templates — and the expanded template body is sent to the model as plain text. A `/goal ...` line inside a template body is never dispatched; Goal mode silently never activates. And the reverse nesting (`/goal` with a whole procedure as the objective) fights pi-goal's 4,000-character objective limit. + +This extension bridges the two: it shadows the `finish_audit` prompt template with a command of the same name (extension commands win the dispatch race) and offers both modes behind one keystroke. + +## Usage + +| Command | Mode | Behavior | +|---|---|---| +| `/finish_audit` | One-shot | Sends the procedure body from `finish_audit.md` as a user message — the same behavior as the bare template. | +| `/finish_audit goal` | Goal mode | Starts pi-goal with an objective pointing at the procedure file. Goal mode's continuation loop carries the "repeat until reviews find no more issues" contract across turns automatically. | + +Any other argument is rejected with a usage notification. Argument autocomplete offers `goal`. + +## How it works + +- **Goal mode** is started through pi-goal's managed-run RPC: `pi.events.emit("pi-goal:start", { runId, objective })` on Pi's shared extension event bus. Start/error events are surfaced as terminal notifications. +- **One-shot mode** reads the procedure file, strips its YAML frontmatter, and sends the body via `pi.sendUserMessage(body, { deliverAs: "followUp", expandPromptTemplates: false })`. The `expandPromptTemplates: false` flag is load-bearing — without it the sent text would re-dispatch as a command and recurse. + +## Requirements + +- The [`@narumitw/pi-goal`](https://www.npmjs.com/package/@narumitw/pi-goal) extension, and **Managed run RPC enabled** in its settings (`rpc.enabled: true` in `~/.pi/agent/pi-goal.json`, or `/goal` → settings). +- The audit procedure at `~/.pi/agent/prompts/finish_audit.md` (fresh-context reviewer fanout: `ponytail-review`, `security-review`, `review`). Edit `PROCEDURE_PATH` in the source if yours lives elsewhere. + +## Install + +Symlink the extension into Pi's user extension directory and reload: + +```bash +ln -s ~/Projects/scripting/pi-extensions/finish-audit-goal/finish-audit-goal.ts ~/.pi/agent/extensions/finish-audit-goal.ts +``` + +Then `/reload` in an active session. + +## Limitations + +- If a goal is already active, pi-goal's RPC refuses with `GOAL_ALREADY_EXISTS` (shown as an error notification). `/goal clear` first, or replace it from the `/goal` menu. +- The `pi-goal:start` channel is a third-party extension's protocol; a future pi-goal update could change it. Start failures surface as notifications rather than silent no-ops. +[Generated by AI] diff --git a/finish-audit-goal.ts b/finish-audit-goal.ts new file mode 100644 index 0000000..49c0377 --- /dev/null +++ b/finish-audit-goal.ts @@ -0,0 +1,74 @@ +import { readFileSync } from "node:fs"; +import type { ExtensionAPI } from "@earendil-works/pi-coding-agent"; + +const PROCEDURE_PATH = "/Users/alex/.pi/agent/prompts/finish_audit.md"; + +const OBJECTIVE = `Audit the current session's changes by following the procedure in ${PROCEDURE_PATH}: launch the fresh-context reviewer fanout it describes, synthesize the three reports, fix the accepted findings, and repeat the fanout until the reviews find no more issues.`; + +/** Strip YAML frontmatter from a template file, returning the body. */ +function templateBody(path: string): string { + const raw = readFileSync(path, "utf8"); + const match = raw.match(/^---\r?\n[\s\S]*?\r?\n---\r?\n?/); + return (match ? raw.slice(match[0].length) : raw).trim(); +} + +/** + * /finish_audit [goal] + * + * - /finish_audit plain one-shot audit: sends the finish_audit.md + * template body as a user message (same behavior as + * the template alone). + * - /finish_audit goal starts Goal mode via the pi-goal run RPC + * (requires rpc.enabled in pi-goal.json) and runs the + * same procedure on the goal continuation loop. + * + * Extension commands dispatch before prompt templates, so this command + * shadows the finish_audit.md template of the same name. + */ +export default function finishAuditGoalExtension(pi: ExtensionAPI) { + pi.registerCommand("finish_audit", { + description: "Audit the session changes; add 'goal' to run with Goal mode", + getArgumentCompletions: (prefix: string) => + "goal".startsWith(prefix.trim()) ? [{ value: "goal", label: "goal — run with Goal mode" }] : [], + handler: async (args, ctx) => { + const mode = args.trim().toLowerCase(); + + if (mode === "goal") { + const runId = `finish-audit-${Date.now()}`; + const channel = `pi-goal:event:${runId}`; + + const unsubscribe = pi.events.on(channel, (data: unknown) => { + const event = data as { type?: string; status?: string; error?: { message?: string } }; + try { + if (event?.type === "error") { + ctx.ui.notify(`finish_audit goal failed: ${event.error?.message ?? "unknown error"}`, "error"); + unsubscribe(); + } else if (event?.type === "state" && event.status) { + ctx.ui.notify(`Goal mode started: finish-audit loop (${event.status})`, "info"); + unsubscribe(); + } + } catch { + // Session was replaced or reloaded; the stale ctx is not an error. + unsubscribe(); + } + }); + + pi.events.emit("pi-goal:start", { runId, objective: OBJECTIVE }); + return; + } + + if (mode !== "") { + ctx.ui.notify(`Unknown argument '${args.trim()}'. Usage: /finish_audit [goal]`, "error"); + return; + } + + // Plain one-shot: send the procedure body as a user message. + // expandPromptTemplates stays false so the body is never re-dispatched + // as a command (no recursion on /finish_audit). + pi.sendUserMessage(templateBody(PROCEDURE_PATH), { + deliverAs: "followUp", + expandPromptTemplates: false, + }); + }, + }); +}