Task 10: Bridge live terminal frames into Tauri
Summary
Deliver interactive terminal transport through Herdr v0.9.0 without local PTY ownership, as required by parent plan #394. Implement the Task 1 CLI herdr terminal session transport against the Task 4 2code namespace. Forward decoded frames through Tauri channels. Route input, resize, and attachment release through the bridge. Bound buffering. Do not switch the default runtime, restore by respawning shells, apply xterm.js frame semantics, or let Local own a Herdr pane.
Pin remains Herdr v0.9.0, JSON protocol 22, endpoint generation 1. Supported transport: the pinned CLI helper, not the JSON API and not a reimplemented herdr-client.sock binary protocol (docs/herdr-integration.md Terminal frames). Namespace: Task 4 SESSION_NAME (2code) in src-tauri/crates/infra/src/herdr/process.rs. Attach by the Task 7/9 persisted pane_id from bind_session_pane. terminal_id is live PTY identity only and must not be persisted. Interactive attach is herdr terminal session control <pane_id> without --takeover. terminal.release plus helper Drop is GUI detach; Task 9 pane.close remains terminate. Stdout is NDJSON terminal.frame / terminal.closed. Preserve full, seq, width, height, and decoded ANSI bytes so Task 11 can treat full: true as a replacement surface. DSR/DA replies are Herdr PTY input; do not synthesize answers from frames.
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). Checkout base: #419 (task-9-terminal-lifecycle). 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 do not block Linux Task 10. Live Windows attach remains #396. Live macOS Unix/terminal session remains #401. Live Windows named-pipe remains #399.
Work to complete
-
CLI helper attach and frame decode
- Spawn the Task 3 sidecar as
herdr terminal session control <pane_id>with Task 4--session 2code(sameHerdrEndpoint/HERDR_SOCKET_PATHisolation asprocess.rs). Never the user default session or$XDG_CONFIG_HOME/herdr/herdr.sock. Usecommand_without_windows_console. One helper per 2code session. Do not attach observe and control together for the same pane in production. - Parse stdout incrementally as NDJSON. Decode
terminal.framebytesfrom base64 ANSI. Keep an application frame type (seq,full,width,height, decoded bytes) insideinfra::herdr. Do not put CLI wire JSON (type,encoding, base64) inmodelor handler signatures. - Look up
pane_idfromsession_runtime_mappingsfor Herdr-owned ids. If there is no mapping, orpane_identity_stateis Missing, fail closed. Do not persistterminal_id. Do nottab.create,pane.split, or spawn a Local PTY to attach. observemay exist as a test/helper path. Production GUI attach iscontrolwithout--takeover.- Windows attach must fail closed with a structured unsupported error (#396). Unix CLI attach may compile on macOS; do not claim executed macOS verification (#401).
- Spawn the Task 3 sidecar as
-
Input, resize, and release vs terminate
- Implement Herdr
TerminalRuntime::write/resizeby writing one JSON line to the attached control helper stdin:terminal.input(textorbytes, not both) andterminal.resize(cols/rows> 0). Do not use JSONpane.send_text/pane.send_input/pane.send_keysas the live path. If no helper is attached, fail closed; do not fall back to Localwrite_pty/resize_pty. - GUI detach (
detach_pty_outputwith the matchingstream_id, helper Drop, GUI exit) sendsterminal.releasethen kills and reaps the CLI child. The pane process,pane_id, and workspace must keep running. This is notpane.close, notclose_session, and notserver.stop. close_pty_sessionstays Task 9 terminate (pane.close). A passing detach test thatpane.closes is a failure. A passing close test that only drops the helper is still a Task 9 concern, not this task’s success path.- Keep
history/restore_session/flush/clearfail-closed on the Herdr adapter (Task 12). Do not replaypty_logonto a Herdr pane. - Replacing attach for the same session must use
stream_idso stale React cleanup cannot release a newer helper. Release the previous helper before starting the next.
- Implement Herdr
-
Forward frames into Tauri without dual ownership
- Route attach/stream/detach by
RuntimeSelector. Local-owned ids keep today’sPtyOutputSink/Channel<&[u8]>path and Local PTY read loop. Herdr-owned ids must not be inserted intoPtySessionMapor written topty_log. Local-owned ids must not spawn a CLI helper. - Forward decoded frames through the bridge. Preserve
full/seqso Task 11 is not guessing. Local byte IPC stays unchanged. If a new Herdr frame command/channel is added, runbun run typegenand do not editsrc/generated/by hand; do not wireTerminal.tsx(Task 11). RuntimeRouter::newstays Local-default. Do not callensure_herdr_listener/ adoption / sync / terminal attach from Local default startup insrc-tauri/src/lib.rs.- Do not answer CSI
6n/CSI cfrom observe/control frames. Pass decoded bytes through; Herdr already replies as PTY input. - Do not call
server.stop. GUI exit still only drops client helpers (Task 4HerdrClientGuardplus these per-pane CLI children).
- Route attach/stream/detach by
-
Bounded buffering, controller conflicts, helper shutdown
- Bound pending frame bytes/count between the CLI reader and the Tauri sink. A slow consumer must not grow unbounded. Over-cap: drop or disconnect with a structured error; prove it in tests.
- Split reads that cut a JSON line in the middle must reassemble. Reject oversized lines.
- A second
controlwithout--takeoveryieldsterminal.closedwith the verified conflict reason (already has an attached client; retry with --takeover). Surface that as a structured error. Do not retry with--takeover. Task 18 will show it in the UI. - Helper
Dropkills and reaps the CLI child on success, error, and panic paths. After release, a newcontrolwithout--takeovermust be possible (tests may fake this; live optional).
-
Prove with tests
- Fake CLI: fragmented NDJSON;
full: truevsfull: falsedecode against Task 1 fixtures insrc-tauri/crates/infra/tests/fixtures/herdr/frames/; seq ordering; input/resize/release stdin lines; no dual observe+control in the production attach path. - Slow consumer hits the buffer bound. Controller conflict does not pass
--takeover. Drop kills the child and does notpane.close. - Selector: Local write/resize/stream still behave as today; Herdr attach does not spawn a Local PTY; a Local-owned id refuses a Herdr helper; a Herdr-owned id refuses Local
write_pty. - Existing Local PTY, Task 2 routing, Task 6 projection, Task 7 mapping, Task 8 adoption, and Task 9 create/list/close tests keep passing.
RuntimeRouter::newstill selects Local. Herdr-selected tests with no client still do not spawn Local PTYs. Updatewrite_resize_history_restore_stay_fail_closedso write/resize work only with an attached helper; history/restore/flush/clear stay fail-closed. - Optional live check against Task 4
ensure_server+ Task 9 created pane on the2codenamespace: control attach, input, resize, release, process still alive, no--takeover. Skip without sidecar unless an explicit required flag is set. Never target the user default session. Live fixtures must not delete the user’s real checkouts.
- Fake CLI: fragmented NDJSON;
Likely files and directories
- New:
src-tauri/crates/infra/src/herdr/terminal.rs— CLI helper spawn, incremental frame parse, stdin commands, bounded buffer, Drop/reap - Update:
src-tauri/crates/infra/src/herdr/mod.rs— export terminal; stop saying no module attaches terminals - Update:
src-tauri/crates/service/src/runtime/herdr.rs— write/resize via attached helper; keep history/restore/flush/clear fail-closed - Update:
src-tauri/src/bridge.rs,src-tauri/src/handler/pty.rs— route attach/stream/detach/write/resize by backend; forward decoded frames; Local byte channel unchanged - Update if needed:
src-tauri/crates/service/src/runtime.rs— attach/release hooks without changing Local default;src-tauri/crates/model/src/error.rs— conflict / unsupported-platform / oversize / slow-consumer errors only - Reuse, do not duplicate: Task 9 pane lifecycle, Task 7 session→pane mapping, Task 4
SESSION_NAME/ sidecar env, Task 3 executable, Task 1frames/*.jsonand controller-exclusivity table, Task 5 NDJSON incremental parse ideas (CLI stdout is not the JSON request client) - Do not rewrite: Local PTY spawn/read/log, Task 6 subscribe/apply, Task 8
worktree.open, Task 9tab.create/pane.close, frontendTerminal.tsx,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 attach with the pinned CLI
terminal session controlhelper on namespace2codeby persistedpane_id.terminal_idis not persisted. Frames are decoded from NDJSON (full/seq/ANSI bytes preserved). - [ ] Write and resize go to helper stdin. Detach/release kills the helper and does not
pane.close. The pane process stays alive after GUI detach. - [ ] Buffering is bounded. Tests prove split frames, slow-consumer cap, controller conflict without
--takeover, and helper Drop/reap. - [ ] Default runtime remains Local. Local PTY/agent path still works. No 2code session or worktree is owned by both backends. Attach is not started from Local default startup. History/restore/flush/clear stay fail-closed on Herdr.
- [ ] Production attach does not call
pane.send_*,tab.create,pane.split,workspace.*,worktree.*,server.stop, or a reimplementedherdr-client.sockbinary protocol. DSR/DA is not answered from frames. - [ ] Windows live attach is fail-closed and not claimed (#396). macOS live Unix/
terminal sessionis not claimed (#401). Frontend xterm wiring is not this task. - [ ]
cargo test --workspacefromsrc-tauri(orcargo test --workspace --exclude codewhere GTK is unavailable). Frontend lint, typecheck, and tests apply only if frontend scope is introduced. IPC changes usebun run typegen; do not editsrc/generated/by hand.
Out of scope
- Connecting xterm.js to Herdr frames, query replies, paste/mouse, or tab-switch display (Task 11).
- Restore-by-reattachment UI, history replay removal, scroll/search policy (Task 12), agent notifications (Task 13).
- 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.
- Applying
pane.exited/layout.updated/tab.movedin the projection (#413). - Updating
docs/herdr-integration.md. - Removing the local PTY runtime or agent detector.
- Changing frontend terminal rendering, restoration, or detection.
- 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.