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

Task 10: Bridge live terminal frames into Tauri

Open AkaraChen opened this issue 2 months ago • 0 comments

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

  1. CLI helper attach and frame decode

    • Spawn the Task 3 sidecar as herdr terminal session control <pane_id> with Task 4 --session 2code (same HerdrEndpoint / HERDR_SOCKET_PATH isolation as process.rs). Never the user default session or $XDG_CONFIG_HOME/herdr/herdr.sock. Use command_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.frame bytes from base64 ANSI. Keep an application frame type (seq, full, width, height, decoded bytes) inside infra::herdr. Do not put CLI wire JSON (type, encoding, base64) in model or handler signatures.
    • Look up pane_id from session_runtime_mappings for Herdr-owned ids. If there is no mapping, or pane_identity_state is Missing, fail closed. Do not persist terminal_id. Do not tab.create, pane.split, or spawn a Local PTY to attach.
    • observe may exist as a test/helper path. Production GUI attach is control without --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).
  2. Input, resize, and release vs terminate

    • Implement Herdr TerminalRuntime::write / resize by writing one JSON line to the attached control helper stdin: terminal.input (text or bytes, not both) and terminal.resize (cols/rows > 0). Do not use JSON pane.send_text / pane.send_input / pane.send_keys as the live path. If no helper is attached, fail closed; do not fall back to Local write_pty / resize_pty.
    • GUI detach (detach_pty_output with the matching stream_id, helper Drop, GUI exit) sends terminal.release then kills and reaps the CLI child. The pane process, pane_id, and workspace must keep running. This is not pane.close, not close_session, and not server.stop.
    • close_pty_session stays Task 9 terminate (pane.close). A passing detach test that pane.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 / clear fail-closed on the Herdr adapter (Task 12). Do not replay pty_log onto a Herdr pane.
    • Replacing attach for the same session must use stream_id so stale React cleanup cannot release a newer helper. Release the previous helper before starting the next.
  3. Forward frames into Tauri without dual ownership

    • Route attach/stream/detach by RuntimeSelector. Local-owned ids keep today’s PtyOutputSink / Channel<&[u8]> path and Local PTY read loop. Herdr-owned ids must not be inserted into PtySessionMap or written to pty_log. Local-owned ids must not spawn a CLI helper.
    • Forward decoded frames through the bridge. Preserve full/seq so Task 11 is not guessing. Local byte IPC stays unchanged. If a new Herdr frame command/channel is added, run bun run typegen and do not edit src/generated/ by hand; do not wire Terminal.tsx (Task 11).
    • RuntimeRouter::new stays Local-default. Do not call ensure_herdr_listener / adoption / sync / terminal attach from Local default startup in src-tauri/src/lib.rs.
    • Do not answer CSI 6n / CSI c from 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 4 HerdrClientGuard plus these per-pane CLI children).
  4. 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 control without --takeover yields terminal.closed with 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 Drop kills and reaps the CLI child on success, error, and panic paths. After release, a new control without --takeover must be possible (tests may fake this; live optional).
  5. Prove with tests

    • Fake CLI: fragmented NDJSON; full: true vs full: false decode against Task 1 fixtures in src-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 not pane.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::new still selects Local. Herdr-selected tests with no client still do not spawn Local PTYs. Update write_resize_history_restore_stay_fail_closed so 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 the 2code namespace: 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.

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 1 frames/*.json and 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 9 tab.create/pane.close, frontend Terminal.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 control helper on namespace 2code by persisted pane_id. terminal_id is 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 reimplemented herdr-client.sock binary protocol. DSR/DA is not answered from frames.
  • [ ] Windows live attach is fail-closed and not claimed (#396). macOS live Unix/terminal session is not claimed (#401). Frontend xterm wiring is not this task.
  • [ ] cargo test --workspace from src-tauri (or cargo test --workspace --exclude code where GTK is unavailable). Frontend lint, typecheck, and tests apply only if frontend scope is introduced. IPC changes use bun run typegen; do not edit src/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.moved in 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.

AkaraChen avatar Sep 15 '26 12:09 AkaraChen