Skip to content

feat(coderd/x/chatd): bind isolated subagents to their worktree - #135

Merged
ibetitsmike merged 35 commits into
mainfrom
mike/subagent-isolation-3-child-binding
Nov 10, 2026
Merged

ibetitsmike merged 35 commits into
mainfrom
mike/subagent-isolation-3-child-binding

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

Part of #9. Builds on #108 (merged), which creates a worktree for spawn_agent with isolation: worktree but leaves the child working in the shared checkout.

With experiment fork-subagent-isolation on, a child spawned with isolation: worktree now works inside its worktree, and so does every shared descendant below it:

  • Its file tools (read_file, write_file, edit_files, attach_file) resolve relative paths against the worktree, and execute runs there when the call names no workdir. Absolute paths are left alone.
  • A system message before the child's prompt names the worktree path and branch, or says the chat inherits them from an ancestor.
  • The turn's workspace context names the worktree as the working directory.
  • A worktree grandchild spawned without repo_path branches from its parent's worktree instead of the agent directory.

A shared child of a chat outside any worktree is unchanged. With the experiment off, nothing changes.

Size: about 250 production lines and 210 test lines.

Code

  • coderd/x/chatd/fork_isolation_binding.go: chatWorktree finds the worktree a chat works in (its own, or the nearest ancestor's across shared links), worktreeBoundTool rewrites relative paths and the default execute directory, withSubagentWorktreePrompt adds the system message, and forkWorktreeAgent sets the agent directory for the workspace context.
  • coderd/x/chatd/fork_subagents.go, fork_isolation.go, fork_tools.go, turn_environment.go: one call each into the file above.

Only the bound tools treat paths in chatd, so only they needed Windows path handling. The spawn code from #108 sends repo_path, the parent worktree and the agent directory to the agent unchanged (coderd/x/chatd/fork_isolation.go:172-206), and the agent checks and joins them with native filepath (agent/agentgit/fork_worktrees.go:148, :220-221).

Tests

  • TestForkWorktreeBindingTools runs each bound tool through appendForkTools against a recording fake. It covers relative and absolute paths for every file tool, edit_files with several files, execute with no, null, relative and absolute workdir, a tool that is not bound, the workspace context directory, and a shared child of the root that stays unbound.
  • TestForkWorktreeBoundToolWindowsPaths covers a Windows worktree path: relative paths join with a backslash, and drive, UNC and rooted paths stay as they are.
  • TestForkWorktreeBindingPrompt covers a worktree child's own system message, a shared grandchild that inherits the worktree, a worktree grandchild that branches from its parent's worktree, and a shared child of the root with no message.

Each was checked against a production break.

UAT

Dev build of this head (609e4bd8cb, rerun after merging main) on a branch server with a real workspace agent and GLM, five cases:

  1. A worktree child wrote a file by relative path and committed with execute and no workdir. Its pwd was the worktree, the commit landed on its agent branch, and the parent checkout stayed clean.
  2. Two worktree children in the same step both edited README.md and committed. Each commit landed on its own branch, and the parent's README.md was untouched.
  3. A shared grandchild of a worktree child worked in the child's worktree.
  4. A worktree grandchild without repo_path branched from the child's worktree.
  5. A shared child of the root still worked in the agent directory.

All passed. Not covered live: the workspace context working directory header (needs pinned context files) and the relative paths of edit_files, read_file and attach_file; unit tests cover them.

Assert the error message in worktree create and delete rejection cases so
each case proves the guard it names instead of any 400. Move the outside
root delete case beside the root so it exercises the parent directory
check, and add relative path and clean detached HEAD delete cases. Add a
client test for the 201 create and 204 delete success paths, which agent
tests cannot reach because the agent roots worktrees at HOME.
…he created branch

A post-checkout hook failure makes git worktree add exit nonzero after
creating the worktree and branch, so create now discards both before
answering 500. Delete requires the branch create returned, refuses worktrees
whose HEAD is on another branch or detached, and deletes the branch with
update-ref guarded by the tip the unpushed check covered.
…s with submodules

discardWorktree now runs its git commands on a context that outlives a
canceled request so a failed worktree add is still rolled back. Delete
passes --force when the worktree has initialized submodules; the status
check has already proven the tree clean.
…re removing a worktree

A submodule with ignore=all hid its edits from the clean check, and the
forced removal of a worktree with initialized submodules then deleted them.
…rmissions safe

Delete now refuses worktrees whose initialized submodules hold commits
not on any remote before the forced removal. Create rejects repositories
that contain the worktree root, which git would happily nest inside the
source tree, and creates the root with 0700 permissions.
…spawn's worktree

The agent delete endpoint now requires the branch create returned.
…e a forced worktree removal

git submodule foreach skips deinitialized submodules, but their
repositories stay under the worktree's modules directory and --force
deletes them, so count local-only commits by walking that directory.
…quest deadline and widen the submodule commit check
…on-2-spawn-worktree

# Conflicts:
#	coderd/apidoc/docs.go
#	coderd/apidoc/swagger.json
#	coderd/x/chatd/fork_tools.go
#	codersdk/fork_experiments.go
#	docs/reference/api/schemas.md
…on-2-spawn-worktree

# Conflicts:
#	coderd/x/chatd/fork_tools.go
…on-3-child-binding

# Conflicts:
#	coderd/x/chatd/ARCHITECTURE.md
#	coderd/x/chatd/fork_isolation.go
#	coderd/x/chatd/fork_isolation_internal_test.go
#	coderd/x/chatd/fork_subagents.go
#	coderd/x/chatd/fork_tools.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum posted this on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 10, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-10T11:18:34.110511Z b33147a Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 609e4bd8cb

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread coderd/x/chatd/ARCHITECTURE.md Resolved
Comment thread coderd/x/chatd/fork_tools.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum posted this on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 🚀

Reviewed commit: b33147ae64

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) closed and reopened this on behalf of @ibetitsmike.

