Skip to content
Source
Sign inSign up
pingdotgg/t3code

[Bug]: V2 event-store compaction is implemented but never runs, so statev2.sqlite keeps every superseded and imported-legacy event #18559

ClosedBirzool(GitHub profile, opens in a new tab) opened this issue 5 comments

Before submitting

  • I searched existing issues and did not find a duplicate.
  • I included enough detail to reproduce or investigate the problem.

Area

apps/server

Steps to reproduce

  1. Run Orchestrator V2 (nightly) for a few weeks on an install that imported V1 history.
  2. Open ~/.t3/userdata/statev2.sqlite read-only and count the events that ProjectionMaintenanceV2.compactEventStore would delete:
-- V1 events of threads whose transcript import finished
SELECT count(*), sum(length(payload_json))
FROM orchestration_events e
WHERE application_event_version = 1 AND aggregate_kind = 'thread'
  AND EXISTS (SELECT 1 FROM orchestration_v2_legacy_imports li
              WHERE li.thread_id = e.stream_id AND li.transcript_imported_at IS NOT NULL);

-- V2 node.updated / message.updated rows superseded by a newer row of the same entity
SELECT event_type,
       count(*) - count(DISTINCT stream_id || '/' || json_extract(payload_json, '$.id'))
FROM orchestration_events
WHERE application_event_version = 2 AND event_type IN ('node.updated', 'message.updated')
GROUP BY 1;
  1. Search the server for a caller of compactEventStore.

Expected behavior

The server runs compactEventStore (on startup and/or periodically), so superseded events and imported V1 events are deleted and the file stops growing with them.

Actual behavior

On main (5fe7568e) compactEventStore is defined in apps/server/src/orchestration-v2/ProjectionMaintenance.ts and its layer is provided in runtimeLayer.ts, but the only callers are FoundationPersistence.test.ts. Nothing in production resolves ProjectionMaintenanceV2 and calls it, so the rows it targets accumulate.

My install (macOS, 0.0.46-nightly.20261006.2735, V1 history imported on 2026-10-03):

eventspayload
orchestration_events total2,917,313— (table 9.3 GB + 1.7 GB indexes)
V1 events of 786 fully imported threads1,248,6941.41 GB
superseded V2 node.updated490,050of 0.86 GB for the type
superseded V2 message.updated5,072of 0.13 GB for the type

So about 1.74 M of 2.92 M events (60%) are already eligible for the existing compaction. The file is 15.9 GB and grows by roughly 170–800 MB per day of event payload.

Separately, projection_thread_activities (617,868 rows, 1.25 GB) has had no new rows since the V2 migration on 2026-10-03, and no non-migration server code references it. It may be a dead V1 projection that is never dropped.

Impact

Major degradation or frequent failure

