## 1. Backend - TerminalSession Refactoring - [ ] 1.1 Add circular output buffer to TerminalSession (10KB, stores raw bytes) - [ ] 1.2 Add WebSocket connection tracking (support multiple connections, detach without closing) - [ ] 1.3 Add last_activity timestamp and idle timeout support - [ ] 1.4 Add reset() method to kill process and prepare for restart - [ ] 1.5 Add health check for docker exec process - [ ] 1.6 Modify read_output to also write to circular buffer ## 2. Backend - TerminalManager Refactoring - [ ] 2.1 Change session tracking from session_id to instance_id - [ ] 2.2 Add get_or_create_session() method (reattach if exists, create if not) - [ ] 2.3 Modify create_session to support reconnection (don't always create new) - [ ] 2.4 Add reset_session() method (kill existing, create new) - [ ] 2.5 Add attach_websocket() method (add WebSocket to existing session, replay buffer) - [ ] 2.6 Add detach_websocket() method (remove WebSocket, keep session alive) - [ ] 2.7 Add idle timeout background task (check every minute, cleanup after 30min) - [ ] 2.8 Handle concurrent connections (close old WebSocket when new one connects) ## 3. Backend - Terminal WebSocket Endpoint - [ ] 3.1 Modify endpoint to check for existing session first - [ ] 3.2 Add reconnection logic (attach to existing vs create new) - [ ] 3.3 Handle reset command from WebSocket (JSON message type: "reset") - [ ] 3.4 Add buffer replay on WebSocket attach - [ ] 3.5 Add proper cleanup on WebSocket disconnect (detach, don't kill) ## 4. Backend - API Reset Endpoint - [ ] 4.1 Add POST /api/projects/{project_id}/repositories/{repo_id}/instances/{instance_id}/terminal/reset endpoint - [ ] 4.2 Add authorization checks - [ ] 4.3 Call TerminalManager.reset_session() - [ ] 4.4 Return success/error response ## 5. Frontend - Terminal Component - [ ] 5.1 Add "Reset Terminal" button to terminal UI - [ ] 5.2 Handle WebSocket reconnection gracefully - [ ] 5.3 Display "Reconnecting..." indicator - [ ] 5.4 Handle reset confirmation dialog - [ ] 5.5 Display session status (connected, reconnecting, reset) ## 6. Frontend - Terminal Hook - [ ] 6.1 Add reconnection logic with exponential backoff - [ ] 6.2 Handle buffer replay on reconnect (process incoming bytes) - [ ] 6.3 Add reset function (send WebSocket message or call API) - [ ] 6.4 Add heartbeat/ping to detect disconnections faster ## 7. Testing - [ ] 7.1 Test terminal session persistence across reconnections - [ ] 7.2 Test output buffer replay - [ ] 7.3 Test reset functionality - [ ] 7.4 Test idle timeout cleanup - [ ] 7.5 Test concurrent connection handling - [ ] 7.6 Verify existing functionality still works (create, stop, delete instances) ## 8. Documentation - [ ] 8.1 Update API documentation with new reset endpoint - [ ] 8.2 Update user documentation about persistent terminals - [ ] 8.3 Add troubleshooting guide for terminal issues