Repository navigation
Conversation
`GET /api/git/status` reads every untracked file on every request, only to
count its lines. This keeps that count per file for as long as the file is
provably the same one it was: size, modification time, change time and
inode must all match, and the file must have been quiet for at least two
seconds, so a change to it after that point carries a later change time and
is seen. The lines themselves are counted from the bytes rather than from a
decoded string.
This is not a cache of the scan. The listing is still read from Git on every
request, and every request still produces the same answer as before: the
same files, the same line counts, the same additions and deletions. It only
avoids re-reading a file whose bytes cannot have changed.
Why: the maintainer of this repository closed an earlier pull request that
put a two-second cache in front of the whole scan, because the sidebar's
reload triggers land inside that window while files are changing, and a
stale badge is worse than a slow scan. In closing it they wrote:
"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
getGitStatus() itself cheaper (for example how untracked files' lines are
counted), with timings from that repo."
This is that change, with timings.
Measured on the same machine, on the same repositories, in one process, with
origin/main's getGitStatus() and this one timed against each other (21
calls each, median):
300 tracked / 100 changed / 400 untracked, 32 KB each
before 55.6 ms cold, 41.6 ms median after 47.3 ms cold, 32.5 ms median
300 tracked / 100 changed / 400 untracked, 4 KB each
before 35.7 ms cold, 30.5 ms median after 31.1 ms cold, 25.5 ms median
1200 tracked / 300 changed / 200 untracked, 32 KB each
before 120.0 ms cold, 68.4 ms median after 74.5 ms cold, 63.9 ms median
a checkout of this repository, clean
before 21.7 ms cold, 18.5 ms median after 18.9 ms cold, 18.8 ms median
a checkout of this repository with 400 untracked files in it
before 79.6 ms cold, 29.8 ms median after 38.9 ms cold, 20.5 ms median
Git's own listing dominates the total, so the request itself is 7-67% faster
where there are untracked files to read, and the part that reads them is 5-30x
cheaper. A clean checkout has no untracked files to read and is unchanged, as
it should be.
Tested with Node 26.7.0: `npm test` (2802 tests, 0 failures, 1 skipped),
`npx tsc --noEmit` and `npx eslint .` clean. The new tests in
`lib/git-changes.test.mjs` pin that a changed file is counted again, that a
file whose modification time is put back is counted again all the same, and
that an empty, binary or oversized file holds no lines.
Owner
|
Thanks @brynne8 for following up on #1322, and for keeping the listing uncached this time. I asked for timings from a real repo where the scan is slow, so I measured that before deciding, and I'm going to close this one. I loaded
So the trade is the same as in #1322: a process-wide cache with an edge case where the |
agegr
added a commit
that referenced
this pull request
Nov 12, 2026
…#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]>
Hwwwww-dev
pushed a commit
to Hwwwww-dev/pi-web
that referenced
this pull request
Nov 19, 2026
…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)
windli2018
pushed a commit
to aiti-ai/aiti-ai
that referenced
this pull request
Nov 24, 2026
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What it does
GET /api/git/statusruns a full working-tree scan on every request:getGitStatus()inlib/git-changes.tsasks Git for the listing, and then reads every untracked file one by one, only to count its lines.This makes that count cheap to keep. A file's line count is remembered for that file only, and only for as long as the file is provably the same one it was:
\n), not from a decoded string.Two things are deliberately not here: the listing is not cached — Git is asked on every request, exactly as before — and there is no expiry window on an answer. Every request produces the same listing, the same line counts and the same
additions/deletionsas before; the only thing this avoids is re-reading a file whose bytes cannot have changed.The count is also taken from the bytes rather than from
toString("utf8") + split("\n"): it does not build a string and an array of lines for every file, and its cost does not depend on how the lines are shaped. The native split is about twice as fast on a file of ordinary lines, and about eight times slower on a file of very short ones — the fourth row below.Why it matters
When the previous pull request was closed, the maintainer left an invitation:
This is that change. It answers the question that was asked — how the lines of an untracked file are counted — and it does it without touching the listing, so none of the staleness that closed #1322 can happen here.
The semantics are pinned by the file's own metadata rather than by a timer:
Measurements
Timed on the same machine, on the same repositories, in one process:
origin/main'sgetGitStatus()and this branch's, against each other, 21 calls each, median reported.Git's own listing dominates the total (30 ms of the 41 ms in the first row, 63 ms of the 68 ms in the third), so the request is 22 %, 16 % and 7 % faster in the first three rows, 67 % faster on the short-lines repository and 31 % faster on a real checkout with untracked files in it. The part that reads the files is 5–30× cheaper (10.8 ms → 1.7 ms in the first row, 68.5 ms → 2.0 ms in the fourth). A clean checkout has no untracked files to read, and is unchanged, as it should be.
Both branches return the same
additions,deletionsand file list on every repository above — that is checked as part of the measurement, not just asserted."Cold" is the first call on that repository. The process is warmed up on a scratch repository first, so the number is the cost of the scan and not of the first Git call in the process.
Reproduction (run from the repo root, with
origin/main'slib/copied to/tmp/before/):The script's "cold" is the very first call in the process, Git's own first call included, which is why it is higher than the table's.
Scope
One change, two files:
lib/git-changes.tsandlib/git-changes.test.mjs. Nothing else in the repository is touched, and no other optimisation is folded in.To be explicit, because it is what the previous attempt was closed for:
git status --porcelainstill runs on every request, and the file list, its statuses and the trackedadditions/deletionscome from Git every time;Testing
origin/main:tests 2799, pass 2798, fail 0, skipped 1tests 2802, pass 2801, fail 0, skipped 1— the +3 are the new tests, all passing.npx tsc --noEmitclean,npx eslint .clean.The three new tests in
lib/git-changes.test.mjspin the semantics that matter:Provenance
One feature per pull request, split out of a Bun-running fork (
brynne8/pi-web-bun) into a single-purpose branch on today'smain. The fork's owngit statuscache is not part of this change and is not present here — this branch is only the line counting.