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

Task 2: Make Herdr the default runtime

Open AkaraChen opened this issue 2 months ago • 1 comments

Summary

Make Herdr the production default runtime for parent plan #436. The additive stack plus Task 1 (#437 on task-1-client-mode-contract) still constructs RuntimeRouter::new as Local, leaves RuntimeBackend defaulting to Local, and does not start a Herdr listener from GUI setup (ensure_herdr_listener is explicitly forbidden on the Local-default path). This task inverts that default.

New sessions and GUI startup talk to Herdr first. Local remains only as an explicit fallback behind a flag/env until #436 Task 9 deletes it. Do not silently fail over to Local. Do not treat sqlite profiles as source of truth. Do not redesign project/profile/Git/file-tree/notes/tab interaction.

The Task 1 contract (docs/herdr-integration.md) marks #436 Task 2 proceed on Linux x86_64: no new Herdr capability. Reuse the existing sidecar pin (v0.9.0), ensure_herdr_listener, JSON client, and Herdr adapter. Do not reintroduce the #394 runtime boundary (#402 leftover).

Dependencies: Parent plan #436. Prerequisite: #437 (Task 1, accepted on task-1-client-mode-contract). Open #438 is Task 1 line-count tracking only — not a blocker and not work for this branch. Open #394 issues #395–#434 do not block this switch and must not be implemented or closed here.

