diff --git a/HARNESS_VERSION b/HARNESS_VERSION index 074040e44..81fb00552 100644 --- a/HARNESS_VERSION +++ b/HARNESS_VERSION @@ -1 +1 @@ -0.20.10 +0.20.11 diff --git a/src/clean_agent_preview.py b/src/clean_agent_preview.py index 53827e4b8..9a2377932 100644 --- a/src/clean_agent_preview.py +++ b/src/clean_agent_preview.py @@ -4321,6 +4321,7 @@ async def stream_preview(*, endpoint_url, model, messages, headers, turn_contrac successful_artifact_write = False artifact_recovery_attempts = 0 artifact_body_handoff_attempts = 0 + artifact_body_handoff_tool_violations = 0 artifact_body_handoff_target = '' artifact_off_contract_failures = 0 artifact_write_phase = False @@ -4402,6 +4403,9 @@ async def stream_preview(*, endpoint_url, model, messages, headers, turn_contrac if round_number > round_limit and not emergency_completion_round: break rounds_used = round_number + artifact_body_handoff_active_at_round_start = bool( + artifact_body_handoff_target + ) yield event({ 'type': 'agent_step', 'round': round_number, @@ -6279,6 +6283,29 @@ async def stream_preview(*, endpoint_url, model, messages, headers, turn_contrac ] history.append(visual_message) if artifact_body_handoff_target: + if artifact_body_handoff_active_at_round_start: + artifact_body_handoff_tool_violations += 1 + if artifact_body_handoff_tool_violations >= 2: + exhausted_target = artifact_body_handoff_target + artifact_body_handoff_target = '' + force_no_tools_next_round = False + history.append({ + 'role': 'user', + '_harness_control': True, + 'content': ( + 'The bounded body-only recovery is exhausted because tool calls ' + 'were emitted instead of a raw file body. Use the artifact writer ' + f'offered on the next round to create {exhausted_target} with valid ' + 'arguments. Do not use another tool or repeat the rejected call.' + ), + }) + yield event({ + 'type': 'completion_recovery', + 'reason': 'malformed_write_body_handoff_exhausted', + 'path': exhausted_target, + 'tool_violations': artifact_body_handoff_tool_violations, + }) + continue force_no_tools_next_round = True replace_streamed_draft_on_finish = True history.append({ diff --git a/tests/test_clean_agent_preview.py b/tests/test_clean_agent_preview.py index c2bc1f7c0..4d03ff765 100644 --- a/tests/test_clean_agent_preview.py +++ b/tests/test_clean_agent_preview.py @@ -817,6 +817,115 @@ async def test_empty_artifact_writer_turn_recovers_via_body_handoff(monkeypatch) assert any(event.get('type') == 'artifact_body_handoff' for event in events) +@pytest.mark.asyncio +async def test_malformed_writer_body_handoff_reopens_writer_after_two_tool_violations(monkeypatch): + import src.clean_agent_preview as module + + responses = iter([ + {'choices': [{'delta': {'tool_calls': [{ + 'index': 0, 'id': 'malformed-write', + 'function': { + 'name': 'write_file', + 'arguments': '{"path":"/workspace/output.tex"}\n{"content":"truncated"}', + }, + }]}}]}, + {'choices': [{'delta': {'tool_calls': [{ + 'index': 0, 'id': 'off-contract-bash-one', + 'function': { + 'name': 'bash', + 'arguments': json.dumps({'command': 'echo nope > /workspace/output.tex'}), + }, + }]}}]}, + {'choices': [{'delta': {'tool_calls': [{ + 'index': 0, 'id': 'off-contract-bash-two', + 'function': { + 'name': 'bash', + 'arguments': json.dumps({'command': 'echo nope > /workspace/output.tex'}), + }, + }]}}]}, + {'choices': [{'delta': {'tool_calls': [{ + 'index': 0, 'id': 'recovered-write', + 'function': { + 'name': 'write_file', + 'arguments': json.dumps({ + 'path': '/workspace/output.tex', + 'content': '\\begin{tabular}{ll}A & B\\\\\\end{tabular}', + }), + }, + }]}}]}, + {'choices': [{'delta': {'content': 'Created the requested TeX artifact.'}}]}, + ]) + + 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) + schemas = [ + item for item in FUNCTION_TOOL_SCHEMAS + if item['function']['name'] in {'write_file', 'bash'} + ] + contract = resolve_full_inventory_contract(schemas=schemas, policy=ToolPolicy()) + raw = [chunk async for chunk in stream_preview( + endpoint_url='http://test', model='test', + messages=[{'role': 'user', 'content': 'Create the requested TeX table.'}], + 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.tex'], + }, + }, max_tokens=8192, max_rounds=6, + )] + + assert len(requests) == 5 + assert 'tools' not in requests[1] + assert 'tools' not in requests[2] + assert requests[3]['tool_choice'] == { + 'type': 'function', 'function': {'name': 'write_file'}, + } + 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 sum( + event.get('reason') == 'malformed_write_body_handoff' + for event in events + ) == 2 + assert any( + event.get('reason') == 'malformed_write_body_handoff_exhausted' + for event in events + ) + assert any( + event.get('type') == 'final_response' + and event.get('content') == 'Created the requested TeX artifact.' + for event in events + ) + + @pytest.mark.asyncio async def test_binary_artifact_completion_requests_python_not_text_writer(monkeypatch): import src.clean_agent_preview as module diff --git a/tests/test_harness_version.py b/tests/test_harness_version.py index ffb13732b..28c6202f0 100644 --- a/tests/test_harness_version.py +++ b/tests/test_harness_version.py @@ -8,7 +8,7 @@ ROOT = Path(__file__).resolve().parents[1] def test_original_harness_version_is_canonical_semver() -> None: version = (ROOT / "HARNESS_VERSION").read_text(encoding="utf-8").strip() - assert version == "0.20.10" + assert version == "0.20.11" assert re.fullmatch(r"\d+\.\d+\.\d+", version)