From a22d6516df23873247c2f89c586a8e512d79fa9f Mon Sep 17 00:00:00 2001 From: PRATHAMESH75 Date: Mon, 27 Jul 2026 23:09:05 +0530 Subject: [PATCH] fix(gateway): don't re-home a non-git session onto a repo it only visited MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `_reconcile_session_cwd_from_terminal()` treats a settled terminal cwd in a different git working tree than the session's workspace as a relocation. But when the session's workspace is not itself in a git repo, `_git_repo_root_for_cwd(current)` is None, so the `landed == _git_repo_root_for_cwd(current)` guard never matches and the FIRST git directory a tool call steps into hijacks the session: its cwd/git_repo_root flip to that repo, later tools run from the wrong place, and the repo's AGENTS.md gets injected into an unrelated conversation. Only reconcile when both the current and settled cwds resolve to valid, differing git roots. A non-git workspace visiting a git repo to read a file or run a command is a browsing visit, not a re-home — matching the docstring's own intent that `cd`-ing away must not re-home the chat. The intended "follow into a worktree" behavior is unaffected: that path starts from a git checkout, so current_root is valid there. Adds a regression test covering a non-git workspace that touches a git repo. --- tests/tui_gateway/test_session_cwd_follow.py | 28 +++++++++++++++++++- tui_gateway/server.py | 9 ++++++- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/tests/tui_gateway/test_session_cwd_follow.py b/tests/tui_gateway/test_session_cwd_follow.py index 64dc4100ed..09177b472e 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", "t@example.com") _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 a6074592b9..a7f2d4e569 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