Skip to content

Conversation

@daniel-lxs
Copy link
Member

@daniel-lxs daniel-lxs commented Nov 3, 2025

Related GitHub Issue

Supersedes PR #8521 by @heyseth (Seth Miller) with additional enhancement.

Description

This PR implements automatic switching to newly imported modes with an improved fallback mechanism.

Original work by @heyseth (Seth Miller):

  • Auto-switch to imported mode after successful import
  • Backend returns imported mode slug in importModeWithRules()
  • UI switches to imported mode when found in customModes list
  • Comprehensive test coverage for auto-switch functionality

Additional enhancement:

  • Use architect mode as fallback when imported slug is not yet present in customModes state
  • Updated test to expect architect mode in fallback scenario
  • Prevents UI desync during state refresh race conditions

Implementation Details

Backend Changes:

  • CustomModesManager.ts: Returns imported mode slug for UI auto-switch
  • webviewMessageHandler.ts: Passes slug to UI on successful import

UI Changes:

  • ModesView.tsx: Auto-switches to imported mode when found, falls back to architect mode when slug not yet in state
  • ModesView.import-switch.spec.tsx: Tests cover success, fallback, and failure scenarios

Test Procedure

Automated Tests

  • New test suite covers auto-switch functionality including architect fallback
  • All existing tests pass

Manual Testing Steps

Same as original PR #8521:

  1. Navigate to Custom Modes settings
  2. Import a valid mode file
  3. Verify: UI automatically switches to imported mode
  4. Edge case: If timing race occurs, UI falls back to architect mode instead of inconsistent state

Co-authored-by

Co-authored-by: Seth Miller [email protected]


Important

This PR adds auto-switching to imported modes with a fallback to architect mode, including backend, UI changes, and tests.

  • Behavior:
    • Auto-switch to imported mode after successful import; falls back to architect mode if not in state.
    • Handles import failures and cancellations without switching modes.
  • Backend Changes:
    • CustomModesManager.ts: Returns imported mode slug in importModeWithRules().
    • webviewMessageHandler.ts: Sends imported mode slug to UI.
  • UI Changes:
    • ModesView.tsx: Switches to imported mode or falls back to architect mode.
    • Handles import success, failure, and cancellation scenarios.
  • Tests:
    • ModesView.import-switch.spec.tsx: Tests for auto-switch, fallback, and failure scenarios.

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

heyseth and others added 10 commits October 5, 2025 13:59
Fixes [#8239](#8239)

## Summary
Automatically switches to the imported mode after a successful mode import,
providing immediate feedback and eliminating the need for manual mode selection.

## Changes

### Backend (CustomModesManager.ts)
- Modified `importModeWithRules()` to return the imported mode's slug in the result
- Returns `{ success: true, slug: importData.customModes[0]?.slug }` on success
- Enables the UI to know which mode was imported for automatic switching

### Message Handler (webviewMessageHandler.ts)
- Updated `importMode` case to handle the new slug return value
- Passes the slug to the webview via `importModeResult` message
- Maintains backward compatibility with existing error handling

### UI (ModesView.tsx)
- Added auto-switch logic in `importModeResult` message handler
- Attempts to find imported mode in fresh customModes list
- Falls back to direct slug-based switch if mode not yet in list
- Updates visual mode state for immediate UI feedback
- Only switches on success with a valid slug provided

### Tests (ModesView.import-switch.spec.tsx)
- Added new test suite for import auto-switch behavior
- Tests successful switch when slug is provided
- Tests no-switch behavior on import failure or missing slug
- Verifies both backend message and UI state updates

## Testing
- New test coverage added for auto-switch scenarios
- Existing import/export functionality remains unchanged
- Works with both global and project-level imports
…tener

Use a stable ref to hold the latest switchMode and avoid capturing it in the window "message" listener closure:
Add a switchModeRef updated via useEffect; call switchModeRef.current in the importModeResult fallback instead of switchMode
Mirrors existing handleModeSwitchRef/customModesRef pattern to keep handler stable while accessing fresh values
Prevents re-registration churn and removes the ESLint warning with no runtime behavior change

File touched:
webview-ui/src/components/modes/ModesView.tsx

Why:
The effect that registers the window event listener intentionally has an empty dependency array; directly referencing switchMode violates react-hooks/exhaustive-deps. Using a ref decouples the effect from function identity changes while preserving up-to-date behavior.
Type safety for slug

Co-authored-by: roomote[bot] <219738659+roomote[bot]@users.noreply.github.com>
…rt success

updateCustomMode() was catching and swallowing all errors, causing
importModeWithRules() to return success even when mode persistence failed.
This led to auto-switch attempting to activate modes that never persisted.
…rt' of https://github.com/heyseth/Roo-Code into feat/auto-switch-to-imported-mode-after-successful-import
Moves type declaration out of event handler to prevent redeclaration on every message event.
…ejections while allowing importModeWithRules to detect persistence failures
…issues

- Use parseYamlSafely() in importModeWithRules for consistent YAML parsing
across the codebase, ensuring BOM stripping and character cleaning
- Sync visualMode with context.mode to prevent UI desync when modes are
switched programmatically from outside the component
- Add test coverage for fallback branch when imported mode slug is not
yet present in customModes state

Fixes parser inconsistency (P2), visualMode desync risk (P3), and missing
test coverage (P3) identified in code review.
Based on PR #8521 by @heyseth (Seth Miller) with the following enhancements:

Original work by Seth Miller:
- Auto-switch to imported mode after successful import
- Backend returns imported mode slug in importModeWithRules()
- UI switches to imported mode when found in customModes list
- Comprehensive test coverage for auto-switch functionality

Additional improvements:
- Use architect mode as fallback when imported slug not yet present
- Updated test to expect architect mode in fallback scenario
- Prevents UI desync during state refresh race conditions

Co-authored-by: Seth Miller <[email protected]>
@daniel-lxs daniel-lxs requested review from cte, jr and mrubens as code owners November 3, 2025 22:47
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Nov 3, 2025
@roomote
Copy link

roomote bot commented Nov 3, 2025

See this task on Roo Code Cloud

Review Complete

Latest commit (387acca) reviewed - no issues found. The change improves code maintainability by replacing hardcoded "architect" strings with defaultModeSlug constant.

The implementation remains well-structured:

  • Backend properly returns imported mode slug with safe error handling
  • UI implements fallback to default mode for race conditions
  • Refs pattern correctly avoids stale closures in message handlers
  • Comprehensive test coverage for all scenarios (success, fallback, failure, cancellation)
  • Follows established codebase patterns and conventions
Previous Reviews

Mention @roomote in a comment to trigger your PR Fixer agent and make changes to this pull request.

@daniel-lxs daniel-lxs moved this from Triage to PR [Needs Review] in Roo Code Roadmap Nov 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request PR - Needs Review size:L This PR changes 100-499 lines, ignoring generated files.

Projects

Status: PR [Needs Review]

Development

Successfully merging this pull request may close these issues.

4 participants