Skip to content

Long threads: accepting a delegated completion and starting a turn parse the whole thread, and the event loop stalls for seconds #45047

Backlog

Description

@Dropje97

What happened

On a long dispatcher thread (13,369 runs), each step still parses the whole thread on the server. Accepting a delegated child's completion spends about 9 s in reads before the parent wakes, and every turn start spends about 3.3 s. The server's event loop also stalls for 2–16 s about ten times an hour, with no output, MCP replies or provider events for any thread during a stall.

Diagnosis

Both paths read the thread's full record sets on main 5385 (v0.1.14-nightly.20261128.5385):

  • dispatchNotificationAccepted (Orchestrator.ts) calls getProjectionWithPendingEvents, the command projection: every user message, every node that is not a settled leaf and the user and error turn items of the thread. acceptDelegatedCompletionDelivery uses one message, its parent run and the tasks that run can claim or deliver.
  • getTurnStartContext (ProjectionStore.ts) reads every run, every run attempt and every provider turn of the thread (WHERE thread_id = ? ORDER BY …).

The rows are read on a reader worker, but the event loop decodes them. The largest thread's payloads total 15.8 MB of runs, 9.4 MB of run attempts, 17.5 MB of provider turns, 56.5 MB of messages and 89.7 MB of nodes. In 109 of the 201 stalls the traces cover, a server.heap.spike (128 MB or more) landed in the same 30-second sample. Its suspects include orchestrationV2.providerTurnStart.start and ProjectionStore.readCanonicalProjection. This connection is circumstantial: no CPU profile was taken.

For acceptance, a read by key gives the same result: the message by ID (user role), the parent run by ID, then the subagents of that run plus the delivery's task IDs, with the same omitSettledSubagents filter. One case keeps the full read: a parent run awaiting its turn-end capture, which holdRunAwaitingCapture may report as waiting. We run that change locally. Its 15 cases (pending siblings, a delivery naming an older run's task, generation and message mismatches, live and archived parents, a pending capture) match the full read's events.

Steps to reproduce

  1. Use a thread with about 10,000 runs whose agent delegates tasks.
  2. Let a child finish, so the parent accepts the completion notice.
  3. Time orchestrationV2.notificationAccepted and orchestrationV2.providerTurnStart.start, or watch the event loop stalled for N ms warnings.

Version

Production runs a local build of 0.1.12-nightly.20261123.5165. It reads several other commands by key. These two paths use upstream's code, unchanged on 5385.

Environment

Linux (Proxmox LXC, 16 CPUs, ZFS), t3 serve as a systemd service, statev2.sqlite 49 GB. Two dispatcher threads have 11,000–13,000 runs each.

Evidence

Statement log, 24 h, statements over 250 ms:

Path Statement Calls Mean Rows
notification accept messages, user role 149 3.77 s 14,643
notification accept turn items 149 2.45 s 6,141
notification accept nodes 149 2.44 s 12,752
turn start runs 509 1.12 s 12,249
turn start run attempts 584 1.10 s 11,005
turn start provider turns 598 1.11 s 10,500

event-loop-stalls.ndjson, same 24 h: 221 stalls, median 3.2 s, 90th percentile 8.1 s, longest 15.9 s.

Related issues

#35597 (turn start) and #36127 (send and queued start) were closed as partly fixed by #39103, #36289 and #37915; turn start still reads the three record sets above. #42557 reports a server wedged by background sync on a large history, a different path.

Fix applied or workaround

A local patch reads notification acceptance by key. Turn start is unchanged.

Filed by

Measured and written by Claude (Opus 5.5) in T3 Code; posted with the owner's approval.

Activity

  1. added
    via-triage
    on Nov 28, 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

    via-triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions