Skip to content

The agent that grows with you

fix(gateway): don't re-home a non-git session onto a repo it only visited

PRATHAMESH75a22d6516df23
diff --git a/tests/tui_gateway/test_session_cwd_follow.py b/tests/tui_gateway/test_session_cwd_follow.py
index 64dc4100edf..09177b472ef 100644
--- a/tests/tui_gateway/test_session_cwd_follow.py
+++ b/tests/tui_gateway/test_session_cwd_follow.py
@@ -29,7 +29,7 @@ def repo_with_worktree(tmp_path):
_git(repo, "init", "-b", "main")
_git(repo, "config", "user.email", "[email protected]")
_git(repo, "config", "user.name", "t")
- (repo / "README.md").write_text("hi\n")
+ (repo / "README.md").write_text("hi\n", encoding="utf-8")
_git(repo, "add", ".")
_git(repo, "commit", "-m", "init")
@@ -92,6 +92,32 @@ def test_browsing_outside_a_repo_is_not_a_move(session, repo_with_worktree, tmp_
assert session["cwd"] == str(repo)
+def test_a_non_git_workspace_is_not_hijacked_by_visiting_a_repo(
+ session, repo_with_worktree, tmp_path
+):
+ """A non-git workspace must not be re-homed when a tool call steps into a
+ git repo: browsing in to read a file or run a command is a visit, not a
+ relocation. Otherwise the first git directory the agent touches (e.g. its
+ own install checkout) hijacks a home-directory session.
+ """
+ repo, _ = repo_with_worktree
+ home = tmp_path / "home" # a plain, non-git workspace
+ home.mkdir()
+ session["cwd"] = str(home)
+ terminal_tool.record_session_cwd(session["session_key"], str(repo))
+
+ assert server._reconcile_session_cwd_from_terminal(session) is False
+ assert session["cwd"] == str(home)
+
+
+def test_a_deleted_directory_is_not_a_move(session, repo_with_worktree, tmp_path):
+ repo, _ = repo_with_worktree
+ terminal_tool.record_session_cwd(session["session_key"], str(tmp_path / "gone"))
+
+ assert server._reconcile_session_cwd_from_terminal(session) is False
+ assert session["cwd"] == str(repo)
+
+
def test_remote_backends_do_not_reanchor(session, repo_with_worktree, monkeypatch):
"""A remote cwd names a path on the host, not one this gateway can probe."""
repo, worktree = repo_with_worktree
diff --git a/tui_gateway/server.py b/tui_gateway/server.py
index a6074592b97..a7f2d4e5699 100644
--- a/tui_gateway/server.py
+++ b/tui_gateway/server.py
@@ -2373,7 +2373,14 @@ def _reconcile_session_cwd_from_terminal(session: dict | None) -> bool:
# The worktree ROOT, not the common repo root: folding worktrees together
# here is exactly what hides the move we're looking for.
landed = _git_repo_root_for_cwd(resolved)
- if not landed or landed == _git_repo_root_for_cwd(current):
+ current_root = _git_repo_root_for_cwd(current)
+ # A relocation is a move between two DIFFERENT git working trees. When the
+ # session's own workspace is not in a git repo, the agent stepping into one
+ # to read a file or run a command is a browsing visit, not a re-home:
+ # adopting it would hijack a non-git workspace onto whatever repo a tool
+ # call touched first (e.g. a home-directory session pinned to the checkout
+ # it read a file from).
+ if not landed or not current_root or landed == current_root:
return False
session["cwd"] = resolved