Task 2: Introduce a narrow runtime boundary
Summary
Separate 2code's terminal runtime operations from the current local PTY implementation so later Herdr adapters can plug in without rewriting handlers. This is the seam required by parent plan #394; it must not change user-visible terminal behavior.
The Task 1 contract (docs/herdr-integration.md, Herdr v0.9.0) explicitly unblocks this task. Do not talk to a Herdr server, download a sidecar, or change the default runtime.
Dependencies: Parent plan #394. Prerequisite: #395 (Task 1, accepted on issue-395-herdr-integration-contract). Open contract-gap issues #396–#401 do not block this task.
Work to complete
-
Define application-level runtime types
- Add Herdr-agnostic DTOs for backend selection, session identity, lifecycle results, and runtime discovery (which backend is active). Reuse existing
PtySessionMeta/PtyConfig/RestoreResultwhere they already describe 2code behavior. - Do not put Herdr NDJSON, schema method names, pane/workspace wire IDs, or
terminal.frametypes inmodelor handler signatures. - Identity types used by handlers remain 2code session IDs. Mapping onto Herdr pane/tab IDs belongs to later tasks.
- Add Herdr-agnostic DTOs for backend selection, session identity, lifecycle results, and runtime discovery (which backend is active). Reuse existing
-
Define the runtime trait and route handlers through it
- Introduce a service-layer interface covering terminal lifecycle (create, restore, close, list, delete), transport (write, resize, history, flush, clear), and discovery (selected backend).
- Adapt
src-tauri/src/handler/pty.rsso write/resize no longer callinfra::ptydirectly. Tauri channel attach/stream/detach may stay in the app/bridge layer; they are GUI attachment plumbing, not process ownership. - Wrap the existing
service::pty+infra::ptyimplementation as the Local adapter. Do not rewrite PTY spawning,pty_logpersistence, restore/history replay, or shell-init behavior.
-
Add a temporary backend selector
- Process-wide selector with default Local. This is the migration-switch foundation, not a user-facing setting and not a default-runtime change.
- Add a Herdr adapter module that is not selected by default. If selected in tests, it must fail closed (explicit error) without spawning a local PTY or touching a worktree.
- Bind each live session identity to exactly one backend. Reject operations that would let Local and Herdr own the same session. Do not start both backends for one create/restore/close.
- Keep Herdr protocol/wire types, if any placeholders exist, inside the Herdr adapter module only.
-
Prove routing without changing behavior
- Colocated tests: default selector uses Local; Local create/write/resize/close still behave as today; selecting the Herdr stub does not spawn a local PTY for that identity; a session bound to Local is not also handed to Herdr.
- Existing PTY coverage must keep passing: colocated tests in
crates/service/src/pty.rs,src-tauri/tests/pty_test.rs,src-tauri/tests/integration_pty_db.rs. - Prefer constructor/injection for test routing over a documented user env flag. If an override exists, production default remains Local and it is not a UI setting.
Likely files and directories
- New:
src-tauri/crates/model/src/runtime.rs— backend kind, discovery DTO, session-ownership types. - New:
src-tauri/crates/service/src/runtime.rs— trait, selector, Local adapter, stub Herdr adapter, routing tests. - Update:
src-tauri/crates/model/src/lib.rs,src-tauri/crates/service/src/lib.rs— module exports. - Update:
src-tauri/src/handler/pty.rs— delegate lifecycle/transport through the runtime boundary; stop callinginfra::ptyfor write/resize. - Update if needed:
src-tauri/src/bridge.rs,src-tauri/src/lib.rs— wire the Local adapter at startup without changing managed PTY state defaults. - Do not rewrite:
src-tauri/crates/service/src/pty.rs,src-tauri/crates/infra/src/pty.rs,pty_log.rs— wrap them.
Estimated changes: 300–550 lines total, additions plus deletions, including tests; excluding generated files and lockfile churn. Material scope growth should become a separate issue.
Acceptance criteria
- [ ] Application DTOs and a runtime trait cover lifecycle, terminal transport, and discovery without Herdr wire types leaking into
modelor handlers. - [ ] The current PTY implementation is adapted behind a Local adapter; create/write/resize/close/restore/history behavior is unchanged.
- [ ] Handlers no longer call
infra::ptydirectly for write/resize; they go through the runtime boundary. - [ ] A temporary selector defaults to Local. Herdr is not the default runtime.
- [ ] Tests prove routing: Local is selected by default; a Herdr-stub selection does not spawn a local PTY or mutate a worktree for that identity; one session identity cannot be owned by both backends.
- [ ] Existing PTY tests pass:
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 command names and signatures stay compatible; if they change, run
bun run typegenand do not editsrc/generated/by hand. - [ ] The local agent detector and frontend terminal path keep working; no dual-runtime ownership of a terminal or worktree.
Out of scope
- Bundling or downloading the Herdr sidecar (Task 3).
- Server discovery/startup (Task 4), socket client (Task 5), snapshot sync (Task 6).
- Database mapping, profile adoption, Herdr terminal create/attach, xterm transport, or agent-state from Herdr.
- User-facing runtime health UI or making Herdr the default (Tasks 18/21).
- Closing or implementing contract-gap issues #396–#401.
- Removing the local PTY runtime or agent detector.
- Changing frontend terminal rendering, restoration, or detection.
- Remote Herdr operation.