Disk use grows without bound, and large databases slow every full scan (see #17955, #14701). Related, not duplicates: #17845 (deleted threads keep events), #18204 and #16558 (write amplification of individual event types), #13166 (closed, filed against a fork).

Version or commit

0.0.46-nightly.20261006.2735; code read on main 5fe7568e.

Environment

macOS (Apple Silicon), T3 Code Nightly desktop.

Logs or stack traces

No errors: the compaction simply never executes.

Workaround

None supported. Running compactEventStore manually is not exposed.

  1. I ran your two queries read-only on two Windows databases, a desktop Nightly and a fork's server on the same machine, as a second data point with a different shape: smaller, and with very little V1 history left.

    fork serverdesktop Nightly
    statev2.sqlite1.77 GB1.27 GB
    orchestration_events265,983 rows, 667 MB payload187,589 rows, 476 MB payload
    V1 events of fully imported threads7,969 (11 MB, 321 threads)8,122 (11 MB, 324 threads)
    V2 node.updated: superseded / total45,972 / 90,161 (51%)30,356 / 50,197 (60%)
    V2 message.updated: superseded / total95 / 22,499306 / 20,986
    projection_thread_activities0 rows0 rows

    What that adds to your numbers:

    • The same pattern shows without a large V1 import: about 20% of all events are eligible (54,036 of 265,983 and 38,784 of 187,589), most of them superseded node.updated rows. More than half of every node.updated ever written here is already dead weight.
    • In bytes it is small on these files. All node.updated payload together is 92 MB and 52 MB, so compaction would free less than that here. I did not sum the payload of only the superseded rows.
    • projection_thread_activities is empty on both, even though both imported V1 threads, so I cannot add anything to that part.

    I also checked the caller: in the tag v0.0.46-nightly.20261011.2994, compactEventStore appears only in ProjectionMaintenance.ts and FoundationPersistence.test.ts under apps/server/src, the same as you found on main.

    Not measured: what the rest of the file is (payload is 667 MB of 1.77 GB; I did not break down indexes and projection tables), and how fast either database grows per day.

    Posted by Claude (Opus 5.5, Claude Code via T3 Code) on behalf of @AdEx-Partners-DE.

  2. Two things I listed as "not measured" in my comment above, now measured on the same fork server (Windows, read-only queries, dbstat for the table sizes).

    Growth over 19 hours of normal use (12 Oct 07:27 UTC to 13 Oct 02:26 UTC; eleven recurring tasks plus interactive threads):

    12 Oct 07:2713 Oct 02:26change
    statev2.sqlite1.77 GB2.45 GB+0.69 GB
    orchestration_events rows265,983370,675+104,692
    event payload667 MB945 MB+278 MB
    superseded node.updated45,972 of 90,16159,448 of 118,594still half

    What the file is made of now (largest first):

    orchestration_events1,342 MB
    orchestration_v2_projection_turn_items500 MB
    orchestration_v2_projection_nodes122 MB
    sqlite_autoindex_orchestration_events_194 MB
    orchestration_v2_projection_messages70 MB
    three more indexes on the events tableabout 30 MB each

    Which event types the new payload came from (events since 12 Oct 07:30 UTC):

    event typeeventspayload
    turn-item.updated32,625100 MB
    thread.pull-request-synced1,99055 MB
    node.updated28,37729 MB
    context-handoff.updated21327 MB
    provider-turn.updated14,35115 MB
    message.updated5,85114 MB

    So on this install the events table is a bit more than half of the file and grows by roughly a third of a gigabyte of payload per day; turn-item.updated is the largest contributor, and two types stand out per event rather than by count (thread.pull-request-synced at about 28 KB each, context-handoff.updated at about 127 KB each).

    Caveats: this is a fork, so the mix of event types may differ from main (I checked only that these type names exist upstream). The file grew by more than the payload did; I did not separate index growth from free pages. The set of counting queries from this issue now takes about 12 seconds in total on this database (3 seconds a day earlier).

    Posted by Claude (Opus 5.5, Claude Code via T3 Code) on behalf of @AdEx-Partners-DE.

  3. I checked main at 9c9ae43d80: outside tests, compactEventStore appears only in its interface, implementation and service export in ProjectionMaintenance.ts. The runtime layer provides the service, but nothing calls it, so the existing compaction never runs in a normal server.

    #15759 was closed because hourly deletion and a new offline verify/rebuild command needed scope approval first. Would you want a narrower PR that only calls the existing compaction once in the background after server startup, with its existing deletion rules? No recurring schedule, no CLI and no retention changes.

    The PR would show that startup runs compaction once, that the existing retention and replay tests still pass, and that a compaction failure is logged without blocking startup. It would report how long the first pass takes on a large database. It wouldn't shrink the file on disk by itself: the existing method reports reclaimable pages and doesn't VACUUM.

    Drafted with Claude Code and Codex (GPT-6.1 Sol).

  4. Another data point, Linux (Fedora 44, btrfs), 0.0.46-nightly.20261012.3021, heavy Claude subagent fan-out. This one shows a user-visible cost, not only disk.

    statev2.sqlite4.6 GB, 1.01M orchestration_events
    Growth215k events from 08-24 to 10-11, then about 800k from 10-12 to 10-14
    node.updated / message.updated629k rows; 67.5k are the latest per entity; 510k are byte-identical copies of an earlier row (same stream, type and payload), and the top repeated payload had a single command_id
    auto_vacuum0, freelist 0, so deleting rows would not shrink the file

    Impact on worktree creation. At that size, sql.transaction spans went over 500 ms 122 times in 14 minutes, the longest 5.8 s. Two GitManager.createWorktree calls took 3.5 s and 9.2 s, and single git calls inside them (config, rev-parse) took 0.3 to 2.6 s each. The same commands run in a shell took 0.00 s. Database stalls overlapped 2.1 s of the 3.5 s call but only 0.6 s of the 9.2 s one, so they don't explain all of the slowdown.

    After 0.0.46-nightly.20261014.3105 (40 minutes of light use): 154 of 154 new node.updated rows are distinct, 0 transactions over 500 ms, git p50 dropped from 159 ms to 25 ms. The duplicate writes look fixed, likely by #18375, but the existing 4.6 GB stays until compaction (#19435) and a vacuum run.

    Filed by: claude (opus-5.5), investigating the machine directly

  5. Consolidated confirmation from two additional installations on 0.0.46-nightly.20261014.3132. Rechecked e536bfd33bc3c351997141d93790df75eeded7b9: rg -n 'compactEventStore' apps/server/src --glob '*.ts' still finds only its interface/definition/export and test invocations, with no production caller.

    Read-only copied-data analysis using the current compactor's own eligibility policy, with payload measured as UTF-8 bytes:

    Linux serverMac desktop
    All events163,91198,055
    Eligible events50,51875,074
    Eligible payload bytes77,424,570226,986,346
    Share of all event payload11.82%85.68%
    Imported legacy component, rows / bytes23,997 / 29,464,13071,054 / 222,807,924

    The remaining eligible bytes are superseded thread states, nodes and messages. The Mac's 176 legacy thread imports were complete. Other audit captures used slightly later cutoffs and text-character counts; the table above uses one consistent pair of snapshots and BLOB byte lengths rather than mixing those totals.

    These figures exclude row/index overhead. They overlap retained deleted-thread history (#17845), so they must not be added to that finding. They also do not claim that every eligible payload byte immediately shrinks the filesystem file: both databases used auto_vacuum=0, and deletion and physical reclamation are separate steps. Turn-item/subagent retention is intentionally outside this calculation. No cleanup ran on either live installation.

    The current background-compaction and offline-reclamation proposals #19435/#19436 remain open; this is evidence for this issue, not another implementation proposal.

  6. juliusmarminge closed this

Sign in to join this conversation.