diff --git a/src/clean_agent_preview.py b/src/clean_agent_preview.py index 73432985e..0419a48f1 100644 --- a/src/clean_agent_preview.py +++ b/src/clean_agent_preview.py @@ -1455,6 +1455,36 @@ def execution_targets_required_artifact(tool_name, arguments, required_artifacts return any(str(path or '').rstrip('/') in serialized for path in required_artifacts) +def artifact_completion_tool_schemas(offered_schemas, required_artifacts): + """Bind the sole artifact writer to the runner-declared output file. + + This applies only after native execution enters its reserved artifact-write + phase. A JSON-Schema ``const`` gives the provider the exact destination + instead of relying on it to recover the path from a long conversation. + Multiple outputs and directory targets stay unconstrained because choosing + one of those paths requires model intent. + """ + targets = [str(path or '').strip().rstrip('/') for path in required_artifacts] + targets = [path for path in targets if path and Path(path).suffix] + if len(targets) != 1 or len(tuple(required_artifacts or ())) != 1: + return offered_schemas + target = targets[0] + bound = copy.deepcopy(offered_schemas) + for schema in bound: + function = schema.get('function') or {} + if canonical(function.get('name')) != 'write_file': + continue + properties = (function.get('parameters') or {}).get('properties') or {} + path_schema = properties.get('path') + if not isinstance(path_schema, dict): + continue + path_schema['const'] = target + path_schema['description'] = ( + f'Write this exact required artifact path: {target}' + ) + return bound + + def protocol_safe_tool_calls(calls): """Keep malformed model calls out of the next provider request.""" safe_calls = copy.deepcopy(calls) @@ -4334,6 +4364,10 @@ async def stream_preview(*, endpoint_url, model, messages, headers, turn_contrac canonical(schema['function']['name']), 0 ) < round_number ] + if artifact_write_phase and not successful_artifact_write: + round_offered = artifact_completion_tool_schemas( + round_offered, required_artifacts, + ) research_choice = None if not required_artifacts: round_offered, research_choice, _ = bounded_research_tool_policy( diff --git a/tests/test_clean_agent_preview.py b/tests/test_clean_agent_preview.py index ba6daeba6..c809a93cd 100644 --- a/tests/test_clean_agent_preview.py +++ b/tests/test_clean_agent_preview.py @@ -5,7 +5,7 @@ import jsonschema import pytest import re -from src.clean_agent_preview import conversation, readonly_call, preview_call_allowed, evaluate_preview_call, authorized_write_families, compact_schemas, normalize_preview_function_args, normalize_preview_call_args, private_browser_dom_batch, private_browser_state_transition, private_browser_success_repeat_limit, stream_preview, denied_response, execution_has_write_effect, requests_mutation, claims_completion, recent_successful_write_families, scope_preview_contract, multimodal_image_count, attachment_reference_count, active_document_context_message, active_email_context_message, targets_active_editor, active_editor_whole_draft_request, active_editor_suggestion_request, scope_active_editor_contract, native_execution_limits, interactive_execution_limit, runtime_required_artifacts, execution_targets_required_artifact, document_suggestions_event, document_suggestion_quality_error, required_read_tool_choice, required_active_editor_tool_choice, sealed_read_arguments, email_identifier_error, requested_item_limit, contract_item_limit, notes_terminal_response, documents_terminal_response, shell_listing_terminal_response, shell_output_terminal_response, direct_shell_output_request, ui_panel_terminal_response, ui_toggle_state_result, calendar_terminal_response, memory_terminal_response, tasks_terminal_response, task_list_requires_synthesis, skills_terminal_response, cookbook_servers_terminal_response, prior_short_answer_for_no_tool_summary, prior_collection_repeat_answer, prior_failed_operation_answer, prior_cookbook_server_answer, prior_workspace_path_answer, prior_web_source_answer, bounded_web_evidence_answer, inherit_referential_read_arguments, normalized_search_intent, requested_web_source_links, web_source_links, requested_web_link_limit, preserve_requested_web_recency, ground_referenced_note_content, note_search_result_empty, note_referent_error, research_referent_error, private_browser_open_url, private_browser_effective_url, web_fetch_observation_is_boilerplate, broad_current_web_request, record_tool_execution, align_structured_tool_history, provider_request_messages, provider_compatible_tool_choice_request, offered_tool_alias, dependent_write_prerequisite_error, bounded_research_tool_policy, retrieved_source_urls, serialize_required_email_attachment_chain +from src.clean_agent_preview import conversation, readonly_call, preview_call_allowed, evaluate_preview_call, authorized_write_families, compact_schemas, normalize_preview_function_args, normalize_preview_call_args, private_browser_dom_batch, private_browser_state_transition, private_browser_success_repeat_limit, stream_preview, denied_response, execution_has_write_effect, requests_mutation, claims_completion, recent_successful_write_families, scope_preview_contract, multimodal_image_count, attachment_reference_count, active_document_context_message, active_email_context_message, targets_active_editor, active_editor_whole_draft_request, active_editor_suggestion_request, scope_active_editor_contract, native_execution_limits, interactive_execution_limit, runtime_required_artifacts, execution_targets_required_artifact, artifact_completion_tool_schemas, document_suggestions_event, document_suggestion_quality_error, required_read_tool_choice, required_active_editor_tool_choice, sealed_read_arguments, email_identifier_error, requested_item_limit, contract_item_limit, notes_terminal_response, documents_terminal_response, shell_listing_terminal_response, shell_output_terminal_response, direct_shell_output_request, ui_panel_terminal_response, ui_toggle_state_result, calendar_terminal_response, memory_terminal_response, tasks_terminal_response, task_list_requires_synthesis, skills_terminal_response, cookbook_servers_terminal_response, prior_short_answer_for_no_tool_summary, prior_collection_repeat_answer, prior_failed_operation_answer, prior_cookbook_server_answer, prior_workspace_path_answer, prior_web_source_answer, bounded_web_evidence_answer, inherit_referential_read_arguments, normalized_search_intent, requested_web_source_links, web_source_links, requested_web_link_limit, preserve_requested_web_recency, ground_referenced_note_content, note_search_result_empty, note_referent_error, research_referent_error, private_browser_open_url, private_browser_effective_url, web_fetch_observation_is_boilerplate, broad_current_web_request, record_tool_execution, align_structured_tool_history, provider_request_messages, provider_compatible_tool_choice_request, offered_tool_alias, dependent_write_prerequisite_error, bounded_research_tool_policy, retrieved_source_urls, serialize_required_email_attachment_chain from src.tool_capabilities import capabilities_for_tool @@ -454,6 +454,53 @@ def test_runtime_required_artifacts_includes_runner_declared_directory(): ) == ('/tmp_workspace/results',) +def test_artifact_completion_schema_binds_single_required_file_without_mutating_source(): + source = [{ + 'type': 'function', + 'function': { + 'name': 'write_file', + 'description': 'Write a file.', + 'parameters': { + 'type': 'object', + 'properties': { + 'path': {'type': 'string'}, + 'content': {'type': 'string'}, + }, + 'required': ['path', 'content'], + }, + }, + }] + + bound = artifact_completion_tool_schemas( + source, ('/workspace/output.html',), + ) + + path_schema = bound[0]['function']['parameters']['properties']['path'] + assert path_schema['const'] == '/workspace/output.html' + assert '/workspace/output.html' in path_schema['description'] + assert 'const' not in source[0]['function']['parameters']['properties']['path'] + + +def test_artifact_completion_schema_does_not_bind_directory_or_multiple_outputs(): + source = [{ + 'type': 'function', + 'function': { + 'name': 'write_file', + 'parameters': { + 'type': 'object', + 'properties': {'path': {'type': 'string'}}, + }, + }, + }] + + assert artifact_completion_tool_schemas( + source, ('/workspace/results/',), + ) == source + assert artifact_completion_tool_schemas( + source, ('/workspace/a.txt', '/workspace/b.txt'), + ) == source + + def test_runtime_required_artifacts_does_not_promote_inputs_to_outputs(): assert runtime_required_artifacts( 'Read /workspace/input/data.json and write /workspace/results/report.json.',