diff --git a/calliope-backend/src/calliope/agent/harness/plugins/workspace.py b/calliope-backend/src/calliope/agent/harness/plugins/workspace.py index f007f124..39f9ff8d 100644 --- a/calliope-backend/src/calliope/agent/harness/plugins/workspace.py +++ b/calliope-backend/src/calliope/agent/harness/plugins/workspace.py @@ -37,7 +37,8 @@ def register(registry: ToolRegistry) -> None: description=( "Link this sandbox session to an EXISTING project (call " "list_projects first to get valid ids). Only possible while the " - "session is not yet linked — do NOT create a duplicate project " + "session is not yet linked — to switch projects, call " + "unlink_project first. Do NOT create a duplicate project " "when one already matches." ), parameters={ @@ -56,6 +57,21 @@ def register(registry: ToolRegistry) -> None: category="workspace", ) ) + registry.register( + ToolDefinition( + name="unlink_project", + description=( + "Detach this session from its linked project and return to " + "sandbox mode. The project and all its content are untouched — " + "only the session binding is cleared. Use this when the user " + "wants a NEW project (then create_project) or a DIFFERENT " + "existing one (then link_project). Not available in sandbox." + ), + parameters={"type": "object", "properties": {}}, + executor=t_unlink_project, + category="workspace", + ) + ) registry.register( ToolDefinition( name="create_project", @@ -228,6 +244,29 @@ async def t_link_project(ctx: ToolContext, args: dict[str, Any]) -> dict[str, An return {"ok": True, "project_id": pid, "title": project["title"]} +async def t_unlink_project(ctx: ToolContext, args: dict[str, Any]) -> dict[str, Any]: + released_id = ctx.project_id + conn = _db() + try: + project = _project_or_error(conn, released_id) + conn.execute( + "UPDATE agent_sessions SET project_id = NULL, updated_at = CURRENT_TIMESTAMP WHERE id = ?", + (ctx.session_id,), + ) + conn.commit() + finally: + conn.close() + ctx.project_id = None + await publish_session_updated(ctx.session_id) + return { + "ok": True, + "released_project_id": released_id, + "released_title": (project or {}).get("title"), + "mode": "sandbox", + "hint": "Session is back in sandbox — create_project and link_project are available again.", + } + + _TITLE_MAX = 200 # mirrors ProjectCreate/ProjectUpdate schema bounds diff --git a/calliope-backend/src/calliope/agent/harness/registry.py b/calliope-backend/src/calliope/agent/harness/registry.py index 29974293..07e2a0de 100644 --- a/calliope-backend/src/calliope/agent/harness/registry.py +++ b/calliope-backend/src/calliope/agent/harness/registry.py @@ -161,7 +161,13 @@ class ToolRegistry: "error": "This tool requires a linked project. Create one with create_project first.", } if t.blind_only and ctx.project_id is not None: - return {"ok": False, "error": "This tool is only available in a sandbox (unlinked) session."} + return { + "ok": False, + "error": ( + "This tool is only available in a sandbox (unlinked) session. " + "Call unlink_project first to return this session to sandbox." + ), + } decision = ALLOW for hook in self.pre_execute: diff --git a/calliope-backend/tests/test_agent_harness.py b/calliope-backend/tests/test_agent_harness.py index 1265ed70..f36fcf54 100644 --- a/calliope-backend/tests/test_agent_harness.py +++ b/calliope-backend/tests/test_agent_harness.py @@ -66,6 +66,9 @@ def test_openai_payload_scoping(): assert "create_project" not in linked_names assert "link_project" not in linked_names assert "generate_story" in linked_names + # unlink_project is the way back: linked-only, hidden in sandbox + assert "unlink_project" in linked_names + assert "unlink_project" not in blind_names def test_execute_tool_unknown_and_unscoped(): diff --git a/calliope-backend/tests/test_agent_security.py b/calliope-backend/tests/test_agent_security.py index a1a09ad7..e9a81a45 100644 --- a/calliope-backend/tests/test_agent_security.py +++ b/calliope-backend/tests/test_agent_security.py @@ -1727,3 +1727,67 @@ def test_concurrent_start_turn_single_winner(client): results = aio.run(scenario()) assert sorted(results) == ["ok", "rejected"], results + + +# ───────────────────────────────────────────────────────────────────────── +# unlink_project: the way out of the linked-session one-way door +# (Observed live 2026-08-24, session 82: a linked agent burned three failed +# tool calls trying to create a fresh project — create_project and +# link_project are sandbox-only and no unlink existed.) +# ───────────────────────────────────────────────────────────────────────── + + +def test_unlink_project_round_trip(client): + from calliope.agent.harness.registry import ToolContext + from calliope.agent.harness.tools import execute_tool + + pid = _mk_project(client, "Original Film") + sid = _mk_session(project_id=pid) + ctx = ToolContext(session_id=sid, project_id=pid) + + # 1. Linked session: create_project is blocked, and the error now says how + # to get out. + out = asyncio.run(execute_tool(ctx, "create_project", {"title": "Fresh", "idea": "x"})) + assert out["ok"] is False + assert "unlink_project" in out["error"] + + # 2. unlink: binding cleared, project untouched. + out = asyncio.run(execute_tool(ctx, "unlink_project", {})) + assert out["ok"] is True + assert out["released_project_id"] == pid + assert ctx.project_id is None + conn = get_db(settings.db_path) + try: + row = conn.execute( + "SELECT project_id FROM agent_sessions WHERE id = ?", (sid,) + ).fetchone() + assert row["project_id"] is None + assert conn.execute( + "SELECT COUNT(*) AS n FROM projects WHERE id = ?", (pid,) + ).fetchone()["n"] == 1 + finally: + conn.close() + + # 3. Sandbox again: create_project now works and auto-links. + out = asyncio.run(execute_tool(ctx, "create_project", {"title": "Fresh", "idea": "x"})) + assert out["ok"] is True + assert ctx.project_id == out["project"]["id"] + assert ctx.project_id != pid + + # 4. And the session can come back to the original via unlink + link. + out = asyncio.run(execute_tool(ctx, "unlink_project", {})) + assert out["ok"] is True + out = asyncio.run(execute_tool(ctx, "link_project", {"project_id": pid})) + assert out["ok"] is True + assert ctx.project_id == pid + + +def test_unlink_project_requires_linked_session(client): + from calliope.agent.harness.registry import ToolContext + from calliope.agent.harness.tools import execute_tool + + sid = _mk_session(project_id=None) + ctx = ToolContext(session_id=sid, project_id=None) + out = asyncio.run(execute_tool(ctx, "unlink_project", {})) + assert out["ok"] is False + assert "requires a linked project" in out["error"]