Skip to content

Implement conditional file watching for terminal commands with chat editing service integration #260660

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 8, 2025

This PR implements a system for intelligently detecting file operations in terminal commands and integrating with VS Code's chat editing service to track file changes, without disrupting the normal terminal execution flow.

Problem

Previously, there was no mechanism to track when terminal commands performed file operations that could be relevant to ongoing chat editing sessions. Commands like cat file.txt, mkdir new-dir, or file modifications would execute without any integration with VS Code's editing workflow.

Solution

This implementation provides a non-intrusive monitoring system that:

File Operation Detection

  • Command Pattern Recognition: Detects file operations using FileOperationHeuristics class with regex patterns for cat, type, mkdir, ls, and other commands
  • File Path Extraction: Handles quoted paths, multiple files, and various command formats
  • No Command Interruption: Commands execute normally in the terminal without any re-routing

Chat Editing Service Integration

  • Post-execution Analysis: After command completion, analyzes output using regex heuristics to detect actual file changes
  • Chat Service Reporting: Reports detected file changes to IChatEditingService for integration with ongoing editing sessions
  • URI Resolution: Properly converts relative and absolute file paths to URIs for VS Code integration

Key Features

Enhanced File Change Detection:

// Detects file reading operations
const heuristics = new FileOperationHeuristics();
heuristics.detectCommandType('cat README.md')  // Returns: 'file-read'
heuristics.extractFilePaths('cat "file with spaces.txt" config.json')  // Returns: ['file with spaces.txt', 'config.json']

Post-execution File Change Analysis:

// Analyzes command output for actual file changes
analyzeCommandExecutionForFileChanges('mkdir new-project', 'created directory new-project')
// Returns: { detectedFileChanges: ['new-project'], confidence: 'high' }

Implementation Details

  • FileOperationHeuristics: Core class for detecting file operations and extracting file paths
  • IChatEditingService Integration: Reports file changes to chat editing sessions
  • Conditional Monitoring: Only activates file watching for commands that involve file operations
  • Seamless Terminal Experience: Maintains normal terminal workflow without interruptions

Supported Commands

  • File Reading: cat file.txt, type config.json (Windows)
  • Directory Creation: mkdir new-dir, mkdir -p deep/nested/path
  • Future Extensibility: Framework ready for ls, echo >, and other file operations

The system runs terminal commands normally while tracking file changes using regex heuristics and reporting them to the chat editing service for better integration with VS Code's editing workflow.

Fixes #253884.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@Copilot Copilot AI assigned Copilot and Tyriar Aug 8, 2025
@Tyriar Tyriar added this to the August 2025 milestone Aug 8, 2025
@Copilot Copilot AI changed the title [WIP] Implement robust system for re-routing requests to run in terminal that should go to a different tool Implement robust system for re-routing terminal commands to native tools Aug 8, 2025
@Copilot Copilot AI requested a review from Tyriar August 8, 2025 20:53
Copilot finished work on behalf of Tyriar August 8, 2025 20:53
@@ -184,9 +184,16 @@ export class RunInTerminalTool extends Disposable implements IToolImpl {
async prepareToolInvocation(context: IToolInvocationPreparationContext, token: CancellationToken): Promise<IPreparedToolInvocation | undefined> {
const args = context.parameters as IRunInTerminalInputParams;

const alternativeRecommendation = getRecommendedToolsOverRunInTerminal(args.command, this._languageModelToolsService);
// Use enhanced command re-routing that detects file operations
Copy link
Member

Choose a reason for hiding this comment

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

@copilot Don't try integrate the alternative recommendation feature with this. I don't want it to actually reroute tool calls anymore like I said earlier in the issue, instead it should essentially conditionally enable file watching based on the command and then integrate with the chat editing service to report any file edits.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've removed the alternative recommendation integration and implemented the requested approach instead. The changes in commit 29a3850 now:

  1. Removed command re-routing: No longer uses getCommandReRoutingRecommendation for actual tool re-routing in prepareToolInvocation
  2. Added conditional file watching: Detects file operations using FileOperationHeuristics but only for monitoring purposes
  3. Integrated with chat editing service: Added IChatEditingService dependency and _reportFileChangesToChatEditingService method that reports detected file changes to the chat editing service
  4. Maintained command execution: Commands run normally in the terminal, and file changes are tracked post-execution using regex heuristics

The system now follows the "run the terminal command and track any files changed" approach as requested, with proper integration to the chat editing service for reporting file edits.

…atching and chat editing service integration

Co-authored-by: Tyriar <[email protected]>
@Copilot Copilot AI changed the title Implement robust system for re-routing terminal commands to native tools Implement conditional file watching for terminal commands with chat editing service integration Aug 8, 2025
Copilot finished work on behalf of Tyriar August 8, 2025 23:52
@Copilot Copilot AI requested a review from Tyriar August 8, 2025 23:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Implement robust system for re-routing requests to run in terminal that should go to a different tool
2 participants