Skip to content

[Bug]: Each stored event costs ~66 ms of server CPU; SQLite cache is the 2 MB default on a 15.5 GB database, and t3_thread_read loads every run of a thread #37877

Closed

Description

@cartwmic

Before submitting

  • I searched existing issues and did not find an open duplicate. Closed issues that touch the same ground are under Related.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

Run t3 serve with a large database and many agent sessions. Our load is about 46 agent processes under the server, 13 to 18 stored events per second in the windows I measured (9.5 per second averaged over the process lifetime), and one local client that makes most of the t3_thread_read calls (928 of 933 in one window). Nothing else is special.

  1. CPU per stored event. Read process CPU and the event sequence twice, 30 s apart, and divide:
    pid=<t3 serve pid>
    db="file:$HOME/.t3/userdata/statev2.sqlite?mode=ro"
    a=$(awk '{print $14+$15}' /proc/$pid/stat); s1=$(sqlite3 "$db" "SELECT MAX(sequence) FROM orchestration_events")
    sleep 30
    b=$(awk '{print $14+$15}' /proc/$pid/stat); s2=$(sqlite3 "$db" "SELECT MAX(sequence) FROM orchestration_events")
    awk -v t=$((b-a)) -v e=$((s2-s1)) 'BEGIN{printf "%.1f ms CPU per stored event\n", t*10/e}'
    ps -o etimes=,cputimes= -p $pid    # lifetime CPU seconds, for the lifetime average
  2. SQLite cache. The setup code sets no cache_size and no mmap_size:
    grep -n "cache_size\|mmap_size" apps/server/src/persistence/Sqlite.ts packages/shared/src/nodeSqliteClient.ts   # no output at 9a35ab0
    sqlite3 "$db" "PRAGMA cache_size;"    # -2000, the SQLite default (a new connection, so this shows the default, not a read of the server's connections)
  3. Runs per thread:
    sqlite3 "$db" "SELECT thread_id, COUNT(*) c FROM orchestration_v2_projection_runs GROUP BY thread_id ORDER BY c DESC LIMIT 5"

Expected behavior

A stored event costs a few milliseconds of CPU, not tens. A thread read does not load every run of the thread when it returns a few items.

Actual behavior

t3 serve 0.1.10-nightly.20261113.4643 (commit 9a35ab0), up about 25 hours, on a 15.5 GB statev2.sqlite. The host is an 8-core Linux machine under heavy load (load average 15 to 47, CPU pressure 35 %, 12 GB of swap in use).

1. The CPU follows our event volume. The cost of each event is the issue, not a spin or a leak.

In quiet seconds the server uses about 3 % of one core. Per 1 s sample it ranges from 3 % (minimum) through 84 % (median) and 174 % (90th percentile) to 329 % (maximum). Process CPU divided by stored events:

Window CPU Stored events CPU per event
209 s sample 191.9 CPU-s 2,903 66.1 ms
process lifetime 56,451 CPU-s 847,778 66.6 ms

The CPU-per-event figure is the same over 4 minutes and over 25 hours. It is total process CPU, so it includes everything the server does for each event and for the clients' reads, not a single code path. Lifetime CPU is 63 % of one core (15 h 40 min in 89,150 s). Over the lifetime, by thread: main thread 47 %, SQLite worker threads 31 %, V8 helper threads (mostly garbage collection) 20 %. In the 209 s sample, 24 % of the process CPU was system time.

Database reads per event, from a fit over ten-second buckets in one 150 s window (R² 0.58, so a fit, not a measurement): about 1,300 pages (5.3 MB) per stored event, and about 1,355 pages (5.5 MB) per t3_thread_read by the local client. I did not check this read figure over the process lifetime, so I cannot say it is flat over 25 hours. Only the CPU figure is. The lifetime read totals are in section 2.

2. The SQLite cache is the 2 MB default on a 15.5 GB database.

cache_size is -2000 and no mmap_size is set (apps/server/src/persistence/Sqlite.ts lines 72 to 89 set the other PRAGMAs; cache_size and mmap_size appear in neither that file nor packages/shared/src/nodeSqliteClient.ts). The reader worker threads make 20,000 to 57,000 reads of about 4 KB a second, 82 to 233 MB/s. Over the process lifetime: 4.04 billion read calls and 11.4 TB read through system calls, 2.7 TB of it from disk. So most reads are served by the operating system cache, not by SQLite's own.

I have not tested whether a larger cache or mmap_size lowers the read calls or the CPU. #24895 argues a larger page cache would not help, because overflow content is read directly from the file and in WAL mode a reader's cache is reset when another connection commits. I cannot tell which of our reads are overflow pages.

3. t3_thread_read loads every run of the thread, with no limit.

loadReadableThread (apps/server/src/mcp/OrchestratorMcpService.ts lines 1376 to 1406) calls getProjectThreadRecords for runs, providerTurns and runtimeRequests with no filter. That reaches readCanonicalProjection (apps/server/src/orchestration-v2/ProjectionStore.ts line 3542, through getThreadRecords at lines 6278 to 6283, which passes no window), whose runs select has no limit and no status filter when no filter is passed (lines 3789 to 3796):

SELECT payload_json FROM orchestration_v2_projection_runs WHERE thread_id = ? ORDER BY ordinal ASC

The trace shows this select 2,303 times in 351 s, from all callers. This select reads rows in proportion to a thread's history. I did not time a thread read on a 5,000-run thread against a 10-run one, so I cannot say how much of a read's time it is. Our long-lived threads are long: 6 have over 1,000 runs, 43 have over 100, and the largest has 5,263 runs and 5.8 MB of run rows.

