Skip to content

spike: Agent Task Backlog — async task queue for inter-agent pipelines #157

Closed

Description

@vybe

Overview

Today, agent-to-agent communication in Trinity is synchronous — the caller blocks until the target agent responds. The chat_with_agent MCP tool has an async: true flag that returns an execution_id, but there is no way to retrieve the result later. This means agents cannot submit work to each other and continue independently.

This spike proposes an Agent Task Backlog — a persistent, ordered queue of tasks per agent that any actor (human, agent, schedule) can submit to, with results retrievable by execution ID.


Problem

  1. Blocking calls limit throughput — Agent A calls Agent B and waits 2+ minutes for a response. A can't do anything else.
  2. No shared work queue — Humans can trigger tasks from the UI, but there's no concept of a persistent backlog that agents also submit to.
  3. No result retrieval — The async: true mode returns an execution_id, but no MCP tool exists to fetch the result. The async path is a dead end.
  4. No pipeline pattern — Without submit + retrieve, agents can't form asynchronous pipelines (A submits to B, B submits to C, A checks results later).

Current State

Component What Exists Gap
Execution Queue (services/execution_queue.py) Redis-backed, max 3 queued, 600s TTL Ephemeral, not a deliberate backlog
chat_with_agent MCP tool async: true returns execution_id No get_execution_result tool
/api/agents/{name}/task Stateless parallel execution No queue semantics, no persistence
schedule_executions table Stores execution results with IDs, status, response, cost Only for schedule-triggered executions
Process Engine Multi-step YAML workflows Heavyweight for ad-hoc task submission
Tasks UI panel Manual task triggering No backlog view, no queue status

Proposed Design

New Concept: Agent Task Backlog

A persistent, per-agent queue where tasks are submitted and processed FIFO (with optional priority). Every actor uses the same queue.

Backend Changes

  1. POST /api/agents/{name}/backlog — Submit a task, returns execution_id immediately

    • Accepts: message, priority (optional), metadata (optional)
    • Persists to database (not just Redis)
    • Source tracking: who submitted (human user, agent, schedule)
  2. Backlog Worker — Background loop per agent

    • Picks next pending task from backlog (FIFO or priority-ordered)
    • Executes via existing task_execution_service.execute_task()
    • One task at a time per agent
    • Marks completion with result, cost, duration
  3. GET /api/agents/{name}/backlog — List pending + recent completed tasks

    • Filterable by status: pending, running, completed, failed
    • Pagination support
  4. GET /api/agents/{name}/executions/{id} — Generalize the existing schedule execution detail endpoint to serve any execution source

MCP Tools (Key Enabler)

  1. submit_task — Add task to an agent's backlog, returns execution_id. Non-blocking. Respects agent permissions.
  2. get_execution_result — Fetch result by execution_id. Returns status + response if completed.
  3. list_backlog (optional) — View queued/completed tasks for an agent.

Frontend Changes

  • Tasks panel becomes a backlog view showing queued → running → completed tasks
  • Humans can add tasks that land in the same queue agents use
  • Status indicators and result inspection on click

Async Pipeline Pattern This Enables

Agent A                          Agent B                          Agent C
   |                                |                                |
   |-- submit_task(B, "analyze X") →|                                |
   |← execution_id: abc123         |                                |
   |                                |-- (processes when ready) --    |
   |   ... does other work ...      |                                |
   |                                |-- submit_task(C, "summarize") →|
   |                                |← execution_id: def456         |
   |-- get_execution_result(abc123) |                                |
   |← status: completed, result:...|                                |

Design Decisions to Resolve

Decision Options Leaning
Persistence Redis (ephemeral) vs SQLite (durable) SQLite — backlog should survive restarts
Queue depth Fixed cap vs configurable per-agent Configurable, default 50
Priority FIFO only vs priority levels Start FIFO, add priority later
Result retrieval Polling only vs webhook/notification Polling via MCP first, webhooks later
Task context Stateless (like /task) vs conversational (like /chat) Stateless — work queue items are independent
Relationship to Process Engine Replace vs complement Complement — process steps can submit to backlogs
Reuse schedule_executions Extend existing table vs new table Extend with new triggered_by="backlog" source

Acceptance Criteria

MVP (Phase 1)

  • POST /api/agents/{name}/backlog endpoint accepts tasks, returns execution_id
  • Background worker processes backlog tasks FIFO, one at a time
  • GET /api/agents/{name}/executions/{id} returns status + result for any execution
  • MCP tool submit_task submits to backlog with permission checks
  • MCP tool get_execution_result retrieves result by ID
  • Tasks UI shows backlog (pending/running/completed)
  • Humans can add tasks to backlog from UI

Phase 2 (Follow-up)

  • Priority levels for backlog tasks
  • Configurable queue depth per agent
  • WebSocket notifications when task completes
  • list_backlog MCP tool
  • Batch task submission
  • Result expiry / cleanup policy

Related

  • Execution Queue: src/backend/services/execution_queue.py
  • Task Execution Service: src/backend/services/task_execution_service.py
  • MCP Chat Tool: src/mcp-server/src/tools/chat.ts
  • Execution Origin Tracking: docs/feature-flows/AUDIT-001-execution-origin-tracking.md
  • Process Engine: src/backend/services/process_engine/

Activity

  1. added
    priority-p1
    type-feature
    on Mar 23, 2026
  2. m13v commented on Mar 26, 2026

    @m13v

    the blocking call problem is brutal in practice. we run a tmux-based agent orchestration system where a parent agent spawns 3-5 child agents in separate sessions, and the original version just waited for each one sequentially - turning a 2 minute parallel task into 10 minutes. the pattern that worked for us: parent agent fires off all sub-agents, then polls their output files on a short interval instead of blocking. results get written to a known path per execution ID. the key insight was separating "submit" from "retrieve" at the IPC level, not just adding async flags. your Redis queue approach is cleaner than our file-based version, but watch out for the result TTL - we had agents that submitted work, got compacted (context window full), resumed, and then couldn't find results because the 600s TTL had expired. bumped ours to 1 hour and the failure rate dropped to near zero.

  3. m13v commented on Mar 26, 2026

    @m13v

    our tmux-based agent orchestration that handles the async submit/poll pattern for parallel agent sessions: https://github.com/m13v/tmux-background-agents/blob/main/SKILL.md

    and the browser lock script that prevents resource contention when multiple agents share a browser instance: https://github.com/m13v/browser-lock/blob/main/playwright-lock.sh

  4. vybe commented on Mar 30, 2026

    @vybe
    ContributorAuthor

    Actualization (2026-03-30)

    Current state: Partially addressed by MCP-007 (MCP Execution Query Tools, req 7.3):

    What exists:

    • list_recent_executions — query execution history
    • get_execution_result — poll async results by execution_id
    • get_agent_activity_summary — monitor agent activity
    • chat_with_agent(async=true) returns execution_id immediately
    • Agents can poll get_execution_result to retrieve completed results

    What this issue proposes beyond MCP-007:

    • Persistent per-agent task backlog (ordered queue)
    • Batch submission
    • Priority ordering
    • Task dependencies
    • Backlog visibility in UI

    Recommendation: Update issue description to note that the basic async polling pattern is implemented via MCP-007. The remaining value is the persistent queue/backlog concept with ordering and dependencies. Keep open but clarify scope.

  5. m13v commented on Mar 30, 2026

    @m13v

    nice actualization. the persistent task store with retry semantics is the big missing piece - without it you lose in-flight work on any restart. we solved this with a simple SQLite-backed queue where each task has a state machine (pending -> claimed -> running -> completed/failed). the key insight was separating the "submit" from the "claim" so multiple agents can compete for work without duplicating effort. for the callback/webhook pattern, we found polling with exponential backoff was more reliable in practice since webhook delivery is never guaranteed. curious if you're planning to build the persistent store on top of the existing MCP execution tracking or as a separate subsystem?

  6. vybe commented on Apr 4, 2026

    @vybe
    ContributorAuthor

    Closing — superseded by #260 (feat: Persistent task backlog for async execution). The spike informed the full feature issue.

  7. added
    complexity-medium
    on Jun 3, 2026
  8. added a commit that references this issue on Jul 30, 2026
  9. 1 remaining item

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-mediumpriority-p1type-feature

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions