Skip to content

fix(vscode): "Add to Context" silently does nothing in certain conditions - fix #8443#8791

Open
bnaydenov wants to merge 2 commits intoKilo-Org:mainfrom
bnaydenov:fix_8443
Open

fix(vscode): "Add to Context" silently does nothing in certain conditions - fix #8443#8791
bnaydenov wants to merge 2 commits intoKilo-Org:mainfrom
bnaydenov:fix_8443

Conversation

@bnaydenov
Copy link
Copy Markdown

Problem - #8443

The Add to Context right-click command silently did nothing under two distinct conditions that could both occur during a normal editing session.

Issue 1 — Selection cleared before the command handler ran

VS Code can emit programmatic (Command-kind) onDidChangeTextEditorSelection events that reset the cursor position, discarding any active text selection. By the time the addToContext command handler executed, editor.selection was empty, getEditorContext() returned undefined, and the command exited with no feedback to the user.

A secondary bug in the initial fix attempt compounded this: the selection tracker only captured events where kind === Mouse orkind === Keyboard. VS Code frequently emits selection events with kind === undefined (mouse-drag selections being a common case), so the fallback selection was never populated and the recovery path was never reached.

Issue 2 — Messages dropped when the Kilo Code panel was not visible

KiloProvider.postMessage() silently discards messages when this.webview is null. Three variants of this existed:

  • Panel never opened: this.webview was null; messages were dropped immediately.
  • Panel disposed: No onDidDispose listener existed to clear this.webview, leaving a stale reference. Messages were posted into a disposed webview and lost.
  • Panel hidden (retainContextWhenHidden: true): this.webview was valid and isWebviewReady was true, so messages were delivered to the live webview context — but the sidebar was never revealed, making the operation invisible to the user.

Solution

Selection tracking (editor-utils.ts, register-code-actions.ts)

  • Introduced initSelectionTracker(), which subscribes to onDidChangeTextEditorSelection and persists the last non-empty selection keyed by document URI.
  • The capture guard uses kind !== Command rather than kind === Mouse || kind === Keyboard, correctly handling all user-driven events including those where kind is undefined.
  • getEditorContext() falls back to the saved selection when the active editor's selection is empty and the document matches.
  • The tracker is initialised once at registration time inside registerCodeActions().

Pending message queue and webview lifecycle (KiloProvider.ts)

  • Added appendChatBoxMessage() public method backed by a pendingChatBoxMessages queue, following the same pattern as the existing
    appendReviewComments / flushPendingReviewComments implementation.
  • The addToContext command now routes through provider.appendChatBoxMessage() instead of posting directly, with agent-active calls
    continuing to go through the agent path unchanged.
  • onDidDispose listeners added to both WebviewView and WebviewPanel clear this.webview, isWebviewReady, and webviewViewRef on panel close, ensuring the next appendChatBoxMessage call correctly re-focuses and reopens the panel.
  • A webviewViewRef field stores the vscode.WebviewView instance. appendChatBoxMessage inspects webviewViewRef.visible and calls focus to reveal the sidebar when hidden. In tab-panel mode webviewViewRef is always undefined, preventing SidebarProvider.focusfrom being called spuriously and avoiding the regression where a second sidebar would open alongside an existing panel tab.
  • Pending messages are flushed in the webviewReady handler alongside flushPendingReviewComments, guaranteeing delivery regardless of panel load timing.
  • Removed leftover debug console.log statements across KiloProvider.ts.

How to Test

Build extension and test it in the Visual Studio code with this command:

bun run extension

Then select some code in editor, right click, go to "Kilo code" and then choose "Add to context". Selected code should be added to the Kilocode chat window chat window.

how to run unit tests:

cd packages/kilo-vscode
bun test tests/unit/

output 1657 pass
 0 fail
 9074 expect() calls
Ran 1657 tests across 104 files.

- Track last user-initiated selection and use it as a fallback in
getEditorContext() when a programmatic (Command-kind) event clears the
selection before the command handler runs; guard uses kind !== Command
to capture undefined-kind events (e.g. mouse-drag)

  - Add appendChatBoxMessage() with a pending queue so messages survive
the panel being closed, disposed, or hidden; flush on webviewReady

  - Store WebviewView reference to detect hidden-but-alive sidebar and
reveal it via focus; clear refs in onDidDispose listeners on both
WebviewView and WebviewPanel to prevent stale-reference message drops
@bnaydenov
Copy link
Copy Markdown
Author

this PR looks like also fixes this #8592 and #7858

@markijbema markijbema self-assigned this Apr 12, 2026
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