Commit Graph

11 Commits

  • feat: implement HTTP polling to bypass WebSocket issue
    The WebSocket closes immediately with code 1006 due to nginx/proxy
    issues. This implementation completely bypasses WebSocket for
    terminal output by using HTTP polling instead.
    
    Architecture:
    - Server buffers PTY output in memory (outputBuffer array)
    - Frontend polls every 100ms via GET /terminals/:id/output?since=N
    - Output entries have monotonically increasing index
    - Old output is automatically cleaned up after 5 minutes
    - Commands sent via HTTP POST (already implemented)
    
    Changes:
    - terminal-service.js: Added bufferOutput(), getTerminalOutput()
    - terminal.js: Added startPolling(), stopPolling(), sendTerminalResize()
    - server.js: Added GET /terminals/:id/output and POST /terminals/:id/resize
    - No WebSocket needed for output display (keeps legacy compatibility)
    
    Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
  • fix: terminal command execution via HTTP POST workaround
    The WebSocket send mechanism fails with close code 1006 when client
    tries to send data to server. Server never receives the message,
    indicating a network/proxy layer issue that couldn't be fixed through
    code changes or nginx configuration.
    
    Solution: Bypass WebSocket send entirely by using HTTP POST to send
    commands directly to the PTY.
    
    Changes:
    - Added sendTerminalInput() method to terminal-service.js that writes
      directly to PTY, bypassing WebSocket
    - Added POST endpoint /claude/api/terminals/:id/input to server.js
    - Modified launchCommand() in terminal.js to use fetch() with HTTP
      POST instead of WebSocket.send()
    
    The WebSocket receive direction still works (server→client for output
    display), only send direction (client→server for commands) is bypassed.
    
    Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
  • fix: update nginx config for proper WebSocket support
    Changes:
    1. Added  map for proper WebSocket upgrade handling
    2. Changed Connection header from literal 'upgrade' to  variable
    3. Added proxy_send_timeout 7d to prevent premature connection closure
    4. Disabled proxy_buffering and proxy_request_buffering for WebSocket
    5. Added proxy_request_buffering off for better WebSocket performance
    
    These changes should fix the WebSocket closure issue that was preventing
    commands from being sent to the terminal.
  • fix: remove stabilization delay and fitAddon.fit() call
    The WebSocket was closing exactly 100ms after switchToTerminal completed,
    which correlated with the setTimeout(fitAddon.fit(), 100) call.
    
    Hypothesis: The fitAddon.fit() call or the 100ms delay is causing
    the WebSocket to close through an unknown mechanism (possibly triggering
    browser GC, event loop blocking, or some resource cleanup).
    
    Changes:
    - Removed 100ms stabilization delay in launchCommand
    - Disabled fitAddon.fit() call in switchToTerminal
    
    This should prevent the WebSocket closure and allow commands to be sent.
  • fix: await switchToTerminal to prevent race condition
    The issue was that switchToTerminal() was not being awaited, so
    launchCommand() was called while switchToTerminal() was still
    executing. This caused a race condition where the WebSocket
    closed before the command could be sent.
    
    By awaiting switchToTerminal(), we ensure the terminal is fully
    switched before attempting to send any commands.
  • debug: add switchToTerminal logging and fitAddon error handling
    - Log when switchToTerminal is called and if terminal is in map
    - Log fitAddon.fit() execution with error handling
    - Log when switchToTerminal completes
    
    This will help identify if switchToTerminal is causing the WebSocket closure
  • debug: add detailed spawn attempt and error logging
    - Log spawn attempt before calling spawn()
    - Add detailed error logging with stack trace
    - This will help identify if spawn is failing silently
  • debug: add PTY lifecycle logging
    - Log PTY PID and shell when spawned
    - Log all PTY output data
    - Log PTY exit events with exit code and signal
    - Log when WebSocket cannot send PTY data
    
    This will help identify if the PTY is exiting immediately
    or if there's an issue with the PTY process.
  • debug: add WebSocket stability delay and enhanced checks
    - Added 100ms delay before sending command to ensure WebSocket stability
    - Added detailed WebSocket state logging (CONNECTING, OPEN, CLOSING, CLOSED)
    - Added bufferedAmount check to wait for pending sends
    - Wrapped ws.send() in try-catch for better error reporting
    - Split terminal and ws existence checks for clearer debugging
  • debug: add visual debug panel and comprehensive logging
    - Added debug panel in terminal view that shows all terminal activity
    - Added debugLog() method to TerminalManager for consistent logging
    - Updated connectTerminal, handleTerminalMessage, launchCommand, createTerminal, initializeXTerm with detailed logging
    - Enhanced backend logging for WebSocket messages and close codes
    - Logs now show both to console and visual debug panel
    
    This should help diagnose the terminal command execution issue without
    requiring browser console access.
  • feat: add session move endpoint and project-session cascading delete
    - Add sessions table to database with projectId and deletedAt columns
    - Create POST /api/sessions/:id/move endpoint to reassign sessions
    - Update DELETE /api/projects/:id to cascade soft-delete to sessions
    - Support moving sessions between projects or to unassigned state
    - Handle both active (in-memory) and historical sessions
    
    Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>