|
| 1 | +from typing import Any |
| 2 | + |
| 3 | +import render_machine.render_utils as render_utils |
| 4 | +from memory_management import MemoryManager |
| 5 | +from plain2code_console import console |
| 6 | +from plain2code_exceptions import InternalClientError |
| 7 | +from render_machine.actions.base_action import BaseAction |
| 8 | +from render_machine.implementation_code_helpers import ImplementationCodeHelpers |
| 9 | +from render_machine.render_context import RenderContext |
| 10 | +from render_machine.render_types import RenderError |
| 11 | + |
| 12 | + |
| 13 | +class PrepareConformanceTestFix(BaseAction): |
| 14 | + SUCCESSFUL_OUTCOME = "conformance_test_fix_prepared" |
| 15 | + FAILED_OUTCOME = "conformance_test_fix_preparation_failed" |
| 16 | + |
| 17 | + def execute(self, render_context: RenderContext, previous_action_payload: Any | None): |
| 18 | + if render_context.verbose: |
| 19 | + console.info( |
| 20 | + f"Running prepare_conformance_test_fix_script for FRID " |
| 21 | + f"{render_context.conformance_tests_running_context.current_testing_frid}." |
| 22 | + ) |
| 23 | + |
| 24 | + if not previous_action_payload or not previous_action_payload.get("previous_conformance_tests_issue"): |
| 25 | + raise InternalClientError("Previous action payload does not contain previous conformance tests issue.") |
| 26 | + previous_conformance_tests_issue = previous_action_payload["previous_conformance_tests_issue"] |
| 27 | + |
| 28 | + _, existing_files_content = ImplementationCodeHelpers.fetch_existing_files(render_context.build_folder) |
| 29 | + _, memory_files_content = MemoryManager.fetch_memory_files(render_context.memory_manager.memory_folder) |
| 30 | + ( |
| 31 | + _, |
| 32 | + existing_conformance_test_files_content, |
| 33 | + ) = render_context.conformance_tests.fetch_existing_conformance_test_files( |
| 34 | + render_context.module_name, |
| 35 | + render_context.required_modules, |
| 36 | + render_context.conformance_tests_running_context.current_testing_module_name, |
| 37 | + render_context.conformance_tests_running_context.get_current_conformance_test_folder_name(), |
| 38 | + ) |
| 39 | + previous_frid_code_diff = ImplementationCodeHelpers.get_code_diff( |
| 40 | + render_context.build_folder, render_context.plain_source_tree, render_context.frid_context.frid |
| 41 | + ) |
| 42 | + |
| 43 | + with open(render_context.prepare_conformance_test_fix_script, "r", encoding="utf-8") as f: |
| 44 | + script_content = f.read() |
| 45 | + |
| 46 | + api_response = render_context.codeplain_api.prepare_conformance_test_fix( |
| 47 | + frid=render_context.frid_context.frid, |
| 48 | + functional_requirement_id=render_context.conformance_tests_running_context.current_testing_frid, |
| 49 | + plain_source_tree=render_context.plain_source_tree, |
| 50 | + linked_resources=render_context.frid_context.linked_resources, |
| 51 | + existing_files_content=existing_files_content, |
| 52 | + memory_files_content=memory_files_content, |
| 53 | + module_name=render_context.module_name, |
| 54 | + conformance_tests_module_name=( |
| 55 | + render_context.conformance_tests_running_context.current_testing_module_name |
| 56 | + ), |
| 57 | + required_modules=render_context.get_required_modules_functionalities(), |
| 58 | + code_diff=previous_frid_code_diff, |
| 59 | + conformance_tests_files=existing_conformance_test_files_content, |
| 60 | + acceptance_tests=render_context.conformance_tests_running_context.get_current_acceptance_tests(), |
| 61 | + conformance_tests_issue=previous_conformance_tests_issue, |
| 62 | + implementation_fix_count=render_context.conformance_tests_running_context.fix_attempts, |
| 63 | + conformance_tests_folder_name=( |
| 64 | + render_context.conformance_tests_running_context.get_current_conformance_test_folder_name() |
| 65 | + ), |
| 66 | + current_testing_frid_high_level_implementation_plan=( |
| 67 | + render_context.conformance_tests_running_context.current_testing_frid_high_level_implementation_plan |
| 68 | + ), |
| 69 | + run_state=render_context.run_state, |
| 70 | + prepare_conformance_test_fix_script=script_content, |
| 71 | + ) |
| 72 | + |
| 73 | + instructions = api_response.get("instructions", "") |
| 74 | + |
| 75 | + exit_code, conformance_test_fix_information, script_output_path = render_utils.execute_script( |
| 76 | + render_context.prepare_conformance_test_fix_script, |
| 77 | + [instructions], |
| 78 | + render_context.verbose, |
| 79 | + "Prepare Conformance Test Fix", |
| 80 | + timeout=render_context.test_script_timeout, |
| 81 | + stop_event=render_context.stop_event, |
| 82 | + ) |
| 83 | + |
| 84 | + if exit_code == 0 or exit_code == render_utils.TIMEOUT_ERROR_EXIT_CODE: |
| 85 | + render_context.conformance_tests_running_context.conformance_test_fix_information = ( |
| 86 | + conformance_test_fix_information |
| 87 | + ) |
| 88 | + render_context.script_execution_history.latest_prepare_conformance_test_fix_output_path = script_output_path |
| 89 | + render_context.script_execution_history.should_update_script_outputs = True |
| 90 | + return ( |
| 91 | + self.SUCCESSFUL_OUTCOME, |
| 92 | + {"previous_conformance_tests_issue": previous_conformance_tests_issue}, |
| 93 | + ) |
| 94 | + |
| 95 | + return ( |
| 96 | + self.FAILED_OUTCOME, |
| 97 | + RenderError.encode( |
| 98 | + message="Prepare conformance test fix failed. Please check the prepare_conformance_test_fix_script.", |
| 99 | + error_type="PREPARE_CONFORMANCE_TEST_FIX_ERROR", |
| 100 | + exit_code=exit_code, |
| 101 | + script=render_context.prepare_conformance_test_fix_script, |
| 102 | + ).to_payload(), |
| 103 | + ) |
0 commit comments