perf(gateway): stop spawning git for paths that cannot answer

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.
This commit is contained in:
Brooklyn Nicholson
2026-08-09 06:53:29 -05:00
parent 28e401c30b
commit e3836efc5f
2 changed files with 44 additions and 1 deletions

View File

@@ -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"

View File

@@ -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: