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): baseline always uploads when GCS has no transcript
Zamil Majdy0cd0a76305bf
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex dd6aa121b6..8239d8248e 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -720,16 +720,15 @@ def is_transcript_stale(dl: TranscriptDownload | None, session_msg_count: int) -def should_upload_transcript(- user_id: str | None, transcript_covers_prefix: bool+ user_id: str | None, upload_safe: bool) -> bool:"""Return ``True`` when the caller should upload the final transcript.- Uploads require a logged-in user (for the storage key) *and* a- transcript that covered the session prefix when loaded — otherwise- we'd be overwriting a more complete version in storage with a- partial one built from just the current turn.+ Uploads require a logged-in user (for the storage key) *and* a safe+ upload signal from ``_load_prior_transcript`` — i.e. GCS does not hold a+ newer version that we'd be overwriting."""- return bool(user_id) and transcript_covers_prefix+ return bool(user_id) and upload_safeasync def _load_prior_transcript(@@ -740,24 +739,30 @@ async def _load_prior_transcript() -> bool:"""Download and load the prior transcript into ``transcript_builder``.- Returns ``True`` when the loaded transcript fully covers the session- prefix; ``False`` otherwise (stale, missing, invalid, or download- error). Callers should suppress uploads when this returns ``False``- to avoid overwriting a more complete version in storage.+ Returns ``True`` when upload is safe at the end of this turn; ``False``+ when GCS has a *newer* version that we must not overwrite (stale case).++ Upload is suppressed only for **stale** transcripts (GCS watermark >+ current turn's prefix) and **download errors** (we can't know what GCS+ holds). Missing and invalid transcripts return ``True`` because there is+ nothing in GCS worth protecting — uploading is always safe."""try:dl = await download_transcript(user_id, session_id, log_prefix="[Baseline]")except Exception as e:logger.warning("[Baseline] Transcript download failed: %s", e)+ # Unknown GCS state — be conservative and skip upload.return Falseif dl is None:- logger.debug("[Baseline] No transcript available")- return False+ logger.debug("[Baseline] No transcript available — will upload fresh")+ # Nothing in GCS to protect; allow upload.+ return Trueif not validate_transcript(dl.content):- logger.warning("[Baseline] Downloaded transcript but invalid")- return False+ logger.warning("[Baseline] Downloaded transcript is invalid — will overwrite")+ # Corrupt file in GCS; uploading a valid one is strictly better.+ return Trueif is_transcript_stale(dl, session_msg_count):logger.warning(@@ -765,6 +770,7 @@ async def _load_prior_transcript(dl.message_count,session_msg_count,)+ # GCS watermark is ahead of this turn — do not overwrite.return Falsetranscript_builder.load_previous(dl.content, log_prefix="[Baseline]")@@ -897,7 +903,7 @@ async def stream_chat_completion_baseline(# --- Transcript support (feature parity with SDK path) ---transcript_builder = TranscriptBuilder()- transcript_covers_prefix = True+ transcript_upload_safe = True# Build system prompt only on the first turn to avoid mid-conversation# changes from concurrent chats updating business understanding.@@ -916,7 +922,7 @@ async def stream_chat_completion_baseline(# on the request critical path.if user_id and len(session.messages) > 1:(- transcript_covers_prefix,+ transcript_upload_safe,(base_system_prompt, understanding),) = await asyncio.gather(_load_prior_transcript(@@ -1308,7 +1314,7 @@ async def stream_chat_completion_baseline(stop_reason=STOP_REASON_END_TURN,)- if user_id and should_upload_transcript(user_id, transcript_covers_prefix):+ if user_id and should_upload_transcript(user_id, transcript_upload_safe):await _upload_final_transcript(user_id=user_id,session_id=session_id,diff --git a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.pyindex baeb3e3648..336f3badbc 100644--- a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py@@ -119,24 +119,26 @@ class TestLoadPriorTranscript:assert builder.is_empty@pytest.mark.asyncio- async def test_missing_transcript_returns_false(self):+ async def test_missing_transcript_allows_upload(self):+ """Nothing in GCS → safe to upload fresh transcript after the turn."""builder = TranscriptBuilder()with patch("backend.copilot.baseline.service.download_transcript",new=AsyncMock(return_value=None),):- covers = await _load_prior_transcript(+ upload_safe = await _load_prior_transcript(user_id="user-1",session_id="session-1",session_msg_count=2,transcript_builder=builder,)- assert covers is False+ assert upload_safe is Trueassert builder.is_empty@pytest.mark.asyncio- async def test_invalid_transcript_returns_false(self):+ async def test_invalid_transcript_allows_upload(self):+ """Corrupt file in GCS → overwriting with valid data is better."""builder = TranscriptBuilder()download = TranscriptDownload(content='{"type":"progress","uuid":"a"}\n',@@ -146,14 +148,14 @@ class TestLoadPriorTranscript:"backend.copilot.baseline.service.download_transcript",new=AsyncMock(return_value=download),):- covers = await _load_prior_transcript(+ upload_safe = await _load_prior_transcript(user_id="user-1",session_id="session-1",session_msg_count=2,transcript_builder=builder,)- assert covers is False+ assert upload_safe is Trueassert builder.is_empty@pytest.mark.asyncio@@ -560,7 +562,7 @@ class TestTranscriptLifecycle:# --- 3. Gate + upload ---assert (should_upload_transcript(- user_id="user-1", transcript_covers_prefix=covers+ user_id="user-1", upload_safe=covers)is True)@@ -611,7 +613,7 @@ class TestTranscriptLifecycle:assert covers is False# The caller's gate mirrors the production path.assert (- should_upload_transcript(user_id="user-1", transcript_covers_prefix=covers)+ should_upload_transcript(user_id="user-1", upload_safe=covers)is False)upload_mock.assert_not_awaited()@@ -628,14 +630,13 @@ class TestTranscriptLifecycle:)assert (- should_upload_transcript(user_id=None, transcript_covers_prefix=True)+ should_upload_transcript(user_id=None, upload_safe=True)is False)@pytest.mark.asyncioasync def test_lifecycle_missing_download_still_uploads_new_content(self):- """No prior transcript → covers defaults to True in the service,- new turn should upload cleanly."""+ """No prior transcript → upload is safe; the turn writes the first snapshot."""builder = TranscriptBuilder()upload_mock = AsyncMock(return_value=None)with (@@ -648,20 +649,18 @@ class TestTranscriptLifecycle:new=upload_mock,),):- covers = await _load_prior_transcript(+ upload_safe = await _load_prior_transcript(user_id="user-1",session_id="session-1",session_msg_count=1,transcript_builder=builder,)- # No download: covers is False, so the production path would- # skip upload. This protects against overwriting a future- # more-complete transcript with a single-turn snapshot.- assert covers is False+ # Nothing in GCS → upload is safe so the first baseline turn+ # can write the initial snapshot.+ assert upload_safe is Trueassert (should_upload_transcript(- user_id="user-1", transcript_covers_prefix=covers+ user_id="user-1", upload_safe=upload_safe)- is False+ is True)- upload_mock.assert_not_awaited()