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

Task 6: Synchronize runtime snapshots and events

Open AkaraChen opened this issue 2 months ago • 0 comments

Summary

Build a recoverable in-memory projection of Herdr v0.9.0 runtime state, as required by parent plan #394. Reuse the Task 5 NDJSON client (HerdrClient, HerdrSubscription) on the Task 4 2code endpoint. Do not switch the default runtime, attach terminals, own worktrees, persist mappings, or replace the fail-closed Herdr terminal adapter.

Pin remains Herdr v0.9.0, JSON protocol 22, endpoint generation 1. Bootstrap contract: docs/herdr-integration.md Control API and Later-task capabilities (subscribe ack + live events verified; subscribe → snapshot → drain is documented, not race-tested). Envelope fixtures: src-tauri/crates/infra/tests/fixtures/herdr/control-api.json. Subscribe types: schema-excerpt.json.

Herdr’s documented bootstrap (must implement, and must test — Task 1 did not race-test it): open events.subscribe on a dedicated connection and wait for subscription_started; buffer that stream while calling session.snapshot; install the snapshot; apply buffered {event, data} lines in order; continue streaming. After disconnect or a stale cache, resubscribe and replace the projection from a new snapshot. Do not merge pre-disconnect events into the rebuilt cache.

Subscribe types use dots (pane.created); emitted event names use underscores (pane_created). Subscriptions do not replay history.

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

Work to complete

  1. Subscription acknowledgement and buffered bootstrap

    • Connect only to Task 4’s dedicated 2code socket. Never the user default session or $XDG_CONFIG_HOME/herdr/herdr.sock.
    • Use HerdrSubscription for events.subscribe and HerdrClient::session_snapshot for the one-shot snapshot. Keep wire types (method, params, result, event) inside the Herdr adapter / sync module, not model or handler signatures.
    • Wait for subscription_started before taking the snapshot. Buffer events that arrive during the snapshot round-trip. Install result.snapshot (workspaces, tabs, panes, layouts, agents) as the authoritative cache, then apply the buffer in order and keep applying live events.
    • Subscribe to the lifecycle set needed to keep that cache recoverable: workspace.created / updated / renamed / moved / reordered / closed; tab.created / closed / renamed / moved; pane.created / updated / closed / exited / moved; layout.updated; worktree.created / opened / removed. Do not subscribe to pane.scroll_changed or pane.output_matched. Agent events (pane.agent_detected, pane.agent_status_changed) may be stored on the projection if present; do not emit notifications or stop the local detector (Task 13).
    • Production sync is read-only. Do not call workspace.* / worktree.* / tab.* / pane.* / server.stop from the sync loop. Tests may drive a mock (or optional live CLI) to emit events.
  2. Snapshot reconciliation and identity rules

    • Key objects by workspace_id / tab_id / pane_id. Never by display name or label. terminal_id is live PTY identity only; a rebuild after server restart is expected to show new terminal_ids for restored pane_ids.
    • Apply events idempotently onto the installed snapshot (buffered pane_created for a pane already in the snapshot must not duplicate it).
    • Drop objects that a snapshot or a *_closed / worktree.removed / pane.exited event says are gone. If an external pane disappears, drop it — do not recreate it. Split layout stays flattened: keep every pane; replace cached layouts for the same workspace_id+tab_id on layout_updated; do not reconstruct Herdr’s BSP grid.
    • Stale events (previous subscription after reconnect, or events for ids absent from the new snapshot with no matching create in the current buffer) must not resurrect removed objects.
  3. Resubscribe after disconnection

    • On subscribe/request disconnect, mark the projection stale, drop the old stream, resubscribe, take a new snapshot, and replace the cache. Do not replay or merge events from the previous connection.
    • GUI exit still only drops client helpers (release_herdr_client_helpers), including the subscribe connection. Do not call herdr server stop or server.stop.
    • Do not start this loop from default Local startup in src-tauri/src/lib.rs. Provide an explicit start/stop used by tests (and later Herdr-selected startup). Closing the GUI must not stop the server.
  4. Wire without changing Local default behavior

    • Add src-tauri/crates/service/src/runtime_sync.rs. Keep HerdrStubAdapter fail-closed for TerminalRuntime create/restore/close/write/resize. The adapter may hold or query the projection; it must not start owning PTYs or worktrees.
    • A small sink trait (implemented in src-tauri/src/bridge.rs) may observe projection updates for later Task 18. Do not add frontend IPC, health UI, or generated bindings in this task.
    • Do not bind a 2code session or worktree to both backends. Projection ids are Herdr ids; mapping onto 2code project/profile/session rows is Task 7.
    • Do not use herdr-client.sock or herdr terminal session (Task 10).
  5. Prove with tests

    • Local mock socket: subscribe ack → event during snapshot → install + drain without duplicates; buffered close / snapshot-without-object does not resurrect; reconnect drops stale events from the previous stream; externally removed pane/workspace is dropped; objects are not keyed by label.
    • Optional live check against Task 4 ensure_server + Task 5 client on the 2code namespace: subscribe, snapshot, observe one lifecycle event (test-only create). 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. Local default still does not ensure a Herdr listener or start sync.

Likely files and directories

  • New: src-tauri/crates/service/src/runtime_sync.rs — bootstrap, reconcile, reconnect; colocated tests.
  • Update: src-tauri/crates/service/src/runtime/herdr.rs — hold/query projection; keep TerminalRuntime fail-closed.
  • Update: src-tauri/crates/service/src/lib.rs, src-tauri/crates/service/src/runtime.rs — export start/stop that is not on the Local default path.
  • Update: src-tauri/src/bridge.rs — optional projection sink; do not start sync from build_runtime.
  • Update if needed: src-tauri/crates/model/src/error.rs — structured sync/stale errors only. Do not put Herdr method names or wire envelopes in model / handlers.
  • Reuse, do not duplicate: Task 5 transport.rs, Task 4 process/namespace, Task 1 control-api.json / schema excerpt / pin.
  • Do not rewrite: Local PTY, Task 2 routing, frontend terminal code, docs/herdr-integration.md.

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

Acceptance criteria

  • [ ] A recoverable in-memory projection is built with subscribe acknowledgement, buffered bootstrap, snapshot install, ordered event apply, and live updates, using the Task 5 client on the dedicated 2code endpoint.
  • [ ] Disconnect/resubscribe replaces the projection from a new snapshot; stale events from the previous stream are not applied.
  • [ ] Tests cover events arriving during bootstrap, stale events, and externally removed objects (drop, do not recreate).
  • [ ] Identities are workspace_id / tab_id / pane_id, never display names. Flattened pane listing is preserved; BSP layout is not reconstructed.
  • [ ] Production sync is read-only. No server.stop. No CLI terminal attach. No DB mapping, profile adoption, or Herdr terminal create/close.
  • [ ] Default runtime remains Local. Herdr TerminalRuntime stays fail-closed. Local PTY/agent path still works. No session or worktree is owned by both backends. Sync is not started from Local default startup.
  • [ ] Windows named-pipe code may compile; live Windows is not claimed (#399). macOS live Unix is not claimed (#401).
  • [ ] 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. IPC changes (none expected) would use bun run typegen; do not edit src/generated/ by hand.

Out of scope

  • Database mapping (Task 7), profile/worktree adoption (Task 8), Herdr terminal create/close (Task 9), live frame bridge (Task 10), xterm transport (Task 11), reattachment UI (Task 12), agent notifications (Task 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/#410.
  • 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 06:09 AkaraChen