Skip to content

The settled-subagent check reads one run row per subagent instead of using runs_run_status_idx #37897

Closed

Description

@Dropje97

What happened

The server gets slow on a large install with many agents. While an agent archived 3,618 finished delegated threads, the server spent 9,200 s of SQLite read time within one hour. About 2,400 of those reads took 2.1 s each (median).

Diagnosis

settledSubagent in apps/server/src/orchestration-v2/ProjectionStore.ts is used by unsettledSubagentRowids and by the omitSettledSubagents filter of readCanonicalProjection. It checks each finished subagent with:

NOT EXISTS (
  SELECT 1 FROM orchestration_v2_projection_runs AS run
  WHERE run.run_id = <row>.run_id
    AND run.status IN ('preparing', 'queued', 'starting', 'running', 'waiting'))

SQLite answers this from the unique run_id autoindex, not from the covering orchestration_v2_projection_runs_run_status_idx (run_id, status) that migration 089 added. So it reads the run's table row for every subagent. Two other reads in the same file already force that index with INDEXED BY.

Suggested fix: add INDEXED BY orchestration_v2_projection_runs_run_status_idx to the run lookup in settledSubagent.

Steps to reproduce

  1. Use a thread that has delegated thousands of tasks; ours has 5,523 subagents across 3,768 runs.
  2. Archive its child threads, or send to it. Each readCanonicalProjection with omitSettledSubagentRecords or omitSettledSubagents then takes 0.7–2 s.
  3. EXPLAIN QUERY PLAN of the providerThreads read shows SEARCH run USING INDEX sqlite_autoindex_orchestration_v2_projection_runs_1 (run_id=?).

Version

0.1.10-nightly.20261116.4787 with local patches. Measured on a copy with our local indexes dropped; the code is unchanged on main (f39ecd3).

Environment

Linux (LXC), database on ZFS with compression, Node 26.13.1, 49 GB statev2.sqlite

Evidence

providerThreads read with omitSettledSubagentRecords: true, on a copy of the database, sqlite3 CLI, two runs each:

                                          page cache misses   time
as is                                     4,436 / 4,376       2.09 s / 0.67 s
INDEXED BY ..._runs_run_status_idx        467 / 0             0.055 s / 0.013 s

Both return the same 9 rows. Plan with the hint:
  SEARCH run USING COVERING INDEX orchestration_v2_projection_runs_run_status_idx (run_id=? AND status=?)

A page miss is expensive on our storage, so the miss count is the portable number.

Production slow-read log over 9.5 h, statements that contain this check:
  provider_threads read  dispatch.threadMutation  n=2398  total 4960 s  median 2078 ms
  subagents read         dispatchMessage          n=337   total  248 s  median  746 ms
  provider_threads read  dispatchMessage          n=340   total  239 s  median  704 ms
  subagents read         startNextQueuedRun       n=100   total   76 s  median  756 ms

Related issues

None found. #36127 and #35597 cover whole-thread reads at dispatch and turn start; this is a separate index choice.

Fix applied or workaround

None applied. We will carry the INDEXED BY hint as a local patch until a nightly contains a fix.

Filed by

Claude (Opus 5.5) in T3 Code; posted with the owner's approval

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions