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

Task 5: Implement the socket request client

Open AkaraChen opened this issue 2 months ago • 0 comments

Summary

Provide typed Herdr v0.9.0 control requests over native local transport, as required by parent plan #394. Reuse the Task 4 2code endpoint (HerdrEndpoint.socket_path). Do not switch the default runtime, attach terminals, own worktrees, or replace the fail-closed Herdr stub.

Pin remains Herdr v0.9.0, JSON protocol 22, endpoint generation 1. Envelope and fixtures: docs/herdr-integration.md Control API, src-tauri/crates/infra/tests/fixtures/herdr/control-api.json.

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). Checkout base: #406 (task-4-server-discovery). Open contract-gap issues #396–#401, Task 2 gap #403, docs gap #407, and line-count trackers #400/#405/#408 do not block Linux Task 5. Live Windows named-pipe remains #399. Live macOS Unix sockets remain #401.

Work to complete

  1. Native local transport

    • Connect to Task 4’s dedicated 2code socket path. Never the user default session or $XDG_CONFIG_HOME/herdr/herdr.sock.
    • Unix domain sockets are the verified Linux path. Use the same Unix client on macOS at compile time; do not claim macOS executed (#401).
    • Add a Windows named-pipe connect path from documented convention and Task 4’s endpoint string so the crate compiles; do not claim live Windows support (#399). Fail closed on an unknown pipe layout rather than guessing.
    • Do not use herdr-client.sock or herdr terminal session (Task 10 binary/CLI frames).
  2. Framing, correlation, bounds, cancellation, errors

    • Incremental NDJSON: one JSON object per line; read across split writes; reject oversized lines with a structured error.
    • Correlate responses by id. Keep wire types (method, params, result, error.code) inside infra::herdr, not model or handler signatures.
    • Typed envelopes for request, success, error, subscription_started, and later {event, data} lines. Generic request(id, method, params) plus a dedicated subscribe connection. Do not wrap all 102 schema methods.
    • Prove with verified methods: ping, session.snapshot. For subscribe: events.subscribe ack then streamed events. Domain lifecycle (workspace.create, pane close, worktree remove) is not production API in this task.
    • Cancellation: drop/cancel an in-flight request without blocking the process. Closing the GUI still must not stop the server.
  3. Do not auto-replay uncertain mutations

    • Disconnect, timeout, or cancellation while a non-idempotent request is in flight returns a distinct uncertain/disconnected error. The client must not retry workspace.* / worktree.* / tab.* / pane.* / server.stop on its own.
    • Idempotent reads (ping, session.snapshot) may be retried only by the caller, not inside the transport after an uncertain write.
  4. Wire without changing Local default behavior

    • Add src-tauri/crates/infra/src/herdr/transport.rs. Reuse HerdrEndpoint / socket_path from process.rs.
    • Keep HerdrStubAdapter fail-closed. Do not select Herdr by default. Do not call the client from Local startup in src-tauri/src/lib.rs.
    • Do not bind a session or worktree to both backends. Do not call herdr server stop or server.stop.
  5. Prove with tests

    • Local mock socket: fragmented lines, concurrent in-flight ids, peer disconnect, oversize rejection, cancel, ping/session.snapshot envelopes, subscribe ack then event line, no auto-retry of a mutating method after disconnect.
    • Optional live check against Task 4 ensure_server: ping then session.snapshot on the 2code namespace. Skip without sidecar unless an explicit required flag is set. Never target the user default session.
    • Existing PTY and Task 2 routing tests keep passing.

Likely files and directories

  • New: src-tauri/crates/infra/src/herdr/transport.rs — Unix (and cfg Windows pipe) NDJSON client; protocol tests colocated or under crates/infra/tests/.
  • Update: src-tauri/crates/infra/src/herdr/mod.rs — export transport; keep sidecar/process comments accurate.
  • Update if needed: src-tauri/crates/infra/Cargo.toml, src-tauri/crates/model/src/error.rs — structured transport/uncertain errors only.
  • Reuse, do not duplicate: Task 4 process/namespace, Task 3 sidecar, Task 1 control-api.json / pin.
  • Do not rewrite: Local PTY, Task 2 routing, frontend terminal code, 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

  • [ ] Typed NDJSON control client talks to the dedicated 2code endpoint over Unix sockets; Linux ping / session.snapshot match the Task 1 fixtures.
  • [ ] Incremental framing, id correlation, bounded sizes, cancellation, and structured success/error/subscribe envelopes.
  • [ ] Uncertain disconnects are not auto-retried for mutations.
  • [ ] Windows named-pipe code may compile; live Windows is not claimed (#399). macOS live Unix is not claimed (#401).
  • [ ] Default runtime remains Local; Herdr stub stays fail-closed; no PTY/worktree dual ownership; no CLI terminal attach; no 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

  • Snapshot/event projection (Task 6), DB mapping, profile adoption, Herdr terminal create/attach, xterm transport, agent-state (Tasks 6–13).
  • User-facing runtime health UI or making Herdr the default (Tasks 18/21).
  • Closing or implementing contract-gap issues #396–#401, Task 2 gap #403, docs gap #407, or line-count trackers #400/#405/#408.
  • Updating docs/herdr-integration.md.
  • Removing the local PTY runtime or agent detector.
  • Changing frontend terminal rendering, restoration, or detection.
  • Remote Herdr operation.

AkaraChen avatar Sep 15 '26 05:09 AkaraChen