Skip to content

feat: add plan mode with restricted tool boundary - #24236

Merged
ibetitsmike merged 28 commits into
mainfrom
mike/plan-mode-turn-scoped
Apr 16, 2026
Merged

ibetitsmike merged 28 commits into
mainfrom
mike/plan-mode-turn-scoped

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Apr 10, 2026 •

Copy link
Copy Markdown
Collaborator

This PR was authored by Mux on behalf of Mike.

Summary

  • add persistent plan mode for chats and the chat-specific plan file flow
  • add structured planning tools such as ask_user_question and propose_plan
  • keep write_file and edit_files constrained to the chat-specific plan file during plan turns
  • allow shell exploration in plan mode, including subagents, via execute and process_output
  • block implementation-oriented, provider-native, MCP, dynamic, and computer-use tools during plan turns
  • update the chat UI, tests, and docs for the new planning flow

@ibetitsmike
ibetitsmike force-pushed the mike/plan-mode-turn-scoped branch from c4bb5c6 to 485e020 Compare April 10, 2026 14:25
@ibetitsmike ibetitsmike changed the title feat(coderd/x/chatd): add turn-scoped plan mode feat: add turn-scoped plan mode for chat Apr 10, 2026
@ibetitsmike
ibetitsmike force-pushed the mike/plan-mode-turn-scoped branch 5 times, most recently from 6b8948f to b069f01 Compare April 13, 2026 20:21
@ibetitsmike ibetitsmike changed the title feat: add turn-scoped plan mode for chat feat(coderd/x/chatd): add plan mode with restricted tool boundary Apr 14, 2026
@ibetitsmike
ibetitsmike force-pushed the mike/plan-mode-turn-scoped branch from f2311be to 2a723e2 Compare April 15, 2026 12:42
@ibetitsmike ibetitsmike changed the title feat(coderd/x/chatd): add plan mode with restricted tool boundary feat: add plan mode with restricted tool boundary Apr 15, 2026
@ibetitsmike
ibetitsmike force-pushed the mike/plan-mode-turn-scoped branch 4 times, most recently from 8918ef3 to 008233e Compare April 15, 2026 14:19
@ibetitsmike
ibetitsmike marked this pull request as ready for review April 15, 2026 14:24
@coderagents

coderagents Bot commented Apr 15, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Check

Updates Needed

  • docs/ai-coder/agents/index.md — Add ask_user_question to the built-in tools table. (addressed in cdf5055)
  • docs/ai-coder/agents/architecture.md — Add ask_user_question to the platform tools table. (addressed in cdf5055)
    • Clarify that propose_plan and ask_user_question are only available when plan mode is active. (addressed in de66812)
  • docs/ai-coder/agents/platform-controls/index.md — Document the new Plan mode instructions admin-only setting. (addressed in de66812)

New Documentation Needed

  • docs/ai-coder/agents/index.md — Explain the plan mode feature for end users: what it is, how to toggle it, what it restricts, and how it persists. (addressed in de66812)

Automated review via Coder Tasks

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 008233e2e3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/subagent.go Resolved
Comment thread coderd/x/chatd/chatd.go Resolved
@ibetitsmike
ibetitsmike force-pushed the mike/plan-mode-turn-scoped branch 2 times, most recently from d6c0bf1 to 8203c24 Compare April 15, 2026 15:50
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Mux working on behalf of Mike.

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 8203c24dcd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/chatd.go Resolved
Comment thread coderd/x/chatd/subagent.go Resolved
Comment thread coderd/exp_chats.go Resolved
@ibetitsmike
ibetitsmike force-pushed the mike/plan-mode-turn-scoped branch from 8203c24 to f8d521d Compare April 15, 2026 16:08
Adds a chat-level plan mode that restricts the LLM to read-only
investigation and a single writable plan artifact. The mode is stored
on the chats table and persists across page reloads and turns.

Backend:
- Database migration 000469 adds chat_plan_mode enum and plan_mode column
- Plan mode gates tools via allowedPlanToolNames() and stopAfterPlanTools()
- PlanningOverlayPrompt with dynamic plan file path via ResolvePlanPath
- Tool restrictions: write_file/edit_files limited to plan path
- ask_user_question tool for structured clarification during planning
- Chat PATCH API supports plan_mode field for toggling
- Moved propose_plan references out of default prompt into plan-mode overlay
- Plan turns stop after both propose_plan and ask_user_question

Frontend:
- Plan mode toggle in AgentChatPage from chat.plan_mode query state
- updateChatPlanMode mutation with optimistic cache updates
- AskUserQuestionTool wizard UI with radio options and keyboard support
- ConversationTimeline reconstructs answered-question views
- PlanModeInstructionsSettings for admin deployment-wide instructions
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 15, 2026
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Mux working on behalf of Mike.

@codex review

