Skip to content

Opening the Git actions menu on Windows reloads the branch list every time: 2 listRefs requests and 9 git per open, and it clears the persisted refs cache #40355

Closed

Description

@juliusmarminge

What is slow

Opening the Git actions menu in the thread details panel runs a deliberate vcs.refreshStatus (terminal edits and commits publish no status). Its onSettled then invalidates the cached branch list. So every open also clears the environment's persisted refs cache and re-sends the mounted branch selector's two vcs.listRefs with refresh (9 git, ~230 ms).

The menu shows no branch list, and in these runs no ref moved. Mobile uses the same vcsEnvironment.refreshStatus command (use-selected-thread-git-actions.ts), so it pays the same reload.

Measured

  • Windows Server 2025, 4 vCPU, 16 GB, Defender real-time scanning on, Git 2.56.1.windows.1, commit 989d7ec.
  • A 32,210-file clone with a local bare remote, on a branch with an upstream. One modified file: a clean tree leaves the menu disabled. One thread; headless Chromium through Playwright.
  • 7 opens of Git action options, 8 s apart. Every open sent vcs.refreshStatus x1 and vcs.listRefs x2.
  • Server cost per open:
    • The refresh ran 26 or 34 git (1,457-2,048 ms), alternating with and without a background fetch.
    • The 2 listRefs ran 9 git together (229-235 ms).
    • Totals: 35 or 43 git per open.
  • Over 7 opens, 63 git and 14 requests went to a branch list nobody was looking at.
  • One reload:
ws.rpc.vcs.listRefs 230ms git=8
  resolveRepositoryPathsUncached git=3   (rev-parse --git-common-dir, --show-toplevel, symbolic-ref HEAD)
  readGitRefsSnapshot 148ms git=4        (for-each-ref, rev-parse origin/HEAD, worktree list, remote)
  GitVcsDriver.listRefs.currentBranch git=1
ws.rpc.vcs.listRefs 229ms git=1          (second query, shares the snapshot)

How to measure it again

Harness (Windows, PowerShell)

  1. Check out the commit in C:/p/wt. Run vp i, then vp build in apps/web.
  2. Make a large repository with a remote:
    • git clone --bare --no-local <a t3code checkout> C:/p/repos/remote.git
    • git -c core.longpaths=true clone C:/p/repos/remote.git C:/p/repos/p0
    • In p0: set user.name and user.email, then git checkout -b work; git push -u origin work.
    • Add git branch feat-a HEAD~20; git push -u origin feat-a.
  3. Add the free replay provider. Before the first server start, write C:/p/home/userdata/settings.json: {"providerInstances":{"devReplay":{"displayName":"Replay (dev)","driver":"acpRegistry","config":{"source":"local","commandPath":"<full path of node.exe>","commandArgs":["C:/p/wt/apps/server/scripts/dev-replay-agent.ts"]}}}}
  4. In apps/server, with T3CODE_DEV_SEED=0 set, run node src/bin.ts serve --base-dir C:/p/home --port 4800 --host 127.0.0.1 and keep it running. Over SSH, start it with Invoke-CimMethod Win32_Process Create.
  5. node src/bin.ts project add C:/p/repos/p0 --base-dir C:/p/home
  6. Create a thread. In C:/p/repos/p0, run node C:/p/wt/apps/server/src/bin.ts thread start --base-dir C:/p/home --provider devReplay --model command-output C:/p/repos/p0 go. thread list prints its id, mcp:<uuid>. The page path is /ENVID/mcp:<uuid>.
  7. Pair a browser: node src/bin.ts pair --ttl 1h --base-dir C:/p/home prints a pair#token=... URL. Replace its host with 127.0.0.1:4800 and export it as PAIR. Export PW as the checkout's node_modules/.pnpm/playwright@*/node_modules/playwright directory.
  8. Delete C:/p/home/userdata/logs/server.trace.ndjson* before each run. Afterwards, copy them out and run sum.cjs. A git process is a runGitCommand or VcsProcess.spawn span.

common.cjs:

const fs = require('fs');
const { chromium } = require(process.env.PW);
const NL = String.fromCharCode(10);
exports.open = async (path) => {
  const browser = await chromium.launch();
  const page = await (await browser.newContext({ viewport: { width: 1600, height: 1000 } })).newPage();
  const sent = [];
  page.on('websocket', (ws) => ws.on('framesent', (f) => {
    try { for (const line of String(f.payload).split(NL)) { if (!line) continue; const j = JSON.parse(line); if (j._tag === 'Request') sent.push({ t: Date.now(), tag: j.tag }); } } catch {}
  }));
  await page.goto(process.env.PAIR);
  await page.waitForTimeout(4000);
  const env = fs.readFileSync('C:/p/home/userdata/environment-id', 'utf8').trim();
  await page.goto('http://127.0.0.1:4800' + path.replace('ENVID', env));
  return { browser, page, sent };
};
exports.record = (tag, rec) => { fs.mkdirSync('C:/p/perf/out', { recursive: true }); fs.appendFileSync('C:/p/perf/out/' + tag + '.jsonl', JSON.stringify(rec) + NL); };
exports.reqs = (sent, a, b) => { const c = {}; for (const s of sent) if (s.t >= a && s.t <= b) c[s.tag] = (c[s.tag] || 0) + 1; return c; };