Work to complete

  1. Flip the production default to Herdr

    • RuntimeBackend: #[default] becomes Herdr. Update default_backend_is_local / herdr_is_not_the_default_backend.
    • RuntimeSelector::default and RuntimeRouter::new select Herdr. Keep with_backend for tests and for the Local fallback.
    • selected_backend() / discovery().selected_backend report Herdr in production.
    • create_session already routes through selector.default_backend(); with the flip, new tabs go to the Herdr adapter. They must not spawn a Local PTY for that id.
    • create_with_runtime already branches on selected_backend(). Production profile create therefore uses the existing Herdr worktree path. It may still INSERT sqlite profiles on this branch (stopping those writes is Task 4). Fail closed if the worktree client is missing — do not git worktree add as a silent Local fallback.
    • Update docs/herdr-integration.md: this branch has switched the default; Local is explicit fallback until Task 9. Do not bump the v0.9.0 pin.
  2. GUI startup talks to Herdr first

    • bridge::build_runtime / lib.rs setup: when Herdr is selected, resolve the pinned sidecar (try_resolve_sidecar / resolve_sidecar), call ensure_herdr_listener on the dedicated 2code namespace (never the user default session), connect the JSON client, and inject terminal + worktree + CLI attach into the Herdr adapter so create/list/write/resize can succeed. Today HerdrStubAdapter::new() is fail-closed with no client — flipping the enum without this wiring would make every new tab error.
    • Combine clients on one adapter (today with_terminal_client_and_cli and with_worktree_client are separate). Missing that would break Herdr-default profile create.
    • Invert tests that currently require lib.rs not to contain ensure_herdr_listener / HerdrClient::connect on the Local-default path (e.g. gui_exit_drops_herdr_helpers_without_stopping_the_server). GUI exit must still only release_herdr_client_helpers — never herdr server stop.
    • Do not start HerdrRuntimeSync / events.subscribe here (Tasks 3/6). Do not launch/adopt workspaces on project open (Task 10).
  3. Local as explicit fallback only

    • Process env/flag, e.g. TWOCODE_RUNTIME=local (match existing TWOCODE_PROFILE style). Not a Settings control, not sqlite, not a GUI runtime picker (#394 Task 18 leftover).
    • With the flag: construct RuntimeRouter as Local, do not call ensure_herdr_listener, new sessions spawn Local PTYs as today. Herdr adapter may exist unselected. Dual-ownership bind still refuses.
    • No silent failover: if Herdr is the selected default and the sidecar is missing or the namespace is absent/incompatible, keep Herdr selected and fail Herdr ops with existing HerdrServerAbsent / HerdrServerIncompatible (or equivalent). Do not rewrite the selector to Local. The app may still show the sqlite project catalog.
    • Named behavior exception (not a click/label redesign): creating a terminal when Herdr is down surfaces that backend error instead of opening a Local PTY. Same New Tab control.
  4. Ownership and fail-closed

    • Keep RuntimeSelector::bind dual-backend refusal. Local must never own a worktree/workspace Herdr already owns. backend_for on an unbound id follows the new default (Herdr), including get_session_backend. Update the frontend comment in terminalTransport.ts (“Unbound ids are Local”). Keep resolving transport via per-session get_session_backend, not a new discovery UI.
    • list_project_sessions already lists only Herdr when the default is Herdr; keep that. Do not merge leftover Local PTYs into the Herdr-default list.
    • Do not write a sqlite worktree_path / mapping row that can disagree with live Herdr and still win. Existing mapping writes from the Herdr adapter may continue until Tasks 5–6; do not add new authority caches.
  5. Prove the switch without swallowing later tasks

    • Colocated tests: production constructor/discovery is Herdr; Local flag selects Local and does not ensure a listener; Herdr-default create_session does not increment live Local PTY count; dual bind still errors; Herdr-down does not spawn Local.
    • Invert or parameterize tests that hard-assert Local as production default (selector_defaults_to_local, router_production_constructor_selects_local, local_startup_does_not_ensure_herdr_*, profile/project tests that assume RuntimeRouter::new is Local). Local-path tests must pass through with_backend(Local) / the env flag.
    • Existing Herdr adapter and Local PTY suites must keep passing. If the line count grows past the estimate, open a tracking issue; do not fold Task 3+ into this branch.

Likely files and directories

  • Update: src-tauri/crates/model/src/runtime.rs — enum default + tests.
  • Update: src-tauri/crates/service/src/runtime.rs — RuntimeRouter::new, RuntimeSelector::default, env/flag parse, startup tests that currently lock Local-default.
  • Update: src-tauri/crates/service/src/runtime/herdr.rs — combined terminal+worktree+CLI constructor if missing; tests that assume local_default_router.
  • Update: src-tauri/src/bridge.rs, src-tauri/src/lib.rs — Herdr-default build_runtime calls ensure_herdr_listener and injects clients; Local flag skips that.
  • Update: docs/herdr-integration.md — Task 2 done on this branch; Local is explicit fallback.
  • Update if comments/guards mention unbound=Local: src/features/terminal/lib/terminalTransport.ts (and its test if the comment is asserted).
  • Update tests that construct RuntimeRouter::new expecting Local: src-tauri/crates/service/src/profile.rs, src-tauri/crates/service/src/project.rs, src-tauri/tests/integration_pty_db.rs.
  • Do not change: Diesel migrations, profiles drop, mapping-table deletion, sidebar profile list source, Git/file-tree path consumers, HerdrRuntimeSync startup, Local adapter internals, PTY spawn implementation, sidecar pin.

Estimated changes: 300–550 lines total, additions plus deletions, including tests; excluding generated files, lockfile churn and binary artifacts. Material scope growth should become a separate issue.

Acceptance criteria

  • [ ] Production default is Herdr: RuntimeBackend::default(), RuntimeSelector::default(), and RuntimeRouter::new select Herdr; discovery.selected_backend is herdr.
  • [ ] GUI Herdr-default startup resolves pinned Herdr v0.9.0, ensure_herdr_listener on namespace 2code (not the user default session), and injects JSON terminal + worktree + CLI attach so new sessions talk to Herdr first.
  • [ ] New create_session (and Herdr-selected create_with_runtime) do not spawn a Local PTY or local git worktree add for that identity. Dual-backend bind still refuses.
  • [ ] Local is reachable only via an explicit env/flag (documented, e.g. TWOCODE_RUNTIME=local). Not a Settings toggle. With the flag, startup does not ensure_herdr_listener and new sessions are Local.
  • [ ] Herdr-down (absent sidecar / absent or incompatible namespace) does not flip the default to Local. Ops fail closed with existing distinct absent vs incompatible errors. Creating a terminal then errors instead of opening a Local PTY (named exception; same clicks/labels).
  • [ ] sqlite profiles / mapping tables / pty_sessions are not dropped or made read-only here. Profile list UI still may read sqlite (Task 3). Git/file-tree paths are not retargeted (Task 7). No launch/adopt on project open (Task 10). No Local PTY deletion (Task 9).
  • [ ] Frontend still uses per-session get_session_backend for transport; unbound ids follow the new default on the backend. No runtime-picker UI.
  • [ ] Tests: cargo test --workspace from src-tauri (or cargo test --workspace --exclude code where GTK is unavailable). Frontend lint/typecheck/tests only if frontend scope is introduced. bun run typegen if IPC signatures change; do not edit src/generated/ by hand.
  • [ ] GUI exit still drops client helpers only (no server stop). Pin stays v0.9.0.
  • [ ] Another developer can run with the Local flag and get the old default, and run without it and get Herdr-first create.

Out of scope

  • Deriving the sidebar/profile switcher from worktree.list / snapshot (Task 3).
  • Create/delete profile without INSERT/DELETE profiles (Task 4).
  • Stopping mapping / worktree_path writes or one-shot import (Task 5).
  • Session restore from session.snapshot as the store; dropping pty_sessions / session_runtime_mappings as authority (Task 6). Starting HerdrRuntimeSync / events.subscribe.
  • Pointing Git, file tree, and watchers at live Herdr cwd (Task 7).
  • Dropping sqlite profiles (Task 8), deleting the Local PTY runtime (Task 9), launch/adopt on project open (Task 10).
  • Implementing or closing #394 issues #395–#434 (including leftover Tasks 18–23, gap issues, and line-count trackers) or #438.
  • Remote Herdr machines. User-facing runtime health UI. Changing the v0.9.0 pin.
  • Opening a pull request (parent plan: no PR until #436 is complete).

Standing constraints

  • Do not treat sqlite profiles as source of truth. Do not add a path cache that can disagree with live Herdr and still win.
  • projects (and project groups / notes-as-2code-data) remain 2code sqlite.
  • Join key remains path ↔ live Herdr cwd/worktree group.
  • Switching the default runtime is in scope. Deleting Local PTY / profiles rows is in scope for Tasks 8–9, not this task.
  • Never let Local and Herdr own the same worktree.
  • Preserve existing 2code interaction (project list, profile switcher, create/delete profile, Git, file tree, notes, terminal tabs). Do not redesign those flows. UX change is allowed only when Herdr cannot back the old clicks/labels; the only named exception is fail-closed errors instead of a silent Local shell when Herdr is down.
  • One task; stack on task-1-client-mode-contract. Do not open a PR until the parent plan is complete.

AkaraChen avatar Sep 16 '26 02:09 AkaraChen

Line count on task-2-herdr-default-runtime versus task-1-client-mode-contract is 624 insertions / 73 deletions (plus a small frontend comment/test follow-up), past the 300–550 estimate. Tracking: #440. Growth is tests, docs, and fail-closed GUI attach — not Task 3+ scope.

AkaraChen avatar Sep 16 '26 03:09 AkaraChen