GeezerVison is showing github.com — feat(core): add agent instructions and use them for Plan mode by rekram1-node · Pull Request #63669 · anomalyco/opencode
Conversation
Issue for this PR
Closes #62628. That loop came from Plan re-adding its reminder as a synthetic after each compaction, which woke the session again. This PR removes that code path, and after compaction the rules sit in the system prompt with no new message. The review bot ran the issue's mock-model repro with a 3,000-token context limit: on dev it never exits (1,873 requests in 40s), and with this PR it exits after 3 requests (title, one compaction, the answer).
Related: #62554 (this removes the one-off Plan reminder it lists; the other one-off prompts are unchanged).
The revert case below relies on #63667, which is merged.
Type of change
- Bug fix
- New feature
- Refactor / code improvement
- Documentation
What does this PR do?
Agents get an instructions field: text that's added to the default prompt while the agent is selected, without replacing the prompt like system does. Plan mode now uses it in place of its own reminder machinery.
Today a plugin can't add rules for one agent without trouble. Plan injects "you are in Plan mode" reminders as synthetic messages and repairs them on every request after compaction or a revert (#42652). The internal orchestrator agent adds its rules to the system prompt in a context hook, so every agent switch rewrites the start of the prompt and loses the cache, and the rules aren't stored with the session.
Core already has the right mechanism: instruction sources, which are stored per session, shown once in the system prompt, and sent as an update at the next step when they change. This adds one source, core/agent, built from the selected agent. The field takes two shapes:
// Core writes the framing:
item.instructions = "Always reply in French."
// The plugin writes both texts, sent exactly as written:
item.instructions = { selected: "You are in Plan mode...", unselected: "You are no longer the Plan agent..." }
string { selected, unselected }
first step / switch in "You are the X agent. Follow selected
these instructions while you
are the X agent:" + the text
switch away "You are no longer the X agent. unselected
Stop following the instructions
you were given as the X agent.
Keep following anything the
user asked for."
plugin edits the text "Your instructions as the X selected
agent changed. These replace..."
switch between two one update: the previous agent's leave text + the new agent's text
after compaction the selected agent's text is in the system prompt; no leave text
Compaction, reverts, forks and storage all come from the existing instruction state. The leave text is stored with the value, so it can still be sent after the agent is edited or removed. The source is last in the list because it changes most often.
plan.ts now sets instructions and drops the event subscription, the per-request repair hook and the reminder bookkeeping (about 60 lines). Its permissions and the error rewrite for blocked edits stay.
Why the string form names the agent: a plugin's text often doesn't mention its agent, and then models can't tell which instructions a leave note refers to. I tested this across 12 models, through every way notes reach a model (native system message, developer message, user-turn <system-update>), with a Translator agent whose only rule is "Always reply in French":
- With the raw text and a generic leave note, 59/100 dropped the rule when it was in the system prompt (Claude Haiku 5.5, GPT-6 Luna and Gemini 3.5 Flash Lite 0/10). With the framing above, 97/100.
- The leave note says what still applies. When the user had added their own rule while in the agent ("start every answer with Note:"), "...no longer apply. Do not keep following them." made models drop it too (kept 57/100 and 68/100); this wording keeps it (90/100 and 100/100). A rule from AGENTS.md survived 239/240.
- With no leave note at all, models kept following the old agent's rules about as often as when it was still selected.
What changes for Plan, beyond the plumbing:
- Plan uses the object form with its existing texts. The leave text gains one clause, "You are no longer the Plan agent, so you are NO LONGER in Plan mode...": Plan's rules say they last "until the user switches agents", and without saying the agent changed, Claude Haiku 5.5 left Plan only 6/10 times when the session started in Plan (9/10 with the clause).
- The rules arrive as system content (in the system prompt, or as a system note before the next prompt), not as a user-role synthetic, so the
<system-update> wrapper is gone. In the timeline that shows as the standard "Instructions updated: core/agent" row.
- Switching away and back before the next step now sends nothing. Plan used to send both reminders.
- Sessions that are already in Plan when this ships still have the old reminder in history and get the rules once more as an update. That happens once per session.
The field is plugin-only for now. Agent config files have their own schema, and config already uses instructions for a list of file paths, so exposing this there needs a naming decision first.
How did you verify your code works?
New runner scenarios under agent instructions in session-runner.test.ts use real agents and switchAgent: starting in an agent, switching in, switching to an agent without instructions, switching away and back, switching between two agents, a plugin editing the text, compaction, reverting past the switch in, reverting between switching in and out, and the object form (sent as written, editing only unselected sends nothing, no leave text after compaction). All pass, and the system prompt stays byte-identical across switches.
- Plan against dev's exact delivery (user messages wrapped in
<system-update>), 12 models x 10 runs, rules in the system prompt or arriving mid-session: leaving Plan, dev 240/240 went ahead and this PR 237/240 (single misses on Claude Haiku 5.5, Muse Wonder and DeepSeek Flash). Muse models ran with their own prompt on the route OpenCode uses for them.
- Asked to save the plan while in Plan, models wrote it to the plan directory and never anywhere else.
- Live against a server built from this branch: starting in Plan, the model planned without edits; after switching to build the stored leave note landed before the next prompt and the model made the edit. Switching into Plan mid-session, it refused to implement and pointed to the build agent.
bun run check from the root, and the protocol and client generated-file checks.
Screenshots / recordings
Not a UI change; the timeline row is the existing instruction notice.
Checklist
- I have tested my changes locally
- I have not included unrelated changes in this PR
Added by the opencode bot (AI-generated).
Fixes #62628
Related: #62554
Public API: adds optional instructions (string or { selected, unselected }) to Agent.Info (schema, plugin agent editor, client AgentInfo, OpenAPI); backwards compatible
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works as described. In the TUI with the simulated model, switching build → Plan → build now sends the Plan rules once before the next prompt and the generic "no longer selected" note once after leaving. The system prompt stays byte-identical across switches. On the base, the old <system-reminder> user messages are sent instead. plan.test.ts and session-runner.test.ts pass (267/267), and tsc -b passes for core and its test config.
Public API: adds an optional instructions?: string to Agent.Info. This is in the schema, the plugin agent editor, the generated client AgentInfo and the HTTP agent responses. It is backwards compatible: existing plugins and clients are unaffected, and agent config files don't accept it yet. The Plan reminders change from user-role <system-reminder> synthetics to instruction updates; anything matching the old reminder text will no longer find it.
One thing to fix before merging: packages/protocol/openapi.json wasn't regenerated, so bun run check:generated fails (see inline).
I couldn't run the mock-model repro from #62628 here, because the source CLI's run doesn't start in this sandbox. The context hook that caused that loop is removed by this change, though.
Fixes #62628
Related: #62554


Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new commit regenerates openapi.json, so the stale-spec point is fixed: check:generated now passes.
I also ran the #62628 mock-model repro from source. On the base, opencode run --agent plan with a 3,000-token context limit never exits (1,873 requests in 40s, alternating compaction and answer). With this PR it exits after 3 requests: title, one compaction, the answer.
Public API impact is unchanged since my last review: it adds an optional instructions field to Agent.Info (schema, plugin agent editor, client AgentInfo, OpenAPI spec), and that is backwards compatible. No new concerns.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new push is a rebase onto dev now that #63667 is merged; the PR's own commits are unchanged. The openapi.json point was already fixed by the regenerated spec, and check:generated passes.
On the rebased head I ran the #62628 mock-model repro again: the base still loops between compaction and answer, and this PR exits after 3 requests. Plan and runner tests pass (267/267), and the core typecheck passes.
Public API impact is unchanged: it adds an optional instructions field to Agent.Info (schema, plugin agent editor, client AgentInfo, OpenAPI spec), and that is backwards compatible. No new concerns.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new commit works as described. In a TUI run with the simulated model (build → Plan → build), Plan's rules are sent once before the next prompt and its own "You are NO LONGER in Plan mode…" text once after switching away. The #62628 repro still exits after one compaction. check:generated, the Plan and runner tests (269/269) and the core typecheck all pass.
Public API: Agent.Info.instructions (new in this PR) changes from string to string | { active: string; inactive?: string } in the schema, the plugin agent editor, the client AgentInfo and the OpenAPI spec. That is backwards compatible, since the field hasn't shipped yet. Anything reading the field now has to handle both shapes.
No new concerns.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
swf-cmd
added a commit
to swf-cmd/opencode-upstream
that referenced
this pull request
Nov 24, 2026
dev now fixes two of the cases this branch covered, in its own way:
- anomalyco#65788 adds the goal reminder right after the compaction summary, so
later steps repeat it and the tail breakpoint no longer lands on it.
- anomalyco#63669 moved the Plan mode text into agent instructions and removed
the reminder hook from plan.ts.
The conflicts in goal.ts, plan.ts and plan.test.ts are resolved to dev's
version. goal.test.ts goes back to dev's version too, since this branch's
change there tested the old placement. The request-local flag and its use
for the session.generate and compaction prompts are unchanged.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Nov 26, 2026
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|