Skip to content

Conversation

@roomote
Copy link
Contributor

@roomote roomote bot commented Nov 13, 2025

This PR implements automatic capture of editor selection as hidden context for LLM messages, allowing users to select text and ask questions like "fix this" or "what does this do" without explicitly pasting code.

Changes

Type Definitions

  • Added selection context fields to WebviewMessage and ExtensionMessage types
  • Fields include: selectedText, selectionFilePath, selectionStartLine, selectionEndLine

Frontend (ChatView)

  • Implemented selection capture on component mount via requestSelectionContext message
  • Selection context is automatically included in all outgoing messages
  • State management for current selection context

Backend (Message Handler)

  • Added requestSelectionContext case to capture active editor selection
  • Converts VSCode 0-based line numbers to 1-based for user-friendly display
  • Handles both workspace-relative and absolute file paths
  • Stores selection context in Task instance

Task Management

  • Added selectionContext property to Task class
  • Selection context is stored before message handling in newTask and askResponse cases
  • Proper type guards ensure all required fields are present before assignment

Environment Details

  • Selection context is included at the start of environment details sent to LLM
  • Formatted as code block with file path and line numbers
  • Automatically clears after use to prevent persistence across messages

Testing

  • Added comprehensive tests to existing test suite
  • Tests cover selection capture, clearing, and inclusion in environment details
  • All tests pass successfully

User Experience

Users can now:

  1. Select text in any editor
  2. Type a message like "fix this" or "explain this"
  3. The selection is automatically included as hidden context with file path and line numbers
  4. The LLM receives the complete context without cluttering the chat UI

The feature seamlessly integrates with the existing message flow and follows established patterns in the codebase.


Important

This PR adds automatic capture of editor selection as context for LLM messages, enhancing user interactions by including selected text and file details without manual input.

  • Behavior:
    • Automatically captures editor selection as context for LLM messages, allowing users to ask questions like "fix this" without pasting code.
    • Selection context includes selectedText, selectionFilePath, selectionStartLine, selectionEndLine.
  • Frontend (ChatView):
    • Captures selection on mount via requestSelectionContext message.
    • Includes selection context in all outgoing messages.
    • Manages state for current selection context.
  • Backend (Message Handler):
    • Handles requestSelectionContext to capture active editor selection.
    • Converts 0-based line numbers to 1-based for display.
    • Supports workspace-relative and absolute file paths.
    • Stores selection context in Task instance.
  • Task Management:
    • Adds selectionContext property to Task class.
    • Stores selection context before message handling in newTask and askResponse cases.
    • Ensures all required fields are present before assignment.
  • Environment Details:
    • Includes selection context at the start of environment details sent to LLM.
    • Formats context as code block with file path and line numbers.
    • Clears context after use to prevent persistence across messages.
  • Testing:
    • Adds tests for selection capture, clearing, and inclusion in environment details.
    • All tests pass successfully.

This description was created by Ellipsis for e25c2d6. You can customize this summary. It will automatically update as commits are pushed.

- Add selection context fields to WebviewMessage and ExtensionMessage types
- Implement selection capture in ChatView on component mount
- Add backend message handler to capture editor selection with line numbers
- Store selection context in Task instance for use in environment details
- Include selection context at start of environment details sent to LLM
- Automatically clear selection context after use to prevent persistence
- Add comprehensive tests for selection context functionality
- Convert VSCode 0-based line numbers to 1-based for user-friendly display
- Handle both workspace-relative and absolute file paths

This allows users to select text in the editor and ask questions like
"fix this" or "what does this do" without explicitly pasting code.
The selection is automatically included as hidden context for the LLM.
@roomote roomote bot requested review from cte, jr and mrubens as code owners November 13, 2025 02:27
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Nov 13, 2025
@roomote
Copy link
Contributor Author

roomote bot commented Nov 13, 2025

Rooviewer Clock   See task on Roo Cloud

All previous issues resolved. No new issues found.

  • Stale selection context can be sent with messages due to mount-time capture instead of send-time capture
  • Selection context persists in task instance and can become stale between selection changes
  • Selection context not explicitly cleared when tasks are cleared/aborted, risking context leakage
  • Race condition between requesting fresh selection context and sending message with stale context
Previous reviews

Mention @roomote in a comment to request specific changes to this pull request or fix all unresolved issues.

