# Worktrees and isolation

> How zelk gives a session a git worktree of its own, how the session's root and working directory move there and are kept in its log, how subagents get theirs, what a worktree doesn't protect, and what becomes of one that holds unfinished work.

Two sessions that edit one checkout step on each other. One overwrites a
file the other has half changed, and a test run in the middle sees both.
A git worktree gives each its own directory, on its own branch, over the
same repository. zelk uses them so that a session can try something without
touching your checkout, and so that several sessions, or a subagent and its
parent, can work at once.

A repository drawn as a git branch graph, with the worktrees zelk makes as directories hanging off the branches they check out, in `/home/you/repo/.zelk/worktrees/NAME` on `worktree-NAME`, and the sessions that work in them attached to the directory they work in.

- **Enter**. A worktree is a directory on a branch of its own, and the session’s root moves with it. The new branch forks from where HEAD was. The session’s edits and its commit land in the worktree and on that branch, while the main checkout stays as it was.
   1. s1 works in the main checkout, on main. Its log records that root, and every path it names and every command it runs resolves against it.
   2. s1’s shell runs cd cmd/zelk. The log records cwd_changed, so the next command starts there, after a restart as well.
   3. EnterWorktree fix-42: before it makes .zelk/worktrees/fix-42, zelk writes its record, in state “creating”, so a crash leaves a record to settle, never a checkout it can’t account for.
   4. git adds .zelk/worktrees/fix-42 on a new branch, worktree-fix-42, made from HEAD (ccb6). The record says “ready”. Ignored files listed in .worktreeinclude are copied in; uncommitted edits in your checkout are not.
   5. s1 records workspace_move_prepared, from repo to fix-42. It still works at repo, and nothing new starts there while the move is prepared.
   6. workspace_move_committed switches the root to fix-42, and the execution there records workspace_move_handed_off. The working directory returns to the top, approvals given for the old root no longer match, and files it read must be read again.
   7. s1 edits parser.go in .zelk/worktrees/fix-42. No other directory changes.
   8. s1 commits “Fix #42” on worktree-fix-42, 6b11. main stays at ccb6, and the main checkout is as it was.
- **Subagent**. Each isolated child works in a worktree of its own, and only the one that changed something keeps it. The parent and child A both edited parser.go, and neither saw the other’s copy. Child B only read, so its worktree and branch went when it finished. Child A’s work is on its branch, in a directory the report names.
   1. parent works in the main checkout, on main. Its log records that root, and every path it names and every command it runs resolves against it.
   2. parent edits parser.go in the main checkout. No other directory changes.
   3. The Task call asks for isolation: worktree, so zelk makes .zelk/worktrees/agent-07de… on worktree-agent-07de… from the parent’s HEAD (ccb6), and child A works there. It doesn’t have the parent’s uncommitted parser.go.
   4. The Task call asks for isolation: worktree, so zelk makes .zelk/worktrees/agent-06de… on worktree-agent-06de… from the parent’s HEAD (ccb6), and child B works there. It doesn’t have the parent’s uncommitted parser.go.
   5. child A edits parser.go in .zelk/worktrees/agent-07de…. parser.go is modified in the main checkout too: two copies of one file, each in its own directory, and neither sees the other’s.
   6. child A commits “Handle EOF” on worktree-agent-07de…, 5773. main stays at ccb6, and the main checkout is as it was.
   7. child B finishes having changed nothing. Its worktree is clean at its base, so zelk removes it and its branch, and says so in the report: it can’t be continued.
   8. child A finishes. Its worktree holds 1 commit, so zelk keeps it, and the report names .zelk/worktrees/agent-07de…: the work isn’t in the parent’s checkout. Continuing the child works there.