sum.cjs (node sum.cjs <traceDir> <out.jsonl>; CALLERS=1 lists calling spans):

const fs = require('fs'); const path = require('path');
const NL = String.fromCharCode(10);
const [dir, outFile, sf = 'start', ef = 'end'] = process.argv.slice(2);
const spans = [];
for (const f of fs.readdirSync(dir)) for (const l of fs.readFileSync(path.join(dir, f), 'utf8').split(NL)) { if (!l) continue; try { spans.push(JSON.parse(l)); } catch {} }
const byId = new Map(spans.map((s) => [s.spanId, s]));
const ms = (s) => Number(BigInt(s.startTimeUnixNano) / 1000000n);
const plumbing = ['VcsProcess', 'runGitCommand', 'GitVcsDriver.execute', 'GitVcsDriver.run', 'GitVcsDriver.gitCommand', 'gitCommand', 'ProcessRunner'];
const root = (s) => { while (s.parentSpanId && byId.has(s.parentSpanId)) s = byId.get(s.parentSpanId); return s; };
const caller = (s) => { let p = s; while (p.parentSpanId && byId.has(p.parentSpanId)) { p = byId.get(p.parentSpanId); if (!plumbing.some((x) => p.name.startsWith(x))) return p.name; } return '?'; };
const spawns = spans.filter((s) => s.name === 'VcsProcess.spawn' || s.name === 'runGitCommand');
for (const l of fs.readFileSync(outFile, 'utf8').split(NL)) {
  if (!l) continue; const r = JSON.parse(l); const a = r[sf], b = r[ef];
  const roots = new Map();
  for (const sp of spawns) { const t = ms(sp); if (t < a || t > b) continue; const rt = root(sp); const e = roots.get(rt.name) || { n: 0, ms: new Set(), callers: {} }; e.n++; e.ms.add(Math.round(rt.durationMs)); const c = caller(sp); e.callers[c] = (e.callers[c] || 0) + 1; roots.set(rt.name, e); }
  let total = 0; for (const e of roots.values()) total += e.n;
  console.log('run', r.run, 'total git=' + total);
  for (const [k, e] of roots) console.log('   ', k, 'git=' + e.n, 'ms=' + [...e.ms].join(','), process.env.CALLERS ? JSON.stringify(e.callers) : '');
}

Dirty the tree first: Add-Content C:/p/repos/p0/README.md x. Then run gitmenu.cjs (node gitmenu.cjs /ENVID/<thread id>, RUNS=7 TAG=menu):

const { open, record, reqs } = require('./common.cjs');
(async () => {
  const runs = +(process.env.RUNS || 6); const tag = process.env.TAG;
  const { browser, page, sent } = await open(process.argv[2]);
  const btn = page.getByRole('button', { name: 'Git action options' }).first();
  await btn.waitFor({ timeout: 60000 });
  await page.waitForTimeout(10000);
  for (let i = 0; i < runs; i++) {
    const t0 = Date.now();
    await btn.click();
    await page.getByRole('menu').first().waitFor({ timeout: 30000 });
    await page.waitForTimeout(5000);
    await page.keyboard.press('Escape');
    await page.waitForTimeout(3000);
    const t2 = Date.now();
    const rec = { run: i, start: t0, end: t2, reqs: reqs(sent, t0, t2) };
    record(tag, rec); console.log(JSON.stringify(rec));
  }
  await browser.close();
})().catch((e) => { console.error(e); process.exit(1); });

Every open's reqs shows vcs.listRefs: 2. sum.cjs shows the ws.rpc.vcs.listRefs roots with 9 git.

Where the time goes

  • packages/client-runtime/src/state/vcs.ts:337 declares refreshStatus with onSettled: invalidateRefs.
  • invalidateCachedVcsRefs (packages/client-runtime/src/state/vcsRefInvalidation.ts:57) clears the persisted refs for the whole environment (cache.clearVcsRefs(environmentId)) and invalidates every mounted listRefs atom for the cwd.
  • The details panel's branch selector is always mounted, so its two paginated queries refetch with refresh: true, which bypasses the server's refs snapshot cache.
  • The caller is GitActionsControl.tsx:1679 (Menu onOpenChange, then requestVcsStatusRefresh). The hover and focus refresh for an empty menu and the client refresh after a worktree removal use the same command.

Why it is avoidable

A status refresh changes refs only when its fetch moved a remote-tracking ref, or when something outside T3 Code moved one. The server already compares ref snapshots (readRefSnapshot) before and after its read. Reloading the branch list after every refresh repeats a full ref listing whose answer did not change.

What a fix has to show

Same driver, 7 menu opens with no ref change in between:

  • Expected: 0 vcs.listRefs per open, from 2. That is 9 fewer git per open (35/43 to 26/34), and the persisted refs cache is not cleared.
  • What stays: the menu-open refreshStatus itself (26-34 git, deliberate).
  • Still reloads: when a ref did move (a fetch brought a new remote branch, or git branch x ran in a terminal before a menu open), the picker still lists it.
  • Out of scope: the picker's own open refresh (refreshLocalStatusForPicker).

Found by the automated Windows performance sweep on 989d7ec.

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