Skip to content
Source
Sign inSign up
pingdotgg/t3code

[Bug]: Intermediate turn-item snapshots remain outside event compaction (78.5 MB on one install) #19780

Closedjosephv123(GitHub profile, opens in a new tab) opened this issue 9 comments

Before submitting

Area

apps/server

Steps to reproduce

  1. Use V2 with tool-heavy agent threads over time.
  2. On a copy of statev2.sqlite, run:
WITH ranked AS (
  SELECT length(CAST(payload_json AS BLOB)) AS bytes,
         row_number() OVER (
           PARTITION BY stream_id, json_extract(payload_json, '$.id')
           ORDER BY sequence
         ) AS first_rank,
         row_number() OVER (
           PARTITION BY stream_id, json_extract(payload_json, '$.id')
           ORDER BY sequence DESC
         ) AS latest_rank
  FROM orchestration_events
  WHERE application_event_version = 2 AND event_type = 'turn-item.updated'
)
SELECT count(*) AS intermediate_snapshots, sum(bytes) AS payload_bytes
FROM ranked WHERE first_rank > 1 AND latest_rank > 1;

Actual behavior and evidence

An October 14/15 Linux database copy, approximately 1.46 GB overall, has:

All turn-item.updated snapshots:                52,295 / 434,343,273 payload bytes
Intermediate snapshots excluding first+latest:  10,366 /  78,496,430 payload bytes

The aggregate was rechecked while consolidating the audits. Payload bytes are UTF-8 JSON bytes, excluding row/index overhead. They are not a promise of immediately reclaimable filesystem space.