Closing and reopening only to make GitHub recompute the merge ref against current main (17ef3432d9) and run fork-ci on it. The head is unchanged.

@ibetitsmike ibetitsmike reopened this Nov 10, 2026
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) closed and reopened this again on behalf of @ibetitsmike.

The merge ref now points at current main (17ef3432d9), but the run started by the first reopen was queued against the old merge ref. This second reopen starts fork-ci on the recomputed merge. The head is unchanged.

@ibetitsmike ibetitsmike reopened this Nov 10, 2026
@ibetitsmike
ibetitsmike merged commit ee5b159 into main Nov 10, 2026
22 of 29 checks passed
@ibetitsmike
ibetitsmike deleted the mike/subagent-isolation-3-child-binding branch November 10, 2026 11:53
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum (AI agent) merged this on behalf of @ibetitsmike.

Merge evidence for head b33147ae64759172b12d2c92c5c53b8ff2445a97:

  • fork-ci pull_request run 42808543135 passed; its build job checked out Merge b33147ae64 into 17ef3432d9 (current main). All checks green. Run 42805962168 had also passed on this head.
  • Codex on this exact head: no major issues (comment 6765951352). 0 unresolved review threads. Of round 1's four findings, the Windows path finding was fixed; the other three were declined as out of scope.
  • UAT PASS on a fresh branch server built from 609e4bd8cb with a real workspace agent and GLM (PR body, UAT): all five cases passed. They cover a worktree child's relative write and commit, two parallel worktree children, a shared grandchild inheriting the worktree, a worktree grandchild branching from the child's worktree, and an unchanged shared child of the root. Accepted as covering this head: the only later change is the Windows path handling in fork_isolation_binding.go, which only runs for Windows worktree paths and is covered by TestForkWorktreeBoundToolWindowsPaths (fails with the fix reverted).
  • feat: add worktree isolation to spawn_agent #108's worktree code on main makes no POSIX path assumption: fork_isolation.go passes the paths to the agent unchanged, and the agent joins them with native filepath in agent/agentgit/fork_worktrees.go.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant