Skip to content

feat: report the subagent capacity of a nested chat family - #109

Merged
ibetitsmike merged 4 commits into
mainfrom
mike/nested-subagents-10-family-api
Nov 10, 2026
Merged

ibetitsmike merged 4 commits into
mainfrom
mike/nested-subagents-10-family-api

Conversation

@ibetitsmike

Copy link
Copy Markdown
Owner

Xum opened this PR on behalf of @ibetitsmike.

Part of #8 (W2 nested subagents, item 5b: family capacity in the sidebar). This is the server half. The sidebar badges follow in a separate PR that stacks on #104.

With fork-nested-subagents on, GET /api/v2/chats/fork/{chat}/family returns the subagent capacity of the family under a root chat:

{ "live_subagents": 3, "max_live_subagents": 6, "waiting_for_capacity": ["<chat id>"] }

With the experiment off, the route answers 403 like the other fork routes.

Why. The sidebar needs two facts that no chat response carries. The first is whether a running subagent waits for a concurrency slot. Today only list_agents and a single-chat GET (queued_for_capacity) report that, one chat at a time. The second is whether the family is at its live subagent limit, which makes the next spawn or message_agent resume fail. Computing either for every descendant on every sidebar list call would cost several queries per chat, so the sidebar asks once per family instead.

Change.

  • Server.ForkChatFamilyCapacity (new coderd/x/chatd/fork_family.go) reuses the admission rules. It reads the organization's effective limits (subagentLimits, upstream pool cap through subagentPoolLimit), counts live subagents with CountLiveSubagentsInFamily, and runs CountActiveSubagentsForCapacity plus subagentCapacityFull per candidate. That is the same check forkChatQueuedForCapacity makes for one chat.
  • A new forkdb query, GetFamilySubagentsAwaitingWorker, prefilters the candidates: the unarchived running subagents of the family that no worker with a fresh heartbeat owns, with the heartbeat predicate of GetChatAwaitingWorker. Only those get the per-chat count. With no concurrency limit set, the method skips both queries. The dbauthz wrapper authorizes through the root chat, like CountLiveSubagentsInFamily, and dbmetrics gets its timing wrapper.
  • The handler (new coderd/fork_chat_family.go, one route in fork_chat_routes.go) reads the chat through ExtractChatParam, so only callers who can read the root get an answer. A child chat gets 400. The counts run as chatd because they span other users' chats.
  • codersdk.ForkChatFamilyCapacity and the client method, plus the generated TypeScript type and swagger. The endpoint is x-apidocgen skip like the other fork chat routes. Only schemas.md gains the type, and the generated enum-order drift that check-apidoc.sh ignores is left out.
Validation
  • New TestForkChatFamilyCapacity (chatd, real Postgres in Colima): a family whose only per-family slot is held by a worker-owned subagent reports live_subagents: 2, max_live_subagents: 5 and the unowned running sibling as waiting. After the holder's heartbeat is gone, nothing waits.
  • New TestForkChatFamilyCapacity (coderd): Enabled returns the live count and the deployment live limit for a root with one paused and one finished subagent, 400 for a child, and 404 for another member. Disabled returns 403.
  • TestForkQuerier covers the new dbauthz wrapper. TestForkChatRoutes, EndpointsDocumented|TestSwagger, .github/fork/check-apidoc.sh, forkdb/generate.sh (no diff) and go vet on the touched packages also pass.

Adds GET /api/v2/chats/fork/{chat}/family behind fork-nested-subagents. For a
root chat it returns the family's live subagent count and live limit, and the
running subagents that wait for a concurrency slot.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum commented 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-10T05:42:04.362508Z d32bb19 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: f565c968ed

ℹ️ 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.go Resolved
Comment thread coderd/fork_chat_family.go Resolved
…escendants

GetFamilySubagentsAwaitingWorker now returns the capacity counts of every
waiting subagent, so the family endpoint makes one query instead of one per
waiting chat. The route uses Chat.IsSubChat so a descendant whose parent was
purged is not treated as a root.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum commented 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: dd21f532e4

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

