# Add VS Code Diff Preview Integration (#448) #466
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Implements optional VS Code integration to preview suggested code changes as side-by-side diffs before approval, providing faster feedback and safer code review.
🎯 Fixes
Closes #448
📝 Summary
This PR adds VS Code diff preview capability to deepagents-cli. When enabled with the
--vscode-diffflag, the CLI automatically opens VS Code to show side-by-side diffs of proposed code changes before the approval prompt, allowing developers to review changes in their familiar editor environment with full syntax highlighting and editor features.🔧 Implementation
Core Components
New Module (
vscode_integration.py)is_vscode_available()- Detects if VS Code CLI is installedopen_diff_in_vscode()- Opens diff view with temp file management--waitflag to block until VS Code closes (prevents race conditions)atexithandler for cleanup as safety fallbackConfiguration (
config.py)vscode_diff_previewtoSessionStatetoggle_vscode_diff_preview()methodCLI Integration (
main.py)--vscode-diffcommand-line flagApproval Flow (
execution.py)prompt_for_tool_approval()write_fileandedit_fileoperationsperform_string_replacementto validate edits before showing diffold_stringexists and counts occurrences properlyKey Features
✅ Automatic Detection: Checks for
codecommand in PATH✅ Syntax Highlighting: Preserves file extensions for proper highlighting
✅ Reliable Cleanup: Uses
--wait+atexithandler to prevent race conditions✅ Edit Validation: Validates string replacements before showing diff to ensure accuracy
✅ Graceful Fallback: Works without VS Code, prints helpful message
✅ Non-Blocking: Uses
wait=Falseparameter for optional blocking behavior🧪 Testing
Test Suite (
test_vscode_integration.py)Test Coverage:
Note to Maintainers: This PR includes a Windows compatibility fix for the
execution.pymodule to resolve import errors on Windows.The Issue
The original code imported
termiosandttyunconditionally at the module level. These are Unix-only modules that don't exist on Windows, causingModuleNotFoundErrorwhen running the CLI or tests on Windows.The Fix
Changes Made:
termios/ttyimports conditional with try-exceptHAS_TERMIOSflag for runtime checksif HAS_TERMIOS:blocksinput()for approval promptsImpact:
Question for Maintainers:
Is this Windows compatibility approach acceptable? The alternative would be to make Windows support explicit in documentation/requirements, but this seemed like a minimal change that enables cross-platform usage.
📚 Documentation
Updated README (
libs/deepagents-cli/README.md)🔄 Backward Compatibility
✅ Fully backward compatible - feature is opt-in via
--vscode-diffflag✅ Zero impact when flag is not used
✅ Graceful degradation - works without VS Code installed
📋 Usage Example
🔍 Technical Notes
Temp File Management:
tempfile.mkdtemp(prefix="deepagents_diff_")--waitflag to block until VS Code closes the difffinallyblock after VS Code closesatexithandler registered as safety fallbackatexithandler after successful cleanupEdit Validation:
perform_string_replacementfromdeepagents.backends.utilsfor all edit operationsold_stringexists in file before creating diff previewreplace_allvs single-replace semanticsold_stringnot found, skipping diff previewWhy --wait is Always Used:
The initial implementation had a configurable
waitparameter, but we discovered this caused race conditions where temp files were deleted before VS Code could read them. Now--waitis always used to ensure VS Code has finished reading files before cleanup. Thewaitparameter is deprecated but kept for backwards compatibility.✅ Checklist