Task 7: Persist project-to-runtime associations
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
-
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_idand session →pane_id(includeworkspace_idon the session row for integrity). Do not persistterminal_idortab_idas the association key. Do not key anything on workspace/tab/pane labels. - Preserve existing project/profile IDs, notes,
sort_order/ pin fields, andworktree_path. Do not change LocalProfile/ProjectIPC shape; keep mapping types out of the serialized catalog DTOs. - Foreign keys:
profile_id→profiles,session_id→pty_sessions, bothON DELETE CASCADE. Deleting a 2code row drops the mapping only; it must not imply Herdrworkspace.close/worktree.remove/pane.close. - If both a profile mapping and a session mapping exist, the session row’s
workspace_idmust equal that profile’s boundworkspace_id. - Regenerate
src-tauri/crates/model/src/schema.rsviadiesel print-schema(seesrc-tauri/diesel.toml). Do not hand-edit schema.
- Add a Diesel migration (new tables, not destructive rewrites of
-
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_idper profile; one bound profile per(namespace, workspace_id); one boundpane_idper 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 ofmodel/ handler signatures. Application DTOs only.
-
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_idis present in the projection, status is bound. - If it is absent, mark missing. Do not search by label, path, or
worktree_pathto 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_idstill hits; a newterminal_idon that pane must not break or rewrite the mapping.
- Provide a pure reconcile helper that takes stored mappings plus a Task 6
-
Wire without changing Local default behavior
- Do not start sync or
ensure_herdr_listenerfrom Local default startup insrc-tauri/src/lib.rs. - Keep
HerdrStubAdapterfail-closed for create/restore/close/write/resize. Mapping rows must not bindRuntimeRouteridentities to Herdr or spawn/kill PTYs. - Do not call
workspace.*/worktree.*/tab.*/pane.*/server.stop. Do not useherdr-client.sockorherdr terminal session. - A mapping is an association cache, not ownership transfer. Local still owns live terminals and worktrees until later tasks.
- Do not start sync or
-
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_idrejected; 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_idsurvives a newterminal_id;terminal_idis 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 extendruntime.rswithout wire types) - New:
src-tauri/crates/repo/src/runtime_mapping.rs— CRUD + uniqueness; colocated tests viatest_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.rsviadiesel 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.rsif a full-schema apply check is the existing pattern - Reuse, do not duplicate: Task 6
RuntimeProjectionid rules, Task 4SESSION_NAME, Task 1 mapping table indocs/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
2codeplus profile→workspace_idand session→pane_idmappings without rewriting existing catalog rows. - [ ] Project/profile IDs, notes, and ordering survive;
worktree_pathremains a cache, not workspace authority. - [ ] Identities are
workspace_id/pane_idscoped to the dedicated namespace, never display names.terminal_idis 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, noserver.stop, no CLI terminal attach. - [ ] Default runtime remains Local. Herdr
TerminalRuntimestays 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 --workspacefromsrc-tauri(orcargo test --workspace --exclude codewhere GTK is unavailable). Frontend lint, typecheck, and tests apply only if frontend scope is introduced. IPC changes (none expected) would usebun run typegen; do not editsrc/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.movedin 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.