Skip to content

fix(server): migration 95 finishes on large databases - #37995

Open
juliusmarminge wants to merge 1 commit into
mainfrom
factory/issue-37961
Open

juliusmarminge wants to merge 1 commit into
mainfrom
factory/issue-37961

Conversation

@juliusmarminge

Copy link
Copy Markdown
Member

Migration 95 set inline_work_output with one bulk UPDATE inside the migration transaction. SQLite keeps a copy (the statement journal) of every row a statement rewrites, payload overflow pages included, until the statement ends, and Sqlite.ts sets temp_store = MEMORY, so that copy sat in RAM and peak memory grew about 1 GB per GB of matching payloads. The migration now pages through matching rows by rowid (keyset, 500 per page; same pattern as migration 081) and updates one row per statement, so each journal holds a single row. The WHERE condition, the result and the single migration transaction are unchanged; temp_store is untouched. A small node:sqlite test confirmed the cause: a 510 MB rewrite peaked at 569 MB with temp_store=MEMORY and 53 MB with FILE; UPDATE OR FAIL did not avoid the journal. Measured the way triage did, on Linux: a statev2.sqlite built to migration 94 with the server's own runMigrations, filled with 150 KB dynamic_tool rows (N containing htmlRender, N not), the real server started (node src/bin.ts serve --base-dir <copy>), VmHWM sampled every second until /.well-known/t3/environment returned 200; fresh copy per run, sync before each, before and after alternated. 2 GB matched (13,400 rows): BEFORE peak RSS 2503, 2480, 2512, 2497, 2509 MB (18-59 s, median 41 s of the 3 alternated runs); AFTER 595, 576, 494, 585, 537, 539 MB (20-54 s, median 23 s of the 3 alternated runs). 4 GB matched (26,800 rows): AFTER 511 MB, 90 s; before not rerun here (triage measured 4483 MB, 93 s on main). Every run reached migration 97 with exactly the matching rows flagged. The 21 GB case was not run, but peak memory stayed flat from 2 GB to 4 GB. No new regex or parser.

Fixes #37961

Reproduction on main