…each chat's lineage

GetFamilySubagentsAwaitingWorker joined every waiting subagent with every
active subagent. It now counts the deployment, organization and family totals
once and subtracts each waiting chat's own active ancestors.
@ibetitsmike

Copy link
Copy Markdown
Owner Author

@codex review

Xum commented 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: cb34d8e97b

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

Copy link
Copy Markdown
Owner Author

@codex review

Xum commented on behalf of @ibetitsmike.

@chatgpt-codex-connector

Copy link
Copy Markdown

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

Reviewed commit: d32bb19dfc

ℹ️ 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 commented Nov 10, 2026 •

Copy link
Copy Markdown
Owner Author

Xum commented on behalf of @ibetitsmike.

UAT: family capacity endpoint

Dev server in the W2 UAT container, fork-nested-subagents on, org limits max_concurrent_per_family=1, max_live_per_family=4 (reset afterwards). A root spawns worker-a and worker-b (short notes), then waits on both. The script polls GET /api/v2/chats/fork/{root}/family every 3 s and logs each change next to the children's DB status and whether a worker owns them.

Run on cb34d8e (the query fix; d32bb19 only merges main, which changes generated doc text for Chat.children and nothing in this PR's code):

05:31:00 family={"live_subagents":0,"max_live_subagents":4,"waiting_for_capacity":[]} kids=[]
05:34:53 family={"live_subagents":2,"max_live_subagents":4,"waiting_for_capacity":["1a9befa4…"]} kids=[worker-a running owned; worker-b running unowned]
05:41:43 family={"live_subagents":1,"max_live_subagents":4,"waiting_for_capacity":[]} kids=[worker-a waiting; worker-b running unowned]
05:41:46 family={"live_subagents":1,"max_live_subagents":4,"waiting_for_capacity":[]} kids=[worker-a waiting; worker-b running owned]
05:46:21 family={"live_subagents":0,"max_live_subagents":4,"waiting_for_capacity":[]} kids=[both waiting]
child GET family -> HTTP 400 (both children)

What this shows:

  • Only the unowned running worker is listed as waiting while the family slot is held.
  • The list empties as soon as the slot frees, before the next worker is acquired.
  • The live count follows the running children.
  • A child chat gets 400.

The same lifecycle passed on dd21f53 (the earlier query shape), and on f565c96 for the waiting state and the 400. In that run the waiting worker also had queued_for_capacity=true on GET /api/v2/chats/{id}, which matches the family list.

@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 (6ae89b1c7f) 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 (6ae89b1c7f), 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 bfac64b into main Nov 10, 2026
20 of 27 checks passed
@ibetitsmike
ibetitsmike deleted the mike/nested-subagents-10-family-api branch November 10, 2026 06:27
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head d32bb19dfcf0773f68559bc836ffb1ffc0fec435:

  • fork-ci pull_request run 42766836586 passed; its build job checked out Merge d32bb19dfc into 6ae89b1c7f (current main). All checks green. Run 42763493067 had also passed on this head.
  • Codex on this exact head: no major issues (comment 6760019813). Fixed in earlier rounds: per-chat round trips replaced by one batched query and the child check switched to Chat.IsSubChat (dd21f53), and the capacity query rewritten to count totals once and subtract each waiting chat's own lineage, with a forkdb parity test against CountActiveSubagentsForCapacity (cb34d8e). Declined: an archived chat mid-purge with both ancestry fields nulled cannot be told from a root without a schema change, and the endpoint only reports display capacity. 0 unresolved review threads. Merge state CLEAN.
  • UAT PASS at cb34d8e (comment 6760428082), with fork-nested-subagents on and org limits max_concurrent_per_family=1, max_live_per_family=4: polling GET /api/v2/chats/fork/{root}/family listed only the unowned running worker as waiting while the family slot was held, emptied the list as soon as the slot freed, tracked the live count, and returned 400 for a child chat. This head is cb34d8e plus a merge of main 26c1e02 that changes only generated doc text in this PR's files.

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