7623f29ffb
- Fill empty apply-pr2.md with backend API + frontend client apply report - Mark all 12 tasks as completed in tasks.md - Update .openspec.yaml status from exploring to completed The implementation was already merged to dev across PR 1, PR 2, and PR 3. This commit only synchronizes the OpenSpec change metadata.
4.9 KiB
4.9 KiB
Apply Report: PR 2 – Backend API + Frontend Client for Multi-Session Terminal UX
Summary
Implemented the session-scoped WebSocket route, REST endpoints for terminal session CRUD, and the frontend API client/hook. All changes are backward-compatible with the legacy single-session /terminal WebSocket endpoint and POST .../terminal/reset alias.
Key Changes
- WebSocket Routing – Added
/ws/tool-instances/{instance_id}/terminal/{session_id}route and preserved/ws/tool-instances/{instance_id}/terminalas the default-session alias. Extracted a shared_handle_terminal_websocket()handler containing auth, validation, and the I/O loop. - REST Endpoints – Added the following endpoints under
/instances/{instance_id}/terminal:GET /sessions– list sessions with livehas_websocketsflagPOST /sessions– create a new session (409 when max 5 reached)DELETE /sessions/{session_id}– close a sessionPOST /sessions/{session_id}/reset– reset a specific sessionPOST /sessions/{session_id}/rename– rename a sessionPOST /reset– legacy alias for resetting the default session
- Frontend API Client – Created
apps/web/src/api/terminal.tswith typed REST wrappers. - Frontend Hook – Created
apps/web/src/hooks/use-terminal-sessions.tsfor loading, creating, closing, resetting, and renaming sessions with optimistic UI updates. - Tests – Added
apps/api/tests/api/test_terminal_ws_multi.pywith auth-requirement coverage for every new endpoint and WebSocket route.
Files Created
apps/api/tests/api/test_terminal_ws_multi.pyapps/web/src/api/terminal.tsapps/web/src/hooks/use-terminal-sessions.ts
Files Modified
apps/api/src/api/system/terminal.py– added WebSocket session route, shared handler, REST endpoints, legacy aliasesapps/web/src/api/terminal.ts(created)apps/web/src/hooks/use-terminal-sessions.ts(created)
Test Results
New Tests (8/8 passed)
$ cd apps/api && python -m pytest tests/api/test_terminal_ws_multi.py -v
tests/api/test_terminal_ws_multi.py::TestTerminalWebSocketMultiSession::test_specific_session_websocket_route_exists PASSED
tests/api/test_terminal_ws_multi.py::TestTerminalWebSocketMultiSession::test_default_session_alias_route_exists PASSED
tests/api/test_terminal_ws_multi.py::TestTerminalRestApi::test_list_sessions_requires_auth PASSED
tests/api/test_terminal_ws_multi.py::TestTerminalRestApi::test_create_session_requires_auth PASSED
tests/api/test_terminal_ws_multi.py::TestTerminalRestApi::test_close_session_requires_auth PASSED
tests/api/test_terminal_ws_multi.py::TestTerminalRestApi::test_reset_session_requires_auth PASSED
tests/api/test_terminal_ws_multi.py::TestTerminalRestApi::test_rename_session_requires_auth PASSED
tests/api/test_terminal_ws_multi.py::TestTerminalRestApi::test_legacy_reset_alias_requires_auth PASSED
======================== 8 passed in 0.42s ========================
Full Suite (no regressions)
$ cd apps/api && python -m pytest tests/ -q
182 passed, 51 pre-existing failures, 6 warnings
- Baseline failures: 51 (pre-existing, unchanged by this PR)
- New passes: +8 (from
test_terminal_ws_multi.py) - No new failures introduced
Deviations from Design
- Path simplification – REST endpoints were mounted under
/instances/{instance_id}/terminal/...rather than the originally proposed/projects/{pid}/repositories/{rid}/instances/{iid}/terminal/.... This keeps the API consistent with the existing terminal WebSocket path which only requiresinstance_id, and matches the frontend's routing model where the terminal page is reached directly by instance ID. - Endpoint path prefix – The terminal router is included at the application root; endpoints live at
/instances/{instance_id}/terminal/sessions(no/projects/...nesting). - Test scope – The integration tests for PR 2 focus on auth/404 routing because fully exercising WebSocket multi-session I/O requires a running Docker container and authenticated cookie flow. Those deeper integration scenarios are covered in PR 1 unit tests (manager behavior) and can be added to PR 2 later if an async test client with cookie auth becomes practical.
Blockers / Risks
- Auth-only REST tests – The new REST endpoints are only tested for auth gating in this PR. End-to-end CRUD behavior with real instances is exercised indirectly through the frontend and the manager unit tests from PR 1.
- WebSocket restore path – When a client connects to
/ws/.../terminal/{session_id}for a session that is in the DB but not in memory (e.g. after API restart), the handler recreates the PTY from the DB row. This path is not yet covered by automated tests.
Next Recommended Action
- Task 9–12 (Frontend UI + tests) – Implement
TerminalSessionTabs, updateTerminalPagefor multi-session orchestration, fullscreen, and keyboard shortcuts, and add frontend tests.