Skip to content

feat: add an agent git worktree API for isolated subagents - #81

Merged
ibetitsmike merged 12 commits into
mainfrom
mike/subagent-isolation-1-agent-worktrees
Nov 10, 2026
Merged

ibetitsmike merged 12 commits into
mainfrom
mike/subagent-isolation-1-agent-worktrees

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

Part of #9.

Adds the workspace agent API that chatd will use to give worktree-isolated subagents their own git worktree. Nothing calls it yet: chatd starts using it in the next PR of this stack, behind experiment fork-subagent-isolation. The agent routes themselves are not experiment-gated, since agents do not know deployment experiments.

Agent: /api/v0/git/worktrees

  • POST creates a worktree at ~/.coder/worktrees/<repo>/<name> on a new branch, starting at base_ref (default HEAD of repo_path). It returns the path, branch, base commit and repo root with 201. If git worktree add fails after checkout (for example a failing post-checkout hook), the half-created worktree and branch are rolled back, on a detached context so a canceled request still cleans up, with the same 10 minute budget as the request. The root is created 0700, and an existing root is tightened to 0700. It rejects repositories that contain the worktree root, relative repo paths, unsafe names, invalid or existing branches, option-looking values, an existing target and any path that resolves outside the root.
  • DELETE takes the path and the branch that create returned. It removes a linked worktree exactly two levels below the root, then deletes that branch, and answers 204. It refuses with 409 and says why when HEAD is not on that branch (another branch or detached), when the tree has uncommitted or untracked changes (ignored files do not count, as with git worktree remove). A clean worktree with initialized submodules is removed with --force, which git requires for it, unless a submodule has commits that are on no remote (409); this also covers submodules that were deinitialized but still have a repository under <gitdir>/modules, and commits that only a submodule reflog still holds, or when the branch tip or the worktree HEAD and branch reflogs have commits that no other local branch or remote-tracking ref holds ("unpushed", which includes commits a reset --hard moved away from). The branch is deleted with update-ref -d against the tip the unpushed check covered, so a tip that moves in between is kept, and its branch.<name> config (such as an upstream) is removed with it. That race window has no test: hitting it needs a commit landing between two git calls inside one request, which a test cannot schedule without an injection seam in production code. Missing paths get 404; anything outside the root, the root itself, a repo directory or a main working tree gets 400.

Both routes extend the agent server's 20 second write deadline and bound the request to 10 minutes, so a large checkout can finish. The routes are mounted by one hook line in agent/agentgit/api.go; the logic is in agent/agentgit/fork_worktrees.go.

Client

codersdk/workspacesdk/fork_worktrees.go adds the wire types and ForkWorktreeConn, implemented by *agentConn. Callers type-assert their AgentConn, so the AgentConn interface and its mock are unchanged. Non-2xx answers come back as *codersdk.Error with the status code (an older agent answers 404).

Tests

Real git in temp dirs: create defaults and explicit base, every rejection with its reason, two worktrees editing the same file on different branches with the parent checkout unchanged, and every delete outcome (clean, dirty, unpushed, held by another branch or remote, HEAD on another branch or detached, path checks), plus rollback after a failing post-checkout hook, rollback with a canceled context, clean, dirty, deinitialized, reflog-only, refs-named and libs/sub submodule worktrees, a repository inside submodule metadata that must not be scanned, a pre-existing permissive root, and a checkout that outlasts the server's write timeout. The client success paths run over a stub tailnet, and the error mapping plus the hook line run through a real agent. Each new test was checked against a production break.

Size: 585 production lines, 917 test lines.

UAT: per the lead, this PR has no UAT of its own; the next PR's dev-build UAT exercises this API end to end through chatd. Note that fork-ci's test job does not run ./codersdk/workspacesdk/ or ./agent/ tests; those passed locally in golang:1.26.10-bookworm.

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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 9, 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-10T03:24:16.902768Z df86079 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: bbda241312

ℹ️ 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 agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
…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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@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: 54f764178f

ℹ️ 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 agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
…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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

ℹ️ 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 agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
…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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

ℹ️ 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 agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
…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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@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: 9c816e5299

ℹ️ 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 agent/agentgit/fork_worktrees.go Resolved
…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.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@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: 1e6d2c2675

ℹ️ 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 agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
…quest deadline and widen the submodule commit check
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

ℹ️ 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 agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

ℹ️ 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 agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@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: 6ba292b86c

ℹ️ 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 agent/agentgit/fork_worktrees.go Resolved
Comment thread agent/agentgit/fork_worktrees.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

ℹ️ 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 agent/agentgit/fork_worktrees.go Resolved
Comment thread codersdk/workspacesdk/fork_worktrees.go Resolved
@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 (23d5021361) 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 (23d5021361), 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 5565160 into main Nov 10, 2026
21 of 28 checks passed
@ibetitsmike
ibetitsmike deleted the mike/subagent-isolation-1-agent-worktrees branch November 10, 2026 04:35
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head df86079813162693a77cd935c9d7054e6f5f08f4:

  • fork-ci pull_request run 42754158434 passed; its build job checked out Merge df86079813 into 23d5021361 (current main). All checks green. Run 42748310543 had also passed on this head (all jobs, including agent-sdk and chat-tests).
  • Codex review round 10 ran on this exact head. Round 9's two findings are fixed in this head: the submodule walk now descends only into each git dir's own modules dir (a red/green test covers a repo inside submodule metadata), and create rollback uses the 10 minute request budget instead of 30s (a one-constant change). Round 8 had 4 findings: 2 fixed, 2 dropped because they need hand-made git internals. Round 10 raised 2 findings, both dropped with replies: ignored files such as .env are not counted as dirty, which matches git worktree remove and is documented in the PR body; and WrapAgentConn not forwarding ForkWorktreeConn has no caller. 0 unresolved review threads. Merge state CLEAN.
  • UAT on a branch server (:3200, fork experiments plus fork-subagent-isolation, fresh database) at faed039 with the stacked spawn-worktree change: PASS, 7 cases. Worktree spawn (path ~/.coder/worktrees/proj/<child id>, branch agent/<slug>-<8hex>, parent untouched, root 0700), base_branch=feature, shared isolation plus repo_path error, non-git repo_path error, missing agent directory error, bad isolation value, and default shared isolation unchanged. The children answered in each case.

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