Task 4: Implement server discovery and startup
Summary
Connect 2code to a compatible Herdr v0.9.0 server in a dedicated 2code namespace, starting one when necessary, as required by parent plan #394. Discovery must not double-launch. GUI exit must release only client helpers and leave the server running.
Reuse the Task 1 contract in docs/herdr-integration.md and the Task 3 sidecar resolver (resolve_sidecar / report_version). Do not switch the default runtime, attach terminals, or own worktrees. Pin remains Herdr v0.9.0, protocol 22, endpoint generation 1.
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). Checkout base: #404 (task-3-herdr-sidecar). Open contract-gap issues #396–#401, Task 2 gap #403, and Task 3 tracker #405 do not block this task.
Work to complete
-
Namespace and endpoint resolution
- Use a dedicated session name
2code. Never the user default session or$XDG_CONFIG_HOME/herdr/herdr.sock. - Named sessions live at
$XDG_CONFIG_HOME/herdr/sessions/<name>/herdr.sockand can overflowsun_path(104 bytes). If the named path would overflow, fall back to a short socket still in the2codesession (not the default session). - Socket resolution follows the contract:
--sessionwins; elseHERDR_SOCKET_PATH; else the default socket. PassHERDR_SESSION=2codeand an explicitHERDR_SOCKET_PATHso 2code never attaches to the user's default server.
- Use a dedicated session name
-
Probe, capability check, serialized start, readiness timeout
- Probe with
herdr status --jsonplus socket-accept readiness. Do not build a Task 5 NDJSON client (events.subscribe, framed requests, etc.). - Treat version
0.9.0, JSON protocol 22, and endpoint generation 1 as compatible. Distinguish an absent listener from an incompatible one with distinct errors. - Reuse a compatible listener. Start at most one server (serialize startup). Never stop, overwrite, or steal a foreign/incompatible server.
- Bound readiness with a timeout. A timeout is not "compatible".
- Probe with
-
Detached spawn and GUI-exit lifetime
herdr serveris foreground (detached_server_daemon: false); 2code must detach it (setsid/ equivalent) so GUI exit does not take the server.- Spawn from the Task 3 resolved sidecar path. Do not spawn via Tauri's sidecar Command / process manager.
- GUI exit drops client helpers only. Do not call
herdr server stop.
-
Wire without changing Local default behavior
- Split existing
src-tauri/crates/infra/src/herdr.rsinto a module and addprocess.rs. Reuseresolve_sidecar/report_version. - Keep
HerdrStubAdapterfail-closed. Do not call ensure-server from default Local startup insrc-tauri/src/lib.rs. - Runtime selector default remains Local. Do not bind a session or worktree to both backends.
- Split existing
-
Prove with tests
- Dedicated
2codenamespace is never the default session; long XDG paths fall back to a short socket still in that session. - Absent vs incompatible errors stay distinct; a compatible listener is reused; concurrent ensure starts at most one server.
- Detached spawn does not use
herdr server stop; GUI exit releases helpers only; Local startup does not ensure a Herdr listener. - Existing PTY and Task 2 routing tests keep passing. Live checks may skip without a sidecar unless an explicit required flag is set.
- Dedicated
Likely files and directories
- New:
src-tauri/crates/infra/src/herdr/process.rs— namespace, probe, detached start, GUI-exit helper cleanup; no NDJSON client. - Update: split
src-tauri/crates/infra/src/herdr.rsintosrc-tauri/crates/infra/src/herdr/mod.rs. - Update:
src-tauri/crates/service/src/runtime.rs— ensure/release wiring that is not on the Local default path. - Update:
src-tauri/src/lib.rs— GUI-exit helper drop only. - Update if needed:
src-tauri/crates/model/src/error.rs— distinct absent vs incompatible errors. - Reuse, do not duplicate: Task 3 sidecar discovery,
docs/herdr-integration.md, pin.json. - Do not rewrite: Local PTY, Task 2 routing, frontend terminal code.
Estimated changes: 250–450 lines total, additions plus deletions, including tests; excluding generated files and lockfile churn. Material scope growth should become a separate issue.
Acceptance criteria
- [ ] Dedicated 2code namespace, never the user default session.
- [ ] Probe then start at most one detached herdr server; reuse a compatible listener.
- [ ] Incompatible vs absent are distinct errors; never stop or overwrite a foreign/incompatible server.
- [ ] GUI exit drops client helpers only; server stays alive.
- [ ] Default runtime remains Local; no PTY/worktree dual ownership; no NDJSON client.
- [ ] Sidecar spawn is not via Tauri sidecar Command / process manager; GUI exit does not call
herdr server stop. - [ ]
cargo test --workspacefromsrc-tauri(orcargo test --workspace --exclude codewhere GTK is unavailable). Frontend lint, typecheck, and tests apply only if frontend scope is introduced.
Out of scope
- Socket request client, snapshot sync, DB mapping, profile adoption, terminal create/attach, xterm transport, or agent-state from Herdr (Tasks 5–13).
- User-facing runtime health UI or making Herdr the default (Tasks 18/21).
- Closing or implementing contract-gap issues #396–#401, line-count trackers #400/#405, or Task 2 gap #403.
- Updating
docs/herdr-integration.md(Task 1 contract file). - Removing the local PTY runtime or agent detector.
- Changing frontend terminal rendering, restoration, or detection.
- Remote Herdr operation.