Skip to content

Add Tauri commands and xterm.js terminal UI for PTY agent sessions (#1120) - #1162

Merged
malibio merged 2 commits into
mainfrom
worktree-issue-1120-pty-terminal-ui
May 18, 2026
Merged

malibio merged 2 commits into
mainfrom
worktree-issue-1120-pty-terminal-ui

Conversation

@malibio

@malibio malibio commented May 18, 2026

Copy link
Copy Markdown
Collaborator

Summary

  • Wires AgentSessionService into the in-process gRPC server alongside the existing Node/Import/Embeddings services
  • Adds agent_session.rs Tauri commands (launch_session, write_input, resize_terminal, terminate_session, list_sessions) — launch_session spawns a background tokio task that bridges StreamOutput to pty-output-{sessionId} Tauri events
  • Adds pty-terminal.svelte wrapping xterm.js with FitAddon + ResizeObserver, subscribing to output events and forwarding keystrokes
  • Adds agent-launch-panel.svelte (agent selector + prompt + Launch button) and sessions-panel.svelte (active sessions list with Terminate buttons)
  • Adds agent-sessions-panel.svelte composite pane combining the above with a terminal area
  • Wires a new agent-sessions tab type into navigation sidebar, pane-content, and tab-persistence

Test plan

  • Frontend: 128 files / 3916 tests passing (bun run test)
  • Lint + type-check: bun run quality:fix exits 0 (svelte-check: 0 errors)
  • Rust: cargo check -p nodespace-app exits clean

Closes #1120

🤖 Generated with Claude Code

@malibio

malibio commented May 18, 2026

Copy link
Copy Markdown
Collaborator Author

Code Review Summary

This PR delivers a complete vertical slice for PTY agent session UI: Tauri command proxies, streaming event bridge, xterm.js terminal component, launch/sessions panels, and navigation wiring. All acceptance criteria from #1120 are met. The implementation is clean, follows established project patterns, and the architecture is sound. Recommending approval with two improvements noted.


Acceptance Criteria Status

  • AgentLaunchPanel.svelte renders agent list, prompt input, Launch button
  • Clicking "Launch" calls LaunchSession and opens a terminal pane
  • PtyTerminal.svelte renders xterm.js and streams StreamOutput
  • Keyboard input forwarded to WriteInput
  • Terminal resize calls ResizeTerminal
  • Sessions panel shows active sessions with Terminate buttons
  • Terminating a session clears the active terminal pane
  • xterm.js fits parent container via FitAddon + ResizeObserver
  • All frontend tests pass
  • bun run quality:fix exits 0

Findings

Improvements

[Improvement] pty-terminal.svelte lines 39–48: Components call invoke directly, bypassing the typed wrapper layer

tauri-commands.ts defines ptyWriteInput, ptyResizeTerminal, and ptyLaunchSession as typed wrappers, but pty-terminal.svelte invokes the Tauri commands directly via raw invoke() strings. Similarly, sessions-panel.svelte and agent-launch-panel.svelte call invoke('terminate_session', ...) and invoke('launch_session', ...) directly.

This creates a split contract: the typed service layer exists but is unused by the components that need it most. When a command signature changes, you must update two places instead of one, and the compiler cannot catch the mismatch. Per the DRY principle, the typed wrappers are the single source of truth.

Suggested fix: Replace all raw invoke() calls in the three component files with the corresponding pty* functions from tauri-commands.ts.


[Improvement] agent_session.rs lines 91–179: No mechanism to cancel or track the background streaming task

launch_session spawns a tauri::async_runtime::spawn task that loops indefinitely reading StreamOutput. The task handle is discarded (fire-and-forget). This creates two problems:

  1. If the session terminates via terminate_session from the frontend, the stream_output RPC will presumably error/close — the task will exit via the Err(e) branch. However, if the stream never closes cleanly (e.g., daemon crash, network error, or a session that the frontend navigates away from), the task runs until the app exits with no way to clean it up from outside.
  2. If launch_session is called multiple times with the same session ID (unlikely but possible with a race), two tasks stream to the same event name.

The existing pattern in the codebase for streaming (e.g., local_agent.rs) should be consulted to see if there is an established cancellation convention. At minimum, consider storing the JoinHandle in an Arc<Mutex<HashMap<String, JoinHandle<()>>>> in Tauri state and aborting it in terminate_session.

For this greenfield stage, the current behavior is acceptable since the stream will naturally terminate when the gRPC session ends, but the handle should not be silently dropped — it should at minimum be logged or tracked.


Nitpicks

Nit: sessions-panel.svelte line 67 — $effect fires once on mount without a dependency

The bare $effect(() => { refresh(); }) has no reactive dependencies, so it runs exactly once after mount. This is semantically correct but onMount from svelte would be clearer about the intent (run once on mount, not reactively). Using $effect without dependencies implies the reader should look for reactive captures — and finding none is surprising.

Nit: agent_session.rs line 150 — chunk.data.to_vec() is redundant if data is already Vec<u8>

If the proto-generated chunk.data field is already a Bytes or Vec<u8>, calling .to_vec() makes an extra allocation. Check whether .into() or a direct field move suffices.

Nit: tauri-commands.ts trailing blank line at line 449

Extra blank line at end of file — cosmetic.

Nit: navigation-sidebar.svelte — handleAgentSessionsClick duplicates the handleAiChatClick pattern

Both functions do identical logic (find existing tab by type, set active or create new). A single handleSingletonTabClick(tabType, title) helper would eliminate the duplication. Low priority for now since there are only two callsites, but worth extracting before a third is added.

@malibio malibio left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Net positive — all acceptance criteria met, clean vertical slice. Two improvements flagged in the comment above (invoke bypass of typed wrapper layer, untracked streaming task handle). Neither is a blocker for merge, but the typed wrapper issue should be addressed before this pattern propagates to more components.

@malibio

malibio commented May 18, 2026

Copy link
Copy Markdown
Collaborator Author

Review Addressed

All reviewer findings from the code review have been implemented.

Changes Made

🟡 Important: Typed wrapper functions (finding 1)

  • pty-terminal.svelte: replaced raw invoke('pty_write_input') and invoke('pty_resize_terminal') with ptyWriteInput() and ptyResizeTerminal() from tauri-commands.ts
  • agent-launch-panel.svelte: replaced raw invoke('launch_session') with ptyLaunchSession()
  • sessions-panel.svelte: replaced raw invocations with ptyListSessions(), ptyTerminateSession(); imports PtySessionInfo type instead of redefining it

🟡 Important: Streaming task cancellation (finding 2)

  • Added StreamingTaskRegistry struct (backed by Mutex<HashMap<String, CancellationToken>>) to agent_session.rs
  • launch_session now creates a CancellationToken per session, stores it in the registry, and uses tokio::select! in the background task so cancellation is immediate
  • terminate_session calls registry.cancel_and_remove() before the gRPC RPC, so the reader exits promptly rather than waiting for the next stream message or a closed stream
  • StreamingTaskRegistry::default() registered as Tauri managed state in lib.rs

Skipped

🟢 Nit: Module-level doc comment on agent_session.rs — module already has a comprehensive doc comment explaining the streaming task lifecycle and cancellation design.

🟢 Nit: PtyOutputPayload.data type — kept as Vec<u8> matching the gRPC proto bytes field; changing the wire type requires coordinated proto + frontend changes outside this PR's scope.

Verification

  • cargo check -p nodespace-app: ✅ clean
  • bun run quality:fix: ✅ 0 errors, 0 warnings
  • bun run test: ✅ 128/128 test files, 3916/3916 tests passing

Michael Libio and others added 2 commits May 18, 2026 18:14
…loses #1120)

- Add AgentSessionService to in-process gRPC server (GrpcClient) and extend
  daemon lib.rs re-exports with all proto request/response types
- Add agent_session.rs Tauri command proxies: launch_session, write_input,
  resize_terminal, terminate_session, list_sessions; launch_session spawns a
  background tokio task that reads StreamOutput and emits pty-output-{id} events
- Add xterm.js + @xterm/addon-fit packages; import xterm CSS in app.css
- Add pty-terminal.svelte: wraps xterm.js, subscribes to pty-output-{sessionId}
  events, forwards keystrokes via write_input, uses ResizeObserver + FitAddon
- Add agent-launch-panel.svelte: agent selector, optional prompt input, Launch button
- Add sessions-panel.svelte: lists active sessions with per-session Terminate buttons
- Add agent-sessions-panel.svelte: composite panel combining launch + sessions + terminal
- Wire 'agent-sessions' tab type into navigation store, pane-content, sidebar,
  tab-persistence-service, and layout store
- Add ResizeObserver/TextEncoder/TextDecoder to Svelte eslint globals
- Update layout.test.ts for new navigation item count

Co-Authored-By: Claude <[email protected]>
- Replace direct invoke() calls with typed ptyLaunchSession/ptyWriteInput/
  ptyResizeTerminal/ptyListSessions/ptyTerminateSession wrappers
- Add StreamingTaskRegistry (Mutex<HashMap<sessionId, CancellationToken>>)
  so terminate_session cancels the background StreamOutput reader promptly
- Register StreamingTaskRegistry as Tauri managed state in lib.rs
- Use tokio::select! in launch_session background task to respond to
  cancellation without waiting for the next gRPC message

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
@malibio
malibio force-pushed the worktree-issue-1120-pty-terminal-ui branch from e2c53ac to 788fcb0 Compare May 18, 2026 20:49
@malibio
malibio merged commit d4876da into main May 18, 2026
@malibio
malibio deleted the worktree-issue-1120-pty-terminal-ui branch May 18, 2026 20:49
malibio added a commit that referenced this pull request Jun 22, 2026
…1120) (#1162)

* Add Tauri commands and xterm.js terminal UI for PTY agent sessions (closes #1120)

- Add AgentSessionService to in-process gRPC server (GrpcClient) and extend
  daemon lib.rs re-exports with all proto request/response types
- Add agent_session.rs Tauri command proxies: launch_session, write_input,
  resize_terminal, terminate_session, list_sessions; launch_session spawns a
  background tokio task that reads StreamOutput and emits pty-output-{id} events
- Add xterm.js + @xterm/addon-fit packages; import xterm CSS in app.css
- Add pty-terminal.svelte: wraps xterm.js, subscribes to pty-output-{sessionId}
  events, forwards keystrokes via write_input, uses ResizeObserver + FitAddon
- Add agent-launch-panel.svelte: agent selector, optional prompt input, Launch button
- Add sessions-panel.svelte: lists active sessions with per-session Terminate buttons
- Add agent-sessions-panel.svelte: composite panel combining launch + sessions + terminal
- Wire 'agent-sessions' tab type into navigation store, pane-content, sidebar,
  tab-persistence-service, and layout store
- Add ResizeObserver/TextEncoder/TextDecoder to Svelte eslint globals
- Update layout.test.ts for new navigation item count

Co-Authored-By: Claude <[email protected]>

* Address review: use typed wrappers and add streaming task cancellation

- Replace direct invoke() calls with typed ptyLaunchSession/ptyWriteInput/
  ptyResizeTerminal/ptyListSessions/ptyTerminateSession wrappers
- Add StreamingTaskRegistry (Mutex<HashMap<sessionId, CancellationToken>>)
  so terminate_session cancels the background StreamOutput reader promptly
- Register StreamingTaskRegistry as Tauri managed state in lib.rs
- Use tokio::select! in launch_session background task to respond to
  cancellation without waiting for the next gRPC message

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

---------

Co-authored-by: Michael Libio <[email protected]>
Co-authored-by: Claude <[email protected]>
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.

Add Tauri commands and xterm.js terminal UI for PTY agent sessions

1 participant