From e3836efc5fbcae31929057abec9d330a0e26ec07 Mon Sep 17 00:00:00 2001 From: Brooklyn Nicholson Date: Sun, 9 Aug 2026 06:53:29 -0500 Subject: [PATCH] perf(gateway): stop spawning git for paths that cannot answer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The project tree probes every distinct session cwd, and on a long-lived history most of those directories are deleted worktrees — `git -C` there can only fail, at the price of a fork each. Stat first. The second elision is `common_repo_root`: only repos have a common dir, and the parallel warm never covers that probe because `resolve()` reaches it only for cwds that already resolved. Every non-repo cwd was therefore paying a serial `git` spawn on the discovery pass. --- tests/tui_gateway/test_projects_rpc.py | 32 ++++++++++++++++++++++++++ tui_gateway/git_probe.py | 13 ++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/tests/tui_gateway/test_projects_rpc.py b/tests/tui_gateway/test_projects_rpc.py index e3dcf61a51..2c4ad82c65 100644 --- a/tests/tui_gateway/test_projects_rpc.py +++ b/tests/tui_gateway/test_projects_rpc.py @@ -146,6 +146,38 @@ def test_warm_roots_probes_in_parallel_and_fills_the_cache(monkeypatch): assert live["calls"] == before +def test_missing_directory_costs_no_subprocess(monkeypatch): + # Deleted worktrees dominate a long session history's cwds, and `git -C` on + # one can only fail — so it must never reach the fork. + from tui_gateway import git_probe + + def boom(*_a, **_kw): + raise AssertionError("spawned git for a directory that does not exist") + + monkeypatch.setattr(git_probe, "bounded_git_probe", boom) + + assert git_probe.run_git("/gone/worktree", "rev-parse", "--show-toplevel") == "" + + +def test_non_repo_cwd_is_not_probed_for_a_common_dir(monkeypatch, tmp_path): + # `warm_roots` only reaches `common_repo_root` for cwds that ARE repos, so a + # common-dir probe here is one the warm can't absorb: it runs serially on + # the discovery pass, once per non-repo cwd. + from tui_gateway import git_probe + + git_probe.invalidate() + asked = [] + + def probe(cwd, *args): + asked.append(args[-1]) + return "" # not a repo, whatever we ask + + monkeypatch.setattr(git_probe, "run_git", probe) + + assert git_probe.common_repo_root(str(tmp_path)) == "" + assert asked == ["--show-toplevel"] + + def test_create_list_roundtrip(tmp_path): created = _call("projects.create", {"name": "Demo", "folders": [str(tmp_path)], "use": True}) assert created["project"]["slug"] == "demo" diff --git a/tui_gateway/git_probe.py b/tui_gateway/git_probe.py index 96053d85cd..a0b5d13530 100644 --- a/tui_gateway/git_probe.py +++ b/tui_gateway/git_probe.py @@ -50,7 +50,11 @@ def run_git(cwd: str, *args: str) -> str: session readiness when a killed git left a suspended descendant holding the pipe handles (issue #68609). """ - if not cwd: + if not cwd or not os.path.isdir(cwd): + # `git -C` on a directory that no longer exists can only fail, and it + # fails at the price of a fork. Deleted worktrees dominate the cwds a + # long-lived session history hands us, so the stat pays for itself many + # times over on every project-tree build. return "" return bounded_git_probe(["git", "-C", cwd, *args], timeout=_GIT_TIMEOUT) @@ -153,6 +157,13 @@ def common_repo_root(cwd: str) -> str: if not cwd: return "" + # No work tree, nothing to fold. Reading the (warmed, negative-cached) + # toplevel first spares every non-repo cwd a second `git` spawn — one the + # parallel warm can never absorb, since `resolve()` only reaches here for + # cwds that ARE repos. + if not repo_root(cwd): + return "" + def _probe() -> str: gitdir = run_git(cwd, "rev-parse", "--path-format=absolute", "--git-common-dir") if gitdir: