Skip to content

chatd: subagent capacity is counted once per candidate, under a deployment-wide lock #301

Closed

Description

@ibetitsmike

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_agents calls 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-28 CountActiveSubagentsForCapacity: recursive lineage CTE (depth 64) plus chat_heartbeats JOIN chats with no organization or family filter on the counted side.
  • fork_list_agents.go:223-230: forkChatQueuedForCapacity inside the loop over children.
  • fork_capacity.go:150-192 forkChatQueuedForCapacity: for a running, unarchived chat it reads the organization limits, and when any concurrency limit is configured, GetChatAwaitingWorker, then CountActiveSubagentsForCapacity when 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-100 forkCapacityLimiter.Admit: SetTransactionLockTimeout, AcquireLock(LockIDChatCapacityAdmission) (one lock for the whole deployment), then the count, inside the worker's acquire transaction.
  • chat_capacity.sql:56-121 GetFamilySubagentsAwaitingWorker: one statement that returns the same counts for every awaiting subagent of a family. Used only by ForkChatFamilyCapacity (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

  1. One query for a list of candidates. Generalize GetFamilySubagentsAwaitingWorker to take candidate_ids uuid[] instead of a root ID (the awaiting CTE becomes WHERE c.id = ANY(@candidate_ids) with the same worker and heartbeat predicate). Delete CountActiveSubagentsForCapacity and GetChatAwaitingWorker.
  2. list_agents: collect the listed children, call the query once, fill WaitingForCapacity from the result. Read the organization limits once per call.
  3. GET /chats/{chat} and ChatQueuedForCapacity: call the same query with one ID.
  4. Admission: keep the count inside the acquire transaction, but take a per-family advisory lock (chatFamilyAdmissionLockID(root), already defined at fork_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.
  5. Lock timeout on the resume path. forkValidateSubagentResume takes the family advisory lock inside the SendMessage transaction with no lock timeout (fork_subagents.go:236-239), while Admit uses 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_agents with 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_agent send 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

  1. added
    enhancement
    performance
    on Nov 14, 2026
  2. ibetitsmike commented on Nov 16, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    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.

  3. ibetitsmike commented on Nov 16, 2026

    @ibetitsmike
    OwnerAuthor

    Coder Agents acted on behalf of @ibetitsmike.

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions