Skip to content

feat: Pi-style iterative planning for OpenCode plugin - #318

Merged
backnotprop merged 8 commits into
mainfrom
feat/opencode-better-planning
Mar 16, 2026
Merged

backnotprop merged 8 commits into
mainfrom
feat/opencode-better-planning

Conversation

@backnotprop

@backnotprop backnotprop commented Mar 16, 2026 •

Copy link
Copy Markdown
Owner

Summary

Rewrites the OpenCode plugin's planning experience with file-based plan storage, an exploration-first prompt, and interactive browser review.

What changed

  • File-based plan storage — Plans are written to $XDG_DATA_HOME/opencode/plans/ (defaults to ~/.local/share/opencode/plans/), matching OpenCode's native permission-allowed path. Agent picks the filename, revises the same file on feedback.
  • submit_plan(path) — Takes a file path (not plan text). Validates: absolute path, canonical containment (symlink/traversal protection), file exists, readable, non-empty. Opens browser review UI.
  • Exploration-first prompt — Phased workflow: Explore → Ask → Write → Submit. Agent researches the codebase before creating a plan file. Greenfield tasks can skip to questions.
  • Tool-neutral — No write/edit tool references in the prompt. Works with GPT models using apply_patch.
  • Prompt stripping — Removes OpenCode's native "STRICTLY FORBIDDEN: ANY file edits" and experimental-mode lines, replacing with Plannotator's planning instructions.
  • plan_exit suppressed — tool.definition hook rewrites description to redirect to submit_plan.
  • Subagent control — submit_plan hidden from subagents by default via primary_tools. PLANNOTATOR_ALLOW_SUBAGENTS=1 makes it visible (visibility toggle only — no prompt injection for subagents, see Subagents Can't find submit_plan tool #289).
  • Agent caching — Agents list fetched once per session instead of every LLM call.
  • Checklist utilities — Extracted to @plannotator/shared/checklist for cross-plugin reuse.

How it works

  1. Agent enters plan mode → plugin strips OpenCode's read-only rules, injects exploration-first planning prompt referencing ~/.local/share/opencode/plans/
  2. Agent explores codebase, asks questions if needed, then creates a plan file with a descriptive name
  3. Agent calls submit_plan(path: "...") → plugin validates path, reads content, opens browser UI
  4. User reviews, annotates, approves or denies
  5. Deny → agent revises same file → calls submit_plan again
  6. Approve → agent proceeds to implementation

Test plan

  • bun test apps/opencode-plugin/plan-mode.test.ts — 13 tests pass
  • bun run build:opencode — builds successfully
  • Sandbox test: ./tests/manual/local/sandbox-opencode.sh
    • Plan mode: agent explores first, writes plan to ~/.local/share/opencode/plans/, calls submit_plan — no PermissionDeniedError
    • Deny/revise cycle: agent edits same file, resubmits
    • Approve: agent proceeds to build
    • Code review: /plannotator-review works with sample changes

Closes #289, closes #63, closes #152, closes #129, closes #183

🤖 Generated with Claude Code

backnotprop and others added 3 commits March 16, 2026 11:06
Move ChecklistItem, parseChecklist, extractDoneSteps, and
markCompletedSteps from Pi extension to @plannotator/shared
for reuse by the OpenCode plugin.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Rewrite the OpenCode plugin to match Pi's planning methodology:

- Inject rich iterative planning prompt when agent is "plan"
  (explore → update plan → ask user loop, structured plan format)
- File-based submit_plan: reads plan from disk instead of requiring
  it as a string arg. Resolves path from OpenCode's system prompt,
  falls back to PLAN.md
- Suppress plan_exit via tool.definition hook (directs to submit_plan)
- Add PLANNOTATOR_ALLOW_SUBAGENTS env var for #289
- Pass planFilePath to denial feedback template
- Keep existing subagent/build/title guards intact

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
- Cache agents list (static per session, was fetched every LLM call)
- Remove TOCTOU: drop redundant file.exists() before file.text()
- Eliminate duplicate system.join() by reusing joined string
- Resolve getSharingEnabled() once in submit_plan instead of twice
- Use path.join() instead of string concatenation for file paths

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@backnotprop

Copy link
Copy Markdown
Owner Author

Keep plan_exit compatibility, but remove plan_exit from the normal injected prompt.

Specifically:

  • Remove Do NOT call plan_exit — use submit_plan for plan review. from getPlanningPrompt()
    in apps/opencode-plugin/index.ts.
  • Keep the existing tool.definition override for plan_exit as a compatibility guard. If the
    tool exists in experimental mode, we still redirect it; if it does not exist, the hook is
    inert.
  • Expand stripConflictingPlanModeRules() in apps/opencode-plugin/plan-mode.ts so it strips
    known experimental-mode prompt lines too:
    • your plan at ...
    • plan file already exists at ...
    • .opencode/plans/
    • any line telling the model to call plan_exit
  • Add tests for those stripping cases.

Why this is the right balance:

  • Non-experimental users stop seeing irrelevant plan_exit prompt noise.
  • Experimental users still get protection if plan_exit exists.
  • We avoid needing explicit mode detection.
  • The normal flow stays centered on the session-scoped ~/.plannotator/session-plans/.../
    plan.md file and submit_plan.

So the principle is:

  • Prompt: non-experimental-first, no plan_exit.
  • Hook: compatibility fallback for experimental mode.

backnotprop and others added 2 commits March 16, 2026 13:09
Replace plugin-global resolvedPlanFilePath and prompt-path parsing with
per-session plan files at ~/.plannotator/session-plans/opencode/{id}/plan.md.
Remove resolvePlanFilePath() regex (fixes spaces-in-path and cross-session
race). Make planning prompt tool-neutral (no write/edit references) so it
works with apply_patch models. Strip OpenCode's native read-only and
experimental-mode prompt lines before injecting Plannotator's planning prompt.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Replace session-scoped plan files with a shared plan directory at
~/.plannotator/session-plans/opencode/. Agent picks the filename,
submit_plan takes a path arg and validates it (absolute, inside plan
dir, exists, readable, non-empty) with canonical path checks to
defeat traversal and symlink escapes. Remove summary and plan string
args. Planning prompt is now tool-neutral and directs the agent to
reuse the same file on feedback, not create new ones.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@backnotprop

Copy link
Copy Markdown
Owner Author

All points from this comment are addressed. Here's the full rationale for what changed and why:

plan_exit handling (this comment):

  • Removed plan_exit from the planning prompt — non-experimental users no longer see it
  • tool.definition hook kept as inert compatibility guard for experimental mode
  • stripConflictingPlanModeRules expanded to strip experimental-mode lines (plan_exit, your plan at ..., .opencode/plans/, etc.)

Plan storage (from external review feedback):

  • Original approach: regex-parsed plan path from OpenCode's system prompt + plugin-global resolvedPlanFilePath
  • Problems found: regex broke on paths with spaces, global state raced across concurrent sessions, PLAN.md in repo annoyed users
  • Intermediate fix: per-session files at ~/.plannotator/session-plans/opencode/{sessionID}/plan.md — but sessions can have multiple plans and the session-scoped path was over-structured
  • Final approach: shared directory at ~/.plannotator/session-plans/opencode/, agent picks the filename, submit_plan takes a path arg. No hidden state, no resolution logic.

submit_plan simplification:

  • Removed plan string arg and summary arg — path is the only contract
  • Added 5-step validation: absolute path → canonical containment (defeats .. traversal and symlinks) → file exists → readable → non-empty
  • Each failure returns a specific error message to the agent

Planning prompt:

  • Made tool-neutral: no write/edit references (fixes apply_patch models like GPT)
  • Agent creates one file per plan, reuses the same file on feedback, only creates a new file for genuinely new plans
  • Directs agent to use question tool (not plain text) for asking questions

@backnotprop

Copy link
Copy Markdown
Owner Author

Re: subagent prompt injection concern — PLANNOTATOR_ALLOW_SUBAGENTS=1 intentionally only toggles tool visibility without injecting the planning prompt. Subagents using this are custom workflows with their own instructions. See #289 for context.

backnotprop and others added 3 commits March 16, 2026 16:01
OpenCode's plan mode permission ruleset only allows edits to
.opencode/plans/*.md and $XDG_DATA_HOME/opencode/plans/*.md.
Our custom ~/.plannotator/session-plans/opencode/ path was blocked
by PermissionDeniedError at the tool level regardless of prompt
stripping. Switch to the XDG path that OpenCode already allows.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Restructure the planning prompt as a phased workflow:
Explore → Ask → Write → Submit. The agent now explores the
codebase before creating a plan file or asking questions,
producing better-researched plans for existing codebases.
Greenfield tasks can skip straight to questions.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@rcdailey

Copy link
Copy Markdown
Contributor

For this to work reliably, shouldn't the submit_plan tool be hidden from context? How would OpenCode know not to use this? I'm a little confused about how this all works.

backnotprop added a commit that referenced this pull request Mar 18, 2026
PR #318 replaced the original text-based submit_plan with a file-path-only
version and split prompt injection between plan/non-plan agents. This caused
regressions: the agent couldn't figure out file paths from non-plan agents,
and the 80+ line prompt with TodoWrite replacements was fragile against
OpenCode upstream changes.

This restores the original submit_plan(plan) that accepts markdown text
directly — the contract that worked uniformly across all agents — and keeps
the file-based workflow as submit_plan_file(path) for users who want
persistent plan files.

Key changes:
- Two tools: submit_plan (text) + submit_plan_file (path)
- Unified prompt for all primary agents (not just plan mode)
- Removed aggressive TodoWrite string replacements and system-reminder
- Kept adversarial stripping of OpenCode's STRICTLY FORBIDDEN rules
- Extracted shared server helpers to reduce duplication

Addresses #328

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
backnotprop added a commit that referenced this pull request Mar 18, 2026
* fix: restore uniform submit_plan and add submit_plan_file for OpenCode

PR #318 replaced the original text-based submit_plan with a file-path-only
version and split prompt injection between plan/non-plan agents. This caused
regressions: the agent couldn't figure out file paths from non-plan agents,
and the 80+ line prompt with TodoWrite replacements was fragile against
OpenCode upstream changes.

This restores the original submit_plan(plan) that accepts markdown text
directly — the contract that worked uniformly across all agents — and keeps
the file-based workflow as submit_plan_file(path) for users who want
persistent plan files.

Key changes:
- Two tools: submit_plan (text) + submit_plan_file (path)
- Unified prompt for all primary agents (not just plan mode)
- Removed aggressive TodoWrite string replacements and system-reminder
- Kept adversarial stripping of OpenCode's STRICTLY FORBIDDEN rules
- Extracted shared server helpers to reduce duplication

Addresses #328

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: single submit_plan with auto-detect text/path for OpenCode

Collapses submit_plan + submit_plan_file back into one tool that
auto-detects whether the argument is plan text or a file path.

First submission: agent passes markdown text (simple, works from any agent).
On deny: response includes the history path where the plan was saved, so the
agent can Edit the file for targeted revisions and resubmit with the path.

Key changes:
- One tool: submit_plan(plan) accepts text or absolute .md file path
- Server surfaces historyPath through waitForDecision (already saved by
  saveToHistory, just not returned before)
- Deny response includes file path hint for Edit-based revision workflow
- Unified prompt for all primary agents (~25 lines, no TodoWrite warfare)
- Still strips OpenCode's STRICTLY FORBIDDEN rules and suppresses plan_exit

Addresses #328 and rcdailey's feedback on PR #333

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: remove historyPath leak, error on missing file paths, restore two-tier prompt

- Remove historyPath from deny flow — internal history directory should not
  be exposed to agents. Text submissions get text feedback; file submissions
  get file feedback. No crossover.
- Error when agent passes an absolute .md path that doesn't exist instead of
  silently treating it as plan text.
- Restore two-tier prompt: plan agent gets full planning instructions, other
  primary agents get a minimal reminder (matching pre-v0.13.0 behavior).
- Clean up stale prompt copy referencing historyPath-based revision flow.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

* fix: restore todowrite override, strengthen planning prompt, remove noisy logs

- Override todowrite description to defer to submit_plan during active planning
- Tool description now instructs agent to explore and ask questions before submitting
- Sequenced planning prompt: explore, ask, then write
- Remove success logs from integration saves (stderr was bleeding through)
- Append resubmit reminder to OpenCode deny feedback

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

---------

Co-authored-by: Claude Opus 4.6 (1M context) <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants