Skip to content

The agent that grows with you

fix(tui): follow a chat into the worktree its agent settled in

ethernet65cfee332a8f
diff --git a/tests/tui_gateway/test_session_cwd_follow.py b/tests/tui_gateway/test_session_cwd_follow.py
index 1c1719026ce..bf933a1a97a 100644
--- a/tests/tui_gateway/test_session_cwd_follow.py
+++ b/tests/tui_gateway/test_session_cwd_follow.py
@@ -140,12 +140,28 @@ def test_an_unrelated_repo_is_not_a_move(session, repo_with_worktree, tmp_path):
assert session["cwd"] == str(repo)
-def test_an_explicit_workspace_is_never_overridden(session, repo_with_worktree):
- """A user-chosen cwd must survive even a legitimate same-repo worktree move."""
+def test_a_session_created_in_a_checkout_still_follows_its_agent(session, repo_with_worktree):
+ """The reported bug: an agent told to make a worktree and work in it left the chat on `main`.
+
+ ``session.create`` marks `explicit_cwd` for ANY session whose cwd exists on disk, so every
+ desktop session starts that way — a cwd the chat merely STARTED in is not a pin.
+ """
repo, worktree = repo_with_worktree
session["explicit_cwd"] = True
terminal_tool.record_session_cwd(session["session_key"], str(worktree))
+ assert server._reconcile_session_cwd_from_terminal(session) is True
+ assert session["cwd"] == str(worktree)
+
+
+def test_a_deliberately_moved_workspace_is_never_overridden(session, repo_with_worktree):
+ """A workspace the chat was MOVED to (folder picker / project switch) survives a same-repo
+ worktree settle: only another deliberate move re-homes it."""
+ repo, worktree = repo_with_worktree
+ server._set_session_cwd(session, str(repo)) # what session.cwd.set / workspace.move do
+ assert session["cwd_pinned"] is True
+ terminal_tool.record_session_cwd(session["session_key"], str(worktree))
+
assert server._reconcile_session_cwd_from_terminal(session) is False
assert session["cwd"] == str(repo)
diff --git a/tui_gateway/session_workdir.py b/tui_gateway/session_workdir.py
index 6ccc251dab2..b53a9b836fb 100644
--- a/tui_gateway/session_workdir.py
+++ b/tui_gateway/session_workdir.py
@@ -284,11 +284,16 @@ def _reconcile_session_cwd_from_terminal(session: dict | None) -> bool:
"""Re-anchor a session that SETTLED in another worktree of the SAME repo. Returns moved. An agent told to work in
a fresh worktree `git worktree add`s and `cd`s in while the session stays pinned (labelled with the primary
checkout's branch). A plain `cd` is deliberately NOT a workspace move (see ``_apply_project_workspace``): a non-git
- workspace stepping into a repo or a visit to an unrelated repo is browsing, and an explicitly chosen workspace is
- never overridden. Local backends only (a remote cwd cannot be stat'ed or git-probed here)."""
- # An explicit choice only moves by another explicit action; a cwd adopted HERE is marked `cwd_from_settle` so
- # successive settles keep following.
- if not session or (session.get("explicit_cwd") and not session.get("cwd_from_settle")):
+ workspace stepping into a repo or a visit to an unrelated repo is browsing, and a workspace the user deliberately
+ moved the chat into is never overridden. Local backends only (a remote cwd cannot be stat'ed or git-probed
+ here)."""
+ # A workspace the USER put the chat in (the composer's folder picker -> session.cwd.set, the sidebar's
+ # move-to-project -> session.workspace.move) only moves by another deliberate action; a cwd adopted HERE is
+ # marked `cwd_from_settle` so successive settles keep following. NOT `explicit_cwd`: session.create sets that for
+ # ANY session whose cwd exists on disk, so keying the pin on it made every desktop session — all of which are
+ # created with a workspace — unfollowable, which is exactly the agent-made-a-worktree case this reconcile exists
+ # for.
+ if not session or (session.get("cwd_pinned") and not session.get("cwd_from_settle")):
return False
if not _session_is_local_backend(session):
return False
@@ -729,8 +734,9 @@ def _set_session_cwd(session: dict, cwd: str) -> str:
from hermes_constants import translate_cwd_for_wsl_backend
cwd = translate_cwd_for_wsl_backend(str(cwd))
resolved = _workspace_cwd(session.get("profile_home"), cwd)
- # An explicit user choice: persisted as the workspace (not the launch-dir fallback), superseding a settle-adopted cwd.
- session.update(cwd=resolved, explicit_cwd=True, cwd_from_settle=False)
+ # An explicit user choice: persisted as the workspace (not the launch-dir fallback), superseding a settle-adopted
+ # cwd — and PINNED, so the settle reconcile cannot drag it to another worktree the agent merely visited.
+ session.update(cwd=resolved, explicit_cwd=True, cwd_pinned=True, cwd_from_settle=False)
_register_session_cwd(session)
# The synchronous DB write claims ordering authority; git probes may publish only for that exact generation.
_persist_session_cwd_and_schedule_git_meta(session, resolved)
diff --git a/website/docs/user-guide/desktop.md b/website/docs/user-guide/desktop.md
index 7e19d4324d4..ca9030d2206 100644
--- a/website/docs/user-guide/desktop.md
+++ b/website/docs/user-guide/desktop.md
@@ -168,7 +168,7 @@ While delegated workers are live, a **Subagents** frame appears above the compos
For sessions running inside a Git repository, the app has a built-in source-control surface:
- **Review pane** — **Cmd/Ctrl+G** toggles the working-tree review pane: branch and ahead/behind status, changed files (list or tree view), and diffs scoped to **Uncommitted**, **Branch**, or **Last turn** (just what the agent changed in its most recent turn). Stage/unstage files, revert changes, write a commit message (or **Generate commit message**), then **Commit** or **Commit & Push** — and **Create PR** via the GitHub CLI (`gh`), or hand the whole thing to the agent with **Ask Hermes to open PR**. You can also create and switch branches from here.
-- **Worktrees** — **Cmd/Ctrl+Shift+B** (or **New worktree** on a project in the sidebar) creates a Git worktree on a new branch so an agent can work on a parallel copy of the repo without touching your checkout. Worktrees show up as their own lanes under the project; removing one offers to delete the worktree directory (the branch stays) or just hide the lane and leave it on disk, with a force option when it has uncommitted changes.
+- **Worktrees** — **Cmd/Ctrl+Shift+B** (or **New worktree** on a project in the sidebar) creates a Git worktree on a new branch so an agent can work on a parallel copy of the repo without touching your checkout. Worktrees show up as their own lanes under the project; removing one offers to delete the worktree directory (the branch stays) or just hide the lane and leave it on disk, with a force option when it has uncommitted changes. A chat also **follows the agent**: when you ask it to make a worktree and work in it, the chat moves to that lane at the end of the turn and the sidebar re-scopes with it. Only a workspace you deliberately switched the chat to (the folder picker, or a project switch) stays put.
### Memory Graph