@ibetitsmike ibetitsmike reopened this Apr 15, 2026
@coder coder unlocked this conversation Apr 15, 2026
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: de2c024435

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/chatd.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Mux is working on behalf of Mike.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Mux requested this follow-up Codex review on Mike's behalf after fixing the latest dynamic tool regression.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c0ab8353d7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Mux requested this follow-up Codex review on Mike's behalf after preserving the composer on chat-setting sync failures.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Mux working on behalf of Mike.

@codex review

Latest push relaxes plan mode so root chats and subagents can use execute and process_output for exploration, while write_file and edit_files stay limited to the chat-specific plan file.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

Mux working on behalf of Mike.

Please review the latest push.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

Mux working on behalf of Mike.

Requesting a fresh exact Codex trigger because the previous comments included extra text and may not have matched the repository's expected trigger format.

@ibetitsmike

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: e736ab6a24

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/chatd.go Resolved
@ibetitsmike
ibetitsmike merged commit 1cf0354 into main Apr 16, 2026
34 of 36 checks passed
@ibetitsmike
ibetitsmike deleted the mike/plan-mode-turn-scoped branch April 16, 2026 09:12
@github-actions github-actions Bot locked and limited conversation to collaborators Apr 16, 2026
@coder coder unlocked this conversation Apr 16, 2026
};

const DEFAULT_CHAT_PAGE_LIMIT = 50;
const nilUUID = "00000000-0000-0000-0000-000000000000";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

i think this is probably a bad use of nilUUID const re-defining

planMode?: ChatPlanModeOrClear;
};

const clearPlanMode = "" satisfies ChatPlanModeOrClear;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this feels wrong, why is clear plan mode an empty string?

Comment on lines +1110 to +1145
let latestAskUserQuestionToolId: string | undefined;
let hasUserResponseAfterAskQuestion = false;
const askUserQuestionResponseTextByToolId = new Map<string, string>();
let pendingAskUserQuestionToolId: string | undefined;
for (const { message, parsed } of parsedMessages) {
let askUserQuestionToolIdInMessage: string | undefined;
for (const tool of parsed.tools) {
if (tool.name === "ask_user_question") {
askUserQuestionToolIdInMessage = tool.id;
latestAskUserQuestionToolId = tool.id;
hasUserResponseAfterAskQuestion = false;
}
}

if (askUserQuestionToolIdInMessage) {
pendingAskUserQuestionToolId = askUserQuestionToolIdInMessage;
}

if (pendingAskUserQuestionToolId && message.role === "user") {
hasUserResponseAfterAskQuestion =
pendingAskUserQuestionToolId === latestAskUserQuestionToolId;
const responseText = getChatMessageTextContent(message.content);
if (responseText !== undefined) {
askUserQuestionResponseTextByToolId.set(
pendingAskUserQuestionToolId,
responseText,
);
}
pendingAskUserQuestionToolId = undefined;
}
}
const historicalAskUserQuestionResponseTextByToolId =
askUserQuestionResponseTextByToolId.size > 0
? askUserQuestionResponseTextByToolId
: undefined;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We're massively bloating the complexity of this component. This can cause React Compiler to bail out. Please move some/most of this out into a helper.

Comment on lines +59 to +75
const cloneAnswer = (
answer: QuestionAnswer | undefined,
): QuestionAnswer | undefined => {
if (!answer) {
return undefined;
}

if (answer.kind === "other") {
return { kind: "other", text: answer.text };
}

return {
kind: "option",
label: answer.label,
optionIndex: answer.optionIndex,
};
};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

.join("\n");
};

