feat: add "Clean context" option to plan mode exit#13971
feat: add "Clean context" option to plan mode exit#13971adynplt wants to merge 3 commits intoanomalyco:devfrom
Conversation
|
The following comment was made by an LLM, it may be inaccurate: |
There was a problem hiding this comment.
Pull request overview
This PR adds a "Clean context" option to plan mode exit, allowing users to start implementing a plan in a new session with a fresh context window, while maintaining parent-child session linkage for traceability.
Changes:
- Adds a third dialog option "Clean context" to the plan_exit tool alongside existing "Yes" and "No" options
- Creates a child session via
Session.create({ parentID })when selected and returns metadata for TUI handling - Extends TUI event handler to detect clean context mode, send initial prompt to new session, and navigate to it
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| packages/opencode/src/tool/plan.ts | Adds "Clean context" option to plan_exit dialog; creates new child session and returns metadata when selected |
| packages/opencode/src/cli/cmd/tui/routes/session/index.tsx | Handles cleanContext metadata in plan_exit event; sends initial prompt to new session and navigates to it |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| sdk.client.session | ||
| .prompt({ | ||
| sessionID: meta.sessionID as string, | ||
| ...selectedModel, | ||
| messageID: Identifier.ascending("message"), | ||
| agent: "build", | ||
| model: selectedModel, | ||
| parts: [ | ||
| { | ||
| id: Identifier.ascending("part"), | ||
| type: "text" as const, | ||
| text: `The plan at ${meta.plan} has been approved, you can now edit files. Execute the plan`, | ||
| }, | ||
| ], | ||
| }) | ||
| .catch(() => {}) | ||
| navigate({ type: "session", sessionID: meta.sessionID as string }) |
There was a problem hiding this comment.
The navigation to the new session occurs immediately after starting the prompt request without waiting for it to complete. This could lead to a race condition where the user navigates to the empty session before the initial prompt is processed. Consider awaiting the prompt call before navigating, similar to how the fork operation works in dialog-fork-from-timeline.tsx (lines 34-56), which awaits the fork before navigating.
| const selectedModel = local.model.current() | ||
| if (selectedModel) { | ||
| local.agent.set("build") | ||
| sdk.client.session | ||
| .prompt({ | ||
| sessionID: meta.sessionID as string, | ||
| ...selectedModel, | ||
| messageID: Identifier.ascending("message"), | ||
| agent: "build", | ||
| model: selectedModel, | ||
| parts: [ | ||
| { | ||
| id: Identifier.ascending("part"), | ||
| type: "text" as const, | ||
| text: `The plan at ${meta.plan} has been approved, you can now edit files. Execute the plan`, | ||
| }, | ||
| ], | ||
| }) | ||
| .catch(() => {}) | ||
| navigate({ type: "session", sessionID: meta.sessionID as string }) | ||
| } | ||
| } else { | ||
| local.agent.set("build") |
There was a problem hiding this comment.
If selectedModel is falsy (no model selected), a new session has already been created server-side (in plan.ts line 47), but the TUI falls through to line 238 without sending a prompt or navigating to it. This orphans the newly created session. Consider either: (1) checking if a model is available before creating the session in plan.ts, or (2) navigating to the new session even without sending an initial prompt, or (3) showing a user-facing error message that model selection is required for clean context mode.
| if (answer === "Clean context") { | ||
| const newSession = await Session.create({ parentID: ctx.sessionID }) | ||
| return { | ||
| title: "Switching to build agent (clean context)", | ||
| output: "User approved switching to build agent with clean context. A new session has been created.", | ||
| metadata: { | ||
| cleanContext: true, | ||
| sessionID: newSession.id, | ||
| plan, | ||
| }, | ||
| } | ||
| } |
There was a problem hiding this comment.
The new "Clean context" option in the plan_exit tool lacks test coverage. While the entire PlanExitTool currently has no tests, other tools in the codebase (like QuestionTool, BashTool, etc.) have comprehensive test coverage. Consider adding tests that verify: (1) the new session is created with the correct parentID, (2) the metadata contains cleanContext, sessionID, and plan fields, and (3) the returned output and title are correct.
0288f94 to
9cf9ae1
Compare
Add a third option to the plan_exit dialog that creates a new session and sends the plan execution prompt to it, giving the build agent a fresh context window free of planning history. - Await prompt before navigating to prevent race condition - Show error toast if no model is selected instead of orphaning session Closes anomalyco#13271
9cf9ae1 to
d0d2fd8
Compare
| const selectedModel = local.model.current() | ||
| if (!selectedModel) { | ||
| toast.show({ variant: "warning", message: "Select a model before switching to clean context", duration: 3000 }) | ||
| } else { |
There was a problem hiding this comment.
Possible to avoid 'else' statements? See https://github.com/anomalyco/opencode/blob/dev/CONTRIBUTING.md#style-preferences
There was a problem hiding this comment.
Fixed — removed else blocks and switched to early returns.
|
This is a very useful feature, I'm really looking forward to it. |
|
I would love this feature |

Summary
plan_exitdialogCloses #13271
Screenshots
Test plan
OPENCODE_EXPERIMENTAL_PLAN_MODE=true)plan_exit