Task 14: Create profiles through Herdr worktree management
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
-
Herdr-selected profile create via
worktree.create- Route
create_profilethroughRuntimeHandlethe same way delete already does.RuntimeRouter::newstays Local-default. Local-selected create keeps today’sgit worktree add -b+ setup_script path and must not callworktree.create. - Herdr-selected create (tests / later cutover) uses JSON
worktree.createon the Task 42codesocket. 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 Localgit worktree add. - Do not call
ensure_herdr_listener/ adoption / sync from Local default startup insrc-tauri/src/lib.rs. - Sanitize / auto-generate branch names with the existing CJK slug and
pr/{city}-{hex}helpers inprofile.rs. Pass that branch asbranch. 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 onworktree_create_failed/ already-exists, capped like today. - Directory preference: compute the same absolute path as Local (
2code.jsonworktree_dir, then SettingsdefaultWorktreeDir, then~/.2code/workspace, plusbuild_worktree_dir_name). Pass it as absolute JSONpathwhen the pin accepts it, together withbranch. Use at most one ofcwd(project git dir, absolute) or parentworkspace_id(default profile mapping Bound in namespace2code). Prefer parentworkspace_idwhen Bound so the new worktree groups with the adopted primary; otherwisecwd.trust_repository: true,focus: false. Optionallabelis display-only. - If v0.9.0 JSON ignores or rejects
path, persist the returnedworktree.pathasprofiles.worktree_pathand do not alsogit worktree addat 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_workspacewith the returnedworkspace_idin namespace2code.worktree_pathis the checkout Herdr created. Do not persistterminal_idortab_id. Do not writesession_runtime_mappingsor bindRuntimeSelectorsession ids. Leave the root pane unbound for Task 9 first-terminal reuse. - Default profiles (
is_default) are not created withworktree.create. Do notworkspace.createas a checkout-recreation path,worktree.openof a path that does not yet exist,worktree.remove,git worktree add/remove,pane.split,tab.create, orpane.closeas part of profile create.
- Route
-
One setup owner, and uncertain / partial outcomes
- Setup owner is 2code: run
execute_scriptsforsetup_scriptonce in the created checkout after the profile row exists. Do not run Herdr pluginworktree.createdhooks,pane.send_input,herdr pane run, or a second Local setup. Retry after a successful insert must not re-run setup. Setup failure does notworktree.remove(Task 15). worktree.createis outcome-uncertain. OnHerdrUncertainOutcome, do not auto-replay. Reconcile withsession.snapshot,worktree.list, orworktree.openof the intended absolute path (already_open+ sameworkspace_id). Then insert/bind the observed workspace. Do not issue a secondworktree.createuntil that reconcile. Do not assume liveworktree.createdfrom 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 mustworktree.open/list the existing checkout, not create a second worktree. If bind fails after insert, retry bind only. Uniqueness still refuses oneworkspace_idon two profiles (#416 replace rules stay; this task does notreplace_profile_workspaceas the happy path). - Keep Herdr wire envelopes (
method,params,result) inside the Herdr adapter / transport helpers, notmodelor handler signatures. A typedworktree.createhelper onHerdrClientis required; extendWorktreeOpenResultor add a create result withworkspace_id+ checkout path (notterminal_id). - Do not call
server.stop. GUI exit still only drops client helpers.
- Setup owner is 2code: run
-
Ownership: one backend per worktree
- Local create must not write
profile_runtime_mappingsor callworktree.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.
- Local create must not write
-
Prove with tests
- Fake client: Herdr-selected create calls
worktree.createonce with sanitizedbranch, absolutecwdor parentworkspace_id,focus: false,trust_repository: true, and absolutepathwhen used; bindsworkspace_id;worktree_pathmatches the returned checkout; nogit worktree add; setup_script runs once in that checkout. - Uncertain
worktree.create: no auto-replay; reconcile binds oneworkspace_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 callworktree.create. Existingprofile.rsandintegration_project.rscreate tests keep passing. - Herdr-selected with no client does not spawn a Local worktree.
RuntimeRouter::newstill 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 the2codenamespace 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 testif frontend/IPC scope is introduced. Rust:cargo test --workspacefromsrc-tauri(or--exclude codewhere GTK is unavailable). IPC changes usebun run typegen; do not editsrc/generated/by hand.
- Fake client: Herdr-selected create calls
Likely files and directories
- Update:
src-tauri/crates/service/src/profile.rs— Herdr-selected create; keep Localcreate_worktree; colocated tests - Update:
src-tauri/src/handler/profile.rs— passRuntimeHandle; keep IPCprojectId/branchName/defaultWorktreeDirunless a new field is required - Update:
src-tauri/crates/infra/src/herdr/transport.rs— typedworktree.create; keepserver.stoprefused; 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 8runtime_adoption.rs(worktree.openonly), Task 9 unbound root-pane reuse,WorktreeSettings.tsx/hooks.tsdefaultWorktreeDir(no new Herdr UI) - Do not rewrite: Local PTY, Task 10 CLI attach, Task 11/12/13 terminal/agent paths,
delete_profilegit 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.createon namespace2code. Branch slugs match today’s sanitizer/auto-name.worktree_pathis the checkout Herdr created. Mapping isworkspace_id, never a display name.terminal_idis 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,cwdxor parentworkspace_id). Unsupported directory translation is not papered over with Localgit worktree add. - [ ]
setup_scriptruns 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 -band still works. No 2code session or worktree is owned by both backends. Profile create is not started from Local default startup. Adapter choice usesRuntimeRouterselected backend. - [ ] Production create does not call
worktree.remove,git worktree addon the Herdr path,workspace.createas recreation,tab.create/pane.close/pane.split,pane.send_input,server.stop,--takeover, or a reimplementedherdr-client.sockprotocol. - [ ] 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 --workspacefromsrc-tauri(orcargo test --workspace --exclude codewhere GTK is unavailable). Frontend lint, typecheck, and tests apply if frontend/IPC scope is introduced.bun run typegenfor IPC; do not editsrc/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_pathcache (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.movedin 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.