Skip to content

feat: add worktree isolation to spawn_agent - #108

Merged
ibetitsmike merged 25 commits into
mainfrom
mike/subagent-isolation-2-spawn-worktree
Nov 10, 2026
Merged

ibetitsmike merged 25 commits into
mainfrom
mike/subagent-isolation-2-spawn-worktree

Conversation

@ibetitsmike

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

Part of #9. Builds on #81 (merged), which added the agent worktree API this PR calls.

Behind experiment fork-subagent-isolation, spawn_agent takes three new optional arguments:

  • isolation: shared (default, today's behavior) or worktree.
  • repo_path: the git repository to branch from. Defaults to the workspace agent's directory.
  • base_branch: the branch or commit the child's branch starts from. Defaults to the repository's current HEAD.

With worktree, chatd asks the parent chat's workspace agent to create a worktree at ~/.coder/worktrees/<repo>/<child chat id> on a new branch agent/<title slug>-<first 8 hex of the child chat id>, before the child chat transaction. The child's chat_subagents row records isolation, worktree_path and branch, and the tool result returns the same three fields. If the transaction fails, the worktree is removed again on a detached context. The child does not yet work inside its worktree; binding its working directory is the next PR.

Errors reach the model as tool errors: repo_path or base_branch without worktree, a chat with no workspace, a computer use child, an agent with no directory and no repo_path, an agent older than the server (404, with a hint to restart the workspace or use shared), and any rejection from the agent API with its reason.

With the experiment off, the tool schema and behavior are unchanged.

Size: about 300 production lines and 430 test lines; the rest is generated.

Code

  • coderd/x/chatd/fork_isolation.go: wraps spawn_agent (schema, argument checks, result fields), creates and cleans up the worktree, builds the branch name.
  • coderd/x/chatd/fork_subagents.go: createSubagentChatWithID writes the chat_subagents row for isolated spawns and removes the worktree when the transaction does not commit.
  • codersdk/fork_experiments.go: the experiment. The remaining generated files follow from it.

Tests

TestForkSpawnIsolation* run the real tool against a database with a fake agent connection: the worktree request (default directory, explicit repo and base), the row and result fields, every error above except the agent with no directory (covered by UAT), an unreachable agent, the experiment off, and worktree removal when the chat insert fails. TestForkWorktreeBranch covers the branch name. Each was checked against a production break.

UAT

Dev build on a branch server with a real workspace agent and GLM: worktree spawn (worktree at the expected path on the expected branch, parent checkout untouched, root 0700), base_branch, shared with repo_path, a non-git repo_path, no agent directory, an unknown isolation value, and the default shared spawn. All passed; the children answered. UAT found one cosmetic issue (a doubled .: in agent errors), fixed here.

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
@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-10T09:27:07.600834Z 47ff189 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: a79e3e51c0

ℹ️ 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/fork_subagents.go Resolved
Comment thread coderd/x/chatd/ARCHITECTURE.md Resolved
Comment thread coderd/x/chatd/fork_isolation.go Resolved
Comment thread coderd/x/chatd/fork_isolation.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum posted this on behalf of @ibetitsmike.

@codex review

@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum posted this on behalf of @ibetitsmike.

@codex review

@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: f4e3ccd2b3

ℹ️ 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/fork_isolation.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. More of your lovely PRs please.

Reviewed commit: ab14e1527c

ℹ️ 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 posted this on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Chef's kiss.

Reviewed commit: 47ff189026

ℹ️ 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 (273daf03dd) 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 (273daf03dd), 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 643ba9a into main Nov 10, 2026
20 of 27 checks passed
@ibetitsmike
ibetitsmike deleted the mike/subagent-isolation-2-spawn-worktree branch November 10, 2026 10:47
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head 47ff1890260de0e1ca633549be6823ba42daf1d8:

  • fork-ci pull_request run 42799009425 passed; its build job checked out Merge 47ff189026 into 273daf03dd (current main). All checks green. Run 42790347791 had also passed on this head.
  • Codex on this exact head: no major issues (comment 6763785455). 0 unresolved review threads.
  • 47ff189026 merges main 2d635ea9a6 (feat(coderd/x/chatd): accept model_config_id and reasoning_effort for computer_use spawns #95). The only conflict was the appendForkTools tail in fork_tools.go: main's question block, then withStoredSubagentTypes(withComputerUseSpawnOverrides(tools)), then the isolation wrap. TestForkSpawnIsolationStoredAgentType runs with fork-agent-definitions on and asserts that spawn_agent keeps the isolation parameters and feat(coderd/x/chatd): accept model_config_id and reasoning_effort for computer_use spawns #95's override text; dropping either wrapper makes it fail.
  • UAT PASS on a branch server at faed0396c5 with a real workspace agent and GLM (PR body, UAT): worktree spawn at the expected path and branch with the parent checkout untouched, base_branch, shared with repo_path, a non-git repo_path, no agent directory, an unknown isolation value, and the default shared spawn. Accepted as covering this head: the later product changes are two Codex fixes, each with its own test, and the merge-only wrapper order above.

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