Skip to content

feat: add "Clean context" option to plan mode exit#13971

Open
adynplt wants to merge 3 commits intoanomalyco:devfrom
adynplt:feat/plan-exit-clean-context
Open

feat: add "Clean context" option to plan mode exit#13971
adynplt wants to merge 3 commits intoanomalyco:devfrom
adynplt:feat/plan-exit-clean-context

Conversation

@adynplt
Copy link
Copy Markdown

@adynplt adynplt commented Feb 17, 2026

Summary

  • Adds a third option "Clean context" to the plan_exit dialog
  • Creates a new session with empty message history and sends the plan execution prompt there
  • Pre-allows the plans directory so the build agent can read the plan without a permission prompt

Closes #13271

Screenshots

image image

Test plan

  • Enable plan mode (OPENCODE_EXPERIMENTAL_PLAN_MODE=true)
  • Complete a plan and let the agent call plan_exit
  • Verify dialog shows 3 options: Yes, Clean context, No
  • Yes: switches to build in same session (unchanged)
  • Clean context: navigates to a new session with only the plan execution prompt
  • No: stays in plan mode (unchanged)
  • Build agent can read the plan file without a permission prompt
  • If no model is selected, a warning toast appears

Copilot AI review requested due to automatic review settings February 17, 2026 11:51
@github-actions
Copy link
Copy Markdown
Contributor

The following comment was made by an LLM, it may be inaccurate:

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment on lines +219 to +235
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 })
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +216 to +238
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")
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Comment on lines +46 to +57
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,
},
}
}
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
@adynplt adynplt force-pushed the feat/plan-exit-clean-context branch 3 times, most recently from 0288f94 to 9cf9ae1 Compare February 17, 2026 12:17
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
@adynplt adynplt force-pushed the feat/plan-exit-clean-context branch from 9cf9ae1 to d0d2fd8 Compare February 17, 2026 12:18
const selectedModel = local.model.current()
if (!selectedModel) {
toast.show({ variant: "warning", message: "Select a model before switching to clean context", duration: 3000 })
} else {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed — removed else blocks and switched to early returns.

@51HzOuO
Copy link
Copy Markdown

51HzOuO commented Feb 22, 2026

This is a very useful feature, I'm really looking forward to it.

@francisco-revoredo-smec
Copy link
Copy Markdown

I would love this feature

@SunYanbox
Copy link
Copy Markdown

SunYanbox commented Apr 16, 2026

Where are status to be reported?
image
This action didn't work

We need this feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE]: New Plan Mode: Add "Accept plan and clear context" option

6 participants