Skip to content

feat(coderd/x/chatd): remove subagent worktrees when their family is archived - #138

Merged
ibetitsmike merged 4 commits into
mainfrom
mike/subagent-isolation-4-archive-cleanup
Nov 10, 2026
Merged

ibetitsmike merged 4 commits into
mainfrom
mike/subagent-isolation-4-archive-cleanup

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 and #135 (both merged), which give a spawn_agent child with isolation: worktree its own git worktree and branch but never remove them.

With experiment fork-subagent-isolation on, archiving a chat family now cleans up the worktrees of its subagents:

  • After the archive commits, a background task asks the workspace agent to remove each worktree subagent's worktree and branch. The agent's existing delete endpoint (feat: add worktree isolation to spawn_agent #108) only removes a worktree that is clean, still on its branch, and has no commits that no other branch or remote holds.
  • Each worktree the agent keeps, and each one it could not be asked about, is listed in one user-visible system notice on the root chat with its path, branch and reason, for example worktree /x has 2 uncommitted or untracked changes; commit or discard them first, agent unreachable, or that the agent is too old to remove worktrees. The server also logs one line per kept worktree.
  • The notice starts no turn and leaves the root archived. A worktree that is already gone counts as removed.
  • The cleanup runs only after the archive transaction commits and outside every chat lock, so an unreachable or slow agent cannot fail or delay the archive. It is best effort: dialing each agent is capped at the chatd dial timeout (30s), all agent calls together at 10 minutes, and the notice is posted even when that deadline runs out.

Both the archive API and the auto-archive worker go through forkSetFamilyArchived, so both clean up. Unarchiving does nothing to worktrees. With the experiment off, nothing changes.

Size: about 220 production lines and 180 test lines.

Code

  • coderd/x/chatd/fork_family_lock.go: forkSetFamilyArchived wraps the lock-ordered family update (renamed setFamilyArchivedInLockOrder) and schedules the cleanup when an archive succeeds.
  • coderd/x/chatd/fork_isolation_cleanup.go: scheduleArchivedWorktreeCleanup runs the cleanup as tracked in-flight work, groups the worktree subagents by workspace agent, calls ForkDeleteWorktree for each, maps the result to a kept reason (worktreeKeptReason), and posts the notice.
  • coderd/x/chatd/chatstate/fork_notice.go: Tx.ForkAppendNotice inserts a user-visible system message through ChatMachine.Update without changing the execution state or the archived flag, since every chat message write goes through a chatstate transition.
  • coderd/x/chatd/ARCHITECTURE.md: a TODO for the cleanup and the new notice write.

Tests

TestForkArchiveWorktreeCleanup archives a root whose children have fake worktrees on a recording agent connection:

  • RemovesCleanWorktreesAndListsKept: a clean worktree and an already-deleted one are removed and left out of the notice. A dirty worktree (409), an agent without the route (404 Route not found.) and a delete that fails in transport are kept and listed with their reasons. Every delete sends the subagent's stored branch, a shared child gets no delete, and the root stays archived in the same status.
  • AgentUnreachable: with no agent connection, the worktree is listed as agent unreachable and no delete is sent.
  • AgentDialTimesOut: a dial that waits until its context ends, as a dial to an offline agent does, gives up at the dial timeout, and the worktree is listed as agent unreachable.

Negative controls, each reverted after the run: removing the schedule call, treating a 409 as removed, treating a transport error as removed, and dialing without the dial timeout each make TestForkArchiveWorktreeCleanup fail.

Known gaps

  • The cleanup dials the agent stored on each child chat, as the rest of chatd does. After a workspace rebuild that agent is gone, so its worktrees are kept as agent unreachable and listed in the notice.
  • Unarchiving does not recreate removed worktrees. The branches of kept worktrees stay as they are.

UAT

On a branch server at e03161c (before the dial fix), with workspace agent e03161c and a real model, a root spawned three worktree children. One child stayed clean, one left an untracked file, and one committed without pushing. Archiving the root through PATCH /api/v2/chats/{id} returned 204. About 30 seconds later the clean child's worktree and branch were gone from git worktree list and git branch -a. The other two were kept, and the root got one notice listing both with the agent's reasons (has 1 uncommitted or untracked changes, has 1 commits that are not on any other branch or remote). The root stayed archived and waiting, with no turn after the notice, and the main checkout stayed on main.

@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

@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-10T12:30:41.050810Z 93463b5 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: e03161c528

ℹ️ 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_family_lock.go Resolved
Comment thread coderd/x/chatd/chatstate/fork_notice.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

@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: 93463b5817

ℹ️ 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_family_lock.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum posted this on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: 93463b5817

ℹ️ 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 (3036142c68) 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 (3036142c68), 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

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 (ad1010782c) 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 (ad1010782c), 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 77b2073 into main Nov 10, 2026
32 of 46 checks passed
@ibetitsmike
ibetitsmike deleted the mike/subagent-isolation-4-archive-cleanup branch November 10, 2026 13:40
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head 93463b58175b4072a6f6af14cc6ea34395774f17:

  • fork-ci pull_request run 42823295098 on the merge with current main ad1010782c passed (its build job checked out Merge 93463b5817 into ad1010782c). Run 42817462083 had also passed on this head against 3036142c68. All checks green.
  • Codex on this exact head: no major issues (comment 6767369875). 0 unresolved review threads; the dropped findings are answered in their threads.
  • UAT PASS on a branch server at e03161c528 (PR body, UAT): archiving a root removed its clean worktree child's worktree and branch and kept the dirty and unpushed ones with a notice. Accepted as covering this head: the only later change bounds the agent dial and keeps the notice on the caller's context (fork_isolation_cleanup.go), covered by fork_isolation_cleanup_internal_test.go.
  • No migration.

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