Skip to content

feat: store organization subagent limits and enforce max_depth behind fork-nested-subagents - #45

Merged
ibetitsmike merged 42 commits into
mainfrom
mike/nested-subagents-4-org-limits
Nov 9, 2026
Merged

ibetitsmike merged 42 commits into
mainfrom
mike/nested-subagents-4-org-limits

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

With fork-nested-subagents on, an organization admin can store subagent limits for their organization, and chatd enforces the organization's max_depth. With the experiment off, nothing reads the new table and the new routes answer 403.

  • Table. A new chatd graph node creates chatd.chat_organization_subagent_limits, keyed by organization_id (cascade delete). It has four nullable limits: max_depth (1 to 16), max_concurrent, max_concurrent_per_family and max_live_per_family (each at least 1). NULL means "use the deployment value", and a set value can only lower it.
  • API. GET and PUT /api/v2/chats/fork/organizations/{organization}/subagent-limits, with codersdk client methods. An unset limit is omitted from the JSON. Access follows the organization system prompt: reading needs read on the org's chat model config (other callers get 404), and writing needs update (403 when the caller can read but not update, 404 otherwise). Invalid values get 400 with per-field validations. A PUT replaces all four values.
  • Depth. Every depth check in fork_subagents.go uses the effective value: the deployment --chat-max-subagent-depth, lowered by the org's max_depth. That covers the spawn pre-check, the transactional recheck, the spawn tools, the orchestration prompt and the spawn_agent description (one line in subagent_catalog.go). If the description cannot read the org row, it states the deployment depth instead of failing the turn. Spawning still enforces the effective depth.
  • The three capacity limits are stored but not enforced. PR 5 enforces them.
  • 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/..., TestForkQuerier, ./coderd/ -run 'TestForkChatOrganizationSubagentLimits|TestForkChatRoutes', the swagger annotation tests (EndpointsDocumented|TestSwagger, OSS and enterprise) and ./coderd/database/migrations/chatdmig/.... forkdb/generate.sh and the typesGenerated.ts regeneration leave no diff, and chatdmig check passes.
  • New tests: TestUpsertChatOrganizationSubagentLimits, TestForkQuerier cases, TestForkChatOrganizationSubagentLimits (round trip, validation, member and auditor access, experiment off) and TestNestedSubagentOrganizationMaxDepth (an org value lowers the depth at every call site; a higher org value does not raise the deployment ceiling). Negative controls: 18 single-line production breaks each made a test fail.
  • UAT ran on a disposable dev deployment with a local model, at b98bc19. Later commits merge main, let update-only callers write, and omit unset limits from the JSON; they are covered by the tests above. Results: the API round trip, validation and access rules passed. With org max_depth 1, the coordinator had no spawn_agent and no grandchild existed. With no org max_depth, a depth-2 family completed. With org 16 above a deployment value of 2, a depth-2 worker could not spawn. With the experiment off, the routes returned 403 and spawning behaved as upstream.

…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

@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: 887d20c7b8

ℹ️ 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 docs/reference/api/chats.md Resolved
@ibetitsmike

Copy link
Copy Markdown
Owner Author

Xum commented on behalf of @ibetitsmike.

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

To use Codex here, create an environment for this repo.

…-3-cascade

# Conflicts:
#	coderd/x/chatd/ARCHITECTURE.md
#	coderd/x/chatd/fork_tools_internal_test.go
… into mike/nested-subagents-4-org-limits

# Conflicts:
#	coderd/fork_chat_routes.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. Keep them coming!

Reviewed commit: 22f0565d68

ℹ️ 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-3-cascade to main November 9, 2026 13:46
…-4-org-limits

# Conflicts:
#	coderd/x/chatd/ARCHITECTURE.md
#	coderd/x/chatd/fork_subagents.go
@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: cc38d735ae

ℹ️ 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_subagent_limits.go Resolved
Comment thread coderd/fork_subagent_limits.go Resolved
Comment thread coderd/x/chatd/fork_subagents.go Resolved
@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: 10431b8988

ℹ️ 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_subagents.go Resolved
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: 97544ab373

ℹ️ 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 106a935 into main Nov 9, 2026
10 checks passed
@ibetitsmike

Copy link
Copy Markdown
Owner Author

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

Merge evidence for head 97544ab3731845fc7f43ea47a2a0eddeded2cedc:

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