Skip to content

fix(vscode): sync mode picker with agent mode changes from backend#8426

Merged
kirillk merged 7 commits into
mainfrom
fix/vscode-mode-picker-sync
Apr 6, 2026
Merged

fix(vscode): sync mode picker with agent mode changes from backend#8426
kirillk merged 7 commits into
mainfrom
fix/vscode-mode-picker-sync

Conversation

@kirillk

@kirillk kirillk commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Problem

When the agent switched modes automatically — for example from plan to code after a plan follow-up — the VS Code mode picker stayed stuck on the old value (e.g. ask). This was confusing because the agent was already operating in a different mode while the UI still showed the previous one.

The root cause: agentSelections in the session store was only written on explicit user picker actions. It was never updated from incoming backend message events.

What this PR fixes

Demo

Screen.Recording.2026-04-06.at.12.02.59.PM.mp4

Every backend agent/mode change manifests as a user message with the agent field set, followed by a message.updated SSE event. The fix intercepts those messages in two places to keep the picker in sync.

Scenario Mechanism Fixed?
User sends a message with a non-default agent createUserMessage() sets agent on user msg → message.updated SSE ✅ via handleMessageCreated
Plan follow-up "Continue here" (plan → code) PlanFollowup.inject() writes synthetic user msg with agent: "code" → SSE ✅ via handleMessageCreated
Plan follow-up custom answer PlanFollowup.inject() writes synthetic user msg with agent: "plan" → SSE ✅ via handleMessageCreated
Shell command shell() creates user msg with agent: input.agent → SSE ✅ via handleMessageCreated
Command execution command()createUserMessage() with resolved agent → SSE ✅ via handleMessageCreated
Session reload / switch Extension calls loadMessages → history batch load ✅ via handleMessagesLoaded
Question option with mode field clicked QuestionDock.pick() detects option.mode ✅ optimistic update via selectAgent()

Changes

  • session-agent.ts — new pure helper resolveSessionAgent(messages, names) that finds the latest valid user-message agent from a message list, ignoring assistant messages and unrecognized names
  • session.tsxhandleMessagesLoaded now calls resolveSessionAgent and writes result to agentSelections; handleMessageCreated updates agentSelections when a user message with a changed agent arrives
  • types/messages.ts — added mode?: string to QuestionOption so the VS Code webview preserves the same question option metadata the backend and shared app already support
  • question-dock-utils.ts — new resolveQuestionMode(options, answer) helper
  • QuestionDock.tsx — calls session.selectAgent(mode) on pick when the selected option carries a mode, giving instant feedback before the next user message arrives

Tests

  • packages/kilo-vscode/tests/unit/session-agent.test.ts — 5 unit tests for the reconciliation helper
  • packages/kilo-vscode/tests/unit/question-dock-utils.test.ts — 3 new cases for resolveQuestionMode
  • packages/kilo-vscode/tests/unit/kilo-provider-utils.test.ts — extended question.asked test to assert option.mode survives SSE mapping

kirillk added 2 commits April 6, 2026 12:07
When the agent switched modes automatically (e.g. plan → code via plan
follow-up), the VS Code mode picker stayed stuck on the previous value.
The root cause was that agentSelections was only written on explicit
user picker actions, not updated from incoming message events.

- Add resolveSessionAgent() helper that derives the active agent from
  the latest valid user message, ignoring assistant messages and unknown
  agent names
- Wire handleMessagesLoaded() to reconcile agentSelections from history
  so the picker is correct when reopening or switching sessions
- Wire handleMessageCreated() to update agentSelections whenever a user
  message arrives with a changed agent field (covers live plan→code,
  shell commands, command execution, and synthetic mode-switch messages)
- Add mode?: string to QuestionOption so the VS Code webview preserves
  the same question option metadata the backend and shared app already
  support
- Wire QuestionDock.pick() to call session.selectAgent() immediately
  when a predefined single-choice option carries a mode field, giving
  instant feedback before the next user message arrives
Comment thread packages/kilo-vscode/webview-ui/src/context/session.tsx
Comment thread packages/kilo-vscode/webview-ui/src/components/chat/QuestionDock.tsx Outdated
@kilo-code-bot

kilo-code-bot Bot commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Code Review Summary

Status: 3 Issues Found | Recommendation: Address before merge

Overview

Severity Count
CRITICAL 0
WARNING 3
SUGGESTION 0

Fix these issues in Kilo Cloud

Issue Details (click to expand)

WARNING

File Line Issue
packages/kilo-vscode/webview-ui/src/context/session.tsx 819 messagesLoaded can still run before agentsLoaded, so the picker misses the restored agent until a later rescan or message arrives.
packages/kilo-vscode/webview-ui/src/components/chat/QuestionDock.tsx 107 The optimistic mode change is only rolled back on questionError, so failed replies can leave the picker and model selection mutated.
packages/kilo-vscode/webview-ui/src/components/chat/QuestionDock.tsx 105 Switching from a mode-carrying option to a plain/custom answer before submit leaves the earlier optimistic mode selected and can send later prompts with the wrong defaults.
Other Observations (not in diff)

No additional observations outside the diff.

Files Reviewed (9 files)
  • packages/kilo-docs/pages/code-with-ai/platforms/vscode/whats-new.md - 0 issues
  • packages/kilo-vscode/tests/unit/kilo-provider-utils.test.ts - 0 issues
  • packages/kilo-vscode/tests/unit/question-dock-utils.test.ts - 0 issues
  • packages/kilo-vscode/tests/unit/session-agent.test.ts - 0 issues
  • packages/kilo-vscode/webview-ui/src/components/chat/QuestionDock.tsx - 2 issues
  • packages/kilo-vscode/webview-ui/src/components/chat/question-dock-utils.ts - 0 issues
  • packages/kilo-vscode/webview-ui/src/context/session-agent.ts - 0 issues
  • packages/kilo-vscode/webview-ui/src/context/session.tsx - 1 issue
  • packages/kilo-vscode/webview-ui/src/types/messages.ts - 0 issues

Reviewed by gpt-5.4-20260305 · 603,405 tokens

Comment thread packages/kilo-vscode/webview-ui/src/components/chat/QuestionDock.tsx Outdated
@kirillk kirillk merged commit d10d25a into main Apr 6, 2026
13 checks passed
@kirillk kirillk deleted the fix/vscode-mode-picker-sync branch April 6, 2026 17:37
jliounis pushed a commit to jliounis/kilocode that referenced this pull request May 18, 2026
…-sync

fix(vscode): sync mode picker with agent mode changes from backend
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.

2 participants