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.
feat(platform/copilot): live baseline streaming + render flag + Sonar web_search + simulator cost tracking + reconnect fixes (#12873)
Zamil Majdy33a608ec7833
diff --git a/autogpt_platform/backend/backend/copilot/baseline/reasoning.py b/autogpt_platform/backend/backend/copilot/baseline/reasoning.pyindex 0c689ed4a7..1d0da8ce7e 100644--- a/autogpt_platform/backend/backend/copilot/baseline/reasoning.py+++ b/autogpt_platform/backend/backend/copilot/baseline/reasoning.py@@ -50,13 +50,13 @@ _VISIBLE_REASONING_TYPES = frozenset({"reasoning.text", "reasoning.summary"})# (~4,700 deltas per turn in one observed session, vs ~28 for Sonnet); without# coalescing, every chunk is one Redis ``xadd`` + one SSE frame + one React# re-render of the non-virtualised chat list, which paint-storms the browser-# main thread and freezes the UI. Batching into ~32-char / ~40 ms windows-# cuts the event rate ~100x while staying snappy enough that the Reasoning+# main thread and freezes the UI. Batching into ~64-char / ~50 ms windows+# cuts the event rate ~150x while staying snappy enough that the Reasoning# collapse still feels live (well under the ~100 ms perceptual threshold).# Per-delta persistence to ``session.messages`` stays granular — we only# coalesce the *wire* emission.-_COALESCE_MIN_CHARS = 32-_COALESCE_MAX_INTERVAL_MS = 40.0+_COALESCE_MIN_CHARS = 64+_COALESCE_MAX_INTERVAL_MS = 50.0class ReasoningDetail(BaseModel):@@ -242,6 +242,12 @@ class BaselineReasoningEmitter:fresh ``ChatMessage(role="reasoning")`` is appended and mutatedin-place as further deltas arrive; :meth:`close` drops the referencebut leaves the appended row intact.++ ``render_in_ui=False`` suppresses only the live wire events+ (``StreamReasoning*``); the ``role='reasoning'`` persistence row is+ still appended so ``convertChatSessionToUiMessages.ts`` can hydrate+ the reasoning bubble on reload. The state machine advances+ identically either way."""def __init__(@@ -250,21 +256,19 @@ class BaselineReasoningEmitter:*,coalesce_min_chars: int = _COALESCE_MIN_CHARS,coalesce_max_interval_ms: float = _COALESCE_MAX_INTERVAL_MS,+ render_in_ui: bool = True,) -> None:self._block_id: str = str(uuid.uuid4())self._open: bool = Falseself._session_messages = session_messagesself._current_row: ChatMessage | None = None- # Coalescing state — ``_pending_delta`` accumulates reasoning text- # between wire flushes. Providers like Kimi K2.6 emit very fine-- # grained chunks; batching them reduces Redis ``xadd`` + SSE + React- # re-render load by ~100x for equivalent text output. Tuning knobs- # are kwargs so tests can disable coalescing (``=0``) for- # deterministic event assertions.+ # Coalescing state — tests can disable (``=0``) for deterministic+ # event assertions.self._coalesce_min_chars = coalesce_min_charsself._coalesce_max_interval_ms = coalesce_max_interval_msself._pending_delta: str = ""self._last_flush_monotonic: float = 0.0+ self._render_in_ui = render_in_ui@propertydef is_open(self) -> bool:@@ -296,8 +300,9 @@ class BaselineReasoningEmitter:# syscalls off the hot path without changing semantics.now = time.monotonic()if not self._open:- events.append(StreamReasoningStart(id=self._block_id))- events.append(StreamReasoningDelta(id=self._block_id, delta=text))+ if self._render_in_ui:+ events.append(StreamReasoningStart(id=self._block_id))+ events.append(StreamReasoningDelta(id=self._block_id, delta=text))self._open = Trueself._last_flush_monotonic = nowif self._session_messages is not None:@@ -305,17 +310,15 @@ class BaselineReasoningEmitter:self._session_messages.append(self._current_row)return events- # Persist per-delta (no coalescing here — the session snapshot stays- # consistent at every chunk boundary, independent of the wire- # coalesce window).if self._current_row is not None:self._current_row.content = (self._current_row.content or "") + textself._pending_delta += textif self._should_flush_pending(now):- events.append(- StreamReasoningDelta(id=self._block_id, delta=self._pending_delta)- )+ if self._render_in_ui:+ events.append(+ StreamReasoningDelta(id=self._block_id, delta=self._pending_delta)+ )self._pending_delta = ""self._last_flush_monotonic = nowreturn events@@ -348,12 +351,13 @@ class BaselineReasoningEmitter:if not self._open:return []events: list[StreamBaseResponse] = []- if self._pending_delta:- events.append(- StreamReasoningDelta(id=self._block_id, delta=self._pending_delta)- )- self._pending_delta = ""- events.append(StreamReasoningEnd(id=self._block_id))+ if self._render_in_ui:+ if self._pending_delta:+ events.append(+ StreamReasoningDelta(id=self._block_id, delta=self._pending_delta)+ )+ events.append(StreamReasoningEnd(id=self._block_id))+ self._pending_delta = ""self._open = Falseself._block_id = str(uuid.uuid4())self._current_row = Nonediff --git a/autogpt_platform/backend/backend/copilot/baseline/reasoning_test.py b/autogpt_platform/backend/backend/copilot/baseline/reasoning_test.pyindex e18c8066e4..1f5ca01845 100644--- a/autogpt_platform/backend/backend/copilot/baseline/reasoning_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/reasoning_test.py@@ -452,3 +452,63 @@ class TestReasoningPersistence:events = emitter.on_delta(_delta(reasoning="pure wire"))assert len(events) == 2 # start + delta, no crash# Nothing else to assert — just proves None session is supported.+++class TestBaselineReasoningEmitterRenderFlag:+ """``render_in_ui=False`` must silence ``StreamReasoning*`` wire events+ AND drop persistence of ``role="reasoning"`` rows — the operator hides+ the collapse on both the live wire and on reload. Persistence is tied+ to the wire events because the frontend's hydration path unconditionally+ re-renders persisted reasoning rows; keeping them would make the flag a+ no-op post-reload. These tests pin the contract in both directions so+ future refactors can't flip only one half."""++ def test_render_off_suppresses_start_and_delta(self):+ emitter = BaselineReasoningEmitter(render_in_ui=False)+ events = emitter.on_delta(_delta(reasoning="hidden"))+ # No wire events, but state advanced (is_open == True) so close()+ # below has something to rotate.+ assert events == []+ assert emitter.is_open is True++ def test_render_off_suppresses_close_end(self):+ emitter = BaselineReasoningEmitter(render_in_ui=False)+ emitter.on_delta(_delta(reasoning="hidden"))+ events = emitter.close()+ assert events == []+ assert emitter.is_open is False++ def test_render_off_still_persists(self):+ """Persistence is decoupled from the render flag — session+ transcript always keeps the ``role="reasoning"`` row so audit+ and ``--resume``-equivalent replay never lose thinking text.+ The frontend gates rendering separately."""+ session: list[ChatMessage] = []+ emitter = BaselineReasoningEmitter(session, render_in_ui=False)++ emitter.on_delta(_delta(reasoning="part one "))+ emitter.on_delta(_delta(reasoning="part two"))+ emitter.close()++ assert len(session) == 1+ assert session[0].role == "reasoning"+ assert session[0].content == "part one part two"++ def test_render_off_rotates_block_id_between_sessions(self):+ """Even with wire events silenced the block id must rotate on close,+ otherwise a hypothetical mid-session flip would reuse a stale id."""+ emitter = BaselineReasoningEmitter(render_in_ui=False)+ emitter.on_delta(_delta(reasoning="first"))+ first_block_id = emitter._block_id+ emitter.close()+ emitter.on_delta(_delta(reasoning="second"))+ assert emitter._block_id != first_block_id++ def test_render_on_is_default(self):+ """Defaulting to True preserves backward compat — existing callers+ that don't pass the kwarg keep emitting wire events as before."""+ emitter = BaselineReasoningEmitter()+ events = emitter.on_delta(_delta(reasoning="hello"))+ assert len(events) == 2+ assert isinstance(events[0], StreamReasoningStart)+ assert isinstance(events[1], StreamReasoningDelta)diff --git a/autogpt_platform/backend/backend/copilot/baseline/service.py b/autogpt_platform/backend/backend/copilot/baseline/service.pyindex 6aa88e9d41..9f9153ffaf 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service.py@@ -15,7 +15,7 @@ import reimport shutilimport tempfileimport uuid-from collections.abc import AsyncGenerator, Mapping, Sequence+from collections.abc import AsyncGenerator, Iterable, Mapping, Sequencefrom dataclasses import dataclass, fieldfrom functools import partialfrom typing import TYPE_CHECKING, Any, cast@@ -343,7 +343,21 @@ class _BaselineStreamState:"""model: str = ""- pending_events: list[StreamBaseResponse] = field(default_factory=list)+ # Live delivery channel drained concurrently by ``stream_chat_completion_baseline``+ # so reasoning / text / tool events reach the SSE wire **during** the upstream+ # LLM stream, not after ``_baseline_llm_caller`` returns. Before this was a+ # ``list`` drained per ``tool_call_loop`` iteration, so any model with+ # extended thinking (Anthropic via OpenRouter, Moonshot, future reasoning+ # routes) froze the UI for the entire duration of each LLM round before+ # flushing the backlog in one burst. The queue is single-producer (the+ # streaming loop) / single-consumer (the outer async-gen yield loop);+ # ``None`` is the close sentinel.+ pending_events: asyncio.Queue[StreamBaseResponse | None] = field(+ default_factory=asyncio.Queue+ )+ # Mirror of every event put on ``pending_events`` — kept for unit tests that+ # inspect post-hoc what was emitted. Not consumed by production code.+ emitted_events: list[StreamBaseResponse] = field(default_factory=list)assistant_text: str = ""text_block_id: str = field(default_factory=lambda: str(uuid.uuid4()))text_started: bool = False@@ -382,7 +396,33 @@ class _BaselineStreamState:# frontend's ``convertChatSessionToUiMessages`` relies on these# rows to render the Reasoning collapse after the AI SDK's# stream-end hydrate swaps in the DB-backed message list.- self.reasoning_emitter = BaselineReasoningEmitter(self.session_messages)+ # ``render_in_ui`` is sourced from ``config.render_reasoning_in_ui``+ # so the operator can silence the reasoning collapse globally+ # without dropping the persisted audit trail.+ self.reasoning_emitter = BaselineReasoningEmitter(+ self.session_messages,+ render_in_ui=config.render_reasoning_in_ui,+ )+++def _emit(state: "_BaselineStreamState", event: StreamBaseResponse) -> None:+ """Queue *event* for the live SSE wire AND mirror into ``emitted_events``.++ Single helper so every streaming producer (LLM stream loop, tool executor,+ conversation updater) posts to the same single-consumer queue. The mirror+ list is read-only from production code — it exists so unit tests can assert+ on the full sequence emitted during one call.+ """+ state.pending_events.put_nowait(event)+ state.emitted_events.append(event)+++def _emit_all(+ state: "_BaselineStreamState", events: Iterable[StreamBaseResponse]+) -> None:+ """Queue *events* in order — convenience for emitter batches."""+ for event in events:+ _emit(state, event)def _is_anthropic_model(model: str) -> bool:@@ -519,7 +559,7 @@ async def _baseline_llm_caller(Extracted from ``stream_chat_completion_baseline`` for readability."""- state.pending_events.append(StreamStartStep())+ _emit(state, StreamStartStep())# Fresh thinking-strip state per round so a malformed unclosed# block in one LLM call cannot silently drop content in the next.state.thinking_stripper = _ThinkingStripper()@@ -621,31 +661,30 @@ async def _baseline_llm_caller(if not delta:continue- state.pending_events.extend(state.reasoning_emitter.on_delta(delta))+ _emit_all(state, state.reasoning_emitter.on_delta(delta))if delta.content:# Text and reasoning must not interleave on the wire — the# AI SDK maps distinct start/end pairs to distinct UI# parts. Close any open reasoning block before emitting# the first text delta of this run.- state.pending_events.extend(state.reasoning_emitter.close())+ _emit_all(state, state.reasoning_emitter.close())emit = state.thinking_stripper.process(delta.content)if emit:if not state.text_started:- state.pending_events.append(- StreamTextStart(id=state.text_block_id)- )+ _emit(state, StreamTextStart(id=state.text_block_id))state.text_started = Trueround_text += emit- state.pending_events.append(- StreamTextDelta(id=state.text_block_id, delta=emit)+ _emit(+ state,+ StreamTextDelta(id=state.text_block_id, delta=emit),)if delta.tool_calls:# Same rule as the text branch: close any open reasoning# block before a tool_use starts so the AI SDK treats# reasoning and tool-use as distinct parts.- state.pending_events.extend(state.reasoning_emitter.close())+ _emit_all(state, state.reasoning_emitter.close())for tc in delta.tool_calls:idx = tc.indexif idx not in tool_calls_by_index:@@ -676,19 +715,17 @@ async def _baseline_llm_caller(# ``async for chunk in response`` would otherwise leave reasoning# and/or text unterminated and only ``StreamFinishStep`` emitted —# the Reasoning / Text collapses would never finalise.- state.pending_events.extend(state.reasoning_emitter.close())+ _emit_all(state, state.reasoning_emitter.close())# Flush any buffered text held back by the thinking stripper.tail = state.thinking_stripper.flush()if tail:if not state.text_started:- state.pending_events.append(StreamTextStart(id=state.text_block_id))+ _emit(state, StreamTextStart(id=state.text_block_id))state.text_started = Trueround_text += tail- state.pending_events.append(- StreamTextDelta(id=state.text_block_id, delta=tail)- )+ _emit(state, StreamTextDelta(id=state.text_block_id, delta=tail))if state.text_started:- state.pending_events.append(StreamTextEnd(id=state.text_block_id))+ _emit(state, StreamTextEnd(id=state.text_block_id))state.text_started = Falsestate.text_block_id = str(uuid.uuid4())# Always persist partial text so the session history stays consistent,@@ -696,7 +733,7 @@ async def _baseline_llm_caller(state.assistant_text += round_text# Always emit StreamFinishStep to match the StreamStartStep,# even if an exception occurred during streaming.- state.pending_events.append(StreamFinishStep())+ _emit(state, StreamFinishStep())# Convert to shared formatllm_tool_calls = [@@ -738,13 +775,14 @@ async def _baseline_tool_executor(except orjson.JSONDecodeError as parse_err:parse_error = f"Invalid JSON arguments for tool '{tool_name}': {parse_err}"logger.warning("[Baseline] %s", parse_error)- state.pending_events.append(+ _emit(+ state,StreamToolOutputAvailable(toolCallId=tool_call_id,toolName=tool_name,output=parse_error,success=False,- )+ ),)return ToolCallResult(tool_call_id=tool_call_id,@@ -753,15 +791,17 @@ async def _baseline_tool_executor(is_error=True,)- state.pending_events.append(- StreamToolInputStart(toolCallId=tool_call_id, toolName=tool_name)+ _emit(+ state,+ StreamToolInputStart(toolCallId=tool_call_id, toolName=tool_name),)- state.pending_events.append(+ _emit(+ state,StreamToolInputAvailable(toolCallId=tool_call_id,toolName=tool_name,input=tool_args,- )+ ),)# Announce the tool call to the session so in-turn guards like@@ -785,7 +825,7 @@ async def _baseline_tool_executor(session=session,tool_call_id=tool_call_id,)- state.pending_events.append(result)+ _emit(state, result)tool_output = (result.output if isinstance(result.output, str) else str(result.output))@@ -802,13 +842,14 @@ async def _baseline_tool_executor(error_output,exc_info=True,)- state.pending_events.append(+ _emit(+ state,StreamToolOutputAvailable(toolCallId=tool_call_id,toolName=tool_name,output=error_output,success=False,- )+ ),)return ToolCallResult(tool_call_id=tool_call_id,@@ -1660,139 +1701,172 @@ async def stream_chat_completion_baseline(state=state,)- try:- loop_result = None- async for loop_result in tool_call_loop(- messages=openai_messages,- tools=tools,- llm_call=_bound_llm_caller,- execute_tool=_bound_tool_executor,- update_conversation=_bound_conversation_updater,- max_iterations=_MAX_TOOL_ROUNDS,- ):- # Drain buffered events after each iteration (real-time streaming)- for evt in state.pending_events:- yield evt- state.pending_events.clear()-- # Inject any messages the user queued while the turn was- # running. ``tool_call_loop`` mutates ``openai_messages``- # in-place, so appending here means the model sees the new- # messages on its next LLM call.- #- # IMPORTANT: skip when the loop has already finished (no- # more LLM calls are coming). ``tool_call_loop`` yields- # a final ``ToolCallLoopResult`` on both paths:- # - natural finish: ``finished_naturally=True``- # - hit max_iterations: ``finished_naturally=False``- # and ``iterations >= max_iterations``- # In either case the loop is about to return on the next- # ``async for`` step, so draining here would silently- # lose the message (the user sees 202 but the model never- # reads the text). Those messages stay in the buffer and- # get picked up at the start of the next turn.- is_final_yield = (- loop_result.finished_naturally- or loop_result.iterations >= _MAX_TOOL_ROUNDS- )- if is_final_yield:- continue- try:- pending = await drain_pending_messages(session_id)- except Exception:- logger.warning(- "[Baseline] mid-loop drain_pending_messages failed for session %s",- session_id,- exc_info=True,- )- pending = []- if pending:- # Flush any buffered assistant/tool messages from completed- # rounds into session.messages BEFORE appending the pending- # user message. ``_baseline_conversation_updater`` only- # records assistant+tool rounds into ``state.session_messages``- # — they are normally batch-flushed in the finally block.- # Without this in-order flush, the mid-loop pending user- # message lands before the preceding round's assistant/tool- # entries, producing chronologically-wrong session.messages- # on persist (user interposed between an assistant tool_call- # and its tool-result), which breaks OpenAI tool-call ordering- # invariants on the next turn's replay.+ # Run the tool-call loop concurrently with the event consumer so+ # ``StreamReasoning*`` / ``StreamText*`` deltas emitted inside+ # ``_baseline_llm_caller`` reach the SSE wire DURING the upstream LLM+ # stream instead of only at iteration boundaries. Any reasoning route+ # that streams for several minutes per round (extended thinking on+ # Anthropic / Moonshot / future providers) would otherwise freeze the+ # UI for the whole window before flushing the backlog in one burst.+ loop_result_holder: list[Any] = [None]+ loop_task: asyncio.Task[None] | None = None++ async def _run_tool_call_loop() -> None:+ # Read/write the current session via ``_session_holder`` so this+ # closure doesn't need to ``nonlocal session`` — pyright can't narrow+ # the outer ``session: ChatSession | None`` through a nested scope,+ # but the holder is typed non-optional after the preflight guard+ # above.+ try:+ async for loop_result in tool_call_loop(+ messages=openai_messages,+ tools=tools,+ llm_call=_bound_llm_caller,+ execute_tool=_bound_tool_executor,+ update_conversation=_bound_conversation_updater,+ max_iterations=_MAX_TOOL_ROUNDS,+ ):+ loop_result_holder[0] = loop_result+ # Inject any messages the user queued while the turn was+ # running. ``tool_call_loop`` mutates ``openai_messages``+ # in-place, so appending here means the model sees the new+ # messages on its next LLM call.#- # Also persist any assistant text from text-only rounds (rounds- # with no tool calls, which ``_baseline_conversation_updater``- # does NOT record in session_messages). If we only update- # ``_flushed_assistant_text_len`` without persisting the text,- # that text is silently lost: the finally block only appends- # assistant_text[_flushed_assistant_text_len:], so text generated- # before this drain never reaches session.messages.- recorded_text = "".join(- m.content or ""- for m in state.session_messages- if m.role == "assistant"- )- unflushed_text = state.assistant_text[- state._flushed_assistant_text_len :- ]- text_only_text = (- unflushed_text[len(recorded_text) :]- if unflushed_text.startswith(recorded_text)- else unflushed_text+ # IMPORTANT: skip when the loop has already finished (no+ # more LLM calls are coming). ``tool_call_loop`` yields+ # a final ``ToolCallLoopResult`` on both paths:+ # - natural finish: ``finished_naturally=True``+ # - hit max_iterations: ``finished_naturally=False``+ # and ``iterations >= max_iterations``+ # In either case the loop is about to return on the next+ # ``async for`` step, so draining here would silently+ # lose the message (the user sees 202 but the model never+ # reads the text). Those messages stay in the buffer and+ # get picked up at the start of the next turn.+ is_final_yield = (+ loop_result.finished_naturally+ or loop_result.iterations >= _MAX_TOOL_ROUNDS)- if text_only_text.strip():- session.messages.append(- ChatMessage(role="assistant", content=text_only_text)+ if is_final_yield:+ continue+ try:+ pending = await drain_pending_messages(session_id)+ except Exception:+ logger.warning(+ "[Baseline] mid-loop drain_pending_messages failed for "+ "session %s",+ session_id,+ exc_info=True,)- for _buffered in state.session_messages:- session.messages.append(_buffered)- state.session_messages.clear()- # Record how much assistant_text has been covered by the- # structured entries just flushed, so the finally block's- # final-text dedup doesn't re-append rounds already persisted.- state._flushed_assistant_text_len = len(state.assistant_text)-- # Persist the assistant/tool flush BEFORE the pending append- # so a later pending-persist failure can roll back the- # pending rows without also discarding LLM output.- session = await persist_session_safe(session, "[Baseline]")- # ``upsert_chat_session`` may return a *new* ``ChatSession``- # instance (e.g. when a concurrent title update has written a- # newer title to Redis, it returns ``session.model_copy``).- # Keep ``_session_holder`` in sync so subsequent tool rounds- # executed via ``_bound_tool_executor`` see the fresh session- # — any tool-side mutations on the stale object would be- # discarded when the new one is persisted in the ``finally``.- _session_holder[0] = session-- # ``format_pending_as_user_message`` embeds file attachments- # and context URL/page content into the content string so- # the in-session transcript is a faithful copy of what the- # model actually saw. We also mirror each push into- # ``openai_messages`` so the model's next LLM round sees it.- #- # Pre-compute the formatted dicts once so both the openai- # messages append and the content_of lookup inside the- # shared helper use the same string — and so ``on_rollback``- # can trim ``openai_messages`` to the recorded anchor.- formatted_by_pm = {- id(pm): format_pending_as_user_message(pm) for pm in pending- }- _openai_anchor = len(openai_messages)- for pm in pending:- openai_messages.append(formatted_by_pm[id(pm)])-- def _trim_openai_on_rollback(_session_anchor: int) -> None:- del openai_messages[_openai_anchor:]-- await persist_pending_as_user_rows(- session,- transcript_builder,- pending,- log_prefix="[Baseline]",- content_of=lambda pm: formatted_by_pm[id(pm)]["content"],- on_rollback=_trim_openai_on_rollback,- )-+ pending = []+ if pending:+ # Flush any buffered assistant/tool messages from completed+ # rounds into session.messages BEFORE appending the pending+ # user message. ``_baseline_conversation_updater`` only+ # records assistant+tool rounds into ``state.session_messages``+ # — they are normally batch-flushed in the finally block.+ # Without this in-order flush, the mid-loop pending user+ # message lands before the preceding round's assistant/tool+ # entries, producing chronologically-wrong session.messages+ # on persist (user interposed between an assistant tool_call+ # and its tool-result), which breaks OpenAI tool-call ordering+ # invariants on the next turn's replay.+ #+ # Also persist any assistant text from text-only rounds (rounds+ # with no tool calls, which ``_baseline_conversation_updater``+ # does NOT record in session_messages). If we only update+ # ``_flushed_assistant_text_len`` without persisting the text,+ # that text is silently lost: the finally block only appends+ # assistant_text[_flushed_assistant_text_len:], so text generated+ # before this drain never reaches session.messages.+ recorded_text = "".join(+ m.content or ""+ for m in state.session_messages+ if m.role == "assistant"+ )+ unflushed_text = state.assistant_text[+ state._flushed_assistant_text_len :+ ]+ text_only_text = (+ unflushed_text[len(recorded_text) :]+ if unflushed_text.startswith(recorded_text)+ else unflushed_text+ )+ current_session = _session_holder[0]+ if text_only_text.strip():+ current_session.messages.append(+ ChatMessage(role="assistant", content=text_only_text)+ )+ for _buffered in state.session_messages:+ current_session.messages.append(_buffered)+ state.session_messages.clear()+ # Record how much assistant_text has been covered by the+ # structured entries just flushed, so the finally block's+ # final-text dedup doesn't re-append rounds already persisted.+ state._flushed_assistant_text_len = len(state.assistant_text)++ # Persist the assistant/tool flush BEFORE the pending append+ # so a later pending-persist failure can roll back the+ # pending rows without also discarding LLM output.+ current_session = await persist_session_safe(+ current_session, "[Baseline]"+ )+ # ``upsert_chat_session`` may return a *new* ``ChatSession``+ # instance (e.g. when a concurrent title update has written a+ # newer title to Redis, it returns ``session.model_copy``).+ # Keep ``_session_holder`` in sync so subsequent tool rounds+ # executed via ``_bound_tool_executor`` see the fresh session+ # — any tool-side mutations on the stale object would be+ # discarded when the new one is persisted in the ``finally``.+ _session_holder[0] = current_session++ # ``format_pending_as_user_message`` embeds file attachments+ # and context URL/page content into the content string so+ # the in-session transcript is a faithful copy of what the+ # model actually saw. We also mirror each push into+ # ``openai_messages`` so the model's next LLM round sees it.+ #+ # Pre-compute the formatted dicts once so both the openai+ # messages append and the content_of lookup inside the+ # shared helper use the same string — and so ``on_rollback``+ # can trim ``openai_messages`` to the recorded anchor.+ formatted_by_pm = {+ id(pm): format_pending_as_user_message(pm) for pm in pending+ }+ _openai_anchor = len(openai_messages)+ for pm in pending:+ openai_messages.append(formatted_by_pm[id(pm)])++ def _trim_openai_on_rollback(_session_anchor: int) -> None:+ del openai_messages[_openai_anchor:]++ await persist_pending_as_user_rows(+ current_session,+ transcript_builder,+ pending,+ log_prefix="[Baseline]",+ content_of=lambda pm: formatted_by_pm[id(pm)]["content"],+ on_rollback=_trim_openai_on_rollback,+ )+ finally:+ # Always post the sentinel so the outer consumer exits — even if+ # ``tool_call_loop`` raised. ``_baseline_llm_caller``'s own+ # finally block has already pushed ``StreamReasoningEnd`` /+ # ``StreamTextEnd`` / ``StreamFinishStep`` at this point, so the+ # sentinel only terminates the consumer; it does not suppress+ # any still-unflushed events.+ state.pending_events.put_nowait(None)++ loop_task = asyncio.create_task(_run_tool_call_loop())+ try:+ while True:+ evt = await state.pending_events.get()+ if evt is None:+ break+ yield evt+ # Sentinel received — surface any exception the inner task hit.+ await loop_task+ loop_result = loop_result_holder[0]if loop_result and not loop_result.finished_naturally:limit_msg = (f"Exceeded {_MAX_TOOL_ROUNDS} tool-call rounds "@@ -1803,25 +1877,34 @@ async def stream_chat_completion_baseline(errorText=limit_msg,code="baseline_tool_round_limit",)-except Exception as e:_stream_error = Trueerror_msg = str(e) or type(e).__name__logger.error("[Baseline] Streaming error: %s", error_msg, exc_info=True)- # ``_baseline_llm_caller``'s finally block closes any open- # reasoning / text blocks and appends ``StreamFinishStep`` on- # both normal and exception paths, so pending_events already has- # the correct protocol ordering:- # StreamStartStep -> StreamReasoningStart -> ...deltas... ->- # StreamReasoningEnd -> StreamTextStart -> ...deltas... ->- # StreamTextEnd -> StreamFinishStep- # Just drain what's buffered, then yield the error.- for evt in state.pending_events:- yield evt- state.pending_events.clear()+ # Drain any queued tail events (reasoning/text close + finish step)+ # that ``_baseline_llm_caller``'s finally block pushed before the+ # sentinel arrived — without this the frontend would be missing the+ # matching end / finish parts for the partial round.+ while not state.pending_events.empty():+ evt = state.pending_events.get_nowait()+ if evt is not None:+ yield evtyield StreamError(errorText=error_msg, code="baseline_error")# Still persist whatever we gotfinally:+ # Cancel the inner task if we're unwinding early (client disconnect,+ # unexpected error in the consumer) so it doesn't keep streaming+ # tokens into a dead queue.+ if loop_task is not None and not loop_task.done():+ loop_task.cancel()+ try:+ await loop_task+ except (asyncio.CancelledError, Exception):+ pass+ # Re-sync the outer ``session`` binding in case the inner task+ # reassigned it via a mid-loop ``persist_session_safe`` call.+ session = _session_holder[0]+# In-flight tool-call announcements are only meaningful for the# current turn; clear at the top of the outer finally so the next# turn starts with a clean scratch buffer even if one of thediff --git a/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py b/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.pyindex 5a95c5c901..44c49eb732 100644--- a/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py+++ b/autogpt_platform/backend/backend/copilot/baseline/service_unit_test.py@@ -39,7 +39,10 @@ from backend.util.tool_call_loop import LLMLoopResponse, LLMToolCall, ToolCallReclass TestBaselineStreamState:def test_defaults(self):state = _BaselineStreamState()- assert state.pending_events == []+ # ``pending_events`` is an asyncio.Queue now (live SSE channel).+ # The durable inspection view is ``emitted_events``.+ assert state.pending_events.empty()+ assert state.emitted_events == []assert state.assistant_text == ""assert state.text_started is Falseassert state.turn_prompt_tokens == 0@@ -1687,7 +1690,7 @@ class TestBaselineReasoningStreaming:state=state,)- types = [type(e).__name__ for e in state.pending_events]+ types = [type(e).__name__ for e in state.emitted_events]assert "StreamReasoningStart" in typesassert "StreamReasoningDelta" in typesassert "StreamReasoningEnd" in types@@ -1702,14 +1705,14 @@ class TestBaselineReasoningStreaming:# a fresh id after the reasoning-end rotation.reasoning_ids = {e.id- for e in state.pending_events+ for e in state.emitted_eventsif isinstance(e, (StreamReasoningStart, StreamReasoningDelta, StreamReasoningEnd))}text_ids = {e.id- for e in state.pending_events+ for e in state.emitted_eventsif isinstance(e, (StreamTextStart, StreamTextDelta, StreamTextEnd))}assert len(reasoning_ids) == 1@@ -1717,7 +1720,7 @@ class TestBaselineReasoningStreaming:assert reasoning_ids.isdisjoint(text_ids)combined = "".join(- e.delta for e in state.pending_events if isinstance(e, StreamReasoningDelta)+ e.delta for e in state.emitted_events if isinstance(e, StreamReasoningDelta))assert combined == "thinking... more"@@ -1759,7 +1762,7 @@ class TestBaselineReasoningStreaming:# A reasoning-end must have been emitted — this is the tool_calls# branch's responsibility, not the stream-end cleanup.- types = [type(e).__name__ for e in state.pending_events]+ types = [type(e).__name__ for e in state.emitted_events]assert "StreamReasoningStart" in typesassert "StreamReasoningEnd" in types@@ -1802,7 +1805,7 @@ class TestBaselineReasoningStreaming:state=state,)- types = [type(e).__name__ for e in state.pending_events]+ types = [type(e).__name__ for e in state.emitted_events]# The reasoning block was opened, the exception fired, and the# finally block must have closed it before emitting the finish# step.@@ -1935,7 +1938,7 @@ class TestBaselineReasoningStreaming:state=state,)- types = [type(e).__name__ for e in state.pending_events]+ types = [type(e).__name__ for e in state.emitted_events]assert "StreamReasoningStart" in typesassert "StreamReasoningEnd" in types# No text was produced — no text events should be emitted.diff --git a/autogpt_platform/backend/backend/copilot/config.py b/autogpt_platform/backend/backend/copilot/config.pyindex d2c66a3484..08dcaf8898 100644--- a/autogpt_platform/backend/backend/copilot/config.py+++ b/autogpt_platform/backend/backend/copilot/config.py@@ -92,8 +92,10 @@ class ChatConfig(BaseSettings):description="Model to use for generating session titles (should be fast/cheap)",)simulation_model: str = Field(- default="google/gemini-2.5-flash",- description="Model for dry-run block simulation (should be fast/cheap with good JSON output)",+ default="google/gemini-2.5-flash-lite",+ description="Model for dry-run block simulation (should be fast/cheap with good JSON output). "+ "Gemini 2.5 Flash-Lite is ~3x cheaper than Flash ($0.10/$0.40 vs $0.30/$1.20 per MTok) "+ "with JSON-mode reliability adequate for shape-matching block outputs.",)api_key: str | None = Field(default=None, description="OpenAI API key")base_url: str | None = Field(@@ -249,6 +251,20 @@ class ChatConfig(BaseSettings):"``max_thinking_tokens`` kwarg so the CLI falls back to model default ""(which, without the flag, leaves extended thinking off).",)+ render_reasoning_in_ui: bool = Field(+ default=True,+ description="Render reasoning as live UI parts "+ "(``StreamReasoning*`` wire events). False suppresses the live "+ "wire events only; ``role='reasoning'`` rows are always persisted "+ "so the reasoning bubble hydrates on reload. Tokens are billed "+ "upstream regardless.",+ )+ stream_replay_count: int = Field(+ default=200,+ ge=1,+ le=10000,+ description="Max Redis stream entries replayed on SSE reconnect.",+ )claude_agent_thinking_effort: Literal["low", "medium", "high", "max"] | None = (Field(default=None,diff --git a/autogpt_platform/backend/backend/copilot/config_test.py b/autogpt_platform/backend/backend/copilot/config_test.pyindex fe8e67b7ff..25f9f477f2 100644--- a/autogpt_platform/backend/backend/copilot/config_test.py+++ b/autogpt_platform/backend/backend/copilot/config_test.py@@ -19,6 +19,8 @@ _ENV_VARS_TO_CLEAR = ("OPENAI_BASE_URL","CHAT_CLAUDE_AGENT_CLI_PATH","CLAUDE_AGENT_CLI_PATH",+ "CHAT_RENDER_REASONING_IN_UI",+ "CHAT_STREAM_REPLAY_COUNT",)@@ -164,3 +166,38 @@ class TestClaudeAgentCliPathEnvFallback:monkeypatch.setenv("CLAUDE_AGENT_CLI_PATH", str(tmp_path))with pytest.raises(Exception, match="not a regular file"):ChatConfig()+++class TestRenderReasoningInUi:+ """``render_reasoning_in_ui`` gates reasoning wire events globally."""++ def test_defaults_to_true(self):+ """Default must stay True — flipping it silences the reasoning+ collapse for every user, which is an opt-in operator decision."""+ cfg = ChatConfig()+ assert cfg.render_reasoning_in_ui is True++ def test_env_override_false(self, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setenv("CHAT_RENDER_REASONING_IN_UI", "false")+ cfg = ChatConfig()+ assert cfg.render_reasoning_in_ui is False+++class TestStreamReplayCount:+ """``stream_replay_count`` caps the SSE reconnect replay batch size."""++ def test_default_is_200(self):+ """200 covers a full Kimi turn after coalescing (~150 events) while+ bounding the replay storm from 1000+ chunks."""+ cfg = ChatConfig()+ assert cfg.stream_replay_count == 200++ def test_env_override(self, monkeypatch: pytest.MonkeyPatch) -> None:+ monkeypatch.setenv("CHAT_STREAM_REPLAY_COUNT", "500")+ cfg = ChatConfig()+ assert cfg.stream_replay_count == 500++ def test_zero_rejected(self):+ """count=0 would make XREAD replay nothing — rejected via ge=1."""+ with pytest.raises(Exception):+ ChatConfig(stream_replay_count=0)diff --git a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.pyindex 17b54797b8..829e511f7e 100644--- a/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/p0_guardrails_test.py@@ -714,11 +714,16 @@ class TestDoTransientBackoff:mock_sleep.assert_called_once_with(7)async def test_replaces_adapter_with_new_instance(self):- """state.adapter is replaced with a new SDKResponseAdapter after yield."""+ """state.adapter is replaced with a new SDKResponseAdapter after yield,+ and ``render_reasoning_in_ui`` is threaded from the SDK service config+ (not hardcoded) so ``CHAT_RENDER_REASONING_IN_UI=false`` at runtime+ flips the reconstruction consistently with the rest of the path."""from unittest.mock import AsyncMock, MagicMock, patchfrom backend.copilot.sdk.service import _do_transient_backoff+ cfg = _make_config(render_reasoning_in_ui=False)+original_adapter = MagicMock()state = MagicMock()state.adapter = original_adapter@@ -726,6 +731,7 @@ class TestDoTransientBackoff:with (patch("asyncio.sleep", new=AsyncMock()),+ patch(f"{_SVC}.config", cfg),patch("backend.copilot.sdk.service.SDKResponseAdapter") as mock_cls,):new_adapter = MagicMock()@@ -733,7 +739,11 @@ class TestDoTransientBackoff:async for _ in _do_transient_backoff(3, state, "msg-1", "sess-1"):pass- mock_cls.assert_called_once_with(message_id="msg-1", session_id="sess-1")+ mock_cls.assert_called_once_with(+ message_id="msg-1",+ session_id="sess-1",+ render_reasoning_in_ui=False,+ )assert state.adapter is new_adapterasync def test_resets_usage_after_yield(self):diff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter.pyindex fbd73d9277..6db4615062 100644--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py@@ -53,7 +53,13 @@ class SDKResponseAdapter:text blocks, tool calls, and message lifecycle."""- def __init__(self, message_id: str | None = None, session_id: str | None = None):+ def __init__(+ self,+ message_id: str | None = None,+ session_id: str | None = None,+ *,+ render_reasoning_in_ui: bool = True,+ ):self.message_id = message_id or str(uuid.uuid4())self.session_id = session_idself.text_block_id = str(uuid.uuid4())@@ -62,6 +68,7 @@ class SDKResponseAdapter:self.reasoning_block_id = str(uuid.uuid4())self.has_started_reasoning = Falseself.has_ended_reasoning = True+ self.render_reasoning_in_ui = render_reasoning_in_uiself.current_tool_calls: dict[str, dict[str, str]] = {}self.resolved_tool_calls: set[str] = set()self.step_open = False@@ -142,6 +149,17 @@ class SDKResponseAdapter:# it live, extended_thinking turns that end# thinking-only left the UI stuck on "Thought for Xs"# with nothing rendered until a page refresh.+ #+ # When ``render_reasoning_in_ui=False`` the three+ # reasoning helpers below (and the append) no-op, so+ # the frontend sees a text-only stream AND no+ # ``ChatMessage(role='reasoning')`` row is persisted+ # (the row is only created by ``_dispatch_response``+ # when ``StreamReasoningStart`` arrives, which is+ # suppressed here). Persistence of the thinking text+ # into the SDK transcript via+ # ``_format_sdk_content_blocks`` is unaffected — that+ # feeds ``--resume`` continuity, not the UI.if block.thinking:self._end_text_if_open(responses)self._ensure_reasoning_started(responses)@@ -347,8 +365,12 @@ class SDKResponseAdapter:"""Start (or restart) a reasoning block if needed.Each ``ThinkingBlock`` the SDK emits gets its own streaming block- on the wire so the frontend can render a new ``Reasoning`` part- per LLM turn (rather than concatenating across the whole session).+ so the frontend can render a new ``Reasoning`` part per LLM turn+ (rather than concatenating across the whole session). Events+ are emitted unconditionally — the caller filters them out of the+ SSE wire when ``render_reasoning_in_ui=False`` but still feeds+ them through ``_dispatch_response`` so the session transcript+ keeps a ``role='reasoning'`` row."""if not self.has_started_reasoning or self.has_ended_reasoning:if self.has_ended_reasoning:diff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.pyindex 634454f9e5..b9a4237792 100644--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py@@ -331,6 +331,69 @@ def test_empty_thinking_block_is_ignored():assert [type(r).__name__ for r in results] == ["StreamStartStep"]+def test_render_reasoning_in_ui_false_still_emits_adapter_events():+ """With the persist/render decoupling the adapter is flag-agnostic:+ it always emits ``StreamReasoning*`` so the session transcript keeps a+ durable reasoning record. Wire-level suppression when+ ``render_reasoning_in_ui=False`` happens at the SDK service yield+ boundary, not here — see+ ``backend/copilot/sdk/service.py::_filter_reasoning_events``.+ """+ adapter = SDKResponseAdapter(+ message_id="m",+ session_id="s",+ render_reasoning_in_ui=False,+ )+ msg = AssistantMessage(+ content=[ThinkingBlock(thinking="plan", signature="sig")],+ model="test",+ )+ results = adapter.convert_message(msg)+ types = [type(r).__name__ for r in results]+ assert "StreamReasoningStart" in types+ assert "StreamReasoningDelta" in types+++def test_render_reasoning_off_text_after_thinking_still_closes_reasoning():+ """Adapter still emits a ``StreamReasoningEnd`` when text follows a+ thinking block — decoupled from the render flag. The service layer+ drops the reasoning events at yield time; the adapter's structural+ open/close pairing must not depend on the flag or downstream filters+ would see orphan reasoning starts on the persisted transcript.+ """+ adapter = SDKResponseAdapter(+ message_id="m",+ session_id="s",+ render_reasoning_in_ui=False,+ )+ adapter.convert_message(+ AssistantMessage(+ content=[ThinkingBlock(thinking="warming up", signature="sig")],+ model="test",+ )+ )+ results = adapter.convert_message(+ AssistantMessage(content=[TextBlock(text="hello")], model="test")+ )+ types = [type(r).__name__ for r in results]+ assert "StreamReasoningEnd" in types+ assert "StreamTextStart" in types+ assert "StreamTextDelta" in types+++def test_render_reasoning_on_is_default():+ """Default is True — existing callers keep emitting reasoning events."""+ adapter = SDKResponseAdapter(message_id="m", session_id="s")+ msg = AssistantMessage(+ content=[ThinkingBlock(thinking="plan", signature="sig")],+ model="test",+ )+ results = adapter.convert_message(msg)+ types = [type(r).__name__ for r in results]+ assert "StreamReasoningStart" in types+ assert "StreamReasoningDelta" in types++def test_result_success_synthesizes_fallback_text_when_final_turn_is_thinking_only():"""If the model's last LLM call after a tool_result produced only aThinkingBlock (no TextBlock), the UI would hang on the tool outputdiff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 908e2aebdd..ca0d69e6ba 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -823,7 +823,11 @@ async def _do_transient_backoff("""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)+ state.adapter = SDKResponseAdapter(+ message_id=message_id,+ session_id=session_id,+ render_reasoning_in_ui=config.render_reasoning_in_ui,+ )state.usage.reset()@@ -2374,6 +2378,18 @@ async def _run_stream_attempt(skip_strip=response is tail_delta,)if dispatched is not None:+ # Persistence (via _dispatch_response) always runs so the+ # session transcript keeps role='reasoning' rows; the+ # wire is gated so UI can suppress rendering.+ if not state.adapter.render_reasoning_in_ui and isinstance(+ dispatched,+ (+ StreamReasoningStart,+ StreamReasoningDelta,+ StreamReasoningEnd,+ ),+ ):+ continueyield dispatched# Mid-turn follow-up persistence: the MCP tool wrapper drains@@ -3160,7 +3176,11 @@ async def stream_chat_completion_sdk(options = ClaudeAgentOptions(**sdk_options_kwargs) # type: ignore[arg-type] # dynamic kwargs- adapter = SDKResponseAdapter(message_id=message_id, session_id=session_id)+ adapter = SDKResponseAdapter(+ message_id=message_id,+ session_id=session_id,+ render_reasoning_in_ui=config.render_reasoning_in_ui,+ )# Propagate user_id/session_id as OTEL context attributes so the# langsmith tracing integration attaches them to every span. This@@ -3494,7 +3514,9 @@ async def stream_chat_completion_sdk(session, user_id, is_user_message, state.query_message)state.adapter = SDKResponseAdapter(- message_id=message_id, session_id=session_id+ message_id=message_id,+ session_id=session_id,+ render_reasoning_in_ui=config.render_reasoning_in_ui,)# Reset token accumulators so a failed attempt's partial# usage is not double-counted in the successful attempt.diff --git a/autogpt_platform/backend/backend/copilot/stream_registry.py b/autogpt_platform/backend/backend/copilot/stream_registry.pyindex 424964e075..e4559c46e5 100644--- a/autogpt_platform/backend/backend/copilot/stream_registry.py+++ b/autogpt_platform/backend/backend/copilot/stream_registry.py@@ -485,9 +485,11 @@ async def subscribe_to_session(subscriber_queue: asyncio.Queue[StreamBaseResponse] = asyncio.Queue()stream_key = _get_turn_stream_key(session.turn_id)- # Step 1: Replay messages from Redis Stream+ # Replay batch capped by ``stream_replay_count``.xread_start = time.perf_counter()- messages = await redis.xread({stream_key: last_message_id}, block=None, count=1000)+ messages = await redis.xread(+ {stream_key: last_message_id}, block=None, count=config.stream_replay_count+ )xread_time = (time.perf_counter() - xread_start) * 1000logger.info(f"[TIMING] Redis xread (replay) took {xread_time:.1f}ms, status={session_status}",diff --git a/autogpt_platform/backend/backend/copilot/tools/helpers.py b/autogpt_platform/backend/backend/copilot/tools/helpers.pyindex 6c25e79188..9de94cb2f2 100644--- a/autogpt_platform/backend/backend/copilot/tools/helpers.py+++ b/autogpt_platform/backend/backend/copilot/tools/helpers.py@@ -181,7 +181,9 @@ async def execute_block(# (e.g., "42" → 42, string booleans → bool, enum defaults applied).coerce_inputs_to_schema(input_data, block.input_schema)outputs: dict[str, list[Any]] = defaultdict(list)- async for output_name, output_data in simulate_block(block, input_data):+ async for output_name, output_data in simulate_block(+ block, input_data, user_id=user_id+ ):outputs[output_name].append(output_data)# simulator signals internal failure via ("error", "[SIMULATOR ERROR …]")sim_error = outputs.get("error", [])diff --git a/autogpt_platform/backend/backend/copilot/tools/models.py b/autogpt_platform/backend/backend/copilot/tools/models.pyindex 08b62056a4..39a84cfa49 100644--- a/autogpt_platform/backend/backend/copilot/tools/models.py+++ b/autogpt_platform/backend/backend/copilot/tools/models.py@@ -602,11 +602,13 @@ class WebSearchResponse(ToolResponseBase):type: ResponseType = ResponseType.WEB_SEARCHquery: str+ # Web-grounded synthesised answer the search provider wrote from+ # fresh page content. The LLM caller should read this directly+ # instead of re-fetching each citation URL — many sites are+ # bot-protected and ``web_fetch`` won't get through. Empty string+ # when the provider returned only citations.+ answer: str = ""results: list[WebSearchResult] = Field(default_factory=list)- # Backend-reported usage for this call (copied from Anthropic's- # ``usage.server_tool_use``). Surfaces as metadata for frontend- # debug panels but is also what drives rate-limit / cost tracking- # via ``persist_and_record_usage(provider="anthropic")``.search_requests: int = 0diff --git a/autogpt_platform/backend/backend/copilot/tools/test_dry_run.py b/autogpt_platform/backend/backend/copilot/tools/test_dry_run.pyindex 1f71c837cf..fc44a57c86 100644--- a/autogpt_platform/backend/backend/copilot/tools/test_dry_run.py+++ b/autogpt_platform/backend/backend/copilot/tools/test_dry_run.py@@ -237,7 +237,7 @@ async def test_execute_block_dry_run_skips_real_execution():mock_block = make_mock_block()mock_block.execute = AsyncMock() # should NOT be called- async def fake_simulate(block, input_data):+ async def fake_simulate(block, input_data, **_kwargs):yield "result", "simulated"# Patching at helpers.simulate_block works because helpers.py imports@@ -267,7 +267,7 @@ async def test_execute_block_dry_run_response_format():"""Dry-run response should look like a normal success (no dry-run signal to LLM)."""mock_block = make_mock_block()- async def fake_simulate(block, input_data):+ async def fake_simulate(block, input_data, **_kwargs):yield "result", "simulated"with patch(@@ -331,7 +331,7 @@ async def test_execute_block_real_execution_unchanged():# Just verify simulate_block is NOT called.simulate_called = False- async def fake_simulate(block, input_data):+ async def fake_simulate(block, input_data, **_kwargs):nonlocal simulate_calledsimulate_called = Trueyield "result", "should not happen"@@ -455,7 +455,7 @@ async def test_execute_block_dry_run_no_empty_error_from_simulator():"""mock_block = make_mock_block()- async def fake_simulate(block, input_data):+ async def fake_simulate(block, input_data, **_kwargs):# Simulator now omits empty error pins at sourceyield "result", "simulated output"@@ -485,7 +485,7 @@ async def test_execute_block_dry_run_keeps_nonempty_error_pin():"""Dry-run should keep the 'error' pin when it contains a real error message."""mock_block = make_mock_block()- async def fake_simulate(block, input_data):+ async def fake_simulate(block, input_data, **_kwargs):yield "result", ""yield "error", "API rate limit exceeded"@@ -515,7 +515,7 @@ async def test_execute_block_dry_run_message_includes_completed_status():"""Dry-run message should clearly indicate COMPLETED status."""mock_block = make_mock_block()- async def fake_simulate(block, input_data):+ async def fake_simulate(block, input_data, **_kwargs):yield "result", "simulated"with patch(@@ -541,7 +541,7 @@ async def test_execute_block_dry_run_simulator_error_returns_error_response():"""When simulate_block yields a SIMULATOR ERROR tuple, execute_block returns ErrorResponse."""mock_block = make_mock_block()- async def fake_simulate_error(block, input_data):+ async def fake_simulate_error(block, input_data, **_kwargs):yield ("error","[SIMULATOR ERROR — NOT A BLOCK FAILURE] No LLM client available (missing OpenAI/OpenRouter API key).",diff --git a/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py b/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.pyindex 7b370f810c..f64a7550cd 100644--- a/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/tool_schema_test.py@@ -25,7 +25,14 @@ from backend.copilot.tools import TOOL_REGISTRY# (server-side Anthropic beta). Description already trimmed to the# minimum viable copy; the bump absorbs the schema skeleton cost# (~300 chars / ~75 tokens) for a new LLM-facing primitive.-_CHAR_BUDGET = 32_800+# Bumped 32800 -> 33200 on PR #12873 for the web_search Perplexity+# Sonar refactor — adds a load-bearing `deep` boolean with explicit+# "~100x more expensive" cost warning the model must see to avoid+# accidentally triggering sonar-reasoning on ordinary lookups, plus+# synthesised-answer wording in the top-level description so the LLM+# reads the answer before reaching for `web_fetch`. Both are+# LLM-decision-critical copy, not bloat.+_CHAR_BUDGET = 33_200@pytest.fixture(scope="module")diff --git a/autogpt_platform/backend/backend/copilot/tools/web_search.py b/autogpt_platform/backend/backend/copilot/tools/web_search.pyindex feb999d4d6..ed54868917 100644--- a/autogpt_platform/backend/backend/copilot/tools/web_search.py+++ b/autogpt_platform/backend/backend/copilot/tools/web_search.py@@ -1,29 +1,66 @@-"""Web search tool — wraps Anthropic's server-side ``web_search`` beta.+"""Web search tool — Perplexity Sonar via OpenRouter.-Single entry point for web search on both SDK and baseline paths. The-``web_search_20250305`` tool is server-side on Anthropic, so we call-the Messages API directly regardless of which LLM invoked the copilot-tool — OpenRouter can't proxy server-side tool execution.+One provider, two tiers, one billing path:++* ``deep=False`` (default) — ``perplexity/sonar``. Searches the web+ natively and returns citation annotations in a single inference pass.+* ``deep=True`` — ``perplexity/sonar-deep-research``. Multi-step+ agentic research; slower and costlier.++Why Sonar and not the ``openrouter:web_search`` server tool + dispatch+model? The server tool feeds all search-result page content back into+the dispatch model for a second inference pass — one observed call was+74K input tokens at Gemini Flash rates, billing $0.072. Sonar+searches natively in one pass, returns annotations typed as+``ChatCompletionMessage.annotations`` in ``openai.types``, and at+$1 / MTok base pricing lands ~$0.01 / call at our default shape.++``resp.usage.cost`` carries the real billed value via OpenRouter's+``include: true`` extension; the value flows through+``persist_and_record_usage(provider='open_router')`` into the daily /+weekly microdollar rate-limit counter on the same rails as every other+OpenRouter turn — no separate provider ledger line, no estimation+drift. ``_extract_cost_usd`` mirrors the baseline service's+``_extract_usage_cost`` logic; keep the two in sync if one changes."""import logging+import mathfrom typing import Any-from anthropic import AsyncAnthropic+from openai import AsyncOpenAI+from openai.types import CompletionUsage+from openai.types.chat import ChatCompletion+from backend.copilot.config import ChatConfigfrom backend.copilot.model import ChatSessionfrom backend.copilot.token_tracking import persist_and_record_usage-from backend.util.settings import Settingsfrom .base import BaseToolfrom .models import ErrorResponse, ToolResponseBase, WebSearchResponse, WebSearchResultlogger = logging.getLogger(__name__)-_WEB_SEARCH_DISPATCH_MODEL = "claude-haiku-4-5"-_MAX_DISPATCH_TOKENS = 512+_chat_config = ChatConfig()++_QUICK_MODEL = "perplexity/sonar"+# Sonar base can emit up to ~4K output; cap at the provider ceiling so the+# model stops when the answer is complete rather than when our budget trips.+_QUICK_MAX_TOKENS = 4096++_DEEP_MODEL = "perplexity/sonar-deep-research"+# Deep runs can produce long structured writeups — ~4x the quick ceiling+# is enough headroom for multi-source comparisons without uncapping.+_DEEP_MAX_TOKENS = _QUICK_MAX_TOKENS * 4+_DEFAULT_MAX_RESULTS = 5_HARD_MAX_RESULTS = 20+_SNIPPET_MAX_CHARS = 500++# OpenRouter-specific extra_body flag that embeds the real generation+# cost into the response usage object. Same dict shape the baseline+# service uses — keep the two aligned.+_OPENROUTER_INCLUDE_USAGE_COST: dict[str, Any] = {"usage": {"include": True}}class WebSearchTool(BaseTool):@@ -36,9 +73,13 @@ class WebSearchTool(BaseTool):@propertydef description(self) -> str:return (- "Search the web for live info (news, recent docs). Returns "- "{title, url, snippet}; use web_fetch to deep-dive a URL. "- "Prefer one targeted query over many reformulations."+ "Search the web for live info (news, recent docs). Returns a "+ "synthesised answer grounded in fresh page content plus "+ "{title, url, snippet} citations — read the answer first "+ "before reaching for web_fetch. Set deep=true when the user "+ "asks for research / comparison / in-depth analysis; leave "+ "deep=false for quick fact lookups. Prefer one targeted "+ "query over many reformulations.")@property@@ -58,6 +99,18 @@ class WebSearchTool(BaseTool):),"default": _DEFAULT_MAX_RESULTS,},+ "deep": {+ "type": "boolean",+ "description": (+ "Only set true when the user EXPLICITLY asks for "+ "research, comparison, or in-depth investigation "+ "across many sources — it is ~100x more expensive "+ "and much slower than a normal search. Default "+ "false; do not flip it for ordinary fact lookups "+ "or fresh-news questions."+ ),+ "default": False,+ },},"required": ["query"],}@@ -68,7 +121,7 @@ class WebSearchTool(BaseTool):@propertydef is_available(self) -> bool:- return bool(Settings().secrets.anthropic_api_key)+ return bool(_chat_config.api_key and _chat_config.base_url)async def _execute(self,@@ -76,6 +129,7 @@ class WebSearchTool(BaseTool):session: ChatSession,query: str = "",max_results: int = _DEFAULT_MAX_RESULTS,+ deep: bool = False,**kwargs: Any,) -> ToolResponseBase:query = (query or "").strip()@@ -93,44 +147,35 @@ class WebSearchTool(BaseTool):max_results = _DEFAULT_MAX_RESULTSmax_results = max(1, min(max_results, _HARD_MAX_RESULTS))- api_key = Settings().secrets.anthropic_api_key- if not api_key:+ if not _chat_config.api_key or not _chat_config.base_url:return ErrorResponse(message=("Web search is unavailable — the deployment has no "- "Anthropic API key configured."+ "OpenRouter credentials configured."),error="web_search_not_configured",session_id=session_id,)- client = AsyncAnthropic(api_key=api_key)+ client = AsyncOpenAI(+ api_key=_chat_config.api_key, base_url=_chat_config.base_url+ )+ model_used = _DEEP_MODEL if deep else _QUICK_MODEL+ max_tokens = _DEEP_MAX_TOKENS if deep else _QUICK_MAX_TOKENS+try:- resp = await client.messages.create(- model=_WEB_SEARCH_DISPATCH_MODEL,- max_tokens=_MAX_DISPATCH_TOKENS,- tools=[- {- "type": "web_search_20250305",- "name": "web_search",- "max_uses": 1,- }- ],- messages=[- {- "role": "user",- "content": (- f"Use the web_search tool exactly once with the "- f"query {query!r} and then stop. Do not "- f"summarise — the caller parses the raw "- f"tool_result."- ),- }- ],+ resp = await client.chat.completions.create(+ model=model_used,+ max_tokens=max_tokens,+ messages=[{"role": "user", "content": query}],+ extra_body=_OPENROUTER_INCLUDE_USAGE_COST,)except Exception as exc:logger.warning(- "[web_search] Anthropic call failed for query=%r: %s", query, exc+ "[web_search] OpenRouter call failed (deep=%s) for query=%r: %s",+ deep,+ query,+ exc,)return ErrorResponse(message=f"Web search failed: {exc}",@@ -138,20 +183,20 @@ class WebSearchTool(BaseTool):session_id=session_id,)- results, search_requests = _extract_results(resp, limit=max_results)+ answer = _extract_answer(resp)+ results = _extract_results(resp, limit=max_results)+ cost_usd = _extract_cost_usd(resp.usage)- cost_usd = _estimate_cost_usd(resp, search_requests=search_requests)try:- usage = getattr(resp, "usage", None)await persist_and_record_usage(session=session,user_id=user_id,- prompt_tokens=getattr(usage, "input_tokens", 0) or 0,- completion_tokens=getattr(usage, "output_tokens", 0) or 0,+ prompt_tokens=resp.usage.prompt_tokens if resp.usage else 0,+ completion_tokens=resp.usage.completion_tokens if resp.usage else 0,log_prefix="[web_search]",cost_usd=cost_usd,- model=_WEB_SEARCH_DISPATCH_MODEL,- provider="anthropic",+ model=model_used,+ provider="open_router",)except Exception as exc:logger.warning("[web_search] usage tracking failed: %s", exc)@@ -159,66 +204,92 @@ class WebSearchTool(BaseTool):return WebSearchResponse(message=f"Found {len(results)} result(s) for {query!r}.",query=query,+ answer=answer,results=results,- search_requests=search_requests,+ search_requests=1 if results else 0,session_id=session_id,)-def _extract_results(resp: Any, *, limit: int) -> tuple[list[WebSearchResult], int]:- """Pull results + server-side request count from an Anthropic response."""- results: list[WebSearchResult] = []- search_requests = 0-- for block in getattr(resp, "content", []) or []:- btype = getattr(block, "type", None)- if btype == "web_search_tool_result":- content = getattr(block, "content", []) or []- for item in content:- if getattr(item, "type", None) != "web_search_result":- continue- if len(results) >= limit:- break- # Anthropic's ``web_search_result`` exposes only- # ``title``/``url``/``page_age`` plus an opaque- # ``encrypted_content`` blob that is meant for citation- # round-tripping, not for display — it is base64-ish- # binary and would show as gibberish if surfaced to the- # model or the frontend. There is no plain-text snippet- # field in the current beta; callers get the readable- # text via the model's ``text`` blocks with citations,- # not via this list. Leave ``snippet`` empty.- results.append(- WebSearchResult(- title=getattr(item, "title", "") or "",- url=getattr(item, "url", "") or "",- snippet="",- page_age=getattr(item, "page_age", None),- )- )-- usage = getattr(resp, "usage", None)- server_tool_use = getattr(usage, "server_tool_use", None) if usage else None- if server_tool_use is not None:- search_requests = getattr(server_tool_use, "web_search_requests", 0) or 0-- return results, search_requests---# Update when Anthropic revises pricing.-_COST_PER_SEARCH_USD = 0.010 # $10 per 1,000 web_search requests-_HAIKU_INPUT_USD_PER_MTOK = 1.0-_HAIKU_OUTPUT_USD_PER_MTOK = 5.0---def _estimate_cost_usd(resp: Any, *, search_requests: int) -> float:- """Per-search fee × count + Haiku dispatch tokens."""- usage = getattr(resp, "usage", None)- input_tokens = getattr(usage, "input_tokens", 0) if usage else 0- output_tokens = getattr(usage, "output_tokens", 0) if usage else 0-- search_cost = search_requests * _COST_PER_SEARCH_USD- inference_cost = (input_tokens / 1_000_000) * _HAIKU_INPUT_USD_PER_MTOK + (- output_tokens / 1_000_000- ) * _HAIKU_OUTPUT_USD_PER_MTOK- return round(search_cost + inference_cost, 6)+def _extract_answer(resp: ChatCompletion) -> str:+ """Return the synthesised answer text from Sonar's response.++ Sonar reads every page it cites and writes a web-grounded synthesis+ into ``choices[0].message.content`` on the same call we pay for.+ Surfacing it saves the agent from re-fetching citation URLs — many+ are bot-protected and ``web_fetch`` can't reach them.+ """+ if not resp.choices:+ return ""+ content = resp.choices[0].message.content+ return content or ""+++def _extract_results(resp: ChatCompletion, *, limit: int) -> list[WebSearchResult]:+ """Pull ``url_citation`` annotations from the response.++ Shared across both tiers — OpenRouter normalises the annotation+ schema across Perplexity's sonar models into+ ``Annotation.url_citation`` (typed in ``openai.types.chat``). The+ ``content`` snippet is an OpenRouter extension on the otherwise-+ typed ``AnnotationURLCitation``; pydantic stashes unknown fields in+ ``model_extra``, which we read there rather than via ``getattr``.+ """+ if not resp.choices:+ return []+ annotations = resp.choices[0].message.annotations or []+ out: list[WebSearchResult] = []+ for ann in annotations:+ if len(out) >= limit:+ break+ if ann.type != "url_citation":+ continue+ citation = ann.url_citation+ extras = citation.model_extra or {}+ snippet_raw = extras.get("content")+ snippet = (snippet_raw or "")[:_SNIPPET_MAX_CHARS] if snippet_raw else ""+ out.append(+ WebSearchResult(+ title=citation.title,+ url=citation.url,+ snippet=snippet,+ page_age=None,+ )+ )+ return out+++def _extract_cost_usd(usage: CompletionUsage | None) -> float | None:+ """Return the provider-reported USD cost off the response usage.++ OpenRouter piggybacks a ``cost`` field on the OpenAI-compatible+ usage object when the request body includes+ ``usage: {"include": True}``. The OpenAI SDK's typed+ ``CompletionUsage`` does not declare it, so we read it off+ ``model_extra`` (the pydantic v2 container for extras) to keep+ access fully typed — no ``getattr``. Mirrors the baseline service+ ``_extract_usage_cost``; keep the two in sync.++ Returns ``None`` when the field is absent, null, non-numeric,+ non-finite, or negative. Invalid values log at error level because+ they indicate a provider bug worth chasing; plain absences are+ silent so the caller can dedupe the "missing cost" warning.+ """+ if usage is None:+ return None+ extras = usage.model_extra or {}+ if "cost" not in extras:+ return None+ raw = extras["cost"]+ if raw is None:+ logger.error("[web_search] usage.cost is present but null")+ return None+ try:+ val = float(raw)+ except (TypeError, ValueError):+ logger.error("[web_search] usage.cost is not numeric: %r", raw)+ return None+ if not math.isfinite(val) or val < 0:+ logger.error("[web_search] usage.cost is non-finite or negative: %r", val)+ return None+ return valdiff --git a/autogpt_platform/backend/backend/copilot/tools/web_search_test.py b/autogpt_platform/backend/backend/copilot/tools/web_search_test.pyindex 3d516f295a..7b341e3c44 100644--- a/autogpt_platform/backend/backend/copilot/tools/web_search_test.py+++ b/autogpt_platform/backend/backend/copilot/tools/web_search_test.py@@ -1,212 +1,289 @@"""Tests for the ``web_search`` copilot tool.-Covers the result extractor + cost estimator as pure units (fed with-synthetic Anthropic response objects), plus light integration tests that-mock ``AsyncAnthropic.messages.create`` and confirm the handler plumbs-through to ``persist_and_record_usage`` with the right provider tag.+Covers the annotation extractor + cost extractor as pure units (fed+with real ``openai`` SDK types — no duck-typed ``SimpleNamespace``+stand-ins), plus integration tests exercising both the quick+(``perplexity/sonar``) and deep (``perplexity/sonar-deep-research``)+paths — mocking ``AsyncOpenAI.chat.completions.create`` and confirming+the handler plumbs through to ``persist_and_record_usage`` with+``provider='open_router'`` and the real ``usage.cost`` value."""-from types import SimpleNamespace+from typing import Anyfrom unittest.mock import AsyncMock, patchimport pytest+from openai.types import CompletionUsage+from openai.types.chat import ChatCompletion+from openai.types.chat.chat_completion import Choice+from openai.types.chat.chat_completion_message import (+ Annotation,+ AnnotationURLCitation,+ ChatCompletionMessage,+)from backend.copilot.model import ChatSession-from .models import ErrorResponse, WebSearchResponse, WebSearchResult+from .models import ErrorResponse, WebSearchResponsefrom .web_search import (- _COST_PER_SEARCH_USD,WebSearchTool,- _estimate_cost_usd,+ _extract_answer,+ _extract_cost_usd,_extract_results,)-def _fake_anthropic_response(+def _usage(*,- results: list[dict] | None = None,- search_requests: int = 1,- input_tokens: int = 120,- output_tokens: int = 40,-) -> SimpleNamespace:- """Build a synthetic Anthropic Messages response.-- Matches the shape produced by ``client.messages.create`` when the- response includes a ``web_search_tool_result`` content block and- ``usage.server_tool_use.web_search_requests`` on the turn meter.- """- content = []- if results is not None:- content.append(- SimpleNamespace(- type="web_search_tool_result",- content=[- SimpleNamespace(- type="web_search_result",- title=r.get("title", "untitled"),- url=r.get("url", ""),- encrypted_content=r.get("snippet", ""),- page_age=r.get("page_age"),- )- for r in results- ],- )+ prompt_tokens: int = 120,+ completion_tokens: int = 40,+ cost: object = 0.01,+) -> CompletionUsage:+ """Typed ``CompletionUsage`` with OpenRouter's ``cost`` extension+ parked in ``model_extra`` — the same channel the production code+ reads it from. ``model_construct`` preserves unknown fields;+ ``model_validate`` would drop them because ``CompletionUsage``+ treats the schema as strict."""+ payload: dict[str, Any] = {+ "prompt_tokens": prompt_tokens,+ "completion_tokens": completion_tokens,+ "total_tokens": prompt_tokens + completion_tokens,+ }+ if cost is not None:+ payload["cost"] = cost+ return CompletionUsage.model_construct(None, **payload)+++def _citation(*, url: str, title: str, content: str | None = None) -> Annotation:+ """Typed ``Annotation`` for a URL citation. ``content`` is an+ OpenRouter extension on the otherwise-typed schema — goes into+ ``url_citation.model_extra`` when model_construct preserves it."""+ payload: dict[str, Any] = {+ "url": url,+ "title": title,+ "start_index": 0,+ "end_index": len(title),+ }+ if content is not None:+ payload["content"] = content+ url_citation = AnnotationURLCitation.model_construct(None, **payload)+ return Annotation(type="url_citation", url_citation=url_citation)+++def _fake_response(+ *,+ citations: list[dict] | None = None,+ answer: str = "ok",+ prompt_tokens: int = 120,+ completion_tokens: int = 40,+ cost: object = 0.01,+) -> ChatCompletion:+ """Build a typed ``ChatCompletion`` shaped like an OpenRouter+ response — typed end-to-end so the production code's attribute+ access runs under the real SDK types in tests."""+ annotations = [+ _citation(+ url=c.get("url", ""),+ title=c.get("title", "untitled"),+ content=c.get("content"),)- usage = SimpleNamespace(- input_tokens=input_tokens,- output_tokens=output_tokens,- server_tool_use=SimpleNamespace(web_search_requests=search_requests),+ for c in citations or []+ ]+ message = ChatCompletionMessage.model_construct(+ None,+ role="assistant",+ content=answer,+ annotations=annotations,+ )+ choice = Choice.model_construct(+ None,+ index=0,+ finish_reason="stop",+ message=message,+ )+ return ChatCompletion.model_construct(+ None,+ id="cmpl-test",+ object="chat.completion",+ created=0,+ model="perplexity/sonar",+ choices=[choice],+ usage=_usage(+ prompt_tokens=prompt_tokens,+ completion_tokens=completion_tokens,+ cost=cost,+ ),)- return SimpleNamespace(content=content, usage=usage)class TestExtractResults:- """The extractor is the only Anthropic-response-shape contact point;- pin its behaviour so an API shape change surfaces here first."""-- def test_extracts_title_url_page_age_and_drops_encrypted_snippet(self):- # Anthropic's ``web_search_result`` ships an opaque- # ``encrypted_content`` blob that is not safe to surface —- # the extractor must drop it (snippet=="") regardless of- # whether the blob is non-empty.- resp = _fake_anthropic_response(- results=[+ """Pin the annotation shape — a schema bump in the OpenAI SDK or+ OpenRouter surfaces here first. Same extractor serves both tiers+ because OpenRouter normalises annotations across models."""++ def test_extracts_title_url_and_content_snippet(self):+ resp = _fake_response(+ citations=[{"title": "Kimi K2.6 launch","url": "https://example.com/kimi",- "snippet": "EiJjbGF1ZGUtZW5jcnlwdGVkLWJsb2I=",- "page_age": "1 day",+ "content": "Moonshot released K2.6 on 2026-04-20.",},{"title": "OpenRouter pricing","url": "https://openrouter.ai/moonshotai/kimi-k2.6",- "snippet": "",},])- out, requests = _extract_results(resp, limit=10)- assert requests == 1+ out = _extract_results(resp, limit=10)assert len(out) == 2assert out[0].title == "Kimi K2.6 launch"assert out[0].url == "https://example.com/kimi"- assert out[0].snippet == ""- assert out[0].page_age == "1 day"+ assert out[0].snippet.startswith("Moonshot released")+ # Missing ``content`` extension → empty snippet rather than crash.assert out[1].snippet == ""def test_limit_caps_returned_results(self):- resp = _fake_anthropic_response(- results=[{"title": f"r{i}", "url": f"https://e/{i}"} for i in range(10)]+ resp = _fake_response(+ citations=[{"title": f"r{i}", "url": f"https://e/{i}"} for i in range(10)])- out, _ = _extract_results(resp, limit=3)+ out = _extract_results(resp, limit=3)assert len(out) == 3assert [r.title for r in out] == ["r0", "r1", "r2"]- def test_missing_content_returns_empty(self):- resp = SimpleNamespace(content=[], usage=None)- out, requests = _extract_results(resp, limit=10)- assert out == []- assert requests == 0-- def test_non_search_blocks_are_ignored(self):- resp = SimpleNamespace(- content=[- SimpleNamespace(type="text", text="Here's what I found..."),- SimpleNamespace(- type="web_search_tool_result",- content=[- SimpleNamespace(- type="web_search_result",- title="real",- url="https://real.example",- encrypted_content="body",- page_age=None,- )- ],- ),- ],- usage=None,+ def test_missing_choices_returns_empty(self):+ resp = ChatCompletion.model_construct(+ None,+ id="cmpl-test",+ object="chat.completion",+ created=0,+ model="perplexity/sonar",+ choices=[],+ usage=_usage(),+ )+ assert _extract_results(resp, limit=10) == []++ def test_extract_answer_returns_message_content(self):+ resp = _fake_response(+ answer="Sonar's synthesised, web-grounded answer text.",+ citations=[{"title": "t", "url": "https://e"}],+ )+ assert _extract_answer(resp) == "Sonar's synthesised, web-grounded answer text."++ def test_extract_answer_returns_empty_when_no_choices(self):+ resp = ChatCompletion.model_construct(+ None,+ id="cmpl-test",+ object="chat.completion",+ created=0,+ model="perplexity/sonar",+ choices=[],+ usage=_usage(),+ )+ assert _extract_answer(resp) == ""++ def test_snippet_clamped_to_max_chars(self):+ long_body = "x" * 5000+ resp = _fake_response(+ citations=[{"title": "t", "url": "https://e", "content": long_body}]+ )+ out = _extract_results(resp, limit=1)+ assert len(out) == 1+ assert len(out[0].snippet) == 500+++class TestExtractCostUsd:+ """Read real ``usage.cost`` via typed ``model_extra`` — no+ hard-coded rates, so a future provider price change is reflected+ automatically. Error handling mirrors the baseline service's+ ``_extract_usage_cost``."""++ def test_returns_cost_value(self):+ assert _extract_cost_usd(_usage(cost=0.023456)) == pytest.approx(0.023456)++ def test_returns_none_when_usage_missing(self):+ assert _extract_cost_usd(None) is None++ def test_returns_none_when_cost_field_missing(self):+ assert _extract_cost_usd(_usage(cost=None)) is None++ def test_returns_none_when_cost_is_explicit_null(self):+ usage = CompletionUsage.model_construct(+ None, prompt_tokens=0, completion_tokens=0, total_tokens=0, cost=None)- out, _ = _extract_results(resp, limit=10)- assert len(out) == 1 and out[0].title == "real"---class TestEstimateCostUsd:- """Pin the per-search fee + Haiku inference math — the pricing- constants in ``web_search.py`` are hard-coded (no live lookup) so a- drift between Anthropic's schedule and our constants must surface- in this test for the next reader to notice."""-- def test_zero_searches_still_charges_inference(self):- resp = _fake_anthropic_response(results=[], search_requests=0)- cost = _estimate_cost_usd(resp, search_requests=0)- # Haiku at 1000 input / 5000 output tokens = tiny but non-zero.- assert 0 < cost < 0.001-- def test_single_search_fee_dominates(self):- resp = _fake_anthropic_response(- results=[{"title": "x", "url": "https://e"}],- search_requests=1,- input_tokens=100,- output_tokens=20,+ assert _extract_cost_usd(usage) is None++ def test_returns_none_when_cost_is_negative(self):+ usage = CompletionUsage.model_construct(+ None, prompt_tokens=0, completion_tokens=0, total_tokens=0, cost=-1.0)- cost = _estimate_cost_usd(resp, search_requests=1)- # ~$0.010 search + trivial inference — total still ~1 cent.- assert cost >= _COST_PER_SEARCH_USD- assert cost < _COST_PER_SEARCH_USD + 0.001-- def test_three_searches_linear_in_count(self):- resp = _fake_anthropic_response(- results=[], search_requests=3, input_tokens=0, output_tokens=0+ assert _extract_cost_usd(usage) is None++ def test_accepts_numeric_string(self):+ usage = CompletionUsage.model_construct(+ None, prompt_tokens=0, completion_tokens=0, total_tokens=0, cost="0.017")- cost = _estimate_cost_usd(resp, search_requests=3)- assert cost == pytest.approx(3 * _COST_PER_SEARCH_USD)+ assert _extract_cost_usd(usage) == pytest.approx(0.017)class TestWebSearchToolDispatch:- """Lightweight integration test: mock the Anthropic client, confirm- the handler returns a ``WebSearchResponse`` and the usage tracker is- called with ``provider='anthropic'`` (not 'open_router', even on the- baseline path — server-side web_search bills Anthropic regardless of- the calling LLM's route)."""+ """Integration test: mock the OpenAI client, confirm both paths+ dispatch the right Sonar model + track cost."""def _session(self) -> ChatSession:s = ChatSession.new("test-user", dry_run=False)s.session_id = "sess-1"return s+ def _mock_client(self, fake_resp: ChatCompletion) -> Any:+ return type(+ "MC",+ (),+ {+ "chat": type(+ "C",+ (),+ {+ "completions": type(+ "CC",+ (),+ {"create": AsyncMock(return_value=fake_resp)},+ )()+ },+ )()+ },+ )()+@pytest.mark.asyncio- async def test_returns_response_with_results_and_tracks_cost(self, monkeypatch):- fake_resp = _fake_anthropic_response(- results=[+ async def test_quick_path_uses_sonar_base(self, monkeypatch):+ fake_resp = _fake_response(+ citations=[{"title": "hello","url": "https://example.com",- "snippet": "greeting",+ "content": "greeting",}],- search_requests=1,+ answer="Kimi K2.6 launched 2026-04-20 [1].",+ cost=0.01,)- mock_client = type(- "MC",- (),- {- "messages": type(- "M", (), {"create": AsyncMock(return_value=fake_resp)}- )()- },- )()+ mock_client = self._mock_client(fake_resp)- # Stub the Anthropic API key so ``is_available`` is True.monkeypatch.setattr(- "backend.copilot.tools.web_search.Settings",- lambda: SimpleNamespace(- secrets=SimpleNamespace(anthropic_api_key="sk-test")- ),+ "backend.copilot.tools.web_search._chat_config",+ type(+ "C",+ (),+ {+ "api_key": "sk-test",+ "base_url": "https://openrouter.ai/api/v1",+ },+ )(),)with (patch(- "backend.copilot.tools.web_search.AsyncAnthropic",+ "backend.copilot.tools.web_search.AsyncOpenAI",return_value=mock_client,),patch(@@ -220,35 +297,88 @@ class TestWebSearchToolDispatch:session=self._session(),query="kimi k2.6 launch",max_results=5,+ deep=False,)assert isinstance(result, WebSearchResponse)- assert result.query == "kimi k2.6 launch"+ assert result.answer == "Kimi K2.6 launched 2026-04-20 [1]."assert len(result.results) == 1- assert isinstance(result.results[0], WebSearchResult)- assert result.search_requests == 1+ assert result.results[0].snippet == "greeting"++ create_call = mock_client.chat.completions.create.call_args+ assert create_call.kwargs["model"] == "perplexity/sonar"+ # Sonar searches natively — no server-tool extras.+ assert create_call.kwargs["extra_body"] == {"usage": {"include": True}}- # Cost tracker must have been called with provider="anthropic".- assert mock_track.await_count == 1kwargs = mock_track.await_args.kwargs- assert kwargs["provider"] == "anthropic"- assert kwargs["model"] == "claude-haiku-4-5"- assert kwargs["user_id"] == "u1"- assert kwargs["cost_usd"] >= _COST_PER_SEARCH_USD+ assert kwargs["provider"] == "open_router"+ assert kwargs["model"] == "perplexity/sonar"+ assert kwargs["cost_usd"] == pytest.approx(0.01)@pytest.mark.asyncio- async def test_missing_api_key_returns_error_without_calling_anthropic(- self, monkeypatch- ):+ async def test_deep_path_uses_sonar_deep_research(self, monkeypatch):+ fake_resp = _fake_response(+ citations=[+ {+ "title": "deep find",+ "url": "https://example.com/deep",+ "content": "research body",+ }+ ],+ cost=0.087,+ )+ mock_client = self._mock_client(fake_resp)+monkeypatch.setattr(- "backend.copilot.tools.web_search.Settings",- lambda: SimpleNamespace(secrets=SimpleNamespace(anthropic_api_key="")),+ "backend.copilot.tools.web_search._chat_config",+ type(+ "C",+ (),+ {+ "api_key": "sk-test",+ "base_url": "https://openrouter.ai/api/v1",+ },+ )(),)- anthropic_stub = AsyncMock()+with (patch(- "backend.copilot.tools.web_search.AsyncAnthropic",- return_value=anthropic_stub,+ "backend.copilot.tools.web_search.AsyncOpenAI",+ return_value=mock_client,+ ),+ patch(+ "backend.copilot.tools.web_search.persist_and_record_usage",+ new=AsyncMock(return_value=160),+ ) as mock_track,+ ):+ tool = WebSearchTool()+ result = await tool._execute(+ user_id="u1",+ session=self._session(),+ query="research question",+ deep=True,+ )++ assert isinstance(result, WebSearchResponse)+ create_call = mock_client.chat.completions.create.call_args+ assert create_call.kwargs["model"] == "perplexity/sonar-deep-research"++ kwargs = mock_track.await_args.kwargs+ assert kwargs["provider"] == "open_router"+ assert kwargs["model"] == "perplexity/sonar-deep-research"+ assert kwargs["cost_usd"] == pytest.approx(0.087)++ @pytest.mark.asyncio+ async def test_missing_credentials_returns_error(self, monkeypatch):+ monkeypatch.setattr(+ "backend.copilot.tools.web_search._chat_config",+ type("C", (), {"api_key": "", "base_url": ""})(),+ )+ openai_stub = AsyncMock()+ with (+ patch(+ "backend.copilot.tools.web_search.AsyncOpenAI",+ return_value=openai_stub,),patch("backend.copilot.tools.web_search.persist_and_record_usage",@@ -264,21 +394,26 @@ class TestWebSearchToolDispatch:)assert isinstance(result, ErrorResponse)assert result.error == "web_search_not_configured"- anthropic_stub.messages.create.assert_not_called()+ openai_stub.chat.completions.create.assert_not_called()mock_track.assert_not_called()@pytest.mark.asyncioasync def test_empty_query_rejected_without_api_call(self, monkeypatch):monkeypatch.setattr(- "backend.copilot.tools.web_search.Settings",- lambda: SimpleNamespace(- secrets=SimpleNamespace(anthropic_api_key="sk-test")- ),+ "backend.copilot.tools.web_search._chat_config",+ type(+ "C",+ (),+ {+ "api_key": "sk-test",+ "base_url": "https://openrouter.ai/api/v1",+ },+ )(),)- anthropic_stub = AsyncMock()+ openai_stub = AsyncMock()with patch(- "backend.copilot.tools.web_search.AsyncAnthropic",- return_value=anthropic_stub,+ "backend.copilot.tools.web_search.AsyncOpenAI",+ return_value=openai_stub,):tool = WebSearchTool()result = await tool._execute(@@ -286,13 +421,13 @@ class TestWebSearchToolDispatch:)assert isinstance(result, ErrorResponse)assert result.error == "missing_query"- anthropic_stub.messages.create.assert_not_called()+ openai_stub.chat.completions.create.assert_not_called()class TestToolRegistryIntegration:"""The tool must be registered under the ``web_search`` name so theMCP layer exposes it as ``mcp__copilot__web_search`` — which is- what the SDK path now dispatches to (see+ what the SDK path dispatches to (see``sdk/tool_adapter.py::SDK_DISALLOWED_TOOLS`` which blocks the CLI'snative ``WebSearch`` in favour of the MCP route)."""diff --git a/autogpt_platform/backend/backend/executor/manager.py b/autogpt_platform/backend/backend/executor/manager.pyindex 87ee3cbc44..0cf0ea0936 100644--- a/autogpt_platform/backend/backend/executor/manager.py+++ b/autogpt_platform/backend/backend/executor/manager.py@@ -366,7 +366,7 @@ async def execute_node(try:if execution_context.dry_run and _dry_run_input is None:- block_iter = simulate_block(node_block, input_data)+ block_iter = simulate_block(node_block, input_data, user_id=user_id)else:block_iter = node_block.execute(input_data, **extra_exec_kwargs)diff --git a/autogpt_platform/backend/backend/executor/simulator.py b/autogpt_platform/backend/backend/executor/simulator.pyindex 7d514fb2b9..5d4770a46c 100644--- a/autogpt_platform/backend/backend/executor/simulator.py+++ b/autogpt_platform/backend/backend/executor/simulator.py@@ -31,21 +31,31 @@ Inspired by https://github.com/Significant-Gravitas/agent-simulatorimport inspectimport jsonimport logging+import mathfrom collections.abc import AsyncGeneratorfrom typing import Any+from openai.types import CompletionUsage+from backend.blocks.agent import AgentExecutorBlockfrom backend.blocks.io import AgentInputBlock, AgentOutputBlockfrom backend.blocks.orchestrator import OrchestratorBlock+from backend.copilot.token_tracking import persist_and_record_usagefrom backend.util.clients import get_openai_clientlogger = logging.getLogger(__name__)-# Default simulator model — Gemini 2.5 Flash via OpenRouter (fast, cheap, good at-# JSON generation). Configurable via ChatConfig.simulation_model-# (CHAT_SIMULATION_MODEL env var).-_DEFAULT_SIMULATOR_MODEL = "google/gemini-2.5-flash"+# Default simulator model — Gemini 2.5 Flash-Lite via OpenRouter. Same provider+# as Flash ($0.10 / $0.40 per MTok vs $0.30 / $1.20 — ~3× cheaper) with JSON-mode+# reliability that's more than enough for dry-run shape-matching. Configurable+# via ChatConfig.simulation_model (CHAT_SIMULATION_MODEL env var).+_DEFAULT_SIMULATOR_MODEL = "google/gemini-2.5-flash-lite"++# OpenRouter-specific extra_body flag that embeds the real generation cost on+# the response usage object. Same shape used by the baseline copilot service+# and web_search tool — keep the three aligned.+_OPENROUTER_INCLUDE_USAGE_COST: dict[str, Any] = {"usage": {"include": True}}def _simulator_model() -> str:@@ -105,10 +115,15 @@ async def _call_llm_for_simulation(user_prompt: str,*,label: str = "simulate",+ user_id: str | None = None,) -> dict[str, Any]:"""Send a simulation prompt to the LLM and return the parsed JSON dict.- Handles client acquisition, retries on invalid JSON, and logging.+ Handles client acquisition, retries on invalid JSON, logging, and platform+ cost tracking. The dry-run simulator calls OpenRouter on the platform's+ key rather than a user's own API credentials, so every successful call is+ recorded against the triggering ``user_id``'s rate-limit counter via+ ``persist_and_record_usage`` (same rails as every copilot turn).Raises:RuntimeError: If no LLM client is available.@@ -133,6 +148,7 @@ async def _call_llm_for_simulation({"role": "system", "content": system_prompt},{"role": "user", "content": user_prompt},],+ extra_body=_OPENROUTER_INCLUDE_USAGE_COST,)if not response.choices:raise ValueError("LLM returned empty choices array")@@ -141,13 +157,21 @@ async def _call_llm_for_simulation(if not isinstance(parsed, dict):raise ValueError(f"LLM returned non-object JSON: {raw[:200]}")- logger.debug(- "simulate(%s): attempt=%d tokens=%s/%s",- label,- attempt + 1,- getattr(getattr(response, "usage", None), "prompt_tokens", "?"),- getattr(getattr(response, "usage", None), "completion_tokens", "?"),- )+ usage = response.usage+ if usage is not None:+ logger.debug(+ "simulate(%s): attempt=%d tokens=%d/%d",+ label,+ attempt + 1,+ usage.prompt_tokens,+ usage.completion_tokens,+ )+ else:+ logger.debug(+ "simulate(%s): attempt=%d usage unavailable", label, attempt + 1+ )++ await _track_simulator_cost(usage=usage, user_id=user_id, model=model)return parsedexcept (json.JSONDecodeError, ValueError) as e:@@ -174,6 +198,69 @@ async def _call_llm_for_simulation(raise ValueError(msg)+def _extract_cost_usd(usage: CompletionUsage | None) -> float | None:+ """Return the provider-reported USD cost on the response usage object.++ OpenRouter attaches a ``cost`` field to the OpenAI-compatible usage object+ when the request body includes ``usage: {"include": True}``. The typed+ ``CompletionUsage`` does not declare it, so we read it off ``model_extra``+ (pydantic v2's container for extras) to keep access fully typed — no+ ``getattr``. Mirrors ``backend.copilot.tools.web_search._extract_cost_usd``+ and ``backend.copilot.baseline.service._extract_usage_cost``; keep the+ three in sync.+ """+ if usage is None:+ return None+ extras = usage.model_extra or {}+ if "cost" not in extras:+ return None+ raw = extras["cost"]+ if raw is None:+ logger.error("[simulator] usage.cost is present but null")+ return None+ try:+ val = float(raw)+ except (TypeError, ValueError):+ logger.error("[simulator] usage.cost is not numeric: %r", raw)+ return None+ if not math.isfinite(val) or val < 0:+ logger.error("[simulator] usage.cost is non-finite or negative: %r", val)+ return None+ return val+++async def _track_simulator_cost(+ *,+ usage: CompletionUsage | None,+ user_id: str | None,+ model: str,+) -> None:+ """Record platform cost for a single simulator LLM call.++ The simulator runs outside a copilot ``ChatSession`` — pass ``session=None``+ so ``persist_and_record_usage`` skips the session append but still charges+ the user's rate-limit counter and writes a ``PlatformCostLog`` entry. No+ user_id means no tracking (e.g. in-process tests that don't plumb one+ through); rate-limit accounting silently no-ops in that case.+ """+ if usage is None:+ return+ cost_usd = _extract_cost_usd(usage)+ try:+ await persist_and_record_usage(+ session=None,+ user_id=user_id,+ prompt_tokens=usage.prompt_tokens,+ completion_tokens=usage.completion_tokens,+ log_prefix="[simulator]",+ cost_usd=cost_usd,+ model=model,+ provider="open_router",+ )+ except Exception as exc:+ logger.warning("[simulator] usage tracking failed: %s", exc)++# ---------------------------------------------------------------------------# Prompt builders# ---------------------------------------------------------------------------@@ -393,12 +480,18 @@ def _default_for_input_result(result_schema: dict[str, Any], name: str | None) -async def simulate_block(block: Any,input_data: dict[str, Any],+ *,+ user_id: str | None = None,) -> AsyncGenerator[tuple[str, Any], None]:"""Simulate block execution using an LLM.All block types (including MCPToolBlock) use the same generic LLM promptwhich includes the block's run() source code for accurate simulation.+ ``user_id`` is threaded through to platform cost tracking — every dry-run+ LLM call hits the platform's OpenRouter key and is charged against the+ triggering user's rate-limit counter, same rails as copilot turns.+Note: callers should check ``prepare_dry_run(block, input_data)`` first.OrchestratorBlock and AgentExecutorBlock execute for real in dry-run mode(see manager.py).@@ -462,7 +555,9 @@ async def simulate_block(label = getattr(block, "name", "?")try:- parsed = await _call_llm_for_simulation(system_prompt, user_prompt, label=label)+ parsed = await _call_llm_for_simulation(+ system_prompt, user_prompt, label=label, user_id=user_id+ )# Track which pins were yielded so we can fill in missing required# ones afterwards — downstream nodes connected to unyielded pinsdiff --git a/autogpt_platform/backend/backend/executor/simulator_test.py b/autogpt_platform/backend/backend/executor/simulator_test.pyindex 8590d9bdbf..d331f1ebc1 100644--- a/autogpt_platform/backend/backend/executor/simulator_test.py+++ b/autogpt_platform/backend/backend/executor/simulator_test.py@@ -5,6 +5,7 @@ Covers:- Input/output block passthrough- prepare_dry_run routing- simulate_block output-pin filling+ - Default simulator model + OpenRouter cost tracking"""from __future__ import annotations@@ -13,8 +14,14 @@ from typing import Anyfrom unittest.mock import AsyncMock, MagicMock, patchimport pytest+from openai.types import CompletionUsage+from openai.types.chat import ChatCompletion+from openai.types.chat.chat_completion import Choice+from openai.types.chat.chat_completion_message import ChatCompletionMessagefrom backend.executor.simulator import (+ _DEFAULT_SIMULATOR_MODEL,+ _extract_cost_usd,_truncate_input_values,_truncate_value,build_simulation_prompt,@@ -511,3 +518,217 @@ class TestSimulateBlockPassthrough:assert len(outputs) == 1assert outputs[0][0] == "error"assert "No client" in outputs[0][1]+++# ---------------------------------------------------------------------------+# Default model + OpenRouter cost tracking+# ---------------------------------------------------------------------------+++def _sim_usage(+ *,+ prompt_tokens: int = 1200,+ completion_tokens: int = 300,+ cost: object = 0.000157,+) -> CompletionUsage:+ """Typed ``CompletionUsage`` carrying OpenRouter's ``cost`` extension+ via ``model_extra`` — same pattern as+ ``copilot/tools/web_search_test.py::_usage``. ``model_construct``+ preserves unknown fields; ``model_validate`` would drop them."""+ payload: dict[str, Any] = {+ "prompt_tokens": prompt_tokens,+ "completion_tokens": completion_tokens,+ "total_tokens": prompt_tokens + completion_tokens,+ }+ if cost is not None:+ payload["cost"] = cost+ return CompletionUsage.model_construct(None, **payload)+++def _sim_completion(*, content: str, usage: CompletionUsage) -> ChatCompletion:+ """Typed ``ChatCompletion`` shaped like an OpenRouter simulator+ response so the production code runs under real SDK types."""+ message = ChatCompletionMessage.model_construct(+ None, role="assistant", content=content+ )+ choice = Choice.model_construct(+ None, index=0, finish_reason="stop", message=message+ )+ return ChatCompletion.model_construct(+ None,+ id="cmpl-sim",+ object="chat.completion",+ created=0,+ model=_DEFAULT_SIMULATOR_MODEL,+ choices=[choice],+ usage=usage,+ )+++class TestDefaultSimulatorModel:+ """Pin the default model — anyone flipping this without a cost review+ trips the test."""++ def test_default_is_flash_lite(self) -> None:+ assert _DEFAULT_SIMULATOR_MODEL == "google/gemini-2.5-flash-lite"+++class TestExtractCostUsd:+ """Provider-reported USD cost via typed ``model_extra`` — mirrors+ ``copilot.tools.web_search._extract_cost_usd`` and+ ``copilot.baseline.service._extract_usage_cost``."""++ def test_returns_cost_value(self) -> None:+ assert _extract_cost_usd(_sim_usage(cost=0.000157)) == pytest.approx(0.000157)++ def test_returns_none_when_usage_missing(self) -> None:+ assert _extract_cost_usd(None) is None++ def test_returns_none_when_cost_field_missing(self) -> None:+ assert _extract_cost_usd(_sim_usage(cost=None)) is None++ def test_returns_none_when_cost_is_explicit_null(self) -> None:+ usage = CompletionUsage.model_construct(+ None, prompt_tokens=0, completion_tokens=0, total_tokens=0, cost=None+ )+ assert _extract_cost_usd(usage) is None++ def test_returns_none_when_cost_is_negative(self) -> None:+ usage = CompletionUsage.model_construct(+ None, prompt_tokens=0, completion_tokens=0, total_tokens=0, cost=-0.5+ )+ assert _extract_cost_usd(usage) is None++ def test_accepts_numeric_string(self) -> None:+ usage = CompletionUsage.model_construct(+ None, prompt_tokens=0, completion_tokens=0, total_tokens=0, cost="0.017"+ )+ assert _extract_cost_usd(usage) == pytest.approx(0.017)+++class TestSimulatorCostTracking:+ """Integration: mock the OpenAI client, confirm the simulator sends+ the flash-lite default + extra_body, then plumbs through to+ ``persist_and_record_usage`` with ``provider='open_router'`` and the+ real ``usage.cost`` pulled off ``model_extra``."""++ def _mock_client(self, fake_resp: ChatCompletion) -> tuple[Any, AsyncMock]:+ """Build a fake ``AsyncOpenAI`` client. Same nested-type pattern as+ ``copilot/tools/web_search_test.py::_mock_client`` — avoids+ MagicMock's auto-child-attr behaviour so the exact ``create`` call+ surface is what gets invoked."""+ create_mock = AsyncMock(return_value=fake_resp)+ client = type(+ "MC",+ (),+ {+ "chat": type(+ "C",+ (),+ {"completions": type("CC", (), {"create": create_mock})()},+ )()+ },+ )()+ return client, create_mock++ @pytest.mark.asyncio+ async def test_passes_default_model_and_tracks_cost(self) -> None:+ block = _make_block()+ fake_resp = _sim_completion(+ content='{"result": "simulated"}',+ usage=_sim_usage(prompt_tokens=1100, completion_tokens=220, cost=0.000189),+ )+ client, create_mock = self._mock_client(fake_resp)++ with (+ patch(+ "backend.executor.simulator.get_openai_client",+ return_value=client,+ ),+ patch(+ "backend.executor.simulator.persist_and_record_usage",+ new=AsyncMock(return_value=1320),+ ) as mock_track,+ ):+ outputs = []+ async for name, data in simulate_block(+ block, {"query": "hello"}, user_id="user-42"+ ):+ outputs.append((name, data))++ assert ("result", "simulated") in outputs++ create_kwargs = create_mock.await_args.kwargs+ assert create_kwargs["model"] == _DEFAULT_SIMULATOR_MODEL+ assert create_kwargs["extra_body"] == {"usage": {"include": True}}++ track_kwargs = mock_track.await_args.kwargs+ assert track_kwargs["provider"] == "open_router"+ assert track_kwargs["model"] == _DEFAULT_SIMULATOR_MODEL+ assert track_kwargs["user_id"] == "user-42"+ assert track_kwargs["prompt_tokens"] == 1100+ assert track_kwargs["completion_tokens"] == 220+ assert track_kwargs["cost_usd"] == pytest.approx(0.000189)+ assert track_kwargs["session"] is None+ assert track_kwargs["log_prefix"] == "[simulator]"++ @pytest.mark.asyncio+ async def test_tracks_even_when_cost_absent(self) -> None:+ """Provider may omit ``cost`` (e.g. non-OpenRouter proxies). We+ still record token counts — ``persist_and_record_usage`` logs the+ turn and skips the rate-limit ledger when cost is ``None``."""+ block = _make_block()+ fake_resp = _sim_completion(+ content='{"result": "ok"}',+ usage=_sim_usage(prompt_tokens=100, completion_tokens=20, cost=None),+ )+ client, _ = self._mock_client(fake_resp)++ with (+ patch(+ "backend.executor.simulator.get_openai_client",+ return_value=client,+ ),+ patch(+ "backend.executor.simulator.persist_and_record_usage",+ new=AsyncMock(return_value=120),+ ) as mock_track,+ ):+ async for _name, _data in simulate_block(+ block, {"query": "x"}, user_id="user-7"+ ):+ pass++ track_kwargs = mock_track.await_args.kwargs+ assert track_kwargs["cost_usd"] is None+ assert track_kwargs["user_id"] == "user-7"+ assert track_kwargs["provider"] == "open_router"++ @pytest.mark.asyncio+ async def test_tracking_failure_does_not_break_simulation(self) -> None:+ """Cost-tracking failures are warnings, not simulation failures —+ the block output must still flow to the caller."""+ block = _make_block()+ fake_resp = _sim_completion(+ content='{"result": "simulated"}',+ usage=_sim_usage(),+ )+ client, _ = self._mock_client(fake_resp)++ with (+ patch(+ "backend.executor.simulator.get_openai_client",+ return_value=client,+ ),+ patch(+ "backend.executor.simulator.persist_and_record_usage",+ new=AsyncMock(side_effect=RuntimeError("redis down")),+ ),+ ):+ outputs = []+ async for name, data in simulate_block(+ block, {"query": "hello"}, user_id="user-42"+ ):+ outputs.append((name, data))++ assert ("result", "simulated") in outputsdiff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotStream.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotStream.test.tsnew file mode 100644index 0000000000..e56317bf04--- /dev/null+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/__tests__/useCopilotStream.test.ts@@ -0,0 +1,177 @@+import { act, renderHook } from "@testing-library/react";+import type { UIMessage } from "ai";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import { useCopilotStream } from "../useCopilotStream";++// Capture the args passed to ``useChat`` so tests can invoke onFinish/onError+// directly — that's the only way to drive handleReconnect without a real SSE.+let lastUseChatArgs: {+ onFinish?: (args: { isDisconnect?: boolean; isAbort?: boolean }) => void;+ onError?: (err: Error) => void;+} | null = null;++const resumeStreamMock = vi.fn();+const sdkStopMock = vi.fn();+const sdkSendMessageMock = vi.fn();+const setMessagesMock = vi.fn();++function resetSdkMocks() {+ lastUseChatArgs = null;+ resumeStreamMock.mockReset();+ sdkStopMock.mockReset();+ sdkSendMessageMock.mockReset();+ setMessagesMock.mockReset();+}++vi.mock("@ai-sdk/react", () => ({+ useChat: (args: unknown) => {+ lastUseChatArgs = args as typeof lastUseChatArgs;+ return {+ messages: [] as UIMessage[],+ sendMessage: sdkSendMessageMock,+ stop: sdkStopMock,+ status: "ready" as const,+ error: undefined,+ setMessages: setMessagesMock,+ resumeStream: resumeStreamMock,+ };+ },+}));++vi.mock("ai", async () => {+ const actual = await vi.importActual<typeof import("ai")>("ai");+ return {+ ...actual,+ DefaultChatTransport: class {+ constructor(public opts: unknown) {}+ },+ };+});++vi.mock("@tanstack/react-query", () => ({+ useQueryClient: () => ({ invalidateQueries: vi.fn() }),+}));++vi.mock("@/app/api/__generated__/endpoints/chat/chat", () => ({+ getGetV2GetCopilotUsageQueryKey: () => ["copilot-usage"],+ getGetV2GetSessionQueryKey: (id: string) => ["session", id],+ postV2CancelSessionTask: vi.fn(),+ deleteV2DisconnectSessionStream: vi.fn().mockResolvedValue(undefined),+}));++vi.mock("@/components/molecules/Toast/use-toast", () => ({+ toast: vi.fn(),+}));++vi.mock("@/services/environment", () => ({+ environment: {+ getAGPTServerBaseUrl: () => "http://localhost",+ },+}));++vi.mock("../helpers", async () => {+ const actual =+ await vi.importActual<typeof import("../helpers")>("../helpers");+ return {+ ...actual,+ getCopilotAuthHeaders: vi.fn().mockResolvedValue({}),+ disconnectSessionStream: vi.fn(),+ };+});++vi.mock("../useHydrateOnStreamEnd", () => ({+ useHydrateOnStreamEnd: () => undefined,+}));++function renderStream() {+ return renderHook(() =>+ useCopilotStream({+ sessionId: "sess-1",+ hydratedMessages: [],+ hasActiveStream: false,+ refetchSession: vi.fn().mockResolvedValue({ data: undefined }),+ copilotMode: undefined,+ copilotModel: undefined,+ }),+ );+}++describe("useCopilotStream — reconnect debounce", () => {+ beforeEach(() => {+ resetSdkMocks();+ vi.useFakeTimers();+ // Pin Date.now so sinceLastResume math is deterministic. The hook reads+ // Date.now() both when stashing lastReconnectResumeAtRef and when+ // deciding whether to debounce.+ vi.setSystemTime(new Date(2025, 0, 1, 12, 0, 0));+ });++ afterEach(() => {+ vi.useRealTimers();+ });++ it("coalesces a burst of disconnect events into one resumeStream call", async () => {+ renderStream();++ // First disconnect — schedules a reconnect at the exponential backoff+ // delay (1000ms for attempt #1).+ await act(async () => {+ await lastUseChatArgs!.onFinish!({ isDisconnect: true });+ });++ // Fire the scheduled timer → resumeStream runs once and stamps+ // lastReconnectResumeAtRef.current = Date.now().+ await act(async () => {+ await vi.advanceTimersByTimeAsync(1_000);+ });+ expect(resumeStreamMock).toHaveBeenCalledTimes(1);++ // A second disconnect arrives immediately after (still inside the+ // 1500ms debounce window) — the debounce path must fire and queue a+ // coalesced timer, NOT a fresh resume.+ await act(async () => {+ await lastUseChatArgs!.onFinish!({ isDisconnect: true });+ });+ expect(resumeStreamMock).toHaveBeenCalledTimes(1);++ // The coalesced timer fires at the window boundary and reschedules a+ // real reconnect. Advance past the window AND past the second+ // reconnect's backoff (attempt #2 = 2000ms) so resume runs.+ await act(async () => {+ await vi.advanceTimersByTimeAsync(1_500);+ });+ await act(async () => {+ await vi.advanceTimersByTimeAsync(2_000);+ });+ expect(resumeStreamMock).toHaveBeenCalledTimes(2);+ });++ it("does not debounce a reconnect that arrives after the window closes", async () => {+ renderStream();++ // First reconnect cycle.+ await act(async () => {+ await lastUseChatArgs!.onFinish!({ isDisconnect: true });+ });+ await act(async () => {+ await vi.advanceTimersByTimeAsync(1_000);+ });+ expect(resumeStreamMock).toHaveBeenCalledTimes(1);++ // Wait past the debounce window before the next disconnect.+ await act(async () => {+ await vi.advanceTimersByTimeAsync(2_000);+ });++ // Now a fresh disconnect should go through the normal path (NOT the+ // debounce branch) and schedule a backoff of 2000ms (attempt #2).+ await act(async () => {+ await lastUseChatArgs!.onFinish!({ isDisconnect: true });+ });+ await act(async () => {+ await vi.advanceTimersByTimeAsync(2_000);+ });+ expect(resumeStreamMock).toHaveBeenCalledTimes(2);+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.test.tsindex 91e09efde3..7c61390f1f 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.test.ts@@ -7,6 +7,7 @@ import {formatNotificationTitle,getSendSuppressionReason,parseSessionIDs,+ shouldDebounceReconnect,shouldSuppressDuplicateSend,} from "./helpers";@@ -466,3 +467,88 @@ describe("deduplicateMessages", () => {expect(result).toHaveLength(2); // duplicate step-start messages are deduped});});++describe("shouldDebounceReconnect", () => {+ const WINDOW_MS = 1_500;++ it("returns null for the first reconnect (lastResumeAt === 0)", () => {+ expect(shouldDebounceReconnect(0, 10_000, WINDOW_MS)).toBeNull();+ });++ it("returns null for a negative lastResumeAt sentinel", () => {+ // Defensive: a negative value is still treated as "no reconnect yet".+ expect(shouldDebounceReconnect(-1, 10_000, WINDOW_MS)).toBeNull();+ });++ it("returns the remaining delay when now is inside the window", () => {+ // 500ms since the last resume — the caller must wait another 1000ms+ // before the storm cap reopens.+ const remaining = shouldDebounceReconnect(1_000, 1_500, WINDOW_MS);+ expect(remaining).toBe(1_000);+ });++ it("coalesces a reconnect that arrives immediately after the previous resume", () => {+ // now === lastResumeAt → sinceLastResume === 0, so the full window remains.+ const remaining = shouldDebounceReconnect(5_000, 5_000, WINDOW_MS);+ expect(remaining).toBe(WINDOW_MS);+ });++ it("returns null when exactly on the window boundary", () => {+ // sinceLastResume === windowMs is NOT inside the window — the next+ // reconnect should fire immediately.+ expect(shouldDebounceReconnect(1_000, 2_500, WINDOW_MS)).toBeNull();+ });++ it("returns null when the window has elapsed", () => {+ expect(shouldDebounceReconnect(1_000, 5_000, WINDOW_MS)).toBeNull();+ });++ it("returns a small remaining delay at the far edge of the window", () => {+ // 1ms before the window closes → 1ms left.+ const remaining = shouldDebounceReconnect(1_000, 2_499, WINDOW_MS);+ expect(remaining).toBe(1);+ });++ it("collapses a burst of reconnects into one debounced scheduling", () => {+ // Simulates the browser tab-throttle storm: three reconnect calls fire+ // within a single second after the last resume. Only the first slot+ // would actually run; subsequent calls must always be coalesced.+ const lastResumeAt = 10_000;+ const firstCallRemaining = shouldDebounceReconnect(+ lastResumeAt,+ 10_100,+ WINDOW_MS,+ );+ const secondCallRemaining = shouldDebounceReconnect(+ lastResumeAt,+ 10_200,+ WINDOW_MS,+ );+ const thirdCallRemaining = shouldDebounceReconnect(+ lastResumeAt,+ 10_300,+ WINDOW_MS,+ );+ expect(firstCallRemaining).toBe(1_400);+ expect(secondCallRemaining).toBe(1_300);+ expect(thirdCallRemaining).toBe(1_200);+ });++ it("allows a reconnect to fire immediately once the window has passed", () => {+ // After the window expires, a retry that came in earlier can now fire+ // rather than stalling the loop. Guards against the regression that+ // motivated the coalesce-instead-of-drop fix.+ const lastResumeAt = 10_000;+ expect(+ shouldDebounceReconnect(lastResumeAt, 10_500, WINDOW_MS),+ ).not.toBeNull();+ expect(shouldDebounceReconnect(lastResumeAt, 11_500, WINDOW_MS)).toBeNull();+ });++ it("honours a custom windowMs value", () => {+ // Shouldn't hard-code 1500 anywhere: the helper is generic over the+ // window.+ expect(shouldDebounceReconnect(1_000, 1_500, 2_000)).toBe(1_500);+ expect(shouldDebounceReconnect(1_000, 3_500, 2_000)).toBeNull();+ });+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.tsindex b1d87a25d2..131a721117 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/helpers.ts@@ -184,6 +184,28 @@ export function disconnectSessionStream(sessionId: string): void {deleteV2DisconnectSessionStream(sessionId).catch(() => {});}+/**+ * Decide whether a reconnect request must be coalesced onto the debounce+ * window boundary, rather than firing immediately.+ *+ * Returns the remaining milliseconds until the window closes (so the caller+ * can schedule a `setTimeout` for that delay) when the previous resume+ * happened inside the window, or `null` to let the reconnect proceed now.+ *+ * `lastResumeAt === 0` signals "no reconnect has fired yet in this session"+ * — the first reconnect always passes through regardless of `now`.+ */+export function shouldDebounceReconnect(+ lastResumeAt: number,+ now: number,+ windowMs: number,+): number | null {+ if (lastResumeAt <= 0) return null;+ const sinceLastResume = now - lastResumeAt;+ if (sinceLastResume >= windowMs) return null;+ return windowMs - sinceLastResume;+}+/*** Deduplicate messages by ID and by consecutive content fingerprint.*diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsxindex 74aa3153d5..694571de7a 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/GenericTool.tsx@@ -313,11 +313,19 @@ function getWebAccordionData(: null;if (results) {+ const deep = inp.deep === true;+ const noun = deep ? "research source" : "search result";+ const answer = getStringField(output, "answer");return {- title: `${results.length} search result${results.length === 1 ? "" : "s"}`,+ title: `${results.length} ${noun}${results.length === 1 ? "" : "s"}`,description: query ? truncate(query, 80) : undefined,content: (<div className="space-y-3">+ {answer && (+ <div className="whitespace-pre-wrap rounded-md bg-slate-50 p-3 text-sm text-slate-800">+ {answer}+ </div>+ )}{results.map((r, i) => {const title = getStringField(r, "title") ?? "(untitled)";const href = getStringField(r, "url") ?? "";diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/GenericTool.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/GenericTool.test.tsxindex 48e0409393..61339eeac2 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/GenericTool.test.tsx+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/__tests__/GenericTool.test.tsx@@ -141,6 +141,7 @@ describe("GenericTool", () => {function makeWebSearchPart(results: Array<Record<string, unknown>>,query = "kimi k2.6",+ answer = "",): ToolUIPart {return {type: "tool-web_search",@@ -149,6 +150,7 @@ describe("GenericTool", () => {input: { query },output: {type: "web_search_response",+ answer,results,query,search_requests: 1,@@ -254,6 +256,25 @@ describe("GenericTool", () => {expect(normalized).toContain('Searched "kimi k2.6"');});+ it("renders the synthesised answer above the citations when present", () => {+ render(+ <GenericTool+ part={makeWebSearchPart(+ [+ { title: "Citation 1", url: "https://example.com/one" },+ { title: "Citation 2", url: "https://example.com/two" },+ ],+ "kimi k2.6 launch",+ "Kimi K2.6 launched on 2026-04-20 with SWE-Bench parity to Opus.",+ )}+ />,+ );+ fireEvent.click(screen.getByRole("button", { expanded: false }));+ expect(+ screen.getByText(/Kimi K2\.6 launched on 2026-04-20/),+ ).not.toBeNull();+ });+it("uses '(untitled)' when a search result has no title", () => {render(<GenericTooldiff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/helpers.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/helpers.tsindex e1103e1435..23e9b43192 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/helpers.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/tools/GenericTool/helpers.ts@@ -205,6 +205,14 @@ export function humanizeFileName(filePath: string): string {/* Animation text *//* ------------------------------------------------------------------ */+// web_search accepts a ``deep`` arg that dispatches to a multi-step+// research model; render a distinct verb ("Researching"/"Researched"/+// "Research failed") so users know the call takes longer.+function _isDeepWebSearch(part: ToolUIPart): boolean {+ const input = part.input as Record<string, unknown> | undefined;+ return input?.deep === true;+}+export function getAnimationText(part: ToolUIPart,category: ToolCategory,@@ -223,9 +231,11 @@ export function getAnimationText(: "Running command\u2026";case "web":if (toolName === "WebSearch" || toolName === "web_search") {+ const deep = _isDeepWebSearch(part);+ const verb = deep ? "Researching" : "Searching";return shortSummary- ? `Searching "${shortSummary}"`- : "Searching the web\u2026";+ ? `${verb} "${shortSummary}"`+ : `${verb} the web\u2026`;}return shortSummary? `Fetching ${shortSummary}`@@ -285,9 +295,12 @@ export function getAnimationText(return shortSummary ? `Ran: ${shortSummary}` : "Command completed";case "web":if (toolName === "WebSearch" || toolName === "web_search") {- return shortSummary- ? `Searched "${shortSummary}"`+ const deep = _isDeepWebSearch(part);+ const verb = deep ? "Researched" : "Searched";+ const completed = deep+ ? "Web research completed": "Web search completed";+ return shortSummary ? `${verb} "${shortSummary}"` : completed;}return shortSummary? `Fetched ${shortSummary}`@@ -354,9 +367,10 @@ export function getAnimationText(case "bash":return "Command failed";case "web":- return toolName === "WebSearch" || toolName === "web_search"- ? "Search failed"- : "Fetch failed";+ if (toolName === "WebSearch" || toolName === "web_search") {+ return _isDeepWebSearch(part) ? "Research failed" : "Search failed";+ }+ return "Fetch failed";case "browser":return "Browser action failed";default:diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.tsindex 2412ff5988..afef20c85a 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/useCopilotStream.ts@@ -18,6 +18,7 @@ import {resolveInProgressTools,getSendSuppressionReason,disconnectSessionStream,+ shouldDebounceReconnect,} from "./helpers";import type { CopilotLlmModel, CopilotMode } from "./store";import { useHydrateOnStreamEnd } from "./useHydrateOnStreamEnd";@@ -25,6 +26,19 @@ import { useHydrateOnStreamEnd } from "./useHydrateOnStreamEnd";const RECONNECT_BASE_DELAY_MS = 1_000;const RECONNECT_MAX_ATTEMPTS = 3;+/**+ * Minimum spacing between successive reconnect attempts.+ * `isReconnectScheduledRef` already prevents OVERLAPPING reconnects, but+ * tab-throttle / visibility wake bursts can fire `onFinish(isDisconnect)`+ * several times inside a single second — each one would schedule a fresh+ * reconnect the moment the previous timer cleared the ref. Requests that+ * arrive inside this window since the last reconnect's resume are COALESCED:+ * scheduled to run at the window boundary rather than dropped, so a+ * fast-failing resume (e.g. a 502 on GET /stream that trips `onError` inside+ * 500 ms) still retries instead of stalling the retry loop.+ */+const RECONNECT_DEBOUNCE_MS = 1_500;+/** Minimum time the page must have been hidden to trigger a wake re-sync. */const WAKE_RESYNC_THRESHOLD_MS = 30_000;@@ -110,6 +124,11 @@ export function useCopilotStream({const isReconnectScheduledRef = useRef(false);const [isReconnectScheduled, setIsReconnectScheduled] = useState(false);const reconnectTimerRef = useRef<ReturnType<typeof setTimeout>>();+ // Timestamp of the last reconnect's actual resume call — used together+ // with RECONNECT_DEBOUNCE_MS to drop rapid duplicate reconnect requests+ // (e.g. visibility throttle firing onFinish(isDisconnect) several times+ // in the same second). 0 = no reconnect has fired yet in this session.+ const lastReconnectResumeAtRef = useRef(0);const hasShownDisconnectToast = useRef(false);// Set when the user explicitly clicks stop — prevents onError from// triggering a reconnect cycle for the resulting AbortError.@@ -127,6 +146,32 @@ export function useCopilotStream({function handleReconnect(sid: string) {if (isReconnectScheduledRef.current || !sid) return;+ // Debounce: if the previous reconnect resumed within the last+ // RECONNECT_DEBOUNCE_MS, COALESCE this request onto the window boundary+ // rather than dropping it. Browser tab-throttle bursts can fire+ // onFinish(isDisconnect) 2–3 times in a second; without the debounce,+ // each fires its own GET /stream, each one replays the Redis stream,+ // and the flicker storm is back. Dropping the request silently (the+ // previous behaviour) stalled the retry loop when a resume failed+ // quickly — e.g. a 502 on GET /stream that trips onError inside 500 ms+ // while the 1500 ms window is still open. Scheduling the retry for+ // the remaining window preserves both the storm cap and the retry.+ const remainingDelay = shouldDebounceReconnect(+ lastReconnectResumeAtRef.current,+ Date.now(),+ RECONNECT_DEBOUNCE_MS,+ );+ if (remainingDelay !== null) {+ isReconnectScheduledRef.current = true;+ setIsReconnectScheduled(true);+ reconnectTimerRef.current = setTimeout(() => {+ isReconnectScheduledRef.current = false;+ setIsReconnectScheduled(false);+ handleReconnect(sid);+ }, remainingDelay);+ return;+ }+const nextAttempt = reconnectAttemptsRef.current + 1;if (nextAttempt > RECONNECT_MAX_ATTEMPTS) {setReconnectExhausted(true);@@ -163,6 +208,7 @@ export function useCopilotStream({}return prev;});+ lastReconnectResumeAtRef.current = Date.now();resumeStreamRef.current();}, delay);}@@ -469,6 +515,7 @@ export function useCopilotStream({setRateLimitMessage(null);hasShownDisconnectToast.current = false;lastSubmittedMsgRef.current = null;+ lastReconnectResumeAtRef.current = 0;setReconnectExhausted(false);setIsSyncing(false);hasResumedRef.current.clear();