Repository navigation
perf(coderd/x/chatd): count subagent capacity in one statement and lock per family - #359
Conversation
…ck per family Replace the per-candidate capacity queries with GetSubagentCapacityCounts, which counts every candidate in one set-based statement. list_agents reads organization limits once per organization and runs one batched query. Admission takes the shared deployment lock plus the per-family lock when only a family limit is configured, so spawns in different families do not serialize. Resume validation now uses the same 5 s lock timeout as Admit. Closes #301
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
|
Codex Review: Didn't find any major issues. Bravo. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…capacity-count-47cef674
Merged main (#351) into the branch so it is not behind. No conflicts. Regenerated forkdb code (no diff). Reran chatd, forkdb, dbmetrics, dbauthz fork and plugins tests: all ok. @codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…capacity-count-47cef674
Merged main (#357) into the branch so it is not behind. No conflicts. Regenerated forkdb code (no diff). Reran chatd, forkdb, dbmetrics, chatdmig and dbauthz fork tests: all ok. @codex review |
|
Codex Review: Didn't find any major issues. Already looking forward to the next diff. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…capacity-count-47cef674
Merged main (#360) into the branch so it is not behind. No conflicts. Regenerated forkdb code (no diff). Reran chatd, forkdb, dbmetrics, plugins, enterprise/dbcrypt and dbauthz fork tests: all ok. @codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
A family-mode Admit held the shared deployment admission lock while it waited for the family lock that spawns and resumes hold. A resume holds that lock and waits for a sender row that a lease takeover locks, and the takeover waits for the exclusive deployment lock, so the three could deadlock. Admit now takes a separate per-family capacity lock. Spawns and resumes never change the active count, so Admit need not wait for them. The resume path also clears its 5 s lock timeout once it holds the family lock, so EditMessage and the later admission wait as before.
Pushed 1ca9ece to fix a regression found in review. Family-mode Admit now takes its own lock, @codex review |
|
Codex Review: Didn't find any major issues. Swish! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
…capacity-count-47cef674
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: ae328f99fe
ℹ️ 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".
…in SQL A family-mode admission now takes the family capacity lock before the shared deployment lock, so admissions that wait for their family do not hold the shared lock and delay heartbeat renewal. GetSubagentCapacityCounts takes an optional root_chat_id and selects the family's candidates itself, so the family sidebar no longer loads every family chat ID into Go and sends it back.
@codex review |
|
Codex Review: Didn't find any major issues. 🚀 Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
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". |
Merged at head Gates on this exact head:
Tests I ran on the PR commit at
On the final head
Lock order after this PR, from an Opus review of Not exercised: a test that replays the full resume, takeover and admission cycle; a test that the resume path clears its lock timeout; the spawn path's family lock still has no timeout (unchanged); a running server or the UI. |
Closes #301
What changed
All of this sits behind
fork-nested-subagents. Fork logic stays infork_*.gofiles. No upstream file gained a hook line.GetSubagentCapacityCountstakescandidate_ids uuid[]and an optionalroot_chat_id. Candidates are the listed IDs plus, when the root is set, every chat of that family. For each candidate it returns the deployment, organization and family active counts. Each count excludes the candidate's own active lineage.awaiting_onlylimits the rows to candidates that wait for a worker.CountActiveSubagentsForCapacity,GetChatAwaitingWorkerandGetFamilySubagentsAwaitingWorker. Nothing else used them.AcquireSharedLock(pg_advisory_xact_lock_shared). dbauthz and dbmetrics wrappers were added for both queries.list_agentsgroups running children by organization. It reads the limits once per organization and runs one batched count.GET /chats/{chat}(ChatQueuedForCapacity) andAdmituse the same query with one ID. The family sidebar uses it with the root ID, so the family is filtered in SQL in one statement and the result stays in ID order.chatFamilyCapacityLockID(root)(GenLockID("fork_chat_family_capacity:"+root)). Admissions in different families no longer wait for each other. This key is separate fromchatFamilyAdmissionLockID, which spawns and resumes hold. Spawns and resumes never change the active count, so Admit does not wait for them. Reusing that key could deadlock: a resume holds it and waits on the sender row (FK insert), a lease takeover holds the sender row and waits on the exclusive deployment lock, and Admit holds the shared deployment lock while it waits on the family lock.leaseStaleUnderAdmissionLocknever has to upgrade a shared lock.forkValidateSubagentResumesets the same 5 s lock timeout asAdmitbefore it takes the family lock, and resets it to 0 (SET LOCAL lock_timeout) right after the lock is acquired. The rest of theSendMessagetransaction, includingEditMessageand the later FK and admission waits, waits as before.ARCHITECTURE.mddescribes the batched statement, the lock modes and the resume lock wait.Tests I ran
Head
fb0072acbd, after the Codex lock order and sidebar fixes (all underflock ~/.fork-go.lock):go test ./coderd/x/chatd/ -count=1 -v: ok. 2560 PASS, 0 FAIL.go test ./coderd/x/chatd/ -run TestForkCapacityAdmissionLockScope -count=5: ok.go test ./coderd/database/forkdb/ ./coderd/database/dbmetrics/ -count=1 -v: ok. 66 PASS, 0 FAIL.go test ./coderd/database/dbauthz/ -run "TestForkQuerier|TestMethodTestSuite" -count=1: ok.go test ./coderd/ -run TestForkChatFamilyCapacity -count=1 -v: 3 PASS, 0 FAIL.go build ./...andgo vet ./coderd/x/chatd/: clean.gofmt -l coderd/: clean.golangci-lint run --fix=false --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/... ./coderd/database/...: 0 issues.Head
ae328f99fe, after the deadlock fix:go test ./coderd/x/chatd/ ./coderd/database/forkdb/ ./coderd/database/dbmetrics/ -count=1 -v: ok for all 3 packages. 2624 PASS, 0 FAIL.go test ./coderd/ -run TestForkChatFamilyCapacity -count=1 -v: 3 PASS, 0 FAIL.go build ./...andgo vet ./coderd/x/chatd/: clean.gofmt -l coderd/: clean.golangci-lint run --fix=false --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/... ./coderd/database/...: 0 issues.Earlier heads:
flock ~/.fork-go.lock go test ./coderd/x/chatd/ ./coderd/database/forkdb/ ./coderd/database/dbmetrics/ -count=1 -v: ok for all 3 packages. 2618 PASS, 0 FAIL.flock ~/.fork-go.lock go test ./coderd/database/dbauthz/ -run "TestForkQuerier|TestMethodTestSuite" -count=1: ok.flock ~/.fork-go.lock go test ./coderd/ -run TestForkChatFamilyCapacity -count=1 -v: PASS (Enabled, Disabled).go veton the touched packages: clean.gofmt -l: clean.golangci-lint run --fix=false --config=.golangci.new-code.yaml --new-from-merge-base=origin/main ./coderd/x/chatd/... ./coderd/database/...: 0 issues.New tests:
TestGetSubagentCapacityCountsandTestGetSubagentCapacityCountsAwaitingOnly(forkdb): several candidates across 2 organizations in one call. The second test also selects byroot_chat_idand checks it returns the same 3 rows as the explicit IDs, in ID order.TestForkListAgentsCapacityStatements: 50 running children. It countsGetChatOrganizationSubagentLimitsplusGetSubagentCapacityCountsthrough dbmetrics and asserts at most 2.TestForkCapacityAdmissionLockScope:chatFamilyAdmissionLockIDof its family (the spawn and resume lock).chatFamilyCapacityLockID, and while it waits only 1 transaction holds the deployment lock.TestNestedSubagentResumeLockTimeout: holds the family admission lock in another transaction. Amessage_agentsend fails with "lock timeout" after about 5 s while its context is still live. This uses the real Postgres lock timeout, because mock time cannot shorten a wait inside Postgres. The test takes about 5.2 s.Negative controls
Each one was a temporary revert, and the code was restored afterwards.
list_agents, the statement test fails: 102 statements, not at most 2.chatFamilyAdmissionLockID, the lock scope test fails at the new assertion (line 596) with "canceling statement due to lock timeout" after 5.23 s.AND false),TestGetSubagentCapacityCountsAwaitingOnlyfails (0 rows, not 3) andTestForkChatFamilyCapacityfails (emptyWaitingForCapacity).Not verified and residual risk
make genstops athelm/ai-gateway/testsbecause helm is not installed in this workspace.make -k genproduced no diff in the files this PR touches. The forkdb code was generated withcoderd/database/forkdb/generate.sh.