"""A normal next call carries only safe schema feedback, without repair calls.""" from types import SimpleNamespace import pytest from pydantic import BaseModel, ValidationError from backend.browser import diagnostics, runner class Output(BaseModel): action: list[dict] def record_error(): try: Output.model_validate({'PRIVATE-KEY': 'PRIVATE-RESPONSE'}) except ValidationError as error: diagnostics.schema_error(None, error, Output) @pytest.fixture def local(tmp_path, monkeypatch): monkeypatch.setattr(diagnostics, 'task_doc_dir', lambda _: tmp_path) async def test_correction_is_added_to_one_normal_call_and_clears_after_success(local): from browser_use.llm.messages import UserMessage calls = [] class Base: async def get_model_output(self, messages): calls.append(messages) return SimpleNamespace(action=[]) agent = runner._prospecting_agent_class(Base)() agent._goal_based = True original = [UserMessage(content='Current task')] with diagnostics.run_scope('correction_fixture', 'run_fixture'): record_error() await agent.get_model_output(original) assert len(calls) == 1 and len(original) == 1 hint = calls[0][-1].text assert 'action' in hint and 'missing' in hint and 'PRIVATE' not in hint # A framework fallback sees the same correction once, not a growing # stack of messages or a paid correction loop. await agent.get_model_output(calls[0]) assert len(calls) == 2 and len(calls[1]) == 2 diagnostics.validated_response('browser') await agent.get_model_output(original) assert len(calls) == 3 and calls[2] is original async def test_other_run_and_legacy_agent_do_not_receive_previous_error(local): from browser_use.llm.messages import UserMessage calls = [] class Base: async def get_model_output(self, messages): calls.append(messages) return None agent = runner._prospecting_agent_class(Base)() original = [UserMessage(content='Current task')] with diagnostics.run_scope('first', 'first_run'): record_error() await agent.get_model_output(original) assert calls[-1] is original agent._goal_based = True with diagnostics.run_scope('second', 'second_run'): await agent.get_model_output(original) assert calls[-1] is original