Skip to content

feat: enforce the live subagent limit per family behind fork-nested-subagents - #49

Merged
ibetitsmike merged 66 commits into
mainfrom
mike/nested-subagents-6-live-cap
Nov 9, 2026
Merged

ibetitsmike merged 66 commits into
mainfrom
mike/nested-subagents-6-live-cap

Conversation

@ibetitsmike

@ibetitsmike ibetitsmike commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

Part of #8 (W2 nested subagents, PR 6 of the stack). Stacked on #47: review only the commits after #47.

With fork-nested-subagents on, spawning a subagent fails when its family (one root chat and its descendants) already has the maximum number of live subagents. With the experiment off, nothing changes.

  • Live. A live subagent is not archived and has status running, interrupting, requires_action or paused. A subagent that finished (waiting or error) does not count, so the family can spawn again once a child finishes.
  • Limits. A new deployment flag --chat-max-live-subagents-per-family (CODER_CHAT_MAX_LIVE_SUBAGENTS_PER_FAMILY), default 0 (no limit), negative values rejected. The organization's max_live_per_family, stored since feat: store organization subagent limits and enforce max_depth behind fork-nested-subagents #45, now applies and lowers the deployment value, the same rule as the concurrency limits in feat: enforce subagent concurrency limits behind fork-nested-subagents #47.
  • Spawn checks. The spawn pre-check returns the error to the tool. The spawn transaction then locks the family root, as family operations do, and checks again under a per-family advisory lock, so two concurrent spawns cannot both pass the count. The error names the count and the maximum.
  • Resume check. message_agent to a finished child (waiting or error) would make it live again. sendSubagentMessage runs the same count before SendMessage, so a capped resume fails before any prompt hook fires. It also marks its send context, and SendMessage counts again inside the transaction that resumes the child, under the same per-family advisory lock, so a resume and a concurrent spawn cannot both pass the count. Owner messages through the chat API are not limited. This adds one hook line to upstream's SendMessage and a few lines to sendSubagentMessage.
  • Unlike the concurrency limits, this one refuses the spawn instead of queueing the child.
  • ARCHITECTURE.md gets only a TODO for the author.
Validation
  • Ran with real Postgres in Colima: the full go test ./coderd/x/chatd/, plus ./coderd/database/forkdb/..., ./coderd/database/dbauthz/, ./codersdk/ and the CLI help and YAML goldens (OSS and enterprise). forkdb/generate.sh and the typesGenerated.ts regeneration leave no diff.
  • New tests: TestCountLiveSubagentsInFamily (counts only unarchived, unfinished subagents of the family), TestNestedSubagentLiveLimit (a second spawn fails at the limit, the limit covers grandchildren, the pre-check fails too, and a finished child frees the slot, and message_agent cannot resume a finished child over the limit), TestNestedSubagentLiveLimitSerializesResume (a resume waits for a spawn that holds the family lock, then counts its child), plus the flag case in TestForkChatSubagentCapacityOptions.
  • UAT: pending, results will follow in a comment.
  • fork-ci triggers only on PRs to main, so CI for this stacked PR runs through workflow_dispatch on the branch until it targets main.

…s, direct children only

With fork-nested-subagents on, a general subagent below the maximum depth
gets spawn_agent and the other subagent tools, keeps the orchestration
prompt, and the subagent tools act only on the caller's direct children.
With the experiment off, tools, prompts and target rules stay upstream's.
…nd spawn description

Assert that wait, message and interrupt reject a grandchild, that list_agents works on a delegated chat and lists direct children only, that the spawn_agent description rewrite applies with the configured depth, and that appendForkTools leaves delegated chats untouched with fork experiments off.
The Coder-Ancestor-Chat-Ids header carried only the immediate parent, so a
grandchild's file edits and commands were never attributed to the family root's
path store and the root's git watch and diff omitted that work.

Resolve the chain with a new chatd helper: nil for a root, the parent alone when
fork-nested-subagents is off (upstream behavior, no extra queries), and the full
chain nearest ancestor first up to the root when it is on. A failed lookup warns
and keeps the ancestors found so far.
…nto mike/nested-subagents-2-tools

# Conflicts:
#	coderd/x/chatd/ARCHITECTURE.md
#	coderd/x/chatd/fork_tools.go
…-2-tools

# Conflicts:
#	coderd/x/chatd/fork_subagents.go
#	coderd/x/chatd/fork_subagents_internal_test.go
…nto mike/nested-subagents-3-cascade

# Conflicts:
#	coderd/database/dbauthz/fork_forkdb.go
#	coderd/database/dbauthz/fork_forkdb_test.go
#	coderd/database/dbmetrics/fork_forkdb.go
#	coderd/database/forkdb/forkdbmock/forkdbmock.go
#	coderd/database/forkdb/querier.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum commented on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 46c32d3c1a

ℹ️ 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".

…ir family

A coordinator that holds its family's last slot while it waits in
wait_agent can only make progress through its own child, which excludes
it from the family count. Older queued siblings, which cannot fit, kept
that child out of the bounded candidates.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum commented on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: 7f4041dd25

ℹ️ 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".

…' into mike/nested-subagents-6-live-cap

# Conflicts:
#	coderd/x/chatd/fork_subagents.go
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum commented on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: b45a82e669

ℹ️ 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 commented 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: 15acf45ae9

ℹ️ 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 codersdk/fork_subagent_limits.go Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum commented on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: 2125a722d5

ℹ️ 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".

Base automatically changed from mike/nested-subagents-5-capacity to main November 9, 2026 20:43
Resolved against #47's head 0e846e7 as the merge base, because main's
tree is exactly #47's merge with e2f73af.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum commented on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Already looking forward to the next diff.

Reviewed commit: 86b7199e9c

ℹ️ 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
ibetitsmike merged commit b96ada7 into main Nov 9, 2026
9 checks passed
@ibetitsmike
ibetitsmike deleted the mike/nested-subagents-6-live-cap branch November 9, 2026 21:05
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head 86b7199e9c2e141a509fc3eb045010f01679e57d:

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