Repository navigation
Description
Summary
POST /api/agents/{name}/task only enqueues to the persistent backlog (BACKLOG-001) when the request has async_mode=true. Synchronous parallel-task calls (the default for chat_with_agent from MCP without async) hit the slot service, get rejected on Agent at capacity (N/N parallel tasks running), and fail terminally without ever touching the backlog. Result: heavy fan-out callers (one parent agent dispatching 5-7 parallel tasks/minute to a 3-slot worker) generate hundreds of capacity rejections that the platform was supposedly designed to absorb. Observed on a live instance: 214 Agent at capacity failures in 24 hours, 0 backlog enqueues from the same caller across 541 dispatches.
⚠️ Validation needed during implementation — this may be intentional design (sync mode = "I'm waiting on the wire, queueing helps no one") rather than a bug. Filing for explicit review of the trade-off.
Component
Backend / Chat Router / Backlog Service (BACKLOG-001)
Priority
P2
Error
Per execution failed for capacity (sample, sanitized):
[Slots] Agent '<agent>' at capacity (3/3), rejecting execution <exec-id>
[Chat] Agent '<agent>' at capacity, could not acquire slot for chat <exec-id>
DB-side surface: schedule_executions.error = "Agent at capacity (3/3 parallel tasks running)" (note: no ... and backlog is full suffix — the backlog code path was never entered).
Location
- File:
routers/chat.py:893–956 - Function: parallel-task POST handler
- Branch:
if request.async_mode:(line ~895) — only this branch enters the backlog spill block
Root Cause
The capacity-rejection / backlog-spill code (lines 909-956) is nested inside if request.async_mode:. For synchronous calls (async_mode=false or omitted), the slot-acquisition failure path is different and returns the capacity error directly without offering the queue.
This is internally consistent — sync mode means the caller is HTTP-waiting on a response, so queuing is awkward — but it leaves a hole in the platform's capacity story. Observed caller pattern: an MCP chat_with_agent call with parallel=true and async=false (the apparent default from how the tool is being used in practice) keeps the HTTP connection open for the full task duration, hits the slot wall, and bounces. The caller has no built-in retry/backoff and treats the rejection as terminal failure.
The MCP tool description for chat_with_agent advertises BACKLOG-001 in spirit ("agent will eventually run your task") but the sync default doesn't actually use it.
Either the platform should:
- Spill sync calls to the backlog too and convert them to long-poll (i.e., the HTTP connection waits on backlog drain)
- Default
async_mode=trueforchat_with_agentparallel calls - Document explicitly in the MCP tool surface that sync parallel calls are not queued and callers must handle 429 themselves
Today's behavior is "silently expects the caller to know."
Reproduction Steps
- Agent A configured with
max_parallel_tasks=3,max_backlog_depth=50 - From a different agent or MCP client, dispatch 6 parallel sync calls to A in rapid succession (
chat_with_agent agent_name=A parallel=truewith noasync=true) - First 3 acquire slots, last 3 immediately fail with
Agent at capacity (3/3 parallel tasks running) - Verify
SELECT queued_at FROM schedule_executions WHERE agent_name='A' ...— no rows havequeued_at IS NOT NULL - Verify the 3 failures show no
... and backlog is fullsuffix, confirming the backlog branch was not entered
Suggested Fix
Either:
Option A — Default to async + poll for parallel calls (least invasive):
Change the MCP chat_with_agent tool so that parallel=true implies async=true unless the caller explicitly sets async=false. The tool already returns an execution_id in async mode, and list_recent_executions / get_execution_result exist for polling.
Option B — Make sync calls long-poll on the backlog:
In routers/chat.py:893+, when a sync request fails to acquire a slot but the backlog has room, enqueue it and await drain completion (with the request's timeout_seconds as the wait bound). Returns the result on the same HTTP connection. Higher engineering cost, more transparent to callers.
Option C — Document and surface clearly:
Update the MCP tool description to explicitly state sync mode does not queue, and add a structured retry hint to the 429/error response (e.g., Retry-After header based on slot ETA from current TTLs).
If sync-bypasses-backlog is the desired behavior, Option C alone closes the gap.
Environment
- Trinity version:
c3f3f1c - Observed caller: agent dispatching parallel
chat_with_agentcalls via MCP, fan-out factor 7, no explicitasyncflag - Observed on a live instance over 24 hours: 541 dispatches, 0 backlog enqueues, 214 capacity rejections (~40% terminal failure rate from one cause)
Related
- feat: Persistent task backlog for async execution (BACKLOG-001) #260 (BACKLOG-001 feature) — the queue this caller path bypasses
- refactor: consolidate ExecutionQueue + SlotService + BacklogService into CapacityManager #428 (refactor: consolidate ExecutionQueue + SlotService + BacklogService) — natural place to address sync-vs-async parity
- Related but distinct: the backlog drain itself is currently broken (filed separately) — fixing this issue without fixing that one would expose latent breakage to more callers
Activity
fix(chat): sync /task long-polls on backlog at capacity (#498) (#515)
Release: v0.5.0 (#608)
docs(planning): Abilityai#408 dissolution gated on Abilityai#428, not A…
refactor(chat): extract post-task side effects from async task wrapper (
fix(workspace): scope a client's agent page to their own runs (#3139) (…