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(backend/copilot): robust context fallback — upload gate, gap-fill, token-budget compression (#12782)
Zamil Majdy55869d3c7520
diff --git a/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.py b/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.pynew file mode 100644index 0000000000..5b99296314--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/context_fallback_test.py@@ -0,0 +1,555 @@+"""Tests for context fallback paths introduced in fix/copilot-transcript-resume-gate.++Scenario table+==============++| # | use_resume | transcript_msg_count | gap | target_tokens | Expected output |+|---|------------|----------------------|---------|---------------|--------------------------------------------|+| A | True | covers all | empty | None | bare message (--resume has full context) |+| B | True | stale | 2 msgs | None | gap context prepended |+| C | True | stale | 2 msgs | 50_000 | gap compressed to budget, prepended |+| D | False | 0 | N/A | None | full session compressed, prepended |+| E | False | 0 | N/A | 50_000 | full session compressed to budget |+| F | False | 2 (partial) | 2 msgs | None | full session compressed (not just gap; |+| | | | | | CLI has zero context without --resume) |+| G | False | 2 (partial) | 2 msgs | 50_000 | full session compressed to budget |+| H | False | covers all | empty | None | full session compressed |+| | | | | | (NOT bare message — the bug that was fixed)|+| I | False | covers all | empty | 50_000 | full session compressed to tight budget |+| J | False | 2 (partial) | n/a | None | exactly ONE compression call (full prior) |++Compression unit tests+=======================++| # | Input | target_tokens | Expected |+|---|----------------------|---------------|-----------------------------------------------|+| K | [] | None | ([], False) — empty guard |+| L | [1 msg] | None | ([msg], False) — single-msg guard |+| M | [2+ msgs] | None | target_tokens=None forwarded to _run_compression |+| N | [2+ msgs] | 30_000 | target_tokens=30_000 forwarded |+| O | [2+ msgs], run fails | None | returns originals, False |+"""++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, _compress_messages+from backend.util.prompt import CompressResult++# ---------------------------------------------------------------------------+# 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]+++def _passthrough_compress(target_tokens=None):+ """Return a mock that passes messages through and records its call args."""+ calls: list[tuple[list, int | None]] = []++ async def _mock(msgs, tok=None):+ calls.append((msgs, tok))+ return msgs, False++ _mock.calls = calls # type: ignore[attr-defined]+ return _mock+++# ---------------------------------------------------------------------------+# _build_query_message — scenario A–J+# ---------------------------------------------------------------------------+++class TestBuildQueryMessageResume:+ """use_resume=True paths (--resume supplies history; only inject gap if stale)."""++ @pytest.mark.asyncio+ async def test_scenario_a_transcript_current_returns_bare_message(self):+ """Scenario A: --resume covers full context → no prefix injected."""+ session = _make_session(+ _msgs(("user", "q1"), ("assistant", "a1"), ("user", "q2"))+ )+ result, compacted = await _build_query_message(+ "q2", session, use_resume=True, transcript_msg_count=2, session_id="s"+ )+ assert result == "q2"+ assert compacted is False++ @pytest.mark.asyncio+ async def test_scenario_b_stale_transcript_injects_gap(self, monkeypatch):+ """Scenario B: stale transcript → gap context prepended."""+ session = _make_session(+ _msgs(+ ("user", "q1"),+ ("assistant", "a1"),+ ("user", "q2"),+ ("assistant", "a2"),+ ("user", "q3"),+ )+ )++ async def _mock_compress(msgs, target_tokens=None):+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ result, compacted = await _build_query_message(+ "q3", session, use_resume=True, transcript_msg_count=2, session_id="s"+ )+ assert "<conversation_history>" in result+ assert "q2" in result+ assert "a2" in result+ assert "Now, the user says:\nq3" in result+ # q1/a1 are covered by the transcript — must NOT appear in gap context+ assert "q1" not in result++ @pytest.mark.asyncio+ async def test_scenario_c_stale_transcript_passes_target_tokens(self, monkeypatch):+ """Scenario C: target_tokens is forwarded to _compress_messages for the gap."""+ session = _make_session(+ _msgs(+ ("user", "q1"),+ ("assistant", "a1"),+ ("user", "q2"),+ ("assistant", "a2"),+ ("user", "q3"),+ )+ )+ captured: list[int | None] = []++ async def _mock_compress(msgs, target_tokens=None):+ captured.append(target_tokens)+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ await _build_query_message(+ "q3",+ session,+ use_resume=True,+ transcript_msg_count=2,+ session_id="s",+ target_tokens=50_000,+ )+ assert captured == [50_000]+++class TestBuildQueryMessageNoResumeNoTranscript:+ """use_resume=False, transcript_msg_count=0 — full session compressed."""++ @pytest.mark.asyncio+ async def test_scenario_d_full_session_compressed(self, monkeypatch):+ """Scenario D: no resume, no transcript → compress all prior messages."""+ session = _make_session(+ _msgs(("user", "q1"), ("assistant", "a1"), ("user", "q2"))+ )++ async def _mock_compress(msgs, target_tokens=None):+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ result, compacted = await _build_query_message(+ "q2", session, use_resume=False, transcript_msg_count=0, session_id="s"+ )+ assert "<conversation_history>" in result+ assert "q1" in result+ assert "a1" in result+ assert "Now, the user says:\nq2" in result++ @pytest.mark.asyncio+ async def test_scenario_e_passes_target_tokens_to_compression(self, monkeypatch):+ """Scenario E: target_tokens forwarded to _compress_messages."""+ session = _make_session(+ _msgs(("user", "q1"), ("assistant", "a1"), ("user", "q2"))+ )+ captured: list[int | None] = []++ async def _mock_compress(msgs, target_tokens=None):+ captured.append(target_tokens)+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ await _build_query_message(+ "q2",+ session,+ use_resume=False,+ transcript_msg_count=0,+ session_id="s",+ target_tokens=15_000,+ )+ assert captured == [15_000]+++class TestBuildQueryMessageNoResumeWithTranscript:+ """use_resume=False, transcript_msg_count > 0 — gap or full-session fallback."""++ @pytest.mark.asyncio+ async def test_scenario_f_no_resume_always_injects_full_session(self, monkeypatch):+ """Scenario F: use_resume=False with transcript_msg_count > 0 still injects+ the FULL prior session — not just the gap since the transcript end.++ When there is no --resume the CLI starts with zero context, so injecting+ only the post-transcript gap would silently drop all transcript-covered+ history. The correct fix is to always compress the full session.+ """+ session = _make_session(+ _msgs(+ ("user", "q1"), # transcript_msg_count=2 covers these+ ("assistant", "a1"),+ ("user", "q2"), # post-transcript gap starts here+ ("assistant", "a2"),+ ("user", "q3"), # current message+ )+ )+ compressed_msgs: list[list] = []++ async def _mock_compress(msgs, target_tokens=None):+ compressed_msgs.append(list(msgs))+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ result, _ = await _build_query_message(+ "q3",+ session,+ use_resume=False,+ transcript_msg_count=2, # transcript covers q1/a1 but no --resume+ session_id="s",+ )+ assert "<conversation_history>" in result+ # Full session must be injected — transcript-covered turns ARE included+ assert "q1" in result+ assert "a1" in result+ assert "q2" in result+ assert "a2" in result+ assert "Now, the user says:\nq3" in result+ # Compressed exactly once with all 4 prior messages+ assert len(compressed_msgs) == 1+ assert len(compressed_msgs[0]) == 4++ @pytest.mark.asyncio+ async def test_scenario_g_no_resume_passes_target_tokens(self, monkeypatch):+ """Scenario G: target_tokens forwarded when use_resume=False + transcript_msg_count > 0."""+ session = _make_session(+ _msgs(+ ("user", "q1"),+ ("assistant", "a1"),+ ("user", "q2"),+ ("assistant", "a2"),+ ("user", "q3"),+ )+ )+ captured: list[int | None] = []++ async def _mock_compress(msgs, target_tokens=None):+ captured.append(target_tokens)+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ await _build_query_message(+ "q3",+ session,+ use_resume=False,+ transcript_msg_count=2,+ session_id="s",+ target_tokens=50_000,+ )+ assert captured == [50_000]++ @pytest.mark.asyncio+ async def test_scenario_h_no_resume_transcript_current_injects_full_session(+ self, monkeypatch+ ):+ """Scenario H: the bug that was fixed.++ Old code path: use_resume=False, transcript_msg_count covers all prior+ messages → gap sub-path: gap = [] → ``return current_message, False``+ → model received ZERO context (bare message only).++ New code path: use_resume=False always compresses the full prior session+ regardless of transcript_msg_count — model always gets context.+ """+ session = _make_session(+ _msgs(+ ("user", "q1"),+ ("assistant", "a1"),+ ("user", "q2"),+ ("assistant", "a2"),+ ("user", "q3"),+ )+ )++ 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(+ "q3",+ session,+ use_resume=False,+ transcript_msg_count=4, # covers ALL prior → old code returned bare msg+ session_id="s",+ )+ # NEW: must inject full session, NOT return bare message+ assert result != "q3"+ assert "<conversation_history>" in result+ assert "q1" in result+ assert "Now, the user says:\nq3" in result++ @pytest.mark.asyncio+ async def test_scenario_i_no_resume_target_tokens_forwarded_any_transcript_count(+ self, monkeypatch+ ):+ """Scenario I: target_tokens forwarded even when transcript_msg_count covers all."""+ session = _make_session(+ _msgs(("user", "q1"), ("assistant", "a1"), ("user", "q2"))+ )+ captured: list[int | None] = []++ async def _mock_compress(msgs, target_tokens=None):+ captured.append(target_tokens)+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ await _build_query_message(+ "q2",+ session,+ use_resume=False,+ transcript_msg_count=2,+ session_id="s",+ target_tokens=15_000,+ )+ assert 15_000 in captured++ @pytest.mark.asyncio+ async def test_scenario_j_no_resume_single_compression_call(self, monkeypatch):+ """Scenario J: use_resume=False always makes exactly ONE compression call+ (the full session), regardless of transcript coverage.++ This verifies there is no two-step gap+fallback pattern for no-resume —+ compression is called once with the full prior session.+ """+ session = _make_session(+ _msgs(+ ("user", "q1"),+ ("assistant", "a1"),+ ("user", "q2"),+ ("assistant", "a2"),+ ("user", "q3"),+ )+ )+ call_count = 0++ async def _mock_compress(msgs, target_tokens=None):+ nonlocal call_count+ call_count += 1+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages", _mock_compress+ )++ await _build_query_message(+ "q3",+ session,+ use_resume=False,+ transcript_msg_count=2,+ session_id="s",+ )+ assert call_count == 1+++# ---------------------------------------------------------------------------+# _compress_messages — unit tests K–O+# ---------------------------------------------------------------------------+++class TestCompressMessages:+ @pytest.mark.asyncio+ async def test_scenario_k_empty_list_returns_empty(self):+ """Scenario K: empty input → short-circuit, no compression."""+ result, compacted = await _compress_messages([])+ assert result == []+ assert compacted is False++ @pytest.mark.asyncio+ async def test_scenario_l_single_message_returns_as_is(self):+ """Scenario L: single message → short-circuit (< 2 guard)."""+ msg = ChatMessage(role="user", content="hello")+ result, compacted = await _compress_messages([msg])+ assert result == [msg]+ assert compacted is False++ @pytest.mark.asyncio+ async def test_scenario_m_target_tokens_none_forwarded(self):+ """Scenario M: target_tokens=None forwarded to _run_compression."""+ msgs = [+ ChatMessage(role="user", content="q"),+ ChatMessage(role="assistant", content="a"),+ ]+ fake_result = CompressResult(+ messages=[+ {"role": "user", "content": "q"},+ {"role": "assistant", "content": "a"},+ ],+ token_count=10,+ was_compacted=False,+ original_token_count=10,+ )+ with patch(+ "backend.copilot.sdk.service._run_compression",+ new_callable=AsyncMock,+ return_value=fake_result,+ ) as mock_run:+ await _compress_messages(msgs, target_tokens=None)++ mock_run.assert_awaited_once()+ _, kwargs = mock_run.call_args+ assert kwargs.get("target_tokens") is None++ @pytest.mark.asyncio+ async def test_scenario_n_explicit_target_tokens_forwarded(self):+ """Scenario N: explicit target_tokens forwarded to _run_compression."""+ msgs = [+ ChatMessage(role="user", content="q"),+ ChatMessage(role="assistant", content="a"),+ ]+ fake_result = CompressResult(+ messages=[{"role": "user", "content": "summary"}],+ token_count=5,+ was_compacted=True,+ original_token_count=50,+ )+ with patch(+ "backend.copilot.sdk.service._run_compression",+ new_callable=AsyncMock,+ return_value=fake_result,+ ) as mock_run:+ result, compacted = await _compress_messages(msgs, target_tokens=30_000)++ mock_run.assert_awaited_once()+ _, kwargs = mock_run.call_args+ assert kwargs.get("target_tokens") == 30_000+ assert compacted is True++ @pytest.mark.asyncio+ async def test_scenario_o_run_compression_exception_returns_originals(self):+ """Scenario O: _run_compression raises → return original messages, False."""+ msgs = [+ ChatMessage(role="user", content="q"),+ ChatMessage(role="assistant", content="a"),+ ]+ with patch(+ "backend.copilot.sdk.service._run_compression",+ new_callable=AsyncMock,+ side_effect=RuntimeError("compression timeout"),+ ):+ result, compacted = await _compress_messages(msgs)++ assert result == msgs+ assert compacted is False++ @pytest.mark.asyncio+ async def test_compaction_messages_filtered_before_compression(self):+ """filter_compaction_messages is applied before _run_compression is called."""+ # A compaction message is one with role=assistant and specific content pattern.+ # We verify that only real messages reach _run_compression.+ from backend.copilot.sdk.service import filter_compaction_messages++ msgs = [+ ChatMessage(role="user", content="q"),+ ChatMessage(role="assistant", content="a"),+ ]+ # filter_compaction_messages should not remove these plain messages+ filtered = filter_compaction_messages(msgs)+ assert len(filtered) == len(msgs)+++# ---------------------------------------------------------------------------+# target_tokens threading — _retry_target_tokens values match expectations+# ---------------------------------------------------------------------------+++class TestRetryTargetTokens:+ def test_first_retry_uses_first_slot(self):+ from backend.copilot.sdk.service import _RETRY_TARGET_TOKENS++ assert _RETRY_TARGET_TOKENS[0] == 50_000++ def test_second_retry_uses_second_slot(self):+ from backend.copilot.sdk.service import _RETRY_TARGET_TOKENS++ assert _RETRY_TARGET_TOKENS[1] == 15_000++ def test_second_slot_smaller_than_first(self):+ from backend.copilot.sdk.service import _RETRY_TARGET_TOKENS++ assert _RETRY_TARGET_TOKENS[1] < _RETRY_TARGET_TOKENS[0]+++# ---------------------------------------------------------------------------+# Single-message session edge cases+# ---------------------------------------------------------------------------+++class TestSingleMessageSessions:+ @pytest.mark.asyncio+ async def test_no_resume_single_message_returns_bare(self):+ """First turn (1 message): no prior history to inject."""+ session = _make_session([ChatMessage(role="user", content="hello")])+ result, compacted = await _build_query_message(+ "hello", session, use_resume=False, transcript_msg_count=0, session_id="s"+ )+ assert result == "hello"+ assert compacted is False++ @pytest.mark.asyncio+ async def test_resume_single_message_returns_bare(self):+ """First turn with resume flag: transcript is empty so no gap."""+ session = _make_session([ChatMessage(role="user", content="hello")])+ result, compacted = await _build_query_message(+ "hello", session, use_resume=True, transcript_msg_count=0, session_id="s"+ )+ assert result == "hello"+ assert compacted is Falsediff --git a/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py b/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.pyindex 57f037baba..a6e88889c3 100644--- a/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/query_builder_test.py@@ -6,6 +6,7 @@ import pytestfrom backend.copilot.model import ChatMessage, ChatSessionfrom backend.copilot.sdk.service import (+ _BARE_MESSAGE_TOKEN_FLOOR,_build_query_message,_format_conversation_context,)@@ -130,6 +131,34 @@ async def test_build_query_resume_up_to_date():assert was_compacted is False+async def test_build_query_resume_misaligned_watermark():+ """With --resume and watermark pointing at a user message, skip gap."""+ # Simulates a deleted message shifting DB positions so the watermark+ # lands on a user turn instead of the expected assistant turn.+ session = _make_session(+ [+ ChatMessage(role="user", content="turn 1"),+ ChatMessage(role="assistant", content="reply 1"),+ ChatMessage(+ role="user", content="turn 2"+ ), # ← watermark points here (role=user)+ ChatMessage(role="assistant", content="reply 2"),+ ChatMessage(role="user", content="turn 3"),+ ]+ )+ result, was_compacted = await _build_query_message(+ "turn 3",+ session,+ use_resume=True,+ transcript_msg_count=3, # prior[2].role == "user" — misaligned+ session_id="test-session",+ )+ # Misaligned watermark → skip gap, return bare message+ assert result == "turn 3"+ assert was_compacted is False++@pytest.mark.asyncioasync def test_build_query_resume_stale_transcript():"""With --resume and stale transcript, gap context is prepended."""@@ -204,7 +233,7 @@ async def test_build_query_no_resume_multi_message(monkeypatch):)# Mock _compress_messages to return the messages as-is- async def _mock_compress(msgs):+ async def _mock_compress(msgs, target_tokens=None):return msgs, Falsemonkeypatch.setattr(@@ -237,7 +266,7 @@ async def test_build_query_no_resume_multi_message_compacted(monkeypatch):])- async def _mock_compress(msgs):+ async def _mock_compress(msgs, target_tokens=None):return msgs, True # Simulate actual compactionmonkeypatch.setattr(@@ -253,3 +282,85 @@ async def test_build_query_no_resume_multi_message_compacted(monkeypatch):session_id="test-session",)assert was_compacted is True+++async def test_build_query_no_resume_at_token_floor():+ """When target_tokens is at or below the floor, return bare message.++ This is the final escape hatch: if the retry budget is exhausted and+ even the most aggressive compression might not fit, skip history+ injection entirely so the user always gets a response.+ """+ session = _make_session(+ [+ ChatMessage(role="user", content="old question"),+ ChatMessage(role="assistant", content="old answer"),+ ChatMessage(role="user", content="new question"),+ ]+ )+ result, was_compacted = await _build_query_message(+ "new question",+ session,+ use_resume=False,+ transcript_msg_count=0,+ session_id="test-session",+ target_tokens=_BARE_MESSAGE_TOKEN_FLOOR,+ )+ # At the floor threshold, no history is injected+ assert result == "new question"+ assert was_compacted is False+++async def test_build_query_no_resume_below_token_floor():+ """target_tokens strictly below floor also returns bare message."""+ session = _make_session(+ [+ ChatMessage(role="user", content="old"),+ ChatMessage(role="assistant", content="reply"),+ ChatMessage(role="user", content="new"),+ ]+ )+ result, was_compacted = await _build_query_message(+ "new",+ session,+ use_resume=False,+ transcript_msg_count=0,+ session_id="test-session",+ target_tokens=_BARE_MESSAGE_TOKEN_FLOOR - 1,+ )+ assert result == "new"+ assert was_compacted is False+++async def test_build_query_no_resume_above_token_floor_compresses(monkeypatch):+ """target_tokens just above the floor still triggers compression."""+ session = _make_session(+ [+ ChatMessage(role="user", content="old"),+ ChatMessage(role="assistant", content="reply"),+ ChatMessage(role="user", content="new"),+ ]+ )++ async def _mock_compress(msgs, target_tokens=None):+ return msgs, False++ monkeypatch.setattr(+ "backend.copilot.sdk.service._compress_messages",+ _mock_compress,+ )++ result, was_compacted = await _build_query_message(+ "new",+ session,+ use_resume=False,+ transcript_msg_count=0,+ session_id="test-session",+ target_tokens=_BARE_MESSAGE_TOKEN_FLOOR + 1,+ )+ # Above the floor → history is injected (not the bare message)+ assert "<conversation_history>" in result+ assert "Now, the user says:\nnew" in resultdiff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex f291d96431..f4aa019b08 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -261,6 +261,11 @@ class ReducedContext(NamedTuple):resume_file: str | Nonetranscript_lost: booltried_compaction: bool+ # Token budget for history compression on the DB-message fallback path.+ # None means "use model-aware default". Halved on each retry so+ # compress_context applies progressively more aggressive reduction+ # (LLM summarize → content truncate → middle-out delete → first/last trim).+ target_tokens: int | None = None@dataclass@@ -305,6 +310,10 @@ class _RetryState:adapter: SDKResponseAdaptertranscript_builder: TranscriptBuilderusage: _TokenUsage+ # Token budget for history compression on retries (DB-message fallback path).+ # None = model-aware default. Halved each retry for progressively more+ # aggressive compression (LLM summarize → truncate → middle-out → trim).+ target_tokens: int | None = None@dataclass@@ -336,12 +345,34 @@ class _StreamContext:lock: AsyncClusterLock+# Per-retry token budgets for the no-transcript (use_resume=False) path.+# When there is no CLI native session to --resume, context is built from DB+# messages via _format_conversation_context. For large sessions this text+# can exceed the model context window; each retry halves the token budget so+# compress_context applies progressively more aggressive reduction:+# LLM summarize → content truncate → middle-out delete → first/last trim.+# Index 0 = first retry, 1 = second retry; last value applies beyond that.+_RETRY_TARGET_TOKENS: tuple[int, ...] = (50_000, 15_000)++# Below this token budget the model context is so tight that injecting any+# conversation history would likely exceed the limit regardless of content.+# _build_query_message returns the bare message when target_tokens falls to+# or below this floor, giving the user a response instead of a hard error.+_BARE_MESSAGE_TOKEN_FLOOR: int = 5_000++# Tight token budget for seeding the transcript builder on turns where no+# CLI native session exists. Kept below _RETRY_TARGET_TOKENS[0] so the+# seeded JSONL upload stays compact and future gap injections are small.+_SEED_TARGET_TOKENS: int = 30_000++async def _reduce_context(transcript_content: str,tried_compaction: bool,session_id: str,sdk_cwd: str,log_prefix: str,+ attempt: int = 1,) -> ReducedContext:"""Prepare reduced context for a retry attempt.@@ -349,9 +380,19 @@ async def _reduce_context(On subsequent retries (or if compaction fails), drops the transcriptentirely so the query is rebuilt from DB messages only.- `transcript_lost` is True when the transcript was dropped (caller- should set `skip_transcript_upload`).+ When no transcript is available (use_resume=False fallback path), returns+ a decreasing ``target_tokens`` budget so ``compress_context`` applies+ progressively more aggressive reduction (LLM summarize → content truncate+ → middle-out delete → first/last trim). The budget applies in+ ``_build_query_message`` and is halved on each retry.++ ``transcript_lost`` is True when the transcript was dropped (caller+ should set ``skip_transcript_upload``)."""+ # Token budget for the DB fallback on this attempt (no-transcript path).+ idx = max(0, attempt - 1)+ retry_target = _RETRY_TARGET_TOKENS[min(idx, len(_RETRY_TARGET_TOKENS) - 1)]+# First retry: try compacting our transcript builder state.# Note: the CLI native --resume file is not updated with the compacted# content (it would require emitting CLI-native JSONL format), so the@@ -375,9 +416,14 @@ async def _reduce_context(return ReducedContext(tb, False, None, False, True)logger.warning("%s Compaction failed, dropping transcript", log_prefix)- # Subsequent retry or compaction failed: drop transcript entirely- logger.warning("%s Dropping transcript, rebuilding from DB messages", log_prefix)- return ReducedContext(TranscriptBuilder(), False, None, True, True)+ # Subsequent retry or compaction failed: drop transcript entirely.+ # Return retry_target so the caller compresses DB messages to that budget.+ logger.warning(+ "%s Dropping transcript, rebuilding from DB messages" " (target_tokens=%d)",+ log_prefix,+ retry_target,+ )+ return ReducedContext(TranscriptBuilder(), False, None, True, True, retry_target)def _append_error_marker(@@ -830,6 +876,7 @@ def _format_sdk_content_blocks(blocks: list) -> list[dict[str, Any]]:async def _compress_messages(messages: list[ChatMessage],+ target_tokens: int | None = None,) -> tuple[list[ChatMessage], bool]:"""Compress a list of messages if they exceed the token threshold.@@ -838,6 +885,10 @@ async def _compress_messages(`_compress_messages` and `compact_transcript` share this helper soclient acquisition and error handling are consistent.+ ``target_tokens`` sets a hard ceiling for the compressed output so+ callers can enforce a tighter budget on retries. When ``None``,+ ``compress_context`` uses the model-aware default.+See also:`_run_compression` — shared compression with timeout guards.`compact_transcript` — compresses JSONL transcript entries.@@ -861,7 +912,9 @@ async def _compress_messages(messages_dict.append(msg_dict)try:- result = await _run_compression(messages_dict, config.model, "[SDK]")+ result = await _run_compression(+ messages_dict, config.model, "[SDK]", target_tokens=target_tokens+ )except Exception as exc:# Guard against timeouts or unexpected errors in compression —# return the original messages so the caller can proceed without@@ -990,44 +1043,139 @@ async def _build_query_message(use_resume: bool,transcript_msg_count: int,session_id: str,+ target_tokens: int | None = None,) -> tuple[str, bool]:"""Build the query message with appropriate context.+ When ``use_resume=True``, the CLI has the full session via ``--resume``;+ only a gap-fill prefix is injected when the transcript is stale.++ When ``use_resume=False``, the CLI starts a fresh session with no prior+ context, so the full prior session is always compressed and injected via+ ``_format_conversation_context``. ``compress_context`` handles size+ reduction internally (LLM summarize → content truncate → middle-out delete+ → first/last trim). ``target_tokens`` decreases on each retry to force+ progressively more aggressive compression when the first attempt exceeds+ context limits.+Returns:Tuple of (query_message, was_compacted)."""msg_count = len(session.messages)+ prior = session.messages[:-1] # all turns except the current user message++ logger.info(+ "[SDK] [%s] Context path: use_resume=%s, transcript_msg_count=%d,"+ " db_msg_count=%d, target_tokens=%s",+ session_id[:8],+ use_resume,+ transcript_msg_count,+ msg_count,+ target_tokens,+ )if use_resume and transcript_msg_count > 0:if transcript_msg_count < msg_count - 1:- gap = session.messages[transcript_msg_count:-1]- compressed, was_compressed = await _compress_messages(gap)+ # Sanity-check the watermark: the last covered position should be+ # an assistant turn. A user-role message here means the count is+ # misaligned (e.g. a message was deleted and DB positions shifted).+ # Skip the gap rather than injecting wrong context — the CLI session+ # loaded via --resume still has good history.+ if prior[transcript_msg_count - 1].role != "assistant":+ logger.warning(+ "[SDK] [%s] Watermark misaligned: prior[%d].role=%r"+ " (expected 'assistant') — skipping gap to avoid"+ " injecting wrong context (transcript=%d, db=%d)",+ session_id[:8],+ transcript_msg_count - 1,+ prior[transcript_msg_count - 1].role,+ transcript_msg_count,+ msg_count,+ )+ return current_message, False+ gap = prior[transcript_msg_count:]+ compressed, was_compressed = await _compress_messages(gap, target_tokens)gap_context = _format_conversation_context(compressed)if gap_context:logger.info("[SDK] Transcript stale: covers %d of %d messages, "- "gap=%d (compressed=%s)",+ "gap=%d (compressed=%s), gap_context_bytes=%d",transcript_msg_count,msg_count,len(gap),was_compressed,+ len(gap_context),)return (f"{gap_context}\n\nNow, the user says:\n{current_message}",was_compressed,)+ logger.warning(+ "[SDK] [%s] Transcript stale: gap produced empty context"+ " (%d msgs, transcript=%d/%d) — sending message without gap prefix",+ session_id[:8],+ len(gap),+ transcript_msg_count,+ msg_count,+ )+ else:+ logger.info(+ "[SDK] [%s] --resume covers full context (%d messages)",+ session_id[:8],+ transcript_msg_count,+ )+ return current_message, False+elif not use_resume and msg_count > 1:+ # No --resume: the CLI starts a fresh session with no prior context.+ # Injecting only the post-transcript gap would omit the transcript-covered+ # prefix entirely, so always compress the full prior session here.+ # compress_context handles size reduction internally (LLM summarize →+ # content truncate → middle-out delete → first/last trim).++ # Final escape hatch: if the token budget is at or below the floor,+ # the model context is so tight that even fully compressed history+ # would risk a "prompt too long" error. Return the bare message so+ # the user always gets a response rather than a hard failure.+ if target_tokens is not None and target_tokens <= _BARE_MESSAGE_TOKEN_FLOOR:+ logger.warning(+ "[SDK] [%s] target_tokens=%d at or below floor (%d) —"+ " skipping history injection to guarantee response delivery"+ " (session has %d messages)",+ session_id[:8],+ target_tokens,+ _BARE_MESSAGE_TOKEN_FLOOR,+ msg_count,+ )+ return current_message, False+logger.warning(- f"[SDK] Using compression fallback for session "- f"{session_id} ({msg_count} messages) — no transcript for --resume"+ "[SDK] [%s] No --resume for %d-message session — compressing"+ " full session history (pod affinity issue or first turn after"+ " restore failure); target_tokens=%s",+ session_id[:8],+ msg_count,+ target_tokens,)- compressed, was_compressed = await _compress_messages(session.messages[:-1])+ compressed, was_compressed = await _compress_messages(prior, target_tokens)history_context = _format_conversation_context(compressed)if history_context:+ logger.info(+ "[SDK] [%s] Fallback context built: compressed=%s," " context_bytes=%d",+ session_id[:8],+ was_compressed,+ len(history_context),+ )return (f"{history_context}\n\nNow, the user says:\n{current_message}",was_compressed,)+ logger.warning(+ "[SDK] [%s] Fallback context empty after compression"+ " (%d messages) — sending message without history",+ session_id[:8],+ len(prior),+ )return current_message, False@@ -1951,6 +2099,48 @@ async def _run_stream_attempt()+async def _seed_transcript(+ session: ChatSession,+ transcript_builder: TranscriptBuilder,+ transcript_covers_prefix: bool,+ transcript_msg_count: int,+ log_prefix: str,+) -> tuple[str, bool, int]:+ """Seed the transcript builder from compressed DB messages.++ Called when ``use_resume=False`` and no prior transcript exists in storage+ so that ``upload_transcript`` saves a compact version for future turns.+ This ensures the next turn can use the full-session compression path with+ the benefit of an already-compressed baseline, and a restored CLI session+ on the next pod gets a usable compact base even for sessions that started+ on old pods.++ Returns ``(transcript_content, transcript_covers_prefix, transcript_msg_count)``+ updated values — unchanged if seeding is not possible.+ """+ if len(session.messages) <= 1:+ return "", transcript_covers_prefix, transcript_msg_count++ _prior = session.messages[:-1]+ _comp, _ = await _compress_messages(_prior, _SEED_TARGET_TOKENS)+ if not _comp:+ return "", transcript_covers_prefix, transcript_msg_count++ _seeded = _session_messages_to_transcript(_comp)+ if not _seeded or not validate_transcript(_seeded):+ return "", transcript_covers_prefix, transcript_msg_count++ transcript_builder.load_previous(_seeded, log_prefix=log_prefix)+ logger.info(+ "%s Seeded transcript from %d compressed DB messages"+ " for next-turn upload (seed_target_tokens=%d)",+ log_prefix,+ len(_comp),+ _SEED_TARGET_TOKENS,+ )+ return _seeded, True, len(_prior)++async def stream_chat_completion_sdk(session_id: str,message: str | None = None,@@ -2222,9 +2412,20 @@ async def stream_chat_completion_sdk(# Builder loaded but CLI native session not available.# --resume will not be used this turn; upload after turn# will seed the native session for the next turn.+ #+ # Still record transcript_msg_count so _build_query_message+ # can use the transcript-aware gap path (inject only new+ # messages since the transcript end) instead of compressing+ # the full DB history. This avoids prompt-too-long on+ # large sessions where the CLI session is temporarily+ # unavailable (e.g. mixed-version rolling deployment).+ transcript_msg_count = dl.message_countlogger.info(- "%s CLI session not restored — running without --resume this turn",+ "%s CLI session not restored — running without"+ " --resume this turn (transcript_msg_count=%d for"+ " gap-aware fallback)",log_prefix,+ transcript_msg_count,)else:logger.warning("%s Transcript downloaded but invalid", log_prefix)@@ -2460,6 +2661,22 @@ async def stream_chat_completion_sdk(if attachments.hint:query_message = f"{query_message}\n\n{attachments.hint}"+ # When running without --resume and no prior transcript in storage,+ # seed the transcript builder from compressed DB messages so that+ # upload_transcript saves a compact version for future turns.+ if not use_resume and not transcript_content and not skip_transcript_upload:+ (+ transcript_content,+ transcript_covers_prefix,+ transcript_msg_count,+ ) = await _seed_transcript(+ session,+ transcript_builder,+ transcript_covers_prefix,+ transcript_msg_count,+ log_prefix,+ )+tried_compaction = False# Build the per-request context carrier (shared across attempts).@@ -2542,12 +2759,14 @@ async def stream_chat_completion_sdk(session_id,sdk_cwd,log_prefix,+ attempt=attempt,)state.transcript_builder = ctx.builderstate.use_resume = ctx.use_resumestate.resume_file = ctx.resume_filetried_compaction = ctx.tried_compactionstate.transcript_msg_count = 0+ state.target_tokens = ctx.target_tokensif ctx.transcript_lost:skip_transcript_upload = True@@ -2565,7 +2784,6 @@ async def stream_chat_completion_sdk(# T2+ retry without --resume: do not pass --session-id.# The T1 session file already exists at that path; re-using# the same ID would fail with "Session ID already in use".- # The upload guard skips T2+ no-resume turns anyway.sdk_options_kwargs_retry.pop("resume", None)sdk_options_kwargs_retry.pop("session_id", None)# Recompute system_prompt for retry — ctx.use_resume may have@@ -2585,6 +2803,7 @@ async def stream_chat_completion_sdk(state.use_resume,state.transcript_msg_count,session_id,+ target_tokens=state.target_tokens,)if attachments.hint:state.query_message = f"{state.query_message}\n\n{attachments.hint}"@@ -3070,6 +3289,21 @@ async def stream_chat_completion_sdk(# the shielded inner coroutine continues running to completion so the# upload is not lost. This is intentional and matches the pattern# used for upload_transcript immediately above.+ #+ # NOTE: upload is attempted regardless of state.use_resume — even when+ # this turn ran without --resume (restore failed or first T2+ on a new+ # pod), the T1 session file at the expected path may still be present+ # and should be re-uploaded so the next turn can resume from it.+ # upload_cli_session silently skips when the file is absent, so this is+ # always safe.+ #+ # Intentionally NOT gated on skip_transcript_upload: that flag is set+ # when our custom JSONL transcript is dropped (transcript_lost=True on+ # reduced-context retries) but the CLI's native session file is written+ # independently. Blocking CLI upload on transcript_lost would prevent+ # T1 prompt-too-long retries from uploading their valid session file,+ # breaking --resume on the next pod. The ended_with_stream_error gate+ # above already covers actual turn failures.if (config.claude_agent_use_resumeand user_id@@ -3077,9 +3311,15 @@ async def stream_chat_completion_sdk(and session is not Noneand state is not Noneand not ended_with_stream_error- and not skip_transcript_upload- and (not has_history or state.use_resume)):+ logger.info(+ "%s Attempting CLI session upload"+ " (use_resume=%s, has_history=%s, skip_transcript=%s)",+ log_prefix,+ state.use_resume,+ has_history,+ skip_transcript_upload,+ )try:await asyncio.shield(upload_cli_session(diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.pyindex 53289b3c1f..be2c46bdbb 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py@@ -15,6 +15,7 @@ from claude_agent_sdk import AssistantMessage, TextBlock, ToolUseBlockfrom .conftest import build_test_transcript as _build_transcriptfrom .service import (+ _RETRY_TARGET_TOKENS,ReducedContext,_is_prompt_too_long,_is_tool_only_message,@@ -207,6 +208,24 @@ class TestReduceContext:assert ctx.transcript_lost is True+ @pytest.mark.asyncio+ async def test_drop_returns_target_tokens_attempt_1(self) -> None:+ ctx = await _reduce_context("", False, "sess-1", "/tmp", "[t]", attempt=1)+ assert ctx.transcript_lost is True+ assert ctx.target_tokens == _RETRY_TARGET_TOKENS[0]++ @pytest.mark.asyncio+ async def test_drop_returns_target_tokens_attempt_2(self) -> None:+ ctx = await _reduce_context("", False, "sess-1", "/tmp", "[t]", attempt=2)+ assert ctx.transcript_lost is True+ assert ctx.target_tokens == _RETRY_TARGET_TOKENS[1]++ @pytest.mark.asyncio+ async def test_drop_clamps_attempt_beyond_limits(self) -> None:+ ctx = await _reduce_context("", False, "sess-1", "/tmp", "[t]", attempt=99)+ assert ctx.transcript_lost is True+ assert ctx.target_tokens == _RETRY_TARGET_TOKENS[-1]+# ---------------------------------------------------------------------------# _iter_sdk_messagesdiff --git a/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py b/autogpt_platform/backend/backend/copilot/sdk/transcript_test.pyindex bd2932854a..14e404a994 100644--- a/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py@@ -960,7 +960,7 @@ class TestRunCompression:)call_count = [0]- async def _compress_side_effect(*, messages, model, client):+ async def _compress_side_effect(*, messages, model, client, target_tokens=None):call_count[0] += 1if client is not None:# Simulate a hang that exceeds the timeoutdiff --git a/autogpt_platform/backend/backend/copilot/transcript.py b/autogpt_platform/backend/backend/copilot/transcript.pyindex a59130c478..a1e11f352d 100644--- a/autogpt_platform/backend/backend/copilot/transcript.py+++ b/autogpt_platform/backend/backend/copilot/transcript.py@@ -1179,6 +1179,7 @@ async def _run_compression(messages: list[dict],model: str,log_prefix: str,+ target_tokens: int | None = None,) -> CompressResult:"""Run LLM-based compression with truncation fallback.@@ -1187,6 +1188,12 @@ async def _run_compression(truncation-based compression which drops older messages withoutsummarization.+ ``target_tokens`` sets a hard token ceiling for the compressed output.+ When ``None``, ``compress_context`` derives the limit from the model's+ context window. Pass a smaller value on retries to force more aggressive+ compression — the compressor will LLM-summarize, content-truncate,+ middle-out delete, and first/last trim until the result fits.+A 60-second timeout prevents a hung LLM call from blocking theretry path indefinitely. The truncation fallback also has a30-second timeout to guard against slow tokenization on very large@@ -1196,18 +1203,27 @@ async def _run_compression(if client is None:logger.warning("%s No OpenAI client configured, using truncation", log_prefix)return await asyncio.wait_for(- compress_context(messages=messages, model=model, client=None),+ compress_context(+ messages=messages, model=model, client=None, target_tokens=target_tokens+ ),timeout=_TRUNCATION_TIMEOUT_SECONDS,)try:return await asyncio.wait_for(- compress_context(messages=messages, model=model, client=client),+ compress_context(+ messages=messages,+ model=model,+ client=client,+ target_tokens=target_tokens,+ ),timeout=_COMPACTION_TIMEOUT_SECONDS,)except Exception as e:logger.warning("%s LLM compaction failed, using truncation: %s", log_prefix, e)return await asyncio.wait_for(- compress_context(messages=messages, model=model, client=None),+ compress_context(+ messages=messages, model=model, client=None, target_tokens=target_tokens+ ),timeout=_TRUNCATION_TIMEOUT_SECONDS,)