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

Task 4: Implement server discovery and startup

Open AkaraChen opened this issue 2 months ago • 0 comments

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

  1. 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.sock and can overflow sun_path (104 bytes). If the named path would overflow, fall back to a short socket still in the 2code session (not the default session).
    • Socket resolution follows the contract: --session wins; else HERDR_SOCKET_PATH; else the default socket. Pass HERDR_SESSION=2code and an explicit HERDR_SOCKET_PATH so 2code never attaches to the user's default server.
  2. Probe, capability check, serialized start, readiness timeout

    • Probe with herdr status --json plus 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".
  3. Detached spawn and GUI-exit lifetime

    • herdr server is 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.
  4. Wire without changing Local default behavior

    • Split existing src-tauri/crates/infra/src/herdr.rs into a module and add process.rs. Reuse resolve_sidecar / report_version.
    • Keep HerdrStubAdapter fail-closed. Do not call ensure-server from default Local startup in src-tauri/src/lib.rs.
    • Runtime selector default remains Local. Do not bind a session or worktree to both backends.
  5. Prove with tests

    • Dedicated 2code namespace 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.

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.rs into src-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 --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.

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.

AkaraChen avatar Sep 15 '26 04:09 AkaraChen