compactEventStore explicitly leaves turn-item.updated intact. Its deletion cases cover complete thread states, node.updated, message.updated, and eligible legacy events. Thus merely adding its missing production caller (#18559) does not address this category. The proposed #19435 also excludes turn items.

Expected behavior / repair constraints

Redundant intermediate full-state tool snapshots should not accumulate indefinitely. A repair must preserve original insertion position, latest state, prompt references, and replay/reconnect behavior. The source explicitly cites first-write ordering as a reason for retaining these records; deleting everything except the latest is unsafe. The query excludes both endpoints to establish the remaining storage cost, not to assert that blind deletion of its result is safe.

Closed PR #19451 attempted first/latest retention and a backfill. Its maintainer closure asked for the storage-growth evidence in an issue and agreement on a smaller repair. This issue records independent evidence; it does not request approval of that PR's implementation or backfill.

Impact / version / environment

Storage growth on an Ubuntu Linux server using Claude. Nightly 0.0.46-nightly.20261014.3132; retention logic unchanged at main e536bfd33bc3c351997141d93790df75eeded7b9. No CPU slowdown or heap crash was demonstrated from these particular rows. No live database was compacted.

This count excludes the first/latest snapshots and projection copies. Normal event/projection duplication and required served-image storage are not counted as a separate defect here; #16647's already-fixed unserved-image case is also different.

  1. Another data point that is larger and has a different shape: macOS desktop, 0.0.46-nightly.20261015.3145, statev2.sqlite created 2026-10-05, now 22.7 GB after 10 days (freelist_count = 0). Measured read-only with dbstat and a GROUP BY event_type over orchestration_events:

    event_typeeventspayload
    turn-item.updated302,5789.15 GB
    subagent.updated63,9668.22 GB
    node.updated524,4830.63 GB

    One thread accounts for 15.7 GB. It ran a Workflow whose script (~207 KB) is passed inline as the subagent prompt. Averaged over the latest 200 events of each type in that thread, prompt is 206,885 bytes in both subagent.updated and turn-item.updated. The next largest fields are diffStr (~42 KB) and workflow (~13 KB). 213 distinct subagents produced 40,304 subagent.updated events, so the same prompt is rewritten roughly 190 times per subagent.

    So beyond the intermediate snapshots described here, the size driver on this install is an unchanged large field being copied into every snapshot. Compacting intermediate snapshots would help. Storing large immutable fields such as prompt once per entity, or emitting only changed fields, would address the growth rate itself. #19435 states that turn items are never compacted; I have not verified whether subagent.updated falls under its node rules.

  2. Same query from the report, run read-only on a much larger install (a 19.8 GB statev2.sqlite), on 2026-10-16:

    All turn-item.updated snapshots:                892,418 / 3,430,716,566 payload bytes
    Intermediate snapshots excluding first+latest:  137,091 /   600,533,278 payload bytes

    So on this install the intermediate snapshots are about 0.6 GB, or 17% of the bytes in turn-item.updated events, which is the largest event type (the whole table holds about 4.95 GB of event payload).

    By item type, the intermediate part is:

    item typesnapshotspayload MB
    reasoning33,633238
    command_execution36,771170
    assistant_message30,00179
    subagent21,04478
    dynamic_tool7,02419

    Counting every non-latest snapshot (the first one included) gives 425,630 of 891,622 rows and about 1.04 GB, because the first snapshot of an item is also kept for replay ordering.

    One thing that may help when sizing the repair: file size is a poor proxy here. The same file has 1,138,303 free pages (4.66 GB) and auto_vacuum = 0, and the data in use (15.2 GB) is 1.1 GB smaller than a VACUUM INTO copy of the same database taken three days earlier, because compaction and the legacy cleanup shrank the event table in between. Only t3 doctor returns those free pages to the filesystem.

    Payload bytes exclude row and index overhead, as in the report. Counts and sizes only; no thread content was read beyond $.id and $.type.

  3. Another data point, from a newer build (0.1.2-nightly.20261022.3512, macOS, Claude and Codex providers, a few hundred agent threads a day). It still reproduces after the #21154 fix.

    Measured read-only on a copy of statev2.sqlite on 22 Oct 2026:

    • statev2.sqlite: 11.56 GB, no free pages, auto_vacuum off.
    • orchestration_events: 6.49 GB table data, 1,309,069 rows. That's 7.68 GB with indexes, 66% of the file.
      • turn-item.updated alone: 2.76 GB.
      • Superseded snapshots of turn-item.updated, subagent.updated and provider-turn.updated (every version but the latest per item): about 1.9 GB.
    • orchestration_v2_projection_turn_items: 2.32 GB, 278,730 rows. orchestration_v2_projection_nodes: 0.60 GB, 202,831 rows.
    • Growth: about 0.6–0.75 GB a day.
    • The periodic compaction ran (every 6 h, last at 09:02 local), and VACUUM INTO on the copy only took it from 11.56 to 11.26 GB. So t3 doctor --fix can't recover the space: the bytes are in live rows, the superseded snapshots compaction keeps.

    Same ask as this issue: let compaction drop intermediate *.updated snapshots once a turn item is final, keeping the first and latest. A retention setting next to storageCleanup would also work.

  4. Another data point for this issue, from a larger install and with a measured reclamation result.

    Install

    T3 Code 0.1.1 desktop (macOS, Apple Silicon), 2026-10-22. statev2.sqlite was 26.99 GB (26,985,054,208 bytes) at the time of the measurement, with no free pages. 3,112 threads, of which 1,255 are archived.

    Where the bytes are (read-only, on a consistent copy)

    tablebytesshare of file
    orchestration_events15.42 GB57.1%
    orchestration_v2_projection_turn_items4.85 GB18.0%
    orchestration_v2_projection_nodes1.76 GB6.5%
    orchestration_v2_projection_messages0.34 GB1.2%
    all other tables1.64 GB6.1%
    all indexes (11)2.97 GB11.0%

    Inside the event log: 11.03 GB of payload over 3,948,291 rows, and one event type dominates it.

    event_typepayloadshare of event payload
    turn-item.updated7.48 GB67.8%
    node.updated1.63 GB14.8%
    provider-turn.updated0.51 GB4.7%
    provider-session.updated0.32 GB2.9%
    message.updated0.26 GB2.4%
    context-handoff.updated0.20 GB1.8%
    the other 37 event types0.62 GB5.6%

    The payload of turn-item.updated is mostly reasoning (2.87 GB) and command output (2.85 GB), because every change to a turn item stores the whole item again.

    The same query as this issue, on this install

    WITH ranked AS (
      SELECT length(CAST(payload_json AS BLOB)) AS bytes,
             row_number() OVER (PARTITION BY stream_id, json_extract(payload_json, '$.id') ORDER BY sequence) AS first_rank,
             row_number() OVER (PARTITION BY stream_id, json_extract(payload_json, '$.id') ORDER BY sequence DESC) AS latest_rank
      FROM orchestration_events
      WHERE application_event_version = 2 AND event_type = 'turn-item.updated'
    )
    SELECT count(*), sum(bytes) FROM ranked WHERE first_rank > 1 AND latest_rank > 1;
    All turn-item.updated snapshots:                1,567,471 / 7,131,369,660 payload bytes
    Intermediate snapshots excluding first+latest:    322,165 / 2,890,508,022 payload bytes
    Kept if pruned to first+latest:                1,245,306 / 4,240,861,638 payload bytes

    So the intermediate snapshots are 2.89 GB, 40.5% of the turn-item.updated payload on this install. That share is higher than the 18% and 17.5% in the two data points above, and it grows with the install: the more history a database keeps, the more of it is intermediate.

    No single thread dominates. Of the 3,125 threads that carry events, the largest holds 4.5% of the event payload, the top 12 hold 24.9%, the top 100 hold 63.0%, and the remaining 2,125 threads hold 8.6% (951 MB) between them. 48.2% of the payload is in threads that are already settled.

    Growth

    26.0 GB at 2026-10-21T23:01Z, 26.99 GB at 2026-10-22T14:22Z: about 0.9 GB a day on this install, with streaming in its reduced mode.

    A measured reclamation result

    On a copy of the database (nothing live was touched), I deleted the superseded turn-item.updated rows, keeping the first and the newest row per item, plus every row that a prompt sequence or a command receipt points at and the newest event of every stream, and then ran VACUUM:

    before  24.23 GiB
    after   20.33 GiB        (-16%)
    PRAGMA integrity_check  ok

    The projection was unchanged: for all 746,171 turn items, the projection row equals the newest surviving event, with 0 mismatches, before and after. A server opened the pruned copy and listed the same threads (3,063 threads, 16 archived, 13 deleted). t3 doctor --fix on the result reports nothing to repair.

    So the reclaimable space is real and measurable, and it comes back to the filesystem only if the file is vacuumed; auto_vacuum=INCREMENTAL afterwards is what keeps returning it online.

    Suggestion

    Compacting superseded turn-item.updated rows the way message.updated and node.updated already are - keeping the first and the newest row per item, preserving insertion position, prompt references and replay behaviour - would reclaim a large part of the event log on every long-lived install. The measurement above suggests the reclamation is worth having on its own, and not only for the event log: on this install the projections and the indexes add another 9.9 GB of the file.

  5. Another data point, on the newest build so far: 0.1.3-nightly.20261023 (server .3566 when measured, doctor run from .3579). macOS desktop app, Apple Silicon, Codex and Claude providers. The workload is heavy on delegate_task fan-out, with 6,172 threads and none archived.

    All of this was measured read-only on the live statev2.sqlite on 23 Oct 2026.

    • statev2.sqlite: 19.14 GiB. freelist_count = 0, auto_vacuum = NONE.
    • t3 doctor reports "19.14 GiB, 0 KiB reclaimable" and offers no repair. The 6-hourly compaction last ran a few hours earlier.

    Where the bytes are (dbstat):

    objectMB
    orchestration_events10,738
    its 4 largest indexes~2,055
    orchestration_v2_projection_turn_items3,760
    orchestration_v2_projection_nodes1,105

    orchestration_events by type (payload MB): turn-item.updated 1,081,208 rows / 4,977; node.updated 682,382 / 809; subagent.updated 81,580 / 355; provider-turn.updated 308,505 / 324; message.updated 80,551 / 156.

    The query from this issue:

    All turn-item.updated snapshots:                1,083,458 / 5,238,163,750 payload bytes
    Intermediate snapshots excluding first+latest:    330,962 / 1,492,184,255 payload bytes

    So 28% of the turn-item.updated bytes are intermediate snapshots, a higher share than the 17% on the 19.8 GB install above.

    Growth per day (orchestration_events payload):

    dayrowsMB
    Oct 15148,584367
    Oct 16130,264316
    Oct 17183,312420
    Oct 18195,456347
    Oct 19105,438210
    Oct 20145,724296
    Oct 212,8785
    Oct 2323,48054

    Usage on Oct 21 to 23 was lower, so I can't tell how much of the drop comes from the #21154 / #19435 fix.

    95% of the event rows are from October; the database was mostly built up in the last three weeks.

    Measured and drafted by Claude Code (claude-opus-5-5) via t3 triage, on behalf of @kosovojs.

  6. Another data point for this issue, from a long-running headless server: t3 v0.1.2-nightly.20261022.3541, macOS 27, t3 serve run by a launchd service, Claude and Codex providers, remote desktop and mobile clients, several thousand threads.

    Size and growth. ~/.t3/userdata/statev2.sqlite reached about 34.9 GB (32.5 GiB) in 17 days. That is about 1.9 GiB and about 286,000 events a day on average, and it is still growing at about 1.8 GiB a day.

    turn-item.updated is the largest event type. Newest 20,000 events (a 73-minute busy window), payload bytes only (sum(length(payload_json)), without row or index overhead):

    event_typerowspayload bytesshare
    turn-item.updated8,80723.9 MB64%
    node.updated5,3936.0 MB16%
    provider-turn.updated3,8144.4 MB12%
    thread.visited1041.3 MB4%
    message.updated5680.7 MB2%
    all others1,3141.0 MB3%

    An earlier 5,000-event sample gave the same order. Highest rowids, as a size hint: orchestration_v2_projection_turn_items 1,058,421; ..._nodes 844,414; ..._messages 128,802; ..._provider_turns 17,337; ..._runs 15,341; ..._threads about 4,000.

    Compaction and t3 doctor returned little. Automatic compaction ran ("Compacted orchestration history" is logged three times; the last run matches userdata/compacted-at), and the file did not shrink. t3 doctor --fix took the file from 32.41 GiB to 31.48 GiB (about 800 events, 0.93 GiB, 2.9%), with the server stopped for 51 minutes. auto_vacuum was 0 before the doctor ran (it set it to 2), so until then deleted rows could not return space to disk.

    Impact. Long reads and checkpoints slow down as the file grows (see #21840 and #23482), and the host's free disk fell to its alarm floor. The doctor's two APFS-clone backups also took about 32 GiB together once the live file diverged from them.

    Not measured: bytes per table or index. A dbstat scan of a live 32 GiB file would hold a long reader snapshot, so these readings use only max(rowid), PRAGMA lookups and rowid-bounded reads.

    Questions:

    1. Is compaction of turn-item.updated (and node.updated) snapshots planned, and in which release?
    2. Is there a supported retention setting, or an online way to shrink the database, for a server that runs for weeks? #21371 asked for one and was closed as a duplicate. Which issue tracks it?
    3. Could t3 doctor print a dry-run estimate (rows and bytes it would remove) and its progress? A 51-minute exclusive lock is hard to plan around without one.
    4. Is there a safe, read-only way to report per-table and per-index bytes on a live database?
  7. Note

    Posted by Claude Code (Opus 5.5) on behalf of @omar7550.

    Another data point, from a delegate-heavy install where the intermediate snapshots are only a small part of the growth.

    Install: T3 Code Nightly 0.1.3-nightly.20261024.3630, macOS desktop, Codex and Claude, heavy delegate_task fan-out. 7,366 threads (2,867 deleted). statev2.sqlite is 23.3 GB, freelist_count = 0. Background compaction runs (compacted-at from earlier the same day; only 480 intermediate message.updated / node.updated rows left, all written after that pass).

    orchestration_events by type (payload bytes; 2,908,049 rows, 6.92 GB):

    event_typerowspayloadshare
    turn-item.updated1,279,1004,895 MB70.7%
    message.updated710,031863 MB12.5%
    node.updated483,528579 MB8.4%
    provider-turn.updated198,503208 MB3.0%
    subagent.updated60,954159 MB2.3%
    the other types175,933216 MB3.1%

    The query from this issue:

    Intermediate snapshots excluding first+latest:     85,330 /   269 MB   (5.5% of turn-item payload)
      of which item type "subagent":                   47,243 /   131 MB
    All but the latest per item:                      291,001 /   692 MB
    Distinct turn items:                              988,106

    So on this install, first-and-latest compaction would remove about 0.27 GB of 4.9 GB. #20990 keeps every subagent snapshot and command-associated ones, so it would remove at most about half of that (≤0.14 GB). Most items have one row; the bytes are the latest state of about a million items (command_execution 1.75 GB, dynamic_tool 1.62 GB, assistant_message 0.66 GB, user_message 0.43 GB), stored again in the projections.

    Event payload grows 0.15–0.65 GB a day (Oct 10–24), about 70% of it turn-item.updated. Compaction (ProjectionMaintenance.ts#L233-L243) only removes superseded rows, and nothing removes old or deleted threads; deleted threads alone account for 1.68 GB of event payload here (see #17845). On this install, startup after a 1.3 GB WAL took ~4.5 min, past the desktop's 60 s readiness timeout (details in #22983).

    #20990 is still worth having, but on installs like this one what bounds the file is retention (#15122, #22630) or deletes that purge rows (#17845).

    Measured read-only on the live file in short chunked reads (to avoid a long reader snapshot, #23482); totals may be a few minutes' writes off.

  8. I'm seeing a severe instance of this on Windows with 0.1.3-nightly.20261026.3740. My statev2.sqlite is 117.9 GiB, with 15,893,258 rows in orchestration_events. Three delegated Codex threads account for 15,489,331 rows (97.5%).

    Read-only samples from early, middle, and late in those threads are dominated by turn-item.updated events for command execution and dynamic tool items. In one sample, a single tool item had 64 snapshots with only two distinct payloads. Its completed snapshot contained the same 287 KB tool result repeatedly.

    t3 doctor --dry-run reports 0 KiB reclaimable, so reclaiming free pages alone does not appear to address this case. With this data directory, the desktop backend repeatedly times out waiting for readiness and no window opens. The same installed build opens a window promptly with a fresh data directory.

    I have not deleted or modified events. This looks like a case where repeated full turn-item snapshots have accumulated enough to affect startup, beyond the disk growth described in this issue.

  9. Closing: current main already covers this as of 481b9b7192. If you still see it on a build that includes that commit, reply and we'll reopen.

  10. juliusmarminge closed this

Sign in to join this conversation.