- **Resume**. The log says where a session works, so zelk takes it back there. Resuming from the main checkout landed the session in its worktree, with its uncommitted edit and its working directory. When someone removed the worktree, the session moved to where it was launched, and the move is in its log.
   1. s1 stops. zelk asks about .zelk/worktrees/fix-42, with “Keep worktree” selected, and says what removing it would mean: user-named worktree; uncommitted work will be discarded. You keep it. The log still records fix-42 as the root.
   2. s1 is launched in the main checkout, but its log says it worked in fix-42, and git confirms that is a checkout of this repository. zelk continues it there: root fix-42, working directory internal/parse, nothing new recorded.
   3. s1 commits “Fix #42” on worktree-fix-42, 6b11. main stays at ccb6, and the main checkout is as it was.
   4. s1 stops. zelk asks about .zelk/worktrees/fix-42, with “Keep worktree” selected, and says what removing it would mean: user-named worktree; launched directly in this existing worktree; commits differ from the recorded base. You keep it. The log still records fix-42 as the root.
   5. git worktree remove deletes .zelk/worktrees/fix-42. The branch worktree-fix-42 stays, with its commits. Nothing tells the session, whose log still records the directory as its root.
   6. The worktree /home/you/repo/.zelk/worktrees/fix-42 this session worked in is gone; the session continues in /home/you/repo. The move is recorded: prepared, committed, handed off. Its commits are still on the branch; the uncommitted edits went with the directory.
- **Remove**. A clean worktree goes quietly; one with work goes only when you say so. Nothing was lost with the first worktree, so it was removed. The second held an uncommitted edit, so the removal was refused until the model had your word. Either way the session moved out first.
   1. ExitWorktree remove: nothing in .zelk/worktrees/try-it would be lost, so s1 moves back to the main checkout first: prepared, committed, handed off. Only then does the host remove the worktree, so the session’s new root is on disk before anything is deleted.
   2. The host removes .zelk/worktrees/try-it, then deletes worktree-try-it, the branch zelk made for it, but only if it still points where zelk last saw it. Nothing was lost.
   3. ExitWorktree remove is refused: .zelk/worktrees/fix-42 has uncommitted changes, which removing it would lose. The model has to ask you, then call again with discard_changes, or keep it.
   4. ExitWorktree remove: you agreed to lose what .zelk/worktrees/fix-42 holds, so s2 moves back to the main checkout first: prepared, committed, handed off. Only then does the host remove the worktree, so the session’s new root is on disk before anything is deleted.
   5. The host removes .zelk/worktrees/fix-42, then deletes worktree-fix-42, the branch zelk made for it, but only if it still points where zelk last saw it. The uncommitted parser.go is gone.
- **Hooks**. A hook can make the directory, and then zelk can’t see what is in it. With a WorktreeCreate hook there is no branch for zelk to draw and no git to ask, so nothing it can check says the directory is safe to remove. It takes your word, and the WorktreeRemove hook does the removing.
   1. s1 works in the main checkout, on main. Its log records that root, and every path it names and every command it runs resolves against it.
   2. EnterWorktree fix-42: a trusted WorktreeCreate hook makes worktrees in place of git. zelk writes its record first, then runs the hook with the name.
   3. The hook prints /home/you/trees/fix-42, and zelk checks that it is a directory. zelk ran no git and made no branch: it knows the directory only by that path and the hook that made it.
   4. s1 moves to fix-42: workspace_move_prepared, workspace_move_committed, workspace_move_handed_off. The working directory goes back to the top, and approvals given for the old root no longer match.
   5. s1 edits parser.go in /home/you/trees/fix-42. No other directory changes.
   6. ExitWorktree remove is refused: a hook made /home/you/trees/fix-42, so zelk can’t tell whether removing it loses work. The model has to ask you, then call again with discard_changes, or keep it.
   7. ExitWorktree remove: you agreed to lose what /home/you/trees/fix-42 holds, so s1 moves back to the main checkout first: prepared, committed, handed off. Only then does the host remove the worktree, so the session’s new root is on disk before anything is deleted.
   8. The WorktreeRemove hook runs with worktree_path /home/you/trees/fix-42, and zelk drops its record. Whatever the directory held is gone.

Read the drawing as one repository. A column of dots is a branch, newest at
the bottom. A dashed line from a branch's tip down to a directory means that
directory has the branch checked out, and the pill under the directory is
the session that works in it. The strip at the bottom is that session's log.

## What a worktree is

A repository has one `.git` directory that holds the commits, the branches
and the objects. A checkout is a working directory with one branch checked
out. Git lets one repository have several: `git worktree add` makes another
directory with its own files, index and branch, over the same object store.
The branches are shared, so a commit made in one worktree is on its branch
for all of them, but a branch is checked out in only one worktree at a time.

zelk keeps its worktrees under the main checkout, in
`.zelk/worktrees/<name>`, each on the branch `worktree-<name>`.
`.zelk/worktrees` holds a `.gitignore` containing `*`, so the worktrees stay
out of the main checkout's status without touching your ignore files.

## Making one

Three things make a worktree: the `-worktree NAME` flag, the model's
`EnterWorktree` tool, and a subagent started with isolation, all through the
same routine. The tools are offered in a git repository, or wherever a hook
makes worktrees, and `EnterWorktree` tells the model to use it only when you
ask for a worktree or for isolation. A name is 1 to 64 letters, digits, `.`,
`_` or `-`, starting with a letter or digit.

A new worktree is made from the `HEAD` of the checkout zelk is working in. It
has your commits, none of your uncommitted edits, and only the ignored files
that `.worktreeinclude` lists. If the directory exists already, zelk reopens
it, and a branch of that name is used as it is.

zelk writes its record of the worktree, in its state directory, before git
runs, and marks it ready once the checkout and the copied files are in place.
A crash in between leaves a record the next open settles, not a checkout zelk
can't account for. The record also says whether zelk made the worktree and the
branch, because cleanup removes only what zelk made. While a process holds a
worktree, zelk locks it with git's own lock, so `git worktree remove` and
`git worktree prune` leave it alone.

## Where a session works

A session's workspace root is part of what its log decides with: paths and
shell commands resolve against it, and approvals, file observations and check
evidence describe files under it. The log records the root when the session
starts (`session_started`), and each directory the shell ends in
(`cwd_changed`). Fold the log and you have the session's root and working
directory, so neither is lost when the process dies.

Entering a worktree is a **move**, in three records. `workspace_move_prepared`
says where from and to. `workspace_move_committed` is the switch, and it
waits until nothing of the old root is live: no background process, no
unfinished subagent. The execution at the new root then records
`workspace_move_handed_off`. The commit puts the working directory back at the
top, bumps the policy revision so approvals given for the old root never
match again, drops file observations and check evidence, and tells the model
that files it read before must be read again. `ExitWorktree` is the same move
back. A session that owns published skill files, or requirement evidence
scoped to its workspace, can't move.

### Resuming

Launched in the main checkout with `-c` or `-session`, zelk reads the root
the log recorded. If it is a worktree of this repository, and git confirms it
is a separate checkout of it, zelk continues the session there, with its
uncommitted edits and its working directory. If the worktree is gone, zelk
takes the session up where you launched it and records a move. The branch and
its commits remain, but uncommitted edits went with the directory. A worktree
a hook made is known by its record instead, and the session goes back to it
only while the same hook is trusted.

## Subagents

A `Task` call with `isolation: "worktree"`, or a subagent type whose file
says `isolation: worktree`, runs the child in a worktree of its own. It is
named `agent-` and 16 hex digits that follow from the child's session ID, so
a call run again after a crash reopens the worktree its first attempt made.
It is made from the parent's `HEAD`, so the child lacks the parent's
uncommitted edits: use isolation only for work that doesn't need them.

The child gets its own file and shell tools over that directory, its own
lock on its edits, and the same sandbox settings as the agent, rooted there.
Its configuration records the workspace as its root, with a note telling it
where it works. Without a host that can make one the call is refused, and
never falls back to the parent's workspace. Making a worktree is never
read-only, so plan mode refuses the call.

When the child's execution ends, zelk looks at the worktree. If the child
left nothing, meaning no uncommitted or untracked files, no new ignored
files, no new commits and no submodules, the worktree and its branch are removed, and the
report says the child can't be continued. Otherwise it is kept, and the
report names its path: the work isn't in the parent's checkout, and the
parent decides what to do with it. Continuing the child works there. When
zelk starts, it also removes a subagent's worktree that is clean, has no
holder and hasn't been touched for 30 days.

## A worktree is not a sandbox

A worktree keeps edits apart. It doesn't limit what a command can reach.

- The branches and objects are shared. A command in one worktree can reach
  every other worktree's branches and history.
- In a worktree session, a file tool whose path is outside the workspace is
  refused in every mode, bypass included, and so is a shell command that may
  run git against another checkout (`-C`, `--git-dir`, `GIT_DIR` and the
  like). No approval runs it. These checks read the command's words, not its
  effects: a script that runs git elsewhere, or a command that edits a file
  in another directory, gets past them.
