Skip to content

Run sessions in git worktrees #79

Closed

Description

@m4rocks

Work on several tasks at once: a session can be switched into a git worktree and back, the way it switches branches, so each task has its own branch and folder and can open its own PR. Sessions are independent of worktrees. Claudiu makes new worktrees with git worktree add, and every switch resumes the conversation in the target folder.

Decisions

  • A new session always starts in the project's main checkout, on whatever branch that has, or in the project's folder when it is not a git repository. This holds for every way of starting one: the + New button and its shortcut, the project menu, the home screen, the GitHub pane and requests from other sessions. Fix-with-Claude is the exception: it works in the folder it was asked about. The new-session toggle is removed.
  • Switching a session means ending its process and resuming the same conversation (same Claude session id and transcript, so the history is kept) in the target folder. The Claudiu session record keeps its title, cards and position in the sidebar. Its recorded folder becomes the target's root, even when the old folder was a subfolder. Tested against the real CLI with claude -p.
  • A new worktree is made by Claudiu with git worktree add at <repo>/.claude/worktrees/<name>, on a new branch called <name>. A name that only an existing branch has is refused, so every branch in .claude/worktrees is one Claudiu made, and removal may delete it once it is merged. This replaced Claude Code's own claude --worktree, which needed two ways of launching, a settings file for the base, and a wait for the old process to exit before the transcript could be written. With git, every switch takes the same path. Tested against the real CLI: claude --resume <id> from a new worktree's folder, and then from the main checkout, keeps the same session id and transcript file.
  • Every switch ends the process and resumes from the target folder: the main checkout, a worktree Claudiu made, or one made anywhere else.
  • Claude Code records the active worktree of a session started with claude --worktree (under the earlier design, or by the user) in its transcript as worktree-state lines, and restores it on every resume, which wins over the folder the process is launched from. So before such a session moves, Claudiu appends one line to the transcript: {"type":"worktree-state","worktreeSession":null,"sessionId":"<id>"}. It is the line Claude's own ExitWorktree writes, and the last worktree-state line in the file decides. Tested: after it, a plain resume from the main checkout and from a hand-made worktree folder lands there with the history intact. Claudiu cannot call ExitWorktree itself, since only Claude can invoke it. This is the only write Claudiu makes into a transcript. It only appends, while the session is idle and just before its process ends, and README says so. Whether Claude Code writes a worktree-state line of its own while shutting down is not verified.
  • A switch is allowed only for a running session that Claude Code reports as idle. A session that is busy, waiting for a permission answer, still loading, in an account hand-off or already switching is refused with a message. An ended session cannot be switched until it is resumed. Text typed but not sent in Claude's input box, background tasks and subagents end with the process.
  • Switching away from a folder with uncommitted files asks first, because unlike a branch switch they stay behind. The count ignores anything under .claude/.
  • The session's shell is replaced by a fresh one in the new folder, with no warning, because Claudiu may not type into a terminal and cannot cd it. The editor's file tree re-roots to the new folder.
  • The hand-off screen (the skeleton prompt that says "Handing over to …") is reused for the switch and made reusable: it takes its message from the caller instead of being fixed to a hand-off.
  • Kept from before: names may contain letters, digits, dash and underscore; a blank name gets a generated one; the base of a new worktree is "main" (origin's default branch, fetched first as the branch menu's New branch does, or HEAD without an origin) or "current branch" (the main checkout's HEAD, even when the shown session is in a worktree); the .gitignore offer; worktree removal and the branch menu below.
  • Replaced: Claude Code's --worktree for making and entering worktrees, the new-session toggle, starting a session straight in a new worktree, showing the latest session of a worktree when its row is picked, and the + on worktree rows.

New sessions

  • The sidebar toggle is gone. A saved worktree_sessions setting is ignored and Claudiu's state still loads.
  • Every way of starting a session, except Fix-with-Claude, starts it in the project's main checkout, even when the shown session is in a worktree or the project was added from a worktree or a subfolder. With no project chosen, the + New button shows the project menu first, as today.

Switch a session between worktrees

  • The top-bar worktree menu lists the main checkout first, then the worktrees by name. The shown session's folder has a tick, and a folder with a running session carries a dot. Clicking another row switches the shown session into it. Clicking the ticked row does nothing.
  • Every switch ends the process and resumes the conversation with claude --resume <id> from the target folder. The conversation, title, cards and sidebar position are unchanged, and the record's folder is the target's root.
  • When the transcript's last worktree-state names a worktree, the null line is appended before the process ends. When it names none, nothing is appended.
  • If the transcript can't be found (a session that has not had its first prompt has none), the line can't be appended, or git can't make the worktree, a message says "Couldn't switch: " and the session keeps running where it was.
  • From the click until the new process is up, the sidebar shows the session as running and its pane shows the switching screen, never an empty pane.
  • "New worktree…" is the menu's last item. It is disabled, with the reason shown, in a folder that is not a git repository or has no commits. Its dialog asks for a name and a base, and the button reads "Create and switch". The text says the session moves into the new worktree and keeps its history. Confirming runs the same checks as any switch, then makes the worktree with git and switches the shown session into it.
  • In the branch menu, a branch that another working tree has checked out shows the worktree icon and "opens ". Clicking it switches the shown session into that worktree, with the same checks as the worktree menu. Those rows have no delete x.
  • The checks run in this order: the session is running and idle, then the uncommitted-files question, then the transcript step. Idleness is checked again after the question is confirmed. While Claude is working, clicking a row shows "Wait until Claude finishes, then switch." and changes nothing.
  • For a session that is not running, the menu rows and "New worktree…" are disabled with the hint "Resume the session to switch".
  • Leaving a folder that has uncommitted files, not counting anything under .claude/, asks "N uncommitted files stay in . Your shell restarts." Cancelling changes nothing. A clean folder switches without asking.
  • The session's shell is replaced by a new one in the new folder. If no shell was open, none is started, and the shell panel's open state and height carry over.
  • The editor's file tree shows the new folder's root. An open tab moves to the same path relative to the old root when that file exists in the new folder. A tab with unsaved edits, or whose file is missing there, stays on its old path and its note says so.
  • During the switch the terminal shows the hand-off screen with "Switching to …" or "Switching to the main checkout…" until the new process shows its first output. The account hand-off uses the same screen with its own text.
  • The branch pill, Commit & Push and the shell act on the folder the session is in after the switch. A worktree that was just made shows in the pill within a few seconds, once Claudiu has re-read the repository.
  • A name that matches an existing folder under .claude/worktrees switches into that worktree and the base choice has no effect. A name that matches an existing local branch, and no folder there, keeps the dialog open with "A branch called already exists. Pick another name." A generated name skips existing branches too.
  • Sessions started under the earlier design, whose record folder is a worktree, keep working and can be switched like any other.
  • A second session may be in the same worktree. Nothing refuses it.
  • A new session that has not had its first prompt can be switched. There is no conversation to resume, so the process ends and a new claude --session-id <id> starts in the target folder, under the same Claudiu session.

Top-bar worktree button

  • A pill beside the branch pill is shown for sessions inside a git repository, when the session's folder is one of the project's known working trees. It shows the worktree's folder name when the folder is inside a linked worktree, and "main checkout" otherwise.
  • In a repository with no commits, the pill is shown and its "new worktree" item is disabled.
  • The pill uses a folder-with-git-mark icon, so it is not mistaken for the branch button, and the menu starts with a "Worktrees" title.

Keep worktree folders out of git status

  • When a worktree is created through Claudiu and .claude/worktrees/ is not git-ignored in that repository, a toast offers "Add to .gitignore" as the switch starts. The switch does not wait for an answer.
  • Clicking the button appends the line .claude/worktrees/ to the repo's .gitignore, creating the file if it is missing. The change is left uncommitted.
  • Dismissing the toast leaves nothing stored, so the offer appears again on the next creation.

Remove a worktree

  • In the top-bar menu, every worktree row under <repo>/.claude/worktrees shows an x while the pointer is over it. The main checkout and worktrees made anywhere else have no x.
  • Clicking the x when a running session is in that worktree is refused with "“<title>” is in . Switch it to main first.", naming the most recently active such session. Otherwise it asks the removal question without switching to the worktree.
  • After a session in such a worktree is ended from Claudiu and no other session is in it, a dialog asks whether to remove the worktree too. Cancelling leaves it in place. A session whose process ends by itself, or quitting Claudiu, does not ask.
  • The dialog shows how many files are uncommitted and how many commits are on no remote branch. With no uncommitted files, the button reads "Remove". With some, it reads "Remove and discard" and is red. Commits no remote has stay on the worktree's branch, which is kept.
  • A lock left behind by an ended Claude session is released before removal. A lock Claudiu can't tell is stale makes the removal fail with Claudiu's message that a Claude session is still running.
  • After removal the worktree's branch is deleted when it is fully merged and kept otherwise. The toast says which happened.
  • Sessions that were in the removed worktree stay in the history and resume in the main checkout.
  • When a program running in the folder stops git from deleting it ("Permission denied" on Windows), removal waits up to 10 seconds for it to exit. If it still holds the folder, the toast says the worktree and merged branch are removed and the folder is still in use.

Branch menu

  • The branch menu starts with a "Branches" title and lists the 15 most recently committed branches. Each local branch except the current one and origin's default branch shows an x on hover. Remote-only rows have none, and a remote branch is never deleted.
  • The x asks first. A branch whose commits are all on another branch or on origin gets "Delete"; otherwise the dialog says "N commits are on no other branch and will be lost" and the button is a red "Delete anyway".

Worktrees in Claudiu's data folder

New worktrees go to <data dir>/worktrees/<repo name>-<short hash of the repo path>/<name> instead of <repo>/.claude/worktrees. This replaces the folder in the decisions above and the .gitignore offer. Worktrees already in <repo>/.claude/worktrees keep working as sessions' folders but are no longer Claudiu's: they have no remove button and are not listed in settings.

  • A new worktree is made in the data folder, on a new branch named like it. Names are checked against that folder and against local branches.
  • Only folders in the data folder show the x in the top-bar menu, get the remove question after a session ends, and can be removed by Claudiu.
  • The "Add to .gitignore" toast and the action behind it are gone.
  • Settings has a "Worktrees" page listing every worktree folder in the data folder, grouped by repository. Each row has "Show in file manager" and "Remove". Remove asks the same question as the top-bar x.
  • A folder whose repository is gone, or that git no longer lists, still shows in the page and can be deleted after a confirmation.
  • A running session in a worktree blocks its removal from the page with the same message as the top-bar x.

Out of scope

A session that Claude itself moves with its own EnterWorktree tool (Claudiu's recorded folder would not follow); switching a session that is not running; renaming a worktree's branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions