fix(vscode): "Add to Context" silently does nothing in certain conditions - fix #8443#8791
Open
bnaydenov wants to merge 2 commits intoKilo-Org:mainfrom
Open
fix(vscode): "Add to Context" silently does nothing in certain conditions - fix #8443#8791bnaydenov wants to merge 2 commits intoKilo-Org:mainfrom
bnaydenov wants to merge 2 commits intoKilo-Org:mainfrom
Conversation
- 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
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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)
onDidChangeTextEditorSelectionevents that reset the cursor position, discarding any active text selection. By the time theaddToContextcommand 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 withkind === 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 whenthis.webviewis null. Three variants of this existed:this.webviewwas null; messages were dropped immediately.onDidDisposelistener existed to clearthis.webview, leaving a stale reference. Messages were posted into a disposed webview and lost.retainContextWhenHidden: true):this.webviewwas valid andisWebviewReadywas 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)
Pending message queue and webview lifecycle (KiloProvider.ts)
appendChatBoxMessage()public method backed by a pendingChatBoxMessages queue, following the same pattern as the existingappendReviewComments / flushPendingReviewComments implementation.
continuing to go through the agent path unchanged.
onDidDisposelisteners added to bothWebviewViewandWebviewPanelclearthis.webview,isWebviewReady, andwebviewViewRefon panel close, ensuring the nextappendChatBoxMessagecall correctly re-focuses and reopens the panel.webviewViewReffield stores thevscode.WebviewViewinstance. appendChatBoxMessageinspectswebviewViewRef.visibleand calls focus to reveal the sidebar when hidden. In tab-panel modewebviewViewRefis always undefined, preventingSidebarProvider.focusfrombeing called spuriously and avoiding the regression where a second sidebar would open alongside an existing panel tab.webviewReadyhandler alongsideflushPendingReviewComments, guaranteeing delivery regardless of panel load timing.KiloProvider.ts.How to Test
Build extension and test it in the Visual Studio code with this command:
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: