## Problem
The concurrent-agent cap (5 root chats and 10 subagent chats per
deployment by default) could be exceeded. Three paths put more chats in
`running` with a fresh lease than the pool allows:
1. **Interrupting a waiting chat.** A user sends a message with
`busy_behavior=interrupt` to a chat that is waiting for capacity. The
chat becomes `interrupting` while unowned, a worker acquires it without
admission, and `FinishInterruption` promotes the queued message into a
running turn under that ownership.
2. **Taking over an orphaned `requires_action` chat.** A replica dies
while its chat is in `requires_action`, its lease goes stale and another
chat takes the slot. A worker acquires the `requires_action` chat
without admission, and a tool result, an interrupt or the action
deadline returns it to `running` under that ownership.
3. **Heartbeat reviving a stale lease.** A replica loses the database
for longer than the 30 second stale threshold and another replica admits
a second chat into the slot. When the first replica comes back, its
heartbeat upsert renews the stale lease unconditionally, and its runner
keeps generating until the turn ends.
## Root cause
`agentCapacityLimiter.Admit` returned `true` without taking the
admission lock or counting for every status other than `running`. The
count query already treats every owned chat with a fresh lease as
holding a slot, so the shortcut let chats acquire a slot uncounted and
later move back to `running` under that ownership. Separately,
`BatchUpsertChatHeartbeats` had no freshness or ownership condition.
## Fix
- **Admission for every runnable status.** `Admit` now counts for
`running`, `interrupting` and `requires_action`, and a refused pool
skips the rest of its candidates in the pass regardless of status. An
owned chat holds its slot until it releases ownership, so every later
transition back to `running` stays within capacity.
- **Fenced heartbeat renewal.** `BatchUpsertChatHeartbeats` is replaced
by `RenewChatHeartbeats`, which only extends rows that are still fresh
and still own their chat, using `statement_timestamp()` read after the
capacity admission lock is granted. The renewal runs under that lock
with a `lock_timeout` (`HeartbeatLockTimeout`) and a per-tick timeout
(`HeartbeatRenewalTimeout`, a third of the stale threshold). Admission
also waits at most 5 seconds for the lock.
- **Stop runners that lost their lease.** `heartbeatOnce` requests
cleanup of every runner whose lease was not renewed, which cancels its
generation. The chat stays owned by the stale lease until an admitted
worker takes it over. A tick that fails or times out cleans up no
runners and retries on the next tick.
- **Instant Stop on a queued chat.** Requiring admission for
`interrupting` would have made Stop on a chat waiting for capacity hang
until a slot frees. When `Interrupt`, or `SendMessage` with
`busy_behavior=interrupt`, runs on a running chat with no owner, the
transition now applies `FinishInterruption` in the same transaction and
cancels orphaned tool calls. An empty queue lands in `waiting`;
otherwise the queue head is promoted into `running`, still unowned, so
it goes through admission.
- **Queued metric.** `CountChatCapacityQueuedByPool` (behind
`coderd_chatd_agents_queued_for_capacity`) now also counts
`interrupting` and `requires_action` chats without a fresh lease, since
they also wait for admission.
## How it was found
A TLA+ model of capacity admission found a counterexample for each path.
Go tests reproduced all three against the previous code (each failed
with `"2" is not less than or equal to "1"` for a root pool of capacity
1), and the model with the fix enabled passes the safety invariant and
the liveness properties. The model itself is not part of this PR.
## Tests
The three reproductions are kept as regression tests in
`coderd/x/chatd/agentcapacity_regression_internal_test.go`:
`TestAdmission_InterruptWithMessageOnWaitingChatStaysWithinCapacity`,
`TestAdmission_RequiresActionTakeoverStaysWithinCapacity` and
`TestRunnerManager_HeartbeatDoesNotReviveStaleLease`. The original
reproductions failed on the base commit. Restoring the admission
shortcut makes the `requires_action` test fail, and restoring the
unconditional heartbeat upsert, or dropping the runner cleanup, makes
the heartbeat test fail. New coverage also includes
`TestAdmission_StopWaitingChatNeedsNoCapacity`,
`TestRenewChatHeartbeats_OnlyRenewsFreshOwnedLeases`,
`TestRunnerManager_FailedHeartbeatTickKeepsRunners`, and chatstate tests
for the unowned interrupt paths. Existing tests that encoded the old
behavior (`requires_action` chats holding no slot, interrupting chats
always acquirable, unconditional heartbeat renewal) were updated.
## Behavior / API change
- `POST /api/v2/chats/{chat}/messages` with `busy_behavior=interrupt` on
a chat that no worker owns (for example one waiting for capacity) now
returns the promoted message with `queued=false` instead of a queued
message, and the chat stays in `running` instead of `interrupting`. If
the chat already had queued messages, the queue head is promoted and the
new message stays queued.
- `POST /api/v2/chats/{chat}/interrupt` on such a chat lands it in
`waiting` immediately (or in `running` with the queue head promoted)
instead of `interrupting`.
- An unowned `interrupting` or `requires_action` chat (for example after
its replica died) now waits for a free slot before its runner starts,
which also delays enforcing its action deadline while the pool is full.
- New internal chat worker settings `HeartbeatRenewalTimeout` and
`HeartbeatLockTimeout`, defaulting to a third of the stale threshold and
half of that.
- Heartbeat ticks now take the capacity admission advisory lock briefly
on each replica.
## Out of scope / follow-ups
- Interrupting a chat still owned by a dead runner (stale lease,
`worker_id` set) still waits for a slot.
- An admission lock timeout should end the acquisition pass instead of
logging a warning per candidate.
- The per-chat `queued_for_capacity` flag in the chat API still reports
only `running` chats.
- Alert on failed heartbeat ticks.
`coderd/x/chatd/ARCHITECTURE.md` gains TODO lines for the affected
sections; the prose is left for the owner.
---
_Generated with [`xum`](https://github.com/coder/xum) • Model:
`anthropic:claude-opus-5-5` • Thinking: `high`_
100 lines
3.1 KiB
Go
100 lines
3.1 KiB
Go
package chatd
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
)
|
|
|
|
const (
|
|
defaultMaxConcurrentRootAgents = int64(5)
|
|
defaultMaxConcurrentSubagents = int64(10)
|
|
|
|
// admissionLockTimeout bounds how long admission waits for the
|
|
// capacity admission lock. Holders keep it for milliseconds.
|
|
admissionLockTimeout = 5 * time.Second
|
|
)
|
|
|
|
// AgentCapacityLimiter controls chat admission and reports the current per-pool limits.
|
|
type AgentCapacityLimiter interface {
|
|
// Admit runs inside the acquisition transaction so its serialization
|
|
// extends through the ownership write. Refused chats remain unowned.
|
|
// Every runnable status needs admission: an owned chat holds its slot
|
|
// until it releases ownership, so later transitions back to running
|
|
// (finishing an interruption, resolving requires_action) stay within
|
|
// capacity.
|
|
Admit(ctx context.Context, store database.Store, chat database.Chat) (bool, error)
|
|
Limits() (limits AgentCapacityLimits, capped bool)
|
|
}
|
|
|
|
// AgentCapacityUnlock reports whether the default chat agent caps are disabled.
|
|
type AgentCapacityUnlock interface {
|
|
Unlocked() bool
|
|
}
|
|
|
|
// AgentCapacityLimits defines concurrent-agent limits for root and subagent pools.
|
|
type AgentCapacityLimits struct {
|
|
Root int64
|
|
Subagent int64
|
|
}
|
|
|
|
type agentCapacityLimiter struct {
|
|
unlock AgentCapacityUnlock
|
|
|
|
staleSeconds int32
|
|
rootCapacity int64
|
|
subagentCapacity int64
|
|
}
|
|
|
|
func newAgentCapacityLimiter(unlock AgentCapacityUnlock, staleSeconds int32) *agentCapacityLimiter {
|
|
return &agentCapacityLimiter{
|
|
unlock: unlock,
|
|
staleSeconds: staleSeconds,
|
|
rootCapacity: defaultMaxConcurrentRootAgents,
|
|
subagentCapacity: defaultMaxConcurrentSubagents,
|
|
}
|
|
}
|
|
|
|
func (a *agentCapacityLimiter) Admit(ctx context.Context, store database.Store, chat database.Chat) (bool, error) {
|
|
//nolint:gocritic // Capacity accounting is chatd-internal state.
|
|
ctx = dbauthz.AsChatd(ctx)
|
|
if a.unlocked() {
|
|
return true, nil
|
|
}
|
|
// A stalled lock holder fails this candidate, which the next
|
|
// acquisition pass retries, instead of blocking the pass.
|
|
if err := store.SetTransactionLockTimeout(ctx, admissionLockTimeout.Milliseconds()); err != nil {
|
|
return false, err
|
|
}
|
|
// The transaction lock remains held through the caller's ownership write,
|
|
// preventing replicas from over-admitting the pool.
|
|
if err := store.AcquireLock(ctx, database.LockIDChatCapacityAdmission); err != nil {
|
|
return false, err
|
|
}
|
|
counts, err := store.CountChatCapacityActiveByPool(ctx, database.CountChatCapacityActiveByPoolParams{
|
|
ExcludeChatID: chat.ID,
|
|
StaleSeconds: a.staleSeconds,
|
|
})
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
used, capacity := counts.ActiveRootCount, a.rootCapacity
|
|
if chat.ParentChatID.Valid {
|
|
used, capacity = counts.ActiveSubagentCount, a.subagentCapacity
|
|
}
|
|
return used < capacity, nil
|
|
}
|
|
|
|
func (a *agentCapacityLimiter) Limits() (AgentCapacityLimits, bool) {
|
|
return AgentCapacityLimits{
|
|
Root: a.rootCapacity,
|
|
Subagent: a.subagentCapacity,
|
|
}, !a.unlocked()
|
|
}
|
|
|
|
func (a *agentCapacityLimiter) unlocked() bool {
|
|
return a.unlock != nil && a.unlock.Unlocked()
|
|
}
|