Add unlink_project: the way out of the linked-session one-way door
Once a session linked to a project, the agent could neither create a fresh project (create_project is sandbox-only) nor switch (link_project is sandbox-only) — and no unlink existed. Observed live: a linked agent burned three failed tool calls and ~7 minutes of reasoning discovering the box, then overwrote the linked project as its only option. unlink_project (linked-only) clears the session binding and nothing else; the blind_only rejection message now points to it, so the model recovers in one step; link_project's description documents the switch path. (cherry picked from commit f4fc7cde6461529177563d58383c5c50287026e1)
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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():
|
||||
|
||||
@@ -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"]
|
||||
|
||||
Reference in New Issue
Block a user