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): transient retries bill once, never repeat a tool call, and work on first turns (#15420)
Nicholas Tindle1f3dfabc4d90
diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/audit.py b/autogpt_platform/backend/backend/copilot/billing_e2e/audit.pyindex a1ae97c5e1..7d8e9a21d0 100644--- a/autogpt_platform/backend/backend/copilot/billing_e2e/audit.py+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/audit.py@@ -25,12 +25,12 @@ def audit_thread(ledger: PlatformLedger,session: ChatSession,usage_by_turn: list[list[Usage]],- cli_resumed: list[bool] | None,+ cli_resumed: list[tuple[int, bool]] | None,) -> list[Finding]:"""Every way the thread strayed from its invoice or its planned path.- ``cli_resumed`` says, for each CLI launch of an SDK thread, whether it- resumed the chat's session; None for engines without a CLI.+ ``cli_resumed`` says, for each CLI launch of an SDK thread, its turn and+ whether it resumed the chat's session; None for engines without a CLI."""findings = _audit_path(provider, cli_resumed, turns=len(usage_by_turn))findings += _audit_attribution(session, ledger)@@ -61,7 +61,10 @@ def _audit_attribution(session: ChatSession, ledger: PlatformLedger) -> list[Findef _audit_path(- provider: FakeProvider, cli_resumed: list[bool] | None, *, turns: int+ provider: FakeProvider,+ cli_resumed: list[tuple[int, bool]] | None,+ *,+ turns: int,) -> list[Finding]:"""Whether the thread took the path it meant to.@@ -74,11 +77,13 @@ def _audit_path(messages.append(f"faults served {provider.faults_served}, planned {provider.faults}")- # Every launch after the first, retries included, resumes the session.+ # The first turn's launches start the session, retries included; every+ # later turn's launches resume it.if cli_resumed is not None and (- len(cli_resumed) < turns or cli_resumed[0] or not all(cli_resumed[1:])+ {turn for turn, _ in cli_resumed} != set(range(turns))+ or any(resumed != (turn > 0) for turn, resumed in cli_resumed)):- messages.append(f"CLI launches resumed the session: {cli_resumed}")+ messages.append(f"CLI launches (turn, resumed): {cli_resumed}")return [Finding(check="path", message=message) for message in messages]@@ -99,6 +104,8 @@ def _audit_turn()]owed = scenario.owed_calls(turn, calls)+ if not owed:+ return _audit_unbilled(turn, ledger, session_usage)findings = _audit_spend(scenario, turn, owed, ledger)rows = ledger.rows_in(turn)if len(rows) != 1:@@ -110,6 +117,23 @@ def _audit_turn(return findings+def _audit_unbilled(+ turn: int, ledger: PlatformLedger, session_usage: list[Usage]+) -> list[Finding]:+ """A turn that owes nothing leaves no charge, cost-log row or usage."""+ spend = [charge.microdollars for charge in ledger.spend_in(turn)]+ tree = [charge.microdollars for charge in ledger.tree_in(turn)]+ rows = ledger.rows_in(turn)+ if not (spend or tree or rows or session_usage):+ return []+ message = (+ f"turn {turn} owes nothing, yet charged {spend} to spend and {tree} to "+ f"the tree, logged {len(rows)} cost-log row(s) and recorded "+ f"{len(session_usage)} session usage entries"+ )+ return [Finding(check="charge", message=message)]++def _audit_spend(scenario: Scenario, turn: int, calls: list[Call], ledger: PlatformLedger) -> list[Finding]:diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/audit_test.py b/autogpt_platform/backend/backend/copilot/billing_e2e/audit_test.pyindex 87125f5e23..96d8331bf7 100644--- a/autogpt_platform/backend/backend/copilot/billing_e2e/audit_test.py+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/audit_test.py@@ -1,4 +1,5 @@-"""The audit on its own: a relaunched turn owes only the attempt that succeeded."""+"""The audit on its own: a relaunched turn owes only the attempt that+succeeded, and a turn the failure ended owes nothing."""from backend.copilot.model import Usagefrom backend.data.platform_cost import PlatformCostEntry, usd_to_microdollars@@ -6,9 +7,16 @@ from backend.data.platform_cost import PlatformCostEntry, usd_to_microdollarsfrom .audit import _audit_turnfrom .invoice import ANTHROPIC_PRICES, SONNET, Call, scripted_usagefrom .ledger import CostLogRow, PlatformLedger, SpendCharge, TreeCharge-from .scenarios import SCENARIOS+from .provider import PlannedFault+from .scenarios import SCENARIOS, _sdk_anthropic_failing-_RELAUNCHED = next(s for s in SCENARIOS if s.id == "sdk-anthropic-dropped-after-tool")+# A turn relaunched after the provider billed a call that streamed nothing.+_RELAUNCHED = _sdk_anthropic_failing(+ "relaunched-after-a-billed-call",+ PlannedFault(kind="dropped", turn=1, request=2),+ calls_per_turn=(1, 2, 1),+)+_ENDED = next(s for s in SCENARIOS if s.id == "sdk-anthropic-dropped-after-tool")def _call(index: int, request: int) -> Call:@@ -24,7 +32,7 @@ def _call(index: int, request: int) -> Call:)-# The tool round's first call, then the dropped request 2, then the retry's call.+# Turn 1's first call, then the dropped request 2, then a relaunch's call._FAILED_ATTEMPT = _call(1, request=1)_RETRY = _call(2, request=3)@@ -50,6 +58,21 @@ def test_charging_the_failed_attempt_is_reported():assert all("the turn owes" in finding.message for finding in findings)+def test_a_turn_the_failure_ended_charged_nothing_passes():+ findings = _audit_turn(_ENDED, 1, [_FAILED_ATTEMPT], PlatformLedger(), [])++ assert findings == []+++def test_charging_a_turn_the_failure_ended_is_reported():+ ledger, usage = _turn_charged_for(_FAILED_ATTEMPT)++ findings = _audit_turn(_ENDED, 1, [_FAILED_ATTEMPT], ledger, usage)++ assert [finding.check for finding in findings] == ["charge"]+ assert "owes nothing" in findings[0].message++def _turn_charged_for(*calls: Call) -> tuple[PlatformLedger, list[Usage]]:"""Turn 1's ledger and session usage, as if the platform charged ``calls``."""microdollars = usd_to_microdollars(sum(call.invoice_usd for call in calls))diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/scenarios.py b/autogpt_platform/backend/backend/copilot/billing_e2e/scenarios.pyindex 20a55a8fb9..19445d4fc6 100644--- a/autogpt_platform/backend/backend/copilot/billing_e2e/scenarios.py+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/scenarios.py@@ -13,7 +13,8 @@ charge is expected to rest on:A turn the platform relaunches after a provider failure owes only the callsbilled after the failure. The failed attempt is the platform's to absorb, not-the user's.+the user's. A turn the failure ends, because it had already streamed a tool+call that a relaunch would roll back and could repeat, owes nothing."""from typing import Any, Literal@@ -60,15 +61,23 @@ class Scenario(BaseModel):# attempt, so every call after it is the retry's: the CLI's own retries# must be off (``cli_max_retries=0``) for this to hold.fault_relaunches_turn: bool = False+ # Whether the fault ends the turn in an error instead: the turn had+ # already streamed a tool call, which a relaunch would roll back out of+ # the chat and could run again.+ fault_ends_turn: bool = False# CLAUDE_CODE_MAX_RETRIES for the CLI; None keeps its own default.cli_max_retries: int | None = Noneknown_gap: KnownGap | None = Nonedef owed_calls(self, turn: int, calls: list[Call]) -> list[Call]:"""The turn's billed calls the user pays for: after a relaunch, only- the attempt that succeeded."""+ the attempt that succeeded, and none for a turn the fault ended."""fault = self.fault- if fault is None or fault.turn != turn or not self.fault_relaunches_turn:+ if fault is None or fault.turn != turn:+ return calls+ if self.fault_ends_turn:+ return []+ if not self.fault_relaunches_turn:return callsreturn [call for call in calls if call.request > fault.request]@@ -105,6 +114,8 @@ def _sdk_anthropic_failing(scenario_id: str,fault: PlannedFault,calls_per_turn: tuple[int, int, int],+ *,+ relaunches: bool = True,) -> Scenario:"""The direct-Anthropic thread with one request failed and the CLI's ownretries off, so the platform's handling of the failure is what runs."""@@ -112,7 +123,8 @@ def _sdk_anthropic_failing(update={"id": scenario_id,"fault": fault,- "fault_relaunches_turn": True,+ "fault_relaunches_turn": relaunches,+ "fault_ends_turn": not relaunches,"calls_per_turn": calls_per_turn,"cli_max_retries": 0,}@@ -121,28 +133,34 @@ def _sdk_anthropic_failing(SCENARIOS = [_SDK_ANTHROPIC,- # The tool round's second call is refused: the platform relaunches the CLI- # on the session. The provider billed the tool round's first call and the- # retry's; the turn owes only the retry's.+ # The tool round's second call is refused after the tool ran. Relaunching+ # would roll the call the user watched out of the chat and could repeat+ # it, so the turn ends in the error: the provider billed the tool round's+ # first call, and a turn that failed owes nothing._sdk_anthropic_failing("sdk-anthropic-overloaded-after-tool",PlannedFault(kind="overloaded", turn=1, request=2),- calls_per_turn=(1, 2, 1),+ calls_per_turn=(1, 1, 1),+ relaunches=False,),- # The connection drops instead, which is transient too: the platform stops- # reading before the CLI's result and relaunches the CLI on the session.+ # The connection drops instead, which is transient too, and ends the turn+ # the same way._sdk_anthropic_failing("sdk-anthropic-dropped-after-tool",PlannedFault(kind="dropped", turn=1, request=2),- calls_per_turn=(1, 2, 1),+ calls_per_turn=(1, 1, 1),+ relaunches=False,),- # A resumed turn whose first request drops is relaunched before anything- # streamed, and charged its one successful call.+ # A chat's first request drops before anything streamed. The relaunch+ # starts the CLI session over, which the CLI refuses while the failed+ # launch's session file is still there, and is charged its one call._sdk_anthropic_failing(- "sdk-anthropic-dropped-on-resume",- PlannedFault(kind="dropped", turn=2, request=1),+ "sdk-anthropic-dropped-first-turn",+ PlannedFault(kind="dropped", turn=0, request=1),calls_per_turn=(1, 2, 1),),+ # A resumed turn whose first request is refused is relaunched on the+ # session before anything streamed, and charged its one successful call._sdk_anthropic_failing("sdk-anthropic-overloaded-on-resume",PlannedFault(kind="overloaded", turn=2, request=1),diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py b/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.pyindex 767a9967bb..e9757e7357 100644--- a/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/sdk_turns.py@@ -57,8 +57,10 @@ class SdkTurns:self._config = configself._transcripts: dict[str, TranscriptDownload] = {}self._lease = _codex_lease(provider) if codex else None- # The CLI session each launch resumed, None for a fresh one.- self.resumed: list[str | None] = []+ # The turn of each launch, and the CLI session it resumed (None for a+ # fresh one).+ self.resumed: list[tuple[int, str | None]] = []+ self._turn = -1# The CLI inherits these, and the platform reads the CLI's session# files from the same place.monkeypatch.setenv("HOME", str(tmp_path / "home"))@@ -79,6 +81,7 @@ class SdkTurns:async def __call__(self, session: ChatSession, prompt: str, *, new_pod: bool) -> None:+ self._turn += 1if new_pod:# Only what the turn uploaded survives a move to another pod.shutil.rmtree(self._config_dir / "projects", ignore_errors=True)@@ -134,7 +137,7 @@ class SdkTurns:def _client(self, *args: Any, options: ClaudeAgentOptions, **kwargs: Any) -> ClaudeSDKClient:- self.resumed.append(options.resume)+ self.resumed.append((self._turn, options.resume))# The copilot's own tools and hooks are not what is under test; the# CLI answers the provider's tool call with "no such tool" instead.options.mcp_servers = {}diff --git a/autogpt_platform/backend/backend/copilot/billing_e2e/thread_billing_test.py b/autogpt_platform/backend/backend/copilot/billing_e2e/thread_billing_test.pyindex 1b55308807..ac41264d64 100644--- a/autogpt_platform/backend/backend/copilot/billing_e2e/thread_billing_test.py+++ b/autogpt_platform/backend/backend/copilot/billing_e2e/thread_billing_test.py@@ -65,7 +65,7 @@ async def test_each_turn_is_charged_what_its_provider_billed(usage_by_turn.append(session.usage[usage_before:])cli_resumed = (- [launch == session.session_id for launch in run_turn.resumed]+ [(turn, launch == session.session_id) for turn, launch in run_turn.resumed]if isinstance(run_turn, SdkTurns)else None)diff --git a/autogpt_platform/backend/backend/copilot/sdk/openrouter_cost.py b/autogpt_platform/backend/backend/copilot/sdk/openrouter_cost.pyindex ebf183b8d2..7f0af7eb91 100644--- a/autogpt_platform/backend/backend/copilot/sdk/openrouter_cost.py+++ b/autogpt_platform/backend/backend/copilot/sdk/openrouter_cost.py@@ -22,6 +22,9 @@ If every lookup fails (404 / timeout / parse error), the caller's``fallback_cost_usd`` is recorded instead — keeps the rate-limit counterpopulated with the best available estimate rather than leaving the turnuncharged.++:class:`TurnGenerations` collects the IDs during the turn, one CLI launch at+a time, so a transient retry can leave out what the failed launch ran."""from __future__ import annotations@@ -29,6 +32,7 @@ from __future__ import annotationsimport asyncioimport loggingimport os+from collections.abc import Collectionfrom pathlib import Pathfrom typing import TYPE_CHECKING@@ -220,11 +224,21 @@ def _discover_turn_subagent_gen_ids(Preserves ``known`` ordering so main-turn IDs stay first; onlyappends truly new IDs from the sweep."""- merged: list[str] = list(known)- seen = set(merged)+ seen = set(known)+ swept = _turn_subagent_gen_ids(project_dir, session_id, turn_start_ts)+ return [*known, *(gen_id for gen_id in swept if gen_id not in seen)]+++def _turn_subagent_gen_ids(+ project_dir: Path, session_id: str, turn_start_ts: float+) -> list[str]:+ """Gen-IDs in this session's subagent files written during this turn,+ deduplicated, file by file and sorted within each file. Scoped as+ :func:`_discover_turn_subagent_gen_ids` describes."""+ ids: dict[str, None] = {}subagents_dir = project_dir / session_id / "subagents"if not subagents_dir.exists():- return merged+ return []try:for jsonl in subagents_dir.glob("agent-*.jsonl"):try:@@ -232,13 +246,74 @@ def _discover_turn_subagent_gen_ids(continueexcept OSError:continue- for gen_id in _gen_ids_from_jsonl(jsonl):- if gen_id not in seen:- seen.add(gen_id)- merged.append(gen_id)+ ids.update(dict.fromkeys(sorted(_gen_ids_from_jsonl(jsonl))))except OSError as exc:logger.debug("Failed to walk subagents dir=%s: %s", subagents_dir, exc)- return merged+ return list(ids)+++class TurnGenerations:+ """The OpenRouter generations a turn is billed for, one CLI launch at a time.++ The live stream names each generation of the main agent; the CLI's+ subagents (compaction, Task) leave theirs in JSONL files, which the+ reconcile sweeps once the turn ends. A transient retry relaunches the CLI+ after a provider failure, and what the failed launch ran is ours to absorb,+ as its CLI-priced spend is (``_do_transient_backoff`` in ``service.py``):+ its generations, live or in a subagent's file, are left out of the+ reconcile. The retry only relaunches a launch that streamed nothing, so+ none of its queries had reported a result whose spend must stay billed.++ Without a project dir the subagent files are not read, which is right for+ a turn that does not reconcile.+ """++ def __init__(+ self,+ cli_project_dir: str | None = None,+ cli_session_id: str | None = None,+ turn_start_ts: float | None = None,+ ) -> None:+ # The live generation IDs to bill, in arrival order.+ self.ids: list[str] = []+ # What absorbed launches ran, kept so the sweep cannot bill it again.+ self.absorbed: set[str] = set()+ self._project_dir = cli_project_dir+ self._session_id = cli_session_id+ self._turn_start_ts = turn_start_ts+ self._launch_start = 0+ self._subagent_ids_at_launch: set[str] = set()++ def add(self, generation_id: str) -> None:+ if generation_id not in self.ids:+ self.ids.append(generation_id)++ async def start_launch(self) -> None:+ """Mark what is billed when a CLI launch starts, so a retry can tell+ what the launch itself ran."""+ self._launch_start = len(self.ids)+ self._subagent_ids_at_launch = await self._subagent_ids()++ async def absorb_launch(self) -> None:+ """Leave out everything the launch since :meth:`start_launch` ran.++ Call it once the launch's CLI process has exited, so its subagents+ have written their last generation.+ """+ self.absorbed.update(self.ids[self._launch_start :])+ del self.ids[self._launch_start :]+ self.absorbed.update(await self._subagent_ids() - self._subagent_ids_at_launch)++ async def _subagent_ids(self) -> set[str]:+ if not (self._project_dir and self._session_id) or self._turn_start_ts is None:+ return set()+ ids = await asyncio.to_thread(+ _turn_subagent_gen_ids,+ Path(os.path.expanduser(self._project_dir)),+ self._session_id,+ self._turn_start_ts,+ )+ return set(ids)async def record_turn_cost_from_openrouter(@@ -258,6 +333,7 @@ async def record_turn_cost_from_openrouter(api_key: str | None,log_prefix: str,langfuse_trace_id: str | None = None,+ absorbed_generation_ids: Collection[str] = (),) -> None:"""Persist turn cost from OpenRouter's authoritative ``/generation``.@@ -267,6 +343,9 @@ async def record_turn_cost_from_openrouter(counting: the caller skips its own sync persist for non-AnthropicOpenRouter turns and defers entirely to this task.+ ``absorbed_generation_ids`` (:attr:`TurnGenerations.absorbed`) are left+ out wherever they come from, the caller's list or the subagent sweep.+Launched via ``asyncio.create_task`` from the stream ``finally`` blockso the ~500-2000ms ``/generation`` indexing delay doesn't add latencyto the turn. During that window the rate-limit counter is briefly@@ -318,6 +397,15 @@ async def record_turn_cost_from_openrouter()generation_ids = merged_ids+ billed_ids = [g for g in generation_ids if g not in absorbed_generation_ids]+ if len(billed_ids) != len(generation_ids):+ logger.info(+ "%s[cost-record] left out %d gen-IDs a transient retry absorbed",+ log_prefix,+ len(generation_ids) - len(billed_ids),+ )+ generation_ids = billed_ids+if not generation_ids:returndiff --git a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.pyindex 191eb63252..0ad82bef24 100644--- a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py@@ -1,6 +1,6 @@"""Tests for P0 guardrails: _resolve_fallback_model, security env vars, TMPDIR."""-from unittest.mock import patch+from unittest.mock import MagicMock, patchimport pytestfrom pydantic import ValidationError@@ -13,6 +13,7 @@ from backend.copilot.constants import (from backend.copilot.model import ChatSessionfrom backend.copilot.response_model import StreamCompactionProgressfrom backend.copilot.sdk.compaction import CompactionTracker+from backend.copilot.sdk.openrouter_cost import TurnGenerationsfrom backend.copilot.sdk.service import _EPHEMERAL_EVENT_TYPES, _TokenUsage@@ -719,6 +720,13 @@ class TestNextTransientBackoff:# ---------------------------------------------------------------------------+def _backoff_ctx(message_id: str = "msg-id", session_id: str = "sess-id") -> MagicMock:+ """The stream a backoff relaunches, with no CLI working dir to clear."""+ return MagicMock(+ message_id=message_id, session_id=session_id, sdk_cwd="", log_prefix=""+ )++class TestDoTransientBackoff:"""Unit tests for _do_transient_backoff.@@ -733,12 +741,12 @@ class TestDoTransientBackoff:from backend.copilot.response_model import StreamStatusfrom backend.copilot.sdk.service import _do_transient_backoff- state = MagicMock()+ state = MagicMock(generations=TurnGenerations())state.usage = MagicMock()events = []with patch("asyncio.sleep", new=AsyncMock()):- async for evt in _do_transient_backoff(5, state, "msg-id", "sess-id"):+ async for evt in _do_transient_backoff(5, state, _backoff_ctx()):events.append(evt)assert len(events) == 1@@ -751,12 +759,12 @@ class TestDoTransientBackoff:from backend.copilot.sdk.service import _do_transient_backoff- state = MagicMock()+ state = MagicMock(generations=TurnGenerations())state.usage = MagicMock()mock_sleep = AsyncMock()with patch("asyncio.sleep", new=mock_sleep):- async for _ in _do_transient_backoff(7, state, "msg-id", "sess-id"):+ async for _ in _do_transient_backoff(7, state, _backoff_ctx()):passmock_sleep.assert_called_once_with(7)@@ -768,7 +776,7 @@ class TestDoTransientBackoff:from backend.copilot.sdk.service import _do_transient_backofforiginal_adapter = MagicMock()- state = MagicMock()+ state = MagicMock(generations=TurnGenerations())state.adapter = original_adapterstate.usage = MagicMock()@@ -778,7 +786,9 @@ class TestDoTransientBackoff:):new_adapter = MagicMock()mock_cls.return_value = new_adapter- async for _ in _do_transient_backoff(3, state, "msg-1", "sess-1"):+ async for _ in _do_transient_backoff(+ 3, state, _backoff_ctx("msg-1", "sess-1")+ ):passmock_cls.assert_called_once_with(message_id="msg-1", session_id="sess-1")@@ -792,7 +802,7 @@ class TestDoTransientBackoff:from backend.copilot.sdk.service import _do_transient_backoff- state = MagicMock()+ state = MagicMock(generations=TurnGenerations())state.usage = _TokenUsage(prompt_tokens=100,completion_tokens=10,@@ -804,7 +814,7 @@ class TestDoTransientBackoff:)with patch("asyncio.sleep", new=AsyncMock()):- async for _ in _do_transient_backoff(2, state, "msg-id", "sess-id"):+ async for _ in _do_transient_backoff(2, state, _backoff_ctx()):passassert state.usage == _TokenUsage(@@ -896,37 +906,40 @@ class TestEphemeralEventTypesConstant:assert StreamHeartbeat in _EPHEMERAL_EVENT_TYPESdef test_compaction_progress_is_in_ephemeral_types(self):- """SDK-internal compaction emits progress phases inside the attempt- loop, alongside the tool-row events that are already exempt. Counting- them would make a transient failure after a compaction unretryable.+ """A compaction row's progress phases ride along with the row's own+ tool events, which decide whether the row counts; a phase is not+ output of its own."""assert StreamCompactionProgress in _EPHEMERAL_EVENT_TYPES, ("StreamCompactionProgress must be in _EPHEMERAL_EVENT_TYPES — a ""cosmetic progress phase must not suppress the transient retry")- def test_every_compaction_row_event_is_ephemeral(self):- """The whole compaction row — open, close and progress — is cosmetic.+ async def test_a_compaction_the_cli_ran_counts_as_output(self):+ """The CLI's own compaction streams a row and persists it with the+ attempt, so a relaunch would roll the row the user watched back out+ of the chat: the row's tool events count, its progress phases do not.- Guards the class of bug where a new compaction event type is added to- the stream but not exempted here.+ A pre-query compaction row never reaches this count: the retry loop+ yields it outside the attempt."""tracker = CompactionTracker()session = ChatSession.new(user_id="test-user", dry_run=False)+ tracker.on_compact()events = [- *tracker.emit_pre_query_start(),- *tracker.emit_pre_query_end(session, None),+ *tracker.emit_start_if_ready(),+ *(await tracker.emit_end_if_ready(session)).events,]- assert events- non_ephemeral = [+ counted = {type(e).__name__for e in eventsif not isinstance(e, _EPHEMERAL_EVENT_TYPES)- ]- assert not non_ephemeral, (- f"compaction emits non-ephemeral event(s) {non_ephemeral} — they "- "would be counted toward events_yielded and block transient retries"- )+ }+ assert counted == {+ "StreamToolInputStart",+ "StreamToolInputAvailable",+ "StreamToolOutputAvailable",+ }def test_is_a_tuple(self):"""isinstance() requires a tuple (not a list) as second argument."""@@ -1130,7 +1143,7 @@ class TestHandledStreamErrorTransientRetry:# Second call: success — yield a content eventyield StreamFinish()- state = MagicMock()+ state = MagicMock(generations=TurnGenerations())state.usage = MagicMock()state.transcript_builder = MagicMock()state.transcript_builder.snapshot.return_value = ([], None)@@ -1160,7 +1173,7 @@ class TestHandledStreamErrorTransientRetry:if backoff is not None:with patch("asyncio.sleep", new=AsyncMock()):async for evt in _do_transient_backoff(- backoff, state, "msg-id", "sess-id"+ backoff, state, _backoff_ctx()):emitted.append(evt)continue # retry@@ -1214,7 +1227,7 @@ class TestHandledStreamErrorTransientRetry:returnyield # noqa: B901- state = MagicMock()+ state = MagicMock(generations=TurnGenerations())state.usage = MagicMock()state.transcript_builder = MagicMock()state.transcript_builder.snapshot.return_value = ([], None)@@ -1242,7 +1255,7 @@ class TestHandledStreamErrorTransientRetry:if backoff is not None:with patch("asyncio.sleep", new=AsyncMock()):async for evt in _do_transient_backoff(- backoff, state, "m", "s"+ backoff, state, _backoff_ctx()):emitted.append(evt)continue@@ -1319,7 +1332,7 @@ class TestGenericExceptionTransientRetry:raise Exception("ECONNRESET")yield StreamFinish()- state = MagicMock()+ state = MagicMock(generations=TurnGenerations())state.usage = MagicMock()state.transcript_builder = MagicMock()state.transcript_builder.snapshot.return_value = ([], None)@@ -1348,7 +1361,7 @@ class TestGenericExceptionTransientRetry:if backoff is not None:with patch("asyncio.sleep", new=AsyncMock()):async for evt in _do_transient_backoff(- backoff, state, "m", "s"+ backoff, state, _backoff_ctx()):emitted.append(evt)continuediff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 7e771a9d6d..7285783ac6 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -171,7 +171,6 @@ from ..response_model import (StreamTextStart,StreamToolDisplayAvailable,StreamToolInputAvailable,- StreamToolInputStart,StreamToolOutputAvailable,StreamUsage,)@@ -228,7 +227,7 @@ from .compaction import (transcript_stats,)from .env import build_sdk_env # noqa: F401 — re-export for backward compat-from .openrouter_cost import record_turn_cost_from_openrouter+from .openrouter_cost import TurnGenerations, record_turn_cost_from_openrouterfrom .response_adapter import SDKResponseAdapterfrom .security_hooks import create_security_hooksfrom .tool_adapter import (@@ -517,12 +516,8 @@ async def _consume_sdk_until_done(# doesn't know about — harmlessly ignored at reconcile time.if isinstance(sdk_msg, AssistantMessage):msg_id = sdk_msg.message_id- if (- msg_id is not None- and msg_id.startswith("gen-")- and msg_id not in state.generation_ids- ):- state.generation_ids.append(msg_id)+ if msg_id is not None and msg_id.startswith("gen-"):+ state.generations.add(msg_id)# Track the model the SDK actually used — when a fallback# activates, this differs from ``state.options.model``.# Consumed by the Moonshot cost-override decision so we@@ -1180,20 +1175,21 @@ _OUTER_LOOP_YIELDS_ERROR_CODES: frozenset[str] = frozenset(# ``events_yielded`` in the transient-retry loop. Counting them would prevent# the backoff retry from firing because ``_next_transient_backoff`` returns# ``None`` when ``events_yielded > 0``.+#+# Tool events are deliberately absent: a tool row, a call the model made or+# the CLI's own compaction, is output the retry cannot take back. The retry+# rolls the attempt's rows out of the session, so a row the user watched would+# vanish on refresh, and the resent query can run a side-effecting tool (an+# email, an agent run) a second time. Pre-query compaction rows are yielded+# by the retry loop itself, outside the attempt, so they never count._EPHEMERAL_EVENT_TYPES = (StreamHeartbeat,StreamCheckpoint,StreamPendingDrained,- StreamToolDisplayAvailable,- # Compaction UI events are cosmetic and must not block retry — they're- # emitted before the SDK query on compacted attempts.StreamStartStep,StreamFinishStep,- StreamToolInputStart,- StreamToolInputAvailable,- StreamToolOutputAvailable,- # The compaction row's live progress phases ride along with those UI- # events on the SDK-internal path, inside the attempt loop.+ # A compaction row's progress phases ride along with its tool events,+ # which count on their own.StreamCompactionProgress,# Transient StreamError and StreamStatus are ephemeral notifications,# not content. Counting them would prevent the backoff retry from@@ -1387,14 +1383,15 @@ class _RetryState:# not reset the per-turn cap to zero — otherwise multiple retries# could each fire their own re-prompt round.thinking_only_reprompted: bool = False- # OpenRouter generation IDs collected across all attempts of this turn.- # Populated from ``AssistantMessage.message_id`` when routed via+ # OpenRouter generations this turn is billed for, collected launch by+ # launch. Populated from ``AssistantMessage.message_id`` when routed via# OpenRouter (``gen-...`` prefix). Consumed by the finally block to# fire ``record_turn_cost_from_openrouter`` for non-Anthropic models —# the CLI's static-Anthropic-priced estimate is replaced with the# authoritative ``/generation`` total_cost. Lives on ``_RetryState``- # (not per-attempt ``_StreamAccumulator``) so it survives retries.- generation_ids: list[str] = dataclass_field(default_factory=list)+ # (not per-attempt ``_StreamAccumulator``) so it survives retries; a+ # transient retry absorbs the failed launch's (``_do_transient_backoff``).+ generations: TurnGenerations = dataclass_field(default_factory=TurnGenerations)# The *actually executed* model observed on ``AssistantMessage.model`` —# differs from ``state.options.model`` (the requested primary) when# ``_resolve_fallback_model`` swaps to a fallback mid-attempt. The@@ -2390,6 +2387,32 @@ def _codex_gateway_usage(return result.usage if result is not None else None+def _reconciles_openrouter_cost(is_codex_transport: bool) -> bool:+ """Whether a turn's cost is read back from OpenRouter's ``/generation``."""+ return (+ not is_codex_transport+ and config.openrouter_active+ and config.sdk_reconcile_openrouter_cost+ )+++def _cli_project_dir(sdk_cwd: str) -> str | None:+ """The CLI's project dir, where the OpenRouter reconcile sweeps for+ subagents' gen-IDs.++ ``sdk_cwd`` is the per-session CLI working directory; the CLI encodes it+ into the project-dir name the same way ``encode_cwd_for_cli`` does, and+ writes the main transcript + any ``subagents/`` alongside it under+ ``~/.claude/projects/<encoded>/``. ``None`` when sdk_cwd isn't set+ (shouldn't happen in practice for SDK turns).+ """+ if not sdk_cwd:+ return None+ return os.path.join(+ os.path.expanduser("~/.claude/projects"), encode_cwd_for_cli(sdk_cwd)+ )++_MAX_TRANSIENT_BACKOFF_SECONDS = 30@@ -2437,8 +2460,7 @@ def _next_transient_backoff(async def _do_transient_backoff(backoff: int,state: "_RetryState",- message_id: str,- session_id: str,+ ctx: _StreamContext,) -> AsyncIterator[StreamStatus]:"""Emit a retry notification, sleep, and reset the SDK adapter.@@ -2447,18 +2469,35 @@ async def _do_transient_backoff(Usage already reported survives the retry, but the CLI cost baseline doesnot: the relaunch re-reads the total the resumed session restores, sowhatever the failed attempt spent without reporting a result is not- charged. The user pays for the attempt that succeeds, not for our failure.+ charged. Its OpenRouter generations, live or a subagent's, are absorbed+ for the same reason. The user pays for the attempt that succeeds, not for+ our failure.++ A launch that started a fresh CLI session (``--session-id``, no+ ``--resume``: a chat's first turn, or a turn whose session could not be+ restored) wrote that session's file before its call failed, and the CLI+ refuses the id again ("Session ID ... is already in use"). The file goes,+ so the relaunch can start the session again; the failed launch streamed+ nothing, so it holds nothing worth resuming.++ All of that is settled before the first yield. A turn torn down while it+ waits out the backoff, by a client that goes away or a stop, never comes+ back to this generator, yet the failed launch is no less ours.Extracted from both exception handlers in the retry loop to removenear-identical code duplication."""+ state.usage.cli_session_total_usd = None+ await state.generations.absorb_launch()+ fresh_session = state.options.session_id+ if ctx.sdk_cwd and fresh_session and state.options.resume is None:+ delete_stale_cli_session_file(ctx.sdk_cwd, fresh_session, ctx.log_prefix)yield StreamStatus(message=f"Connection interrupted, retrying in {backoff}s…")await asyncio.sleep(backoff)state.adapter = SDKResponseAdapter(- message_id=message_id,- session_id=session_id,+ message_id=ctx.message_id,+ session_id=ctx.session_id,)- state.usage.cli_session_total_usd = Nonedef _is_fallback_stderr(line: str) -> bool:@@ -4156,6 +4195,8 @@ async def _run_stream_attempt(`stream_chat_completion_sdk` — owns the retry loop that calls thisfunction up to `_MAX_STREAM_ATTEMPTS` times with reduced context."""+ # First, so whatever fails from here on is this launch's to absorb.+ await state.generations.start_launch()acc = _StreamAccumulator(assistant_response=ChatMessage(role="assistant", content=""),accumulated_tool_calls=[],@@ -5703,6 +5744,16 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesadapter=adapter,transcript_builder=transcript_builder,usage=_TokenUsage(),+ # Subagent files are read only on a turn the reconcile bills.+ generations=TurnGenerations(+ (+ _cli_project_dir(sdk_cwd)+ if _reconciles_openrouter_cost(is_codex_transport)+ else None+ ),+ session_id,+ turn_start_ts,+ ),)attempt = 0@@ -5955,7 +6006,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesmax_transient_retries,)async for evt in _do_transient_backoff(- backoff, state, message_id, session_id+ backoff, state, stream_ctx):yield evtcontinue # retry the same context-level attempt@@ -6029,7 +6080,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesmax_transient_retries,)async for evt in _do_transient_backoff(- backoff, state, message_id, session_id+ backoff, state, stream_ctx):yield evtcontinue # retry same context-level attempt@@ -6308,32 +6359,17 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssueseffective_model = sdk_model or config.thinking_standard_model# ``state`` is populated lazily inside the retry loop; when the# turn exits before the first attempt runs (e.g. very early- # validation error) it's still None, so ``generation_ids`` is- # empty by definition.+ # validation error) it's still None, so it has no generations.collected_gen_ids: list[str] = (- list(state.generation_ids) if state is not None else []+ list(state.generations.ids) if state is not None else []+ )+ absorbed_gen_ids: set[str] = (+ state.generations.absorbed if state is not None else set())_use_openrouter_reconcile = bool(- not is_codex_transport- and config.openrouter_active- and config.sdk_reconcile_openrouter_cost- and collected_gen_ids+ _reconciles_openrouter_cost(is_codex_transport) and collected_gen_ids)- # CLI project dir — used by the reconcile task to sweep for- # compaction subagents' gen-IDs. ``sdk_cwd`` is the per-session- # CLI working directory; the CLI encodes it into the project-dir- # name the same way ``encode_cwd_for_cli`` does, and writes- # the main transcript + any ``subagents/`` alongside it under- # ``~/.claude/projects/<encoded>/``. Empty when sdk_cwd isn't- # set (shouldn't happen in practice for SDK turns).- cli_project_dir: str | None = None- if sdk_cwd:- cli_project_dir = os.path.join(- os.path.expanduser("~/.claude/projects"),- encode_cwd_for_cli(sdk_cwd),- )-if is_codex_transport:await persist_and_record_usage(session=session,@@ -6388,13 +6424,14 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuescache_read_tokens=turn_cache_read_tokens,cache_creation_tokens=turn_cache_creation_tokens,generation_ids=collected_gen_ids,- cli_project_dir=cli_project_dir,+ cli_project_dir=_cli_project_dir(sdk_cwd),cli_session_id=session_id,turn_start_ts=turn_start_ts,fallback_cost_usd=turn_cost_usd,api_key=config.api_key,log_prefix=log_prefix,langfuse_trace_id=langfuse_trace_id,+ absorbed_generation_ids=absorbed_gen_ids,))_background_tasks.add(cost_reconcile_task)diff --git a/autogpt_platform/backend/backend/copilot/sdk/transient_retry_test.py b/autogpt_platform/backend/backend/copilot/sdk/transient_retry_test.pynew file mode 100644index 0000000000..5e00f9cc02--- /dev/null+++ b/autogpt_platform/backend/backend/copilot/sdk/transient_retry_test.py@@ -0,0 +1,629 @@+"""A turn the platform relaunches after a transient provider failure.++The relaunch hides a 5xx or an overload the user never saw, so it must leave+no trace of the failed launch: no charge for what it ran, on the live stream+or in a subagent's file, and no session id the CLI would refuse to start+again. A launch that already streamed a tool call cannot be hidden: the call+ran and the user watched it, and a relaunch would resend the query that made+it. That turn ends in the error instead, with the call kept.++Each turn below runs ``stream_chat_completion_sdk`` for real against a+scripted CLI. On an OpenRouter turn the cost reconcile runs for real too,+against a stand-in for OpenRouter's ``/generation`` endpoint.+"""++import asyncio+import contextlib+import json+import os+import random+import time+from collections.abc import Callable+from datetime import UTC, datetime+from pathlib import Path+from typing import Any+from unittest.mock import AsyncMock, MagicMock, patch++import httpx+import pytest+from claude_agent_sdk import (+ AssistantMessage,+ ResultMessage,+ TextBlock,+ ToolResultBlock,+ ToolUseBlock,+ UserMessage,+)+from pydantic import BaseModel++from backend.copilot.constants import (+ COMPACTION_TOOL_NAME,+ COPILOT_ERROR_PREFIX,+ COPILOT_RETRYABLE_ERROR_PREFIX,+)+from backend.copilot.model import ChatMessage, ChatSession+from backend.copilot.response_model import (+ StreamBaseResponse,+ StreamError,+ StreamStatus,+ StreamToolInputAvailable,+ StreamToolOutputAvailable,+)+from backend.copilot.sdk import service as sdk_service+from backend.copilot.transcript import cli_session_path++from .conftest import build_test_transcript+from .retry_scenarios_test import _make_sdk_patches+from .turn_cost_test import _SDK_CWD, _SESSION_ID, _SVC, _result++_MODEL = "anthropic/claude-sonnet-4-6"+# What OpenRouter billed for each generation, by ID.+_COSTS = {+ "gen-overflow": 0.011,+ "gen-failed": 0.02,+ "gen-failed-compact": 0.004,+ "gen-ok": 0.03,+ "gen-ok-compact": 0.005,+}+++ "overflowed_first",+ [+ pytest.param(False, id="first-launch"),+ # A context retry keeps the overflowing launch billed, as the+ # CLI-priced path does; only the launch the 529 ends is absorbed.+ pytest.param(True, id="after-a-context-retry"),+ ],+)+async def test_a_relaunch_bills_nothing_the_failed_launch_ran(+ overflowed_first, tmp_path, monkeypatch+):+ """The failed launch ran a generation OpenRouter billed, then its next+ call was refused with a 529. Only the launch that succeeded is charged."""+ turn = _Turn(tmp_path, monkeypatch)+ overflow = [[_generation("gen-overflow"), _prompt_too_long()]]+ launches = [+ *(overflow if overflowed_first else []),+ [_generation("gen-failed"), _overloaded()],+ _reply("gen-ok"),+ ]++ await turn.run(launches)++ assert len(turn.launched) == len(launches)+ assert not turn.errors()+ billed = ["gen-overflow", "gen-ok"] if overflowed_first else ["gen-ok"]+ assert turn.reconciled_cost() == pytest.approx(sum(_COSTS[g] for g in billed))+++async def test_a_turn_left_during_the_backoff_bills_nothing_of_the_failed_launch(+ tmp_path, monkeypatch+):+ """The client goes away on the "retrying" notice, before the relaunch.++ The failed launch was ours to absorb the moment the retry was decided, so+ the turn, which ends there, is charged nothing for it.+ """+ turn = _Turn(tmp_path, monkeypatch)++ await turn.run(+ [[_generation("gen-failed"), _overloaded()], _reply("gen-ok")],+ leave_when=lambda event: isinstance(event, StreamStatus)+ and "retrying" in event.message,+ )++ assert len(turn.launched) == 1+ turn.reconcile_mock().assert_not_awaited()+++async def test_a_failure_before_any_output_is_retried_and_billed_once(+ tmp_path, monkeypatch+):+ """The CLI compacted the session, then the query's own call was refused.++ Nothing reached the user, so the turn is relaunched, and the+ compaction's subagent generation goes with the failed launch: the turn+ is charged the launch that succeeded, its own compaction included.+ """+ turn = _Turn(tmp_path, monkeypatch)++ await turn.run(+ [+ [turn.subagent("agent-acompact-1", "gen-failed-compact"), _overloaded()],+ [turn.subagent("agent-acompact-2", "gen-ok-compact"), *_reply("gen-ok")],+ ]+ )++ assert len(turn.launched) == 2+ assert not turn.errors()+ assert [m.content for m in turn.persisted_messages()[3:]] == ["ok"]+ assert turn.reconciled_cost() == pytest.approx(+ _COSTS["gen-ok"] + _COSTS["gen-ok-compact"]+ )+++async def test_a_first_turn_failure_before_any_output_is_retried(tmp_path, monkeypatch):+ """A chat's first turn starts a fresh CLI session, and the CLI writes its+ file before the first call. Relaunched under the same session id the CLI+ refuses to start ("Session ID ... is already in use"), so the retry must+ start the session over instead of failing the turn."""+ turn = _Turn(tmp_path, monkeypatch, first_turn=True)++ await turn.run([[_overloaded()], _reply("gen-ok")])++ assert [options.resume for options in turn.launched] == [None, None]+ assert not turn.errors()+ assert [m.content for m in turn.persisted_messages()[1:]] == ["ok"]+ assert turn.reconciled_cost() == pytest.approx(_COSTS["gen-ok"])+++ ("failure", "code"),+ [+ pytest.param("overloaded", "transient_api_error", id="overloaded"),+ pytest.param("dropped", "sdk_stream_error", id="connection-dropped"),+ ],+)+async def test_a_launch_that_ran_a_tool_is_not_relaunched(+ failure, code, tmp_path, monkeypatch+):+ """The model sent an email, then its next call failed transiently.++ A relaunch would resend the query, and a model that sends the email+ again sends it twice; it would also roll the call the user watched out+ of the chat. The turn ends in the error, and the chat keeps what ran.+ """+ turn = _Turn(tmp_path, monkeypatch)+ sent: list[str] = []+ fail = _overloaded() if failure == "overloaded" else _dropped++ await turn.run(+ [+ [*_sends_email("toolu_1", sent), fail],+ # Relaunched, the model repeats the call: a second email.+ [*_sends_email("toolu_2", sent), *_reply(None)],+ ]+ )++ assert len(turn.launched) == 1+ assert sent == ["toolu_1"]+ assert [error.code for error in turn.errors()] == [code]+ assert turn.streamed_tool_rows() == turn.persisted_tool_rows()+ assert turn.streamed_tool_rows() == ({"toolu_1"}, {"toolu_1"})+ persisted = turn.persisted_messages()+ assert (persisted[-1].content or "").startswith(+ (COPILOT_ERROR_PREFIX, COPILOT_RETRYABLE_ERROR_PREFIX)+ )+++async def test_random_turns_relaunch_only_what_streamed_nothing(tmp_path, monkeypatch):+ """Seeded random turns: every launch does some unseen work (a generation,+ a subagent), maybe shows text or runs a tool, then succeeds, overflows+ the context, or fails transiently with a 529 or a dropped connection.++ However the steps fall, the loop relaunches exactly when the retry rules+ allow it and nothing was streamed, so no tool runs twice and the chat keeps+ every row it showed; and OpenRouter bills exactly the launches no transient+ retry absorbed.+ """+ for seed in range(_SEEDS):+ rng = random.Random(seed)+ root = tmp_path / str(seed)+ root.mkdir()+ turn = _Turn(root, monkeypatch, first_turn=rng.random() < 0.3)+ sent: list[str] = []+ plans = [+ _random_launch(rng, f"{seed}-{n}", turn, sent, monkeypatch)+ for n in range(_MAX_LAUNCHES)+ ]++ await turn.run([plan.steps for plan in plans])++ ran = plans[: len(turn.launched)]+ context = f"seed {seed}: {[(p.shown, p.end) for p in ran]}"+ assert len(ran) == _launches_allowed(plans), context+ assert sent == [p.tool for p in ran if p.tool], context+ assert turn.streamed_tool_rows() == turn.persisted_tool_rows(), context+ kept = [p for i, p in enumerate(ran) if i == len(ran) - 1 or not p.transient]+ billed = [g for p in kept for g in p.live + p.subagent]+ reconcile = turn.reconcile_mock()+ if any(p.live for p in kept):+ reconcile.assert_awaited_once()+ assert reconcile.await_args.kwargs["cost_usd"] == pytest.approx(+ sum(_COSTS[g] for g in billed)+ ), context+ else:+ reconcile.assert_not_awaited()+++# A step of a scripted launch: a message the CLI streams, or something done+# at that point (a subagent writing its transcript, a tool doing its work).+_Step = AssistantMessage | UserMessage | ResultMessage | Callable[[], None]++_SEEDS = 200+# Three context attempts, each with one transient retry.+_MAX_LAUNCHES = 6+_TRANSIENT_ENDS = ("overloaded", "dropped")+++class _Launch(BaseModel):+ """One scripted launch of a random turn, and what it does."""++ steps: list[Any] = []+ live: list[str] = []+ subagent: list[str] = []+ shown: str = "nothing"+ tool: str | None = None+ end: str = "ok"++ @property+ def streamed(self) -> bool:+ return self.shown != "nothing" or self.end == "ok"++ @property+ def transient(self) -> bool:+ return self.end in _TRANSIENT_ENDS+++def _random_launch(+ rng: random.Random,+ key: str,+ turn: "_Turn",+ sent: list[str],+ monkeypatch: pytest.MonkeyPatch,+) -> _Launch:+ launch = _Launch(shown=rng.choice(["nothing", "text", "tool"]))+ for n in range(rng.randint(0, 2)):+ generation = f"gen-{key}-{n}"+ monkeypatch.setitem(_COSTS, generation, rng.randint(1, 99) / 1000)+ if rng.random() < 0.5:+ launch.steps.append(_generation(generation))+ launch.live.append(generation)+ else:+ launch.steps.append(turn.subagent(f"agent-{key}-{n}", generation))+ launch.subagent.append(generation)+ shown = f"gen-{key}-shown"+ monkeypatch.setitem(_COSTS, shown, rng.randint(1, 99) / 1000)+ if launch.shown == "text":+ launch.steps.append(+ AssistantMessage(+ content=[TextBlock(text="partial")], model=_MODEL, message_id=shown+ )+ )+ launch.live.append(shown)+ elif launch.shown == "tool":+ launch.tool = f"toolu_{key}"+ launch.steps += _sends_email(launch.tool, sent, generation_id=shown)+ launch.live.append(shown)+ launch.end = rng.choice(["ok", "overflow", *_TRANSIENT_ENDS])+ if launch.end == "ok":+ reply = f"gen-{key}-reply"+ monkeypatch.setitem(_COSTS, reply, rng.randint(1, 99) / 1000)+ launch.steps += _reply(reply)+ launch.live.append(reply)+ elif launch.end == "overflow":+ launch.steps.append(_prompt_too_long())+ elif launch.end == "overloaded":+ launch.steps.append(_overloaded())+ else:+ launch.steps.append(_dropped)+ return launch+++def _launches_allowed(plans: list[_Launch]) -> int:+ """How many launches the retry rules make of *plans*: a launch is retried+ only if it streamed nothing, a transient failure once per context+ attempt, a context overflow until the third attempt."""+ attempt = transient = 0+ for n, plan in enumerate(plans, start=1):+ if plan.streamed:+ return n+ if plan.end == "overflow":+ attempt, transient = attempt + 1, 0+ if attempt >= 3:+ return n+ else:+ transient += 1+ if transient > 1:+ return n+ return len(plans)+++class _Turn:+ """A user turn of a chat: its second by default, so the CLI resumes its+ session; on ``first_turn`` the CLI starts one."""++ def __init__(+ self,+ tmp_path: Path,+ monkeypatch: pytest.MonkeyPatch,+ *,+ first_turn: bool = False,+ ) -> None:+ # The CLI keeps its sessions, and its subagents their transcripts,+ # under the home dir; the reconcile reads them back from there.+ monkeypatch.setenv("HOME", str(tmp_path))+ monkeypatch.setenv("CLAUDE_CONFIG_DIR", str(tmp_path / ".claude"))+ session_file = Path(cli_session_path(_SDK_CWD, _SESSION_ID))+ self._subagents = session_file.parent / _SESSION_ID / "subagents"+ self._subagents.mkdir(parents=True)+ self.launched: list[Any] = []+ self.events: list[StreamBaseResponse] = []+ prior = [+ ChatMessage(role="user", content="prior question"),+ ChatMessage(role="assistant", content="prior answer"),+ ]+ self.session = ChatSession(+ session_id=_SESSION_ID,+ user_id="test-user",+ usage=[],+ started_at=datetime.now(UTC),+ updated_at=datetime.now(UTC),+ messages=[+ *([] if first_turn else prior),+ ChatMessage(role="user", content="hello"),+ ],+ # A titled chat skips title generation, a model call of its own.+ title="Retries",+ )+ self._launches: list[list[_Step]] = []+ self._mocks: dict[str, MagicMock] = {}++ def subagent(self, agent: str, generation_id: str) -> Callable[[], None]:+ """A step: a subagent of the CLI's writes its transcript."""++ def write() -> None:+ path = self._subagents / f"{agent}.jsonl"+ entry = {"type": "assistant", "message": {"id": generation_id}}+ path.write_text(json.dumps(entry) + "\n")+ # During the turn, whatever the filesystem's clock says.+ now = time.time()+ os.utime(path, (now, now))++ return write++ async def run(+ self,+ launches: list[list[_Step]],+ *,+ leave_when: Callable[[StreamBaseResponse], bool] | None = None,+ ) -> None:+ """Play the turn; ``leave_when`` closes the stream at the first event+ it matches, the way a client that goes away does."""+ self._launches = launches+ patches = _make_sdk_patches(+ self.session,+ original_transcript=build_test_transcript(+ [("user", "prior question"), ("assistant", "prior answer")]+ ),+ compacted_transcript=build_test_transcript(+ [("user", "summary"), ("assistant", "summarized answer")]+ ),+ client_side_effect=self._launch,+ )+ for target, kwargs in patches:+ if target == f"{_SVC}.config":+ kwargs.update(+ openrouter_active=True,+ sdk_reconcile_openrouter_cost=True,+ thinking_standard_model=_MODEL,+ )+ with contextlib.ExitStack() as stack:+ for target, kwargs in patches:+ self._mocks[target] = stack.enter_context(patch(target, **kwargs))+ # A resumed turn diffs the user's skills, and a first turn loads+ # the user's context, both over DB and scheduler RPCs.+ for name, value in (+ ("build_skills_update_notice", None),+ ("build_session_context", ""),+ ("inject_user_context", None),+ ):+ stack.enter_context(+ patch(f"{_SVC}.{name}", new_callable=AsyncMock, return_value=value)+ )+ stack.enter_context(+ patch(f"{_SVC}._compute_transient_backoff", return_value=0)+ )+ self._mocks["persist"] = stack.enter_context(+ patch(f"{_SVC}.persist_and_record_usage", new_callable=AsyncMock)+ )+ self._mocks["reconcile"] = stack.enter_context(+ patch(+ "backend.copilot.sdk.openrouter_cost.persist_and_record_usage",+ new_callable=AsyncMock,+ )+ )+ stack.enter_context(patch("httpx.AsyncClient.get", new=_generation_lookup))+ stream = sdk_service.stream_chat_completion_sdk(+ session_id=_SESSION_ID,+ message="hello",+ is_user_message=True,+ user_id="test-user",+ session=self.session,+ )+ async for event in stream:+ self.events.append(event)+ if leave_when is not None and leave_when(event):+ break+ await stream.aclose()+ # The reconcile charges from a task the turn leaves behind.+ await asyncio.gather(*_reconcile_tasks())++ def errors(self) -> list[StreamError]:+ return [e for e in self.events if isinstance(e, StreamError)]++ def reconcile_mock(self) -> MagicMock:+ """What the OpenRouter reconcile charged, if it ran."""+ return self._mocks["reconcile"]++ def reconciled_cost(self) -> float:+ self._mocks["persist"].assert_not_awaited()+ reconcile = self._mocks["reconcile"]+ reconcile.assert_awaited_once()+ return reconcile.await_args.kwargs["cost_usd"]++ def persisted_messages(self) -> list[ChatMessage]:+ """The chat as the turn's last save wrote it."""+ upsert = self._mocks[f"{_SVC}.upsert_chat_session"]+ return list(upsert.await_args.args[0].messages)++ def streamed_tool_rows(self) -> tuple[set[str], set[str]]:+ """The model's tool calls and their results the turn streamed, by ID.++ Compaction rows are left out: one the loop opens and then finds+ nothing to compact for is retired on the wire, never persisted.+ """+ calls = {+ e.toolCallId+ for e in self.events+ if isinstance(e, StreamToolInputAvailable)+ and e.toolName != COMPACTION_TOOL_NAME+ }+ results = {+ e.toolCallId+ for e in self.events+ if isinstance(e, StreamToolOutputAvailable)+ and e.toolName != COMPACTION_TOOL_NAME+ }+ return calls, results++ def persisted_tool_rows(self) -> tuple[set[str], set[str]]:+ """The model's tool calls and their results the chat kept, by ID."""+ messages = self.persisted_messages()+ tool_calls = [tc for m in messages for tc in m.tool_calls or []]+ compactions = {+ tc["id"]+ for tc in tool_calls+ if tc["function"]["name"] == COMPACTION_TOOL_NAME+ }+ calls = {tc["id"] for tc in tool_calls} - compactions+ results = {+ m.tool_call_id+ for m in messages+ if m.role == "tool" and m.tool_call_id and m.tool_call_id not in compactions+ }+ return calls, results++ def _launch(self, *args: Any, **kwargs: Any) -> AsyncMock:+ options = kwargs["options"]+ script = self._launches[len(self.launched)]+ self.launched.append(options)+ refusal = _start_session(options)++ async def receive():+ if refusal is not None:+ raise refusal+ for step in script:+ if callable(step):+ step()+ else:+ yield step++ client = MagicMock()+ client.query = AsyncMock()+ client.receive_response = receive+ context = AsyncMock()+ context.__aenter__.return_value = client+ context.__aexit__.return_value = None+ return context+++def _start_session(options: Any) -> Exception | None:+ """What the CLI does with ``--session-id``: write the session's file before+ its first call, and refuse an id whose file is already there."""+ if options.resume is not None or not options.session_id:+ return None+ path = Path(cli_session_path(_SDK_CWD, options.session_id))+ if path.exists():+ return RuntimeError(+ "Command failed with exit code 1: "+ f"Error: Session ID {options.session_id} is already in use."+ )+ path.write_text("")+ return None+++def _overloaded() -> AssistantMessage:+ """How the bundled CLI (2.1.284) reports a 529 once its retries are spent."""+ return AssistantMessage(+ content=[+ TextBlock(+ text="API Error: 529 Overloaded. This is a server-side issue, "+ "usually temporary — try again in a moment."+ )+ ],+ model=_MODEL,+ error="server_error",+ )+++def _dropped() -> None:+ raise ConnectionError("ECONNRESET: connection reset by peer")+++def _generation(generation_id: str) -> AssistantMessage:+ """A billed generation that streamed nothing the user could see."""+ return AssistantMessage(content=[], model=_MODEL, message_id=generation_id)+++def _reply(generation_id: str | None) -> list[_Step]:+ return [+ AssistantMessage(+ content=[TextBlock(text="ok")], model=_MODEL, message_id=generation_id+ ),+ _result(0.03),+ ]+++def _prompt_too_long() -> ResultMessage:+ result = _result(0.011)+ result.subtype = "error_during_execution"+ result.is_error = True+ result.result = "Prompt is too long"+ return result+++def _sends_email(+ tool_use_id: str, sent: list[str], generation_id: str | None = None+) -> list[_Step]:+ return [+ AssistantMessage(+ content=[+ ToolUseBlock(+ id=tool_use_id,+ name="send_email",+ input={"to": "[email protected]", "subject": "Report"},+ )+ ],+ model=_MODEL,+ message_id=generation_id,+ ),+ lambda: sent.append(tool_use_id),+ UserMessage(content=[ToolResultBlock(tool_use_id=tool_use_id, content="sent")]),+ ]+++async def _generation_lookup(+ _client: httpx.AsyncClient, _url: str, *, params: dict[str, str], **_: Any+) -> httpx.Response:+ cost = _COSTS.get(params["id"])+ if cost is None:+ # Permanent, so the reconcile does not back off and retry it.+ return httpx.Response(403)+ return httpx.Response(200, json={"data": {"total_cost": cost}})+++def _reconcile_tasks() -> list[asyncio.Task]:+ loop = asyncio.get_running_loop()+ return [+ task+ for task in sdk_service._background_tasks+ if task.get_loop() is loop and not task.done()+ ]