The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(project): keep task-scoped create/switch off the profile-global pointer
Börje0c7c8b69631e
diff --git a/tests/tools/test_project_tools.py b/tests/tools/test_project_tools.pynew file mode 100644index 00000000000..2f8b47f36cd--- /dev/null+++ b/tests/tools/test_project_tools.py@@ -0,0 +1,97 @@+from __future__ import annotations++import json++from hermes_cli import projects_db as pdb+from tools import project_tools+++def _use_projects_db(monkeypatch, tmp_path):+ db_path = tmp_path / "projects.db"+ original_connect_closing = pdb.connect_closing+ monkeypatch.setattr(pdb, "connect_closing", lambda: original_connect_closing(db_path=db_path))+ return db_path, original_connect_closing+++def test_agent_session_project_create_keeps_profile_active_pointer_and_moves_session(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="Foreground", folders=["/work/foreground"])+ 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:+ result = json.loads(+ project_tools.project_create(+ "Background", path="/work/background", task_id="session-background"+ )+ )+ finally:+ project_tools.set_project_workspace_callback(None)++ with connect_closing(db_path=db_path) as conn:+ assert pdb.get_active_id(conn) == active_id+ created = pdb.get_project(conn, result["id"])++ assert result["success"] is True+ assert created is not None+ assert created.name == "Background"+ assert workspace_moves == [("session-background", "/work/background", "Background")]+++def test_legacy_project_create_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:+ previous_id = pdb.create_project(conn, name="Previous", folders=["/work/previous"])+ pdb.set_active(conn, previous_id)++ result = json.loads(project_tools.project_create("Created", path="/work/created"))++ with connect_closing(db_path=db_path) as conn:+ assert pdb.get_active_id(conn) == result["id"]++ assert result["success"] is True+++def test_legacy_project_switch_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"])+ 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))++ with connect_closing(db_path=db_path) as conn:+ assert pdb.get_active_id(conn) == target_id++ assert result["success"] is True+ assert result["id"] == target_id+++def test_agent_session_project_switch_keeps_profile_active_pointer(monkeypatch, tmp_path):+ db_path = tmp_path / "projects.db"+ original_connect_closing = pdb.connect_closing++ with original_connect_closing(db_path=db_path) as conn:+ active_id = pdb.create_project(conn, name="Foreground", folders=["/work/foreground"])+ background_id = pdb.create_project(conn, name="Background", folders=["/work/background"])+ pdb.set_active(conn, active_id)++ monkeypatch.setattr(pdb, "connect_closing", lambda: original_connect_closing(db_path=db_path))+ workspace_moves: list[tuple[str, str, str]] = []+ project_tools.set_project_workspace_callback(lambda *args: workspace_moves.append(args))++ try:+ result = json.loads(project_tools.project_switch(background_id, task_id="session-background"))+ finally:+ project_tools.set_project_workspace_callback(None)++ with original_connect_closing(db_path=db_path) as conn:+ assert pdb.get_active_id(conn) == active_id++ assert result["success"] is True+ assert result["id"] == background_id+ assert workspace_moves == [("session-background", "/work/background", "Background")]diff --git a/tools/project_tools.py b/tools/project_tools.pyindex c5925b92061..09192024eb2 100644--- a/tools/project_tools.py+++ b/tools/project_tools.py@@ -3,7 +3,9 @@``projects.db``, the desktop sidebar's named workspaces). Creating/switching is an explicittool call, never a side effect of ``cd``. GUI-only: the `project` toolset stays off``_HERMES_CORE_TOOLS``; the desktop/TUI gateway folds it in and wires-``set_project_workspace_callback`` so the live session's cwd and sidebar follow."""+``set_project_workspace_callback`` so the live session's cwd and sidebar follow. A live+session create/switch re-anchors only that session; it must not move the profile-global+Desktop selection shared by concurrent chats."""import jsonimport os@@ -97,7 +99,11 @@ def project_create(name: str, path: Optional[str] = None, task_id: Optional[str]proj = existingelse:pid = pdb.create_project(conn, name=name, folders=[folder] if folder else [], primary_path=folder or None)- pdb.set_active(conn, pid)+ # 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)except ValueError as exc:return json.dumps({"success": False, "error": str(exc)})@@ -112,7 +118,11 @@ 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}'"})- pdb.set_active(conn, proj.id)+ # 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.+ if not task_id:+ pdb.set_active(conn, proj.id)return _activated(proj, task_id)