Comment on lines 839 to 843
useMount(() => {
textAreaRef.current?.focus()
// Request initial selection context when component mounts
vscode.postMessage({ type: "requestSelectionContext" })
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Stale selection context can be sent with messages if the user changes their selection after component mount but before sending a message. The requestSelectionContext call on mount stores the selection in the task, but if the user then selects different text and sends a message, the old selection context will be included. Selection context should be captured at message send time, not at component mount time, to ensure it reflects the user's current selection.

Fix it with Roo Code or mention @roomote and request a fix.

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Nov 13, 2025
Comment on lines 462 to 471
// Store selection context in current task for use in environment details
const currentTask = provider.getCurrentTask()
if (currentTask) {
currentTask.selectionContext = {
selectedText,
selectionFilePath: relativeFilePath,
selectionStartLine: startLine,
selectionEndLine: endLine,
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Selection context stored here persists until explicitly overwritten, which can cause stale context from a previous selection to leak into new messages. If a user selects text, the context is stored in the task, then the user changes their selection or clears it before sending a message, the old selection context will still be sent. The stored context should be cleared when the selection changes or when a message is sent to prevent stale data from being included in subsequent messages.

Fix it with Roo Code or mention @roomote and request a fix.

Comment on lines 158 to 163
selectionContext?: {
selectedText: string
selectionFilePath: string
selectionStartLine: number
selectionEndLine: number
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Selection context from a previous task can leak into a new task if the user starts a new task without changing their editor selection. When clearTask is called (which eventually creates a new task), the selection context stored in the old task instance is not explicitly cleared. If the same selection remains active in the editor, the requestSelectionContext call on mount will capture and store it again, but there's a brief window where the old context could be used. The selection context should be explicitly cleared when clearing/aborting a task to prevent any possibility of context leakage between tasks.

Fix it with Roo Code or mention @roomote and request a fix.

@roomote
Copy link
Contributor Author

roomote bot commented Nov 13, 2025

Fixaroo Clock   See task on Roo Cloud

Fixed the reported issues. All local checks passed.

View commit | Revert commit

- Move selection context request from mount-time to send-time in ChatView
- Remove storage of selection context in Task instance to prevent staleness
- Pass selection context through message flow as ephemeral parameter
- Add getAndClearSelectionContext() method to Task for one-time use
- Update tests to reflect new architecture with mocked getter method
- Add selectionContext to CreateTaskOptions interface

This ensures selection context is always fresh when sent and automatically
cleared after use, preventing context leakage between messages.
Comment on lines 571 to 580
// Request fresh selection context before sending
vscode.postMessage({ type: "requestSelectionContext" })

if (messagesRef.current.length === 0) {
vscode.postMessage({ type: "newTask", text, images })
vscode.postMessage({
type: "newTask",
text,
images,
...(selectionContext || {}),
})
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Race condition between requesting selection context and sending the message. The code calls vscode.postMessage({ type: "requestSelectionContext" }) on line 572, then immediately sends the message on line 576 using ...(selectionContext || {}). Since requestSelectionContext is asynchronous and updates state via the message handler (lines 767-775), the message will be sent with stale selectionContext state before the fresh context arrives. This defeats the purpose of requesting fresh context at send-time.

Fix it with Roo Code or mention @roomote and request a fix.

@mrubens
Copy link
Collaborator

mrubens commented Nov 13, 2025

@roomote why does the webview need to get the selection context? Can't the extension just get it when it needs it?

@roomote
Copy link
Contributor Author

roomote bot commented Nov 13, 2025

Fixaroo Clock   See task on Roo Cloud

Refactored selection capture to be handled directly by the extension instead of the webview. All local checks passed.

View commit | Revert commit

- Remove selection context state management from ChatView
- Remove requestSelectionContext message type and handler
- Extension now captures selection directly in newTask and askResponse handlers
- Eliminates race condition between selection request and message send
- Simplifies architecture by removing unnecessary webview complexity
Comment on lines 95 to 100
selectionContext?: {
selectedText: string
selectionFilePath: string
selectionStartLine: number
selectionEndLine: number
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

@roomote this is repeated a lot - should we move it into a type?

@roomote
Copy link
Contributor Author

roomote bot commented Nov 13, 2025

Fixaroo Clock   Follow along on Roo Cloud

Working on extracting the repeated selection context type definition into a shared type.

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

Labels

enhancement New feature or request Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: Triage

Development

Successfully merging this pull request may close these issues.

4 participants