Skip to content

The agent that grows with you

fix(api): persist session pins instead of 400ing them

Brooklyn Nicholsoncef7d1a1e18e
diff --git a/gateway/platforms/api_server.py b/gateway/platforms/api_server.py
index a756134d99c..0f0dff5fbbc 100644
--- a/gateway/platforms/api_server.py
+++ b/gateway/platforms/api_server.py
@@ -3205,9 +3205,13 @@ class APIServerAdapter(BasePlatformAdapter):
"output_tokens", "cache_read_tokens", "cache_write_tokens",
"reasoning_tokens", "estimated_cost_usd", "actual_cost_usd",
"api_call_count", "parent_session_id", "last_active", "preview",
- "_lineage_root_id",
+ "_lineage_root_id", "pinned", "archived",
)
payload = {key: session.get(key) for key in safe_keys if key in session}
+ # SQLite stores these as 0/1; clients reconcile against a real boolean.
+ for flag in ("pinned", "archived"):
+ if flag in payload:
+ payload[flag] = bool(payload[flag])
# Avoid exposing full system prompts/model_config through the client API;
# callers only need to know whether those snapshots exist.
payload["has_system_prompt"] = bool(session.get("system_prompt"))
@@ -3417,17 +3421,29 @@ class APIServerAdapter(BasePlatformAdapter):
body, err = await self._read_json_body(request)
if err:
return err
- allowed = {"title", "end_reason"}
+ # `pinned` and `archived` are durable per-session flags the desktop
+ # sidebar owns (the "keep" flag exempts a chat from the auto-archive
+ # sweep). Rejecting them here was silently 400ing every pin the desktop
+ # made, so pins only ever lived in that one app's localStorage.
+ allowed = {"title", "end_reason", "pinned", "archived"}
unknown = sorted(set(body) - allowed)
if unknown:
return web.json_response(_openai_error(f"Unsupported session fields: {', '.join(unknown)}", code="unsupported_session_field"), status=400)
+ for flag in ("pinned", "archived"):
+ if flag in body and not isinstance(body[flag], bool):
+ return web.json_response(_openai_error(f"'{flag}' must be a boolean", code="invalid_session_field"), status=400)
+
db = await self._ensure_session_db_async()
if "title" in body:
try:
await asyncio.to_thread(db.set_session_title, session_id, "" if body["title"] is None else str(body["title"]))
except ValueError as exc:
return web.json_response(_openai_error(str(exc), code="invalid_title"), status=400)
+ if "pinned" in body:
+ await asyncio.to_thread(db.set_session_pinned, session_id, body["pinned"])
+ if "archived" in body:
+ await asyncio.to_thread(db.set_session_archived, session_id, body["archived"])
if body.get("end_reason"):
await asyncio.to_thread(db.end_session, session_id, str(body["end_reason"]))
session = await asyncio.to_thread(db.get_session, session_id) or session
diff --git a/tests/gateway/test_session_api.py b/tests/gateway/test_session_api.py
index 686d8104597..05e42c7d1a9 100644
--- a/tests/gateway/test_session_api.py
+++ b/tests/gateway/test_session_api.py
@@ -608,3 +608,56 @@ async def test_require_model_lock_hard_fails_when_global_default_would_be_used(a
mock_run.assert_not_called()
+async def test_patch_session_persists_pinned_and_archived(adapter, session_db):
+ """PATCH must accept the durable pin/archive flags and round-trip them.
+
+ These were rejected as unsupported fields, so every pin the desktop made
+ 400'd silently (the client swallows the error) and the pin only ever lived
+ in that one app's localStorage. The auto-archive sweep reads
+ `sessions.pinned` server-side, so an unpersisted pin does not protect the
+ chat it was supposed to keep.
+ """
+ session_id = session_db.create_session("pin-session", "api_server")
+ app = _create_session_app(adapter)
+
+ async with TestClient(TestServer(app)) as cli:
+ resp = await cli.patch(f"/api/sessions/{session_id}", json={"pinned": True})
+ assert resp.status == 200, await resp.text()
+ assert (await resp.json())["session"]["pinned"] is True
+
+ # The flag is durable, not just echoed back from the request body.
+ assert bool(session_db.get_session(session_id)["pinned"]) is True
+
+ resp = await cli.get(f"/api/sessions/{session_id}")
+ assert (await resp.json())["session"]["pinned"] is True
+
+ resp = await cli.patch(f"/api/sessions/{session_id}", json={"pinned": False})
+ assert (await resp.json())["session"]["pinned"] is False
+ assert bool(session_db.get_session(session_id)["pinned"]) is False
+
+ resp = await cli.patch(f"/api/sessions/{session_id}", json={"archived": True})
+ assert (await resp.json())["session"]["archived"] is True
+ assert bool(session_db.get_session(session_id)["archived"]) is True
+
+
+async def test_patch_session_rejects_non_boolean_pinned(adapter, session_db):
+ session_id = session_db.create_session("pin-type-session", "api_server")
+ app = _create_session_app(adapter)
+
+ async with TestClient(TestServer(app)) as cli:
+ resp = await cli.patch(f"/api/sessions/{session_id}", json={"pinned": "yes"})
+ assert resp.status == 400, await resp.text()
+ assert (await resp.json())["error"]["code"] == "invalid_session_field"
+
+
+async def test_patch_session_still_rejects_unknown_fields(adapter, session_db):
+ session_id = session_db.create_session("unknown-field-session", "api_server")
+ app = _create_session_app(adapter)
+
+ async with TestClient(TestServer(app)) as cli:
+ resp = await cli.patch(f"/api/sessions/{session_id}", json={"nonsense": 1})
+ assert resp.status == 400, await resp.text()
+ assert (await resp.json())["error"]["code"] == "unsupported_session_field"