2code icon indicating copy to clipboard operation
2code copied to clipboard

Task 11: Connect xterm.js to the Herdr transport

Open AkaraChen opened this issue 2 months ago • 0 comments

Summary

Render one Herdr-backed terminal end to end in the Tauri GUI, as required by parent plan #394. Replace backend-specific calls in Terminal.tsx with a frontend transport adapter. Apply the Task 1 frame semantics against Task 10 stream_herdr_output. Coordinate terminal query responses. Route keyboard, paste, mouse, and resize input correctly. Preserve mounted terminals when switching tabs. Do not switch the default runtime, restore by respawning shells, drive agent state from Herdr, or let Local own a Herdr pane.

Pin remains Herdr v0.9.0, JSON protocol 22, endpoint generation 1. Frames are already decoded application objects (seq, full, width, height, ANSI bytes) on HerdrTerminalFrame and stream_herdr_output. Local live bytes stay on stream_pty_output. full: true is a replacement surface (docs/herdr-integration.md Terminal frames; fixture frames/full-redraw.json). full: false is incremental (frames/incremental.json). DSR/DA replies are Herdr PTY input; xterm must not also answer CSI 6n / CSI c from Herdr frames (frames/dsr-da.json). Production attach remains Task 10 control without --takeover. Namespace stays Task 4 2code.

Dependencies: Parent plan #394. Prerequisites: #395 (Task 1, accepted on issue-395-herdr-integration-contract), #402 (Task 2, accepted on task-2-runtime-boundary), #404 (Task 3, accepted on task-3-herdr-sidecar), #406 (Task 4, accepted on task-4-server-discovery), #409 (Task 5, accepted on task-5-socket-client), #411 (Task 6, accepted on task-6-runtime-sync), #414 (Task 7, accepted on task-7-runtime-mapping), #417 (Task 8, accepted on task-8-profile-adoption), #419 (Task 9, accepted on task-9-terminal-lifecycle), #421 (Task 10, accepted on task-10-frame-bridge). Checkout base: #421 (task-10-frame-bridge). Open contract-gap issues #396–#401, docs gap #407, Task 6 event-apply gap #413, implemented Task 8/9 gaps #403/#416, and line-count trackers #400/#405/#408/#410/#412/#415/#418/#420/#422 do not block Linux Task 11. Live Windows attach remains #396. Live macOS Unix/terminal session remains #401. Live Windows named-pipe remains #399.

Work to complete

  1. Frontend transport adapter

    • Introduce a small adapter so Terminal.tsx does not hard-code Local byte IPC. Local-owned ids keep attachPtyOutput + streamPtyOutput (Channel<ArrayBuffer>) + history replay. Herdr-owned ids use attachPtyOutput + streamHerdrOutput (Channel<HerdrTerminalFrame>) and must not call streamPtyOutput (Task 10 no-ops it and delivers no frames).
    • Discover per-session ownership from RuntimeRouter::backend_for, not from RuntimeDiscovery.selected_backend and not by trying both streams. Unbound ids are Local. Add a thin IPC wrapper if needed (for example get_session_backend); run bun run typegen; do not edit src/generated/ by hand. Do not change RuntimeRouter::new away from Local.
    • writeToPty / resizePty / detachPtyOutput already route by backend. Keep using them. Do not add a second Herdr write/resize client in the frontend.
    • Do not spawn a Local PTY, write pty_log, or call streamHerdrOutput for a Local-owned id.
  2. Apply verified frame semantics and coordinate queries

    • Convert HerdrTerminalFrame.bytes to Uint8Array and term.write them. Do not UTF-8-decode chunks.
    • full: true: replacement surface, not extra scrollback. Reset/clear xterm scrollback, then write the frame. CSI 2J in the payload is not enough. Fixture: frames/full-redraw.json (CSI ?2026h, hide-cursor, OSC 8 reset, CSI 2J, CSI 1;1H).
    • full: false: incremental write on the current surface. Fixture: frames/incremental.json.
    • Apply increasing seq on a given stream. Ignore duplicate/older seq. A full: true frame is the new surface.
    • First Herdr frame after attach is full: true with the live screen. Skip restoreBuffer, getPtySessionHistory, flushPtyOutput, and sessionHistory replay on the Herdr path. Do not persist serialized buffers onto a Herdr pane on unmount.
    • DSR/DA: Herdr already replies as PTY input. On Herdr terminals, intercept CSI 6n / CSI c so xterm does not emit replies through onData → writeToPty. Do not synthesize answers from width/height/seq. Keep Local query answering unchanged. Existing suppressQueryResponses only hides displayed CPR; it does not stop xterm from answering.
    • Do not treat streamHerdrOutput ending as process exit. terminal.closed may be release or controller conflict. Do not print [Process exited] or rely on pty-exit-{id} for Herdr (Local emitter only). Task 18 surfaces conflicts.
  3. Input, resize, and mounted tab switches

    • Keyboard, paste, and application mouse-reporting sequences stay on onData → writeToPty. Wire onBinary the same way if xterm emits it. Do not send CLI terminal.scroll / JSON pane.scroll (Task 12).
    • Resize stays on the existing scheduler + resizePty. Do not send cols/rows 0 when the host is hidden. createResizeScheduler already cancels 0×0; keep that.
    • Terminals stay mounted across tab and route changes (display: none / parking). Hidden Herdr terminals stay attached and keep applying frames. Do not detach on isActive === false. Do not conditionally unmount <Terminal>.
    • Clear-screen on Herdr sends \x0C as input and waits for frames. Do not clearPtyOutput / rewrite pty_log on a Herdr id.
    • Keep the local agent detector running on Herdr frames (Task 13). Do not call --takeover. Do not attach observe and control together.
  4. Prove with tests

    • Local: still streamPtyOutput, history replay, select-to-copy, detector, and tab hide without unmount. Existing Terminal.test.tsx cases keep passing.
    • Herdr: streamHerdrOutput only; full: true replaces scrollback; full: false appends; stale seq ignored; no getPtySessionHistory / restoreBuffer; CSI 6n / CSI c do not writeToPty; hide/show does not detachPtyOutput.
    • Selector: a Local-owned id never calls streamHerdrOutput; a Herdr-owned id never calls streamPtyOutput.
    • bun run lint:check, bun run typecheck, bun run test. If Rust IPC is added: cargo test --workspace from src-tauri (or --exclude code where GTK is unavailable). IPC changes use bun run typegen.

