feat: keep tmux sessions visible when a worktree's HEAD diverges#17
Merged
Conversation
codeherd could only create worktrees from branches the local clone already knew about, and creation started from whatever state that clone happened to be in. Reviewing a teammate's pushed branch meant fetching and adding a worktree by hand, and even an ordinary new worktree could be based on a stale default branch. A new tracking-creation path fetches a remote branch and adds a worktree whose local branch tracks it. The ref is parsed as [<remote>/]<branch> with origin as the default, so both "feat-x" and "upstream/feat-x" work while branch names containing slashes stay intact. When the derived local name would collide with an existing branch, creation refuses rather than silently reusing it, and a failed fetch of the requested branch is fatal — there is nothing to check out. Every creation path now freshens its source before branching. The fetch is best-effort and never aborts an ordinary creation, but when a local source branch exists it is fast-forwarded with --ff-only and used as the base point, so the worktree reflects current upstream state without ever dropping un-pushed local commits. Local-only refs, tags and SHAs skip the fetch and are used verbatim. On the CLI, create worktree gains --track, mutually exclusive with --from, and the branch argument becomes optional: when omitted the local name is derived from the remote branch. Shell completion suggests the project's remote-tracking branches. In the TUI, r is rebound from manual refresh to a remote-branch picker. The dashboard already auto-refreshes every few seconds, so the manual binding was redundant; r now fetches all remotes and opens a filterable list, and selecting a branch hands off to the existing create-worktree form, pre-filled and routed to the tracking path. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
56bd285 to
7dadb68
Compare
The TUI dashboard and `ch list worktree` correlated each worktree to its tmux session by the worktree's live git branch. That key is wrong the moment HEAD moves off the branch the worktree was created for — a rebase in progress leaves a detached HEAD, and checking out another branch swaps the name entirely. In both cases the live branch no longer matches the session's frozen canonical name, so a running agent silently disappears from the dashboard and the worktree listing, exactly when the user most needs to find it. Correlation now keys off a stable identity branch rather than the live one. For worktrees under `<clone>__worktrees/` that identity is the folder base name, which equals the flattened branch the worktree was created for; for the main clone dir it is the configured default branch, falling back to the live branch when none is set. Feeding this identity into SessionName reproduces the session's original name exactly, so the fix works retroactively for sessions that already exist. The list still shows where HEAD actually points. A worktree whose HEAD diverged is annotated with a live-state hint — `detached` mid-rebase, or `on <branch>` when a different branch is checked out — parsed from the `detached` line of `git worktree list --porcelain`. To display the real branch the session belongs to rather than a flattened folder name, a new `@codeherd_branch` tmux option records the exact pretty branch at session start and is read back during listing. Pre-upgrade sessions lack that option, so display falls back to the configured default (clone dir) and then the folder name.
7dadb68 to
9f38907
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Keeps a codeherd worktree's tmux session visible in the TUI dashboard and
ch list worktreewhen its HEAD diverges from the branch it was created for — a rebase in progress (detached HEAD) or a different branch checked out.The dashboard correlated each worktree to its session by the worktree's live git branch. That key breaks the moment HEAD moves off the branch the worktree was created for: the live branch no longer matches the session's frozen canonical name, so a running agent silently disappears from the dashboard and the listing — exactly when the user most needs to find it.
Correlation now keys off a stable identity branch instead:
<clone>__worktrees/→ folder base name (the flattened branch the worktree was created for).Feeding this identity into
SessionNamereproduces the session's original name exactly, so the fix works retroactively for sessions that already exist.The list still shows where HEAD actually points, via a live-state hint:
detachedmid-rebase, oron <branch>when a different branch is checked out (parsed from thedetachedline ofgit worktree list --porcelain). A new@codeherd_branchtmux option records the exact pretty branch at session start and is read back during listing; pre-upgrade sessions without the option fall back to the configured default and then the folder name.Commits
601fb3d— check out remote branches into tracking worktrees (pre-existing work on this branch).docs/.Verification
make checkpasses — coverage 81.6% (≥ 80% gate), integration tests green, lint clean, build OK. Each layer (semconv,worktree,tmux,session,tui) was built test-first.🤖 Generated with Claude Code