Skip to content

feat: enforce subagent concurrency limits behind fork-nested-subagents - #47

Merged
ibetitsmike merged 48 commits into
mainfrom
mike/nested-subagents-5-capacity
Nov 9, 2026
Merged

ibetitsmike merged 48 commits into
mainfrom
mike/nested-subagents-5-capacity

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 5 of the stack).

With fork-nested-subagents on, chatd caps how many subagent chats run at once: across the deployment, per organization and per family (one root chat and its descendants). With the experiment off, the wrapper passes every decision to upstream's limiter.

  • Deployment flags. --chat-max-concurrent-subagents and --chat-max-concurrent-subagents-per-family (CODER_CHAT_MAX_CONCURRENT_SUBAGENTS, CODER_CHAT_MAX_CONCURRENT_SUBAGENTS_PER_FAMILY). Both default to 0, which means no limit, and reject negative values.
  • Organization values. The max_concurrent and max_concurrent_per_family values that feat: store organization subagent limits and enforce max_depth behind fork-nested-subagents #45 stores now apply. Each lowers the deployment value; when the deployment has no limit, the organization value is the limit. The deployment flag counts subagents in every organization, and the organization value counts only its own.
  • Admission. forkCapacityLimiter wraps upstream's AgentCapacityLimiter. With the experiment on, it admits subagents itself: it takes the capacity admission lock and counts subagents that a worker owns with a fresh heartbeat, the same rule as upstream's pool count. It also applies upstream's subagent pool cap (10 unless Enterprise unlocks it) with that count. Root chats, and every chat with the experiment off, go to upstream's limiter unchanged.
  • No deadlock. The candidate's own ancestors are not counted, for the fork limits or the pool cap. A coordinator that blocks in wait_agent holds a slot, so its children must still be admitted. Upstream's own pool count would include the coordinators, so ten waiting coordinators would block every child.
  • No starvation. A refusal returns errSkipAcquire, which skips only that chat. A plain refusal makes the worker skip every subagent in the batch, so one full family would block every other family.
  • maxSubagentDepth becomes subagentLimits, which returns depth and capacity limits from one organization read.
  • max_live_per_family is still stored but not enforced. The next PR enforces it.
  • ARCHITECTURE.md gets only a TODO for the author.

About 550 hand-written lines, about half of them tests.

Validation
  • Ran with real Postgres in Colima: the full go test ./coderd/x/chatd/, plus ./coderd/database/forkdb/..., ./coderd/database/dbauthz/, ./codersdk/, the CLI help and YAML goldens (OSS and enterprise) and the swagger annotation tests. forkdb/generate.sh and the typesGenerated.ts regeneration leave no diff.
  • New tests: TestCountActiveSubagentsForCapacity (the query skips roots, stale heartbeats and the candidate's lineage, and counts deployment, organization and family separately), TestForkCapacityAdmission (the deployment limit spans organizations, ancestors do not count against the fork limits or the upstream pool, roots are not capped, experiment off), TestForkCapacityFullFamilyDoesNotBlockOtherFamilies (worker-level: a full family does not keep another family's subagent from starting) and TestForkChatSubagentCapacityOptions (flag defaults and validation).
  • UAT ran on a disposable dev deployment with a local model. Round 1 at a82387f passed every scenario. Round 2 at 10e310a passed the deadlock check under a per-family cap of 1 and a deployment cap of 1: a leaf was admitted while its coordinator held the only slot, and an unrelated family stayed refused. It also passed the sibling cap sanity check. The later commits merge main and reword the cap docs.
  • The deployment and organization cap docs state that a subagent's own ancestors do not count against it, so nested subagents can exceed a cap.

…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
Base automatically changed from mike/nested-subagents-4-org-limits to main November 9, 2026 16:56
…-5-capacity

Also states in the concurrency cap docs that a subagent's own ancestors do
not count against it.
@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: 390011f555

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

…on and family

A capped organization or family whose refused subagents rank oldest filled
the bounded candidate batch on every pass, so other organizations and
families never got admitted until that backlog drained. Rank subagent
candidates by organization, then family, then age. Roots keep age order.
@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: b32b951fbf

ℹ️ 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/database/queries/chats.sql Resolved
Comment thread coderd/x/chatd/ARCHITECTURE.md Resolved
Comment thread coderd/database/queries/chats.sql Resolved
…d order without the experiment

Interleaving only within each status let a capped family's unowned
interrupting subagents fill the batch ahead of every running subagent.
Ranking every unowned subagent also gave up the indexed age-order limit
for roots and for deployments without fork-nested-subagents, so only
the experiment's subagent branch ranks rows now.
@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: d319ad0ba6

ℹ️ 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/database/queries/chats.sql Resolved
Comment thread coderd/database/queries/chats.sql Resolved
…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. Delightful!

Reviewed commit: 1dbe22ad20

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

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Breezy!

Reviewed commit: 0e846e7b60

ℹ️ 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 closed this Nov 9, 2026
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

The head is unchanged (0e846e7b60). This reopen only triggers a pull_request fork-ci run against current main (e2f73afa5d). The last run (42680238723) started before main moved. #62 also changed the same generated forkdb, dbauthz and dbmetrics files, so the fresh run checks the combined result.

@ibetitsmike ibetitsmike reopened this Nov 9, 2026
@ibetitsmike
ibetitsmike merged commit 277d423 into main Nov 9, 2026
18 checks passed
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

What I checked before merging head 0e846e7b609426a64b8aed5622514d9654acb5a7:

  • fork-ci run 42688005139 passed. It ran as a pull_request run against current main (e2f73afa5d), after a close and reopen.
  • feat(coderd/database): store agent definitions for users and organizations #62 also changed the same generated forkdb, dbauthz and dbmetrics files. git merge-tree is clean, and the fresh run covers the combined tree.
  • The Codex receipt for 0e846e7b60 (19:10:18Z) reports no major issues. I confirmed each of the 5 earlier declines, and 0 threads are unresolved.
  • This PR adds 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