Commit 0ff1495 (origin/main). Built a fresh statev2.sqlite at migration 94 with the server's own runMigrations({toMigrationInclusive: 94}). Filled copies with synthetic dynamic_tool rows of about 150 KB each (the reporter's average row size): N rows whose payload contains htmlRender (matched by migration 95's WHERE) plus N that do not. Started the real server on each copy (node apps/server/src/bin.ts serve --base-dir <copy>), sampled /proc//status VmHWM every second, and stopped when /.well-known/t3/environment returned 200 (migrations then recorded through 97). Peak RSS: empty DB 551 MB (6 s). 1 GB matched (6,700 rows): 1457, 1499, 1510 MB (median 1499), 18-23 s. 2 GB matched (13,400 rows): 2483, 2485, 2499 MB (median 2485), 32-48 s. 4 GB matched (26,800 rows): 4483 MB, 93 s. Control on the same 4 GB DB with only PRAGMA temp_store = MEMORY changed to FILE in Sqlite.ts (temporary, reverted): peak 486 MB, 99 s. So peak memory grows about 1 GB per GB of payload rewritten, entirely from the in-memory temp store. Extrapolated to the reporter's 21 GB rewrite that is about 21 GB of extra RAM, consistent with their 15 GB footprint and stall on a 36 GB Mac. The full 21 GB case was not run (this VM has 15 GB RAM), so the final stall itself was not observed. Driver scripts: https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/8357baac1d07e609/seed94.ts https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/d57fe59040998eb1/fill.sh https://gh-file-drop-api-prod-mi5fy3sowv63ufte.pinglabs.workers.dev/f/eead16d544dd71e7/run.sh

Evidence

Being captured: before and after in the real app. This section is filled in before the pull request can merge.

Tests

vp test run src/persistence/Migrations/095_TurnItemInlineWorkOutput.test.ts src/persistence/Migrations.test.ts: 12 passed. Extended the 095 test with 1001 more matching rows; checked that it fails when paging is broken (always breaking after the first page), then restored. vp run check:changed: format, lint, knip, graphql, typecheck (3 packages) and tests (2 files) all pass. Before/after real-server measurements: see summary.

Not in this change

  • PRAGMA temp_store = MEMORY in Sqlite.ts applies to every statement; any other large single-statement rewrite (future migrations or backfills) will hit the same per-statement RAM growth. Not changed here because it is a global default.

Who did the work

  • Written by Claude Opus 5.5, in Claude Code
  • Reviewed by GPT-6.1 Sol, in Codex, and by Claude Opus 5.5, each on its own
  • Proof captured by GPT-6.1 Sol and checked by Claude Opus 5.5

Run by the t3code factory in T3 Code.

Migration 95 flagged inline work output with one bulk UPDATE. SQLite
journals every row a statement rewrites, payload included, and
temp_store = MEMORY keeps that journal in RAM, so peak memory grew about
1 GB per GB of matching payloads and large databases never finished
starting.

Page through matching rows by rowid and update them one row per
statement, so each statement journal holds a single row.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions github-actions Bot added vouch:trusted Author is trusted by repo permissions or the VOUCHED list. size:S 10-29 changed lines (additions + deletions). labels Nov 17, 2026
@macroscopeapp

macroscopeapp Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Approvability

Verdict: Approved at abb090a

Macroscope's review found this PR approvable — Migration 95 now backfills the same inline-output flags using paged identifiers and single-row updates to reduce memory pressure on large databases. Existing migration transaction boundaries and product behavior remain unchanged.

You can add or adjust custom eligibility rules. Learn more.

@github-actions

Copy link
Copy Markdown
Contributor

Thread transfer impact

✅ Thread transfer remains within every enforced ceiling.

Provider Metric Main baseline This PR Impact PR ceiling
Codex Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Codex Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Codex Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Codex Live turn WebSocket decoded 21.5 KiB 21.5 KiB 0 B (0.0%) 24.4 KiB ✅
Codex Live turn messages 1 1 0 (0.0%) 8 ✅
Claude Total thread wire 5.0 KiB 5.0 KiB 0 B (0.0%) 6.8 KiB ✅
Claude Thread snapshot wire 3.8 KiB 3.8 KiB 0 B (0.0%) 4.9 KiB ✅
Claude Live turn WebSocket wire 1.2 KiB 1.2 KiB 0 B (0.0%) 2.0 KiB ✅
Claude Live turn WebSocket decoded 21.9 KiB 21.9 KiB 0 B (0.0%) 24.4 KiB ✅
Claude Live turn messages 1 1 0 (0.0%) 8 ✅

Other scenarios

All 64 scenario measurements match main.

All 64 scenario measurements
Provider Scenario Metric Main baseline This PR Impact PR ceiling
Codex short turn resume WebSocket decoded 10.5 KiB 10.5 KiB 0 B (0.0%) 12.7 KiB ✅
Codex short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn running, cold open HTTP decoded 281.7 KiB 281.7 KiB 0 B (0.0%) 302.7 KiB ✅
Codex long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Codex long turn running, history page HTTP decoded 92.4 KiB 92.4 KiB 0 B (0.0%) 107.4 KiB ✅
Codex long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Codex long turn finished, cold open HTTP decoded 381.5 KiB 381.5 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, socket cold open WebSocket decoded 381.6 KiB 381.6 KiB 0 B (0.0%) 410.2 KiB ✅
Codex long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Codex long turn finished, work page HTTP decoded 186.2 KiB 186.2 KiB 0 B (0.0%) 200.2 KiB ✅
Codex long turn finished, work page rows 150 150 0 (0.0%) 150 ✅
Codex idle thread resume WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell snapshot HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Codex fleet shell subscribe WebSocket decoded 65 B 65 B 0 B (0.0%) 200 B ✅
Codex fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Codex fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Codex fleet shell resume check rows 0 0 0 0 ✅
Codex stale shell reload HTTP decoded 42.4 KiB 42.4 KiB 0 B (0.0%) 53.7 KiB ✅
Codex stale shell reload rows 27 27 0 (0.0%) 27 ✅
Codex stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Codex stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Codex stale todo list cold open HTTP decoded 72.4 KiB 72.4 KiB 0 B (0.0%) 83.0 KiB ✅
Codex stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Codex stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Codex stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Codex stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅
Claude short turn resume WebSocket decoded 10.6 KiB 10.6 KiB 0 B (0.0%) 12.7 KiB ✅
Claude short turn resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn running, cold open HTTP decoded 283.3 KiB 283.3 KiB 0 B (0.0%) 302.7 KiB ✅
Claude long turn running, cold open rows 75 75 0 (0.0%) 75 ✅
Claude long turn running, history page HTTP decoded 93.4 KiB 93.4 KiB 0 B (0.0%) 107.4 KiB ✅
Claude long turn running, history page rows 75 75 0 (0.0%) 75 ✅
Claude long turn finished, cold open HTTP decoded 384.2 KiB 384.2 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, socket cold open WebSocket decoded 384.3 KiB 384.3 KiB 0 B (0.0%) 410.2 KiB ✅
Claude long turn finished, socket cold open WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude long turn finished, socket cold open rows 151 151 0 (0.0%) 151 ✅
Claude long turn finished, work page HTTP decoded 188.1 KiB 188.1 KiB 0 B (0.0%) 200.2 KiB ✅
Claude long turn finished, work page rows 150 150 0 (0.0%) 150 ✅
Claude idle thread resume WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude idle thread resume WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell snapshot HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude fleet shell snapshot rows 27 27 0 (0.0%) 27 ✅
Claude fleet shell subscribe WebSocket decoded 66 B 66 B 0 B (0.0%) 200 B ✅
Claude fleet shell subscribe WebSocket messages 1 1 0 (0.0%) 1 ✅
Claude fleet shell resume check HTTP decoded 18 B 18 B 0 B (0.0%) 100 B ✅
Claude fleet shell resume check rows 0 0 0 0 ✅
Claude stale shell reload HTTP decoded 43.0 KiB 43.0 KiB 0 B (0.0%) 53.7 KiB ✅
Claude stale shell reload rows 27 27 0 (0.0%) 27 ✅
Claude stale shell resume WebSocket decoded 140 B 140 B 0 B (0.0%) 400 B ✅
Claude stale shell resume WebSocket messages 2 2 0 (0.0%) 2 ✅
Claude stale todo list cold open HTTP decoded 73.4 KiB 73.4 KiB 0 B (0.0%) 83.0 KiB ✅
Claude stale todo list cold open rows 75 75 0 (0.0%) 75 ✅
Claude stale todo list cold open nodes 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open subagents 2 2 0 (0.0%) 4 ✅
Claude stale todo list cold open plans 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open providerTurns 1 1 0 (0.0%) 2 ✅
Claude stale todo list cold open checkpoints 1 1 0 (0.0%) 2 ✅

Baseline: cd9c39f · PR result: abb090a · Source CI: success

Scenario and decoded snapshot size

10 historical turns, 5 command tools per turn, 878.9 KiB retained MCP result per historical turn, and a 1.05 MiB retained result in the measured turn.

  • Codex decoded thread snapshot: 108.5 KiB
  • Claude decoded thread snapshot: 108.8 KiB

Updated in place by a trusted workflow. PR artifacts are strictly validated and never executed.

@coderabbitai

coderabbitai Bot commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Migration 95 now scans matching dynamic_tool rows in rowid-ordered batches of up to 500 and updates each selected row individually. The migration test adds 1,001 matching rows and checks that each has inline work output flagged.

Changes: Migration 95 backfill

Layer / File(s) Summary
Batched migration backfill
apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.ts, apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.test.ts
The migration scans matching rows in batches of up to 500 and updates selected rows individually. The test checks the flag on 1,001 matching rows.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: valuin

Merge Risk: Minimal · up to abb09

Migration 95 now updates matching rows in small batches to cut peak memory during large upgrades. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Linked Issues check Inconclusive Linked issue #37961 requires that migration 95 finishes and the server becomes ready when upgrading a large statev2.sqlite from head 94. The reviewed head changes apps/server/src/persistence/Migration… Provide the recorded before and after evidence named in the PR Evidence section. Show that migration 95 completes and the server reaches readiness on a database with about 21 GB of matching payload, or on the largest feasible size with a me…
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title "fix(server): migration 95 finishes on large databases" names the main change. The PR replaces one bulk UPDATE in migration 95 with keyset-paged, per-row updates to stop memory growth from t…
Description check Passed The description covers the problem, the change, the scope, and verification. Problem: migration 95 bulk UPDATE with SQLite statement journal grows RAM about 1 GB per GB of payload. Change: keyset pagi…
Out of Scope Changes check Passed All changed code is in the migration 095 file and its test. The migration change implements the paging and per-row update required to limit statement-journal memory for issue #37961. The test extensio…
Full details: Title check

Explanation

The title "fix(server): migration 95 finishes on large databases" names the main change. The PR replaces one bulk UPDATE in migration 95 with keyset-paged, per-row updates to stop memory growth from the statement journal. The title states the outcome (migration finishes) and the affected area (server, migration 95). It does not name the mechanism, but it is specific and matches the changeset.

Full details: Description check

Explanation

The description covers the problem, the change, the scope, and verification. Problem: migration 95 bulk UPDATE with SQLite statement journal grows RAM about 1 GB per GB of payload. Change: keyset pagination by rowid, 500 rows per page, one UPDATE per row. Scope: links issue #37961 and explains the reproduction on main. Verification: focused tests and before/after real-server measurements. It departs from the template in a few places. The Evidence section says it is still being captured and must be filled before merge. The template's 'Problem' and 'Change' headings are not used as labeled sections, and the Scope section does not state whether a maintainer requested the PR. The description also contains long external driver-script links and extra sections (Not in this change, Who did the work) that the template does not define. These gaps are minor, so the check passes.

Full details: Out of Scope Changes check

Explanation

All changed code is in the migration 095 file and its test. The migration change implements the paging and per-row update required to limit statement-journal memory for issue #37961. The test extension adds rows that exercise pagination across page boundaries, which supports the same objective. The PR states that the global PRAGMA temp_store setting is not changed, and the diff confirms no change to Sqlite.ts. No unrelated changes are present.

Full details: Linked Issues check

Explanation

Linked issue #37961 requires that migration 95 finishes and the server becomes ready when upgrading a large statev2.sqlite from head 94. The reviewed head changes apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.ts. The ALTER TABLE and the WHERE predicate are unchanged from the merge base. The bulk UPDATE is replaced by keyset pagination on rowid (500 rows per page) with one UPDATE per row, inside the same transaction. This targets the per-statement statement-journal cost that the issue describes as the memory driver. The 095 test is extended with 1,001 more matching rows and checks the flagged set. The PR reports real-server measurements up to 4 GB matched payload (peak RSS about 0.5 GB at 2 GB and 4 GB). The 21 GB case in the issue was not run. The Evidence section of the PR description is empty ('being captured'), so no recorded before/after proof exists in the PR. The single transaction still holds all rewritten pages in the WAL until commit. The issue reports WAL growth to 16 GB, and that growth is not measured here. Completion at the issue's scale is therefore not established.

Resolution

Provide the recorded before and after evidence named in the PR Evidence section. Show that migration 95 completes and the server reaches readiness on a database with about 21 GB of matching payload, or on the largest feasible size with a measured extrapolation. Report WAL size and total duration for that run.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

Note

This review was completed with usage-based billing: files reviewed beyond your plan's included limits are billed at $0.25/file. View usage-based billing.

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between cd9c39f and abb090a.

Files selected for processing: 2

  • apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.test.ts
  • apps/server/src/persistence/Migrations/095_TurnItemInlineWorkOutput.ts

Limit details: You’ve used all 10 included reviews currently available.

@juliusmarminge

Copy link
Copy Markdown
Member Author

The automated pipeline stopped on this pull request and will not merge it:

the fix is written and reviewed, but proving it needs something no worker has:

  • Permanent blocker: a macOS host outside the mobile device pool is required to run the real server on the reported platform. The brief prohibits reaching another machine or substituting Linux.
  • Neither commit's migration duration, peak memory, readiness, or resulting data equivalence was measured; stable medians and an improvement exceeding run spread remain unverified.

It stays open for a person to finish or close.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S 10-29 changed lines (additions + deletions). vouch:trusted Author is trusted by repo permissions or the VOUCHED list.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Migration 95 (TurnItemInlineWorkOutput) never finishes on a large database: 21 GB row rewrite with the undo journal held in RAM

1 participant