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

Task 14: Create profiles through Herdr worktree management

Open AkaraChen opened this issue 2 months ago • 0 comments

Summary

Delegate new worktree and workspace creation to Herdr v0.9.0, as required by parent plan #394. Replace direct git worktree add on the Herdr-selected path with JSON worktree.create. Translate 2code branch and directory preferences where the pin supports them. Keep 2code.json setup as one explicit 2code-owned execution. Reconcile partial success before inserting local metadata or retrying. Do not switch the default runtime, delete worktrees, create/close 2code terminal tabs, or let Local git and Herdr both create the same checkout.

Pin remains Herdr v0.9.0, JSON protocol 22, endpoint generation 1. Verified create: CLI worktree create --cwd <repo> --branch wt/contract --no-focus --trust-repository creates a linked worktree (worktree.is_linked_worktree: true), opens workspace wN with root tab/pane, and lands under [worktrees].directory when --path is omitted (docs/herdr-integration.md Workspace and worktree lifecycle; herdr_contract.rs). Duplicate branch → worktree_create_failed. JSON worktree.* is outcome-uncertain (outcome_uncertain). Socket rule: at most one of workspace_id or cwd; cwd/path must be absolute. Namespace stays Task 4 2code. Persist profile→workspace with Task 7 bind_profile_workspace. worktree_path stays a reconciled cache of the path Herdr actually created. terminal_id is not persisted. The create root pane is left unbound so Task 9 first-terminal reuse still applies.

Today create_with_db always calls infra::git::worktree_add (git worktree add -b) then execute_scripts for setup_script. create_profile has no RuntimeHandle. HerdrClient::worktree_open must not grow a silent worktree.create. Task 8 adoption remains worktree.open of existing checkouts only.

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), #414 (Task 7, accepted on task-7-runtime-mapping), #417 (Task 8, accepted on task-8-profile-adoption), #419 (Task 9, accepted on task-9-terminal-lifecycle), #421 (Task 10, accepted on task-10-frame-bridge), #423 (Task 11, accepted on task-11-xterm-transport), #425 (Task 12, accepted on task-12-reattach-restore), #427 (Task 13, accepted on task-13-agent-indicators). Checkout base: #427 (task-13-agent-indicators). Open contract-gap issues #396–#401, docs gap #407, Task 6 event-apply gap #413, implemented Task 8/9 gaps #403/#416, and line-count trackers #400/#405/#408/#410/#412/#415/#418/#420/#422/#424/#426 do not block Linux Task 14. Startup-command gap #397 is Task 17; this task must not use pane.send_input / herdr pane run as setup. Live agent CLI parity remains #398.

