Skip to content

Conversation

@Shaheennazir
Copy link

@Shaheennazir Shaheennazir commented Oct 6, 2025

This change improves the terminal reuse logic to prevent the creation of unnecessary terminals. The previous implementation was too strict and would create new terminals even when idle ones were available.

The new logic introduces a fallback mechanism to reuse idle terminals even if their current working directory has drifted. When a terminal with a drifted CWD is reused, a cd command is prepended to the command being executed to ensure it runs in the correct directory.

This change also includes:

  • Updates to the RooTerminal interface and BaseTerminal class to support the new logic.
  • Comprehensive unit tests to validate the new terminal reuse behavior.

Related GitHub Issue

Closes: #

Roo Code Task Context (Optional)

Description

Test Procedure

Pre-Submission Checklist

  • Issue Linked: This PR is linked to an approved GitHub Issue (see "Related GitHub Issue" above).
  • Scope: My changes are focused on the linked issue (one major feature/fix per PR).
  • Self-Review: I have performed a thorough self-review of my code.
  • Testing: New and/or updated tests have been added to cover my changes (if applicable).
  • Documentation Impact: I have considered if my changes require documentation updates (see "Documentation Updates" section below).
  • Contribution Guidelines: I have read and agree to the Contributor Guidelines.

Screenshots / Videos

Documentation Updates

Additional Notes

Get in Touch


Important

Enhance terminal reuse logic to allow reusing idle terminals with drifted CWD by prepending a cd command.

  • Behavior:
    • Reuse idle terminals with drifted CWD by prepending cd command in Terminal.
    • Update TerminalRegistry to prioritize reusing terminals with the same task ID and provider.
  • Classes and Interfaces:
    • Add requestedCwd to RooTerminal interface and BaseTerminal class.
    • Modify runCommand() in Terminal to handle CWD drift.
  • Tests:
    • Add unit tests in Terminal.spec.ts and TerminalRegistry.spec.ts to validate terminal reuse logic.

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

This change improves the terminal reuse logic to prevent the creation of unnecessary terminals. The previous implementation was too strict and would create new terminals even when idle ones were available.

The new logic introduces a fallback mechanism to reuse idle terminals even if their current working directory has drifted. When a terminal with a drifted CWD is reused, a `cd` command is prepended to the command being executed to ensure it runs in the correct directory.

This change also includes:
- Updates to the `RooTerminal` interface and `BaseTerminal` class to support the new logic.
- Comprehensive unit tests to validate the new terminal reuse behavior.
@dosubot dosubot bot added size:L This PR changes 100-499 lines, ignoring generated files. enhancement New feature or request labels Oct 6, 2025

if (this.requestedCwd && !arePathsEqual(this.requestedCwd, currentCwd)) {
// Wrap path in quotes to handle spaces
commandToRun = `cd "${this.requestedCwd}" && ${command}`
Copy link
Contributor

Choose a reason for hiding this comment

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

When constructing the command (e.g., cd "${this.requestedCwd}" && ...), consider sanitizing 'requestedCwd' further to escape embedded quotes or other special characters.

Suggested change
commandToRun = `cd "${this.requestedCwd}" && ${command}`
commandToRun = `cd "${this.requestedCwd.replace(/\"/g, '\\"')}" && ${command}`

Copy link
Contributor

@roomote roomote bot left a comment

Choose a reason for hiding this comment

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

I found some issues that need attention. Inline comments call out specific fixes.


if (this.requestedCwd && !arePathsEqual(this.requestedCwd, currentCwd)) {
// Wrap path in quotes to handle spaces
commandToRun = `cd "${this.requestedCwd}" && ${command}`
Copy link
Contributor

Choose a reason for hiding this comment

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

[P1] Path escaping and shell portability: requestedCwd is interpolated into a shell command without escaping. If it contains a double quote, this will break command parsing. Also, using '&&' assumes a POSIX-like shell; Windows PowerShell 5 doesn't support '&&'. Escape quotes and consider shell-aware composition.

Suggested change
commandToRun = `cd "${this.requestedCwd}" && ${command}`
commandToRun = `cd "${this.requestedCwd.replace(/"/g, '\\"')}" && ${command}`

}

terminal.taskId = taskId
terminal.requestedCwd = cwd
Copy link
Contributor

Choose a reason for hiding this comment

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

[P2] Normalize CWD for consistency: requestedCwd is assigned directly from input. Other comparisons normalize to fsPath; assign the normalized path here to avoid mismatches across platforms.

Suggested change
terminal.requestedCwd = cwd
terminal.requestedCwd = vscode.Uri.file(cwd).fsPath

@hannesrudolph hannesrudolph added the Issue/PR - Triage New issue. Needs quick review to confirm validity and assign labels. label Oct 6, 2025
@github-project-automation github-project-automation bot moved this from Triage to Done in Roo Code Roadmap Oct 13, 2025
@github-project-automation github-project-automation bot moved this from New to Done in Roo Code Roadmap Oct 13, 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 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

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants