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): fix T2+ --resume by using CLI native session file (#12777)
Zamil Majdy44b58ca22c63
diff --git a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.pyindex 52a1eff5df..a48d7def3d 100644--- a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py@@ -811,20 +811,24 @@ class TestRetryStateReset:assert len(session_messages) == 2assert session_messages == ["msg1", "msg2"]- def test_write_transcript_failure_sets_error_flag(self):- """When write_transcript_to_tempfile fails, skip_transcript_upload- must be set True to prevent uploading stale data."""- # Simulate the logic from service.py lines 1012-1020- skip_transcript_upload = False- use_resume = True- resume_file = None # write_transcript_to_tempfile returned None+ def test_cli_session_restore_failure_skips_resume(self):+ """When restore_cli_session returns False, --resume is not used.+ The transcript builder is still populated for future upload_transcript.- if not resume_file:- use_resume = False- skip_transcript_upload = True+ This covers the guard on the cli_restored branch in service.py.+ For a full integration test exercising the actual service code path,+ see TestStreamChatCompletionRetryIntegration.test_resume_skipped_when_cli_session_missing.+ """+ use_resume = False+ resume_file = None+ cli_restored = False # restore_cli_session returned False++ if cli_restored:+ use_resume = True+ resume_file = "sess-uuid"- assert skip_transcript_upload is Trueassert use_resume is False+ assert resume_file is None@pytest.mark.asyncioasync def test_compact_returns_none_preserves_error_flag(self):@@ -998,7 +1002,11 @@ def _make_sdk_patches(return_value=MagicMock(content=original_transcript, message_count=2),),),- (f"{_SVC}.write_transcript_to_tempfile", dict(return_value="/tmp/sess.jsonl")),+ (+ f"{_SVC}.restore_cli_session",+ dict(new_callable=AsyncMock, return_value=True),+ ),+ (f"{_SVC}.upload_cli_session", dict(new_callable=AsyncMock)),(f"{_SVC}.validate_transcript", dict(return_value=True)),(f"{_SVC}.compact_transcript",@@ -1876,3 +1884,67 @@ class TestStreamChatCompletionRetryIntegration:for e in status_events), f"Expected 'retrying' or 'interrupted' in StreamStatus, got: {[e.message for e in status_events]}"assert any(isinstance(e, StreamStart) for e in events)++ @pytest.mark.asyncio+ async def test_resume_skipped_when_cli_session_missing(self):+ """When restore_cli_session returns False, --resume is NOT passed to ClaudeSDKClient.++ Exercises the actual service code path so any change to the cli_restored+ branch in service.py will be caught immediately by this test.+ """+ import contextlib++ from backend.copilot.response_model import StreamStart+ from backend.copilot.sdk.service import stream_chat_completion_sdk++ session = self._make_session()+ result_msg = self._make_result_message()+ original_transcript = _build_transcript(+ [("user", "prior question"), ("assistant", "prior answer")]+ )+ captured_options: dict = {}++ def _client_factory(**kwargs):+ captured_options.update(kwargs)+ return self._make_client_mock(result_message=result_msg)++ patches = _make_sdk_patches(+ session,+ original_transcript=original_transcript,+ compacted_transcript=None,+ client_side_effect=_client_factory,+ )+ # Override restore_cli_session to return False (CLI native session unavailable)+ patches = [+ (+ (+ f"{_SVC}.restore_cli_session",+ dict(new_callable=AsyncMock, return_value=False),+ )+ if p[0] == f"{_SVC}.restore_cli_session"+ else p+ )+ for p in patches+ ]++ events = []+ with contextlib.ExitStack() as stack:+ for target, kwargs in patches:+ stack.enter_context(patch(target, **kwargs))+ async for event in stream_chat_completion_sdk(+ session_id="test-session-id",+ message="hello",+ is_user_message=True,+ user_id="test-user",+ session=session,+ ):+ events.append(event)++ # --resume must NOT be set on the options when CLI session restore failed.+ # captured_options holds {"options": ClaudeAgentOptions}, so check+ # the attribute directly rather than dict keys.+ assert not getattr(captured_options.get("options"), "resume", None), (+ f"--resume was set even though restore_cli_session returned False: "+ f"{captured_options}"+ )+ assert any(isinstance(e, StreamStart) for e in events)diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 5ecd5a8eb5..209b5fb056 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -44,9 +44,10 @@ from backend.copilot.transcript import (compact_transcript,download_transcript,read_compacted_entries,+ restore_cli_session,+ upload_cli_session,upload_transcript,validate_transcript,- write_transcript_to_tempfile,)from backend.copilot.transcript_builder import TranscriptBuilderfrom backend.data.redis_client import get_redis_async@@ -350,7 +351,11 @@ async def _reduce_context(`transcript_lost` is True when the transcript was dropped (callershould set `skip_transcript_upload`)."""- # First retry: try compacting+ # 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+ # retry runs without --resume. The compacted builder state is still+ # useful for the eventual upload_transcript call that seeds future turns.if transcript_content and not tried_compaction:compacted = await compact_transcript(transcript_content, model=config.model, log_prefix=log_prefix@@ -360,15 +365,13 @@ async def _reduce_context(and compacted != transcript_contentand validate_transcript(compacted)):- logger.info("%s Using compacted transcript for retry", log_prefix)+ logger.info(+ "%s Using compacted transcript for retry (no --resume on this attempt)",+ log_prefix,+ )tb = TranscriptBuilder()tb.load_previous(compacted, log_prefix=log_prefix)- resume_file = await asyncio.to_thread(- write_transcript_to_tempfile, compacted, session_id, sdk_cwd- )- if resume_file:- return ReducedContext(tb, True, resume_file, False, True)- logger.warning("%s Failed to write compacted transcript", log_prefix)+ return ReducedContext(tb, False, None, False, True)logger.warning("%s Compaction failed, dropping transcript", log_prefix)# Subsequent retry or compaction failed: drop transcript entirely@@ -2041,6 +2044,7 @@ async def stream_chat_completion_sdk(# OTEL context manager — initialized inside the try and cleaned up in finally._otel_ctx: Any = Noneskip_transcript_upload = False+ has_history = len(session.messages) > 1transcript_content: str = ""state: _RetryState | None = None@@ -2059,7 +2063,6 @@ async def stream_chat_completion_sdk(# injected into the supplement instead of the generic placeholder.# Catch ValueError early so the failure yields a clean StreamError rather# than propagating outside the stream error-handling path.- has_history = len(session.messages) > 1try:sdk_cwd = _make_sdk_cwd(session_id)os.makedirs(sdk_cwd, exist_ok=True)@@ -2148,7 +2151,10 @@ async def stream_chat_completion_sdk(if warm_ctx:system_prompt += f"\n\n{warm_ctx}"- # Process transcript download result+ # Process transcript download result and restore CLI native session.+ # The CLI native session file (uploaded after each turn) is the+ # source of truth for --resume. Our custom JSONL (TranscriptEntry)+ # is loaded into the builder for future upload_transcript calls.transcript_msg_count = 0if dl:is_valid = validate_transcript(dl.content)@@ -2162,59 +2168,59 @@ async def stream_chat_completion_sdk(is_valid,)if is_valid:- # Load previous FULL context into builder+ # Load previous FULL context into builder for state tracking.transcript_content = dl.contenttranscript_builder.load_previous(dl.content, log_prefix=log_prefix)- resume_file = await asyncio.to_thread(- write_transcript_to_tempfile, dl.content, session_id, sdk_cwd+ # Restore CLI's native session file so --resume session_id works.+ # Falls back gracefully if not available (first turn or upload missed).+ # user_id is guaranteed non-None here: _fetch_transcript only sets dl+ # when `config.claude_agent_use_resume and user_id` is truthy.+ cli_restored = user_id is not None and await restore_cli_session(+ user_id, session_id, sdk_cwd, log_prefix=log_prefix)- if resume_file:+ if cli_restored:use_resume = True+ resume_file = session_id # CLI --resume expects UUID, not file pathtranscript_msg_count = dl.message_count- logger.debug(- "%s Using --resume (%dB, msg_count=%d)",+ logger.info(+ "%s Using --resume %s (%dB transcript, msg_count=%d)",log_prefix,+ session_id[:8],len(dl.content),transcript_msg_count,)+ else:+ # 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.+ logger.info(+ "%s CLI session not restored — running without --resume this turn",+ log_prefix,+ )else:logger.warning("%s Transcript downloaded but invalid", log_prefix)transcript_covers_prefix = Falseelif config.claude_agent_use_resume and user_id and len(session.messages) > 1:- # No transcript on disk — try to reconstruct a full JSONL from the- # session.messages stored in the DB. This gives the Claude CLI- # proper tool_use/tool_result structural context via --resume- # instead of the lossy plain-text injection in _build_query_message- # (which caps tool results at 500 chars and drops call/result IDs).+ # No transcript in storage — reconstruct from DB messages as a+ # last-resort fallback (e.g., first turn after a crash or transition).+ # This path loses tool call IDs and structural fidelity but prevents+ # a completely context-free response for established sessions.prior = session.messages[:-1]reconstructed = _session_messages_to_transcript(prior)if reconstructed:- rebuilt_resume = await asyncio.to_thread(- write_transcript_to_tempfile, reconstructed, session_id, sdk_cwd+ # Populate builder only; no --resume since there is no CLI+ # native session to restore. The transcript builder state is+ # still useful for the upload that seeds future native sessions.+ transcript_content = reconstructed+ transcript_builder.load_previous(reconstructed, log_prefix=log_prefix)+ transcript_msg_count = len(prior)+ transcript_covers_prefix = True+ logger.info(+ "%s Reconstructed transcript from %d session messages "+ "(no CLI native session — running without --resume this turn)",+ log_prefix,+ len(prior),)- if rebuilt_resume:- use_resume = True- resume_file = rebuilt_resume- transcript_msg_count = len(prior)- transcript_content = reconstructed- transcript_builder.load_previous(- reconstructed, log_prefix=log_prefix- )- transcript_covers_prefix = True- logger.info(- "%s Reconstructed transcript from %d session messages "- "for --resume (no previous transcript file)",- log_prefix,- len(prior),- )- else:- logger.warning(- "%s Transcript reconstruction failed — write_transcript_to_tempfile"- " returned None (%d messages)",- log_prefix,- len(prior),- )- transcript_covers_prefix = Falseelse:logger.warning("%s No transcript available and reconstruction produced empty"@@ -2320,7 +2326,18 @@ async def stream_chat_completion_sdk(if sdk_env:sdk_options_kwargs["env"] = sdk_envif use_resume and resume_file:+ # --resume {uuid} implies the session UUID — do NOT also pass+ # --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.+ 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# the CLI bundled in 0.1.46+ is broken against OpenRouter). Falls@@ -2498,8 +2515,19 @@ async def stream_chat_completion_sdk(sdk_options_kwargs_retry = dict(sdk_options_kwargs)if ctx.use_resume and ctx.resume_file:sdk_options_kwargs_retry["resume"] = ctx.resume_file- elif "resume" in sdk_options_kwargs_retry:- del sdk_options_kwargs_retry["resume"]+ sdk_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().+ sdk_options_kwargs_retry.pop("resume", None)+ sdk_options_kwargs_retry["session_id"] = session_id+ else:+ # 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)state.options = ClaudeAgentOptions(**sdk_options_kwargs_retry) # type: ignore[arg-type] # dynamic kwargsstate.query_message, state.was_compacted = await _build_query_message(current_message,@@ -2981,6 +3009,43 @@ async def stream_chat_completion_sdk(exc_info=True,)+ # --- Upload CLI native session file for cross-pod --resume ---+ # The CLI writes its native session JSONL after each turn completes.+ # Uploading it here enables --resume on any pod (no pod affinity needed).+ # Runs after upload_transcript so both are available for the next turn.+ # asyncio.shield: same pattern as upload_transcript above — if the+ # outer finally-block coroutine is cancelled while awaiting shield,+ # the CancelledError propagates (BaseException, not caught by+ # `except Exception`) letting the caller handle cancellation, while+ # 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.+ if (+ config.claude_agent_use_resume+ and user_id+ and sdk_cwd+ and session is not None+ and state is not None+ and not ended_with_stream_error+ and not skip_transcript_upload+ and (not has_history or state.use_resume)+ ):+ try:+ await asyncio.shield(+ upload_cli_session(+ user_id=user_id,+ session_id=session_id,+ sdk_cwd=sdk_cwd,+ log_prefix=log_prefix,+ )+ )+ except Exception as cli_upload_err:+ logger.warning(+ "%s CLI session upload failed in finally: %s",+ log_prefix,+ cli_upload_err,+ )+try:if sdk_cwd:await _cleanup_sdk_tool_results(sdk_cwd)diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.pyindex eaf959ad35..53289b3c1f 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service_helpers_test.py@@ -107,6 +107,9 @@ class TestIsPromptTooLong:class TestReduceContext:@pytest.mark.asyncioasync def test_first_retry_compaction_success(self) -> None:+ # After compaction the retry runs WITHOUT --resume because we cannot+ # inject the compacted content into the CLI's native session file format.+ # The compacted builder state is still set for future upload_transcript.transcript = _build_transcript([("user", "hi"), ("assistant", "hello")])compacted = _build_transcript([("user", "hi"), ("assistant", "[summary]")])@@ -120,18 +123,14 @@ class TestReduceContext:"backend.copilot.sdk.service.validate_transcript",return_value=True,),- patch(- "backend.copilot.sdk.service.write_transcript_to_tempfile",- return_value="/tmp/resume.jsonl",- ),):ctx = await _reduce_context(transcript, False, "sess-123", "/tmp/cwd", "[test]")assert isinstance(ctx, ReducedContext)- assert ctx.use_resume is True- assert ctx.resume_file == "/tmp/resume.jsonl"+ assert ctx.use_resume is False+ assert ctx.resume_file is Noneassert ctx.transcript_lost is Falseassert ctx.tried_compaction is True@@ -186,7 +185,8 @@ class TestReduceContext:assert ctx.transcript_lost is True@pytest.mark.asyncio- async def test_write_tempfile_fails_drops(self) -> None:+ async def test_compaction_invalid_transcript_drops(self) -> None:+ # When validate_transcript returns False for compacted content, drop transcript.transcript = _build_transcript([("user", "hi"), ("assistant", "hello")])compacted = _build_transcript([("user", "hi"), ("assistant", "[summary]")])@@ -198,11 +198,7 @@ class TestReduceContext:),patch("backend.copilot.sdk.service.validate_transcript",- return_value=True,- ),- patch(- "backend.copilot.sdk.service.write_transcript_to_tempfile",- return_value=None,+ return_value=False,),):ctx = await _reduce_context(diff --git a/autogpt_platform/backend/backend/copilot/sdk/transcript.py b/autogpt_platform/backend/backend/copilot/sdk/transcript.pyindex a93bfbfe30..cfbf01a466 100644--- a/autogpt_platform/backend/backend/copilot/sdk/transcript.py+++ b/autogpt_platform/backend/backend/copilot/sdk/transcript.py@@ -19,9 +19,11 @@ from backend.copilot.transcript import (delete_transcript,download_transcript,read_compacted_entries,+ restore_cli_session,strip_for_upload,strip_progress_entries,strip_stale_thinking_blocks,+ upload_cli_session,upload_transcript,validate_transcript,write_transcript_to_tempfile,@@ -39,9 +41,11 @@ __all__ = ["delete_transcript","download_transcript","read_compacted_entries",+ "restore_cli_session","strip_for_upload","strip_progress_entries","strip_stale_thinking_blocks",+ "upload_cli_session","upload_transcript","validate_transcript","write_transcript_to_tempfile",diff --git a/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py b/autogpt_platform/backend/backend/copilot/sdk/transcript_test.pyindex cdc80d467d..bd2932854a 100644--- a/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py@@ -309,7 +309,7 @@ class TestDeleteTranscript:):await delete_transcript("user-123", "session-456")- assert mock_storage.delete.call_count == 2+ assert mock_storage.delete.call_count == 3paths = [call.args[0] for call in mock_storage.delete.call_args_list]assert any(p.endswith(".jsonl") for p in paths)assert any(p.endswith(".meta.json") for p in paths)@@ -319,7 +319,7 @@ class TestDeleteTranscript:"""If .jsonl delete fails, .meta.json delete is still attempted."""mock_storage = AsyncMock()mock_storage.delete = AsyncMock(- side_effect=[Exception("jsonl delete failed"), None]+ side_effect=[Exception("jsonl delete failed"), None, None])with patch(@@ -330,14 +330,14 @@ class TestDeleteTranscript:# Should not raiseawait delete_transcript("user-123", "session-456")- assert mock_storage.delete.call_count == 2+ assert mock_storage.delete.call_count == 3@pytest.mark.asyncioasync def test_handles_meta_delete_failure(self):"""If .meta.json delete fails, no exception propagates."""mock_storage = AsyncMock()mock_storage.delete = AsyncMock(- side_effect=[None, Exception("meta delete failed")]+ side_effect=[None, Exception("meta delete failed"), None])with patch(diff --git a/autogpt_platform/backend/backend/copilot/transcript.py b/autogpt_platform/backend/backend/copilot/transcript.pyindex 7f961a116f..a59130c478 100644--- a/autogpt_platform/backend/backend/copilot/transcript.py+++ b/autogpt_platform/backend/backend/copilot/transcript.py@@ -55,6 +55,8 @@ class TranscriptDownload:# Workspace storage constants — deterministic path from session_id.TRANSCRIPT_STORAGE_PREFIX = "chat-transcripts"+# Storage prefix for the CLI's native session JSONL files (for cross-pod --resume).+_CLI_SESSION_STORAGE_PREFIX = "cli-sessions"# ---------------------------------------------------------------------------@@ -652,6 +654,158 @@ def _build_meta_storage_path(user_id: str, session_id: str, backend: object) ->)+# ---------------------------------------------------------------------------+# CLI native session file — cross-pod --resume support+# ---------------------------------------------------------------------------+++def _cli_session_path(sdk_cwd: str, session_id: str) -> str:+ """Expected path of the CLI's native session JSONL file.++ The CLI resolves the working directory via ``os.path.realpath``, then+ encodes it by replacing every non-alphanumeric character with ``-``,+ placing its session file at::++ {projects_base}/{encoded_cwd}/{session_id}.jsonl++ We must mirror the CLI's realpath + regex encoding exactly. On macOS+ ``/tmp`` is a symlink to ``/private/tmp``, so a naive ``str.replace("/",+ "-")`` would produce the wrong directory name and the file would never be+ found.+ """+ encoded_cwd = re.sub(r"[^a-zA-Z0-9]", "-", os.path.realpath(sdk_cwd))+ safe_id = _sanitize_id(session_id)+ return os.path.join(_projects_base(), encoded_cwd, f"{safe_id}.jsonl")+++def _cli_session_storage_path_parts(+ user_id: str, session_id: str+) -> tuple[str, str, str]:+ """Return (workspace_id, file_id, filename) for a CLI session file in storage."""+ return (+ _CLI_SESSION_STORAGE_PREFIX,+ _sanitize_id(user_id),+ f"{_sanitize_id(session_id)}.jsonl",+ )+++async def upload_cli_session(+ user_id: str,+ session_id: str,+ sdk_cwd: str,+ log_prefix: str = "[Transcript]",+) -> None:+ """Upload the CLI's native session JSONL file to remote storage.++ Called after each turn so the next turn can restore the file on any pod+ (eliminating the pod-affinity requirement for --resume).++ The CLI only writes the session file after the turn completes, so this+ must run in the finally block, AFTER the SDK stream has finished.+ """+ session_file = _cli_session_path(sdk_cwd, session_id)+ real_path = os.path.realpath(session_file)+ projects_base = _projects_base()++ if not real_path.startswith(projects_base + os.sep):+ logger.warning(+ "%s CLI session file outside projects base, skipping upload: %s",+ log_prefix,+ os.path.basename(real_path),+ )+ return++ try:+ content = Path(real_path).read_bytes()+ except FileNotFoundError:+ logger.debug(+ "%s CLI session file not found, skipping upload: %s",+ log_prefix,+ session_file,+ )+ return+ except OSError as e:+ logger.warning("%s Failed to read CLI session file: %s", log_prefix, e)+ return++ storage = await get_workspace_storage()+ wid, fid, fname = _cli_session_storage_path_parts(user_id, session_id)+ try:+ await storage.store(+ workspace_id=wid, file_id=fid, filename=fname, content=content+ )+ logger.info(+ "%s Uploaded CLI session file (%dB) for cross-pod --resume",+ log_prefix,+ len(content),+ )+ except Exception as e:+ logger.warning("%s Failed to upload CLI session file: %s", log_prefix, e)+++async def restore_cli_session(+ user_id: str,+ session_id: str,+ sdk_cwd: str,+ log_prefix: str = "[Transcript]",+) -> bool:+ """Download and restore the CLI's native session file for --resume.++ Returns True if the file was successfully restored and --resume can be+ used with the session UUID. Returns False if not available (first turn+ or upload failed), in which case the caller should not set --resume.+ """+ session_file = _cli_session_path(sdk_cwd, session_id)+ real_path = os.path.realpath(session_file)+ projects_base = _projects_base()++ if not real_path.startswith(projects_base + os.sep):+ logger.warning(+ "%s CLI session restore path outside projects base: %s",+ log_prefix,+ os.path.basename(session_file),+ )+ return False++ # If the session file already exists locally (same-pod reuse), use it directly.+ # Downloading from storage could overwrite a newer local version when a previous+ # turn's upload failed: stored content is stale while the local file already+ # contains extended history from that turn.+ if Path(real_path).exists():+ logger.debug(+ "%s CLI session file already exists locally — using it for --resume",+ log_prefix,+ )+ return True++ storage = await get_workspace_storage()+ path = _build_path_from_parts(+ _cli_session_storage_path_parts(user_id, session_id), storage+ )++ try:+ content = await storage.retrieve(path)+ except FileNotFoundError:+ logger.debug("%s No CLI session in storage (first turn or missing)", log_prefix)+ return False+ except Exception as e:+ logger.warning("%s Failed to download CLI session: %s", log_prefix, e)+ return False++ try:+ os.makedirs(os.path.dirname(real_path), exist_ok=True)+ Path(real_path).write_bytes(content)+ logger.info(+ "%s Restored CLI session file (%dB) for --resume",+ log_prefix,+ len(content),+ )+ return True+ except OSError as e:+ logger.warning("%s Failed to write CLI session file: %s", log_prefix, e)+ return False++async def upload_transcript(user_id: str,session_id: str,@@ -822,6 +976,16 @@ async def delete_transcript(user_id: str, session_id: str) -> None:except Exception as e:logger.warning("[Transcript] Failed to delete metadata: %s", e)+ # Also delete the CLI native session file to prevent storage growth.+ try:+ cli_path = _build_path_from_parts(+ _cli_session_storage_path_parts(user_id, session_id), storage+ )+ await storage.delete(cli_path)+ logger.info("[Transcript] Deleted CLI session for session %s", session_id)+ except Exception as e:+ logger.warning("[Transcript] Failed to delete CLI session: %s", e)+# ---------------------------------------------------------------------------# Transcript compaction — LLM summarization for prompt-too-long recoverydiff --git a/autogpt_platform/backend/backend/copilot/transcript_test.py b/autogpt_platform/backend/backend/copilot/transcript_test.pyindex dd99fd5a85..fec869b6ac 100644--- a/autogpt_platform/backend/backend/copilot/transcript_test.py+++ b/autogpt_platform/backend/backend/copilot/transcript_test.py@@ -77,7 +77,7 @@ class TestStoragePathParts:assert fname.endswith(".jsonl")def test_meta_returns_meta_json(self):- prefix, uid, fname = _meta_storage_path_parts("user-1", "sess-2")+ prefix, _, fname = _meta_storage_path_parts("user-1", "sess-2")assert prefix == "chat-transcripts"assert fname.endswith(".meta.json")@@ -724,3 +724,368 @@ class TestValidateTranscript:def test_assistant_only_is_valid(self):content = _make_jsonl(ASST_ENTRY)assert validate_transcript(content) is True+++# ---------------------------------------------------------------------------+# CLI native session file helpers+# ---------------------------------------------------------------------------+++class TestCliSessionPath:+ def test_encodes_slashes_to_dashes(self):+ from .transcript import _cli_session_path, _projects_base++ sdk_cwd = "/tmp/copilot-abc"+ result = _cli_session_path(sdk_cwd, "12345678-1234-1234-1234-123456789abc")+ base = _projects_base()+ assert result.startswith(base)+ # Encoded cwd replaces '/' with '-'+ assert "-tmp-copilot-abc" in result+ assert result.endswith(".jsonl")++ def test_sanitizes_session_id(self):+ from .transcript import _cli_session_path++ result = _cli_session_path("/tmp/cwd", "../../etc/passwd")+ # _sanitize_id strips non-hex/hyphen chars; path traversal impossible+ assert ".." not in result+ assert "passwd" not in result+++class TestUploadCliSession:+ def test_skips_upload_when_path_outside_projects_base(self, tmp_path):+ """Files outside the CLI projects base are rejected without upload."""+ import asyncio+ from unittest.mock import AsyncMock, patch++ from .transcript import upload_cli_session++ mock_storage = AsyncMock()++ with (+ patch(+ "backend.copilot.transcript._projects_base",+ return_value=str(tmp_path),+ ),+ # Return a path that is genuinely outside tmp_path so that+ # realpath(session_file).startswith(projects_base + "/") is False+ # and the boundary guard actually fires.+ patch(+ "backend.copilot.transcript._cli_session_path",+ return_value="/outside/escaped/session.jsonl",+ ),+ patch(+ "backend.copilot.transcript.get_workspace_storage",+ new_callable=AsyncMock,+ return_value=mock_storage,+ ),+ ):+ asyncio.run(+ upload_cli_session(+ user_id="user-1",+ session_id="12345678-0000-0000-0000-000000000000",+ sdk_cwd=str(tmp_path),+ )+ )++ # storage.store must NOT be called — boundary guard should reject the path+ mock_storage.store.assert_not_called()++ def test_skips_upload_when_file_not_found(self, tmp_path):+ """Missing CLI session file logs debug and skips upload silently."""+ import asyncio+ from unittest.mock import AsyncMock, patch++ from .transcript import upload_cli_session++ mock_storage = AsyncMock()+ projects_base = str(tmp_path)++ with (+ patch(+ "backend.copilot.transcript._projects_base",+ return_value=projects_base,+ ),+ patch(+ "backend.copilot.transcript.get_workspace_storage",+ new_callable=AsyncMock,+ return_value=mock_storage,+ ),+ ):+ # session file doesn't exist — should not raise+ asyncio.run(+ upload_cli_session(+ user_id="user-1",+ session_id="12345678-0000-0000-0000-000000000000",+ sdk_cwd=str(tmp_path),+ )+ )++ mock_storage.store.assert_not_called()++ def test_uploads_file_successfully(self, tmp_path):+ """Happy path: session file exists within projects base → upload called."""+ import asyncio+ from unittest.mock import AsyncMock, patch++ from .transcript import _sanitize_id, upload_cli_session++ projects_base = str(tmp_path)+ session_id = "12345678-0000-0000-0000-000000000001"+ sdk_cwd = str(tmp_path)++ # Build the path the same way _cli_session_path does, but using our tmp_path+ # as projects_base so the boundary check passes.+ # Must use the same encoding: re.sub non-alphanumeric → "-" on realpath.+ import os+ import re++ encoded_cwd = re.sub(r"[^a-zA-Z0-9]", "-", os.path.realpath(sdk_cwd))+ session_dir = tmp_path / encoded_cwd+ session_dir.mkdir(parents=True, exist_ok=True)+ session_file = session_dir / f"{_sanitize_id(session_id)}.jsonl"+ session_file.write_bytes(b'{"type":"assistant"}\n')++ mock_storage = AsyncMock()++ with (+ patch(+ "backend.copilot.transcript._projects_base",+ return_value=projects_base,+ ),+ patch(+ "backend.copilot.transcript.get_workspace_storage",+ new_callable=AsyncMock,+ return_value=mock_storage,+ ),+ ):+ asyncio.run(+ upload_cli_session(+ user_id="user-1",+ session_id=session_id,+ sdk_cwd=sdk_cwd,+ )+ )++ mock_storage.store.assert_called_once()++ def test_skips_upload_on_oserror(self, tmp_path):+ """OSError reading session file is logged as warning; upload is skipped."""+ import asyncio+ from unittest.mock import AsyncMock, patch++ from .transcript import _sanitize_id, upload_cli_session++ projects_base = str(tmp_path)+ sdk_cwd = str(tmp_path)+ session_id = "12345678-0000-0000-0000-000000000002"++ # Build file at a path inside projects_base so boundary check passes.+ import os+ import re++ encoded_cwd = re.sub(r"[^a-zA-Z0-9]", "-", os.path.realpath(sdk_cwd))+ session_dir = tmp_path / encoded_cwd+ session_dir.mkdir(parents=True, exist_ok=True)+ session_file = session_dir / f"{_sanitize_id(session_id)}.jsonl"+ session_file.write_bytes(b'{"type":"assistant"}\n')+ # Remove read permission to trigger OSError+ session_file.chmod(0o000)++ mock_storage = AsyncMock()++ try:+ with (+ patch(+ "backend.copilot.transcript._projects_base",+ return_value=projects_base,+ ),+ patch(+ "backend.copilot.transcript.get_workspace_storage",+ new_callable=AsyncMock,+ return_value=mock_storage,+ ),+ ):+ asyncio.run(+ upload_cli_session(+ user_id="user-1",+ session_id=session_id,+ sdk_cwd=sdk_cwd,+ )+ )+ finally:+ session_file.chmod(0o644) # restore so tmp_path cleanup works++ mock_storage.store.assert_not_called()+++class TestRestoreCliSession:+ def test_returns_false_when_file_not_found_in_storage(self):+ """Returns False (graceful degradation) when the session is missing."""+ import asyncio+ from unittest.mock import AsyncMock, patch++ from .transcript import restore_cli_session++ mock_storage = AsyncMock()+ mock_storage.retrieve.side_effect = FileNotFoundError("not found")++ with patch(+ "backend.copilot.transcript.get_workspace_storage",+ new_callable=AsyncMock,+ return_value=mock_storage,+ ):+ result = asyncio.run(+ restore_cli_session(+ user_id="user-1",+ session_id="12345678-0000-0000-0000-000000000000",+ sdk_cwd="/tmp/copilot-test",+ )+ )++ assert result is False++ def test_returns_false_when_restore_path_outside_projects_base(self, tmp_path):+ """Path traversal guard: rejects restoration outside the projects base."""+ import asyncio+ from unittest.mock import AsyncMock, patch++ from .transcript import restore_cli_session++ mock_storage = AsyncMock()+ mock_storage.retrieve.return_value = b'{"type":"assistant"}\n'++ with (+ patch(+ "backend.copilot.transcript.get_workspace_storage",+ new_callable=AsyncMock,+ return_value=mock_storage,+ ),+ patch(+ "backend.copilot.transcript._projects_base",+ return_value=str(tmp_path),+ ),+ # Return a path genuinely outside tmp_path so the boundary guard fires.+ patch(+ "backend.copilot.transcript._cli_session_path",+ return_value="/outside/escaped/session.jsonl",+ ),+ ):+ result = asyncio.run(+ restore_cli_session(+ user_id="user-1",+ session_id="12345678-0000-0000-0000-000000000000",+ sdk_cwd=str(tmp_path),+ )+ )++ assert result is False++ def test_returns_true_when_local_file_already_exists(self, tmp_path):+ """Same-pod reuse: if local file exists, skip storage download and return True."""+ import asyncio+ import os+ import re+ from pathlib import Path+ from unittest.mock import AsyncMock, patch++ from .transcript import restore_cli_session++ session_id = "12345678-0000-0000-0000-000000000099"+ sdk_cwd = str(tmp_path)++ # Pre-create the local session file (simulates previous turn on same pod)+ projects_base = os.path.realpath(str(tmp_path))+ encoded_cwd = re.sub(r"[^a-zA-Z0-9]", "-", projects_base)+ session_dir = Path(projects_base) / encoded_cwd+ session_dir.mkdir(parents=True, exist_ok=True)+ existing_content = b'{"type":"user"}\n{"type":"assistant"}\n'+ (session_dir / f"{session_id}.jsonl").write_bytes(existing_content)++ mock_storage = AsyncMock()++ with (+ patch(+ "backend.copilot.transcript.get_workspace_storage",+ new_callable=AsyncMock,+ return_value=mock_storage,+ ),+ patch(+ "backend.copilot.transcript._projects_base",+ return_value=projects_base,+ ),+ ):+ result = asyncio.run(+ restore_cli_session(+ user_id="user-1",+ session_id=session_id,+ sdk_cwd=sdk_cwd,+ )+ )++ assert result is True+ # Storage should NOT have been accessed (local file was used as-is)+ mock_storage.retrieve.assert_not_called()+ # Local file should be unchanged+ assert (session_dir / f"{session_id}.jsonl").read_bytes() == existing_content++ def test_returns_true_on_success(self, tmp_path):+ """Happy path: storage has the session → file written → returns True."""+ import asyncio+ from unittest.mock import AsyncMock, patch++ from .transcript import restore_cli_session++ projects_base = str(tmp_path)+ sdk_cwd = str(tmp_path)+ session_id = "12345678-0000-0000-0000-000000000003"+ content = b'{"type":"assistant"}\n'++ mock_storage = AsyncMock()+ mock_storage.retrieve.return_value = content++ with (+ patch(+ "backend.copilot.transcript.get_workspace_storage",+ new_callable=AsyncMock,+ return_value=mock_storage,+ ),+ patch(+ "backend.copilot.transcript._projects_base",+ return_value=projects_base,+ ),+ ):+ result = asyncio.run(+ restore_cli_session(+ user_id="user-1",+ session_id=session_id,+ sdk_cwd=sdk_cwd,+ )+ )++ assert result is True++ def test_returns_false_on_download_exception(self):+ """Non-FileNotFoundError during retrieve logs warning and returns False."""+ import asyncio+ from unittest.mock import AsyncMock, patch++ from .transcript import restore_cli_session++ mock_storage = AsyncMock()+ mock_storage.retrieve.side_effect = RuntimeError("network error")++ with patch(+ "backend.copilot.transcript.get_workspace_storage",+ new_callable=AsyncMock,+ return_value=mock_storage,+ ):+ result = asyncio.run(+ restore_cli_session(+ user_id="user-1",+ session_id="12345678-0000-0000-0000-000000000004",+ sdk_cwd="/tmp/copilot-test",+ )+ )++ assert result is False