Work to complete

  1. Herdr-selected profile create via worktree.create

    • Route create_profile through RuntimeHandle the same way delete already does. RuntimeRouter::new stays Local-default. Local-selected create keeps today’s git worktree add -b + setup_script path and must not call worktree.create.
    • Herdr-selected create (tests / later cutover) uses JSON worktree.create on the Task 4 2code socket. Never the user default session or $XDG_CONFIG_HOME/herdr/herdr.sock. A Herdr adapter with no client/endpoint stays fail-closed and must not fall back to Local git worktree add.
    • Do not call ensure_herdr_listener / adoption / sync from Local default startup in src-tauri/src/lib.rs.
    • Sanitize / auto-generate branch names with the existing CJK slug and pr/{city}-{hex} helpers in profile.rs. Pass that branch as branch. Preserve Local semantics: a name that already exists as a local branch or worktree is an error, not Herdr’s “check out the existing branch” behavior. Auto-generated names may retry on worktree_create_failed / already-exists, capped like today.
    • Directory preference: compute the same absolute path as Local (2code.json worktree_dir, then Settings defaultWorktreeDir, then ~/.2code/workspace, plus build_worktree_dir_name). Pass it as absolute JSON path when the pin accepts it, together with branch. Use at most one of cwd (project git dir, absolute) or parent workspace_id (default profile mapping Bound in namespace 2code). Prefer parent workspace_id when Bound so the new worktree groups with the adopted primary; otherwise cwd. trust_repository: true, focus: false. Optional label is display-only.
    • If v0.9.0 JSON ignores or rejects path, persist the returned worktree.path as profiles.worktree_path and do not also git worktree add at the 2code layout path. Dual filesystem ownership is forbidden. If directory translation is unsupported, record it as a gap issue — do not treat Local git as the adapter.
    • After confirmed create: insert the 2code profile (same UUID generated before create so retries share path/branch), bind_profile_workspace with the returned workspace_id in namespace 2code. worktree_path is the checkout Herdr created. Do not persist terminal_id or tab_id. Do not write session_runtime_mappings or bind RuntimeSelector session ids. Leave the root pane unbound for Task 9 first-terminal reuse.
    • Default profiles (is_default) are not created with worktree.create. Do not workspace.create as a checkout-recreation path, worktree.open of a path that does not yet exist, worktree.remove, git worktree add/remove, pane.split, tab.create, or pane.close as part of profile create.
  2. One setup owner, and uncertain / partial outcomes

    • Setup owner is 2code: run execute_scripts for setup_script once in the created checkout after the profile row exists. Do not run Herdr plugin worktree.created hooks, pane.send_input, herdr pane run, or a second Local setup. Retry after a successful insert must not re-run setup. Setup failure does not worktree.remove (Task 15).
    • worktree.create is outcome-uncertain. On HerdrUncertainOutcome, do not auto-replay. Reconcile with session.snapshot, worktree.list, or worktree.open of the intended absolute path (already_open + same workspace_id). Then insert/bind the observed workspace. Do not issue a second worktree.create until that reconcile. Do not assume live worktree.created from the Task 6 projection is sufficient (#413).
    • Persist order: confirmed Herdr checkout + workspace_id → insert profile → bind mapping → setup once. If insert fails after create, retry must worktree.open/list the existing checkout, not create a second worktree. If bind fails after insert, retry bind only. Uniqueness still refuses one workspace_id on two profiles (#416 replace rules stay; this task does not replace_profile_workspace as the happy path).
    • Keep Herdr wire envelopes (method, params, result) inside the Herdr adapter / transport helpers, not model or handler signatures. A typed worktree.create helper on HerdrClient is required; extend WorktreeOpenResult or add a create result with workspace_id + checkout path (not terminal_id).
    • Do not call server.stop. GUI exit still only drops client helpers.
  3. Ownership: one backend per worktree

    • Local create must not write profile_runtime_mappings or call worktree.create.
    • Herdr create must not call git worktree add / worktree_add.
    • A Herdr-created checkout must not be Local-owned; a Local-created checkout must not be Herdr-owned in the same operation. Task 8 adoption remains the way existing Local checkouts join Herdr later.
    • Do not attach observe and control together. Do not --takeover.
  4. Prove with tests

    • Fake client: Herdr-selected create calls worktree.create once with sanitized branch, absolute cwd or parent workspace_id, focus: false, trust_repository: true, and absolute path when used; binds workspace_id; worktree_path matches the returned checkout; no git worktree add; setup_script runs once in that checkout.
    • Uncertain worktree.create: no auto-replay; reconcile binds one workspace_id. Retry after insert-without-bind does not create a second worktree. Duplicate branch → error, not a second profile on the existing branch’s worktree.
    • Local-selected create still uses git worktree add -b, still honors project/global worktree dirs, still runs setup, and does not call worktree.create. Existing profile.rs and integration_project.rs create tests keep passing.
    • Herdr-selected with no client does not spawn a Local worktree. RuntimeRouter::new still selects Local. Default profile creation is unchanged.
    • Root pane from create is not bound as a 2code session. Task 9 first-create reuse tests still pass.
    • Optional live check against Task 4 ensure_server + Task 5 client on the 2code namespace with a disposable repo: create, returned path exists, mapping Bound, setup ran once, duplicate branch fails. Skip without sidecar unless an explicit required flag is set. Never target the user default session. Live fixtures must not delete the user’s real checkouts.
    • bun run lint:check, bun run typecheck, bun run test if frontend/IPC scope is introduced. Rust: cargo test --workspace from src-tauri (or --exclude code where GTK is unavailable). IPC changes use bun run typegen; do not edit src/generated/ by hand.

Likely files and directories

  • Update: src-tauri/crates/service/src/profile.rs — Herdr-selected create; keep Local create_worktree; colocated tests
  • Update: src-tauri/src/handler/profile.rs — pass RuntimeHandle; keep IPC projectId / branchName / defaultWorktreeDir unless a new field is required
  • Update: src-tauri/crates/infra/src/herdr/transport.rs — typed worktree.create; keep server.stop refused; do not auto-replay
  • Update if needed: src-tauri/crates/service/src/runtime/herdr.rs — create helper only; no terminal create/close changes; src-tauri/crates/service/src/runtime.rs — Local default unchanged
  • Reuse, do not duplicate: Task 7 bind_profile_workspace, Task 8 runtime_adoption.rs (worktree.open only), Task 9 unbound root-pane reuse, WorktreeSettings.tsx / hooks.ts defaultWorktreeDir (no new Herdr UI)
  • Do not rewrite: Local PTY, Task 10 CLI attach, Task 11/12/13 terminal/agent paths, delete_profile git teardown (Task 15), docs/herdr-integration.md

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

  • [ ] Herdr-selected new profiles create the git worktree and Herdr workspace via JSON worktree.create on namespace 2code. Branch slugs match today’s sanitizer/auto-name. worktree_path is the checkout Herdr created. Mapping is workspace_id, never a display name. terminal_id is not persisted. The create root pane is not bound as a 2code session.
  • [ ] Directory and parent grouping preferences are translated where the pin supports them (path, cwd xor parent workspace_id). Unsupported directory translation is not papered over with Local git worktree add.
  • [ ] setup_script runs once under 2code after persist; Herdr is not a second setup owner. Uncertain create is reconciled before retry; tests prove no auto-replay and no duplicate worktrees/profiles.
  • [ ] Default runtime remains Local. Local create still uses git worktree add -b and still works. No 2code session or worktree is owned by both backends. Profile create is not started from Local default startup. Adapter choice uses RuntimeRouter selected backend.
  • [ ] Production create does not call worktree.remove, git worktree add on the Herdr path, workspace.create as recreation, tab.create/pane.close/pane.split, pane.send_input, server.stop, --takeover, or a reimplemented herdr-client.sock protocol.
  • [ ] Duplicate existing branch names fail closed instead of checking out someone else’s branch. Default profiles are not created via worktree.create.
  • [ ] Windows named-pipe code may compile; live Windows is not claimed (#399). macOS live Unix is not claimed (#401). Worktree/project cleanup remains Task 15 (#403 session teardown is already on Task 9).
  • [ ] cargo test --workspace from src-tauri (or cargo test --workspace --exclude code where GTK is unavailable). Frontend lint, typecheck, and tests apply if frontend/IPC scope is introduced. bun run typegen for IPC; do not edit src/generated/ by hand.

Out of scope

  • Routing worktree/project cleanup through Herdr worktree.remove (Task 15), including dirty-file / unpushed-commit delete UX.
  • Git/editor path reconciliation beyond writing the create-time worktree_path cache (Task 16).
  • Startup-command / shell / env mapping (Task 17 / #397).
  • Runtime health UI (Task 18), legacy-session migration (Task 19), acceptance cutover (Task 20), or making Herdr the default (Task 21).
  • Deleting detector/ (Task 22) or the local PTY runtime (Task 23).
  • Closing or implementing contract-gap issues #396–#401, docs gap #407, Task 6 event-apply gap #413, implemented gaps #403/#416, or line-count trackers #400/#405/#408/#410/#412/#415/#418/#420/#422/#424/#426.
  • Applying pane.exited / layout.updated / tab.moved in the projection (#413).
  • Updating docs/herdr-integration.md.
  • Creating or attaching 2code terminal tabs as part of profile create (Task 9/10 already own lifecycle/attach).
  • Remote Herdr operation.

Standing constraints

  • No default-runtime switch.
  • Existing Local PTY/agent path stays working until the Herdr path passes acceptance.
  • Never let both runtimes own the same terminal or worktree.

AkaraChen avatar Sep 15 '26 17:09 AkaraChen