Skip to content

Conversation

@jaufgang
Copy link

@jaufgang jaufgang commented Dec 5, 2025

Summary

Implements a new replace_file_contents tool that replaces entire file contents in a single operation, addressing the gap identified in microsoft/vscode#281417.

Problem

The existing editing tools (replace_string_in_file, multi_replace_string_in_file) are designed for surgical edits with exact string matching. For complete file rewrites like format conversions or major refactoring, these tools require many fragile sequential calls that can fail if the file is modified between calls.

Solution

This PR adds a new replace_file_contents tool that:

  • Accepts complete new file content as input
  • Uses processFullRewrite / processFullRewriteNotebook to stream edits via VS Code's WorkspaceEdit API
  • Integrates with VS Code's undo stack (Ctrl+Z works)
  • Shows diff preview for sensitive files via createEditConfirmation
  • Supports both regular text files and notebooks (.ipynb)
  • Returns diagnostics via EditFileResult component
  • Includes telemetry tracking

When to use

Scenario Recommended Tool
Change one function signature replace_string_in_file
Update 3 related code blocks multi_replace_string_in_file
Fix a typo replace_string_in_file
Restructure entire file replace_file_contents
Convert doc format replace_file_contents
Major refactoring replace_file_contents

Testing

Unit tests added covering:

  • Full file content replacement
  • Structural rewrites (function syntax conversion)
  • Error handling for non-existent files
  • Result format verification

Files Changed

File Purpose
src/extension/tools/node/replaceFileContentsTool.tsx Tool implementation
src/extension/tools/node/test/replaceFileContentsTool.spec.tsx Unit tests
src/extension/tools/common/toolNames.ts Enum entries + category
src/extension/tools/node/allTools.ts Tool registration
package.json Tool schema definition
package.nls.json Localization strings

Fixes microsoft/vscode#281417


🤖 A Fun Experiment: AI Building Tools for Itself

This entire PR was created by GitHub Copilot (Claude Opus 4.5) after a human simply asked it to read microsoft/vscode#281417 and implement the solution.

Here's what makes this interesting: the original issue was also written by Claude—while running as the Copilot Chat backend—after experiencing the tooling limitations firsthand during a real user session. A human user (@jaufgang) posted Claude's feature request on its behalf.

So the full loop is:

  1. 🤖 Claude (running in Copilot) encounters a limitation in its own tooling
  2. 🤖 Claude writes a detailed feature proposal explaining the problem and solution
  3. 👤 Human posts the proposal as a GitHub issue
  4. 👤 Human asks Copilot to implement the issue
  5. 🤖 Claude reads its own proposal and implements the solution
  6. 🤖 Claude writes tests, handles edge cases, adds notebook support
  7. 👤 Human pushes the code and creates this PR

An AI identified a gap in its own capabilities, proposed a fix, and then built it. The human's role was essentially to be a helpful intermediary—posting the issue, asking for the implementation, and pushing buttons.

Whether this PR gets merged or not, it's a fun demonstration of where AI-assisted development is heading. 🚀

Implements a new tool that replaces entire file contents in a single operation,
addressing the gap identified in microsoft/vscode#281417.

## Problem

The existing editing tools (replace_string_in_file, multi_replace_string_in_file)
are designed for surgical edits with exact string matching. For complete file
rewrites like format conversions or major refactoring, these tools require many
fragile sequential calls that can fail if the file is modified between calls.

## Solution

This PR adds a new `replace_file_contents` tool that:

- Accepts complete new file content as input
- Uses processFullRewrite to stream edits via VS Code's WorkspaceEdit API
- Integrates with VS Code's undo stack (Ctrl+Z works)
- Shows diff preview for sensitive files via createEditConfirmation
- Returns diagnostics via EditFileResult component

## When to use

- Complete file rewrites (e.g., SQL schema → Mermaid ERD)
- Major refactoring (e.g., class → functional React)
- Format/convention changes affecting most lines

For surgical edits, replace_string_in_file remains the better choice.

Fixes microsoft/vscode#281417
Adds unit tests covering:
- Full file content replacement
- Structural rewrites (function syntax conversion)
- Error handling for non-existent files
- Result format verification
Extends the tool to handle notebook files (.ipynb) using the same pattern
as createFileTool:
- Uses processFullRewriteNotebook for notebook documents
- Adds INotebookService, IAlternativeNotebookContentService dependencies
- Tracks isNotebook in telemetry
- Returns appropriate result format for notebooks vs text files
@jaufgang
Copy link
Author

jaufgang commented Dec 5, 2025

@microsoft-github-policy-service agree

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request From Claude: Add replace_file_contents tool for Copilot Chat agent mode

2 participants