Skip to content

Conversation

@weegens-aaron
Copy link
Owner

Fix: Windows Ctrl+C Terminal Corruption Issue

Summary

This PR fixes a critical Windows-specific terminal corruption issue that occurs when users interrupt long-running operations with Ctrl+C. The problem manifested as corrupted terminal display with stuck formatting and unresponsive input, significantly degrading the user experience on Windows systems.

Changes Made

Main.py Changes

  • Pre-prompt cleanup: Added Windows-specific terminal state reset before user input prompts to ensure clean state
  • Post-cancellation cleanup: Implemented terminal state restoration after task cancellation to prevent corruption
  • Platform detection: Used platform.system() == "Windows" to apply fixes only on Windows systems
  • Graceful degradation: Wrapped all Windows-specific code in try-catch blocks to prevent crashes on non-Windows systems

Console Spinner Changes

  • Comprehensive cleanup: Added multi-step terminal restoration when Rich's Live display stops
  • ANSI reset: Reset all ANSI formatting codes for both stdout and stderr
  • Line clearing: Clear current line and reposition cursor properly
  • Keyboard buffer flush: Clear any stuck keys from the input buffer using Windows-specific msvcrt functions
  • Defensive programming: Handle ImportError gracefully for non-Windows Python implementations

Technical Details

Root Cause Analysis

The issue stemmed from two main problems:

  1. Rich's Live Display: The Rich library's Live display can leave the Windows terminal in a corrupted state after interruption
  2. ANSI state accumulation: Unhandled Ctrl+C interrupts leave ANSI formatting codes active, causing display corruption

Implementation Approach

  • Layered cleanup: Applied terminal resets at multiple points (before input, after cancellation, spinner cleanup)
  • Minimal invasiveness: Changes only affect Windows systems, with no impact on Unix-like systems
  • Fail-safe design: All Windows-specific code wrapped in exception handlers to prevent cascading failures

Design Patterns Used

  • Platform-specific implementation: Clean conditional logic for platform detection
  • Defensive programming: Comprehensive error handling with silent failure
  • Resource cleanup: Proper terminal state management following RAII principles

Files Modified

code_puppy/main.py

  • Lines ~478-487: Added pre-prompt terminal reset before user input
  • Lines ~650-661: Added post-cancellation terminal reset after task interruption
  • Purpose: Prevents terminal corruption during user interaction

code_puppy/messaging/spinner/console_spinner.py

  • Lines ~79-106: Added comprehensive Windows terminal cleanup in spinner stop method
  • Purpose: Restores terminal state after Rich's Live display operations
  • Added import: platform module for platform detection

Testing

Manual Testing Scenarios

  1. Ctrl+C interruption: Start a long-running agent task and interrupt with Ctrl+C
  2. Multiple interrupts: Rapid Ctrl+C presses during task execution
  3. Spinner cancellation: Cancel operations that show the console spinner
  4. Cross-platform verification: Confirmed no regression on non-Windows systems

Testing Approach

  • Visual verification: Manual testing on Windows 10/11 terminals (cmd, PowerShell, Windows Terminal)
  • Graceful degradation: Verified no impact on Unix-like systems
  • Error handling: Confirmed no crashes when terminal operations fail

Recommended Testing

  • Test on multiple Windows terminal emulators
  • Verify behavior with different Python implementations (CPython, PyPy)
  • Test with and without Rich console coloring enabled

Breaking Changes

None - This is a purely additive fix that only affects Windows systems and doesn't change any existing interfaces or behavior.

Additional Notes

Windows Terminal Compatibility

  • Tested on Windows 10/11 with Command Prompt, PowerShell, and Windows Terminal
  • Compatible with both legacy Windows consoles and modern terminal emulators
  • No dependency on specific Windows terminal features

Future Considerations

  • Consider adding automated tests for Windows terminal state management
  • Could be extended to handle other platform-specific terminal issues
  • May benefit from additional terminal capability detection

Related Issues

  • Fixes terminal corruption during Ctrl+C interruptions
  • Resolves display issues with Rich's Live display on Windows
  • Improves overall CLI experience for Windows users

Performance Impact

  • Negligible: Terminal reset operations are minimal and only executed on Windows
  • No overhead: Conditional checks are simple string comparisons
  • User benefit: Significantly improves UX by preventing corrupted displays

Reviewer Checklist:

  • Changes only affect Windows systems as intended
  • No regression on Unix-like systems
  • Error handling is robust and doesn't mask real issues
  • Code follows existing project patterns and style
  • Documentation is clear and comprehensive

@weegens-aaron weegens-aaron force-pushed the fix/ctrl-c-fixed-for-windows branch from fd68f73 to 5d7f1b1 Compare December 12, 2025 22:33
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.

2 participants