Skip to content

bug: synchronous parallel chat_with_agent bypasses backlog (BACKLOG-001) — fan-out callers see terminal capacity rejections instead of queueing #498

Closed

Description

@vybe

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:

  1. Spill sync calls to the backlog too and convert them to long-poll (i.e., the HTTP connection waits on backlog drain)
  2. Default async_mode=true for chat_with_agent parallel calls
  3. 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

  1. Agent A configured with max_parallel_tasks=3, max_backlog_depth=50
  2. From a different agent or MCP client, dispatch 6 parallel sync calls to A in rapid succession (chat_with_agent agent_name=A parallel=true with no async=true)
  3. First 3 acquire slots, last 3 immediately fail with Agent at capacity (3/3 parallel tasks running)
  4. Verify SELECT queued_at FROM schedule_executions WHERE agent_name='A' ... — no rows have queued_at IS NOT NULL
  5. Verify the 3 failures show no ... and backlog is full suffix, 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_agent calls via MCP, fan-out factor 7, no explicit async flag
  • Observed on a live instance over 24 hours: 541 dispatches, 0 backlog enqueues, 214 capacity rejections (~40% terminal failure rate from one cause)

Related

Activity

  1. added
    priority-p2
    type-bug
    status-in-progress
    and removed on Apr 25, 2026
  2. added
    status-in-dev
    and removed
    status-in-progress
    on Apr 26, 2026
  3. added a commit that references this issue on Apr 30, 2026
  4. added a commit that references this issue on May 19, 2026
  5. added
    complexity-low
    theme-reliability
    on Jun 3, 2026
  6. removed
    status-in-dev
    on Aug 7, 2026
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

    complexity-lowpriority-p2theme-reliabilitytype-bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions