Repository navigation
Closed
Labels
Description
Filed by Xum (an AI coding agent) on behalf of @ibetitsmike. Part of #311. Line numbers are at 607a95b.
Summary
"Is this subagent waiting for a capacity slot" is answered by CountActiveSubagentsForCapacity, which joins every fresh heartbeat in the deployment to its chat and walks the candidate's ancestors with a recursive CTE. It is called once per candidate:
list_agentscalls it for every running child it lists, up to 50, each with its own limits read and ownership read, so one tool call can issue 150 statements;GET /chats/{chat}calls it for a running subagent on the 5 s page poll;- the worker calls it under a deployment-wide advisory lock for every subagent it tries to acquire.
A set-based query for the family already exists and is used by one endpoint. Use that shape everywhere and narrow the lock.
Evidence
chat_capacity.sql:1-28CountActiveSubagentsForCapacity: recursivelineageCTE (depth 64) pluschat_heartbeats JOIN chatswith no organization or family filter on the counted side.fork_list_agents.go:223-230:forkChatQueuedForCapacityinside the loop over children.fork_capacity.go:150-192forkChatQueuedForCapacity: for a running, unarchived chat it reads the organization limits, and when any concurrency limit is configured,GetChatAwaitingWorker, thenCountActiveSubagentsForCapacitywhen no worker owns the chat. Up to 3 statements per running child; 1 (the limits read) when no limit is configured.fork_capacity.go:94-100forkCapacityLimiter.Admit:SetTransactionLockTimeout,AcquireLock(LockIDChatCapacityAdmission)(one lock for the whole deployment), then the count, inside the worker's acquire transaction.chat_capacity.sql:56-121GetFamilySubagentsAwaitingWorker: one statement that returns the same counts for every awaiting subagent of a family. Used only byForkChatFamilyCapacity(fork_family.go:22-67).
chat_heartbeats is small and unlogged, so one execution is cheap. The cost is the multiplier (per child, per poll) and the serialization of every admission in the deployment behind one lock.
Proposed design
- One query for a list of candidates. Generalize
GetFamilySubagentsAwaitingWorkerto takecandidate_ids uuid[]instead of a root ID (theawaitingCTE becomesWHERE c.id = ANY(@candidate_ids)with the same worker and heartbeat predicate). DeleteCountActiveSubagentsForCapacityandGetChatAwaitingWorker. list_agents: collect the listed children, call the query once, fillWaitingForCapacityfrom the result. Read the organization limits once per call.GET /chats/{chat}andChatQueuedForCapacity: call the same query with one ID.- Admission: keep the count inside the acquire transaction, but take a per-family advisory lock (
chatFamilyAdmissionLockID(root), already defined atfork_subagents.go:257-259) when only the family limit is configured, and the deployment lock only when a deployment or organization limit is configured. Two families then admit in parallel. - Lock timeout on the resume path.
forkValidateSubagentResumetakes the family advisory lock inside theSendMessagetransaction with no lock timeout (fork_subagents.go:236-239), whileAdmituses 5 s. Set the same timeout there, so a message to a child cannot wait without bound while holding the child's row lock.
Acceptance criteria
- A store-call test of
list_agentswith 50 running children asserts at most 2 capacity statements in total (one limits read, one batched query), down from up to 150. - The existing capacity tests (
chat_capacity_test.go, family limit, organization limit, ancestors do not count) pass against the batched query. - A test holds the family admission lock in another transaction and asserts that a
message_agentsend fails with the lock timeout error after 5 s of mock time, not later. - Two spawns in different families with only family limits configured do not block each other (test with a held lock on one family).
Activity
ibetitsmike commentedon Nov 16, 2026
Started. A workstream (branch
mike/issue-301-batched-capacity) is implementing the batched capacity query, the per-family admission lock and the lock timeout on the resume path.ibetitsmike commentedon Nov 16, 2026
Closed by #359, merged as d4b1539 at 06:37Z. Subagent capacity is now counted for every candidate in one statement (
GetSubagentCapacityCounts, with the family filter in SQL for the sidebar). Admissions under only a per-family limit take a per-family capacity lock and then the deployment lock in shared mode, so families admit in parallel. A resume waits at most 5 s for the family lock. An Opus review found a deadlock in an earlier head; it was fixed before merge. Gates and test evidence: #359 (comment). Not exercised: a replay of the full deadlock cycle, and a running server.