Likely files and directories

  • New: src/features/terminal/lib/ transport module (Local bytes vs Herdr frames) plus query-guard helper if it does not belong in suppressQueryResponses.ts
  • Update: src/features/terminal/Terminal.tsx, src/features/terminal/Terminal.test.tsx, src/features/terminal/lib/index.ts
  • Update if needed: src/features/terminal/lib/resizeScheduler.ts; src-tauri/src/handler/pty.rs + src-tauri/src/lib.rs for thin backend_for IPC only
  • Reuse, do not duplicate: Task 10 stream_herdr_output / HerdrTerminalFrame, Task 10 attach/detach/write/resize routing, Task 1 frames/*.json, TerminalLayer CSS hide + parking
  • Do not rewrite: CLI helper in terminal.rs, Local PTY read loop, Task 9 tab.create/pane.close, restoration.ts (Task 12), detector rules (Task 13), docs/herdr-integration.md

Estimated changes: 350–600 lines total, additions plus deletions, including tests; excluding generated files and lockfile churn. Material scope growth should become a separate issue.

Acceptance criteria

  • [ ] Herdr-owned sessions render through streamHerdrOutput. full: true replaces the xterm surface; full: false is incremental; seq is monotonic. Bytes are written as bytes.
  • [ ] Herdr attach does not replay pty_log or localStorage scrollback. Local history replay still runs for Local-owned sessions.
  • [ ] Keyboard, paste, mouse-reporting input, and resize use existing write/resize IPC. Hidden tabs stay mounted and attached. Resize is not sent at 0×0.
  • [ ] Herdr terminals do not answer DSR/DA from frames. Local PTY query answering is unchanged. Stream end is not treated as process exit.
  • [ ] Default runtime remains Local. Local PTY/agent path still works. No 2code session or worktree is owned by both backends. Adapter choice uses per-session backend_for, not a default-runtime switch.
  • [ ] Production UI does not call pane.send_*, tab.create, pane.split, workspace.*, worktree.*, server.stop, --takeover, or a reimplemented herdr-client.sock protocol.
  • [ ] Windows live attach is not claimed (#396). macOS live Unix/terminal session is not claimed (#401). Restore-by-reattachment UI is not this task.
  • [ ] bun run lint:check, bun run typecheck, and bun run test pass. If Rust IPC changes: cargo test --workspace from src-tauri (or --exclude code where GTK is unavailable). bun run typegen for IPC; do not edit src/generated/ by hand.

Out of scope

  • Restore-by-reattachment UI, history replay removal as a product policy, scroll/search against Herdr history (Task 12).
  • Driving agent badges/notifications from Herdr (Task 13). Local detector stays on.
  • Creating profiles through Herdr worktree.create (Task 14) or routing worktree/project cleanup through Herdr (Task 15).
  • User-facing runtime health UI, explicit takeover UX, legacy-session migration UI, or making Herdr the default (Tasks 18/19/21).
  • Closing or implementing contract-gap issues #396–#401, docs gap #407, Task 6 event-apply gap #413, implemented gaps #403/#416, or line-count trackers #400/#405/#408/#410/#412/#415/#418/#420/#422.
  • Applying pane.exited / layout.updated / tab.moved in the projection (#413).
  • Updating docs/herdr-integration.md.
  • Removing the local PTY runtime or agent detector.
  • Changing Task 10 CLI attach, bounded buffering, or helper Drop.
  • Remote Herdr operation.
  • Startup commands on create (#397).

Standing constraints

  • No default-runtime switch.
  • Existing Local PTY/agent path stays working until the Herdr path passes acceptance.
  • Never let both runtimes own the same terminal or worktree.

AkaraChen avatar Sep 15 '26 13:09 AkaraChen