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:
Justo Stoll
2026-08-26 21:24:57 +02:00
parent cfc13f4aee
commit d8fe088ce4
4 changed files with 114 additions and 2 deletions
@@ -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: