Skip to content

The agent that grows with you

fix(project): report the calling session's project from desktop_project list

brooklyn!a932f22c7c8b
diff --git a/tests/tools/test_project_tools.py b/tests/tools/test_project_tools.py
index 2f8b47f36cd..a0bfdedc277 100644
--- a/tests/tools/test_project_tools.py
+++ b/tests/tools/test_project_tools.py
@@ -2,8 +2,11 @@ from __future__ import annotations
import json
+import pytest
+
from hermes_cli import projects_db as pdb
-from tools import project_tools
+from tools import project_tools, terminal_tool
+from tools.registry import registry
def _use_projects_db(monkeypatch, tmp_path):
@@ -95,3 +98,97 @@ def test_agent_session_project_switch_keeps_profile_active_pointer(monkeypatch,
assert result["success"] is True
assert result["id"] == background_id
assert workspace_moves == [("session-background", "/work/background", "Background")]
+
+
+def gui_sessions(monkeypatch):
+ """Live GUI sessions wired through the real gateway workspace callback."""
+ monkeypatch.setattr("hermes_cli.banner.prefetch_update_check", lambda: None)
+ from tui_gateway import server
+
+ monkeypatch.setattr(server, "_sessions", {})
+ monkeypatch.setattr(terminal_tool, "_task_env_overrides", {})
+ monkeypatch.setattr(server, "_emit", lambda *a, **k: None)
+ monkeypatch.setattr(server, "_persist_session_cwd_and_schedule_git_meta", lambda *a, **k: None)
+ project_tools.set_project_workspace_callback(server._apply_project_workspace)
+
+ def open_session(key, cwd=""):
+ session = {"session_key": key, "cwd": cwd, "source": "desktop"}
+ server._sessions[f"sid-{key}"] = session
+ server._register_session_cwd(session)
+ return session
+
+ try:
+ yield open_session
+ finally:
+ project_tools.set_project_workspace_callback(None)
+
+
+def _tool(action, task_id=None, **args):
+ return json.loads(registry.dispatch("desktop_project", {"action": action, **args}, task_id=task_id))
+
+
+def test_two_sessions_switch_projects_independently(gui_sessions, tmp_path):
+ folders = {name: tmp_path / name for name in ("foreground", "a", "b")}
+ for folder in folders.values():
+ folder.mkdir()
+ with pdb.connect_closing() as conn:
+ foreground = pdb.create_project(conn, name="Foreground", folders=[str(folders["foreground"])])
+ pdb.set_active(conn, foreground)
+ tab_a = gui_sessions("tab-a", str(folders["foreground"]))
+ tab_b = gui_sessions("tab-b", str(folders["foreground"]))
+
+ project_a = _tool("create", "tab-a", name="Project A", path=str(folders["a"]))["id"]
+ project_b = _tool("create", "tab-b", name="Project B", path=str(folders["b"]))["id"]
+
+ # Each tab's workspace moved; the profile-global pointer the sidebar owns did not.
+ assert (tab_a["cwd"], tab_b["cwd"]) == (str(folders["a"]), str(folders["b"]))
+ with pdb.connect_closing() as conn:
+ assert pdb.get_active_id(conn) == foreground
+ listed_a, listed_b = _tool("list", "tab-a"), _tool("list", "tab-b")
+ assert listed_a["active_id"] == project_a
+ assert listed_b["active_id"] == project_b
+ assert [p["id"] for p in listed_a["projects"] if p["active"]] == [project_a]
+
+ # Tab B switching into A's project leaves tab A (and the global pointer) where they were.
+ assert _tool("switch", "tab-b", name="Project A")["id"] == project_a
+ assert _tool("list", "tab-b")["active_id"] == project_a
+ assert _tool("list", "tab-a")["active_id"] == project_a
+ assert _tool("switch", "tab-a", name="Foreground")["id"] == foreground
+ assert _tool("list", "tab-a")["active_id"] == foreground
+ assert _tool("list", "tab-b")["active_id"] == project_a
+ with pdb.connect_closing() as conn:
+ assert pdb.get_active_id(conn) == foreground
+
+
+def test_project_list_reports_no_project_for_a_detached_session(gui_sessions, tmp_path):
+ with pdb.connect_closing() as conn:
+ active = pdb.create_project(conn, name="Sidebar pick", folders=[str(tmp_path)])
+ pdb.set_active(conn, active)
+ gui_sessions("detached")
+
+ listed = _tool("list", "detached")
+
+ assert listed["active_id"] is None
+ assert not any(p["active"] for p in listed["projects"])
+ # A caller the gateway never registered keeps reading the profile-global pointer.
+ assert _tool("list")["active_id"] == active
+
+
+def test_idempotent_create_from_a_session_keeps_profile_active_pointer(gui_sessions, tmp_path):
+ folders = {name: tmp_path / name for name in ("foreground", "existing")}
+ for folder in folders.values():
+ folder.mkdir()
+ with pdb.connect_closing() as conn:
+ foreground = pdb.create_project(conn, name="Foreground", folders=[str(folders["foreground"])])
+ existing = pdb.create_project(
+ conn, name="Existing", folders=[str(folders["existing"])], primary_path=str(folders["existing"]))
+ pdb.set_active(conn, foreground)
+ tab = gui_sessions("tab", str(folders["foreground"]))
+
+ result = _tool("create", "tab", name="Again", path=str(folders["existing"]))
+
+ assert result["id"] == existing
+ assert tab["cwd"] == str(folders["existing"])
+ with pdb.connect_closing() as conn:
+ assert pdb.get_active_id(conn) == foreground
diff --git a/tools/project_tools.py b/tools/project_tools.py
index 09192024eb2..a05358b618c 100644
--- a/tools/project_tools.py
+++ b/tools/project_tools.py
@@ -66,10 +66,27 @@ def _activated(proj, task_id: Optional[str]) -> str:
"primary_path": primary})
+def _calling_session_project_id(conn, task_id: Optional[str]) -> tuple[bool, Optional[str]]:
+ """``(scoped, project_id)`` for the calling session. The GUI gateway registers each session's
+ workspace (``cwd_source``) in the terminal override table; a caller it never registered (CLI,
+ scripts) is unscoped and falls back to the profile-global pointer."""
+ from hermes_cli import projects_db as pdb
+ from tools.terminal_tool import resolve_task_overrides
+ overrides = resolve_task_overrides(task_id) if task_id else {}
+ if "cwd_source" not in overrides:
+ return False, None
+ cwd = overrides.get("cwd") if overrides["cwd_source"] == "session" else None
+ project = pdb.project_for_path(conn, cwd) if cwd else None
+ return True, project.id if project else None
+
+
def project_list(task_id: Optional[str] = None) -> str:
from hermes_cli import projects_db as pdb
with pdb.connect_closing() as conn:
- active = pdb.get_active_id(conn)
+ # Another tab's switch moves the profile-global pointer; this chat's project is its own cwd.
+ scoped, active = _calling_session_project_id(conn, task_id)
+ if not scoped:
+ active = pdb.get_active_id(conn)
projects = pdb.list_projects(conn)
return json.dumps({
"active_id": active,
@@ -92,19 +109,16 @@ def project_create(name: str, path: Optional[str] = None, task_id: Optional[str]
with pdb.connect_closing() as conn:
existing = pdb.find_by_primary_path(conn, folder) if folder else None
if existing is not None:
- # Idempotent create: duplicates would render N identical sidebar subtrees.
- # Idempotent create: the folder already belongs to a project. Re-activating it beats minting
+ # Idempotent create: the folder already belongs to a project. Reusing it beats minting
# a duplicate — duplicated projects render N identical sidebar subtrees (#75820).
- pdb.set_active(conn, existing.id)
proj = existing
else:
pid = pdb.create_project(conn, name=name, folders=[folder] if folder else [], primary_path=folder or None)
- # A live agent session owns its workspace independently. Creating a
- # project must not let a background chat redirect the profile-global
- # Desktop selection used by unrelated and newly created sessions.
- if not task_id:
- pdb.set_active(conn, pid)
proj = pdb.get_project(conn, pid)
+ # A live session owns its workspace; only an unscoped caller moves the profile-global
+ # pointer, or a background chat would redirect the sidebar and every new session.
+ if not task_id and proj is not None:
+ pdb.set_active(conn, proj.id)
except ValueError as exc:
return json.dumps({"success": False, "error": str(exc)})
if proj is None:
@@ -118,9 +132,7 @@ def project_switch(project: str, task_id: Optional[str] = None) -> str:
proj = _resolve(conn, project)
if proj is None:
return json.dumps({"success": False, "error": f"no project matching '{project}'"})
- # A live agent session owns its workspace independently. Mutating the
- # profile-global pointer here lets a background chat redirect unrelated
- # Desktop windows and newly created sessions into this project.
+ # Same rule as create: a live session never moves the profile-global pointer.
if not task_id:
pdb.set_active(conn, proj.id)
return _activated(proj, task_id)
@@ -153,7 +165,7 @@ registry.register(
"this chat into it — pass path to anchor it to a repo/folder (the "
"chat's workspace moves there, the sidebar follows). switch: move "
"this chat into an existing project by name/slug/id — the "
- "intentional way to move the session, not `cd`. list: all projects + which is active."
+ "intentional way to move the session, not `cd`. list: all projects + which one this chat is in."
),
"parameters": {
"type": "object",