Skip to content

fix(server): base-branch cache keys no longer hold every loose ref path - #41661

Closed
juliusmarminge wants to merge 1 commit into
mainfrom
t3/investigate-server-heap-growth
Closed

juliusmarminge wants to merge 1 commit into
mainfrom
t3/investigate-server-heap-growth

Conversation

@juliusmarminge

Copy link
Copy Markdown
Member

The server heap grew by about 25 MB/h on a long-running desktop server. Heap snapshots taken an hour apart pinned it on baseBranchCache in GitVcsDriverCore, added in #40125. It grew from 11.7 MB to 21.3 MB retained, almost all of the heap's net growth.

Each BaseBranchCacheKey.inputsStamp was the raw tokens.join("\0") of every loose ref's absolute path and stat token. In the t3code repo that is about 750 KB per key. Every ref change mints a new key. Effect's Cache drops an expired key only when that same key is read again, and a stale stamp is never read again, so old keys stay until capacity: 2_048 evicts them. That adds up to roughly 0.75–1.5 GB.

Fix

inputsStamp is now a SHA-256 of those tokens, the same way configHash already is. When the stamp changes and when it stays put works exactly as before; a key just costs about 64 bytes instead of about 750 KB.

Catching it next time

The server-metrics --soak project was a one-commit repo, and nothing in the soak subscribed to VCS status, so this cache never ran there. The soak now:

  • adds 3,000 loose remote-tracking refs to its project, and one more each turn, as fetches and agent branches do;
  • attaches one web client holding the shell, VCS status of the project, and the soak threads.

12-minute soaks of this checkout (node scripts/server-metrics.ts run --soak 12):

heap MB/h RSS MB/h top growth
before the fix 122.7 155.4 (string) +21.2 MB
after the fix 15.5 42.9 (code) +4.0 MB (JIT warm-up)

Testing

  • The existing resolves a branch's base once while its refs and branch config are unchanged test passes.
  • check:changed: format, lint, knip and typecheck pass.
  • One test fails: GitVcsDriverCore.test.ts > runs background Git in its own lane so user commands never queue behind it. It fails the same way on origin/main without this change.

🤖 Generated with Claude Code (Claude Opus 5.5)

The base-branch cache keyed entries by the raw join of every loose ref's path and stat token. In a
repository with thousands of remote branches that is ~750 KB per key, and once refs move a stale key
is never read again, so Effect's Cache keeps it until 2,048 keys pile up. A heap diff of a
long-running server showed it growing ~9.5 MB/h, nearly all of the heap's growth. The stamp is now a
SHA-256 of those tokens.

The server-metrics soak now runs in a project with 3,000 loose remote refs that gains one per turn,
under a web client holding VCS status, so ref-keyed caches churn as they do on a real checkout. A
12-minute soak measured 122.7 MB/h of heap growth before the fix and 15.5 MB/h after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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 23, 2026
@coderabbitai

coderabbitai Bot commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The base-branch input stamp now uses a SHA-256 digest. The server metrics soak scenario creates and grows a set of loose remote refs, attaches a web client, and checks client health after sampling.

Changes: Base-branch inputs and soak

Layer / File(s) Summary
Hash base-branch inputs
apps/server/src/vcs/GitVcsDriverCore.ts
readBaseBranchInputsStamp returns the hex-encoded SHA-256 digest of the NUL-separated input tokens instead of the joined token string.
Exercise the soak scenario
scripts/server-metrics.ts
The soak scenario creates 3,000 loose remote refs with the project’s current HEAD, attaches a web client using a newly issued token, and adds one soak-${turn} ref before each turn. It fails if the client is unhealthy after the final sample. The --soak description now states these scenario details.

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

Change: Bug fix

Suggested reviewers: macroscopeapp, stienswout

Merge Risk: Minimal · up to 735af

The change shrinks the base-branch cache key from the raw joined ref stamps to a fixed-size hash and extends the metrics soak scenario. No merge-blocking risk is evident.

Pre-merge checks: Passed 3 · Inconclusive 1

Failed Failed checks: 1 inconclusive

Check name Status Explanation Resolution
Description check Inconclusive The description follows the template. It has Problem, Change, Scope, and Verification content. The Problem section gives the heap growth, the cause, and the reproduction approach. The Change section e… Add a Scope note. Link the issue that shows the bug, or explain why a small obvious fix needs no issue. If a maintainer asked for this PR, link that request. Optionally state the model and harness in the required closing line in the same fo…
Passed Passed checks: 3 passed

Check name Status Explanation
Title check Passed The title names the server fix (base-branch cache keys no longer hold every loose ref path), which matches the GitVcsDriverCore change that hashes the inputs stamp with SHA-256. It is specific and fol…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title names the server fix (base-branch cache keys no longer hold every loose ref path), which matches the GitVcsDriverCore change that hashes the inputs stamp with SHA-256. It is specific and follows the conventional commit form. It does not mention the soak script changes, which is acceptable because they are secondary to the main fix.

Full details: Description check

Explanation

The description follows the template. It has Problem, Change, Scope, and Verification content. The Problem section gives the heap growth, the cause, and the reproduction approach. The Change section explains the SHA-256 stamp. The Scope section does not link an issue or say why none is needed, and it does not state whether a maintainer requested the PR. The Verification section gives soak measurements and test results, and it names the one failing test with evidence that it fails on origin/main too. The template asks for a short agent or model note at the end; the Claude Code line partly covers this.

Resolution

Add a Scope note. Link the issue that shows the bug, or explain why a small obvious fix needs no issue. If a maintainer asked for this PR, link that request. Optionally state the model and harness in the required closing line in the same format as the template.

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
Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between b5e1f91 and 735af72.

Files selected for processing: 2

  • apps/server/src/vcs/GitVcsDriverCore.ts
  • scripts/server-metrics.ts

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 3 remain after this review.

@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: b5e1f91 · PR result: 735af72 · 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.

@juliusmarminge

Copy link
Copy Markdown
Member Author

Closing in favor of #41740, which takes the stamp out of the base-branch cache key entirely: the key is now cwd + branch + allowRemoteOfCurrent, and the (narrowed, SHA-256) stamp lives in the cached value and is compared on read. Stale stamps are overwritten in place instead of accumulating, so the leak this PR worked around can't occur. The mbp heap-snapshot diff that confirmed the leak (+15.7 MB/h of BaseBranchCacheKey.inputsStamp strings, 69 → 92 keys/h) is summarized in #41740.

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.

1 participant