Repository navigation
Conversation
GET /api/git/status ran a full working-tree scan on every request, reading every untracked file to count its lines, with nothing shared between a sidebar reload and the next, or between two tabs on one project. - lib/git-status-cache.ts: a 2 s per-cwd TTL in front of the scan, and an in-flight scan shared by concurrent requests for the same cwd, following lib/models-cache.ts. - An expired entry is dropped before the request scans rather than served: the focus reload (agegr#1144) is what makes an editor's edit visible, so a listing older than the TTL is not an answer. - The key is the cwd as spelled (the paths are rebased onto it), and the route's allow-list checks still run per request, so a revoked root is never served from an entry. - An upload invalidates eagerly, so the explorer's refetch after one is not the listing cached before the write. The response shape is unchanged; the first request still scans. Measured on a synthetic repo (300 tracked files, 100 modified, 400 untracked files of 300 lines each), timing the scan directly since the route is a thin JSON wrapper around it: origin/main 5 sequential requests: 137.2, 96.2, 162.1, 100.7, 123.5 ms branch 5 sequential requests: 118.6, 0.0, 0.0, 0.0, 0.0 ms origin/main 8 concurrent requests: 659.7 ms branch 8 concurrent requests: 115.0 ms
|
CI note: the first I could not use |
|
Thanks @brynne8 for the careful write-up and the benchmark. I measured how this behaves in the app before deciding, and I'm going to close it.
Correct badges matter more here than ~50 ms. If you've seen the scan be slow on a real large repo, I'd gladly look at a change that makes |
Returning from the mobile breakpoint restores the desktop sidebar preference in an
effect, so the toggle can swap from "Show sidebar" to "Hide sidebar" between the
`isVisible()` check and the click. The click then waits its full 30s for a button
that will not come back, and the suite dies with
locator.click: Timeout 30000ms exceeded. waiting for
getByRole('button', { name: 'Show sidebar', exact: true }) at e2e/run.mjs:496:60
Seen on several unrelated PRs (#1322, #1324 ourselves, #1308 from another
contributor).
The click now retries against a short deadline and only its own timeout is
ignored; the wait for "Hide sidebar" that follows still fails if the sidebar never
opens, so a real regression is not hidden. The fork's earlier 947c7fe reload
fallback does not cover this signature: it fires only when neither toggle appears
within its 10s bound.
…gr#1326) Returning from the mobile breakpoint restores the desktop sidebar preference in an effect, so the toggle can swap from "Show sidebar" to "Hide sidebar" between the `isVisible()` check and the click. The click then waits its full 30s for a button that will not come back, and the suite dies with locator.click: Timeout 30000ms exceeded. waiting for getByRole('button', { name: 'Show sidebar', exact: true }) at e2e/run.mjs:496:60 Seen on several unrelated PRs (agegr#1322, agegr#1324 ourselves, agegr#1308 from another contributor). The click now retries against a short deadline and only its own timeout is ignored; the wait for "Hide sidebar" that follows still fails if the sidebar never opens, so a real regression is not hidden. The fork's earlier 947c7fe reload fallback does not cover this signature: it fires only when neither toggle appears within its 10s bound.
…gr#1326) Returning from the mobile breakpoint restores the desktop sidebar preference in an effect, so the toggle can swap from "Show sidebar" to "Hide sidebar" between the `isVisible()` check and the click. The click then waits its full 30s for a button that will not come back, and the suite dies with locator.click: Timeout 30000ms exceeded. waiting for getByRole('button', { name: 'Show sidebar', exact: true }) at e2e/run.mjs:496:60 Seen on several unrelated PRs (agegr#1322, agegr#1324 ourselves, agegr#1308 from another contributor). The click now retries against a short deadline and only its own timeout is ignored; the wait for "Hide sidebar" that follows still fails if the sidebar never opens, so a real regression is not hidden. The fork's earlier 947c7fe reload fallback does not cover this signature: it fires only when neither toggle appears within its 10s bound.
…#1609) * fix(files): count untracked lines without blocking the server (#1584) /api/git/status read every untracked file with lstatSync/readFileSync in one synchronous pass. With 46k untracked files on Windows that held the server's event loop, and every chat stream and request with it, for 12 s. Untracked files are now read with async fs, 8 at a time, in 64 KiB chunks up to the size lstat reported, and the status entries are mapped in turns of 1000. Counts and response shape are unchanged: one line per newline byte plus an unterminated last line; empty, NUL-holding, over 256 KiB and non-regular files count 0. No cache, no shared scan: every request still reads afresh (#1322, #1350). Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(cli): count a busy server as running in status and stop (#1584) pi-web status, stop, open and update list a record only when its URL answers within 2 s. A server whose event loop a long request held took the connection but answered nothing in time, so status printed "No pi-web server is running." and stop did nothing. A closed port refuses at once, so a probe that times out now counts as a running server; a refused or failed connection still does not. The pid was already found alive and ours before the probe. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(cli): a timed-out probe counts only when a TCP connection opens (#1584) A fetch timeout also comes from a slow refusal (Windows retries a refused loopback connect for about 2 s), an address that drops packets or a stuck name lookup. Counting those as running could let `stop` kill a recycled pid's process tree. After a timeout, connect to the same address: only an accepted connection means a busy server. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(files): stop a git status scan its request left, read 4 files at a time (#1584) The explorer aborts a replaced status request, and the route passes the request's signal, so a long scan whose answer nobody waits for stops instead of piling up behind each tool-end refresh. Four reads at a time match libuv's default thread pool; more only queued ahead of the server's other fs work. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
…1584) (agegr#1609) * fix(files): count untracked lines without blocking the server (agegr#1584) /api/git/status read every untracked file with lstatSync/readFileSync in one synchronous pass. With 46k untracked files on Windows that held the server's event loop, and every chat stream and request with it, for 12 s. Untracked files are now read with async fs, 8 at a time, in 64 KiB chunks up to the size lstat reported, and the status entries are mapped in turns of 1000. Counts and response shape are unchanged: one line per newline byte plus an unterminated last line; empty, NUL-holding, over 256 KiB and non-regular files count 0. No cache, no shared scan: every request still reads afresh (agegr#1322, agegr#1350). Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(cli): count a busy server as running in status and stop (agegr#1584) pi-web status, stop, open and update list a record only when its URL answers within 2 s. A server whose event loop a long request held took the connection but answered nothing in time, so status printed "No pi-web server is running." and stop did nothing. A closed port refuses at once, so a probe that times out now counts as a running server; a refused or failed connection still does not. The pid was already found alive and ours before the probe. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(cli): a timed-out probe counts only when a TCP connection opens (agegr#1584) A fetch timeout also comes from a slow refusal (Windows retries a refused loopback connect for about 2 s), an address that drops packets or a stuck name lookup. Counting those as running could let `stop` kill a recycled pid's process tree. After a timeout, connect to the same address: only an accepted connection means a busy server. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(files): stop a git status scan its request left, read 4 files at a time (agegr#1584) The explorer aborts a replaced status request, and the route passes the request's signal, so a long scan whose answer nobody waits for stops instead of piling up behind each tool-end refresh. Four reads at a time match libuv's default thread pool; more only queued ahead of the server's other fs work. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]> (cherry picked from commit 2e8d3b1)
…1584) (agegr#1609) * fix(files): count untracked lines without blocking the server (agegr#1584) /api/git/status read every untracked file with lstatSync/readFileSync in one synchronous pass. With 46k untracked files on Windows that held the server's event loop, and every chat stream and request with it, for 12 s. Untracked files are now read with async fs, 8 at a time, in 64 KiB chunks up to the size lstat reported, and the status entries are mapped in turns of 1000. Counts and response shape are unchanged: one line per newline byte plus an unterminated last line; empty, NUL-holding, over 256 KiB and non-regular files count 0. No cache, no shared scan: every request still reads afresh (agegr#1322, agegr#1350). Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(cli): count a busy server as running in status and stop (agegr#1584) pi-web status, stop, open and update list a record only when its URL answers within 2 s. A server whose event loop a long request held took the connection but answered nothing in time, so status printed "No pi-web server is running." and stop did nothing. A closed port refuses at once, so a probe that times out now counts as a running server; a refused or failed connection still does not. The pid was already found alive and ours before the probe. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(cli): a timed-out probe counts only when a TCP connection opens (agegr#1584) A fetch timeout also comes from a slow refusal (Windows retries a refused loopback connect for about 2 s), an address that drops packets or a stuck name lookup. Counting those as running could let `stop` kill a recycled pid's process tree. After a timeout, connect to the same address: only an accepted connection means a busy server. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix(files): stop a git status scan its request left, read 4 files at a time (agegr#1584) The explorer aborts a replaced status request, and the route passes the request's signal, so a long scan whose answer nobody waits for stops instead of piling up behind each tool-end refresh. Four reads at a time match libuv's default thread pool; more only queued ahead of the server's other fs work. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
What it does
GET /api/git/statusruns a full working-tree scan on every request (getGitStatus()inlib/git-changes.ts, which reads every untracked file one by one to count its lines). Nothing was shared between a sidebar reload and the next, or between two tabs on the same project — and the sidebar refetches the listing on every reload trigger, at most once a second while a run's tools finish.This puts a short per-cwd cache in front of that scan, following
lib/models-cache.tsexactly:lib/git-status-cache.ts: a 2 s per-cwd TTL, plus an in-flight scan shared by concurrent requests for the same cwd./api/files ?type=upload), so the explorer's refetch after one is not the listing cached before the write.The response shape is unchanged, and the first request still scans.
Why it matters
A sidebar reload, a second tab, and the periodic tool-end refetch all hit the same project within a couple of seconds. On a repository with a few hundred untracked files the scan is the dominant cost of the route, and it was paid in full every time — in parallel, per request. Caching the listing collapses a burst to one scan.
Measurements
Measured by timing the scan directly, because the route handler is a thin
NextResponse.json(await …)wrapper around it and the allow-list checks are unchanged. A synthetic repository is built once, then both forms are timed in the same process against it:origin/mainroute =getGitStatus(cwd)on every request (no cache)loadGitStatusWithCache(cwd, () => getGitStatus(cwd))Repo shape: 300 tracked files, 100 of them modified, 400 untracked files of 300 lines each.
Reproduction (run from the repo root):
A warm request inside the TTL is a cache hit (~0 ms); eight concurrent requests for one cwd collapse from ~660 ms to one real scan (~115 ms), a ~5.7× reduction. The first request of a burst still pays the full scan; the win is everything behind it.
Scope
One change split out of a Bun-running fork (
brynne8/pi-web-bun) into a single-purpose branch on today'smain; one feature per PR. The fork's file-row "Revert" and itslib/git-revert.tsare not part of this change and are not present upstream, so the revert invalidation was dropped; only the upload invalidation is ported.Testing
origin/main:tests 2778, pass 2777, fail 0, skipped 1tests 2785, pass 2784, fail 0, skipped 1— the +7 are the new tests, all passing.bun teston the touched files:bun x tsc --noEmitclean,bun run lintclean.At least one new test fails on
origin/mainand passes here. Withlib/git-status-cache.tspresent but the route left unwired onorigin/main,app/api/git/status/route.test.mjsfails behaviourally (reuses one scan inside the TTL…: the file added inside the TTL still shows up, because the base route rescans on every request); the same file passes on this branch.lib/git-status-cache.test.mjspins the TTL, the shared in-flight scan, the expired-entry-not-served rule, a scan that finishes after invalidation, and a failed scan retrying.Note: on
origin/main, a full-suitebun testis not green for either the base or this branch — the same unrelated files fail under Bun on both (verified, e.g.hooks/useAudio.test.mjs0 pass/13 fail andlib/session-list-scanner.test.mjs8 pass/6 fail onorigin/mainand on this branch). Those are upstream-main-under-Bun issues outside this change; upstream's own runner (npm test) is fully green as above.Provenance
Source commit:
db8af8ainbrynne8/pi-web-bun— "perf(git): cache the status listing and share one scan between requests".