AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(platform): make chat session create idempotent so a lost response can't orphan a session (#15080)
Bentlyf05ee06393ee
diff --git a/autogpt_platform/backend/backend/api/features/chat/routes.py b/autogpt_platform/backend/backend/api/features/chat/routes.pyindex 9ce3a04fca..f100225b5c 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes.py@@ -5,7 +5,7 @@ import loggingfrom collections.abc import AsyncGeneratorfrom datetime import datetime, timezonefrom typing import Annotated, Any, Literal-from uuid import uuid4+from uuid import UUID, uuid4from autogpt_libs import authfrom fastapi import (@@ -58,10 +58,12 @@ from backend.copilot.model import (AutopilotMode,ChatSessionInfo,ChatSessionMetadata,+ SessionIdConflictError,create_chat_session,delete_chat_session,get_chat_session_metadata,get_or_create_builder_session,+ get_or_create_client_chat_session,get_or_create_expert_kickoff_session,get_user_sessions,update_session_autopilot_mode,@@ -482,6 +484,12 @@ class CreateSessionRequest(BaseModel):mode keyed by the authenticated user and expert. It is invalid without``expert_id``.+ ``session_id`` lets the client name the session up front, which makes+ the create idempotent: a retry after a lost response adopts the session+ the first attempt committed (409 if the id is someone else's or scoped+ to another expert). Builder and kickoff sessions pick their own ids, so+ it is rejected alongside them (422).+Extra/unknown fields are rejected (422) to prevent silent mis-use."""@@ -493,6 +501,7 @@ class CreateSessionRequest(BaseModel):llm_credential_id: str | None = Field(default=None, max_length=128)expert_id: str | None = Field(default=None, max_length=128)expert_kickoff: bool = False+ session_id: UUID | None = None# The approval mode the chat starts on. Leave unset to start on the# expert's default (or the platform default); send only a deliberate pick.autopilot_mode: AutopilotMode | None = None@@ -867,7 +876,7 @@ async def create_session() -> CreateSessionResponse:"""Create (or get-or-create) a chat session.- Two modes, selected by the request body:+ Four modes, selected by the request body:- Default: create a fresh session for the user. ``dry_run=True`` forcesrun_capability and run_agent calls to use dry-run simulation.@@ -881,11 +890,15 @@ async def create_session(with the panel's scope (see :data:`BUILDER_BLOCKED_TOOLS`).- Expert kickoff: atomically create or adopt the canonical first sessionfor ``(user_id, expert_id)``.+ - Client-named: when ``session_id`` is set, a plain or expert create+ adopts the session that id already names, so a retry after a lost+ response never leaves an empty session behind.Args:user_id: The authenticated user ID parsed from the JWT (required).request: Optional request body with ``dry_run``,- ``builder_graph_id`` and/or ``expert_id``.+ ``builder_graph_id``, ``expert_id``, ``expert_kickoff`` and/or+ ``session_id``.Returns:CreateSessionResponse: Details of the resulting session.@@ -895,6 +908,15 @@ async def create_session(expert_id = request.expert_id if request else Noneexpert_kickoff = request.expert_kickoff if request else Falseautopilot_mode = request.autopilot_mode if request else None+ client_session_id = (+ str(request.session_id) if request and request.session_id else None+ )++ if client_session_id and (builder_graph_id or expert_kickoff):+ raise HTTPException(+ status_code=422,+ detail="session_id cannot be combined with builder_graph_id or expert_kickoff",+ )if expert_kickoff and expert_id is None:raise HTTPException(@@ -957,16 +979,33 @@ async def create_session()else:try:- session = await create_chat_session(- user_id,- dry_run=dry_run,- organization_id=ctx.org_id,- team_id=ctx.team_id,- llm_auth_provider=llm_auth_provider,- llm_credential_id=llm_credential_id,- expert_id=expert_id,- autopilot_mode=autopilot_mode,- )+ if client_session_id:+ session = await get_or_create_client_chat_session(+ user_id,+ client_session_id,+ dry_run=dry_run,+ organization_id=ctx.org_id,+ team_id=ctx.team_id,+ llm_auth_provider=llm_auth_provider,+ llm_credential_id=llm_credential_id,+ expert_id=expert_id,+ autopilot_mode=autopilot_mode,+ )+ else:+ session = await create_chat_session(+ user_id,+ dry_run=dry_run,+ organization_id=ctx.org_id,+ team_id=ctx.team_id,+ llm_auth_provider=llm_auth_provider,+ llm_credential_id=llm_credential_id,+ expert_id=expert_id,+ autopilot_mode=autopilot_mode,+ )+ except SessionIdConflictError as e:+ raise HTTPException(+ status_code=409, detail="Session id already in use"+ ) from eexcept experts_db.ExpertNotFoundError as e:raise HTTPException(status_code=404, detail="Expert not found") from eexcept experts_db.ExpertPrivateTenancyNotFoundError as e:diff --git a/autogpt_platform/backend/backend/api/features/chat/routes_test.py b/autogpt_platform/backend/backend/api/features/chat/routes_test.pyindex 6b0b4ac03e..c56bca23e1 100644--- a/autogpt_platform/backend/backend/api/features/chat/routes_test.py+++ b/autogpt_platform/backend/backend/api/features/chat/routes_test.py@@ -38,7 +38,7 @@ from backend.integrations.oauth.microsoft_365_copilot import (Microsoft365CopilotDeviceAuthHandler,)from backend.integrations.providers import ProviderName-from backend.util.exceptions import NotFoundError+from backend.util.exceptions import DatabaseError, NotFoundErrorfrom backend.util.settings import BehaveAsapp = fastapi.FastAPI()@@ -5182,6 +5182,229 @@ def test_credential_selection_rejects_a_provider_named_twice(remember.assert_not_awaited()+# ─── Create session: client-named session_id ────────────────────────────+++def _in_memory_session_store(mocker: pytest_mock.MockerFixture):+ """Back the model's get/create with a dict so a replayed create sees the+ session the first request persisted, and a second insert of the same id+ fails the way the primary key would."""+ store: dict[str, ChatSession] = {}++ async def _get(session_id: str, user_id: str | None = None):+ session = store.get(session_id)+ if session is None or (user_id is not None and session.user_id != user_id):+ return None+ return session++ async def _create(user_id: str, *, dry_run: bool, session_id=None, **kwargs):+ if session_id in store:+ raise DatabaseError(f"Failed to create chat session {session_id}")+ session = ChatSession.new(+ user_id,+ dry_run=dry_run,+ session_id=session_id,+ organization_id=kwargs.get("organization_id"),+ team_id=kwargs.get("team_id"),+ expert_id=kwargs.get("expert_id"),+ )+ store[session.session_id] = session+ return session++ mocker.patch("backend.copilot.model.get_chat_session", side_effect=_get)+ create = mocker.patch(+ "backend.copilot.model.create_chat_session",+ new_callable=AsyncMock,+ side_effect=_create,+ )+ return store, create+++def test_create_session_replayed_with_the_same_session_id_returns_one_session(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ """A retry after the first response was lost (the phone saw "Load failed"+ while the server had already committed) adopts that session instead of+ leaving it empty behind a second one."""+ store, create = _in_memory_session_store(mocker)+ session_id = "0b8f3c1e-2d4a-4f6b-9c1d-3e5f7a9b1c2d"++ first = client.post("/sessions", json={"session_id": session_id})+ second = client.post("/sessions", json={"session_id": session_id})++ assert first.status_code == 200+ assert second.status_code == 200+ assert first.json()["id"] == session_id+ assert second.json()["id"] == session_id+ assert list(store) == [session_id]+ create.assert_awaited_once()+++def test_create_session_replay_keeps_the_expert_scope(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ _store, create = _in_memory_session_store(mocker)+ _mock_get_expert(mocker, _make_expert("expert-1"))+ session_id = "0b8f3c1e-2d4a-4f6b-9c1d-3e5f7a9b1c2e"+ body = {"session_id": session_id, "expert_id": "expert-1"}++ first = client.post("/sessions", json=body)+ second = client.post("/sessions", json=body)++ assert second.status_code == 200+ assert second.json()["id"] == first.json()["id"] == session_id+ assert second.json()["expert_id"] == "expert-1"+ create.assert_awaited_once()+++def test_create_session_rejects_a_session_id_scoped_to_another_expert(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ """Replaying an id with a different expert must not hand back a session+ that runs as someone else."""+ store, _ = _in_memory_session_store(mocker)+ _mock_get_expert(mocker, _make_expert("expert-1"))+ session_id = "0b8f3c1e-2d4a-4f6b-9c1d-3e5f7a9b1c2f"+ client.post("/sessions", json={"session_id": session_id})++ response = client.post(+ "/sessions", json={"session_id": session_id, "expert_id": "expert-1"}+ )++ assert response.status_code == 409+ assert store[session_id].expert_id is None+++def test_create_session_rejects_another_users_session_id(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ """An id already taken by someone else is a conflict, never an adoption."""+ store, _ = _in_memory_session_store(mocker)+ session_id = "0b8f3c1e-2d4a-4f6b-9c1d-3e5f7a9b1c30"+ store[session_id] = ChatSession.new(+ "someone-else", dry_run=False, session_id=session_id+ )++ response = client.post("/sessions", json={"session_id": session_id})++ assert response.status_code == 409+ assert "someone-else" not in response.text+++ "body",+ [+ {"session_id": "not-a-uuid"},+ {+ "session_id": "0b8f3c1e-2d4a-4f6b-9c1d-3e5f7a9b1c31",+ "builder_graph_id": "graph-1",+ },+ {+ "session_id": "0b8f3c1e-2d4a-4f6b-9c1d-3e5f7a9b1c31",+ "expert_id": "expert-1",+ "expert_kickoff": True,+ },+ ],+)+def test_create_session_rejects_session_id_it_cannot_honour(+ mocker: pytest_mock.MockerFixture,+ body: dict,+) -> None:+ """Builder and kickoff sessions pick their own ids, so a client id there+ would be silently dropped; reject it instead."""+ _, create = _in_memory_session_store(mocker)+ _mock_get_expert(mocker, _make_expert("expert-1"))++ response = client.post("/sessions", json=body)++ assert response.status_code == 422+ create.assert_not_awaited()+++def test_create_session_replay_keeps_the_mode_it_was_created_with(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ """The first create starts on the expert's default approval mode. A replay+ of the same id, even after the expert's default changed or with a pick+ of its own, adopts that session and never resets its mode."""+ store: dict[str, ChatSession] = {}++ async def _get(session_id: str, user_id: str | None = None):+ session = store.get(session_id)+ if session is None or (user_id is not None and session.user_id != user_id):+ return None+ return session++ async def _persist(**kwargs):+ if kwargs["session_id"] in store:+ raise DatabaseError("duplicate session id")+ return SimpleNamespace(expert_id=kwargs["expert_id"])++ mock_experts_db = MagicMock()+ mock_experts_db.resolve_private_expert_tenancy = AsyncMock(+ return_value=("personal-org", "personal-team")+ )+ mock_experts_db.get_autopilot_mode = AsyncMock(return_value="ask_first")+ mock_chat_db = MagicMock()+ mock_chat_db.create_chat_session = AsyncMock(side_effect=_persist)+ mocker.patch("backend.copilot.model.experts_db", return_value=mock_experts_db)+ mocker.patch("backend.copilot.model.chat_db", return_value=mock_chat_db)+ mocker.patch("backend.copilot.model.get_chat_session", side_effect=_get)+ mocker.patch(+ "backend.copilot.model.cache_chat_session",+ new_callable=AsyncMock,+ side_effect=lambda session: store.setdefault(session.session_id, session),+ )+ mocker.patch(+ "backend.copilot.model.is_feature_enabled",+ new_callable=AsyncMock,+ return_value=True,+ )+ _mock_get_expert(mocker, _make_expert("expert-1"))+ session_id = "0b8f3c1e-2d4a-4f6b-9c1d-3e5f7a9b1c32"+ body = {"session_id": session_id, "expert_id": "expert-1"}++ first = client.post("/sessions", json=body)+ mock_experts_db.get_autopilot_mode.return_value = "unsupervised"+ replay = client.post("/sessions", json=body)+ picked_replay = client.post("/sessions", json={**body, "autopilot_mode": "auto"})++ assert first.status_code == 200+ assert first.json()["metadata"]["autopilot_mode"] == "ask_first"+ for response in (replay, picked_replay):+ assert response.status_code == 200+ assert response.json()["id"] == session_id+ assert response.json()["metadata"]["autopilot_mode"] == "ask_first"+ assert list(store) == [session_id]+ assert store[session_id].metadata.autopilot_mode == "ask_first"+ mock_chat_db.create_chat_session.assert_awaited_once()+++def test_create_session_with_a_session_id_passes_a_picked_mode_through(+ mocker: pytest_mock.MockerFixture,+ test_user_id: str,+) -> None:+ """A client-named create starts on the mode the user picked, like any+ other create."""+ _, create = _in_memory_session_store(mocker)++ response = client.post(+ "/sessions",+ json={+ "session_id": "0b8f3c1e-2d4a-4f6b-9c1d-3e5f7a9b1c33",+ "autopilot_mode": "ask_first",+ },+ )++ assert response.status_code == 200+ assert create.await_args.kwargs["autopilot_mode"] == "ask_first"++def test_create_session_passes_a_picked_mode_through(mocker: pytest_mock.MockerFixture,test_user_id: str,diff --git a/autogpt_platform/backend/backend/copilot/model.py b/autogpt_platform/backend/backend/copilot/model.pyindex f32e463c67..f7e66c93b0 100644--- a/autogpt_platform/backend/backend/copilot/model.py+++ b/autogpt_platform/backend/backend/copilot/model.py@@ -1581,6 +1581,98 @@ async def get_or_create_expert_kickoff_session(raise+class SessionIdConflictError(Exception):+ """A client-named session id is taken by a session the caller cannot adopt."""+++async def get_or_create_client_chat_session(+ user_id: str,+ session_id: str,+ *,+ dry_run: bool,+ organization_id: str | None = None,+ team_id: str | None = None,+ llm_auth_provider: CopilotLlmAuthProvider = "platform",+ llm_credential_id: str | None = None,+ expert_id: str | None = None,+ autopilot_mode: AutopilotMode | None = None,+) -> ChatSession:+ """Create the session the client named, or adopt it on a retry.++ The client mints *session_id* before its first attempt, so a retry after a+ lost response lands on the session that attempt committed instead of+ leaving it empty behind a second one. The primary key is the claim, the+ same way a turn-start send dedupes on its ``ChatMessage`` id.++ ``autopilot_mode`` only shapes a session created here; an adopted one+ keeps the mode it started on, even if the expert's default changed since.++ Raises:+ SessionIdConflictError: the id belongs to another user's session, or+ to one this create could not have made: another expert, another+ organization or team, or a builder-bound session.+ """+ existing = await get_chat_session(session_id, user_id)+ if existing is None:+ try:+ return await create_chat_session(+ user_id,+ dry_run=dry_run,+ session_id=session_id,+ organization_id=organization_id,+ team_id=team_id,+ llm_auth_provider=llm_auth_provider,+ llm_credential_id=llm_credential_id,+ expert_id=expert_id,+ autopilot_mode=autopilot_mode,+ )+ except DatabaseError:+ existing = await get_chat_session(session_id, user_id)+ if existing is None:+ if await get_chat_session(session_id) is not None:+ raise SessionIdConflictError(session_id)+ raise+ if not _client_session_in_scope(+ existing,+ organization_id=organization_id,+ team_id=team_id,+ expert_id=expert_id,+ ):+ raise SessionIdConflictError(session_id)+ return existing+++def _client_session_in_scope(+ session: ChatSession,+ *,+ organization_id: str | None,+ team_id: str | None,+ expert_id: str | None,+) -> bool:+ """Whether a create with these arguments could have made *session*.++ This endpoint only makes interactive sessions with no builder binding,+ external platform, delegation or handoff, so a session that has any of+ those came from another path and is never adopted. An expert session's+ tenancy comes from the expert (``create_chat_session`` pins it to the+ owner's private tenancy), so the expert match covers it.+ """+ metadata = session.metadata+ if (+ session.expert_id != expert_id+ or metadata.origin != "interactive"+ or metadata.builder_graph_id+ or metadata.source_platform+ or metadata.delegated_by_session_id+ or metadata.delegated_by_expert_id+ or metadata.handed_off_from_expert_id+ ):+ return False+ if expert_id is not None:+ return True+ return session.organization_id == organization_id and session.team_id == team_id++async def get_or_create_builder_session(user_id: str,graph_id: str,diff --git a/autogpt_platform/backend/backend/copilot/model_test.py b/autogpt_platform/backend/backend/copilot/model_test.pyindex 7bbb37d5a9..944019fe8a 100644--- a/autogpt_platform/backend/backend/copilot/model_test.py+++ b/autogpt_platform/backend/backend/copilot/model_test.py@@ -2310,6 +2310,292 @@ async def test_save_session_to_db_stamp_backfill_failure_keeps_flag(assert flushed.stamps_pending_save is True+async def test_get_or_create_client_chat_session_recovers_create_race(+ mocker: MockerFixture,+) -> None:+ """Two copies of one create race; the loser adopts the winner's row."""+ from backend.util.exceptions import DatabaseError++ from .model import get_or_create_client_chat_session++ existing = ChatSession.new("user-a", dry_run=False, session_id="sid-1")+ get_session = mocker.patch(+ "backend.copilot.model.get_chat_session",+ new_callable=mocker.AsyncMock,+ side_effect=[None, existing],+ )+ mocker.patch(+ "backend.copilot.model.create_chat_session",+ new_callable=mocker.AsyncMock,+ side_effect=DatabaseError("duplicate session id"),+ )++ result = await get_or_create_client_chat_session("user-a", "sid-1", dry_run=False)++ assert result is existing+ assert get_session.await_count == 2+++async def test_get_or_create_client_chat_session_reraises_real_db_failure(+ mocker: MockerFixture,+) -> None:+ from backend.util.exceptions import DatabaseError++ from .model import get_or_create_client_chat_session++ mocker.patch(+ "backend.copilot.model.get_chat_session",+ new_callable=mocker.AsyncMock,+ return_value=None,+ )+ mocker.patch(+ "backend.copilot.model.create_chat_session",+ new_callable=mocker.AsyncMock,+ side_effect=DatabaseError("database unavailable"),+ )++ with pytest.raises(DatabaseError, match="database unavailable"):+ await get_or_create_client_chat_session("user-a", "sid-1", dry_run=False)+++async def test_get_or_create_client_chat_session_refuses_a_different_expert(+ mocker: MockerFixture,+) -> None:+ from .model import SessionIdConflictError, get_or_create_client_chat_session++ existing = ChatSession.new(+ "user-a", dry_run=False, session_id="sid-1", expert_id="expert-a"+ )+ mocker.patch(+ "backend.copilot.model.get_chat_session",+ new_callable=mocker.AsyncMock,+ return_value=existing,+ )+ create_session = mocker.patch(+ "backend.copilot.model.create_chat_session",+ new_callable=mocker.AsyncMock,+ )++ with pytest.raises(SessionIdConflictError):+ await get_or_create_client_chat_session(+ "user-a", "sid-1", dry_run=False, expert_id="expert-b"+ )+ create_session.assert_not_awaited()+++ "persisted",+ [+ {"organization_id": "org-a", "team_id": "team-a"},+ {"organization_id": "org-b", "team_id": "team-a"},+ {"organization_id": "org-b", "team_id": "team-b", "builder_graph_id": "g-1"},+ {"organization_id": "org-b", "team_id": "team-b", "origin": "automation"},+ {"organization_id": "org-b", "team_id": "team-b", "source_platform": "slack"},+ {+ "organization_id": "org-b",+ "team_id": "team-b",+ "delegated_by_session_id": "sid-0",+ },+ {+ "organization_id": "org-b",+ "team_id": "team-b",+ "handed_off_from_expert_id": "expert-0",+ },+ {+ "organization_id": "org-b",+ "team_id": "team-b",+ "delegated_by_expert_id": "expert-0",+ },+ ],+ ids=[+ "other-organization",+ "other-team",+ "builder-bound",+ "automation",+ "external-platform",+ "delegated",+ "handed-off",+ "delegated-by-expert",+ ],+)+async def test_get_or_create_client_chat_session_refuses_a_plain_session_out_of_scope(+ mocker: MockerFixture, persisted: dict[str, str]+) -> None:+ from .model import SessionIdConflictError, get_or_create_client_chat_session++ existing = ChatSession.new("user-a", dry_run=False, session_id="sid-1", **persisted)+ mocker.patch(+ "backend.copilot.model.get_chat_session",+ new_callable=mocker.AsyncMock,+ return_value=existing,+ )++ with pytest.raises(SessionIdConflictError):+ await get_or_create_client_chat_session(+ "user-a",+ "sid-1",+ dry_run=False,+ organization_id="org-b",+ team_id="team-b",+ )+++async def test_get_or_create_client_chat_session_refuses_a_race_winner_out_of_scope(+ mocker: MockerFixture,+) -> None:+ from backend.util.exceptions import DatabaseError++ from .model import SessionIdConflictError, get_or_create_client_chat_session++ winner = ChatSession.new(+ "user-a", dry_run=False, session_id="sid-1", organization_id="org-a"+ )+ mocker.patch(+ "backend.copilot.model.get_chat_session",+ new_callable=mocker.AsyncMock,+ side_effect=[None, winner],+ )+ mocker.patch(+ "backend.copilot.model.create_chat_session",+ new_callable=mocker.AsyncMock,+ side_effect=DatabaseError("duplicate session id"),+ )++ with pytest.raises(SessionIdConflictError):+ await get_or_create_client_chat_session(+ "user-a", "sid-1", dry_run=False, organization_id="org-b"+ )+++async def test_get_or_create_client_chat_session_adopts_an_expert_session_in_its_private_tenancy(+ mocker: MockerFixture,+) -> None:+ """Expert sessions are pinned to the expert's private tenancy at create+ time, so a replay from a team context still adopts the same session."""+ from .model import get_or_create_client_chat_session++ existing = ChatSession.new(+ "user-a",+ dry_run=False,+ session_id="sid-1",+ expert_id="expert-a",+ organization_id="personal-org",+ )+ mocker.patch(+ "backend.copilot.model.get_chat_session",+ new_callable=mocker.AsyncMock,+ return_value=existing,+ )+ create_session = mocker.patch(+ "backend.copilot.model.create_chat_session",+ new_callable=mocker.AsyncMock,+ )++ result = await get_or_create_client_chat_session(+ "user-a",+ "sid-1",+ dry_run=False,+ organization_id="team-org",+ team_id="team-a",+ expert_id="expert-a",+ )++ assert result is existing+ create_session.assert_not_awaited()+++async def test_get_or_create_client_chat_session_refuses_an_automation_expert_session(+ mocker: MockerFixture,+) -> None:+ from .model import SessionIdConflictError, get_or_create_client_chat_session++ existing = ChatSession.new(+ "user-a",+ dry_run=False,+ session_id="sid-1",+ expert_id="expert-a",+ origin="automation",+ )+ mocker.patch(+ "backend.copilot.model.get_chat_session",+ new_callable=mocker.AsyncMock,+ return_value=existing,+ )++ with pytest.raises(SessionIdConflictError):+ await get_or_create_client_chat_session(+ "user-a", "sid-1", dry_run=False, expert_id="expert-a"+ )+++async def test_get_or_create_client_chat_session_creates_with_the_picked_mode(+ mocker: MockerFixture,+) -> None:+ from .model import get_or_create_client_chat_session++ mocker.patch(+ "backend.copilot.model.get_chat_session",+ new_callable=mocker.AsyncMock,+ return_value=None,+ )+ create = mocker.patch(+ "backend.copilot.model.create_chat_session",+ new_callable=mocker.AsyncMock,+ return_value=ChatSession.new("user-a", dry_run=False, session_id="sid-1"),+ )++ await get_or_create_client_chat_session(+ "user-a", "sid-1", dry_run=False, autopilot_mode="ask_first"+ )++ assert create.await_args.kwargs["autopilot_mode"] == "ask_first"+++async def test_get_or_create_client_chat_session_adoption_keeps_the_stored_mode(+ mocker: MockerFixture,+) -> None:+ """A replay never rewrites the mode the session started on, whatever the+ replay asks for."""+ from .model import get_or_create_client_chat_session++ existing = ChatSession.new(+ "user-a",+ dry_run=False,+ session_id="sid-1",+ expert_id="expert-a",+ autopilot_mode="ask_first",+ )+ mocker.patch(+ "backend.copilot.model.get_chat_session",+ new_callable=mocker.AsyncMock,+ return_value=existing,+ )+ create = mocker.patch(+ "backend.copilot.model.create_chat_session", new_callable=mocker.AsyncMock+ )++ result = await get_or_create_client_chat_session(+ "user-a",+ "sid-1",+ dry_run=False,+ expert_id="expert-a",+ autopilot_mode="unsupervised",+ )++ assert result is existing+ assert result.metadata.autopilot_mode == "ask_first"+ create.assert_not_awaited()++def _mock_delete_dependencies(mocker: MockerFixture, *, deleted: bool):db = mocker.MagicMock()db.delete_chat_session = mocker.AsyncMock(return_value=deleted)diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/codex-session-route.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/codex-session-route.test.tsxindex 39f32e2b6c..14b92e2446 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/codex-session-route.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/codex-session-route.test.tsx@@ -149,7 +149,10 @@ describe("useChatSession transport route", () => {fireEvent.click(screen.getByRole("button", { name: "Create session" }));await waitFor(() => {- expect(getRequestBody()).toEqual({ llm_auth_provider: "platform" });+ expect(getRequestBody()).toEqual({+ llm_auth_provider: "platform",+ session_id: expect.any(String),+ });});});@@ -168,6 +171,7 @@ describe("useChatSession transport route", () => {expect(getRequestBody()).toEqual({llm_auth_provider: "codex",llm_credential_id: "codex-credential-1",+ session_id: expect.any(String),});});});@@ -180,7 +184,10 @@ describe("useChatSession transport route", () => {fireEvent.click(screen.getByRole("button", { name: "Create session" }));await waitFor(() => {- expect(getRequestBody()).toEqual({ llm_auth_provider: "platform" });+ expect(getRequestBody()).toEqual({+ llm_auth_provider: "platform",+ session_id: expect.any(String),+ });});});@@ -198,6 +205,7 @@ describe("useChatSession transport route", () => {expect(getRequestBody()).toEqual({llm_auth_provider: "codex",llm_credential_id: "codex-credential-1",+ session_id: expect.any(String),});});});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/create-session-lost-response.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/create-session-lost-response.test.tsxnew file mode 100644index 0000000000..f6802d506b--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/create-session-lost-response.test.tsx@@ -0,0 +1,290 @@+import type { ChatTransportResponse } from "@/app/api/__generated__/models/chatTransportResponse";+import { server } from "@/mocks/mock-server";+import {+ act,+ fireEvent,+ render,+ screen,+ waitFor,+} from "@/tests/integrations/test-utils";+import { http, HttpResponse } from "msw";+import { useState } from "react";+import { afterEach, describe, expect, it, vi } from "vitest";+import { useAutopilotModeStore } from "../autopilotModeStore";+import { ChatInput } from "../components/ChatInput/ChatInput";+import { useChatSession } from "../useChatSession";++// A phone that loses the create response sees `TypeError: Load failed` even+// though the server committed the session. The composer and the create stay+// real here; only the periphery the composer pulls in is stubbed.++const testState = vi.hoisted(() => ({+ toast: vi.fn(),+}));++const hostedPlatform: ChatTransportResponse = {+ auth_provider: "platform",+ credential_id: null,+ label: "AutoGPT Platform",+ available: true,+ default: true,+};++vi.mock(+ "@/app/api/__generated__/endpoints/chat/chat",+ async (importOriginal) => {+ const actual =+ await importOriginal<+ typeof import("@/app/api/__generated__/endpoints/chat/chat")+ >();+ return {+ ...actual,+ useGetV2ListChatTransports: () => ({+ data: { status: 200, data: { transports: [hostedPlatform] } },+ isError: false,+ }),+ useGetV2ListChatConnections: () => ({+ data: { status: 200, data: { offers: [] } },+ isLoading: false,+ isPending: false,+ isError: false,+ }),+ };+ },+);++vi.mock("@/components/molecules/Toast/use-toast", () => ({+ toast: (...args: unknown[]) => testState.toast(...args),+ useToast: () => ({ toast: testState.toast, dismiss: vi.fn() }),+}));++vi.mock("@/services/feature-flags/use-get-flag", () => ({+ Flag: {+ CHAT_MODE_OPTION: "CHAT_MODE_OPTION",+ CHAT_WORKSPACE_FILES: "chat-workspace-files",+ },+ useGetFlag: () => false,+}));++vi.mock("../components/ChatInput/useVoiceRecording", () => ({+ useVoiceRecording: () => ({+ isRecording: false,+ isTranscribing: false,+ transcriptionError: null,+ hasFailedRecording: false,+ retryTranscription: vi.fn(),+ downloadFailedRecording: vi.fn(),+ dismissTranscriptionError: vi.fn(),+ elapsedTime: 0,+ toggleRecording: vi.fn(),+ handleKeyDown: vi.fn(),+ showMicButton: false,+ isInputDisabled: false,+ audioStream: null,+ }),+}));++vi.mock("@/components/ai-elements/prompt-input", () => ({+ PromptInputSubmit: ({ disabled }: { disabled?: boolean }) => (+ <button disabled={disabled} data-testid="submit">+ Send+ </button>+ ),+ PromptInputButton: () => null,+ PromptInputTextarea: (props: {+ id?: string;+ value?: string;+ onChange?: React.ChangeEventHandler<HTMLTextAreaElement>;+ }) => (+ <textarea+ id={props.id}+ value={props.value}+ onChange={props.onChange}+ data-testid="textarea"+ />+ ),+}));++function ComposerHarness() {+ const { createSession } = useChatSession();+ const [createdId, setCreatedId] = useState<string | null>(null);+ return (+ <>+ <ChatInput+ onSend={async () => {+ setCreatedId(await createSession());+ }}+ />+ <output data-testid="created-session">{createdId ?? ""}</output>+ </>+ );+}++interface CreateBody {+ session_id?: string;+ autopilot_mode?: string;+}++function loseCreateResponses(lost: number) {+ const bodies: CreateBody[] = [];+ server.use(+ http.post("*/api/chat/sessions", async ({ request }) => {+ const body = ((await request.json()) ?? {}) as CreateBody;+ bodies.push(body);+ if (bodies.length <= lost) return HttpResponse.error();+ return HttpResponse.json({+ id: body.session_id ?? "server-minted",+ created_at: "2026-09-30T11:03:02Z",+ user_id: "user-1",+ });+ }),+ );+ return bodies;+}++function refuseFirstCreate() {+ const bodies: Array<{ session_id?: string }> = [];+ server.use(+ http.post("*/api/chat/sessions", async ({ request }) => {+ const body = ((await request.json()) ?? {}) as { session_id?: string };+ bodies.push(body);+ if (bodies.length === 1) {+ return HttpResponse.json(+ { detail: "Session id already in use" },+ { status: 409 },+ );+ }+ return HttpResponse.json({+ id: body.session_id ?? "server-minted",+ created_at: "2026-09-30T11:03:02Z",+ user_id: "user-1",+ });+ }),+ );+ return bodies;+}++function composerValue() {+ return (screen.getByTestId("textarea") as HTMLTextAreaElement).value;+}++function createdSession() {+ return screen.getByTestId("created-session").textContent;+}++function pickMode(mode: "ask_first" | "unsupervised") {+ act(() => useAutopilotModeStore.getState().choose(null, mode));+}++function send(text: string) {+ fireEvent.change(screen.getByTestId("textarea"), {+ target: { value: text },+ });+ fireEvent.click(screen.getByTestId("submit"));+}++afterEach(() => {+ server.resetHandlers();+ testState.toast.mockClear();+ useAutopilotModeStore.setState({ choices: {} });+});++describe("creating a session when the response is lost", () => {+ it("retries once with the same session id and keeps the message sent", async () => {+ const bodies = loseCreateResponses(1);+ render(<ComposerHarness />);++ send("Hi Anika");++ await waitFor(() => expect(bodies).toHaveLength(2), { timeout: 3000 });+ expect(bodies[0].session_id).toMatch(+ /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/,+ );+ expect(bodies[1].session_id).toBe(bodies[0].session_id);+ await waitFor(() => expect(createdSession()).toBe(bodies[0].session_id));+ expect(composerValue()).toBe("");+ expect(testState.toast).not.toHaveBeenCalledWith(+ expect.objectContaining({ variant: "destructive" }),+ );+ });++ it("gives the message back after the one retry also fails", async () => {+ const bodies = loseCreateResponses(2);+ render(<ComposerHarness />);++ send("Hi Anika");++ await waitFor(() => expect(composerValue()).toBe("Hi Anika"), {+ timeout: 3000,+ });+ expect(bodies).toHaveLength(2);+ expect(bodies[1].session_id).toBe(bodies[0].session_id);+ });++ it("resends with the same session id after every response was lost", async () => {+ const bodies = loseCreateResponses(2);+ render(<ComposerHarness />);++ send("Hi Anika");+ await waitFor(() => expect(composerValue()).toBe("Hi Anika"), {+ timeout: 3000,+ });+ send("Hi Anika");++ await waitFor(() => expect(createdSession()).toBe(bodies[0].session_id), {+ timeout: 3000,+ });+ expect(bodies).toHaveLength(3);+ expect(bodies[2].session_id).toBe(bodies[0].session_id);+ });++ it("names a new session after the server answered with a refusal", async () => {+ const bodies = refuseFirstCreate();+ render(<ComposerHarness />);++ send("Hi Anika");+ await waitFor(() => expect(composerValue()).toBe("Hi Anika"), {+ timeout: 3000,+ });+ send("Hi Anika");++ await waitFor(() => expect(createdSession()).toBe(bodies[1].session_id), {+ timeout: 3000,+ });+ expect(bodies).toHaveLength(2);+ expect(bodies[1].session_id).not.toBe(bodies[0].session_id);+ });++ it("retries with the picked approval mode under the same session id", async () => {+ const bodies = loseCreateResponses(1);+ render(<ComposerHarness />);+ pickMode("ask_first");++ send("Hi Anika");++ await waitFor(() => expect(bodies).toHaveLength(2), { timeout: 3000 });+ expect(bodies[0].autopilot_mode).toBe("ask_first");+ expect(bodies[1].autopilot_mode).toBe("ask_first");+ expect(bodies[1].session_id).toBe(bodies[0].session_id);+ await waitFor(() => expect(createdSession()).toBe(bodies[0].session_id));+ });++ it("names a new session when the pick changed before the resend", async () => {+ const bodies = loseCreateResponses(2);+ render(<ComposerHarness />);+ pickMode("ask_first");++ send("Hi Anika");+ await waitFor(() => expect(composerValue()).toBe("Hi Anika"), {+ timeout: 3000,+ });+ pickMode("unsupervised");+ send("Hi Anika");++ await waitFor(() => expect(createdSession()).toBe(bodies[2].session_id), {+ timeout: 3000,+ });+ expect(bodies[2].autopilot_mode).toBe("unsupervised");+ expect(bodies[2].session_id).not.toBe(bodies[0].session_id);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expert-threads.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expert-threads.test.tsxindex a6df5845eb..65cb297464 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expert-threads.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/expert-threads.test.tsx@@ -326,7 +326,10 @@ describe("useChatSession — expert sessions", () => {// No route travels unless the user picked one. Naming it here would// make the server skip its own default, which is how a connection// chosen for one chat leaked into every later chat.- expect(createBody).toEqual({ expert_id: "expert-maria" });+ expect(createBody).toEqual({+ expert_id: "expert-maria",+ session_id: expect.any(String),+ });});await waitFor(() =>expect(diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.tsindex dda131e25d..5c907d04c9 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts@@ -482,3 +482,13 @@ export function isEngineSwitchPart(dataPart: {const mode = (dataPart.data as { mode?: string } | undefined)?.mode;return mode === "extended_thinking" || mode === "fast";}++export const CREATE_SESSION_RETRY_DELAY_MS = 500;++// A network error on create ("Load failed" on Safari, "Failed to fetch" on+// Chrome) can mean the server committed and only the response was lost. The+// request names its session id, so one retry adopts that session instead of+// bouncing the message back to the composer.+export function retryLostCreateResponse(failureCount: number, error: unknown) {+ return failureCount < 1 && error instanceof TypeError;+}diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.tsindex b1f9e40f91..ce02d8fe30 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useChatSession.ts@@ -15,12 +15,17 @@ import { Flag, useGetFlag } from "@/services/feature-flags/use-get-flag";import * as Sentry from "@sentry/nextjs";import { useQueryClient } from "@tanstack/react-query";import { parseAsString, useQueryState } from "nuqs";+import { v4 as uuidv4 } from "uuid";import { useEffect, useMemo, useRef } from "react";import {convertChatSessionMessagesToUiMessages,type TurnStatsMap,} from "./helpers/convertChatSessionToUiMessages";-import { resolveSessionDryRun } from "./helpers";+import {+ CREATE_SESSION_RETRY_DELAY_MS,+ resolveSessionDryRun,+ retryLostCreateResponse,+} from "./helpers";import { isPaywallError } from "@/components/contextual/PaywallToast/helpers";import { getSessionSentFrom } from "./sentFrom";import {@@ -143,6 +148,9 @@ export function useChatSession({// `useSendMessage` flushes its pending first message on *any* sessionId// change, so a late adoption would post that message into the old thread.const sendStartedRef = useRef(false);+ // The id the last create named, kept while every attempt lost its response+ // so a resend of the same request adopts what that attempt may have saved.+ const pendingCreateRef = useRef<{ id: string; request: string } | null>(null);const canAdoptExpertSession =adoptLatestExpertThread &&!!expertId &&@@ -255,7 +263,12 @@ export function useChatSession({}, [freshSessionData, sessionId, hasActiveStream, activeStreamStartedAt]);const { mutateAsync: createSessionMutation, isPending: isCreatingSession } =- usePostV2CreateSession();+ usePostV2CreateSession({+ mutation: {+ retry: retryLostCreateResponse,+ retryDelay: CREATE_SESSION_RETRY_DELAY_MS,+ },+ });async function createSession(options?: { expertKickoff?: boolean }) {if (sessionId) return sessionId;@@ -332,16 +345,23 @@ export function useChatSession({}if (dryRun) sessionData.dry_run = true;if (expertId) sessionData.expert_id = expertId;- if (options?.expertKickoff) sessionData.expert_kickoff = true;// Same rule as the route: only a mode the user picked travels, so a new// expert thread starts on the expert's own default rather than "auto".+ // It is part of the request, so a different pick names a new session.const pickedMode = getAutopilotModeChoice(null);if (pickedMode) sessionData.autopilot_mode = pickedMode;- const body =- Object.keys(sessionData).length > 0- ? { data: sessionData }- : { data: null };- const response = await createSessionMutation(body);+ // Naming the session up front lets a retry after a lost response adopt+ // it. Kickoff is already get-or-create on the server.+ if (options?.expertKickoff) sessionData.expert_kickoff = true;+ else {+ const request = JSON.stringify(sessionData);+ if (pendingCreateRef.current?.request !== request) {+ // uuidv4({}) rather than crypto.randomUUID, which is missing on plain HTTP.+ pendingCreateRef.current = { id: uuidv4({}), request };+ }+ sessionData.session_id = pendingCreateRef.current.id;+ }+ const response = await createSessionMutation({ data: sessionData });if (response.status !== 200 || !response.data?.id) {const error = new Error("Failed to create session");Sentry.captureException(error, {@@ -349,6 +369,7 @@ export function useChatSession({});throw error;}+ pendingCreateRef.current = null;useCopilotStreamStore.getState().bindPendingFirstSendToSession(response.data.id);@@ -362,6 +383,7 @@ export function useChatSession({});return response.data.id;} catch (error) {+ if (!(error instanceof TypeError)) pendingCreateRef.current = null;// The caller owns the one toast for this failure (see// toastSendFailure); toasting here as well stacked two contradictory// messages. A paywall is an account state, not a fault.diff --git a/autogpt_platform/frontend/src/app/api/openapi.json b/autogpt_platform/frontend/src/app/api/openapi.jsonindex 0e6aa7d9c6..5d9ba792cf 100644--- a/autogpt_platform/frontend/src/app/api/openapi.json+++ b/autogpt_platform/frontend/src/app/api/openapi.json@@ -4000,7 +4000,7 @@"post": {"tags": ["v2", "chat", "chat"],"summary": "Create Session",- "description": "Create (or get-or-create) a chat session.\n\nTwo modes, selected by the request body:\n\n- Default: create a fresh session for the user. ``dry_run=True`` forces\n run_capability and run_agent calls to use dry-run simulation.\n- Builder-bound: when ``builder_graph_id`` is set, get-or-create keyed\n on ``(user_id, builder_graph_id)``. Returns the existing session for\n that graph or creates one locked to it. Graph ownership is validated\n inside :func:`get_or_create_builder_session`; raises 404 on\n unauthorized access. Write-side scope is enforced per-tool\n (``edit_agent`` / ``run_agent`` reject any ``agent_id`` other than\n the bound graph) and a small blacklist hides tools that conflict\n with the panel's scope (see :data:`BUILDER_BLOCKED_TOOLS`).\n- Expert kickoff: atomically create or adopt the canonical first session\n for ``(user_id, expert_id)``.\n\nArgs:\n user_id: The authenticated user ID parsed from the JWT (required).\n request: Optional request body with ``dry_run``,\n ``builder_graph_id`` and/or ``expert_id``.\n\nReturns:\n CreateSessionResponse: Details of the resulting session.",+ "description": "Create (or get-or-create) a chat session.\n\nFour modes, selected by the request body:\n\n- Default: create a fresh session for the user. ``dry_run=True`` forces\n run_capability and run_agent calls to use dry-run simulation.\n- Builder-bound: when ``builder_graph_id`` is set, get-or-create keyed\n on ``(user_id, builder_graph_id)``. Returns the existing session for\n that graph or creates one locked to it. Graph ownership is validated\n inside :func:`get_or_create_builder_session`; raises 404 on\n unauthorized access. Write-side scope is enforced per-tool\n (``edit_agent`` / ``run_agent`` reject any ``agent_id`` other than\n the bound graph) and a small blacklist hides tools that conflict\n with the panel's scope (see :data:`BUILDER_BLOCKED_TOOLS`).\n- Expert kickoff: atomically create or adopt the canonical first session\n for ``(user_id, expert_id)``.\n- Client-named: when ``session_id`` is set, a plain or expert create\n adopts the session that id already names, so a retry after a lost\n response never leaves an empty session behind.\n\nArgs:\n user_id: The authenticated user ID parsed from the JWT (required).\n request: Optional request body with ``dry_run``,\n ``builder_graph_id``, ``expert_id``, ``expert_kickoff`` and/or\n ``session_id``.\n\nReturns:\n CreateSessionResponse: Details of the resulting session.","operationId": "postV2CreateSession","security": [{ "HTTPBearerJWT": [] }],"requestBody": {@@ -22165,6 +22165,13 @@"title": "Expert Kickoff","default": false},+ "session_id": {+ "anyOf": [+ { "type": "string", "format": "uuid" },+ { "type": "null" }+ ],+ "title": "Session Id"+ },"autopilot_mode": {"anyOf": [{@@ -22179,7 +22186,7 @@"additionalProperties": false,"type": "object","title": "CreateSessionRequest",- "description": "Request model for creating (or get-or-creating) a chat session.\n\nTwo modes, selected by the body:\n\n- Default: create a fresh session. ``dry_run`` is a **top-level**\n field — do not nest it inside ``metadata``.\n- Builder-bound: when ``builder_graph_id`` is set, the endpoint\n switches to **get-or-create** keyed on\n ``(user_id, builder_graph_id)``. The builder panel calls this on\n mount so the chat persists across refreshes. Graph ownership is\n validated inside :func:`get_or_create_builder_session`. Write-side\n scope is enforced per-tool (``edit_agent`` / ``run_agent`` reject\n any ``agent_id`` other than the bound graph) and a small blacklist\n hides tools that conflict with the panel's scope\n (``create_agent`` / ``customize_agent`` / ``get_agent_building_guide``\n — see :data:`BUILDER_BLOCKED_TOOLS`). Read-side lookups\n (``find_capability``, ``find_agent``, ``search_docs``, …) stay open.\n\n``expert_id`` scopes the session to a hired expert. It must reference\nan expert owned by the caller that is neither a template nor archived,\notherwise the request is rejected with 404. It is mutually exclusive\nwith ``builder_graph_id`` (422) — builder-bound sessions are never\nexpert-scoped.\n\n``expert_kickoff`` switches expert creation to an atomic create-or-adopt\nmode keyed by the authenticated user and expert. It is invalid without\n``expert_id``.\n\nExtra/unknown fields are rejected (422) to prevent silent mis-use."+ "description": "Request model for creating (or get-or-creating) a chat session.\n\nTwo modes, selected by the body:\n\n- Default: create a fresh session. ``dry_run`` is a **top-level**\n field — do not nest it inside ``metadata``.\n- Builder-bound: when ``builder_graph_id`` is set, the endpoint\n switches to **get-or-create** keyed on\n ``(user_id, builder_graph_id)``. The builder panel calls this on\n mount so the chat persists across refreshes. Graph ownership is\n validated inside :func:`get_or_create_builder_session`. Write-side\n scope is enforced per-tool (``edit_agent`` / ``run_agent`` reject\n any ``agent_id`` other than the bound graph) and a small blacklist\n hides tools that conflict with the panel's scope\n (``create_agent`` / ``customize_agent`` / ``get_agent_building_guide``\n — see :data:`BUILDER_BLOCKED_TOOLS`). Read-side lookups\n (``find_capability``, ``find_agent``, ``search_docs``, …) stay open.\n\n``expert_id`` scopes the session to a hired expert. It must reference\nan expert owned by the caller that is neither a template nor archived,\notherwise the request is rejected with 404. It is mutually exclusive\nwith ``builder_graph_id`` (422) — builder-bound sessions are never\nexpert-scoped.\n\n``expert_kickoff`` switches expert creation to an atomic create-or-adopt\nmode keyed by the authenticated user and expert. It is invalid without\n``expert_id``.\n\n``session_id`` lets the client name the session up front, which makes\nthe create idempotent: a retry after a lost response adopts the session\nthe first attempt committed (409 if the id is someone else's or scoped\nto another expert). Builder and kickoff sessions pick their own ids, so\nit is rejected alongside them (422).\n\nExtra/unknown fields are rejected (422) to prevent silent mis-use."},"CreateSessionResponse": {"properties": {