Let sub-agent dispatch run in an isolated git worktree - #293
Merged
TheGreatAxios merged 6 commits intoAug 4, 2026
Conversation
TheGreatAxios
force-pushed
the
cl-4929-run-each-sub-agent-in-its-own-git-worktree-with-attributed
branch
from
August 1, 2026 17:54
fa1ef58 to
8fff41c
Compare
TheGreatAxios
force-pushed
the
cl-4929-run-each-sub-agent-in-its-own-git-worktree-with-attributed
branch
from
August 2, 2026 04:09
8fff41c to
93deadd
Compare
TheGreatAxios
force-pushed
the
cl-4929-run-each-sub-agent-in-its-own-git-worktree-with-attributed
branch
from
August 2, 2026 04:38
93deadd to
1c7d10a
Compare
TheGreatAxios
force-pushed
the
cl-4929-run-each-sub-agent-in-its-own-git-worktree-with-attributed
branch
from
August 2, 2026 04:49
1c7d10a to
aef59b5
Compare
TheGreatAxios
force-pushed
the
cl-4929-run-each-sub-agent-in-its-own-git-worktree-with-attributed
branch
from
August 4, 2026 00:12
aef59b5 to
559efd4
Compare
Sub-agents share the dispatcher's cwd today, so parallel workers collide on the same working tree and the approval-prompt attribution has nothing distinct to show. task() can now opt into worktree isolation: each spawn gets a fresh worktree branched from the dispatcher's HEAD, and its tools resolve against that path instead of the parent cwd, so approval prompts (which already read the sub-agent identity's cwd) surface the worktree path automatically. Worktree setup fails closed when the dispatcher cwd is not a git repository or worktree creation fails. On completion the worktree is removed if unchanged; if the sub-agent left uncommitted changes, it is preserved and a notice is appended to the returned report.
Cleanup checked git status --porcelain, which omits gitignored files, so a sub-agent worktree holding only ignored output (dist/, logs) looked clean and was deleted along with that output. Adding --ignored makes dirty-detection catch it too.
The task tool description and the isolated-run comment both said the sub-agent unconditionally shares your working tree, which is only true in the default dispatch mode. Worktree isolation snapshots the dispatcher's last commit, so uncommitted and untracked changes are never visible to a sub-agent running in that mode.
git status never reports a git stash the sub-agent ran mid-task, so a worktree holding one looked clean and got removed automatically — the stash entry survives in the repo's shared refs/stash either way, but went silently orphaned with no indication of which worktree it came from. createSubAgentWorktree now captures the stash list as a baseline; cleanup diffs the current list against it and preserves the worktree with a notice naming the new stash entries instead of removing it. Also preserves the caught error as "cause" on both WorktreeError throw sites in this file, and collapses two stray double blank lines.
Record HEAD at create, treat a null stash baseline as unknown (preserve), preserve when stash list fails at cleanup, and pass both through finishWithWorktree so commits on detached HEAD are not left reflog-only.
TheGreatAxios
force-pushed
the
cl-4929-run-each-sub-agent-in-its-own-git-worktree-with-attributed
branch
from
August 4, 2026 00:28
559efd4 to
967c4a5
Compare
Sub-agent worktrees run with a different process cwd than the session that built the gate. Relative path checks for auto-allow and restriction must resolve against that process cwd so a cat of a worktree-local path is not judged as if it opened under the session. Also refuse to collapse payloads for bun, deno, busybox, ash, and osascript in the approval dialog.
TheGreatAxios
marked this pull request as ready for review
August 4, 2026 03:17
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
Verification
Closes CL-4929