One client is the main caller. In 229 s of trace, t3_thread_read ran 244 times a minute. 928 of the 933 thread reads came from one user agent, a plain node client on the same host. It is the only plain node client on the server's port, so I take it to be our own tool; the trace does not record the caller, so that is an inference. One MCP session made 870 reads in 229 s, in bursts (median gap 0.03 s, up to 173 in 10 s). A fit of the 150 s window gives that client about 17 % of the process CPU (R² 0.36); the 209 s window gave about 0 (R² 0.29). So most of the CPU is not that client's reads.

Minor: 26 statements per read. A t3_thread_read runs a median 26 SQL statements (95th percentile 38, maximum 49) and takes a median 78 ms. It reads the same thread row 4 times and the thread's runs twice. The count is fixed and does not grow with thread size. In a quiet moment one read took 19 ms.

What I could not establish

  • No CPU profile. perf, strace, bpftrace and pidstat are not installed here, gdb attach is blocked and would pause the server, and the Node inspector would change the process. I cannot say where the main thread's 40 % of a core (209 s sample) goes. The split by thread above comes from top -H: it counts CPU per thread, not per function. The client's 17 % share is a fit, not a measurement.
  • The 11 MB/s of writes. The server wrote 1.05 TB to disk in 25 hours, about 1.2 MB per stored event. Traces are 0.3 to 0.5 MB/s of that. I did not trace the rest.
  • The overloaded host. Load stretches the wall times I quote. It should not change CPU seconds much, but I did not check on a quiet host. I could not stop our load, so I do not know the idle cost with no agents.
  • Which statements read the most pages. The trace records times, not bytes.
  • Whether either fix helps. I did not change cache_size or mmap_size, and I did not time reads against thread size.

Impact

Minor bug or occasional failure. The server averages about 0.63 of a core over its lifetime and reached 329 % of a core in its busiest second. Nothing here fails on its own. I put it under Minor because I cannot show a failure caused by it.

Version or commit

0.1.10-nightly.20261113.4643, commit 9a35ab0. Source lines above are from that commit.

Environment

Linux 7.2 (Arch) on arm64, 8 cores, 32 GB RAM. t3 serve runs as a systemd user service with the Claude provider, with about 46 agent processes under it. The database is ~/.t3/userdata/statev2.sqlite, 15.5 GB.

Logs or stack traces

None beyond the counts above. I can share the trace analysis if it helps.

Workaround

None found. I did not change any setting or restart the server.

Related

Activity

  1. nitedani commented on Nov 17, 2026

    @nitedani

    ClaudeCodex Claude Opus 5.5 (high effort) wrote this; GPT-6.1-Sol (Codex, high effort) reviewed it.

    We see the same unbounded reads on a second server: headless t3 serve on a 32 vCPU Linux VM, nightly 0.1.10-nightly.20261116.4761 plus #31488, with an 11 GiB statev2.sqlite. We have nothing to add on points 1 and 2, because we didn't count stored events or change cache_size.

    The five longest ProjectionStore.readCanonicalProjection spans in our trace (23.5 s to 71.1 s) have no t3_thread_read above them. Four run under Orchestrator.startNextQueuedRun and one under the task_cancel tool's subagent.stop. startNextQueuedRun reads the thread through readCommandProjection, which on main still selects every run and attempt of the thread, while t3_thread_read selects every run at OrchestratorMcpService.ts:1485.

    The runs select took under 1 s in all five, so it isn't the expensive part. In the 71.1 s one, attempts took 15.3 s and nodes 21.4 s. These are wall times on a busy reader pool, not CPU.

    You couldn't say how much of a read's time thread size costs, so I timed it on a 12.5 GB backup copy of our database with Python's sqlite3 rather than the server. Your runs select on a 4,991-run thread (5.75 MB) took 8,447 ms on the first pass and 22 to 60 ms on repeats. With LIMIT 20, run right after those, it took under 1 ms. On a 10-run thread it took 4.0 ms on the first pass and under 0.2 ms on repeats.

    I found no open PR that bounds these reads, and #36141 bounds history pages, a different path. I'd fix the shared command read, keeping only the records its callers use, rather than t3_thread_read alone, because on our server the command path is where the long reads come from.

  2. jonathanli12 commented on Nov 17, 2026

    @jonathanli12

    Note

    🤖 Claude Opus 5.5 responding on behalf of jonathanli12

    Seeing the same thing, and the cost per event grows with database size, not with event rate. Measured with the steps above (60 s windows) on four t3 serve processes on one Linux host, at the same moment:

    Server statev2.sqlite Events stored Longest thread (runs) Events/s ms CPU per stored event CPU
    A (v0.1.10) 35.8 GB 7.0M 2,232 1.1 555 63% of a core
    B (nightly 20261115) 3.6 GB n/a 494 14.0 37 52%
    C (nightly 20261115) 2.4 GB n/a 319 1.4 116 16%
    D (nightly 20261115) 2.1 GB n/a 431 1.9 83 16%
    • A and C store events at about the same rate, but A spends almost 5× as much CPU per event.
    • A uses most of a core at about 1 event per second, while B handles 14 per second on less.
    • Three 30 s windows on A earlier gave 329 to 481 ms per event. Since its last restart, A has averaged 79% of a core (5,047 CPU seconds over 6,378 s).

    To keep each database small, we now run nine extra servers on the same host. That fits your cache and run-loading diagnosis better than a concurrency limit, though we haven't profiled to confirm it.

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