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

Task 7: Persist project-to-runtime associations

Open AkaraChen opened this issue 2 months ago • 0 comments

Summary

Associate existing 2code project/profile/session metadata with Herdr v0.9.0 runtime identities, as required by parent plan #394. Add an additive mapping layer for the dedicated 2code namespace. Do not switch the default runtime, adopt worktrees, create/close Herdr terminals, or replace the fail-closed Herdr terminal adapter.

Pin remains Herdr v0.9.0, JSON protocol 22, endpoint generation 1. Contract mapping: docs/herdr-integration.md External split mapping. Namespace: Task 4 SESSION_NAME (2code) in src-tauri/crates/infra/src/herdr/process.rs. Projection ids: Task 6 RuntimeProjection (workspace_id / tab_id / pane_id). terminal_id is live PTY identity only and must not be the persisted key.

On task-6-runtime-sync, schema.rs still has only projects, profiles, pty_sessions, and project_groups. SessionIdentity remains a 2code session id. A 2code project remains a repository entry. A profile may reference a Herdr workspace_id. A terminal tab may reference a Herdr pane_id. Local worktree_path / folder stay reconciled caches, not an independent workspace authority. Default profiles still point at the original checkout in 2code metadata.

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

Work to complete

  1. Additive mapping schema

    • Add a Diesel migration (new tables, not destructive rewrites of projects / profiles / pty_sessions). Keep historical migrations intact.
    • Store the Herdr server namespace on every mapping row (2code, never the user default session and never a socket path).
    • Persist profile → workspace_id and session → pane_id (include workspace_id on the session row for integrity). Do not persist terminal_id or tab_id as the association key. Do not key anything on workspace/tab/pane labels.
    • Preserve existing project/profile IDs, notes, sort_order / pin fields, and worktree_path. Do not change Local Profile / Project IPC shape; keep mapping types out of the serialized catalog DTOs.
    • Foreign keys: profile_id → profiles, session_id → pty_sessions, both ON DELETE CASCADE. Deleting a 2code row drops the mapping only; it must not imply Herdr workspace.close / worktree.remove / pane.close.
    • If both a profile mapping and a session mapping exist, the session row’s workspace_id must equal that profile’s bound workspace_id.
    • Regenerate src-tauri/crates/model/src/schema.rs via diesel print-schema (see src-tauri/diesel.toml). Do not hand-edit schema.
  2. Repository CRUD and uniqueness

    • Add repo helpers to insert/update/get/list mappings and to look up by 2code id or by Herdr id + namespace.
    • Enforce: one bound workspace_id per profile; one bound profile per (namespace, workspace_id); one bound pane_id per session; one bound session per (namespace, pane_id).
    • Refuse to bind a Herdr id that is already bound to a different 2code row. Return a structured missing/replaced error rather than overwriting.
    • Keep Herdr wire envelopes (method, params, result, event) out of model / handler signatures. Application DTOs only.
  3. Missing and replaced identities

    • Provide a pure reconcile helper that takes stored mappings plus a Task 6 RuntimeProjection (no live subscribe changes, no #413 event-apply work).
    • If the stored workspace_id / pane_id is present in the projection, status is bound.
    • If it is absent, mark missing. Do not search by label, path, or worktree_path to invent a replacement.
    • Replaced is an explicit caller-supplied update (new id in the same namespace) for Task 8 to use later. Silent rematch is a test failure.
    • After a simulated server restart, lookup by pane_id still hits; a new terminal_id on that pane must not break or rewrite the mapping.
  4. Wire without changing Local default behavior

    • Do not start sync or ensure_herdr_listener from Local default startup in src-tauri/src/lib.rs.
    • Keep HerdrStubAdapter fail-closed for create/restore/close/write/resize. Mapping rows must not bind RuntimeRouter identities to Herdr or spawn/kill PTYs.
    • Do not call workspace.* / worktree.* / tab.* / pane.* / server.stop. Do not use herdr-client.sock or herdr terminal session.
    • A mapping is an association cache, not ownership transfer. Local still owns live terminals and worktrees until later tasks.
  5. Prove with tests

    • In-memory SQLite: migration applies; existing fixture projects/profiles/sessions keep ids, notes, and ordering; new tables exist.
    • Uniqueness and cascade: duplicate bound workspace_id / pane_id rejected; deleting a profile/session removes only the mapping row.
    • Reconcile: projection contains id → bound; projection lacks id → missing, not rebound by label; explicit replace updates the id; stale label match is ignored.
    • pane_id survives a new terminal_id; terminal_id is not stored as the key.
    • Existing PTY, Task 2 routing, and Task 6 projection tests keep passing. Local default still does not ensure a Herdr listener or start sync.

Likely files and directories

  • New: src-tauri/migrations/<timestamp>_herdr_runtime_mappings/{up,down}.sql
  • New: src-tauri/crates/model/src/ mapping DTOs (or extend runtime.rs without wire types)
  • New: src-tauri/crates/repo/src/runtime_mapping.rs — CRUD + uniqueness; colocated tests via test_utils.rs
  • New or small update: service reconcile helper next to runtime mapping (not a live sync loop)
  • Update: src-tauri/crates/model/src/schema.rs via diesel print-schema; crates/model/src/lib.rs, crates/repo/src/lib.rs
  • Update if needed: src-tauri/crates/model/src/error.rs — missing/replaced mapping errors only; src-tauri/tests/integration_migrations.rs if a full-schema apply check is the existing pattern
  • Reuse, do not duplicate: Task 6 RuntimeProjection id rules, Task 4 SESSION_NAME, Task 1 mapping table in docs/herdr-integration.md
  • Do not rewrite: Local PTY, Task 2 routing, Task 6 subscribe/apply, frontend terminal code, docs/herdr-integration.md

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

Acceptance criteria

  • [ ] An additive migration stores namespace 2code plus profile→workspace_id and session→pane_id mappings without rewriting existing catalog rows.
  • [ ] Project/profile IDs, notes, and ordering survive; worktree_path remains a cache, not workspace authority.
  • [ ] Identities are workspace_id / pane_id scoped to the dedicated namespace, never display names. terminal_id is not the persisted key.
  • [ ] Missing and replaced identities are explicit; tests prove no silent rebind by label or path.
  • [ ] Uniqueness holds for bound Herdr ids; FK delete drops mapping rows only.
  • [ ] Production mapping is persistence + reconcile against an in-memory projection. No worktree.open/create, no pane lifecycle, no server.stop, no CLI terminal attach.
  • [ ] 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. Mapping rows do not transfer live ownership.
  • [ ] 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

  • Profile/worktree adoption and resumable checkout attach (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, Task 6 event-apply gap #413, or line-count trackers #400/#405/#408/#410/#412.
  • Applying pane.exited / layout.updated / tab.moved in the projection (#413).
  • 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 08:09 AkaraChen