From 39851728852aea9fcfd7648acc2a6aec0baeb662 Mon Sep 17 00:00:00 2001 From: pewdiepie-archdaemon Date: Fri, 18 Sep 2026 16:33:30 +0000 Subject: [PATCH] recover empty artifact writer turns through body handoff --- src/clean_agent_preview.py | 34 +++++++++++++++++ tests/test_clean_agent_preview.py | 63 +++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+) diff --git a/src/clean_agent_preview.py b/src/clean_agent_preview.py index d4c157696..b463d9d24 100644 --- a/src/clean_agent_preview.py +++ b/src/clean_agent_preview.py @@ -4629,6 +4629,40 @@ async def stream_preview(*, endpoint_url, model, messages, headers, turn_contrac message['tool_calls'] = protocol_safe_tool_calls(proposed) history.append(message) if not proposed: + empty_artifact_target = ( + repeated_off_contract_artifact_handoff_target( + artifact_write_phase=artifact_write_phase, + successful_artifact_write=successful_artifact_write, + required_artifacts=required_artifacts, + failures=2, + ) + if not content and not artifact_body_handoff_target else '' + ) + if ( + empty_artifact_target + and artifact_body_handoff_attempts < 2 + and round_number < round_limit + ): + artifact_body_handoff_attempts += 1 + artifact_body_handoff_target = empty_artifact_target + force_no_tools_next_round = True + replace_streamed_draft_on_finish = True + history.append({ + 'role': 'user', + '_harness_control': True, + 'content': ( + 'The writer-only artifact turn returned no usable content. ' + f'Return only the complete raw body for {empty_artifact_target}; ' + 'do not emit JSON, a tool call, commentary, or an action promise.' + ), + }) + yield event({ + 'type': 'completion_recovery', + 'reason': 'empty_artifact_writer_body_handoff', + 'path': empty_artifact_target, + 'attempt': artifact_body_handoff_attempts, + }) + continue if ( unexecutable_dsml_completion and ( diff --git a/tests/test_clean_agent_preview.py b/tests/test_clean_agent_preview.py index 6ca600135..7f56d7bb0 100644 --- a/tests/test_clean_agent_preview.py +++ b/tests/test_clean_agent_preview.py @@ -543,6 +543,69 @@ def test_provider_wire_drops_deepseek_reasoning_only_turn_rejected_by_provider() assert module.provider_wire_messages(messages) == [] +@pytest.mark.asyncio +async def test_empty_artifact_writer_turn_recovers_via_body_handoff(monkeypatch): + import src.clean_agent_preview as module + + responses = iter([ + {'choices': [{'delta': {'reasoning_content': 'spent the turn planning'}}]}, + {'choices': [{'delta': {'content': 'Recovered'}}]}, + ]) + + class Response: + def __init__(self, payload): self.payload = payload + async def __aenter__(self): return self + async def __aexit__(self, *args): pass + def raise_for_status(self): pass + async def aiter_lines(self): + yield 'data: ' + json.dumps(self.payload) + yield 'data: [DONE]' + + requests = [] + + class Client: + def __init__(self, **kwargs): pass + async def __aenter__(self): return self + async def __aexit__(self, *args): pass + def stream(self, *args, **kwargs): + requests.append(kwargs['json']) + return Response(next(responses)) + + executed = [] + + async def execute(block, **kwargs): + executed.append(block) + return block.tool_type, {'output': 'written', 'exit_code': 0} + + monkeypatch.setattr(module, 'NATIVE_ARTIFACT_RESEARCH_LIMIT', 0) + monkeypatch.setattr(module.httpx, 'AsyncClient', Client) + monkeypatch.setattr(module, 'execute_tool_block', execute) + schema = next( + item for item in FUNCTION_TOOL_SCHEMAS + if item['function']['name'] == 'write_file' + ) + contract = resolve_full_inventory_contract(schemas=[schema], policy=ToolPolicy()) + raw = [chunk async for chunk in stream_preview( + endpoint_url='http://test', model='deepseek-flash', + messages=[{'role': 'user', 'content': 'Create the requested HTML.'}], + headers={}, turn_contract=contract, session_id='test', owner='test', + disabled_tools=set(), tool_policy=ToolPolicy(), workspace='/workspace', + client_runtime_context={ + 'surface': 'odysseus-native', 'terminal_agent': True, + 'unattended_mode': True, + 'completion_requirements': { + 'required_artifacts': ['/workspace/output.html'], + }, + }, max_tokens=8192, max_rounds=3, + )] + + assert len(requests) == 2 + assert requests[1]['max_tokens'] == 8192 + assert [block.tool_type for block in executed] == ['write_file'] + events = [json.loads(chunk[6:]) for chunk in raw if '[DONE]' not in chunk] + assert any(event.get('type') == 'artifact_body_handoff' for event in events) + + @pytest.mark.asyncio async def test_repeated_off_contract_calls_recover_via_required_artifact_body(monkeypatch): import src.clean_agent_preview as module