export const AskUserQuestionTool: FC<AskUserQuestionToolProps> = ({

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this entire component is a bit of a mess.

  1. We should be using formik
  2. We're manually adding setIsSubmitting. Again, formik and mutation helpers help here
  3. We have a map call where the content should absolutely be pulled out into a separate component.
  4. There are more issues but I'd be here all day so pls get your agent to do this properly

Comment on lines +70 to +81
const handleImplementPlanClick = async () => {
if (!onImplementPlan || isSubmitting) {
return;
}
setIsSubmitting(true);
try {
await onImplementPlan();
setIsSubmitting(false);
} catch {
setIsSubmitting(false);
}
};

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

setIsSubmitting no. Proper useMutation work should avoid this.

Your agent should look at how we handle this elsewhere, because I think this is probably wrong.

Comment on lines +174 to +175
const toolBadgeClassName =
"inline-flex shrink-0 items-center gap-1 rounded-full bg-surface-secondary px-2 py-0.5 text-xs font-medium text-content-secondary";

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We should never be promoting class variable names up like this. Inline this where it is used.

const planModeInvisibleCharCount = countInvisibleCharacters(
form.values.plan_mode_instructions,
);
const planModeInvisibleCharWarning = `This text contains ${planModeInvisibleCharCount} invisible Unicode ${planModeInvisibleCharCount !== 1 ? "characters" : "character"} that could hide content. These will be stripped on save.`;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why is this a variable? just inline it

ibetitsmike added a commit that referenced this pull request Apr 16, 2026
> This PR was authored by Mux on behalf of Mike.

Address all 8 frontend review comments from DanielleMaywood on #24236.

## Changes

**Low-risk cleanups:**
- Inlined module-level `toolBadgeClassName` in `AgentChatInput.tsx`
- Inlined `planModeInvisibleCharWarning` variable in
`PlanModeInstructionsSettings.tsx`
- Replaced loose `nilUUID` and `clearPlanMode` sentinels in `chats.ts`
with a `toChatPlanModePayload()` helper and inline nil UUID. Page-level
mutation callers now pass `undefined` to clear plan mode instead of
`""`.

**Tool state management:**
- Replaced manual `isSubmitting`/`setIsSubmitting` in
`ProposePlanTool.tsx` with `useMutation`
- Overhauled `AskUserQuestionTool.tsx`: replaced `cloneAnswer` with
`structuredClone`, replaced `setIsSubmitting` with `useMutation`,
extracted `QuestionStep`, `QuestionOption`, `OtherQuestionOption`, and
`AnsweredQuestionText` components

**Timeline complexity reduction:**
- Extracted `deriveMessageDisplayState()` into `messageHelpers.ts`
(pure, testable)
- Extracted user-message rendering into `UserMessageContent.tsx`
- Reduced `ConversationTimeline.tsx` by ~285 lines

## Validation
- TypeScript: pass
- Lint (Biome + ESLint): pass
- React Compiler: 247 functions, 0 diagnostics
- Storybook tests: 42/42 pass (ProposePlanTool 9, ConversationTimeline
22, AskUserQuestionTool 11)
ethanndickson added a commit that referenced this pull request May 19, 2026
`attach_file` was registered for plan-mode turns but never added to
`builtinPlanToolAllowed`, so the per-turn `ActiveTools` allowlist
filtered it out and calls failed with `Tool not active in this turn:
attach_file`. This was an omission rather than a deliberate block — the
tool (#24280) landed shortly after plan mode (#24236) and no subsequent
edit to the allowlist picked it up.

Add `attach_file` under the `isRootChat` case, matching how other
artifact-producing tools (`propose_plan`, `write_file`, `edit_files`)
are gated. The tool only reads from the workspace and writes to
chat-attachment storage, so it preserves plan mode's invariant of not
making implementation changes to the workspace. Subagents in plan mode
remain restricted to the minimal read-only surface.
ibetitsmike added a commit that referenced this pull request Oct 27, 2026
…be resolved (#30983)

Removes the 404 bypass from the plan-turn symlink guard in `write_file`
and `edit_files`. `ensurePlanPathResolvesToItself` treated a 404 from
the agent's `resolve-path` endpoint as success, so a plan turn on an
agent without that endpoint wrote the plan file with no symlink check.
Now every `ResolvePath` error fails the tool call. The PR deletes the
status predicate and its `net/http` import, the two tests that expected
a write after a 404, and the test-only `statusError` file. The PR adds
77 lines and removes 147.

**Behavior change, approved by Mike:** an agent too old to serve the
`resolve-path` endpoint can no longer write plan files during plan
turns. The tool returns `resolve plan path: ...` instead. The endpoint
arrived with plan mode in #24236. Coder does not support mixed coderd
and agent versions, and upgrades happen in maintenance windows.

**Why it is safe:** the agent's `resolve-path` handler never answers
404. It returns 400 for a relative path, 403 for a permission error, and
500 for other errors, and it resolves a path whose last components do
not exist yet, such as a new plan file. So a 404 means that the route is
missing.

- In each tool's tests, the symlink case is now a table with three more
rows: a 404 and a 500 built with `codersdk.NewError`, and a connection
error without a status code. No row expects a write, and the strict
gomock controller fails on any `WriteFile` or `EditFiles` call.
- With the old resolver (`go test -overlay`), the 404 row fails for both
tools.
- A mutant that ignores every `ResolvePath` error fails all three error
rows for both tools. On `main`, the whole `chattool` package passes with
that mutant.
- A mutant that ignores only errors without a status code, such as a
dropped connection, fails the connection error row for both tools.
Without that row, both tools' tests pass with it.
- The success, shared plan path, and mixed-path tests do not change and
pass.

**Review focus:** the behavior change. Only `write_file` and
`edit_files` call the guard. `propose_plan` does not call `ResolvePath`,
and this PR does not change it.

Validation: `go test ./coderd/x/chatd/chattool -count=1` passes, `go vet
./coderd/x/chatd/chattool` is clean, and the pre-commit hook passed.

> Xum, an AI agent, opened this PR for @ibetitsmike and wrote this
description.
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.

4 participants