Skip to content

The agent that grows with you

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

Brooklyn Nicholsone3836efc5fbc
diff --git a/tests/tui_gateway/test_projects_rpc.py b/tests/tui_gateway/test_projects_rpc.py
index e3dcf61a51e..2c4ad82c65c 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 96053d85cdb..a0b5d135306 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: