Skip to content

Conversation

@antsbuildit
Copy link

@antsbuildit antsbuildit commented Mar 11, 2025

Fix mode state sharing when switching between open vscode windows

Context

When using Roo-Code in multiple VS Code windows simultaneously, the "mode" setting (Debug, Architect, Code, etc.) was being shared across all windows. This caused a poor user experience as switching modes in one window would unexpectedly change the mode in all other open windows.. For example one vscode project could be in Architect mode, working away on auto and then, due to another vscode window switching to Code mode, the first also does and it changes it's behavior. Annoying when working on multiple projects concurrently using Auto-approve in particular.

Implementation

The root issue was that VS Code's extension globalState API stores data at the user level rather than per-window. To solve this:

  1. Modified the ContextProxy class to support window-specific state:

    • Added a windowId property that generates a unique identifier for each window based on workspace paths
    • Created a WINDOW_SPECIFIC_KEYS array that defines which settings should be window-specific (currently just 'mode')
    • Updated the state access methods to use namespaced keys for window-specific settings
  2. The implementation is designed to be flexible for future extension:

    • Other settings can easily be made window-specific by adding them to the WINDOW_SPECIFIC_KEYS array
    • The solution requires minimal changes to other parts of the codebase
    • Works in all environments including remote/devcontainers since it uses VS Code's built-in state storage

before - Changing mode in window 1 changes mode in window 2 (and 3 and 4..) when the next message is submitted
after - Each window maintains its own mode independently

How to Test

  1. Open two separate VS Code windows with the Roo-Code extension
  2. In the first window, set the mode to "Debug"
  3. In the second window, set the mode to "Code"
  4. Verify that each window maintains its selected mode independently
  5. Close and reopen the windows to verify that the settings persist correctly

Important

Fixes mode state sharing across VS Code windows by implementing window-specific state management in ContextProxy.

  • Behavior:
    • ContextProxy now supports window-specific state for 'mode' using WINDOW_SPECIFIC_KEYS.
    • Introduces windowId in ContextProxy to uniquely identify VS Code windows.
    • Updates getGlobalState() and updateGlobalState() to handle window-specific keys.
    • resetAllState() now resets window-specific keys using windowId.
  • Implementation:
    • Adds generateWindowId() and createSessionHash() for unique window identification.
    • initializeStateCache() and initializeSecretCache() updated for window-specific handling.
  • Tests:
    • contextProxy.test.ts updated to test window-specific state management.
    • Tests for generateWindowId() and createSessionHash() added to ensure unique ID generation.

This description was created by Ellipsis for b818f6b. It will automatically update as commits are pushed.

@changeset-bot
Copy link

changeset-bot bot commented Mar 11, 2025

⚠️ No Changeset found

Latest commit: 04406a8

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. bug Something isn't working labels Mar 11, 2025
const value = this.stateCache.get(key) as T | undefined
return value !== undefined ? value : (defaultValue as T | undefined)
// Check for window-specific key
if (this.isWindowSpecificKey(key)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Both branches in getGlobalState use identical cache access logic. Consider simplifying to a single branch to improve clarity.

}
}

updateGlobalState<T>(key: string, value: T): Thenable<void> {
Copy link
Contributor

Choose a reason for hiding this comment

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

If the asynchronous update in updateGlobalState fails, the in-memory cache remains updated, potentially causing an inconsistent state. Consider adding error handling to reconcile or revert the cache update on failure.

@antsbuildit antsbuildit marked this pull request as draft March 11, 2025 10:43
Comment on lines 145 to 147
const windowKey = this.getWindowSpecificKey(key)
const value = this.originalContext.globalState.get(windowKey)
this.stateCache.set(key, value)
Copy link
Contributor

Choose a reason for hiding this comment

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

  • This code will make roo save nothing, and make vscode.ExtensionContext wasted
  • can you have reproduce of the bug (if you have video is perfect) when share mode between window ?

Copy link
Contributor

Choose a reason for hiding this comment

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

i think you just skip save and get this.originalContext.globalState when it "this.isWindowSpecificKey"

Copy link
Author

Choose a reason for hiding this comment

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

Hi, thanks for your time on this.

I appreciate your input (I am NOT a TypeScript or vscode plugin developer) so tried to fix this myself and see if it was viable with you guys.

Clearly I've missed some knock on effects here so will investigate and double-check prior to any more.

Also this is my first PR ever (in 25 years of application building - we don't work with github for our data operations) so I realise I should have drafted it first, resolved autobot feedback, re-reviewed and then submitted. I will next time.

As for the video..I honestly cannot believe it. I cannot reliably reproduce it. It has bugged me for weeks and weeks now (literally switch one mode and the other would switch at the next message break/send). My update fixed it for me, stopped immediately for the last couple of days.

I usually have 3-6 windows open, each with different projects, each using Roo-Code and flick each one in and out of devcontainers frequently. Perhaps there is something in the sequence that I am not catching.

I will record it's next occurrence and share but this PR is probably dead without reproducible evidence.

Meanwhile..I have another fix for a task history issue (will check issues first) but clicking recent tasks to restore them deletes them if you create them in/out of a devcontainer and then try to open from the opposite place (out/in). Easily done by accident and trashes them which is shame. May suggest to popup a message but leave the entry in-place, or optionally, configure to store task history on a per project-basis on disk (configurable location). Then you can view, restore, resume, analyse etc.. One to think about.

Cheers,
Iain

Copy link
Collaborator

Choose a reason for hiding this comment

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

@antsbuildit The mode switching state was an issue but we released a fix last Friday (3.8)

@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Mar 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants