AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(backend/copilot): mandate find_block before refusal + suppress false empty-completion on retry (#13086)
Zamil Majdy8d9ecb8197c3
diff --git a/autogpt_platform/backend/backend/copilot/prompting.py b/autogpt_platform/backend/backend/copilot/prompting.pyindex b517d7e329..909091d91b 100644--- a/autogpt_platform/backend/backend/copilot/prompting.py+++ b/autogpt_platform/backend/backend/copilot/prompting.py@@ -131,19 +131,61 @@ After building the file, reference it with `@@agptfile:` in other tools:non-overlapping scope to avoid redundant searches.-### Tool Discovery Priority+### Tool Discovery Priority — `find_block` is MANDATORY before any "no integration" reply-When the user asks to interact with a service or API, follow this order:+When the user asks to interact with a service, integration, platform, or API,+your **first action** in that turn MUST be a `find_block` call with the+service name. This is non-negotiable. Your prior knowledge of which+integrations exist is unreliable — the block registry changes constantly+and is the only source of truth.-1. **find_block first** — Search platform blocks with `find_block`. The platform has hundreds of built-in blocks (Google Sheets, Docs, Calendar, Gmail, Slack, GitHub, etc.) that work without extra setup.+Order of fallbacks (only after `find_block` returns nothing usable):-2. **run_mcp_tool** — If no matching block exists, check if a hosted MCP server is available for the service. Only use known MCP server URLs from the registry.+1. **`find_block` first — ALWAYS.** Search platform blocks with the service+ name (e.g. `find_block(query="linkedin post")`,+ `find_block(query="shopify orders")`). Hundreds of built-in blocks exist+ (Google Sheets, Docs, Calendar, Gmail, Slack, GitHub, LinkedIn via Ayrshare,+ etc.). Most "obscure" integrations have a block.-3. **SendAuthenticatedWebRequestBlock** — If no block or MCP server exists, use `SendAuthenticatedWebRequestBlock` with existing host-scoped credentials. Check available credentials via `connect_integration`.+2. **`run_mcp_tool`** — If `find_block` returns no match, check if a hosted MCP+ server is available for the service. Only use known MCP server URLs from+ the registry.-4. **Manual API call** — As a last resort, guide the user to set up credentials and use `SendAuthenticatedWebRequestBlock` with direct API calls.+3. **`SendAuthenticatedWebRequestBlock`** — If no block or MCP server exists,+ use `SendAuthenticatedWebRequestBlock` with existing host-scoped+ credentials. Check available credentials via `connect_integration`.-**Never skip step 1.** Built-in blocks are more reliable, tested, and user-friendly than MCP or raw API calls.+4. **Manual API call** — As a last resort, guide the user to set up+ credentials and use `SendAuthenticatedWebRequestBlock` with direct API+ calls.++### Anti-pattern: refusing without searching (CRITICAL)++**Never** emit any variant of these without a preceding `find_block` call in+the current turn:++- "We don't have a native X integration yet."+- "X isn't supported on the platform."+- "We can't do X / I can't access X."+- "There's no block for X."+- Any feature-request flow ("I'll flag this as a requested integration").++If you have not called `find_block` for the service the user named, you do+**not yet know** whether it exists. Refusing or pivoting to a workaround+before searching wastes the user's turn and is a known regression. This rule+overrides any worked example earlier in this prompt that shows a capability+gap being declared without a preceding `find_block` call — those examples+are wrong and should be ignored.++Correct flow for *any* integration request, even ones you "know" don't+exist:++```+1. find_block(query="<service> <action>")+2. If a matching block exists → use it (or run with validate_only to inspect).+3. If find_block returns no match → THEN state the gap and offer+ SendAuthenticatedWebRequestBlock / browser automation / feature request.+```### Complex multi-step work- Use `TodoWrite` to track the plan once the job has 3+ distinct steps.diff --git a/autogpt_platform/backend/backend/copilot/prompting_test.py b/autogpt_platform/backend/backend/copilot/prompting_test.pyindex d125b66a74..ae115c0508 100644--- a/autogpt_platform/backend/backend/copilot/prompting_test.py+++ b/autogpt_platform/backend/backend/copilot/prompting_test.py@@ -30,3 +30,31 @@ class TestGetSdkSupplementStaticPlaceholder:def test_e2b_mode_has_no_session_placeholder(self):result = prompting.get_sdk_supplement(use_e2b=True)assert "<session-id>" not in result+++class TestToolDiscoveryPriorityAntiPattern:+ """The Tool Discovery Priority section must forbid claiming a capability+ gap without calling ``find_block`` first — this is the regression the+ LinkedIn-skip incident on dev (May 2026) exposed.+ """++ def test_supplement_contains_find_block_mandatory_language(self):+ result = prompting.get_sdk_supplement(use_e2b=False)+ # The header must signal that find_block is mandatory before any+ # "no integration" reply.+ assert "find_block` is MANDATORY" in result++ def test_supplement_lists_the_forbidden_phrases(self):+ result = prompting.get_sdk_supplement(use_e2b=False)+ # The anti-pattern section must explicitly enumerate the+ # phrases the model emitted in the regression so the model+ # can pattern-match on its own draft and reject it.+ assert "We don't have a native X integration yet." in result+ assert "There's no block for X." in result++ def test_supplement_includes_correct_flow_template(self):+ result = prompting.get_sdk_supplement(use_e2b=False)+ # The 3-step correct-flow block must be present so the model+ # has a concrete template to follow, not just a prohibition.+ assert "Correct flow" in result+ assert 'find_block(query="<service> <action>")' in resultdiff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter.pyindex 2e7a30d848..b27e536987 100644--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter.py@@ -83,6 +83,10 @@ class SDKResponseAdapter:self.has_started_reasoning = Falseself.has_ended_reasoning = Trueself.render_reasoning_in_ui = render_reasoning_in_ui+ # Service layer forwards this from the prior adapter on a retry-recreate+ # so the empty-completion guard doesn't false-fire on a benign empty+ # trailing ResultMessage when the prior attempt already streamed content.+ self.prior_attempt_emitted_visible_content = Falseself.current_tool_calls: dict[str, dict[str, str]] = {}self.resolved_tool_calls: set[str] = set()self.step_open = False@@ -156,6 +160,21 @@ class SDKResponseAdapter:"""True when there are tool calls that haven't received output yet."""return bool(self.current_tool_calls.keys() - self.resolved_tool_calls)+ @property+ def emitted_visible_content(self) -> bool:+ """True when this adapter (or a prior attempt it inherited from) has+ already streamed user-visible content — text, reasoning, or a tool+ result — onto the wire. Used by the retry path so the new adapter+ can carry forward the prior attempt's visibility state without+ reaching into private flags.+ """+ return (+ self.has_started_text+ or self.has_started_reasoning+ or self._any_tool_results_seen+ or self.prior_attempt_emitted_visible_content+ )+def convert_message(self, sdk_message: Message) -> list[StreamBaseResponse]:"""Convert a single SDK message to Vercel AI SDK format."""responses: list[StreamBaseResponse] = []@@ -601,6 +620,9 @@ class SDKResponseAdapter:return Falseif self._any_tool_results_seen:return False+ # Retry adapter — prior attempt already streamed content to the wire.+ if self.prior_attempt_emitted_visible_content:+ return Falseusage = msg.usage or {}output_tokens = usage.get("output_tokens") or 0return output_tokens == 0diff --git a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.pyindex 5c3bacbc44..a054dc9aab 100644--- a/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py+++ b/autogpt_platform/backend/backend/copilot/sdk/response_adapter_test.py@@ -1811,3 +1811,78 @@ def test_summary_walk_skips_fully_streamed_text():summary_deltas = [r for r in summary if isinstance(r, StreamTextDelta)]assert len(partial_deltas) == 1assert summary_deltas == []+++def test_retry_adapter_with_prior_emitted_content_suppresses_empty_completion():+ """Regression: when the service layer recreates the adapter for a retry+ after the prior attempt already streamed text/tools to the wire, an empty+ success ResultMessage on the retry must NOT surface as an empty_completion+ StreamError — the user already received content from the prior attempt+ and would otherwise see a spurious "model returned an empty response"+ overlay on top of working output.+ """+ adapter = _adapter()+ adapter.prior_attempt_emitted_visible_content = True+ adapter.convert_message(SystemMessage(subtype="init", data={}))+ msg = ResultMessage(+ subtype="success",+ duration_ms=100,+ duration_api_ms=50,+ is_error=False,+ num_turns=1,+ session_id="s1",+ result="",+ usage={"output_tokens": 0},+ )+ results = adapter.convert_message(msg)+ types = [type(r).__name__ for r in results]+ assert "StreamFinish" in types+ assert "StreamError" not in types+++def test_retry_adapter_without_prior_content_still_surfaces_empty_completion():+ """Counter-test: when prior_attempt_emitted_visible_content is False (the+ default — first attempt), the SECRT-2252 guard must still fire on a+ ghost-finished success.+ """+ adapter = _adapter()+ assert adapter.prior_attempt_emitted_visible_content is False+ adapter.convert_message(SystemMessage(subtype="init", data={}))+ msg = ResultMessage(+ subtype="success",+ duration_ms=100,+ duration_api_ms=50,+ is_error=False,+ num_turns=1,+ session_id="s1",+ result="",+ usage={"output_tokens": 0},+ )+ results = adapter.convert_message(msg)+ err = next(r for r in results if isinstance(r, StreamError))+ assert err.code == "empty_completion"+++def test_emitted_visible_content_property_combines_signals():+ """The ``emitted_visible_content`` property must be True when any of the+ underlying visibility signals is set, so the service-layer retry path+ can forward the prior adapter's state without reaching into private+ flags.+ """+ adapter = _adapter()+ assert adapter.emitted_visible_content is False++ adapter.has_started_text = True+ assert adapter.emitted_visible_content is True+ adapter.has_started_text = False++ adapter.has_started_reasoning = True+ assert adapter.emitted_visible_content is True+ adapter.has_started_reasoning = False++ adapter._any_tool_results_seen = True+ assert adapter.emitted_visible_content is True+ adapter._any_tool_results_seen = False++ adapter.prior_attempt_emitted_visible_content = True+ assert adapter.emitted_visible_content is Truediff --git a/autogpt_platform/backend/backend/copilot/sdk/service.py b/autogpt_platform/backend/backend/copilot/sdk/service.pyindex 83ba3abeb9..f929324fba 100644--- a/autogpt_platform/backend/backend/copilot/sdk/service.py+++ b/autogpt_platform/backend/backend/copilot/sdk/service.py@@ -4507,6 +4507,7 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssuesstate.query_message = await _maybe_prepend_builder_context(session, user_id, is_user_message, state.query_message)+ prior_adapter = state.adapterstate.adapter = SDKResponseAdapter(message_id=message_id,session_id=session_id,@@ -4515,6 +4516,11 @@ async def stream_chat_completion_sdk( # pyright: ignore[reportGeneralTypeIssues# Carry the per-turn re-prompt cap forward so a transient# retry mid-turn does not unlock another re-prompt round.state.adapter.thinking_only_reprompted = state.thinking_only_reprompted+ # Forward prior visibility so the empty-completion guard on this+ # retry adapter doesn't false-fire on top of content the user+ # already received.+ if prior_adapter.emitted_visible_content:+ state.adapter.prior_attempt_emitted_visible_content = True# Reset token accumulators so a failed attempt's partial# usage is not double-counted in the successful attempt.state.usage.reset()