Skip to content

The agent that grows with you

fix(projects): CLI and pathless switches move the active project again

brooklyn!f92886071cf8
diff --git a/tests/tools/test_project_tools.py b/tests/tools/test_project_tools.py
index a0bfdedc277..6ba618f93c5 100644
--- a/tests/tools/test_project_tools.py
+++ b/tests/tools/test_project_tools.py
@@ -100,6 +100,40 @@ def test_agent_session_project_switch_keeps_profile_active_pointer(monkeypatch,
assert workspace_moves == [("session-background", "/work/background", "Background")]
+def test_session_without_workspace_callback_switch_sets_profile_active_pointer(monkeypatch, tmp_path):
+ """CLI/messaging agents pass a task_id but have no GUI workspace to move — the pointer is the switch."""
+ db_path, connect_closing = _use_projects_db(monkeypatch, tmp_path)
+ with connect_closing(db_path=db_path) as conn:
+ active_id = pdb.create_project(conn, name="Active", folders=["/work/active"])
+ target_id = pdb.create_project(conn, name="Target", folders=["/work/target"])
+ pdb.set_active(conn, active_id)
+
+ result = json.loads(project_tools.project_switch(target_id, task_id="cli-session"))
+
+ with connect_closing(db_path=db_path) as conn:
+ assert pdb.get_active_id(conn) == target_id
+ assert result["success"] is True
+
+
+def test_session_switch_to_pathless_project_sets_profile_active_pointer(monkeypatch, tmp_path):
+ db_path, connect_closing = _use_projects_db(monkeypatch, tmp_path)
+ with connect_closing(db_path=db_path) as conn:
+ active_id = pdb.create_project(conn, name="Active", folders=["/work/active"])
+ pathless_id = pdb.create_project(conn, name="Pathless")
+ pdb.set_active(conn, active_id)
+
+ workspace_moves: list[tuple[str, str, str]] = []
+ project_tools.set_project_workspace_callback(lambda *args: workspace_moves.append(args))
+ try:
+ json.loads(project_tools.project_switch(pathless_id, task_id="session-a"))
+ finally:
+ project_tools.set_project_workspace_callback(None)
+
+ with connect_closing(db_path=db_path) as conn:
+ assert pdb.get_active_id(conn) == pathless_id
+ assert workspace_moves == []
+
+
@pytest.fixture
def gui_sessions(monkeypatch):
"""Live GUI sessions wired through the real gateway workspace callback."""
diff --git a/tools/project_tools.py b/tools/project_tools.py
index a05358b618c..9f0321b7613 100644
--- a/tools/project_tools.py
+++ b/tools/project_tools.py
@@ -32,6 +32,11 @@ def _primary_path(proj) -> Optional[str]:
return proj.folders[0].path if proj.folders else None
+def _moves_session(task_id: Optional[str], path: Optional[str]) -> bool:
+ """True when a live GUI session's workspace will follow the project — ``_apply_workspace``'s gate."""
+ return bool(_workspace_callback and task_id and path)
+
+
def _apply_workspace(task_id: Optional[str], path: Optional[str], name: str) -> None:
cb = _workspace_callback
if cb and task_id and path:
@@ -115,9 +120,10 @@ def project_create(name: str, path: Optional[str] = None, task_id: Optional[str]
else:
pid = pdb.create_project(conn, name=name, folders=[folder] if folder else [], primary_path=folder or None)
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:
+ # A live session that moves owns its workspace, so it leaves the profile-global pointer
+ # alone (a background chat would otherwise redirect the sidebar). Anything that can't
+ # move — CLI/messaging, a pathless project — switches through the pointer instead.
+ if proj is not None and not _moves_session(task_id, _primary_path(proj)):
pdb.set_active(conn, proj.id)
except ValueError as exc:
return json.dumps({"success": False, "error": str(exc)})
@@ -132,8 +138,8 @@ 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}'"})
- # Same rule as create: a live session never moves the profile-global pointer.
- if not task_id:
+ # Same rule as create.
+ if not _moves_session(task_id, _primary_path(proj)):
pdb.set_active(conn, proj.id)
return _activated(proj, task_id)