- A [sandbox](/concepts/sandboxing/) is what confines the process. Under
  one, a linked worktree's common git directory must be writable for commits
  to work, so the sandbox doesn't separate branches, indexes or `HEAD` from
  other worktrees. It does protect git configuration and hooks in every one.
- Network, credentials, other processes, ports and databases are the same
  for every worktree.

Use a worktree to keep work apart, and a sandbox to limit what it can do.

## Hooks instead of git

A trusted `WorktreeCreate` hook makes worktrees in place of git, for
`-worktree`, `EnterWorktree` and isolated subagents. The host runs it, from
the project's hooks file if you have trusted it and otherwise the user's,
never the loop. It reads JSON on standard input that includes the `name`, and
prints the new directory's absolute path as its only output, which zelk
checks is a directory. On Windows the hook runs in Bash or, with
`"shell": "powershell"`, in PowerShell as other [hooks](/cli/hooks/) do, and
may print the path as `C:\work\wt`, `C:/work/wt` or, from Git Bash,
`/c/work/wt`; `cwd` and `worktree_path` reach it as `C:/work/...`. Any other exit fails the operation.
`WorktreeRemove` receives `worktree_path`.

Then zelk ran no git. There is no branch to draw, no base to compare and no
status to read, so it can't tell whether the directory holds work. It is
never removed automatically, a subagent's is always kept, and
`ExitWorktree` removes it only with your word (`discard_changes`) and a
`WorktreeRemove` hook, which does the removing. Starting from a pull request
needs git and is refused. A managed policy that turns hooks off leaves
worktrees to git.

## Cleanup and uncommitted work

- **`zelk exec`** never removes the worktree it was started in. On exit it
  prints the command that returns you to the main checkout and continues the
  session. It does remove one the model leaves with `ExitWorktree` and the
  action `remove`.
- **The terminal UI** removes a worktree on a normal quit only if zelk
  generated its name, nothing in it differs from its base, and it wasn't
  launched directly into an existing one. Any other gets a sheet with **Keep
  worktree** selected that says what removal would delete. After an error or
  a signal the worktree is kept.
- **`ExitWorktree` with `remove`** is refused for a worktree zelk didn't make.
  It is refused too if there are uncommitted changes, ignored files that
  aren't copies of the main checkout's, submodules, or commits made since
  creation, unless `discard_changes` is true, which the model is told to set
  only once you have agreed. The session moves back first, and the host
  removes the worktree only after that move has committed.

Removal runs `git worktree remove`, without `--force` unless you consented.
The branch is deleted only if zelk made it, and only if it still points where
zelk last looked, so a commit made in the meantime is never deleted with it.
Commits on a deleted branch are on no branch. A removal cut off by a crash is
recorded as such, and the next open refuses and prints the manual steps.

## Limits

- The repository needs a main checkout, at least one commit, and a `.zelk`
  that isn't a symbolic link. A bare repository, or one whose git directory is
  elsewhere, is refused.
- A session already in a worktree can't enter another: it leaves first.
- Only what git tracks, plus `.worktreeinclude`, is in a new worktree.
  Dependencies and build caches aren't, so a build may start from nothing.
- zelk doesn't inspect what is inside a submodule, so it won't remove a
  worktree that has one without your word.
- Ownership of a branch is shown by its reflog. Where reflogs are off or
  have expired, zelk keeps the branch.

## Where to go deeper

[Worktrees in the CLI](/cli/worktrees/) has the flags, `.worktreeinclude`,
pull request review and the cleanup sheet. [Subagents](/concepts/subagents/)
says what a child shares with its parent, and
[Sandboxing and enforcement](/concepts/sandboxing/) what confines a command.
The contracts give the exact rules:
[workspace transitions](/internals/transitions/) for the move,
[permissions](/internals/permissions/) for worktree sessions and
[enforcement](/internals/enforcement/) for git under a sandbox.
[Hooks](/concepts/hooks/) and the [hooks guide](/cli/hooks/) cover the worktree
events, [Durability and recovery](/concepts/durability/) the log a resume
reads, and the [tools reference](/reference/tools/) lists `EnterWorktree` and
`ExitWorktree`.
