Skip to content

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): hand chat text to the Claude Code CLI exactly as written (#15910)

Nicholas Tindle4a75ca0f8cea
diff --git a/autogpt_platform/backend/backend/blocks/orchestrator.py b/autogpt_platform/backend/backend/blocks/orchestrator.py
index e9195148ad..426f9c8fb9 100644
--- a/autogpt_platform/backend/backend/blocks/orchestrator.py
+++ b/autogpt_platform/backend/backend/blocks/orchestrator.py
@@ -26,6 +26,8 @@ from backend.blocks._base import (
BlockType,
)
from backend.blocks.agent import AgentExecutorBlock
+from backend.copilot.sdk.cli_version import require_verbatim_prompts_cli
+from backend.copilot.sdk.env import DISABLE_SLASH_COMMANDS_FLAG, cli_prompt_text
from backend.copilot.sdk.env import config as copilot_config
from backend.data.dynamic_fields import (
extract_base_field_name,
@@ -1786,6 +1788,15 @@ class OrchestratorBlock(Block):
cwd=sdk_cwd,
env=sdk_env,
model=input_data.model.value or None,
+ # No settings files or MCP servers from the CLI's config dir:
+ # every run on this host shares it.
+ setting_sources=[],
+ extra_args={
+ "strict-mcp-config": None,
+ DISABLE_SLASH_COMMANDS_FLAG: None,
+ },
+ # The CLI hands every prompt to the model exactly as written.
+ verbatim_prompts=True,
)
# Strip system messages from prompt — they're already passed via
@@ -1812,7 +1823,7 @@ class OrchestratorBlock(Block):
for p in sdk_prompt:
if p.get("role") == "user" and p.get("content"):
user_parts.append(str(p["content"]))
- user_message = (
+ user_message = cli_prompt_text(
"\n\n".join(user_parts) if user_parts else input_data.prompt
)
@@ -1827,6 +1838,9 @@ class OrchestratorBlock(Block):
# span from the trace.
_HEARTBEAT_INTERVAL = 10.0 # seconds
+ options.cli_path = await asyncio.to_thread(
+ require_verbatim_prompts_cli, options.cli_path
+ )
async with ClaudeSDKClient(options=options) as client:
await client.query(user_message)
diff --git a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.py b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.py
index 42d20cab28..ad787d7a5e 100644
--- a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.py
+++ b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_execution_mode.py
@@ -4,11 +4,16 @@ Covers:
- ExecutionMode enum members exist and have stable values
- EXTENDED_THINKING provider validation (anthropic/open_router allowed, others rejected)
- EXTENDED_THINKING model-name validation (must start with "claude")
+- EXTENDED_THINKING options handed to the Claude Agent SDK
"""
+from collections.abc import AsyncGenerator
from unittest.mock import AsyncMock, MagicMock, patch
+import claude_agent_sdk
import pytest
+from claude_agent_sdk import ClaudeAgentOptions, ResultMessage
+from pydantic import SecretStr
from backend.blocks.llm import LLMModel
from backend.blocks.orchestrator import (
@@ -16,6 +21,8 @@ from backend.blocks.orchestrator import (
OrchestratorBlock,
_select_final_answer_parts,
)
+from backend.copilot.sdk.env import DISABLE_SLASH_COMMANDS_FLAG
+from backend.data.model import APIKeyCredentials
# ---------------------------------------------------------------------------
# ExecutionMode enum integrity
@@ -307,3 +314,100 @@ class TestSelectFinalAnswerParts:
has_tool_calls=False,
current=["real final answer"],
) == ["real final answer"], f"blank={blank!r} clobbered current"
+
+
+# ---------------------------------------------------------------------------
+# Options handed to the Claude Agent SDK
+# ---------------------------------------------------------------------------
+
+
+class _ClientRecorder:
+ """Stands in for ``ClaudeSDKClient``: records its options and prompt."""
+
+ seen: list[ClaudeAgentOptions | str] = []
+
+ def __init__(self, options: ClaudeAgentOptions) -> None:
+ self.seen.append(options)
+
+ async def __aenter__(self) -> "_ClientRecorder":
+ return self
+
+ async def __aexit__(self, *_: object) -> None:
+ return None
+
+ async def query(self, prompt: str) -> None:
+ self.seen.append(prompt)
+
+ async def receive_response(self) -> AsyncGenerator[ResultMessage, None]:
+ yield ResultMessage(
+ subtype="success",
+ duration_ms=1,
+ duration_api_ms=1,
+ is_error=False,
+ num_turns=1,
+ session_id="sess-1",
+ )
+
+
+async def _run_sdk_mode(
+ prompt_text: str, cli_check: MagicMock
+) -> tuple[list[tuple[str, object]], list[ClaudeAgentOptions | str]]:
+ """Run SDK mode against ``_ClientRecorder``; return outputs and what it saw."""
+ input_data = MagicMock(sys_prompt="", prompt=prompt_text)
+ input_data.model.value = "claude-test"
+ credentials = APIKeyCredentials(
+ id="test-anthropic-id",
+ provider="anthropic",
+ api_key=SecretStr("mock-anthropic-key"),
+ title="Mock Anthropic key",
+ expires_at=None,
+ )
+ with (
+ patch.object(_ClientRecorder, "seen", []) as seen,
+ patch.object(claude_agent_sdk, "ClaudeSDKClient", _ClientRecorder),
+ patch("backend.blocks.orchestrator.require_verbatim_prompts_cli", cli_check),
+ ):
+ outputs: list[tuple[str, object]] = [
+ output
+ async for output in OrchestratorBlock()._execute_tools_sdk_mode(
+ input_data=input_data,
+ credentials=credentials,
+ tool_functions=[],
+ prompt=[{"role": "user", "content": prompt_text}],
+ execution_params=MagicMock(graph_exec_id="graph-exec-1"),
+ execution_processor=MagicMock(),
+ )
+ ]
+ return outputs, seen
+
+
+async def test_sdk_mode_sends_slash_prompt_as_text() -> None:
+ """A graph prompt such as "/compact" goes to the checked CLI as text, with
+ the CLI's commands off, prompts delivered verbatim, and no settings or MCP
+ servers loaded from the host's config dir."""
+ cli_check = MagicMock(return_value="/opt/claude/claude")
+ _, seen = await _run_sdk_mode("/compact", cli_check)
+
+ options, prompt = seen
+ assert isinstance(options, ClaudeAgentOptions)
+ assert options.extra_args == {
+ "strict-mcp-config": None,
+ DISABLE_SLASH_COMMANDS_FLAG: None,
+ }
+ assert options.setting_sources == []
+ assert options.verbatim_prompts is True
+ assert prompt == " /compact"
+ cli_check.assert_called_once_with(None)
+ assert options.cli_path == "/opt/claude/claude"
+
+
+async def test_sdk_mode_does_not_start_a_cli_that_is_too_old() -> None:
+ """An older CLI ignores ``verbatim_prompts`` without an error, so the
+ block reports an error instead of starting it."""
+ cli_check = MagicMock(side_effect=RuntimeError("The Claude Code CLI is too old"))
+ outputs, seen = await _run_sdk_mode("hello", cli_check)
+
+ assert seen == []
+ assert outputs == [("error", "The Claude Code CLI is too old")]
diff --git a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_sdk_tracing.py b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_sdk_tracing.py
index b4a990c38c..a63c8b0c1b 100644
--- a/autogpt_platform/backend/backend/blocks/test/test_orchestrator_sdk_tracing.py
+++ b/autogpt_platform/backend/backend/blocks/test/test_orchestrator_sdk_tracing.py
@@ -113,6 +113,10 @@ async def test_sdk_mode_replies_get_assistant_turn_runs(
with (
patch.object(claude_agent_sdk, "ClaudeSDKClient", traced_client),
+ patch(
+ "backend.blocks.orchestrator.require_verbatim_prompts_cli",
+ return_value="claude",
+ ),
tracing_context(enabled=True),
):
outputs = [
diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.py
index 85c55f6612..97e24f2470 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/service.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py
@@ -1658,7 +1658,12 @@ async def _load_prior_transcript(
# Corrupt file in GCS; overwriting with a valid one is better.
return True, None
- stripped = strip_for_upload(raw_str)
+ try:
+ stripped = strip_for_upload(raw_str)
+ except ValueError:
+ logger.warning("[Baseline] CLI session content could not be scrubbed")
+ # Corrupt file in GCS; overwriting with a valid one is better.
+ return True, None
if not validate_transcript(stripped):
logger.warning("[Baseline] CLI session content invalid after strip")
# Corrupt file in GCS; overwriting with a valid one is better.
diff --git a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py
index d905fda4a8..6574ed9c57 100644
--- a/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py
+++ b/autogpt_platform/backend/backend/copilot/baseline/transcript_integration_test.py
@@ -364,6 +364,35 @@ class TestLoadPriorTranscript:
assert dl is None
assert builder.is_empty
+ @pytest.mark.asyncio
+ async def test_transcript_it_cannot_scrub_allows_upload(self):
+ """A line the strip cannot parse makes the file as unusable as a
+ corrupt one, instead of failing the turn."""
+ builder = TranscriptBuilder()
+ restore = TranscriptDownload(
+ content=(
+ b'{"type":"assistant","uuid":"a1","message":{"role":"assistant",'
+ b'"content":[{"type":"text","text":"hi"}]}}\n'
+ b'{"type":"attachment","attachment":{"type":"file","con\n'
+ ),
+ message_count=1,
+ mode="sdk",
+ )
+ with patch(
+ "backend.copilot.baseline.service.download_transcript",
+ new=AsyncMock(return_value=restore),
+ ):
+ upload_safe, dl = await _load_prior_transcript(
+ user_id="user-1",
+ session_id="session-1",
+ session_messages=_make_session_messages("user", "assistant"),
+ transcript_builder=builder,
+ )
+
+ assert upload_safe is True
+ assert dl is None
+ assert builder.is_empty
+
@pytest.mark.asyncio
async def test_download_exception_returns_false(self):
builder = TranscriptBuilder()
diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py b/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py
index 0069ed5cfc..7ab3640033 100644
--- a/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py
+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py
@@ -127,6 +127,7 @@ class SdkTurns:
"upsert_chat_session": AsyncMock(return_value=session),
"download_transcript": AsyncMock(side_effect=self._download),
"upload_transcript": AsyncMock(side_effect=self._upload),
+ "delete_transcript": AsyncMock(side_effect=self._delete),
"compact_transcript": AsyncMock(return_value=None),
"get_redis_async": AsyncMock(),
"propagate_attributes": MagicMock(),
@@ -192,6 +193,9 @@ class SdkTurns:
) -> TranscriptDownload | None:
return self._transcripts.get(session_id)
+ async def _delete(self, user_id: str, session_id: str) -> None:
+ self._transcripts.pop(session_id, None)
+
def _lock(*_: Any, owner_id: str = "", **__: Any) -> MagicMock:
return MagicMock(
diff --git a/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.py b/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.py
index 93d1b85e61..a703c3592b 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/building_mode_restart_test.py
@@ -9,8 +9,10 @@ blocks without results and leave the CLI session file unresumable.
from unittest.mock import MagicMock
import pytest
+from claude_agent_sdk import ClaudeAgentOptions
from backend.copilot.model import ChatSession
+from backend.copilot.sdk.env import DISABLE_SLASH_COMMANDS_FLAG
from .service import _graphiti_ingest_allowed, _ready_for_building_mode_restart
@@ -82,6 +84,7 @@ class TestApplyBuildingModeRestart:
auto_mode_supplement: str = "",
use_e2b: bool = False,
expert_id: str | None = None,
+ sdk_options: ClaudeAgentOptions | MagicMock | None = None,
):
from backend.copilot.sdk.service import (
_BUILDING_MODE_CONTINUATION,
@@ -97,11 +100,10 @@ class TestApplyBuildingModeRestart:
state = self._state(
prior_emitted=prior_emitted, thinking_reprompted=thinking_reprompted
)
- sdk_options = MagicMock()
status = await _apply_building_mode_restart(
session=session,
state=state,
- sdk_options=sdk_options,
+ sdk_options=sdk_options or MagicMock(),
base_system_prompt="BASE",
delegation_supplement=delegation_supplement,
oversight_supplement=oversight_supplement,
@@ -253,6 +255,19 @@ class TestApplyBuildingModeRestart:
assert session.building_mode_requested is False
assert state.query_message == _BUILDING_MODE_CONTINUATION
+ @pytest.mark.asyncio
+ async def test_relaunch_keeps_cli_text_as_text(self, mocker):
+ """The relaunch runs the rest of the user's turn, so its text must
+ still reach the model as written after it."""
+ options = ClaudeAgentOptions(
+ extra_args={DISABLE_SLASH_COMMANDS_FLAG: None}, verbatim_prompts=True
+ )
+ _, state, _, _ = await self._run(mocker, sdk_options=options)
+
+ assert state.options is not options
+ assert DISABLE_SLASH_COMMANDS_FLAG in state.options.extra_args
+ assert state.options.verbatim_prompts is True
+
@pytest.mark.asyncio
async def test_adapter_carry_over(self, mocker):
_, state, _, _ = await self._run(
diff --git a/autogpt_platform/backend/backend/copilot/sdk/chat_text_e2e_test.py b/autogpt_platform/backend/backend/copilot/sdk/chat_text_e2e_test.py
new file mode 100644
index 0000000000..f1cd22e3f6
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/chat_text_e2e_test.py
@@ -0,0 +1,379 @@
+"""Chat text reaches the model as the user wrote it, through whole copilot turns.
+
+``stream_chat_completion_sdk`` drives the bundled Claude Code CLI against the
+billing suite's fake provider, so the CLI options, the CLI version check, the
+session upload and restore, and ``--resume`` are production's (see
+``billing_e2e/sdk_turns.py`` for what is stubbed). Text that names files or
+directories, or starts with a CLI command, must reach the provider as written,
+and nothing those files hold may reach it, on that turn or any later one, or
+be stored with the session.
+
+The Linux SDK wheel bundles the CLI, so without one the tests fail there; on
+other platforms, whose wheels ship without it, they skip. CI runs them on Linux.
+A CLI release that writes what it attached into a kind of session line the
+upload keeps fails them, so an SDK upgrade, which brings its own CLI, is
+checked here.
+"""
+
+import json
+import platform
+from pathlib import Path
+from typing import Any
+from unittest.mock import AsyncMock
+
+import pytest
+from aiohttp import web
+from claude_agent_sdk import ClaudeAgentOptions, ClaudeSDKClient
+
+from backend.copilot.billing_e2e.invoice import SONNET, Route
+from backend.copilot.billing_e2e.ledger import PlatformLedger
+from backend.copilot.billing_e2e.provider import FakeProvider
+from backend.copilot.billing_e2e.sdk_turns import SdkTurns
+from backend.copilot.config import ChatConfig
+from backend.copilot.model import ChatMessage, ChatSession
+from backend.copilot.transcript import cli_session_path
+
+from . import service as sdk_service
+from .cli_openrouter_compat_test import _resolve_cli_path
+
+_NOTE = "The note's own words."
+# A line cut off mid-write, which the upload's strip cannot parse.
+_CUT_OFF_LINE = b'{"type":"attachment","attachment":{"type":"file","con\n'
+_READ_SESSION = sdk_service.read_cli_session_from_disk
+
+
+def ledger(monkeypatch: pytest.MonkeyPatch) -> PlatformLedger:
+ """Takes the spend, cost-log and credit writes each turn makes."""
+ ledger = PlatformLedger()
+ ledger.install(monkeypatch)
+ return ledger
+
+
[email protected](autouse=True)
+def _memory_off(monkeypatch: pytest.MonkeyPatch) -> None:
+ """Memory is not under test here, and its ingest needs a database."""
+ monkeypatch.setattr(
+ sdk_service, "_fetch_graphiti_context", AsyncMock(return_value=(False, ""))
+ )
+
+
+# Its own event loop, like the billing suite: a background task another test
+# left on the shared session loop must not run into this one's turns.
[email protected](loop_scope="function")
+async def test_chat_text_reaches_the_model_as_written(
+ ledger: PlatformLedger, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+):
+ """A fresh turn, a turn resumed on the same pod, and one resumed on a new
+ pod from the stored session."""
+ _require_cli()
+ async with _RecordingProvider() as provider:
+ turns = SdkTurns(tmp_path, monkeypatch, provider, _config())
+ elsewhere = _write_files(tmp_path)
+ prompts = [
+ f"compare @note.txt with @{(elsewhere / 'note.txt').as_posix()}",
+ f"/compact what is in @{elsewhere.as_posix()}/",
+ "/model claude-opus-4-1 then @note.txt",
+ ]
+ session = _session()
+ for turn, prompt in enumerate(prompts):
+ await _run_turn(
+ turns, provider, ledger, session, turn, prompt, new_pod=turn == 2
+ )
+
+ sent = json.dumps([body for _, body in provider.bodies])
+ assert _NOTE not in sent
+ assert "unnamed.txt" not in sent
+ for turn in range(len(prompts)):
+ bodies = provider.bodies_in(turn)
+ assert bodies, f"turn {turn} made no model request"
+ assert {body["model"] for body in bodies} == {SONNET}
+ # Each turn's text and every earlier one: no command cleared or
+ # compacted the conversation in between.
+ expected = [_as_sent(prompt) for prompt in prompts[: turn + 1]]
+ assert _user_texts(bodies[-1])[-len(expected) :] == expected
+ sid = session.session_id
+ assert turns.resumed == [(0, None), (1, sid), (2, sid)]
+
+
[email protected](loop_scope="function")
+async def test_a_session_stored_earlier_resumes_without_what_its_text_named(
+ ledger: PlatformLedger, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+):
+ """A CLI session the CLI wrote while it still expanded the text it was
+ given holds what it attached for that text. Restored on a new pod and
+ resumed, it keeps the turn and drops those attachments."""
+ _require_cli()
+ async with _RecordingProvider() as provider:
+ turns = _StoredEarlierTurns(tmp_path, monkeypatch, provider, _config())
+ elsewhere = _write_files(tmp_path)
+ note = elsewhere / "note.txt"
+ earlier = f"compare @{note.as_posix()} with @{elsewhere.as_posix()}/"
+ session = _session()
+ await _run_turn(turns, provider, ledger, session, 0, earlier, new_pod=False)
+ stored = turns.stored(session.session_id)
+ # Whatever reaches the model later can only come from the session.
+ note.unlink()
+ await _run_turn(turns, provider, ledger, session, 1, "and now?", new_pod=True)
+
+ assert {"file", "directory"} <= _attachment_types(stored)
+ assert turns.resumed == [(0, None), (1, session.session_id)]
+ later = provider.bodies_in(1)
+ assert later, "the resumed turn made no model request"
+ assert _user_texts(later[-1])[-2:] == [earlier, "and now?"]
+ sent = json.dumps(later)
+ assert _NOTE not in sent
+ assert "unnamed.txt" not in sent
+
+
[email protected](loop_scope="function")
+async def test_a_session_is_stored_without_what_its_text_named_or_not_at_all(
+ ledger: PlatformLedger, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+):
+ """The upload leaves out what the CLI attached for the text it was given.
+ A session file the upload cannot read whole is not stored, and the copy
+ stored before it is removed, so the next turn starts a new CLI session
+ from the chat's messages."""
+ _require_cli()
+ async with _RecordingProvider() as provider:
+ turns = _UnreadableSecondTurn(tmp_path, monkeypatch, provider, _config())
+ named = _naming_a_file_and_a_directory(_write_files(tmp_path))
+ session = _session()
+ await _run_turn(turns, provider, ledger, session, 0, named, new_pod=False)
+ stored = turns.stored(session.session_id)
+ await _run_turn(turns, provider, ledger, session, 1, "and now?", new_pod=False)
+ left_stored = turns.has_stored(session.session_id)
+ await _run_turn(turns, provider, ledger, session, 2, "and then?", new_pod=False)
+
+ assert {"file", "directory"} <= _attachment_types(turns.written[0])
+ assert not {"file", "directory"} & _attachment_types(stored)
+ assert _NOTE not in stored.decode()
+ assert "unnamed.txt" not in stored.decode()
+ assert len(turns.written) == 3
+ assert not left_stored
+ assert turns.resumed == [(0, None), (1, session.session_id), (2, None)]
+ rebuilt = provider.bodies_in(2)
+ assert rebuilt, "the turn after the refused session made no model request"
+ # One prompt, which carries the chat's earlier turns ahead of its own text.
+ [prompt] = _user_texts(rebuilt[-1])
+ assert named in prompt and "and now?" in prompt
+ assert prompt.endswith("and then?")
+ assert _NOTE not in json.dumps(rebuilt)
+
+
[email protected](loop_scope="function")
+async def test_a_stored_copy_that_cannot_be_deleted_stays_until_a_turn_replaces_it(
+ ledger: PlatformLedger, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+):
+ """When the copy stored before a refused session file cannot be deleted,
+ the turn still ends, the copy stays as it was, and the next turn resumes
+ from it and stores its own session over it."""
+ _require_cli()
+ async with _RecordingProvider() as provider:
+ turns = _UnreadableSecondTurn(
+ tmp_path, monkeypatch, provider, _config(), delete_fails=True
+ )
+ named = _naming_a_file_and_a_directory(_write_files(tmp_path))
+ session = _session()
+ await _run_turn(turns, provider, ledger, session, 0, named, new_pod=False)
+ stored = turns.stored(session.session_id)
+ await _run_turn(turns, provider, ledger, session, 1, "and now?", new_pod=False)
+ kept = turns.stored(session.session_id)
+ await _run_turn(turns, provider, ledger, session, 2, "and then?", new_pod=False)
+ replaced = turns.stored(session.session_id)
+
+ assert kept == stored
+ sid = session.session_id
+ assert turns.resumed == [(0, None), (1, sid), (2, sid)]
+ resumed = provider.bodies_in(2)
+ assert resumed, "the turn after the refused session made no model request"
+ # Turn 1 is filled in from the chat only when its messages are numbered,
+ # which this suite's are not, so only the stored turn and the new one show.
+ texts = _user_texts(resumed[-1])
+ assert texts[0] == named
+ assert texts[-1].endswith("and then?")
+ assert b"and then?" in replaced and b"and then?" not in stored
+
+
+def _require_cli() -> None:
+ """The Linux SDK wheel bundles the CLI, so there a missing one fails the
+ test instead of skipping it."""
+ if _resolve_cli_path() is not None:
+ return
+ if platform.system() == "Linux":
+ pytest.fail("No Claude Code CLI binary: the Linux SDK wheel bundles one")
+ pytest.skip("No Claude Code CLI binary: this platform's SDK wheel has none")
+
+
+class _RecordingProvider(FakeProvider):
+ """The fake provider, keeping the body of every Messages request."""
+
+ def __init__(self) -> None:
+ super().__init__()
+ self.bodies: list[tuple[int, dict[str, Any]]] = []
+
+ def bodies_in(self, turn: int) -> list[dict[str, Any]]:
+ return [body for request_turn, body in self.bodies if request_turn == turn]
+
+ async def _messages(self, request: web.Request, route: Route) -> web.StreamResponse:
+ self.bodies.append((self.turn, await request.json()))
+ return await super()._messages(request, route)
+
+
+class _ExpandingFirstTurn(SdkTurns):
+ """Turn 0 runs with the CLI expanding the text it is given, as it did
+ before the copilot sent chat text verbatim."""
+
+ def stored(self, session_id: str) -> bytes:
+ content = self._transcripts[session_id].content
+ return content if isinstance(content, bytes) else content.encode()
+
+ def has_stored(self, session_id: str) -> bool:
+ return session_id in self._transcripts
+
+ def _client(
+ self, *args: Any, options: ClaudeAgentOptions, **kwargs: Any
+ ) -> ClaudeSDKClient:
+ if self._turn == 0:
+ options.verbatim_prompts = False
+ return super()._client(*args, options=options, **kwargs)
+
+
+class _StoredEarlierTurns(_ExpandingFirstTurn):
+ """Turn 0 is also stored as the CLI wrote it, the way a session written
+ before the copilot sent chat text verbatim was kept."""
+
+ def _stubs(self, session: ChatSession) -> dict[str, Any]:
+ stubs = super()._stubs(session)
+ if self._turn == 0:
+ stubs["read_cli_session_from_disk"] = _session_file_as_written
+ return stubs
+
+
+class _UnreadableSecondTurn(_ExpandingFirstTurn):
+ """Every turn goes through the production upload reader; turn 1's session
+ file first gains a line cut off mid-write. ``written`` keeps each file as
+ the CLI left it. With ``delete_fails``, deleting a stored copy fails the
+ way ``delete_transcript`` reports a JSONL it could not delete."""
+
+ def __init__(self, *args: Any, delete_fails: bool = False, **kwargs: Any) -> None:
+ super().__init__(*args, **kwargs)
+ self.written: list[bytes] = []
+ self._delete_fails = delete_fails
+
+ def _stubs(self, session: ChatSession) -> dict[str, Any]:
+ stubs = super()._stubs(session)
+ stubs["read_cli_session_from_disk"] = self._read_session
+ if self._delete_fails:
+ stubs["delete_transcript"] = AsyncMock(
+ side_effect=OSError("storage unavailable")
+ )
+ return stubs
+
+ def _read_session(self, sdk_cwd: str, session_id: str, log_prefix: str):
+ path = Path(cli_session_path(sdk_cwd, session_id))
+ self.written.append(path.read_bytes())
+ if self._turn == 1:
+ path.write_bytes(self.written[-1] + _CUT_OFF_LINE)
+ return _READ_SESSION(sdk_cwd, session_id, log_prefix)
+
+
+def _session_file_as_written(sdk_cwd: str, session_id: str, _log_prefix: str) -> bytes:
+ return Path(cli_session_path(sdk_cwd, session_id)).read_bytes()
+
+
+def _config() -> ChatConfig:
+ """A direct-Anthropic deployment, pinned so the deployment's own settings
+ cannot change the path a turn takes, on the CLI ``_require_cli`` found."""
+ cli = _resolve_cli_path()
+ return sdk_service.config.model_copy(
+ update={
+ "use_local": False,
+ "use_claude_code_subscription": False,
+ "use_e2b_sandbox": False,
+ "use_openrouter": False,
+ "api_key": "sk-ant-chat-text-e2e",
+ "claude_agent_model": SONNET,
+ "claude_agent_fallback_model": None,
+ "claude_agent_cli_path": str(cli) if cli else None,
+ "claude_agent_use_resume": True,
+ }
+ )
+
+
+def _session() -> ChatSession:
+ session = ChatSession.new("chat-text-e2e-user", dry_run=False)
+ # A titled chat skips title generation, a separate model call.
+ session.title = "Chat text e2e"
+ return session
+
+
+def _write_files(tmp_path: Path) -> Path:
+ """A note in the turn's working directory, and a note and a file nothing
+ names in a directory outside it, which is returned."""
+ (tmp_path / "copilot-cwd" / "note.txt").write_text(_NOTE)
+ elsewhere = tmp_path / "elsewhere"
+ elsewhere.mkdir()
+ (elsewhere / "note.txt").write_text(_NOTE)
+ (elsewhere / "unnamed.txt").write_text(_NOTE)
+ return elsewhere
+
+
+def _naming_a_file_and_a_directory(elsewhere: Path) -> str:
+ """Text naming the note outside the working directory, and its directory."""
+ return (
+ f"compare @{(elsewhere / 'note.txt').as_posix()} "
+ f"with @{elsewhere.as_posix()}/"
+ )
+
+
+async def _run_turn(
+ turns: SdkTurns,
+ provider: FakeProvider,
+ ledger: PlatformLedger,
+ session: ChatSession,
+ turn: int,
+ prompt: str,
+ *,
+ new_pod: bool,
+) -> None:
+ provider.turn = ledger.turn = turn
+ session.messages.append(ChatMessage(role="user", content=prompt))
+ await turns(session, prompt, new_pod=new_pod)
+ await ledger.settle()
+
+
+def _as_sent(prompt: str) -> str:
+ """The copilot puts a space before a leading "/", so the CLI cannot read
+ the text as one of its commands."""
+ return f" {prompt}" if prompt.startswith("/") else prompt
+
+
+def _user_texts(body: dict[str, Any]) -> list[str]:
+ """Each user message's own text in a request, without the reminders the
+ CLI adds."""
+ texts: list[str] = []
+ for message in body["messages"]:
+ if message["role"] != "user":
+ continue
+ content = message["content"]
+ blocks = (
+ [{"type": "text", "text": content}] if isinstance(content, str) else content
+ )
+ texts += [
+ block["text"]
+ for block in blocks
+ if block.get("type") == "text"
+ and not block["text"].startswith("<system-reminder>")
+ ]
+ return texts
+
+
+def _attachment_types(session: bytes) -> set[str]:
+ """The types of the attachment entries in a CLI session file."""
+ entries = [json.loads(line) for line in session.decode().splitlines() if line]
+ return {
+ entry["attachment"].get("type", "")
+ for entry in entries
+ if entry.get("type") == "attachment"
+ }
diff --git a/autogpt_platform/backend/backend/copilot/sdk/cli_prompt_text_test.py b/autogpt_platform/backend/backend/copilot/sdk/cli_prompt_text_test.py
new file mode 100644
index 0000000000..6f7c07bae6
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/cli_prompt_text_test.py
@@ -0,0 +1,399 @@
+"""Text the copilot hands the Claude Code CLI must reach the model as written.
+
+Runs the real Claude Code CLI against a stub Anthropic server, so a
+``claude-agent-sdk`` bump whose CLI ignores ``verbatim_prompts`` or
+``--disable-slash-commands``, or rewrites the text it is given, fails here.
+The first two tests check one of the two on its own; the third checks a
+session restored through ``process_cli_restore``. The SDK finds its bundled CLI,
+which the Linux wheel ships; ``CHAT_CLAUDE_AGENT_CLI_PATH`` (or
+``CLAUDE_AGENT_CLI_PATH``) points it at another. Only off Linux, with no CLI
+to be found, do the tests skip.
+"""
+
+import asyncio
+import gzip
+import json
+import os
+import platform
+import socket
+import uuid
+import zlib
+from collections.abc import AsyncIterator, Awaitable, Callable
+from contextlib import asynccontextmanager
+from pathlib import Path
+from typing import Any
+
+import pytest
+from aiohttp import web
+from claude_agent_sdk import (
+ ClaudeAgentOptions,
+ ClaudeSDKClient,
+ CLINotFoundError,
+ SystemMessage,
+)
+
+from backend.copilot.transcript import (
+ MENTION_ATTACHMENT_TYPES,
+ TranscriptDownload,
+ cli_session_path,
+)
+
+from .cli_version import require_verbatim_prompts_cli
+from .env import (
+ DISABLE_SLASH_COMMANDS_FLAG,
+ build_sdk_env,
+ cli_prompt_text,
+ cli_user_message,
+)
+from .service import process_cli_restore
+from .tool_adapter import get_copilot_tool_names, get_sdk_disallowed_tools
+
+_MODEL = "claude-sonnet-4-5"
+_GUARDED = [
+ cli_prompt_text(text)
+ for text in ["/compact", "/clear", "/model claude-opus-4-1", "/dataviz a chart"]
+]
+# Sent unguarded, so this turn checks the flag on its own.
+_UNGUARDED = "/model claude-opus-4-1"
+_TURNS = ["hello", *_GUARDED, _UNGUARDED, "still there?"]
+_NOTE = "The note's own words."
+_PNG_1X1 = (
+ "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNkYPhfDwAChwGA"
+ "60e6kgAAAABJRU5ErkJggg=="
+)
+
+Send = Callable[[ClaudeSDKClient], Awaitable[None]]
+
+
+def cli_path(monkeypatch: pytest.MonkeyPatch) -> str:
+ """The CLI the copilot would start: the override if one is set, else the
+ one the SDK finds, as returned by the copilot's version check."""
+ override = os.environ.get("CHAT_CLAUDE_AGENT_CLI_PATH") or os.environ.get(
+ "CLAUDE_AGENT_CLI_PATH"
+ )
+ if override and not Path(override).is_file():
+ pytest.fail(f"The CLI path override {override!r} is not a file")
+ # A run inside Claude Code inherits its CLAUDE_* flags, which change the CLI.
+ for key in [k for k in os.environ if k.startswith("CLAUDE")]:
+ monkeypatch.delenv(key)
+ try:
+ return require_verbatim_prompts_cli(override or None)
+ except CLINotFoundError:
+ if platform.system() == "Linux":
+ raise
+ pytest.skip("No Claude Code CLI: this platform's SDK wheel bundles none")
+
+
+async def test_slash_text_reaches_the_model_as_user_text(cli_path: str, tmp_path: Path):
+ """``--disable-slash-commands`` and the leading space, without
+ ``verbatim_prompts``."""
+ sdk_cwd = _session_dir(tmp_path)
+ async with _stub_server() as (base_url, requests):
+ options = _options(
+ cli_path, base_url, tmp_path, sdk_cwd, verbatim_prompts=False
+ )
+ init = await _run(
+ options, [_query(text, "slash-commands-test") for text in _TURNS]
+ )
+
+ assert init["slash_commands"] == [], init["slash_commands"]
+ assert init["skills"] == [], init["skills"]
+ sent = [_user_texts(r)[-1] for r in requests]
+ assert sent[: len(_GUARDED) + 1] == ["hello", *_GUARDED], sent
+ assert sent[-1] == "still there?", sent
+ assert {r["model"] for r in requests} == {_MODEL}
+ # Nothing compacted or cleared the conversation in between.
+ assert set(_GUARDED) <= set(_user_texts(requests[-1]))
+ assert not (tmp_path / "config" / "settings.json").exists()
+
+
+async def test_prompt_text_reaches_the_model_as_written(cli_path: str, tmp_path: Path):
+ """``verbatim_prompts`` on a fresh turn, a turn with an image and a resumed
+ turn: the model gets each text exactly as sent, unguarded."""
+ sdk_cwd = _session_dir(tmp_path)
+ (sdk_cwd / "note.txt").write_text(_NOTE)
+ elsewhere = tmp_path / "elsewhere"
+ elsewhere.mkdir()
+ (elsewhere / "note.txt").write_text(_NOTE)
+ (elsewhere / "unnamed.txt").write_text(_NOTE)
+ fresh = f"compare @note.txt with @{(elsewhere / 'note.txt').as_posix()}"
+ with_image = f"/compact what is in @{elsewhere.as_posix()}/"
+ resumed = "/model claude-opus-4-1 then @note.txt"
+ session_id = str(uuid.uuid4())
+
+ async with _stub_server() as (base_url, requests):
+ first = _options(
+ cli_path,
+ base_url,
+ tmp_path,
+ sdk_cwd,
+ verbatim_prompts=True,
+ session_id=session_id,
+ )
+ await _run(
+ first, [_query(fresh, session_id), _image_turn(with_image, session_id)]
+ )
+ later = _options(
+ cli_path,
+ base_url,
+ tmp_path,
+ sdk_cwd,
+ verbatim_prompts=True,
+ resume=session_id,
+ )
+ await _run(later, [_query(resumed, session_id)])
+
+ # On an image turn the CLI ends the text with a newline before its note.
+ sent_texts = [_user_texts(r)[-1].rstrip("\n") for r in requests]
+ assert sent_texts == [fresh, with_image, resumed]
+ assert "image" in _block_types(requests[1]["messages"][-1])
+ assert {r["model"] for r in requests} == {_MODEL}
+ sent = json.dumps(requests)
+ assert _NOTE not in sent
+ assert "unnamed.txt" not in sent
+
+
+async def test_restored_session_resumes_without_attachments_from_earlier_text(
+ cli_path: str, tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+):
+ """A session written without ``verbatim_prompts`` goes through
+ ``process_cli_restore`` and resumes with its turns but not the attachments
+ the CLI added for them."""
+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path / "config"))
+ sdk_cwd = _session_dir(tmp_path)
+ elsewhere = tmp_path / "elsewhere"
+ elsewhere.mkdir()
+ note = elsewhere / "note.txt"
+ note.write_text(_NOTE)
+ (elsewhere / "unnamed.txt").write_text(_NOTE)
+ earlier = f"compare @{note.as_posix()} with @{elsewhere.as_posix()}/"
+ session_id = str(uuid.uuid4())
+
+ async with _stub_server() as (base_url, requests):
+ written = _options(
+ cli_path,
+ base_url,
+ tmp_path,
+ sdk_cwd,
+ verbatim_prompts=False,
+ session_id=session_id,
+ )
+ await _run(written, [_query(earlier, session_id)])
+ session_file = Path(cli_session_path(str(sdk_cwd), session_id))
+ stored = session_file.read_bytes()
+ session_file.unlink()
+ note.unlink()
+ scrubbed, restored = process_cli_restore(
+ TranscriptDownload(content=stored, message_count=2),
+ str(sdk_cwd),
+ session_id,
+ "[test]",
+ )
+ resumed = _options(
+ cli_path,
+ base_url,
+ tmp_path,
+ sdk_cwd,
+ verbatim_prompts=True,
+ resume=session_id,
+ )
+ await _run(resumed, [_query("and now?", session_id)])
+
+ assert restored
+ assert len(requests) == 2
+ assert {"file", "directory"} <= _attachment_types(stored)
+ assert not _attachment_types(scrubbed.encode()) & MENTION_ATTACHMENT_TYPES
+ later = json.dumps(requests[1])
+ assert _user_texts(requests[1])[-2:] == [earlier, "and now?"]
+ assert _NOTE not in later
+ assert "unnamed.txt" not in later
+
+
+def _session_dir(tmp_path: Path) -> Path:
+ sdk_cwd = tmp_path / "copilot-session"
+ sdk_cwd.mkdir()
+ return sdk_cwd
+
+
+def _options(
+ cli_path: str,
+ base_url: str,
+ tmp_path: Path,
+ sdk_cwd: Path,
+ **overrides: Any,
+) -> ClaudeAgentOptions:
+ """The copilot's CLI options, pointed at the stub server."""
+ env = build_sdk_env(
+ sdk_cwd=str(sdk_cwd), codex_gateway_url=base_url, codex_gateway_token="stub"
+ )
+ env["CLAUDE_CONFIG_DIR"] = str(tmp_path / "config")
+ env["HOME"] = str(tmp_path)
+ return ClaudeAgentOptions(
+ system_prompt="You are a test assistant.",
+ setting_sources=[],
+ extra_args={"strict-mcp-config": None, DISABLE_SLASH_COMMANDS_FLAG: None},
+ allowed_tools=get_copilot_tool_names(use_e2b=True),
+ disallowed_tools=get_sdk_disallowed_tools(use_e2b=True),
+ cwd=str(sdk_cwd),
+ env=env,
+ model=_MODEL,
+ cli_path=cli_path,
+ **overrides,
+ )
+
+
+async def _run(options: ClaudeAgentOptions, sends: list[Send]) -> dict[str, Any]:
+ """One CLI process, one turn per send; returns the CLI's init data."""
+ init: dict[str, Any] = {}
+
+ async def turns() -> None:
+ nonlocal init
+ async with ClaudeSDKClient(options) as client:
+ for send in sends:
+ await send(client)
+ async for message in client.receive_response():
+ if isinstance(message, SystemMessage) and message.subtype == "init":
+ init = init or message.data
+
+ await asyncio.wait_for(turns(), timeout=120)
+ return init
+
+
+def _query(text: str, session_id: str) -> Send:
+ async def send(client: ClaudeSDKClient) -> None:
+ await client.query(text, session_id=session_id)
+
+ return send
+
+
+def _image_turn(text: str, session_id: str) -> Send:
+ """A turn with an attached image, written the way the copilot writes it."""
+ image = {
+ "type": "image",
+ "source": {"type": "base64", "media_type": "image/png", "data": _PNG_1X1},
+ }
+ line = cli_user_message([image, {"type": "text", "text": text}], session_id)
+
+ async def send(client: ClaudeSDKClient) -> None:
+ assert client._transport is not None
+ await client._transport.write(json.dumps(line) + "\n")
+
+ return send
+
+
+@asynccontextmanager
+async def _stub_server() -> AsyncIterator[tuple[str, list[dict[str, Any]]]]:
+ """A stub Messages API on a free port, and the requests it receives."""
+ requests: list[dict[str, Any]] = []
+ runner = web.AppRunner(_stub_app(requests))
+ await runner.setup()
+ sock = socket.socket()
+ sock.bind(("127.0.0.1", 0))
+ await web.SockSite(runner, sock).start()
+ try:
+ yield f"http://127.0.0.1:{sock.getsockname()[1]}", requests
+ finally:
+ await runner.cleanup()
+
+
+def _stub_app(requests: list[dict[str, Any]]) -> web.Application:
+ async def messages(request: web.Request) -> web.StreamResponse:
+ raw = await request.read()
+ encoding = request.headers.get("Content-Encoding", "")
+ if "gzip" in encoding:
+ raw = gzip.decompress(raw)
+ elif "deflate" in encoding:
+ raw = zlib.decompress(raw)
+ payload = json.loads(raw)
+ requests.append(payload)
+ response = web.StreamResponse(headers={"Content-Type": "text/event-stream"})
+ await response.prepare(request)
+ for event in _reply_events(payload.get("model", _MODEL)):
+ await response.write(
+ f"event: {event['type']}\ndata: {json.dumps(event)}\n\n".encode()
+ )
+ await response.write_eof()
+ return response
+
+ async def count_tokens(_: web.Request) -> web.Response:
+ return web.json_response({"input_tokens": 1})
+
+ app = web.Application(client_max_size=64 * 1024**2)
+ app.router.add_post("/v1/messages", messages)
+ app.router.add_post("/v1/messages/count_tokens", count_tokens)
+ return app
+
+
+def _reply_events(model: str) -> list[dict[str, Any]]:
+ usage = {"input_tokens": 10, "output_tokens": 1}
+ return [
+ {
+ "type": "message_start",
+ "message": {
+ "id": "msg_stub",
+ "type": "message",
+ "role": "assistant",
+ "model": model,
+ "content": [],
+ "stop_reason": None,
+ "stop_sequence": None,
+ "usage": usage,
+ },
+ },
+ {
+ "type": "content_block_start",
+ "index": 0,
+ "content_block": {"type": "text", "text": ""},
+ },
+ {
+ "type": "content_block_delta",
+ "index": 0,
+ "delta": {"type": "text_delta", "text": "ok"},
+ },
+ {"type": "content_block_stop", "index": 0},
+ {
+ "type": "message_delta",
+ "delta": {"stop_reason": "end_turn", "stop_sequence": None},
+ "usage": usage,
+ },
+ {"type": "message_stop"},
+ ]
+
+
+def _user_texts(payload: dict[str, Any]) -> list[str]:
+ """Each user message's own text in the request, without the reminders and
+ image notes the CLI adds."""
+ texts = []
+ for message in payload["messages"]:
+ if message["role"] != "user":
+ continue
+ texts += [
+ block["text"]
+ for block in _blocks(message)
+ if block.get("type") == "text"
+ and not block["text"].startswith(("<system-reminder>", "[Image: "))
+ ]
+ return texts
+
+
+def _attachment_types(session: bytes) -> set[str]:
+ """The types of the attachment entries in a CLI session file."""
+ entries = [json.loads(line) for line in session.decode().splitlines() if line]
+ return {
+ entry["attachment"].get("type", "")
+ for entry in entries
+ if entry.get("type") == "attachment"
+ }
+
+
+def _block_types(message: dict[str, Any]) -> list[str]:
+ return [block.get("type", "") for block in _blocks(message)]
+
+
+def _blocks(message: dict[str, Any]) -> list[dict[str, Any]]:
+ content = message["content"]
+ return [{"type": "text", "text": content}] if isinstance(content, str) else content
diff --git a/autogpt_platform/backend/backend/copilot/sdk/cli_version.py b/autogpt_platform/backend/backend/copilot/sdk/cli_version.py
new file mode 100644
index 0000000000..b1b6b1c35f
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/cli_version.py
@@ -0,0 +1,129 @@
+"""The oldest Claude Code CLI the copilot hands chat text to."""
+
+import functools
+import logging
+import os
+import re
+import subprocess
+from pathlib import Path
+
+logger = logging.getLogger(__name__)
+
+# First CLI release that honours ``client_composed`` on a user message, which
+# ``ClaudeAgentOptions.verbatim_prompts`` sets. Older releases ignore the field
+# without an error, so the version is checked here instead.
+MIN_CLI_VERSION = (2, 1, 248)
+
+_VERSION_PROBE_TIMEOUT_SECONDS = 10
+# The whole first line of ``claude -v``: a release version, optionally followed
+# by the product name. Anything else is refused.
+_VERSION_LINE_RE = re.compile(r"(\d+)\.(\d+)\.(\d+)(?: \(Claude Code\))?")
+# Versions read so far, by the identity of the file they were read from: a
+# binary replaced in place is read again. A failed read is never stored.
+_VERSIONS: dict[tuple[str, int, int, int, int], tuple[int, ...]] = {}
+
+
+def require_verbatim_prompts_cli(cli_path: str | Path | None) -> str:
+ """Return the resolved path of the CLI to start, or raise.
+
+ *cli_path* is ``ClaudeAgentOptions.cli_path``; when it is unset, the CLI
+ the SDK finds on its own is checked. Callers start the returned path, so
+ the binary that runs is the one whose version was read. Each refusal is
+ logged once, with the binary's path; the raised message names only the
+ file.
+ """
+ path = os.path.realpath(cli_path if cli_path else _cli_the_sdk_finds())
+ try:
+ version = _cli_version(path)
+ except RuntimeError as e:
+ logger.error("Refusing to start the Claude Code CLI at %s: %s", path, e)
+ raise
+ if version >= MIN_CLI_VERSION:
+ return path
+ logger.error(
+ "Refusing to start the Claude Code CLI at %s: version %s is older than %s",
+ path,
+ _dotted(version),
+ _dotted(MIN_CLI_VERSION),
+ )
+ raise RuntimeError(
+ f"The Claude Code CLI ({os.path.basename(path)} {_dotted(version)}) is "
+ f"older than {_dotted(MIN_CLI_VERSION)}, the first release that hands "
+ "chat text to the model exactly as written. Install a newer one."
+ )
+
+
+def parse_cli_version(output: str) -> tuple[int, ...] | None:
+ """The version in the output of ``claude -v``, or None when its first line
+ is not exactly ``X.Y.Z``, optionally followed by `` (Claude Code)``."""
+ first_line = output.strip().split("\n", 1)[0].rstrip()
+ match = _VERSION_LINE_RE.fullmatch(first_line)
+ return tuple(int(part) for part in match.groups()) if match else None
+
+
+def _cli_the_sdk_finds() -> str:
+ # Imported here so that loading the block list, which imports this module
+ # through the orchestrator block, does not load the SDK.
+ from claude_agent_sdk import ClaudeAgentOptions
+ from claude_agent_sdk._internal.transport.subprocess_cli import (
+ SubprocessCLITransport,
+ )
+
+ transport = SubprocessCLITransport(prompt="", options=ClaudeAgentOptions())
+ return transport._find_cli()
+
+
+def _cli_version(path: str) -> tuple[int, ...]:
+ identity = _identity(path)
+ if identity in _VERSIONS:
+ return _VERSIONS[identity]
+ version = _probe_version(path)
+ if _identity(path) != identity:
+ raise RuntimeError(
+ f"The Claude Code CLI ({os.path.basename(path)}) changed while its "
+ "version was read"
+ )
+ _VERSIONS[identity] = version
+ return version
+
+
+def _identity(path: str) -> tuple[str, int, int, int, int]:
+ try:
+ stat = os.stat(path)
+ except OSError as e:
+ raise RuntimeError(_cannot_run(path)) from e
+ return (path, stat.st_dev, stat.st_ino, stat.st_size, stat.st_mtime_ns)
+
+
+def _probe_version(path: str) -> tuple[int, ...]:
+ try:
+ result = subprocess.run(
+ [path, "-v"],
+ stdin=subprocess.DEVNULL,
+ capture_output=True,
+ encoding="utf-8",
+ errors="replace",
+ timeout=_VERSION_PROBE_TIMEOUT_SECONDS,
+ check=False,
+ )
+ except (OSError, subprocess.SubprocessError) as e:
+ raise RuntimeError(_cannot_run(path)) from e
+ version = parse_cli_version(result.stdout or "")
+ if result.returncode != 0 or version is None:
+ raise RuntimeError(
+ "Could not read the version of the Claude Code CLI "
+ f"({os.path.basename(path)})"
+ )
+ return version
+
+
+def _cannot_run(path: str) -> str:
+ return (
+ f"Could not run the Claude Code CLI ({os.path.basename(path)}) "
+ "to read its version"
+ )
+
+
+def _dotted(version: tuple[int, ...]) -> str:
+ return ".".join(str(part) for part in version)
diff --git a/autogpt_platform/backend/backend/copilot/sdk/cli_version_test.py b/autogpt_platform/backend/backend/copilot/sdk/cli_version_test.py
new file mode 100644
index 0000000000..382466fb50
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/sdk/cli_version_test.py
@@ -0,0 +1,287 @@
+"""``require_verbatim_prompts_cli`` refuses a CLI older than the floor and
+returns the binary it checked."""
+
+import logging
+import os
+import subprocess
+from pathlib import Path
+from unittest.mock import MagicMock, patch
+
+import pytest
+from claude_agent_sdk import CLINotFoundError
+from claude_agent_sdk._internal.transport.subprocess_cli import SubprocessCLITransport
+
+from backend.copilot.sdk.cli_version import (
+ parse_cli_version,
+ require_verbatim_prompts_cli,
+)
+
+_RUN = "backend.copilot.sdk.cli_version.subprocess.run"
+
+
+def cli(tmp_path: Path) -> str:
+ """A file standing in for the CLI binary; ``subprocess.run`` is patched."""
+ path = tmp_path / "claude"
+ path.write_text("binary")
+ return str(path)
+
+
+def _probe(stdout: str, returncode: int = 0) -> MagicMock:
+ return MagicMock(return_value=MagicMock(stdout=stdout, returncode=returncode))
+
+
+ "stdout",
+ [
+ "2.1.248",
+ "2.1.284 (Claude Code)",
+ "2.1.284 (Claude Code)\n",
+ "2.1.284 (Claude Code)\nsecond line",
+ "3.0.0",
+ ],
+)
+def test_supported_cli_is_accepted_and_its_path_returned(cli: str, stdout: str):
+ with patch(_RUN, _probe(stdout)) as run:
+ checked = require_verbatim_prompts_cli(cli)
+
+ assert checked == os.path.realpath(cli)
+ assert run.call_args.args[0] == [checked, "-v"]
+ assert run.call_args.kwargs["stdin"] is subprocess.DEVNULL
+ assert run.call_args.kwargs["encoding"] == "utf-8"
+ assert run.call_args.kwargs["errors"] == "replace"
+ assert run.call_args.kwargs["timeout"] == 10
+
+
[email protected]("stdout", ["2.1.247 (Claude Code)", "2.0.999", "1.9.300"])
+def test_older_cli_is_refused(cli: str, stdout: str):
+ with (
+ patch(_RUN, _probe(stdout)),
+ pytest.raises(RuntimeError, match="older than 2.1.248"),
+ ):
+ require_verbatim_prompts_cli(cli)
+
+
+ ("stdout", "returncode"),
+ [
+ ("", 0),
+ ("Claude Code", 0),
+ ("2.1.284", 1),
+ ("2.1.300-beta.1 (Claude Code)", 0),
+ ("2.1.300-dev", 0),
+ ("2.1.248evil", 0),
+ ("2.1.248.0", 0),
+ ("v2.1.284", 0),
+ ("2.1.284 (Other)", 0),
+ ("2.1.284 (Claude Code) extra", 0),
+ ("warning: something\n2.1.284 (Claude Code)", 0),
+ ("2.1.284�", 0),
+ ],
+)
+def test_unknown_version_output_is_refused(cli: str, stdout: str, returncode: int):
+ with (
+ patch(_RUN, _probe(stdout, returncode)),
+ pytest.raises(RuntimeError, match="Could not read the version"),
+ ):
+ require_verbatim_prompts_cli(cli)
+
+
+ "error", [OSError("no such file"), subprocess.TimeoutExpired(["claude"], 10)]
+)
+def test_cli_that_will_not_run_is_refused_without_its_directory(
+ cli: str, error: Exception
+):
+ with (
+ patch(_RUN, MagicMock(side_effect=error)),
+ pytest.raises(RuntimeError, match="Could not run") as raised,
+ ):
+ require_verbatim_prompts_cli(cli)
+
+ assert os.path.dirname(cli) not in str(raised.value)
+
+
+def test_missing_cli_is_refused(tmp_path: Path):
+ with (
+ patch(_RUN) as run,
+ pytest.raises(RuntimeError, match="Could not run"),
+ ):
+ require_verbatim_prompts_cli(str(tmp_path / "claude"))
+
+ run.assert_not_called()
+
+
+ ("stdout", "returncode", "error"),
+ [
+ ("2.1.247", 0, None),
+ ("garbage", 0, None),
+ ("", 0, subprocess.TimeoutExpired(["claude"], 10)),
+ ],
+)
+def test_each_refusal_is_logged_once_with_the_path(
+ cli: str,
+ caplog: pytest.LogCaptureFixture,
+ stdout: str,
+ returncode: int,
+ error: Exception | None,
+):
+ run = MagicMock(
+ side_effect=error, return_value=MagicMock(stdout=stdout, returncode=returncode)
+ )
+ with (
+ patch(_RUN, run),
+ caplog.at_level(logging.ERROR, logger="backend.copilot.sdk.cli_version"),
+ pytest.raises(RuntimeError),
+ ):
+ require_verbatim_prompts_cli(cli)
+
+ assert len(caplog.records) == 1
+ assert os.path.realpath(cli) in caplog.records[0].getMessage()
+
+
+def test_without_a_path_the_cli_the_sdk_finds_is_checked(cli: str):
+ with (
+ patch.object(SubprocessCLITransport, "_find_cli", return_value=cli),
+ patch(_RUN, _probe("2.1.284")) as run,
+ ):
+ checked = require_verbatim_prompts_cli(None)
+
+ assert checked == os.path.realpath(cli)
+ assert run.call_args.args[0] == [checked, "-v"]
+
+
+def test_without_a_path_and_no_cli_to_find_the_sdk_error_is_raised():
+ with (
+ patch.object(
+ SubprocessCLITransport,
+ "_find_cli",
+ side_effect=CLINotFoundError("Claude Code not found"),
+ ),
+ patch(_RUN) as run,
+ pytest.raises(CLINotFoundError),
+ ):
+ require_verbatim_prompts_cli(None)
+
+ run.assert_not_called()
+
+
+def test_the_cli_found_first_stays_the_one_returned(cli: str, tmp_path: Path):
+ """A ``PATH`` change after the first check does not swap the binary."""
+ later = tmp_path / "later-claude"
+ later.write_text("another binary")
+ find = MagicMock(side_effect=[cli, str(later)])
+ with (
+ patch.object(SubprocessCLITransport, "_find_cli", find),
+ patch(_RUN, _probe("2.1.284")) as run,
+ ):
+ first = require_verbatim_prompts_cli(None)
+ second = require_verbatim_prompts_cli(None)
+
+ assert first == second == os.path.realpath(cli)
+ assert find.call_count == 1
+ assert run.call_count == 1
+
+
+def test_a_swapped_symlink_returns_and_probes_its_new_target(tmp_path: Path):
+ first, second = tmp_path / "claude-a", tmp_path / "claude-b"
+ first.write_text("binary a")
+ second.write_text("binary b, longer")
+ link = tmp_path / "claude"
+ try:
+ link.symlink_to(first)
+ except OSError:
+ pytest.skip("this account cannot create symlinks")
+ run = MagicMock(
+ side_effect=[
+ MagicMock(stdout="2.1.284", returncode=0),
+ MagicMock(stdout="2.1.247", returncode=0),
+ ]
+ )
+ with patch(_RUN, run):
+ assert require_verbatim_prompts_cli(link) == os.path.realpath(first)
+ link.unlink()
+ link.symlink_to(second)
+ with pytest.raises(RuntimeError, match="older than"):
+ require_verbatim_prompts_cli(link)
+
+ assert [c.args[0][0] for c in run.call_args_list] == [
+ os.path.realpath(first),
+ os.path.realpath(second),
+ ]
+
+
+def test_each_binary_is_probed_once(cli: str, tmp_path: Path):
+ other = tmp_path / "other-claude"
+ other.write_text("binary")
+ with patch(_RUN, _probe("2.1.284")) as run:
+ require_verbatim_prompts_cli(cli)
+ require_verbatim_prompts_cli(cli)
+ require_verbatim_prompts_cli(str(other))
+
+ assert run.call_count == 2
+
+
+def test_a_failed_probe_is_tried_again(cli: str):
+ run = MagicMock(
+ side_effect=[
+ subprocess.TimeoutExpired(["claude"], 10),
+ MagicMock(stdout="2.1.284", returncode=0),
+ ]
+ )
+ with patch(_RUN, run):
+ with pytest.raises(RuntimeError):
+ require_verbatim_prompts_cli(cli)
+ require_verbatim_prompts_cli(cli)
+
+ assert run.call_count == 2
+
+
+def test_a_binary_replaced_in_place_is_probed_again(cli: str):
+ run = MagicMock(
+ side_effect=[
+ MagicMock(stdout="2.1.284", returncode=0),
+ MagicMock(stdout="2.1.247", returncode=0),
+ ]
+ )
+ with patch(_RUN, run):
+ require_verbatim_prompts_cli(cli)
+ Path(cli).write_text("a different binary")
+ with pytest.raises(RuntimeError, match="older than"):
+ require_verbatim_prompts_cli(cli)
+
+ assert run.call_count == 2
+
+
+def test_a_binary_that_changes_while_it_is_probed_is_refused_and_not_kept(cli: str):
+ def replace_during_probe(*_args, **_kwargs):
+ Path(cli).write_text("replaced while running -v")
+ return MagicMock(stdout="2.1.284", returncode=0)
+
+ with patch(_RUN, MagicMock(side_effect=replace_during_probe)):
+ with pytest.raises(RuntimeError, match="changed while"):
+ require_verbatim_prompts_cli(cli)
+ with patch(_RUN, _probe("2.1.284")) as run:
+ require_verbatim_prompts_cli(cli)
+
+ run.assert_called_once()
+
+
+ ("output", "version"),
+ [
+ ("2.1.284", (2, 1, 284)),
+ ("2.1.284 (Claude Code)", (2, 1, 284)),
+ ("2.1.284 (Claude Code)\r\nnext line", (2, 1, 284)),
+ ("\n2.1.284\n", (2, 1, 284)),
+ ("2.1.300-beta.1 (Claude Code)", None),
+ ("Update available\n2.1.284 (Claude Code)", None),
+ ("v2.1.284", None),
+ ("2.1.284 (Claude Code) extra", None),
+ ("2.1.284\ufffd", None),
+ ("", None),
+ ],
+)
+def test_the_version_is_read_from_an_exact_first_line(output: str, version):
+ assert parse_cli_version(output) == version
diff --git a/autogpt_platform/backend/backend/copilot/sdk/conftest.py b/autogpt_platform/backend/backend/copilot/sdk/conftest.py
index fc01a40ab5..ee0f581ff1 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/conftest.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/conftest.py
@@ -8,6 +8,7 @@ from uuid import uuid4
import pytest
import pytest_asyncio
+from backend.copilot.sdk.cli_version import _VERSIONS, _cli_the_sdk_finds
from backend.util import json
@@ -59,6 +60,16 @@ def _clean_config_env(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.delenv(var, raising=False)
[email protected](autouse=True)
+def _fresh_cli_version_checks():
+ """No test sees a CLI version or path another test read."""
+ _VERSIONS.clear()
+ _cli_the_sdk_finds.cache_clear()
+ yield
+ _VERSIONS.clear()
+ _cli_the_sdk_finds.cache_clear()
+
+
@pytest_asyncio.fixture(scope="session", loop_scope="session", name="server")
async def _server_noop() -> None:
"""No-op server stub — SDK tests don't need the full backend."""
diff --git a/autogpt_platform/backend/backend/copilot/sdk/env.py b/autogpt_platform/backend/backend/copilot/sdk/env.py
index 2e37d14b62..2d6845e3d5 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/env.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/env.py
@@ -11,6 +11,7 @@ from __future__ import annotations
import json
import os
import re
+from typing import Any
from urllib.parse import urlparse
from backend.copilot.config import ChatConfig
@@ -28,6 +29,36 @@ _HEADER_SAFE_RE = re.compile(r"[^\x20-\x7e]")
_MAX_HEADER_VALUE_LEN = 128
_LOOPBACK_NO_PROXY_HOSTS = ("127.0.0.1", "localhost", "::1")
+# CLI flag for ``ClaudeAgentOptions.extra_args``. Every prompt we send is user
+# or graph text, never a CLI slash command.
+DISABLE_SLASH_COMMANDS_FLAG = "disable-slash-commands"
+
+
+def cli_prompt_text(text: str) -> str:
+ """*text* as the CLI must receive it to send it on to the model.
+
+ Even with the flag above, the CLI answers a prompt that starts with "/"
+ itself when it names a built-in command. It only looks at the first
+ character, so a leading space keeps the text whole.
+ """
+ return f" {text}" if text.startswith("/") else text
+
+
+def cli_user_message(content: list[dict[str, Any]], session_id: str) -> dict[str, Any]:
+ """A stream-json user message the CLI hands to the model as written.
+
+ ``ClaudeAgentOptions.verbatim_prompts`` marks every message the SDK writes
+ ``client_composed``; a message written straight to the transport must
+ carry the mark itself.
+ """
+ return {
+ "type": "user",
+ "message": {"role": "user", "content": content},
+ "parent_tool_use_id": None,
+ "session_id": session_id,
+ "client_composed": True,
+ }
+
def _loopback_no_proxy_value() -> str:
entries: list[str] = []
diff --git a/autogpt_platform/backend/backend/copilot/sdk/env_test.py b/autogpt_platform/backend/backend/copilot/sdk/env_test.py
index 987f9df497..463282f0ad 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/env_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/env_test.py
@@ -5,6 +5,7 @@ from unittest.mock import patch
import pytest
from backend.copilot.config import ChatConfig
+from backend.copilot.sdk.env import cli_prompt_text, cli_user_message
# ---------------------------------------------------------------------------
# Helpers — build a ChatConfig with explicit field values so tests don't
@@ -749,3 +750,37 @@ class TestContextWindowPin:
_make_config(claude_agent_context_window=99_999)
with pytest.raises(ValidationError):
_make_config(claude_agent_context_window=1_000_001)
+
+
+class TestCliPromptText:
+ """The text the CLI receives must never parse as one of its commands."""
+
+ @pytest.mark.parametrize(
+ "text", ["/compact", "/model claude-opus-4-1", "/", "/dataviz chart"]
+ )
+ def test_leading_slash_gets_a_leading_space(self, text: str):
+ assert cli_prompt_text(text) == f" {text}"
+
+ @pytest.mark.parametrize(
+ "text", ["hello", "", " /compact", "a/b", "<user_context>x</user_context>"]
+ )
+ def test_other_text_is_unchanged(self, text: str):
+ assert cli_prompt_text(text) == text
+
+
+class TestCliUserMessage:
+ """A user message written straight to the CLI's transport."""
+
+ def test_carries_the_mark_the_sdk_sets_for_verbatim_prompts(self):
+ content = [
+ {"type": "image", "source": {"type": "base64", "data": "x"}},
+ {"type": "text", "text": "hello"},
+ ]
+
+ assert cli_user_message(content, "s1") == {
+ "type": "user",
+ "message": {"role": "user", "content": content},
+ "parent_tool_use_id": None,
+ "session_id": "s1",
+ "client_composed": True,
+ }
diff --git a/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py b/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py
index 5bed9a5ecf..a154303b86 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/expert_tool_gate_test.py
@@ -80,6 +80,10 @@ def _make_patches():
side_effect=AssertionError("Provider construction is outside this test")
),
),
+ (
+ f"{_SVC}.require_verbatim_prompts_cli",
+ dict(return_value="/opt/claude/claude"),
+ ),
(f"{_SVC}.create_copilot_mcp_server", dict(new=mcp_server_mock)),
(f"{_SVC}.create_security_hooks", dict(return_value=MagicMock())),
(f"{_SVC}.get_copilot_tool_names", dict(return_value=[])),
diff --git a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
index 33a23ab3b7..ad7d20e5df 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/retry_scenarios_test.py
@@ -67,6 +67,7 @@ from backend.copilot.response_model import (
StreamToolOutputAvailable,
)
from backend.copilot.sdk.compaction import CompactionStats
+from backend.copilot.sdk.env import DISABLE_SLASH_COMMANDS_FLAG
from backend.copilot.sdk.service import _RestoreResult, stream_chat_completion_sdk
from backend.copilot.stream_checkpoint import turn_checkpoint
from backend.copilot.stream_drift.recording import saving_into
@@ -1093,6 +1094,10 @@ def _make_sdk_patches(
dict(new_callable=AsyncMock, return_value=compacted_transcript),
),
(f"{_SVC}.ClaudeSDKClient", dict(side_effect=client_side_effect)),
+ (
+ f"{_SVC}.require_verbatim_prompts_cli",
+ dict(return_value="/opt/claude/claude"),
+ ),
(f"{_SVC}.create_copilot_mcp_server", dict(return_value=MagicMock())),
(f"{_SVC}.create_security_hooks", dict(return_value=MagicMock())),
(f"{_SVC}.get_copilot_tool_names", dict(return_value=[])),
@@ -2438,6 +2443,94 @@ class TestStreamChatCompletionRetryIntegration:
assert not errors, f"Unexpected StreamError: {errors}"
assert any(isinstance(e, StreamStart) for e in events)
+ @pytest.mark.asyncio
+ async def test_every_attempt_hands_the_cli_text_as_text(self):
+ """A chat message must reach the model as written, so the first
+ attempt and the retry's copy of the options both turn the CLI's
+ command parser off and ask for prompts to be delivered verbatim."""
+ session = self._make_session()
+ # The session already holds the message the turn is for, as it does
+ # in production by the time the turn starts.
+ session.messages[-1].content = "/compact"
+ result_msg = self._make_result_message()
+ attempt_options: list = []
+
+ def _client_factory(*args, **kwargs):
+ attempt_options.append(kwargs["options"])
+ if len(attempt_options) == 1:
+ return self._make_client_mock(raises_on_enter=True)
+ return self._make_client_mock(result_message=result_msg)
+
+ transcript = _build_transcript(
+ [("user", "prior question"), ("assistant", "prior answer")]
+ )
+ patches = _make_sdk_patches(
+ session,
+ original_transcript=transcript,
+ compacted_transcript=_build_transcript(
+ [("user", "[summary]"), ("assistant", "summary reply")]
+ ),
+ client_side_effect=_client_factory,
+ )
+
+ with contextlib.ExitStack() as stack:
+ for target, kwargs in patches:
+ stack.enter_context(patch(target, **kwargs))
+ async for _ in stream_chat_completion_sdk(
+ session_id="test-session-id",
+ message="/compact",
+ is_user_message=True,
+ user_id="test-user",
+ session=session,
+ ):
+ pass
+
+ assert len(attempt_options) == 2
+ for options in attempt_options:
+ assert DISABLE_SLASH_COMMANDS_FLAG in options.extra_args
+ assert options.extra_args[DISABLE_SLASH_COMMANDS_FLAG] is None
+ assert options.verbatim_prompts is True
+ assert options.cli_path == "/opt/claude/claude"
+
+ @pytest.mark.asyncio
+ async def test_turn_stops_before_the_cli_starts_when_it_is_too_old(self):
+ """An older CLI ignores ``verbatim_prompts`` without an error, so the
+ turn fails before any CLI process starts."""
+ session = self._make_session()
+ clients: list = []
+ patches = _make_sdk_patches(
+ session,
+ original_transcript=_build_transcript(
+ [("user", "prior question"), ("assistant", "prior answer")]
+ ),
+ compacted_transcript=None,
+ client_side_effect=lambda *a, **kw: clients.append(kw),
+ )
+ events = []
+ with contextlib.ExitStack() as stack:
+ for target, kwargs in patches:
+ stack.enter_context(patch(target, **kwargs))
+ check = stack.enter_context(
+ patch(
+ f"{_SVC}.require_verbatim_prompts_cli",
+ side_effect=RuntimeError("The Claude Code CLI is too old"),
+ )
+ )
+ with pytest.raises(RuntimeError, match="too old"):
+ 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)
+
+ check.assert_called_once()
+ assert clients == []
+ errors = [e for e in events if isinstance(e, StreamError)]
+ assert [e.errorText for e in errors] == ["The Claude Code CLI is too old"]
+
@pytest.mark.asyncio
async def test_all_attempts_exhausted_yields_stream_error(self):
"""All 3 ClaudeSDKClient attempts fail with prompt-too-long.
diff --git a/autogpt_platform/backend/backend/copilot/sdk/sdk_compat_test.py b/autogpt_platform/backend/backend/copilot/sdk/sdk_compat_test.py
index 642fbf3e98..a08abe7181 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/sdk_compat_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/sdk_compat_test.py
@@ -146,6 +146,7 @@ def test_agent_options_accepts_all_our_fields():
"fallback_model",
"max_turns",
"max_budget_usd",
+ "verbatim_prompts",
]
sig = inspect.signature(ClaudeAgentOptions)
for field in fields_we_use:
@@ -315,6 +316,20 @@ def test_bundled_cli_version_is_known_good_against_openrouter():
)
+def test_bundled_cli_hands_prompts_over_as_written():
+ """The copilot refuses to start a CLI older than its floor, so the bundled
+ one must clear it."""
+ from claude_agent_sdk._cli_version import __cli_version__
+
+ from backend.copilot.sdk.cli_version import MIN_CLI_VERSION
+
+ bundled = tuple(int(part) for part in __cli_version__.split("."))
+ assert bundled >= MIN_CLI_VERSION, (
+ f"The bundled Claude Code CLI {__cli_version__} is older than "
+ f"{'.'.join(map(str, MIN_CLI_VERSION))}, which `verbatim_prompts` needs."
+ )
+
+
def test_sdk_exposes_cli_path_option():
"""Sanity-check that the SDK still exposes the `cli_path` option we use
for the OpenRouter workaround. If upstream removes it we need to know."""
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.py
index 5a5aba6338..c7a33950a8 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py
@@ -230,16 +230,19 @@ from ..transcript import (
cleanup_stale_project_dirs,
cli_session_path,
compact_transcript,
+ delete_transcript,
download_transcript,
extract_context_messages,
next_uncovered_sequence,
projects_base,
read_compacted_entries,
+ reconnect_entry,
strip_for_upload,
upload_transcript,
validate_transcript,
)
from ..transcript_builder import TranscriptBuilder, TranscriptSnapshot
+from .cli_version import require_verbatim_prompts_cli
from .compaction import (
CompactionStats,
CompactionTracker,
@@ -247,6 +250,7 @@ from .compaction import (
sdk_compaction_stats,
transcript_stats,
)
+from .env import DISABLE_SLASH_COMMANDS_FLAG, cli_prompt_text, cli_user_message
from .env import build_sdk_env # noqa: F401 — re-export for backward compat
from .governor import TurnGovernor
from .openrouter_cost import TurnGenerations, record_turn_cost_from_openrouter
@@ -1072,9 +1076,33 @@ def _strip_synthetic_reprompt_from_cli_jsonl(content: bytes) -> bytes:
j -= 1
if j >= 0 and _is_empty_assistant_entry(parsed[j][1]):
drop.add(j)
- return b"".join(
- line for idx, (line, _entry) in enumerate(parsed) if idx not in drop
- )
+ if not drop:
+ return content
+ # The CLI links each entry to the previous one by uuid; whatever pointed
+ # at a dropped entry moves to its nearest kept ancestor, so ``--resume``
+ # still walks back through the earlier turns.
+ dropped_uuids = {
+ entry["uuid"]
+ for idx, (_line, entry) in enumerate(parsed)
+ if idx in drop and entry and entry.get("uuid")
+ }
+ uuid_to_parent = {
+ entry["uuid"]: entry.get("parentUuid", "")
+ for _line, entry in parsed
+ if entry and entry.get("uuid")
+ }
+ kept: list[bytes] = []
+ for idx, (line, entry) in enumerate(parsed):
+ if idx in drop:
+ continue
+ if entry is not None and reconnect_entry(entry, dropped_uuids, uuid_to_parent):
+ ending = line[len(line.rstrip(b"\r\n")) :]
+ line = (
+ json.dumps(entry, separators=(",", ":"), ensure_ascii=False).encode()
+ + ending
+ )
+ kept.append(line)
+ return b"".join(kept)
def _strip_ephemeral_memory_from_cli_jsonl(content: bytes) -> bytes:
@@ -2830,6 +2858,11 @@ def delete_stale_cli_session_file(
return False
+class CliSessionScrubError(Exception):
+ """The CLI session file could not be scrubbed whole, so none of it may be
+ stored."""
+
+
def read_cli_session_from_disk(
sdk_cwd: str,
session_id: str,
@@ -2838,7 +2871,8 @@ def read_cli_session_from_disk(
"""Read the CLI session JSONL file from disk after the SDK turn.
Returns the file bytes, or None if the file is missing, outside the
- projects base, or unreadable.
+ projects base, or unreadable. Raises ``CliSessionScrubError`` when the
+ file is not UTF-8 or ``strip_for_upload`` cannot scrub it.
Path-traversal guard: rejects paths outside the CLI projects base.
"""
session_file = cli_session_path(sdk_cwd, session_id)
@@ -2869,25 +2903,20 @@ def read_cli_session_from_disk(
)
return None
- # Strip stale thinking blocks and metadata entries before uploading.
- # Thinking blocks from non-last turns can be massive; keeping them causes
- # the CLI to auto-compact its session when the context window fills up,
- # silently losing conversation history.
+ # Strip stale thinking blocks, metadata and mention attachment entries
+ # before uploading. Thinking blocks from non-last turns can be massive;
+ # keeping them causes the CLI to auto-compact its session when the context
+ # window fills up, silently losing conversation history.
try:
raw_text = raw_bytes.decode("utf-8")
stripped_text = strip_for_upload(raw_text)
stripped_bytes = stripped_text.encode("utf-8")
- except UnicodeDecodeError:
- logger.warning("%s CLI session is not valid UTF-8, uploading raw", log_prefix)
- return raw_bytes
except (OSError, ValueError) as e:
- # OSError: encode/decode I/O failure; ValueError: malformed JSONL in strip.
- # Other unexpected exceptions are not silently swallowed here so they propagate
- # to the outer OSError handler and are logged with exc_info.
- logger.warning(
- "%s Failed to strip CLI session, uploading raw: %s", log_prefix, e
- )
- return raw_bytes
+ # Invalid UTF-8 (a ValueError) or a line the strip cannot parse: the
+ # raw file may hold entries the strip drops, so none of it is stored.
+ raise CliSessionScrubError(
+ f"CLI session {os.path.basename(session_file)} could not be scrubbed"
+ ) from e
if len(stripped_bytes) < len(raw_bytes):
# Write back locally so same-pod turns also benefit.
@@ -2936,7 +2965,13 @@ def process_cli_restore(
)
return "", False
- stripped = strip_for_upload(raw_str)
+ try:
+ stripped = strip_for_upload(raw_str)
+ except ValueError:
+ logger.warning(
+ "%s CLI session content could not be scrubbed, skipping", log_prefix
+ )
+ return "", False
is_valid = validate_transcript(stripped)
# Use len(raw_str) rather than len(cli_restore.content) so the unit is always
# characters (raw_str is always str at this point regardless of input type).
@@ -4395,17 +4430,13 @@ async def _run_stream_attempt(
# with content immediately.
yield StreamStatus(message="Reading your message\u2026")
+ cli_text = cli_prompt_text(state.query_message)
if ctx.attachments.image_blocks:
content_blocks: list[dict[str, Any]] = [
*ctx.attachments.image_blocks,
- {"type": "text", "text": state.query_message},
+ {"type": "text", "text": cli_text},
]
- user_msg = {
- "type": "user",
- "message": {"role": "user", "content": content_blocks},
- "parent_tool_use_id": None,
- "session_id": ctx.session_id,
- }
+ user_msg = cli_user_message(content_blocks, ctx.session_id)
if client._transport is None: # noqa: SLF001
raise RuntimeError("ClaudeSDKClient transport is not initialized")
await client._transport.write(json.dumps(user_msg) + "\n") # noqa: SLF001
@@ -4416,7 +4447,7 @@ async def _run_stream_attempt(
]
)
else:
- await client.query(state.query_message, session_id=ctx.session_id)
+ await client.query(cli_text, session_id=ctx.session_id)
state.transcript_builder.append_user(content=ctx.current_message)
loop_state = _SDKLoopState(
@@ -5646,7 +5677,9 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
# compaction thrash on the very first turn. Prod containers
# have a clean HOME, so this is a no-op there.
setting_sources=[],
- extra_args={"strict-mcp-config": None},
+ extra_args={"strict-mcp-config": None, DISABLE_SLASH_COMMANDS_FLAG: None},
+ # The CLI hands every prompt to the model exactly as written.
+ verbatim_prompts=True,
allowed_tools=allowed,
disallowed_tools=disallowed,
hooks=security_hooks,
@@ -5716,6 +5749,9 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
# back to the bundled binary when unset.
if config.claude_agent_cli_path:
sdk_options.cli_path = config.claude_agent_cli_path
+ sdk_options.cli_path = await asyncio.to_thread(
+ require_verbatim_prompts_cli, sdk_options.cli_path
+ )
adapter = SDKResponseAdapter(
message_id=message_id,
@@ -7061,6 +7097,21 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues
log_prefix=log_prefix,
)
)
+ except CliSessionScrubError as scrub_err:
+ # Nothing of it is stored, and the copy stored before it goes
+ # too: the next turn starts from the chat's messages instead.
+ logger.warning("%s %s; not uploading it", log_prefix, scrub_err)
+ try:
+ await asyncio.shield(delete_transcript(user_id, session_id))
+ except Exception as delete_err:
+ # The earlier copy stays with its watermark, like a missed
+ # upload: the next turn resumes from it after the restore's
+ # scrub, and its own upload replaces it.
+ logger.error(
+ "%s Failed to delete the stored CLI session: %s",
+ log_prefix,
+ delete_err,
+ )
except Exception as cli_upload_err:
logger.warning(
"%s CLI session upload failed in finally: %s",
diff --git a/autogpt_platform/backend/backend/copilot/sdk/service_test.py b/autogpt_platform/backend/backend/copilot/sdk/service_test.py
index 8a17a978eb..7f6a4c9ed4 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/service_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/service_test.py
@@ -6,6 +6,7 @@ import json
import logging
import os
from dataclasses import dataclass
+from datetime import UTC, datetime
from types import SimpleNamespace
from typing import cast
from unittest.mock import AsyncMock, MagicMock, patch
@@ -21,13 +22,15 @@ from backend.copilot.graphiti.context_marker import (
append_injected_memory_block,
mark_injected_memory_block,
)
+from backend.copilot.model import ChatSession
from backend.copilot.model_router import ResolvedModel
from backend.copilot.permissions import CopilotPermissions, all_known_tool_names
from backend.data.sharing.workspace_refs import extract_workspace_file_ids
from .codex_compat_gateway import CodexAnthropicGateway
-from .compaction import CompactionStats
+from .compaction import CompactionStats, CompactionTracker
from .cost_tracking import TokenUsage
+from .response_adapter import SDKResponseAdapter
from .service import (
_HUNG_TOOL_CAP_SECONDS,
_IDLE_TIMEOUT_SECONDS,
@@ -48,8 +51,10 @@ from .service import (
_resolve_sdk_model,
_resolve_sdk_model_for_request,
_RetryState,
+ _run_stream_attempt,
_safe_close_sdk_client,
_start_follow_up_warm_context,
+ _StreamContext,
_strip_ephemeral_memory_from_cli_jsonl,
_strip_synthetic_reprompt_from_cli_jsonl,
_TokenUsage,
@@ -1488,6 +1493,31 @@ class TestStripSyntheticReprompt:
)
assert result == prior_user + final_assistant
+ def test_what_pointed_at_a_dropped_line_points_past_it(self):
+ """The CLI links each entry to the one before it; an entry whose
+ parent or leaf was dropped points at the nearest kept ancestor
+ instead, so --resume still reaches the earlier turns and the answer
+ (the chain CLI 2.1.284 writes for a re-prompted turn)."""
+ entries = [
+ {"type": "user", "uuid": "u1", "parentUuid": None, "message": {"role": "user", "content": "hi"}},
+ {"type": "assistant", "uuid": "a2", "parentUuid": "u1", "message": {"role": "assistant", "content": [{"type": "thinking", "thinking": "hmm"}]}},
+ {"type": "user", "uuid": "u3", "parentUuid": "a2", "message": {"role": "user", "content": [{"type": "text", "text": _THINKING_ONLY_REPROMPT}]}},
+ {"type": "attachment", "uuid": "k4", "parentUuid": "u3", "attachment": {"type": "date"}},
+ {"type": "last-prompt", "leafUuid": "u3"},
+ {"type": "assistant", "uuid": "a5", "parentUuid": "k4", "message": {"role": "assistant", "content": [{"type": "text", "text": "Here you go."}]}},
+ ] # fmt: skip
+ content = b"".join(json.dumps(entry).encode() + b"\n" for entry in entries)
+
+ result = [
+ json.loads(line)
+ for line in _strip_synthetic_reprompt_from_cli_jsonl(content).splitlines()
+ ]
+
+ assert [entry.get("uuid") for entry in result] == ["u1", "k4", None, "a5"]
+ assert result[1]["parentUuid"] == "u1"
+ assert result[2]["leafUuid"] == "u1"
+ assert result[3]["parentUuid"] == "k4"
+
class TestConsumeSdkUntilDone:
"""Integration coverage for the extracted SDK consume loop.
@@ -2203,6 +2233,92 @@ class TestStreamEndedWithoutResultMessage:
assert governor.landed is True
+class TestCliBoundUserText:
+ """A message such as "/compact" reaches the CLI with a leading space, so
+ the CLI sends it to the model instead of answering it as a command, while
+ the transcript keeps what the user typed."""
+
+ def _ctx_and_state(self, image_blocks: list[dict]):
+ now = datetime.now(UTC)
+ session = ChatSession(
+ session_id="s1",
+ user_id="u-1",
+ usage=[],
+ started_at=now,
+ updated_at=now,
+ messages=[],
+ )
+ ctx = _StreamContext(
+ session=session,
+ session_id="s1",
+ log_prefix="[SDK] [s1]",
+ sdk_cwd="/tmp/test",
+ current_message="/compact",
+ file_ids=None,
+ message_id="m-1",
+ attachments=MagicMock(image_blocks=image_blocks),
+ compaction=CompactionTracker(),
+ lock=MagicMock(refresh=AsyncMock()),
+ )
+ state = _RetryState(
+ options=MagicMock(resume=None, session_id="s1"),
+ query_message="/compact",
+ compaction_stats=None,
+ use_resume=True,
+ resume_file="s1",
+ transcript_msg_count=0,
+ adapter=SDKResponseAdapter(message_id="m-1", session_id="s1"),
+ transcript_builder=MagicMock(),
+ usage=_TokenUsage(),
+ )
+ return ctx, state
+
+ async def _send(self, image_blocks: list[dict]):
+ async def no_messages(_client, wake=None, tool_display_wake=None):
+ for message in ():
+ yield message
+
+ ctx, state = self._ctx_and_state(image_blocks)
+ client = MagicMock(query=AsyncMock(), _transport=MagicMock(write=AsyncMock()))
+ sdk_client = MagicMock(
+ __aenter__=AsyncMock(return_value=client),
+ __aexit__=AsyncMock(return_value=False),
+ )
+ with (
+ patch(
+ "backend.copilot.sdk.service.ClaudeSDKClient",
+ return_value=sdk_client,
+ ),
+ patch("backend.copilot.sdk.service._iter_sdk_messages", new=no_messages),
+ ):
+ async for _ in _run_stream_attempt(ctx, state):
+ pass
+ return client, state.transcript_builder
+
+ @pytest.mark.asyncio
+ async def test_text_turn(self):
+ client, transcript = await self._send(image_blocks=[])
+
+ client.query.assert_awaited_once_with(" /compact", session_id="s1")
+ transcript.append_user.assert_called_once_with(content="/compact")
+
+ @pytest.mark.asyncio
+ async def test_turn_with_images(self):
+ image = {"type": "image", "source": {"type": "base64", "data": "x"}}
+ client, transcript = await self._send(image_blocks=[image])
+
+ line = json.loads(client._transport.write.await_args.args[0])
+ assert line["message"]["content"] == [
+ image,
+ {"type": "text", "text": " /compact"},
+ ]
+ # The SDK's verbatim_prompts mark only reaches lines it writes itself.
+ assert line["client_composed"] is True
+ transcript.append_user.assert_called_once_with(
+ content=[image, {"type": "text", "text": "/compact"}]
+ )
+
+
class TestHiddenShortNamesForPermissions:
"""``_hidden_short_names_for_permissions`` is the pure function that
decides which MCP tools to skip at registration so the model never sees
diff --git a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
index e018ac3438..cf0fc89c9e 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter.py
@@ -1139,7 +1139,9 @@ _SDK_BUILTIN_TOOLS = [*_SDK_BUILTIN_FILE_TOOLS, *_SDK_BUILTIN_ALWAYS]
# ListAgents/SendMessage: reach other CLI sessions in the same pod.
# NotebookEdit: writes files outside the MCP Write/Edit tools.
# Skill: Claude Code's own skills; copilot skills go through read_skill.
-# Workflow: spawns multi-agent runs with no copilot budget or UI.
+# Workflow: spawns multi-agent runs with no copilot budget or UI. With
+# slash commands disabled its description also grows from ~5K to ~20K
+# chars on every request.
# EnterWorktree/ExitWorktree/ReportFindings: Claude Code dev-loop tools.
# TaskStop: unused here, and permissions cannot name it, so filtered sessions
# would drop it anyway.
diff --git a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
index 93cc7fcfc8..232166ce0a 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/tool_adapter_test.py
@@ -751,6 +751,10 @@ class TestSDKDisallowedTools:
def test_schedule_wakeup_tool_is_disallowed(self):
assert "ScheduleWakeup" in SDK_DISALLOWED_TOOLS
+ @pytest.mark.parametrize("use_e2b", [False, True])
+ def test_workflow_tool_is_disallowed(self, use_e2b: bool):
+ assert "Workflow" in get_sdk_disallowed_tools(use_e2b=use_e2b)
+
@pytest.mark.parametrize("tool", ["CronCreate", "CronList", "CronDelete"])
def test_cli_cron_tools_are_disallowed(self, tool: str):
"""The CLI's cron built-ins schedule nothing that outlives the turn.
diff --git a/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py b/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py
index 4b8849fb57..819b55f059 100644
--- a/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py
+++ b/autogpt_platform/backend/backend/copilot/sdk/transcript_test.py
@@ -298,7 +298,7 @@ class TestStripProgressEntries:
class TestDeleteTranscript:
@pytest.mark.asyncio
async def test_deletes_cli_session_and_meta(self):
- """delete_transcript removes the CLI session .jsonl and .meta.json."""
+ """delete_transcript removes the CLI session .jsonl, then .meta.json."""
mock_storage = AsyncMock()
mock_storage.delete = AsyncMock()
@@ -309,32 +309,44 @@ class TestDeleteTranscript:
):
await delete_transcript("user-123", "session-456")
- assert mock_storage.delete.call_count == 2
paths = [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)
-
+ assert len(paths) == 2
+ assert paths[0].endswith(".jsonl")
+ assert paths[1].endswith(".meta.json")
+
+ @pytest.mark.parametrize(
+ "delete_effect",
+ [
+ [OSError("jsonl delete failed"), None],
+ OSError("storage unavailable"),
+ ],
+ ids=["jsonl-fails", "both-would-fail"],
+ )
@pytest.mark.asyncio
- async def test_continues_on_jsonl_delete_failure(self):
- """If .jsonl delete fails, .meta.json delete is still attempted."""
+ async def test_a_jsonl_it_cannot_delete_raises_and_keeps_its_meta(
+ self, delete_effect
+ ):
+ """A JSONL left without its meta would be resumed as covering the
+ whole chat, so the meta stays and the caller hears of the failure."""
mock_storage = AsyncMock()
- mock_storage.delete = AsyncMock(
- side_effect=[Exception("jsonl delete failed"), None]
- )
+ mock_storage.delete = AsyncMock(side_effect=delete_effect)
with patch(
"backend.copilot.transcript.get_workspace_storage",
new_callable=AsyncMock,
return_value=mock_storage,
):
- # Should not raise
- await delete_transcript("user-123", "session-456")
+ with pytest.raises(OSError):
+ await delete_transcript("user-123", "session-456")
- assert mock_storage.delete.call_count == 2
+ paths = [call.args[0] for call in mock_storage.delete.call_args_list]
+ assert len(paths) == 1
+ assert paths[0].endswith(".jsonl")
@pytest.mark.asyncio
async def test_handles_meta_delete_failure(self):
- """If .meta.json delete fails, no exception propagates."""
+ """If .meta.json delete fails once the .jsonl is gone, no exception
+ propagates: a meta without its JSONL is never restored."""
mock_storage = AsyncMock()
mock_storage.delete = AsyncMock(
side_effect=[None, Exception("meta delete failed")]
@@ -348,6 +360,8 @@ class TestDeleteTranscript:
# Should not raise
await delete_transcript("user-123", "session-456")
+ assert mock_storage.delete.call_count == 2
+
# --- read_compacted_entries ---
@@ -1508,6 +1522,32 @@ class TestProcessCliRestore:
assert not ok
assert stripped_str == ""
+ def test_content_it_cannot_scrub_returns_false(self, tmp_path):
+ """A line the strip cannot parse could be an attachment it must drop,
+ so the session is not restored and nothing is written."""
+ from backend.copilot.sdk.service import process_cli_restore
+ from backend.copilot.transcript import TranscriptDownload
+
+ raw_content = (
+ '{"type":"user","uuid":"u1","parentUuid":null,"message":{"role":"user","content":"hi"}}\n'
+ '{"type":"assistant","uuid":"a1","parentUuid":"u1","message":{"role":"assistant","content":[{"type":"text","text":"hello"}]}}\n'
+ '{"type":"attachment","uuid":"m1","attachment":{"type":"file","con\n'
+ )
+ restore = TranscriptDownload(
+ content=raw_content.encode("utf-8"), message_count=2, mode="sdk"
+ )
+
+ with patch(
+ "backend.copilot.sdk.service._write_cli_session_to_disk"
+ ) as write_to_disk:
+ stripped_str, ok = process_cli_restore(
+ restore, str(tmp_path), "12345678-0000-0000-0000-000000000098", "[Test]"
+ )
+
+ assert not ok
+ assert stripped_str == ""
+ write_to_disk.assert_not_called()
+
class TestReadCliSessionFromDisk:
"""``read_cli_session_from_disk`` reads, strips, and optionally writes back the session."""
@@ -1524,18 +1564,37 @@ class TestReadCliSessionFromDisk:
session_dir.mkdir(parents=True, exist_ok=True)
return sdk_cwd, session_dir / f"{session_id}.jsonl"
- def test_returns_raw_bytes_for_invalid_utf8(self, tmp_path):
- """Non-UTF-8 bytes trigger UnicodeDecodeError — returns raw bytes (upload-raw fallback)."""
+ @pytest.mark.parametrize(
+ "unreadable",
+ [
+ b"\xff\xfe invalid utf-8\n",
+ b'{"type":"attachment","uuid":"m2","attachment":{"type":"file","con\n',
+ ],
+ ids=["invalid utf-8", "truncated attachment line"],
+ )
+ def test_a_session_it_cannot_scrub_whole_is_refused(self, tmp_path, unreadable):
+ """Bytes the strip cannot read make the whole file unreadable for
+ upload, the mention attachment before them included."""
from unittest.mock import patch
- from backend.copilot.sdk.service import read_cli_session_from_disk
+ from backend.copilot.sdk.service import (
+ CliSessionScrubError,
+ read_cli_session_from_disk,
+ )
session_id = "12345678-0000-0000-0000-aabbccdd0001"
projects_base_dir = str(tmp_path)
sdk_cwd, session_file = self._build_session_file(tmp_path, session_id)
-
- # Write raw invalid UTF-8 bytes
- session_file.write_bytes(b"\xff\xfe invalid utf-8\n")
+ content = (
+ b'{"type":"user","uuid":"u1","parentUuid":null,'
+ b'"message":{"role":"user","content":"read @note.txt"}}\n'
+ b'{"type":"attachment","uuid":"m1","parentUuid":"u1",'
+ b'"attachment":{"type":"file","filename":"note.txt",'
+ b'"content":"The note\'s own words."}}\n'
+ b'{"type":"assistant","uuid":"a1","parentUuid":"m1",'
+ b'"message":{"role":"assistant","content":[{"type":"text","text":"ok"}]}}\n'
+ ) + unreadable
+ session_file.write_bytes(content)
with (
patch(
@@ -1546,11 +1605,12 @@ class TestReadCliSessionFromDisk:
"backend.copilot.transcript.projects_base",
return_value=projects_base_dir,
),
+ pytest.raises(CliSessionScrubError),
):
- result = read_cli_session_from_disk(sdk_cwd, session_id, "[Test]")
+ read_cli_session_from_disk(sdk_cwd, session_id, "[Test]")
- # UnicodeDecodeError path returns the raw bytes (upload-raw fallback)
- assert result == b"\xff\xfe invalid utf-8\n"
+ # Nothing was written back over the file either.
+ assert session_file.read_bytes() == content
def test_write_back_oserror_still_returns_stripped_bytes(self, tmp_path):
"""OSError on write-back returns stripped bytes for GCS upload (not raw)."""
diff --git a/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py b/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py
index 47af6c7a05..f86a3dffbe 100644
--- a/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py
+++ b/autogpt_platform/backend/backend/copilot/stream_drift/scripted.py
@@ -192,6 +192,7 @@ async def sdk_service_turn(
stubs: dict[str, Any] = {
"ClaudeSDKClient": MagicMock(side_effect=cli),
+ "require_verbatim_prompts_cli": MagicMock(return_value="claude"),
"AsyncClusterLock": MagicMock(side_effect=lock),
"_make_sdk_cwd": MagicMock(return_value=tempfile.mkdtemp()),
"propagate_attributes": MagicMock(),
diff --git a/autogpt_platform/backend/backend/copilot/transcript.py b/autogpt_platform/backend/backend/copilot/transcript.py
index 75ae992d1e..51f3258718 100644
--- a/autogpt_platform/backend/backend/copilot/transcript.py
+++ b/autogpt_platform/backend/backend/copilot/transcript.py
@@ -46,10 +46,46 @@ _SAFE_ID_RE = re.compile(r"[^0-9a-fA-F-]")
# - queue-operation: internal queue bookkeeping
# - summary: session summaries
# - pr-link: PR link metadata
+# - api-request-blob: a copy of an API request as sent, written by newer CLIs,
+# so it holds whatever the CLI attached to the prompt; ``--resume`` does not
+# send it again
STRIPPABLE_TYPES = frozenset(
- {"progress", "file-history-snapshot", "queue-operation", "summary", "pr-link"}
+ {
+ "progress",
+ "file-history-snapshot",
+ "queue-operation",
+ "summary",
+ "pr-link",
+ "api-request-blob",
+ }
)
+# ``attachment`` entries the CLI writes for a file, directory, image, agent or
+# MCP resource named in the prompt text: the first attachment, the
+# re-attachment after a compaction, and its later reports on such a file.
+# Prompts sent with ``verbatim_prompts`` produce none of them, so sessions
+# written before that are stored and restored without them.
+MENTION_ATTACHMENT_TYPES = frozenset(
+ {
+ "file",
+ "already_read_file",
+ "compact_file_reference",
+ "pdf_reference",
+ "directory",
+ "agent_mention",
+ "mcp_resource",
+ "peer_mention",
+ "at_mention_reference",
+ "edited_text_file",
+ "edited_image_file",
+ }
+)
+
+# Fields that point at another entry's ``uuid``.
+_UUID_REFERENCE_FIELDS = ("parentUuid", "logicalParentUuid", "leafUuid")
+# Parts of a compact boundary's ``compactMetadata`` that name entries by uuid.
+_PRESERVED_SECTIONS = ("preservedSegment", "preservedMessages")
+
TranscriptMode = Literal["sdk", "baseline"]
@@ -228,11 +264,18 @@ def strip_stale_thinking_blocks(content: str) -> str:
def strip_for_upload(content: str) -> str:
- """Combined single-parse strip of progress entries and stale thinking blocks.
-
- Equivalent to ``strip_stale_thinking_blocks(strip_progress_entries(content))``
- but parses the JSONL only once, avoiding redundant ``split`` + ``json.loads``
- passes on every upload.
+ """Combined single-parse strip of progress entries, mention attachments and
+ stale thinking blocks.
+
+ Like ``strip_stale_thinking_blocks(strip_progress_entries(content))``, but
+ it also drops ``MENTION_ATTACHMENT_TYPES`` attachments and parses the JSONL
+ only once. The parent, logical-parent and leaf references to a dropped
+ entry move to its nearest kept ancestor, and a compact boundary's
+ preserved-message references are repaired too, so no kept entry names a
+ dropped one and the conversation tree ``--resume`` walks stays intact.
+
+ Raises ``ValueError`` when a non-blank line is not a JSON object: such a
+ line could be an entry that must be dropped, so nothing is returned.
"""
lines = content.strip().split("\n")
if not lines:
@@ -240,9 +283,12 @@ def strip_for_upload(content: str) -> str:
parsed: list[tuple[str, dict | None]] = []
for line in lines:
- parsed.append((line, json.loads(line, fallback=None)))
+ entry = json.loads(line, fallback=None)
+ if line.strip() and not isinstance(entry, dict):
+ raise ValueError("A CLI session line is not a JSON object")
+ parsed.append((line, entry))
- # --- Phase 1: progress stripping (reparent children) ---
+ # --- Phase 1: drop entries, reconnect what pointed at them ---
stripped_uuids: set[str] = set()
uuid_to_parent: dict[str, str] = {}
@@ -253,28 +299,15 @@ def strip_for_upload(content: str) -> str:
parent = entry.get("parentUuid", "")
if uid:
uuid_to_parent[uid] = parent
- if (
- entry.get("type", "") in STRIPPABLE_TYPES
- and uid
- and not entry.get("isCompactSummary")
- ):
+ if uid and _is_dropped_entry(entry):
stripped_uuids.add(uid)
- reparented: set[str] = set()
- for _line, entry in parsed:
- if not isinstance(entry, dict):
- continue
- parent = entry.get("parentUuid", "")
- original_parent = parent
- seen_parents: set[str] = set()
- while parent in stripped_uuids and parent not in seen_parents:
- seen_parents.add(parent)
- parent = uuid_to_parent.get(parent, "")
- if parent != original_parent:
- entry["parentUuid"] = parent
- uid = entry.get("uuid", "")
- if uid:
- reparented.add(uid)
+ reconnected: set[int] = set()
+ for i, (_line, entry) in enumerate(parsed):
+ if isinstance(entry, dict) and reconnect_entry(
+ entry, stripped_uuids, uuid_to_parent
+ ):
+ reconnected.add(i)
# --- Phase 2: identify last assistant for thinking-block stripping ---
last_asst_msg_id: str | None = None
@@ -283,9 +316,7 @@ def strip_for_upload(content: str) -> str:
_line, entry = parsed[i]
if not isinstance(entry, dict):
continue
- if entry.get("type", "") in STRIPPABLE_TYPES and not entry.get(
- "isCompactSummary"
- ):
+ if _is_dropped_entry(entry):
continue
msg = entry.get("message", {})
if msg.get("role") == "assistant":
@@ -301,18 +332,11 @@ def strip_for_upload(content: str) -> str:
result_lines.append(line)
continue
- # Drop progress/metadata entries
- if entry.get("type", "") in STRIPPABLE_TYPES and not entry.get(
- "isCompactSummary"
- ):
+ if _is_dropped_entry(entry):
continue
- needs_reserialize = False
- uid = entry.get("uuid", "")
-
- # Reparented entries need re-serialization
- if uid in reparented:
- needs_reserialize = True
+ # Entries whose references moved need re-serialization
+ needs_reserialize = i in reconnected
# Strip stale thinking blocks from non-last assistant entries.
# Also strip *signature-less* thinking blocks from the last entry —
@@ -359,6 +383,110 @@ def strip_for_upload(content: str) -> str:
return "\n".join(result_lines) + "\n"
+def _is_dropped_entry(entry: dict) -> bool:
+ """Whether ``strip_for_upload`` leaves *entry* out. Of the strippable
+ types, only a summary is kept for being a compact summary."""
+ entry_type = entry.get("type", "")
+ if entry_type in STRIPPABLE_TYPES:
+ return not (entry_type == "summary" and entry.get("isCompactSummary"))
+ attachment = entry.get("attachment")
+ return (
+ entry_type == "attachment"
+ and isinstance(attachment, dict)
+ and attachment.get("type") in MENTION_ATTACHMENT_TYPES
+ )
+
+
+def reconnect_entry(
+ entry: dict, stripped_uuids: set[str], uuid_to_parent: dict[str, str]
+) -> bool:
+ """Point *entry*'s references past dropped entries; True if any moved.
+
+ *uuid_to_parent* maps every entry's ``uuid`` to its ``parentUuid``, the
+ dropped entries' included, so a reference can move to the nearest kept
+ ancestor.
+ """
+ moved = False
+ for field in _UUID_REFERENCE_FIELDS:
+ if _reconnect_field(entry, field, stripped_uuids, uuid_to_parent):
+ moved = True
+ if _reconnect_compact_metadata(entry, stripped_uuids, uuid_to_parent):
+ moved = True
+ return moved
+
+
+def _reconnect_field(
+ holder: dict, field: str, stripped_uuids: set[str], uuid_to_parent: dict[str, str]
+) -> bool:
+ """Move one uuid field to the nearest kept ancestor; True if it moved."""
+ target = holder.get(field)
+ seen: set[str] = set()
+ while isinstance(target, str) and target in stripped_uuids and target not in seen:
+ seen.add(target)
+ target = uuid_to_parent.get(target, "")
+ if target != holder.get(field):
+ holder[field] = target
+ return True
+ return False
+
+
+def _reconnect_compact_metadata(
+ entry: dict, stripped_uuids: set[str], uuid_to_parent: dict[str, str]
+) -> bool:
+ """Repair a compact boundary's ``compactMetadata`` so no reference in it
+ names a dropped entry. The preserved messages are a chain from
+ ``headUuid`` to ``tailUuid``: the uuid lists lose the dropped ids, a
+ dropped head moves to the first kept member, and the tail and anchors move
+ to their nearest kept ancestor. A segment with no kept member is removed."""
+ metadata = entry.get("compactMetadata")
+ if not isinstance(metadata, dict):
+ return False
+ moved = False
+ for section_key in _PRESERVED_SECTIONS:
+ section = metadata.get(section_key)
+ if not isinstance(section, dict):
+ continue
+ for field in ("uuids", "allUuids"):
+ values = section.get(field)
+ if isinstance(values, list):
+ kept = [uid for uid in values if uid not in stripped_uuids]
+ if kept != values:
+ section[field] = kept
+ moved = True
+ head = section.get("headUuid")
+ if isinstance(head, str) and head in stripped_uuids:
+ moved = True
+ new_head = _first_kept_member(
+ head, section.get("tailUuid"), stripped_uuids, uuid_to_parent
+ )
+ if new_head is None:
+ del metadata[section_key]
+ continue
+ section["headUuid"] = new_head
+ for field in ("anchorUuid", "tailUuid"):
+ if _reconnect_field(section, field, stripped_uuids, uuid_to_parent):
+ moved = True
+ return moved
+
+
+def _first_kept_member(
+ head: str, tail: object, stripped_uuids: set[str], uuid_to_parent: dict[str, str]
+) -> str | None:
+ """The kept entry nearest *head* on the parent chain from *tail* back to
+ *head*, or None when none is kept or the chain does not reach *head*."""
+ found: str | None = None
+ current = tail
+ seen: set[str] = set()
+ while isinstance(current, str) and current not in seen:
+ seen.add(current)
+ if current not in stripped_uuids:
+ found = current
+ if current == head:
+ return found
+ current = uuid_to_parent.get(current)
+ return None
+
+
# ---------------------------------------------------------------------------
# Local file I/O (write temp file for --resume)
# ---------------------------------------------------------------------------
@@ -1108,17 +1236,20 @@ async def extract_context_messages(
async def delete_transcript(user_id: str, session_id: str) -> None:
- """Delete CLI session JSONL and its companion .meta.json from bucket storage."""
- storage = await get_workspace_storage()
+ """Delete CLI session JSONL, then its companion .meta.json, from bucket storage.
- 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)
+ Raises when the JSONL cannot be deleted, and leaves the meta in place: a
+ JSONL without its meta reads as an old upload that covers the whole chat
+ (``download_transcript``), which a later turn would resume with no gap. A
+ meta left without its JSONL is never restored, so failing to delete it is
+ only logged.
+ """
+ storage = await get_workspace_storage()
+ 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)
try:
cli_meta_path = _build_path_from_parts(
diff --git a/autogpt_platform/backend/backend/copilot/transcript_scrub_test.py b/autogpt_platform/backend/backend/copilot/transcript_scrub_test.py
new file mode 100644
index 0000000000..ae4b5db3e7
--- /dev/null
+++ b/autogpt_platform/backend/backend/copilot/transcript_scrub_test.py
@@ -0,0 +1,376 @@
+"""``strip_for_upload`` and the CLI's mention attachments: which attachment
+types it drops, which it keeps, and that no kept entry of a compacted session
+names a dropped one afterwards."""
+
+from __future__ import annotations
+
+from typing import Any
+
+import pytest
+
+from backend.util import json
+
+from .transcript import strip_for_upload
+
+# Attachment types the CLI writes for what the prompt text names; each must be
+# dropped. Listed here rather than read from MENTION_ATTACHMENT_TYPES, so a type
+# missing from that set fails its own case.
+_MENTION_ATTACHMENT_TYPES = [
+ "file",
+ "already_read_file",
+ "compact_file_reference",
+ "pdf_reference",
+ "directory",
+ "agent_mention",
+ "mcp_resource",
+ "peer_mention",
+ "at_mention_reference",
+ "edited_text_file",
+ "edited_image_file",
+]
+
+# Attachment types a resumed session needs; none may be dropped.
+_KEPT_ATTACHMENT_TYPES = [
+ "hook_additional_context",
+ "prompt_snapshot",
+ "environment",
+ "model",
+ "date",
+ "session_context",
+ "agent_listing_delta",
+ "skill_listing",
+ "total_tokens_reminder",
+ "ultrathink_effort",
+]
+
+# The lines of a session CLI 2.1.284 wrote for a turn that named three files, a
+# directory and an agent, a second turn that named a file again and then
+# compacted, and a third turn: (label, type, attachment type or subtype,
+# parent label). A "last-prompt" line's last field is its leaf label.
+_COMPACTED_SESSION: list[tuple[str, str, str, str | None]] = [
+ ("q0", "queue-operation", "", None),
+ ("u2", "user", "", None),
+ ("a3", "attachment", "file", "u2"),
+ ("a4", "attachment", "directory", "a3"),
+ ("a5", "attachment", "file", "a4"),
+ ("a6", "attachment", "file", "a5"),
+ ("a7", "attachment", "agent_mention", "a6"),
+ ("a8", "attachment", "environment", "a7"),
+ ("a9", "attachment", "model", "a8"),
+ ("a10", "attachment", "ultrathink_effort", "a9"),
+ ("a11", "attachment", "agent_listing_delta", "a10"),
+ ("a12", "attachment", "total_tokens_reminder", "a11"),
+ ("l13", "atis-latch", "", None),
+ ("a14", "attachment", "session_context", "a12"),
+ ("a15", "attachment", "date", "a14"),
+ ("a16", "attachment", "prompt_snapshot", "a15"),
+ ("r17", "assistant", "", "a16"),
+ ("a18", "attachment", "prompt_snapshot", "r17"),
+ ("p19", "last-prompt", "", "a18"),
+ ("c21", "cost-state", "", None),
+ ("p22", "last-prompt", "", "a29"),
+ ("m23", "mode", "", None),
+ ("u27", "user", "", "a18"),
+ ("a28", "attachment", "file", "u27"),
+ ("a29", "attachment", "total_tokens_reminder", "a28"),
+ ("s30", "system", "compact_boundary", None),
+ ("a31", "attachment", "session_context", "s30"),
+ ("a32", "attachment", "date", "a31"),
+ ("u33", "user", "compact_summary", "a32"),
+ ("a34", "attachment", "file", "u33"),
+ ("a35", "attachment", "agent_listing_delta", "a34"),
+ ("a36", "attachment", "environment", "a35"),
+ ("a37", "attachment", "model", "a36"),
+ ("a38", "attachment", "prompt_snapshot", "a37"),
+ ("r39", "assistant", "", "a38"),
+ ("a40", "attachment", "prompt_snapshot", "r39"),
+ ("p41", "last-prompt", "", "a40"),
+ ("u47", "user", "", "a40"),
+ ("a48", "attachment", "total_tokens_reminder", "u47"),
+ ("r49", "assistant", "", "a48"),
+ ("p50", "last-prompt", "", "r49"),
+]
+# The compact boundary's metadata, as CLI 2.1.284 wrote it for that session.
+_PRESERVED = {
+ "preservedSegment": {"headUuid": "u27", "anchorUuid": "u33", "tailUuid": "a29"},
+ "preservedMessages": {
+ "anchorUuid": "u33",
+ "uuids": ["u27", "a28", "a29"],
+ "allUuids": ["u27", "a28", "a29"],
+ },
+}
+_LOGICAL_PARENT = "a29"
+
+
[email protected]("attachment_type", _MENTION_ATTACHMENT_TYPES)
+def test_each_mention_attachment_type_is_left_out(attachment_type: str):
+ content = _jsonl(
+ _user("u1", None),
+ _attachment("m2", "u1", attachment_type),
+ _attachment("k3", "m2", "date"),
+ )
+
+ entries = _entries(strip_for_upload(content))
+
+ assert [entry["uuid"] for entry in entries] == [_uid("u1"), _uid("k3")]
+ assert entries[1]["parentUuid"] == _uid("u1")
+
+
+def test_api_request_blobs_are_left_out():
+ """Newer CLIs keep a copy of each request as sent, attachments included."""
+ blob = {
+ "type": "api-request-blob",
+ "hash": "h1",
+ "sessionId": "s1",
+ "message": {"role": "user", "content": "an attached file's words"},
+ }
+ content = _jsonl(_user("u1", None), blob, _assistant("r2", "u1"))
+
+ entries = _entries(strip_for_upload(content))
+
+ assert [entry["type"] for entry in entries] == ["user", "assistant"]
+
+
+ "entry_type",
+ [
+ "api-request-blob",
+ "progress",
+ "file-history-snapshot",
+ "queue-operation",
+ "pr-link",
+ ],
+)
+def test_only_a_summary_is_kept_for_being_a_compact_summary(entry_type: str):
+ """Any other strippable entry is left out even when marked as one."""
+ marked = {
+ "type": entry_type,
+ "isCompactSummary": True,
+ "message": {"role": "user", "content": "an attached file's words"},
+ }
+ summary = {
+ "type": "summary",
+ "isCompactSummary": True,
+ "message": {"role": "user", "content": "compacted context"},
+ }
+ content = _jsonl(_user("u1", None), marked, summary, _assistant("r2", "u1"))
+
+ entries = _entries(strip_for_upload(content))
+
+ assert [entry["type"] for entry in entries] == ["user", "summary", "assistant"]
+
+
+ "unreadable",
+ [
+ '{"type":"attachment","uuid":"m2","attachment":{"type":"file","con',
+ '["type", "attachment"]',
+ '"attachment"',
+ "null",
+ ],
+ ids=["truncated", "array", "string", "null"],
+)
+def test_a_line_that_is_not_a_json_object_stops_the_strip(unreadable: str):
+ """It could hold an entry that must be dropped, so nothing is returned."""
+ content = _jsonl(_user("u1", None), _assistant("r2", "u1")) + unreadable + "\n"
+
+ with pytest.raises(ValueError):
+ strip_for_upload(content)
+
+
+def test_blank_lines_do_not_stop_the_strip():
+ content = _jsonl(_user("u1", None)) + "\n" + _jsonl(_assistant("r2", "u1"))
+
+ assert strip_for_upload(content) == content
+
+
[email protected]("attachment_type", _KEPT_ATTACHMENT_TYPES)
+def test_attachment_types_a_resumed_session_needs_are_kept(attachment_type: str):
+ content = _jsonl(
+ _user("u1", None),
+ _attachment("k2", "u1", attachment_type),
+ _assistant("r3", "k2"),
+ )
+
+ assert strip_for_upload(content) == content
+
+
+def test_no_kept_entry_of_a_compacted_session_names_a_dropped_one():
+ before = _compacted_session(_PRESERVED)
+
+ after = _entries(strip_for_upload(_jsonl(*before)))
+
+ assert _dangling_references(before, after) == []
+ boundary = next(entry for entry in after if entry["type"] == "system")
+ preserved = boundary["compactMetadata"]["preservedMessages"]
+ assert preserved["uuids"] == preserved["allUuids"] == [_uid("u27"), _uid("a29")]
+ assert _walk_preserved_segment(after, boundary) == preserved["uuids"]
+
+
+ ("segment", "kept_uuids"),
+ [
+ ({"headUuid": "a28", "tailUuid": "a29"}, ["a29"]),
+ ({"headUuid": "u27", "tailUuid": "a28"}, ["u27"]),
+ ],
+ ids=["dropped head", "dropped tail"],
+)
+def test_a_preserved_segment_ending_on_a_dropped_entry_is_moved_inside_it(
+ segment: dict[str, str], kept_uuids: list[str]
+):
+ before = _compacted_session({"preservedSegment": {**segment, "anchorUuid": "u33"}})
+
+ after = _entries(strip_for_upload(_jsonl(*before)))
+
+ assert _dangling_references(before, after) == []
+ boundary = next(entry for entry in after if entry["type"] == "system")
+ walked = _walk_preserved_segment(after, boundary)
+ assert walked == [_uid(label) for label in kept_uuids]
+
+
+def test_a_preserved_segment_with_nothing_kept_is_removed():
+ before = _compacted_session(
+ {
+ "preservedSegment": {
+ "headUuid": "a28",
+ "anchorUuid": "u33",
+ "tailUuid": "a28",
+ }
+ }
+ )
+
+ after = _entries(strip_for_upload(_jsonl(*before)))
+
+ assert _dangling_references(before, after) == []
+ boundary = next(entry for entry in after if entry["type"] == "system")
+ assert "preservedSegment" not in boundary["compactMetadata"]
+
+
+def _compacted_session(preserved: dict[str, dict[str, Any]]) -> list[dict]:
+ entries = []
+ for label, kind, detail, link in _COMPACTED_SESSION:
+ if kind == "user":
+ entry = _user(label, link)
+ if detail == "compact_summary":
+ entry["isCompactSummary"] = True
+ elif kind == "assistant":
+ entry = _assistant(label, link)
+ elif kind == "attachment":
+ entry = _attachment(label, link, detail)
+ elif kind == "system":
+ entry = _boundary(label, preserved)
+ elif kind == "last-prompt":
+ entry = {"type": kind, "leafUuid": _uid(link)}
+ else:
+ entry = {"type": kind}
+ entries.append(entry)
+ return entries
+
+
+def _boundary(label: str, preserved: dict[str, dict[str, Any]]) -> dict:
+ metadata: dict[str, Any] = {"trigger": "auto", "preTokens": 190097}
+ for section, fields in preserved.items():
+ metadata[section] = {
+ key: (
+ [_uid(item) for item in value]
+ if isinstance(value, list)
+ else _uid(value)
+ )
+ for key, value in fields.items()
+ }
+ return {
+ "type": "system",
+ "subtype": "compact_boundary",
+ "uuid": _uid(label),
+ "parentUuid": None,
+ "logicalParentUuid": _uid(_LOGICAL_PARENT),
+ "compactMetadata": metadata,
+ }
+
+
+def _user(label: str, parent: str | None) -> dict:
+ return {
+ "type": "user",
+ "uuid": _uid(label),
+ "parentUuid": _uid(parent),
+ "message": {"role": "user", "content": f"text {label}"},
+ }
+
+
+def _assistant(label: str, parent: str | None) -> dict:
+ return {
+ "type": "assistant",
+ "uuid": _uid(label),
+ "parentUuid": _uid(parent),
+ "message": {"role": "assistant", "id": f"msg_{label}", "content": "ok"},
+ }
+
+
+def _attachment(label: str, parent: str | None, attachment_type: str) -> dict:
+ return {
+ "type": "attachment",
+ "uuid": _uid(label),
+ "parentUuid": _uid(parent),
+ "attachment": {"type": attachment_type},
+ }
+
+
+def _uid(label: str | None) -> str | None:
+ """A uuid-shaped id for *label*, so no other string in a line equals it."""
+ if label is None:
+ return None
+ return f"00000000-0000-4000-8000-{int(label[1:]):012d}"
+
+
+def _dangling_references(before: list[dict], after: list[dict]) -> list[str]:
+ """Every place in a kept entry that names an entry which is not kept,
+ found by walking each kept entry recursively."""
+ known = {entry["uuid"] for entry in before if "uuid" in entry}
+ kept = {entry["uuid"] for entry in after if "uuid" in entry}
+ found: list[str] = []
+
+ def walk(value: Any, path: str) -> None:
+ if isinstance(value, dict):
+ for key, item in value.items():
+ walk(item, f"{path}.{key}")
+ elif isinstance(value, list):
+ for index, item in enumerate(value):
+ walk(item, f"{path}[{index}]")
+ elif isinstance(value, str) and value in known and value not in kept:
+ found.append(path)
+ elif isinstance(value, str) and _names_an_entry(path) and value not in kept:
+ found.append(path)
+
+ for index, entry in enumerate(after):
+ for key, value in entry.items():
+ if key != "uuid":
+ walk(value, f"[{index}].{key}")
+ return found
+
+
+def _names_an_entry(path: str) -> bool:
+ field = path.rsplit(".", 1)[-1].split("[", 1)[0]
+ return field.endswith(("Uuid", "UUID", "uuids", "Uuids"))
+
+
+def _walk_preserved_segment(entries: list[dict], boundary: dict) -> list[str]:
+ """The ids from the segment's head to its tail along ``parentUuid``, the way
+ the CLI walks them when it relinks a compacted session."""
+ segment = boundary["compactMetadata"]["preservedSegment"]
+ by_uuid = {entry["uuid"]: entry for entry in entries if "uuid" in entry}
+ walked: list[str] = []
+ current = by_uuid.get(segment["tailUuid"])
+ while current is not None and current["uuid"] not in walked:
+ walked.append(current["uuid"])
+ if current["uuid"] == segment["headUuid"]:
+ return walked[::-1]
+ current = by_uuid.get(current.get("parentUuid") or "")
+ raise AssertionError("the preserved segment's tail does not lead to its head")
+
+
+def _jsonl(*entries: dict) -> str:
+ return "\n".join(json.dumps(entry) for entry in entries) + "\n"
+
+
+def _entries(content: str) -> list[dict]:
+ return [json.loads(line) for line in content.splitlines()]
diff --git a/autogpt_platform/backend/backend/copilot/transcript_test.py b/autogpt_platform/backend/backend/copilot/transcript_test.py
index c3120eb274..3947aaeb7c 100644
--- a/autogpt_platform/backend/backend/copilot/transcript_test.py
+++ b/autogpt_platform/backend/backend/copilot/transcript_test.py
@@ -889,6 +889,149 @@ class TestStripForUpload:
assert len(lines) == 1
+def _attachment(uid: str, parent: str, attachment: dict) -> dict:
+ return {
+ "type": "attachment",
+ "uuid": uid,
+ "parentUuid": parent,
+ "attachment": attachment,
+ }
+
+
+class TestStripForUploadMentionAttachments:
+ """The attachments the CLI adds for files, directories, images and agents
+ named in prompt text are left out; every other entry stays, linked."""
+
+ _FILE_TEXT = {
+ "type": "file",
+ "filename": "/tmp/notes/a.txt",
+ "content": {"type": "text", "file": {"filePath": "/tmp/notes/a.txt"}},
+ }
+ _FILE_IMAGE = {
+ "type": "file",
+ "filename": "/tmp/notes/b.png",
+ "content": {"type": "image", "file": {"base64": "AAAA"}},
+ }
+ _KEPT = [
+ {"type": "environment", "snapshot": {"workingDirectory": "/tmp/s"}},
+ {
+ "type": "hook_additional_context",
+ "content": ["follow-up"],
+ "hookName": "PostToolUse:mcp__copilot__noop",
+ "toolUseID": "toolu_1",
+ "hookEvent": "PostToolUse",
+ },
+ {"type": "ultrathink_effort"},
+ {"type": "prompt_snapshot", "systemPrompt": ["system"]},
+ {"type": "date", "date": "2026-10-11"},
+ ]
+
+ def _session(self) -> list[dict]:
+ user = {
+ "type": "user",
+ "uuid": "u1",
+ "parentUuid": None,
+ "message": {"role": "user", "content": "compare these"},
+ }
+ dropped = [
+ _attachment("m1", "u1", self._FILE_TEXT),
+ _attachment("m2", "m1", {"type": "directory", "path": "/tmp/notes"}),
+ _attachment("m3", "m2", self._FILE_IMAGE),
+ _attachment("m4", "m3", {"type": "agent_mention", "agentType": "x"}),
+ ]
+ kept = [
+ _attachment(f"k{i}", "m4" if i == 0 else f"k{i - 1}", attachment)
+ for i, attachment in enumerate(self._KEPT)
+ ]
+ assistant = {
+ "type": "assistant",
+ "uuid": "a1",
+ "parentUuid": kept[-1]["uuid"],
+ "message": {"role": "assistant", "id": "msg1", "content": "ok"},
+ }
+ return [user, *dropped, *kept, assistant]
+
+ def test_mention_attachments_are_left_out(self):
+ entries = [
+ json.loads(line)
+ for line in strip_for_upload(_make_jsonl(*self._session())).splitlines()
+ ]
+
+ kept_types = [e.get("attachment", {}).get("type") for e in entries]
+ assert kept_types == [
+ None,
+ *(attachment["type"] for attachment in self._KEPT),
+ None,
+ ]
+ assert "/tmp/notes" not in json.dumps(entries)
+
+ def test_the_tree_stays_linked(self):
+ entries = [
+ json.loads(line)
+ for line in strip_for_upload(_make_jsonl(*self._session())).splitlines()
+ ]
+
+ uuids = [e["uuid"] for e in entries]
+ assert [e["parentUuid"] for e in entries] == [None, *uuids[:-1]]
+
+ def test_leaf_and_logical_parent_move_to_the_nearest_kept_entry(self):
+ user = {
+ "type": "user",
+ "uuid": "u1",
+ "parentUuid": None,
+ "message": {"role": "user", "content": "see"},
+ }
+ mention = _attachment("m1", "u1", self._FILE_TEXT)
+ boundary = {
+ "type": "system",
+ "subtype": "compact_boundary",
+ "uuid": "c1",
+ "parentUuid": None,
+ "logicalParentUuid": "m1",
+ }
+ reattached = _attachment("m2", "c1", self._FILE_TEXT)
+ assistant = {
+ "type": "assistant",
+ "uuid": "a1",
+ "parentUuid": "m2",
+ "message": {"role": "assistant", "id": "msg1", "content": "ok"},
+ }
+ last_prompt = {"type": "last-prompt", "leafUuid": "m2"}
+ content = _make_jsonl(
+ user, mention, boundary, reattached, assistant, last_prompt
+ )
+
+ entries = [json.loads(line) for line in strip_for_upload(content).splitlines()]
+
+ by_type = {e["type"]: e for e in entries}
+ assert by_type["system"]["logicalParentUuid"] == "u1"
+ assert by_type["assistant"]["parentUuid"] == "c1"
+ assert by_type["last-prompt"]["leafUuid"] == "c1"
+
+ def test_a_session_without_them_is_unchanged(self):
+ user = {
+ "type": "user",
+ "uuid": "u1",
+ "parentUuid": None,
+ "message": {"role": "user", "content": "hi"},
+ }
+ hook = _attachment("k1", "u1", self._KEPT[1])
+ assistant = {
+ "type": "assistant",
+ "uuid": "a1",
+ "parentUuid": "k1",
+ "message": {"role": "assistant", "id": "msg1", "content": "ok"},
+ }
+ content = _make_jsonl(user, hook, assistant)
+
+ assert strip_for_upload(content) == content
+
+ def test_stripping_twice_changes_nothing(self):
+ once = strip_for_upload(_make_jsonl(*self._session()))
+
+ assert strip_for_upload(once) == once
+
+
# ---------------------------------------------------------------------------
# validate_transcript (additional edge cases)
# ---------------------------------------------------------------------------