Task 11: Connect xterm.js to the Herdr transport
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
-
Frontend transport adapter
- Introduce a small adapter so
Terminal.tsxdoes not hard-code Local byte IPC. Local-owned ids keepattachPtyOutput+streamPtyOutput(Channel<ArrayBuffer>) + history replay. Herdr-owned ids useattachPtyOutput+streamHerdrOutput(Channel<HerdrTerminalFrame>) and must not callstreamPtyOutput(Task 10 no-ops it and delivers no frames). - Discover per-session ownership from
RuntimeRouter::backend_for, not fromRuntimeDiscovery.selected_backendand not by trying both streams. Unbound ids are Local. Add a thin IPC wrapper if needed (for exampleget_session_backend); runbun run typegen; do not editsrc/generated/by hand. Do not changeRuntimeRouter::newaway from Local. writeToPty/resizePty/detachPtyOutputalready 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 callstreamHerdrOutputfor a Local-owned id.
- Introduce a small adapter so
-
Apply verified frame semantics and coordinate queries
- Convert
HerdrTerminalFrame.bytestoUint8Arrayandterm.writethem. Do not UTF-8-decode chunks. full: true: replacement surface, not extra scrollback. Reset/clear xterm scrollback, then write the frame. CSI2Jin 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
seqon a given stream. Ignore duplicate/olderseq. Afull: trueframe is the new surface. - First Herdr frame after attach is
full: truewith the live screen. SkiprestoreBuffer,getPtySessionHistory,flushPtyOutput, andsessionHistoryreplay 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 cso xterm does not emit replies throughonData→writeToPty. Do not synthesize answers fromwidth/height/seq. Keep Local query answering unchanged. ExistingsuppressQueryResponsesonly hides displayed CPR; it does not stop xterm from answering. - Do not treat
streamHerdrOutputending as process exit.terminal.closedmay be release or controller conflict. Do not print[Process exited]or rely onpty-exit-{id}for Herdr (Local emitter only). Task 18 surfaces conflicts.
- Convert
-
Input, resize, and mounted tab switches
- Keyboard, paste, and application mouse-reporting sequences stay on
onData→writeToPty. WireonBinarythe same way if xterm emits it. Do not send CLIterminal.scroll/ JSONpane.scroll(Task 12). - Resize stays on the existing scheduler +
resizePty. Do not sendcols/rows0 when the host is hidden.createResizeScheduleralready 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 onisActive === false. Do not conditionally unmount<Terminal>. - Clear-screen on Herdr sends
\x0Cas input and waits for frames. Do notclearPtyOutput/ rewritepty_logon 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.
- Keyboard, paste, and application mouse-reporting sequences stay on
-
Prove with tests
- Local: still
streamPtyOutput, history replay, select-to-copy, detector, and tab hide without unmount. ExistingTerminal.test.tsxcases keep passing. - Herdr:
streamHerdrOutputonly;full: truereplaces scrollback;full: falseappends; staleseqignored; nogetPtySessionHistory/restoreBuffer; CSI6n/CSI cdo notwriteToPty; hide/show does notdetachPtyOutput. - Selector: a Local-owned id never calls
streamHerdrOutput; a Herdr-owned id never callsstreamPtyOutput. bun run lint:check,bun run typecheck,bun run test. If Rust IPC is added:cargo test --workspacefromsrc-tauri(or--exclude codewhere GTK is unavailable). IPC changes usebun run typegen.
- Local: still
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 insuppressQueryResponses.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.rsfor thinbackend_forIPC only - Reuse, do not duplicate: Task 10
stream_herdr_output/HerdrTerminalFrame, Task 10 attach/detach/write/resizerouting, Task 1frames/*.json, TerminalLayer CSS hide + parking - Do not rewrite: CLI helper in
terminal.rs, Local PTY read loop, Task 9tab.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: truereplaces the xterm surface;full: falseis incremental;seqis monotonic. Bytes are written as bytes. - [ ] Herdr attach does not replay
pty_logor 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 reimplementedherdr-client.sockprotocol. - [ ] Windows live attach is not claimed (#396). macOS live Unix/
terminal sessionis not claimed (#401). Restore-by-reattachment UI is not this task. - [ ]
bun run lint:check,bun run typecheck, andbun run testpass. If Rust IPC changes:cargo test --workspacefromsrc-tauri(or--exclude codewhere GTK is unavailable).bun run typegenfor IPC; do not editsrc/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.movedin 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.