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(copilot): set session_id on mode-switch T1 to enable --resume on subsequent SDK turns (#12795)
Zamil Majdyd23ca824adf0
diff --git a/autogpt_platform/backend/backend/copilot/sdk/mode_switch_context_test.py b/autogpt_platform/backend/backend/copilot/sdk/mode_switch_context_test.pynew file mode 100644index 0000000000..5e1ef41979--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/mode_switch_context_test.py@@ -0,0 +1,326 @@+"""Tests for transcript context coverage when switching between fast and SDK modes.++When a user switches modes mid-session the transcript must bridge the gap so+neither the baseline nor the SDK service loses context from turns produced by+the other mode.++Cross-mode transcript flow+==========================++Both ``baseline/service.py`` (fast mode) and ``sdk/service.py`` (extended_thinking+mode) read and write the same JSONL transcript store via+``backend.copilot.transcript.upload_transcript`` /+``download_transcript``.++Fast → SDK switch+-----------------+On the first SDK turn after N baseline turns:+ • ``use_resume=False`` — no CLI session exists from baseline mode.+ • ``transcript_msg_count > 0`` — the baseline transcript is downloaded and+ validated successfully.+ • ``_build_query_message`` must inject the FULL prior session (not just a+ "gap" since the transcript end) because the CLI has zero context without+ ``--resume``.+ • After our fix, ``session_id`` IS set, so the CLI writes a session file+ on this turn → ``--resume`` works on T2+.++SDK → Fast switch+-----------------+On the first baseline turn after N SDK turns:+ • The baseline service downloads the SDK-written transcript.+ • ``_load_prior_transcript`` loads and validates it normally — the JSONL+ format is identical regardless of which mode wrote it.+ • ``transcript_covers_prefix=True`` → baseline sends ONLY new messages in+ its LLM payload (no double-counting of SDK history).++Scenario table (SDK _build_query_message)+==========================================++| # | Scenario | use_resume | tmc | Expected query message |+|---|--------------------------------|------------|-----|---------------------------------|+| P | Fast→SDK T1 | False | 4 | full session injected |+| Q | Fast→SDK T2+ (after fix) | True | 6 | bare message only (--resume ok) |+| R | Fast→SDK T1, single baseline | False | 2 | full session injected |+| S | SDK→Fast (baseline loads ok) | N/A | N/A | transcript covers prefix=True |+"""++from __future__ import annotations++from datetime import UTC, datetime+from unittest.mock import AsyncMock, patch++import pytest++from backend.copilot.model import ChatMessage, ChatSession+from backend.copilot.sdk.service import _build_query_message++# ---------------------------------------------------------------------------+# Helpers+# ---------------------------------------------------------------------------+++def _make_session(messages: list[ChatMessage]) -> ChatSession:+ now = datetime.now(UTC)+ return ChatSession(+ session_id="test-session",+ user_id="user-1",+ messages=messages,+ title="test",+ usage=[],+ started_at=now,+ updated_at=now,+ )+++def _msgs(*pairs: tuple[str, str]) -> list[ChatMessage]:+ return [ChatMessage(role=r, content=c) for r, c in pairs]+++# ---------------------------------------------------------------------------+# Scenario P — Fast → SDK T1: full session injected from baseline transcript+# ---------------------------------------------------------------------------+++class TestFastToSdkModeSwitch:+ """First SDK turn after N baseline (fast) turns.++ The baseline transcript exists (has been uploaded by fast mode), but+ there is no CLI session file. ``_build_query_message`` must inject+ the complete prior session so the model has full context.+ """++ @pytest.mark.asyncio+ async def test_scenario_p_full_session_injected_on_mode_switch_t1(+ self, monkeypatch+ ):+ """Scenario P: fast→SDK T1 injects all baseline turns into the query."""+ # Simulate 4 baseline messages (2 turns) followed by the first SDK turn.+ session = _make_session(+ _msgs(+ ("user", "baseline-q1"),+ ("assistant", "baseline-a1"),+ ("user", "baseline-q2"),+ ("assistant", "baseline-a2"),+ ("user", "sdk-q1"), # current SDK turn+ )+ )++ async def _mock_compress(msgs, target_tokens=None):+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ # transcript_msg_count=4: baseline uploaded a transcript covering all+ # 4 prior messages, but use_resume=False (no CLI session from baseline).+ result, compacted = await _build_query_message(+ "sdk-q1",+ session,+ use_resume=False,+ transcript_msg_count=4,+ session_id="s",+ )++ # All baseline turns must appear — none of them can be silently dropped.+ assert "<conversation_history>" in result+ assert "baseline-q1" in result+ assert "baseline-a1" in result+ assert "baseline-q2" in result+ assert "baseline-a2" in result+ assert "Now, the user says:\nsdk-q1" in result+ assert compacted is False++ @pytest.mark.asyncio+ async def test_scenario_r_single_baseline_turn_injected(self, monkeypatch):+ """Scenario R: even a single baseline turn is captured on mode-switch T1."""+ session = _make_session(+ _msgs(+ ("user", "baseline-q1"),+ ("assistant", "baseline-a1"),+ ("user", "sdk-q1"),+ )+ )++ async def _mock_compress(msgs, target_tokens=None):+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ result, _ = await _build_query_message(+ "sdk-q1",+ session,+ use_resume=False,+ transcript_msg_count=2,+ session_id="s",+ )++ assert "<conversation_history>" in result+ assert "baseline-q1" in result+ assert "baseline-a1" in result+ assert "Now, the user says:\nsdk-q1" in result++ @pytest.mark.asyncio+ async def test_scenario_q_sdk_t2_uses_resume_after_fix(self):+ """Scenario Q: SDK T2+ uses --resume after mode-switch T1 set session_id.++ With the mode-switch fix, T1 sets session_id → CLI writes session file →+ T2 restores the session → use_resume=True. _build_query_message must+ return the bare message (--resume supplies context via native session).+ """+ # T2: 4 baseline turns + 1 SDK turn already recorded.+ session = _make_session(+ _msgs(+ ("user", "baseline-q1"),+ ("assistant", "baseline-a1"),+ ("user", "baseline-q2"),+ ("assistant", "baseline-a2"),+ ("user", "sdk-q1"),+ ("assistant", "sdk-a1"),+ ("user", "sdk-q2"), # current SDK T2 message+ )+ )++ # transcript_msg_count=6 covers all prior messages → no gap.+ result, compacted = await _build_query_message(+ "sdk-q2",+ session,+ use_resume=True, # T2: --resume works after T1 set session_id+ transcript_msg_count=6,+ session_id="s",+ )++ # --resume has full context — bare message only.+ assert result == "sdk-q2"+ assert compacted is False++ @pytest.mark.asyncio+ async def test_mode_switch_t1_compresses_all_baseline_turns(self, monkeypatch):+ """_compress_messages is called with ALL prior baseline messages.++ There is exactly one compression call containing all 4 baseline messages+ — not just the 2 post-transcript-end messages.+ """+ session = _make_session(+ _msgs(+ ("user", "baseline-q1"),+ ("assistant", "baseline-a1"),+ ("user", "baseline-q2"),+ ("assistant", "baseline-a2"),+ ("user", "sdk-q1"),+ )+ )+ compressed_batches: list[list] = []++ async def _mock_compress(msgs, target_tokens=None):+ compressed_batches.append(list(msgs))+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ await _build_query_message(+ "sdk-q1",+ session,+ use_resume=False,+ transcript_msg_count=4,+ session_id="s",+ )++ # Exactly one compression call, with all 4 prior messages.+ assert len(compressed_batches) == 1+ assert len(compressed_batches[0]) == 4+++# ---------------------------------------------------------------------------+# Scenario S — SDK → Fast: baseline loads SDK-written transcript+# ---------------------------------------------------------------------------+++class TestSdkToFastModeSwitch:+ """Fast mode turn after N SDK (extended_thinking) turns.++ The transcript written by SDK mode uses the same JSONL format as the one+ written by baseline mode (both go through ``TranscriptBuilder``).+ ``_load_prior_transcript`` must accept it and mark the prefix as covered.+ """++ @pytest.mark.asyncio+ async def test_scenario_s_baseline_loads_sdk_transcript(self):+ """Scenario S: SDK-written transcript is accepted by baseline's load helper."""+ from backend.copilot.baseline.service import _load_prior_transcript+ from backend.copilot.transcript import STOP_REASON_END_TURN, TranscriptDownload+ from backend.copilot.transcript_builder import TranscriptBuilder++ # Build a minimal valid transcript as SDK mode would write it.+ # SDK uses append_user / append_assistant on TranscriptBuilder.+ builder_sdk = TranscriptBuilder()+ builder_sdk.append_user(content="sdk-question")+ builder_sdk.append_assistant(+ content_blocks=[{"type": "text", "text": "sdk-answer"}],+ model="claude-sonnet-4",+ stop_reason=STOP_REASON_END_TURN,+ )+ sdk_transcript = builder_sdk.to_jsonl()++ # Baseline session now has those 2 SDK messages + 1 new baseline message.+ download = TranscriptDownload(content=sdk_transcript, message_count=2)++ baseline_builder = TranscriptBuilder()+ with patch(+ "backend.copilot.baseline.service.download_transcript",+ new=AsyncMock(return_value=download),+ ):+ covers = await _load_prior_transcript(+ user_id="user-1",+ session_id="session-1",+ session_msg_count=3, # 2 SDK + 1 new baseline+ transcript_builder=baseline_builder,+ )++ # Transcript is valid and covers the prefix.+ assert covers is True+ assert baseline_builder.entry_count == 2++ @pytest.mark.asyncio+ async def test_scenario_s_stale_sdk_transcript_not_loaded(self):+ """Scenario S (stale): SDK transcript is stale — baseline does not load it.++ If SDK mode produced more turns than the transcript captured (e.g.+ upload failed on one turn), the baseline rejects the stale transcript+ to avoid injecting an incomplete history.+ """+ from backend.copilot.baseline.service import _load_prior_transcript+ from backend.copilot.transcript import STOP_REASON_END_TURN, TranscriptDownload+ from backend.copilot.transcript_builder import TranscriptBuilder++ builder_sdk = TranscriptBuilder()+ builder_sdk.append_user(content="sdk-question")+ builder_sdk.append_assistant(+ content_blocks=[{"type": "text", "text": "sdk-answer"}],+ model="claude-sonnet-4",+ stop_reason=STOP_REASON_END_TURN,+ )+ sdk_transcript = builder_sdk.to_jsonl()++ # Transcript covers only 2 messages but session has 10 (many SDK turns).+ download = TranscriptDownload(content=sdk_transcript, message_count=2)++ baseline_builder = TranscriptBuilder()+ with patch(+ "backend.copilot.baseline.service.download_transcript",+ new=AsyncMock(return_value=download),+ ):+ covers = await _load_prior_transcript(+ user_id="user-1",+ session_id="session-1",+ session_msg_count=10,+ transcript_builder=baseline_builder,+ )++ # Stale transcript must be rejected.+ assert covers is False+ assert baseline_builder.is_emptydiff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 3b655ffd1b..0edf22b5e2 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -2634,13 +2634,19 @@ async def stream_chat_completion_sdk(# --session-id here. CLI >=2.1.97 rejects the combination of# --session-id + --resume unless --fork-session is also given.sdk_options_kwargs["resume"] = resume_file- elif not has_history:- # T1 only: write CLI native session to a predictable path so- # upload_cli_session() can find it after the turn completes.- # On T2+ without --resume the T1 session file already exists at- # that path; passing --session-id again would fail with- # "Session ID already in use". The upload guard also skips T2+- # no-resume turns, so --session-id provides no benefit there.+ else:+ # Set session_id whenever NOT resuming so the CLI writes the+ # native session file to a predictable path for+ # upload_cli_session() after the turn. This covers:+ # • T1 fresh: no prior history, first SDK turn.+ # • Mode-switch T1: has_history=True (prior baseline turns in+ # DB) but no CLI session file was ever uploaded — the CLI has+ # never been invoked with this session_id before.+ # • T2+ without --resume (restore failed): no session file was+ # restored to local storage (restore_cli_session returned+ # False), so no conflict with an existing file.+ # When --resume is active the session_id is already implied by+ # the resume file; passing it again would be rejected by the CLI.sdk_options_kwargs["session_id"] = session_id# Optional explicit Claude Code CLI binary path (decouples the# bundled SDK version from the CLI version we run — needed because@@ -2838,9 +2844,12 @@ async def stream_chat_completion_sdk(if ctx.use_resume and ctx.resume_file:sdk_options_kwargs_retry["resume"] = ctx.resume_filesdk_options_kwargs_retry.pop("session_id", None)- elif not has_history:- # T1 retry: keep session_id so the CLI writes to the- # predictable path for upload_cli_session().+ elif "session_id" in sdk_options_kwargs:+ # Initial invocation used session_id (T1 or mode-switch+ # T1): keep it so the CLI writes the session file to the+ # predictable path for upload_cli_session(). Storage is+ # ephemeral per invocation, so no "Session ID already in+ # use" conflict occurs — no prior file was restored.sdk_options_kwargs_retry.pop("resume", None)sdk_options_kwargs_retry["session_id"] = session_idelse:diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.pyindex 9d8b4bb135..470858dc55 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py@@ -410,7 +410,14 @@ class TestTokenUsageNullSafety:"""def _apply_usage(self, usage: dict, acc: _TokenUsage) -> None:- """Mirror the production accumulation in sdk/service.py."""+ """Null-safe accumulation: ``or 0`` treats missing/None as zero.++ Uses ``usage.get("key") or 0`` rather than ``usage.get("key", 0)``+ because the latter returns ``None`` when the key exists with a null+ value, which would raise ``TypeError`` on ``int += None``. This is+ the intentional pattern that fixes the OpenRouter initial-stream-event+ bug described in the class docstring.+ """acc.prompt_tokens += usage.get("input_tokens") or 0acc.cache_read_tokens += usage.get("cache_read_input_tokens") or 0acc.cache_creation_tokens += usage.get("cache_creation_input_tokens") or 0@@ -477,3 +484,132 @@ class TestTokenUsageNullSafety:assert acc.cache_read_tokens == 16600assert acc.cache_creation_tokens == 512assert acc.completion_tokens == 349+++# ---------------------------------------------------------------------------+# session_id / resume selection logic+# ---------------------------------------------------------------------------+++def _build_sdk_options(+ use_resume: bool,+ resume_file: str | None,+ session_id: str,+) -> dict:+ """Mirror the session_id/resume selection in stream_chat_completion_sdk.++ This helper encodes the exact branching so the unit tests stay in sync+ with the production code without needing to invoke the full generator.+ """+ kwargs: dict = {}+ if use_resume and resume_file:+ kwargs["resume"] = resume_file+ else:+ kwargs["session_id"] = session_id+ return kwargs+++def _build_retry_sdk_options(+ initial_kwargs: dict,+ ctx_use_resume: bool,+ ctx_resume_file: str | None,+ session_id: str,+) -> dict:+ """Mirror the retry branch in stream_chat_completion_sdk."""+ retry: dict = dict(initial_kwargs)+ if ctx_use_resume and ctx_resume_file:+ retry["resume"] = ctx_resume_file+ retry.pop("session_id", None)+ elif "session_id" in initial_kwargs:+ retry.pop("resume", None)+ retry["session_id"] = session_id+ else:+ retry.pop("resume", None)+ retry.pop("session_id", None)+ return retry+++class TestSdkSessionIdSelection:+ """Verify that session_id is set for all non-resume turns.++ Regression test for the mode-switch T1 bug: when a user switches from+ baseline mode (fast) to SDK mode (extended_thinking) mid-session, the+ first SDK turn has has_history=True but no CLI session file. The old+ code gated session_id on ``not has_history``, so mode-switch T1 never+ got a session_id — the CLI used a random ID that couldn't be found on+ the next turn, causing --resume to fail for the whole session.+ """++ SESSION_ID = "sess-abc123"++ def test_t1_fresh_sets_session_id(self):+ """T1 of a fresh session always gets session_id."""+ opts = _build_sdk_options(+ use_resume=False,+ resume_file=None,+ session_id=self.SESSION_ID,+ )+ assert opts.get("session_id") == self.SESSION_ID+ assert "resume" not in opts++ def test_mode_switch_t1_sets_session_id(self):+ """Mode-switch T1 (has_history=True, no CLI session) gets session_id.++ Before the fix, the ``elif not has_history`` guard prevented this+ case from setting session_id, causing all subsequent turns to run+ without --resume.+ """+ # Mode-switch T1: use_resume=False (no prior CLI session) and+ # has_history=True (prior baseline turns in DB). The old code+ # (``elif not has_history``) silently skipped this case.+ opts = _build_sdk_options(+ use_resume=False,+ resume_file=None,+ session_id=self.SESSION_ID,+ )+ assert opts.get("session_id") == self.SESSION_ID+ assert "resume" not in opts++ def test_t2_with_resume_uses_resume(self):+ """T2+ with a restored CLI session uses --resume, not session_id."""+ opts = _build_sdk_options(+ use_resume=True,+ resume_file=self.SESSION_ID,+ session_id=self.SESSION_ID,+ )+ assert opts.get("resume") == self.SESSION_ID+ assert "session_id" not in opts++ def test_t2_without_resume_sets_session_id(self):+ """T2+ when restore failed still gets session_id (no prior file on disk)."""+ opts = _build_sdk_options(+ use_resume=False,+ resume_file=None,+ session_id=self.SESSION_ID,+ )+ assert opts.get("session_id") == self.SESSION_ID+ assert "resume" not in opts++ def test_retry_keeps_session_id_for_t1(self):+ """Retry for T1 (or mode-switch T1) preserves session_id."""+ initial = _build_sdk_options(False, None, self.SESSION_ID)+ retry = _build_retry_sdk_options(initial, False, None, self.SESSION_ID)+ assert retry.get("session_id") == self.SESSION_ID+ assert "resume" not in retry++ def test_retry_removes_session_id_for_t2_plus(self):+ """Retry for T2+ (initial used --resume) removes session_id to avoid conflict."""+ initial = _build_sdk_options(True, self.SESSION_ID, self.SESSION_ID)+ # T2+ retry where context reduction dropped --resume+ retry = _build_retry_sdk_options(initial, False, None, self.SESSION_ID)+ assert "session_id" not in retry+ assert "resume" not in retry++ def test_retry_t2_with_resume_sets_resume(self):+ """Retry that still uses --resume keeps --resume and drops session_id."""+ initial = _build_sdk_options(True, self.SESSION_ID, self.SESSION_ID)+ retry = _build_retry_sdk_options(+ initial, True, self.SESSION_ID, self.SESSION_ID+ )+ assert retry.get("resume") == self.SESSION_ID+ assert "session_id" not in retry