{"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaL4","dataSources":[{"sourceId":84795,"databundleVersionId":11281725,"sourceType":"competition"},{"sourceId":254441400,"sourceType":"kernelVersion"},{"sourceId":237029,"sourceType":"modelInstanceVersion","modelInstanceId":202436,"modelId":224071}],"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":true},"papermill":{"default_parameters":{},"duration":1050.264876,"end_time":"2025-03-08T18:18:10.804536","environment_variables":{},"exception":null,"input_path":"__notebook__.ipynb","output_path":"__notebook__.ipynb","parameters":{},"start_time":"2025-03-08T18:00:40.53966","version":"2.6.0"},"widgets":{"application/vnd.jupyter.widget-state+json":{"state":{"1be8fb532d724219a1c66f536d79ccb9":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HTMLView","description":"","description_allow_html":false,"layout":"IPY_MODEL_d43aa48a5a0a47639fb3bddc4674e7a2","placeholder":"​","style":"IPY_MODEL_d8c7954f4b1a488e8f5b3d48924b6ee8","tabbable":null,"tooltip":null,"value":""}},"305b37813a6f47cabd34ede43970567c":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"472e051c59a049e3bf67082fd3bee1a1":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","background":null,"description_width":"","font_size":null,"text_color":null}},"613b13744cd24ce4a28fa2b38343b851":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"649fa0278f3245f7baaff14229ef7bca":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"FloatProgressModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"FloatProgressModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"ProgressView","bar_style":"success","description":"","description_allow_html":false,"layout":"IPY_MODEL_613b13744cd24ce4a28fa2b38343b851","max":2,"min":0,"orientation":"horizontal","style":"IPY_MODEL_a6951f841c1d447fbe9c8c05d771c227","tabbable":null,"tooltip":null,"value":2}},"8430da0c92894f889a44f76413abf017":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HTMLView","description":"","description_allow_html":false,"layout":"IPY_MODEL_ab123e4421cb4040a479c6db224cec58","placeholder":"​","style":"IPY_MODEL_472e051c59a049e3bf67082fd3bee1a1","tabbable":null,"tooltip":null,"value":"Loading safetensors checkpoint shards: 100% Completed | 2/2 [01:00&lt;00:00, 29.84s/it]\n"}},"a6951f841c1d447fbe9c8c05d771c227":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"ProgressStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"ProgressStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","bar_color":null,"description_width":""}},"ab123e4421cb4040a479c6db224cec58":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"ac7c84880ccc4180a1430c9535b855a3":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HBoxModel","state":{"_dom_classes":[],"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HBoxModel","_view_count":null,"_view_module":"@jupyter-widgets/controls","_view_module_version":"2.0.0","_view_name":"HBoxView","box_style":"","children":["IPY_MODEL_1be8fb532d724219a1c66f536d79ccb9","IPY_MODEL_649fa0278f3245f7baaff14229ef7bca","IPY_MODEL_8430da0c92894f889a44f76413abf017"],"layout":"IPY_MODEL_305b37813a6f47cabd34ede43970567c","tabbable":null,"tooltip":null}},"d43aa48a5a0a47639fb3bddc4674e7a2":{"model_module":"@jupyter-widgets/base","model_module_version":"2.0.0","model_name":"LayoutModel","state":{"_model_module":"@jupyter-widgets/base","_model_module_version":"2.0.0","_model_name":"LayoutModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"LayoutView","align_content":null,"align_items":null,"align_self":null,"border_bottom":null,"border_left":null,"border_right":null,"border_top":null,"bottom":null,"display":null,"flex":null,"flex_flow":null,"grid_area":null,"grid_auto_columns":null,"grid_auto_flow":null,"grid_auto_rows":null,"grid_column":null,"grid_gap":null,"grid_row":null,"grid_template_areas":null,"grid_template_columns":null,"grid_template_rows":null,"height":null,"justify_content":null,"justify_items":null,"left":null,"margin":null,"max_height":null,"max_width":null,"min_height":null,"min_width":null,"object_fit":null,"object_position":null,"order":null,"overflow":null,"padding":null,"right":null,"top":null,"visibility":null,"width":null}},"d8c7954f4b1a488e8f5b3d48924b6ee8":{"model_module":"@jupyter-widgets/controls","model_module_version":"2.0.0","model_name":"HTMLStyleModel","state":{"_model_module":"@jupyter-widgets/controls","_model_module_version":"2.0.0","_model_name":"HTMLStyleModel","_view_count":null,"_view_module":"@jupyter-widgets/base","_view_module_version":"2.0.0","_view_name":"StyleView","background":null,"description_width":"","font_size":null,"text_color":null}}},"version_major":2,"version_minor":0}}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Install packages","metadata":{}},{"cell_type":"code","source":"!pip install /kaggle/input/konwinski-prize/kaggle_evaluation/../kprize_setup/kprize-1.1.1-py3-none-any.whl --no-index --find-links /kaggle/input/konwinski-prize/kaggle_evaluation/../kprize_setup/pip_packages/kprize","metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","papermill":{"duration":5.90132,"end_time":"2025-03-08T18:00:49.321601","exception":false,"start_time":"2025-03-08T18:00:43.420281","status":"completed"},"scrolled":true,"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-08-05T19:53:05.817583Z","iopub.execute_input":"2025-08-05T19:53:05.818015Z","iopub.status.idle":"2025-08-05T19:53:11.159938Z","shell.execute_reply.started":"2025-08-05T19:53:05.817993Z","shell.execute_reply":"2025-08-05T19:53:11.159265Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pip install --no-index --find-links=/kaggle/input/vllm-ins vllm -U\n!pip install --no-index --find-links=/kaggle/input/vllm-ins triton==3.1.0 -q\n!pip install --no-index --find-links=/kaggle/input/vllm-ins bitsandbytes -q\n!pip install --no-index --find-links=/kaggle/input/vllm-ins latex2sympy2==1.9.1 -q\n!pip install --no-index --find-links=/kaggle/input/vllm-ins lmdeploy -q\n!pip install --no-index --find-links=/kaggle/input/vllm-ins pynvml==12.0.0\n# !pip install --no-index --find-links=/kaggle/input/vllm-ins flashinfer_python==0.2.0.post2","metadata":{"papermill":{"duration":72.356969,"end_time":"2025-03-08T18:02:01.689361","exception":false,"start_time":"2025-03-08T18:00:49.332392","status":"completed"},"scrolled":true,"tags":[],"trusted":true,"execution":{"iopub.status.busy":"2025-08-05T19:53:11.161168Z","iopub.execute_input":"2025-08-05T19:53:11.161385Z","iopub.status.idle":"2025-08-05T19:55:39.678997Z","shell.execute_reply.started":"2025-08-05T19:53:11.161363Z","shell.execute_reply":"2025-08-05T19:55:39.67834Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Set seed","metadata":{}},{"cell_type":"code","source":"from transformers import set_seed\nset_seed(42)\n\nSEED = 42\nimport numpy as np\nimport torch\nimport random\n\ndef set_seeds(seed=42):\n    random.seed(seed)\n    np.random.seed(seed)\n    torch.manual_seed(seed)\n    torch.cuda.manual_seed_all(seed)\n    torch.backends.cudnn.deterministic = True\n    torch.backends.cudnn.benchmark = False\nset_seeds()","metadata":{"execution":{"iopub.status.busy":"2025-08-05T19:55:39.679779Z","iopub.execute_input":"2025-08-05T19:55:39.679997Z","iopub.status.idle":"2025-08-05T19:55:55.295895Z","shell.execute_reply.started":"2025-08-05T19:55:39.679976Z","shell.execute_reply":"2025-08-05T19:55:55.295195Z"},"papermill":{"duration":19.156103,"end_time":"2025-03-08T18:02:20.858605","exception":false,"start_time":"2025-03-08T18:02:01.702502","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import io\nimport time\nimport shutil\n\nimport pandas as pd\nimport polars as pl\n\nimport kaggle_evaluation.konwinski_prize_inference_server\nfrom typing import List, Tuple, Dict, Optional\n\nstart_time = time.time()","metadata":{"execution":{"iopub.status.busy":"2025-08-05T19:55:55.296573Z","iopub.execute_input":"2025-08-05T19:55:55.296999Z","iopub.status.idle":"2025-08-05T19:55:57.271491Z","shell.execute_reply.started":"2025-08-05T19:55:55.296978Z","shell.execute_reply":"2025-08-05T19:55:57.270929Z"},"papermill":{"duration":2.060631,"end_time":"2025-03-08T18:02:22.932348","exception":false,"start_time":"2025-03-08T18:02:20.871717","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"instance_count: Optional[int] = None\n\n\ndef get_number_of_instances(num_instances: int) -> None:\n    \"\"\"The very first message from the gateway will be the total number of instances to be served.\n    You don't need to edit this function.\n    \"\"\"\n    global instance_count\n    instance_count = num_instances","metadata":{"execution":{"iopub.status.busy":"2025-08-05T19:55:57.272973Z","iopub.execute_input":"2025-08-05T19:55:57.273642Z","iopub.status.idle":"2025-08-05T19:55:57.276721Z","shell.execute_reply.started":"2025-08-05T19:55:57.273621Z","shell.execute_reply":"2025-08-05T19:55:57.276248Z"},"papermill":{"duration":0.017745,"end_time":"2025-03-08T18:02:22.963183","exception":false,"start_time":"2025-03-08T18:02:22.945438","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Start model","metadata":{}},{"cell_type":"code","source":"from vllm import LLM, SamplingParams, RequestOutput\nimport warnings\nimport re\nimport subprocess\nimport tempfile\nimport os\nimport sys\nfrom typing import List, Tuple, Dict, Optional\nfrom joblib import Parallel, delayed\n# warnings.simplefilter(\"ignore\")\n\nos.environ[\"CUDA_VISIBLE_DEVICES\"] = \"0,1,2,3\"\nos.environ[\"TOKENIZERS_PARALLELISM\"] = \"false\"\nos.environ[\"VLLM_USE_V1\"] = \"1\"\n\nos.environ[\"VLLM_LOGITS_PROCESSOR_THREADS\"] = \"1\"\n\nos.environ[\"VLLM_USE_TRITON_FLASH_ATTN\"]='1'\nos.environ[\"VLLM_USE_TRITON_AWQ\"]='1'\n\n# os.environ[\"VLLM_ATTENTION_BACKEND\"]='FLASHINFER'\n# os.environ[\"VLLM_USE_FLASHINFER_SAMPLER\"]='1'\n# os.environ[\"VLLM_FLASHINFER_FORCE_TENSOR_CORES\"]='1'\nos.environ[\"TRITON_PTXAS_PATH\"] = \"/usr/local/cuda/bin/ptxas\"\n\nif os.getenv(\"KAGGLE_KERNEL_RUN_TYPE\") or os.getenv(\"KAGGLE_IS_COMPETITION_RERUN\"):\n    llm_model_pth = '/kaggle/input/deepseek-r1/transformers/deepseek-r1-distill-qwen-14b-awq/1'\n    print(os.path.isdir(llm_model_pth))\n    if not os.path.isdir(llm_model_pth):\n        llm_model_pth = '/kaggle/input/m/shelterw/deepseek-r1/transformers/deepseek-r1-distill-qwen-32b-awq/1'\nelse:\n    llm_model_pth: str = \"/root/volume/KirillR/QwQ-32B-Preview-AWQ\"\n\nBATCH_SIZE: int = 8\nVALIDATION_COPY_COUNT: int = 1\nMAX_TOKENS: int = 4096\n\nMAX_NUM_SEQS: int = 6\nMAX_MODEL_LEN: int = 32_768\n\nllm: LLM = LLM(\n    llm_model_pth,\n    # max_num_seqs=MAX_NUM_SEQS,  # Maximum number of sequences per iteration. Default is 256\n    max_model_len=MAX_MODEL_LEN,  # Model context length\n    trust_remote_code=True,  # Trust remote code (e.g., from HuggingFace) when downloading the model and tokenizer\n    tensor_parallel_size=4,  # The number of GPUs to use for distributed execution with tensor parallelism\n    gpu_memory_utilization=0.91,  # The ratio (between 0 and 1) of GPU memory to reserve for the model\n    seed=1,\n)","metadata":{"execution":{"iopub.status.busy":"2025-08-05T19:55:57.277254Z","iopub.execute_input":"2025-08-05T19:55:57.277417Z","iopub.status.idle":"2025-08-05T20:00:55.91817Z","shell.execute_reply.started":"2025-08-05T19:55:57.277402Z","shell.execute_reply":"2025-08-05T20:00:55.917542Z"},"papermill":{"duration":255.50413,"end_time":"2025-03-08T18:06:38.480048","exception":false,"start_time":"2025-03-08T18:02:22.975918","status":"completed"},"scrolled":true,"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"tokenizer = llm.get_tokenizer()\n\ndef count_tokens(text: str) -> int:\n    return len(tokenizer.encode(text))","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:55.918796Z","iopub.execute_input":"2025-08-05T20:00:55.91899Z","iopub.status.idle":"2025-08-05T20:00:55.922307Z","shell.execute_reply.started":"2025-08-05T20:00:55.918974Z","shell.execute_reply":"2025-08-05T20:00:55.921815Z"},"papermill":{"duration":0.026711,"end_time":"2025-03-08T18:06:38.526711","exception":false,"start_time":"2025-03-08T18:06:38.5","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Context retrieval function","metadata":{}},{"cell_type":"markdown","source":"## Regex base parse","metadata":{}},{"cell_type":"code","source":"import os\nimport re\nimport ast\nimport tokenize\nfrom io import StringIO\nimport unidiff\nimport subprocess\nimport shutil\nimport threading\nfrom typing import List, Dict, Optional, Tuple\nfrom concurrent.futures import ThreadPoolExecutor, as_completed\nfrom pathlib import Path\n\nclass HiddenPrints:\n    def __enter__(self):\n        self._original_stdout = sys.stdout\n        sys.stdout = open(os.devnull, 'w')\n\n    def __exit__(self, exc_type, exc_val, exc_tb):\n        sys.stdout.close()\n        sys.stdout = self._original_stdout\n\ndef extract_err_msg(trace_err):\n    with HiddenPrints():\n        _, err_lines_startswith_file = parse_error_locations(trace_err)\n        sub_tb, err_msg = parse_sub_trace(trace_err, err_lines_startswith_file, return_err_msg=True)\n    err_msg = err_msg.split('\\n')\n    for i, r in enumerate(err_msg):\n        if r.lstrip()==r:\n            err_msg = err_msg[i:]\n            break\n    return err_msg\n\ndef stringify_directory(directory, return_list=False):\n    full_paths = []\n\n    rel_path_start = len(directory) + 1\n    for root, dirs, files in os.walk(directory):\n        for file in files:\n            full_path = os.path.join(root, file)\n            full_paths.append(str(full_path))\n    if return_list:\n        return full_paths\n    full_paths = [x for x in full_paths if 'test' not in x]\n    full_paths = [x for x in full_paths if '.txt' not in x]\n    full_paths = [x for x in full_paths if '.md' not in x]\n    full_paths = [x for x in full_paths if '.github' not in x]\n    full_paths = [x for x in full_paths if '/doc/' not in x]\n    full_paths = [x for x in full_paths if '/docs/' not in x]\n    full_paths = [x for x in full_paths if '/examples/' not in x]\n    full_paths = [x for x in full_paths if 'venv' not in x]\n    full_paths = [x for x in full_paths if not x.endswith('.cpp')]\n    full_paths = [x for x in full_paths if not x.endswith('.c')]\n    full_paths = [x for x in full_paths if not x.endswith('.h')]\n    full_paths = [x for x in full_paths if not x.endswith('.in')]\n    full_paths = [x for x in full_paths if not x.endswith('.sh')]\n    full_paths = [x for x in full_paths if not x.endswith('.cu')]\n    full_paths = [x for x in full_paths if not x.endswith('.ipynb')]\n    full_paths = [x for x in full_paths if not x.endswith('.rst')]\n    # full_paths = [x for x in full_paths if not x.endswith('.venv')]\n    full_paths = [x for x in full_paths if not x.endswith('.pyc')]\n    full_paths = [x for x in full_paths if not x.endswith('.png')]\n    full_paths = [x for x in full_paths if not x.endswith('.jpg')]\n    full_paths = [x for x in full_paths if not x.endswith('.jpeg')]\n    full_paths = [x for x in full_paths if not x.endswith('.webm')]\n    return \"\\n\".join(full_paths)\n\ndef parse_error_locations(error_log):\n    \"\"\"\n    Parse error logs to extract file paths and function names from both Python 3.11 and 3.12 formats.\n\n    Python 3.11 format: File path:line, in function\n    Python 3.12 format: File \"path\", line line, in function\n\n    Args:\n        error_log (str): The error log text to parse\n\n    Returns:\n        dict: Dictionary with file paths as keys and lists of function names as values\n    \"\"\"\n    # Two patterns to match both formats\n    patterns = [\n        # Python 3.11 format: File path:line, in function\n        r'[ \\t]*File ([^:]+):(\\d+), in ([^\\n\\r]+)',\n\n        # Python 3.12 format: File \"path\", line line, in function\n        r'[ \\t]*File [\"\"]([^\"]+)[\"\"], line (\\d+), in ([^\\n\\r]+)'\n    ]\n\n    results = {}\n    err_lines = []\n    for pattern in patterns:\n        matches = re.finditer(pattern, error_log)\n\n        for match in matches:\n            err_lines.append(match.group(0))\n            line_err = match.group(2)\n            file_path = match.group(1).strip()\n            function_name = match.group(3).strip()\n\n            # Initialize list for new file paths\n            if file_path not in results:\n                results[file_path] = {}\n\n            # Add function name if not already present\n            if function_name and function_name not in results[file_path]:\n                results[file_path][function_name] = line_err\n\n    return results, err_lines\n\ndef find_new_paths(old_paths_dict, new_env_paths):\n    \"\"\"\n    Map old file paths to new paths by finding the best matching path based on\n    reversed string matching, which handles different path formats.\n\n    Args:\n        old_paths_dict (dict): Dictionary with old paths as keys and function names as values\n        new_env_paths (list): List of all available paths in the new environment\n\n    Returns:\n        dict: Dictionary with old paths as keys and corresponding new paths as values\n    \"\"\"\n    def normalize_path(path):\n        \"\"\"Normalize path by replacing backslashes and removing leading/trailing spaces\"\"\"\n        return path.replace('\\\\', '/').strip()\n\n    def reverse_string(s):\n        \"\"\"Reverse a string\"\"\"\n        return s[::-1]\n\n    def common_suffix_length(str1, str2):\n        \"\"\"Find length of common suffix between two strings\"\"\"\n        str1_rev = reverse_string(str1.lower())  # Convert to lowercase for case-insensitive matching\n        str2_rev = reverse_string(str2.lower())\n\n        common_length = 0\n        for c1, c2 in zip(str1_rev, str2_rev):\n            if c1 != c2:\n                break\n            common_length += 1\n\n        return common_length\n\n    result = {}\n\n    # Normalize and prepare paths\n    normalized_old_paths = {normalize_path(path): path for path in old_paths_dict.keys()}\n    normalized_new_paths = [normalize_path(path) for path in new_env_paths]\n\n    # Find best matches\n    for norm_old_path, original_old_path in normalized_old_paths.items():\n        print('EVAL', original_old_path)\n        best_match = None\n        best_length = 0\n\n        for norm_new_path in normalized_new_paths:\n            common_length = common_suffix_length(norm_old_path, norm_new_path)\n\n            # Update best match if we find a longer common suffix\n            if common_length > best_length:\n                best_length = common_length\n                best_match = [path for path in new_env_paths\n                            if normalize_path(path) == norm_new_path][0]\n        print(best_match[-best_length:].count('/') >= 2)\n        if best_length > 0 and best_match[-best_length:].count('/') >= 2:  # Only include if we found a match\n            print('best_length', best_length)\n            print('best_match', best_match)\n            test_trace = old_paths_dict[original_old_path]\n            err_line = int(test_trace[list(test_trace.keys())[0]])\n            print('err_line', err_line)\n            err_str = list(test_trace.keys())[0]\n            with open(best_match, 'r', encoding='utf-8') as f:\n                lines = f.readlines()\n                f.seek(0)\n                txt = f.read()\n                if err_line<len(lines):\n                    # print(lines[err_line-1])\n                    print('err_str', err_str)\n                    # print('txt', txt)\n                    print(err_str.strip() in txt)\n                    result[original_old_path] = best_match\n                else:\n                    print('DIFF TRACE - MAX LENGTH')\n                    result[original_old_path] = None\n                    # result[original_old_path] = best_match\n        else:\n            result[original_old_path] = None\n    return result\n\ndef get_indent_level(text: str, line_start: int | None = None) -> int:\n    for line in text.split('\\n')[line_start:]:\n        if line.strip():\n            return line.count('    ')\n    return 0\n\ndef parse_sub_trace(problem_state:str, err_lines_startswith_file:list, return_err_msg=False) -> dict:\n    problem_state = problem_state.replace('\\r', '')\n    err_lines_list = problem_state.split('\\n')\n    # err_lines_list = [x.strip() for x in err_lines_list]\n    print(err_lines_list)\n    if not err_lines_startswith_file:\n        return None\n    idx = []\n    for line in err_lines_startswith_file:\n        try:\n            idx.append(err_lines_list.index(line))\n        except:\n            print('BUG in def parse_sub_trace')\n    idx.append(len(err_lines_list))\n    if not idx:\n        return None\n    print('idx', idx)\n    parse_dict = {}\n    for i, line in enumerate(err_lines_startswith_file):\n        parse_dict[line] = err_lines_list[idx[i]:idx[i+1]]\n    last_arrow_trace_idx = idx[-2]\n    for i, line in enumerate(err_lines_list[idx[-2]:]):\n        if re.match(r'^-*>\\s?.*$', line):\n            last_arrow_trace_idx = i+idx[-2]\n            break\n    else:\n        if idx[-2]<idx[-1]:\n            last_arrow_trace_idx +=1\n\n    print('last_arrow_trace_idx', last_arrow_trace_idx)\n    if last_arrow_trace_idx==len(err_lines_list):\n        return parse_dict\n    last_trace_row_idx = idx[-1]\n    for i, line in enumerate(err_lines_list[last_arrow_trace_idx+1:]):\n        print('line', line)\n        if line.strip():\n            if len(line.lstrip())==len(line):\n                last_trace_row_idx = i+last_arrow_trace_idx\n                idx[-1] = i+last_arrow_trace_idx\n                break\n    print(idx)\n    parse_dict[err_lines_startswith_file[-1]] = err_lines_list[idx[-2]:idx[-1]]\n    if return_err_msg:\n        return parse_dict, '\\n'.join(err_lines_list[idx[-1]:])\n    return parse_dict\n\ndef get_selection_query_x(directory, problem_statement):\n    test_old_paths, err_lines_startswith_file = parse_error_locations(problem_statement)\n    print('err_lines_startswith_file', err_lines_startswith_file)\n    print('test_old_paths', test_old_paths)\n    new_paths = stringify_directory(directory, return_list=True)\n    old2new_path = find_new_paths(test_old_paths, new_paths)\n    print('old2new_path', old2new_path)\n    new_trace = {}\n    for k, v in test_old_paths.items():\n        if old2new_path[k]:\n            new_trace[old2new_path[k]] = v\n\n    directory_string = '\\n'.join(list(new_trace.keys()))\n    x = parse_sub_trace(problem_statement, err_lines_startswith_file)\n    print('parse_sub_trace x', x)\n    if not x:\n        return {}\n    search_str = {}\n    for k, v in x.items():\n        for xx in v:\n            if re.match(r'^-*>\\s?.*$', xx):\n                search_str[k] = xx.split(' ', maxsplit=2)[-1]\n                break\n        else:\n            search_str[k] = v[1]\n    search_str.values()\n\n    map_x = {v: [] for v in old2new_path.values() if v}\n    for i, line in enumerate(list(search_str.keys())):\n        if not line.startswith(err_lines_startswith_file[i]):\n            break\n    else:\n        fake_xml = search_str\n        for i, line in enumerate(list(search_str.keys())):\n            for k, v in old2new_path.items():\n                if k in line:\n                    if v:\n                        map_x[v].append(search_str[line])\n    return map_x\n\ndef is_valid_patch_format(patch_string: str) -> bool:\n    \"\"\"\n    A quick check to confirm if a patch could be valid.\n    \"\"\"\n    if not(isinstance(patch_string, str)):\n        return False\n    try:\n        patch_set = unidiff.PatchSet(patch_string)\n        if len(patch_set) == 0:\n            return False\n    except Exception:\n        return False\n    return True\n\ndef fix_num_patch(patch):\n    if is_valid_patch_format(patch):\n        return patch\n    patch = patch.strip()\n    RE_HUNK_HEADER = re.compile(r\"^@@ -(\\d+)(?:,(\\d+))? \\+(\\d+)(?:,(\\d+))?\\ @@[ ]?(.*)\")\n    lines = patch.split('\\n')\n    start_patch_idx = []\n    for i, line in enumerate(lines):\n        if line.startswith('---'):\n            start_patch_idx.append(i)\n    start_patch_idx.append(len(lines))\n    if len(start_patch_idx)!=2:\n        return patch\n    fixed_patch = []\n    for i in range(len(start_patch_idx)-1):\n        sub_patch_lines = lines[start_patch_idx[i]:start_patch_idx[i+1]]\n        if len(start_patch_idx)!=2:\n            sub_patch_lines = [i for i in sub_patch_lines if i.strip()]\n        sub_patch_lines_fixed = sub_patch_lines.copy()\n        for i, line in enumerate(sub_patch_lines):\n            is_hunk_header = RE_HUNK_HEADER.match(line)\n            if is_hunk_header:\n                hunk_info = is_hunk_header.groups()\n                src_start, source_line_no, tgt_start, target_line_no = map(int, hunk_info[:-1])\n                section_header = hunk_info[-1]\n                end_line_no = 0\n                src_line_no = 0\n                if i+1!=len(sub_patch_lines):\n                    for idx, linex in enumerate(sub_patch_lines[i+1:]):\n                        if linex.startswith('+'):\n                            end_line_no +=1\n                        elif linex.startswith('-'):\n                            src_line_no +=1\n                        else:\n                            end_line_no +=1\n                            src_line_no +=1\n                    if src_line_no!=source_line_no:\n                        print(f'DETECT DIFF SRC LINE {source_line_no} -> {src_line_no}')\n                    if end_line_no!=target_line_no:\n                        print(f'DETECT DIFF TARGET LINE {target_line_no} -> {end_line_no}')\n                    sub_patch_lines_fixed[i] = f\"@@ -{src_start},{src_line_no} +{tgt_start},{end_line_no} @@ {section_header}\"\n                    print('sub_patch_lines_fixed', sub_patch_lines_fixed)\n                    fixed_patch.append('\\n'.join(sub_patch_lines_fixed))\n                    break\n    return '\\n\\n'.join(fixed_patch)\n\ndef gen_param(i, temp=1.0):\n    sampling_params = SamplingParams(\n    temperature=temp,              # Controls randomness in generation: higher values (e.g., 1.0) produce more diverse output.\n    min_p=0.01,\n    # skip_special_tokens=True,\n    # max_tokens=1800,\n    max_tokens=4096*2,             # Sets a very high limit for token generation to handle longer outputs.\n    # stop=[\"```output\"],\n    seed=1+i*133*2,\n    top_k=40,\n    top_p=0.95,\n    # stop=[\"<｜end▁of▁sentence｜>\", \"<｜User｜>\"],\n    # stop_token_ids=[151643, 151644]\n    )\n\n    return sampling_params","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:55.922956Z","iopub.execute_input":"2025-08-05T20:00:55.923135Z","iopub.status.idle":"2025-08-05T20:00:55.958359Z","shell.execute_reply.started":"2025-08-05T20:00:55.92312Z","shell.execute_reply":"2025-08-05T20:00:55.95776Z"},"papermill":{"duration":0.061098,"end_time":"2025-03-08T18:06:38.607506","exception":false,"start_time":"2025-03-08T18:06:38.546408","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def extract_file_query(xml_content: str) -> Dict[str, List[str]]:\n    import xml.etree.ElementTree as ET\n\n    # Prepare a data structure to collect results\n    parsed_data: Dict[str, List[str]] = {}\n    pattern: str = r\"<root>(.*?)</root>\"\n    matches: List[str] = re.findall(pattern, xml_content, re.DOTALL)\n\n    for match in matches:\n        try:\n            # Parse the XML\n            root = ET.fromstring(\"<root>\" + match + \"</root>\")\n\n            # Find all <entry> elements\n            for entry in root.findall(\"entry\"):\n                # Extract the <filepath> text\n                filepath = entry.find(\"filepath\")\n                filepath_text: Optional[str] = (\n                    filepath.text.strip()\n                    if filepath is not None and filepath.text is not None\n                    else None\n                )\n\n                # Locate <strings_to_search> container\n                strings_container = entry.find(\"strings_to_search\")\n\n                # Gather each <string_to_search> text\n                search_strings: List[str] = []\n                if strings_container is not None:\n                    for s in strings_container.findall(\"string_to_search\"):\n                        if s.text is not None:\n                            search_strings.append(s.text.strip())\n\n                # Store in a dictionary: { filepath: [search_strings...] }\n                parsed_data[filepath_text] = search_strings  # type: ignore\n        except:\n            print(\"Error parsing output\")\n            print(xml_content)\n            return {}\n\n    return parsed_data","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:55.958909Z","iopub.execute_input":"2025-08-05T20:00:55.959074Z","iopub.status.idle":"2025-08-05T20:00:55.96895Z","shell.execute_reply.started":"2025-08-05T20:00:55.95906Z","shell.execute_reply":"2025-08-05T20:00:55.968453Z"},"papermill":{"duration":0.026844,"end_time":"2025-03-08T18:06:38.653686","exception":false,"start_time":"2025-03-08T18:06:38.626842","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## LLM base search","metadata":{}},{"cell_type":"code","source":"reading_prompt: str = (\n    \"\"\"\nYou will be implementing a git diff patch to solve an issue with the code repository.\nYou will first need to select files in the file directory.\n\nThis is the problem statement.\n\n{problem_statement}\n\nThis is the file directory\n\n<directory>\n{directory_string}\n</directory>\n\nWhich files should be inspected so that we can solve the problem?\nWhen we inspect each file, what strings should be searched?\n\nReturn the strings to search in this format\n\n(explanation)\n\n<root>\n    <entry>\n        <filepath>filepath</filepath>\n        <strings_to_search>\n            <string_to_search>string_to_search</string_to_search>\n            ...\n            <string_to_search>string_to_search</string_to_search>\n        </strings_to_search>\n    </entry>\n    <entry>\n        <filepath>filepath</filepath>\n        <strings_to_search>\n            <string_to_search>string_to_search</string_to_search>\n            ...\n            <string_to_search>string_to_search</string_to_search>\n        </strings_to_search>\n    </entry>\n    ...\n</root>\n...\n\nNotes:\n- Make sure to encode each entry between <root> and </root>\n- Return the FULL filepath - exactly as specified in <directory> and </directory>\n    - Example: <filepath>repo/path/to/directory/file.py</filepath>\n- If you are searching for a word instead of a substring, maybe add spaces or brackets before and after the string\n    - For example, if you are searching for uses of the function `calculate`, use ` calculate(` as the search string instead of `calculate`\n- Prefer searching longer strings\n    - Avoid searching for strings that might appear in many parts of the codebase\n- Search the test files as well to understand the feature behavior\n    - Also search for the relevant function calls in the test files\n\"\"\".strip()\n)\n\n\ndef get_selection_query(\n    directory_string: str, problem_statement: str\n) -> Tuple[List[str], List[Dict[str, List[str]]]]:\n    sampling_params: SamplingParams = SamplingParams(\n        temperature=0.6,  # randomness of the sampling\n        min_p=0.01,\n        skip_special_tokens=True,  # Whether to skip special tokens in the output\n        max_tokens=MAX_TOKENS,\n    )\n\n    list_of_messages: List[List[Dict[str, str]]] = [\n        [\n            {\n                \"role\": \"user\",\n                \"content\": reading_prompt.format(\n                    problem_statement=problem_statement[:20_000],\n                    directory_string=directory_string[:30_000],\n                ),\n            },\n        ]\n        for _ in range(BATCH_SIZE)\n    ]\n\n    prompt_texts: List[str] = [\n            tokenizer.apply_chat_template(\n                conversation=messages, tokenize=False, add_generation_prompt=True\n            )  # type: ignore\n        for messages in list_of_messages\n    ]\n    # print(prompt_texts)\n\n    print(\"get_selection_query input\", [count_tokens(text) for text in prompt_texts])\n    request_outputs: list[RequestOutput] = llm.generate(\n        prompt_texts,\n        sampling_params=[gen_param(i) for i in range(len(prompt_texts))]\n    )\n    if not request_outputs:\n        return [], []\n    response_texts: List[str] = [\n        request_output.outputs[0].text for request_output in request_outputs\n    ]\n    print(\"get_selection_query output\", [count_tokens(text) for text in response_texts])\n\n    completion_texts = [\n        prompt_text + response_text\n        for prompt_text, response_text in zip(prompt_texts, response_texts)\n    ]\n    file_queries: List[Dict[str, List[str]]] = [\n        extract_file_query(response_text) for response_text in response_texts\n    ]\n    completion_texts = [x for i, x in enumerate(completion_texts) if file_queries[i]]\n    file_queries = [x for x in file_queries if x]\n    return completion_texts, file_queries","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:55.969534Z","iopub.execute_input":"2025-08-05T20:00:55.969702Z","iopub.status.idle":"2025-08-05T20:00:55.980755Z","shell.execute_reply.started":"2025-08-05T20:00:55.969689Z","shell.execute_reply":"2025-08-05T20:00:55.980283Z"},"papermill":{"duration":0.029153,"end_time":"2025-03-08T18:06:38.70218","exception":false,"start_time":"2025-03-08T18:06:38.673027","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Fetch content function","metadata":{}},{"cell_type":"code","source":"REPO_PATH: str = \"repo\"\n\n\ndef fetch_file_contents(\n    files_to_search: Dict[str, List[str]], context_lines: int = 15, bottom_context_lines: int = 5, max_gap: int = 0, rp_path=''\n) -> str:\n    from io import StringIO\n    from typing import Tuple\n\n    def find_lines_in_files_with_context(\n        search_map: Dict[str, List[str]], context_lines: int = context_lines, bottom_context_lines: int = 5\n    ) -> List[List[List[Tuple[int, str]]]]:\n        \"\"\"\n        Given a dictionary mapping file paths to a list of search terms,\n        open each file and gather *snippets* of lines that contain any\n        of those search terms, including 'context_lines' before and after.\n\n        Returns a list of lists:\n        [\n          [  # For file1\n             [ (line_number, text), (line_number, text), ... ],\n             [ ... ],\n          ],\n          [  # For file2\n             ...\n          ],\n          ...\n        ]\n        \"\"\"\n        all_matches_per_file: List[List[List[Tuple[int, str]]]] = []\n\n        for path, terms in search_map.items():\n            if not os.path.isfile(path):\n                # If the file is not found, record an empty list\n                all_matches_per_file.append([])\n                continue\n\n            with open(path, \"r\", encoding=\"utf-8\", errors=\"replace\") as f:\n                lines = f.readlines()\n\n            file_snippets: List[List[Tuple[int, str]]] = []\n            num_lines: int = len(lines)\n\n            for i, line in enumerate(lines, start=1):\n                if any(t in line for t in terms):\n                    start_idx: int = max(1, i - context_lines)\n                    end_idx: int = min(num_lines, i + bottom_context_lines)\n                    snippet: List[Tuple[int, str]] = []\n                    for snippet_no in range(start_idx, end_idx + 1):\n                        text_content: str = lines[snippet_no - 1].rstrip(\"\\n\")\n                        snippet.append((snippet_no, text_content))\n                    file_snippets.append(snippet)\n\n            all_matches_per_file.append(file_snippets)\n\n        return all_matches_per_file\n\n    # ---------------------------------------------------------\n    # 3. MERGE OVERLAPPING/ADJACENT SNIPPETS\n    # ---------------------------------------------------------\n\n    def merge_file_snippets(\n        file_snippets: List[List[Tuple[int, str]]], gap: int = 0\n    ) -> List[List[Tuple[int, str]]]:\n        \"\"\"\n        Merge overlapping or nearly adjacent snippets in a single file’s snippet list.\n        \"\"\"\n        intervals: List[Tuple[int, int, List[Tuple[int, str]]]] = []\n        for snippet in file_snippets:\n            if snippet:\n                start_line: int = snippet[0][0]\n                end_line: int = snippet[-1][0]\n                intervals.append((start_line, end_line, snippet))\n\n        intervals.sort(key=lambda x: x[0])  # sort by start line\n\n        merged: List[Tuple[int, int, List[Tuple[int, str]]]] = []\n        for start, end, snippet in intervals:\n            if not merged:\n                merged.append((start, end, snippet))\n                continue\n\n            prev_start, prev_end, prev_snippet = merged[-1]\n            if start <= prev_end + gap:\n                new_end: int = max(end, prev_end)\n                combined_dict: Dict[int, str] = {}\n                for ln, txt in prev_snippet:\n                    combined_dict[ln] = txt\n                for ln, txt in snippet:\n                    combined_dict[ln] = txt\n                merged_snippet: List[Tuple[int, str]] = [\n                    (ln, combined_dict[ln]) for ln in sorted(combined_dict)\n                ]\n                merged[-1] = (prev_start, new_end, merged_snippet)\n            else:\n                merged.append((start, end, snippet))\n\n        # Extract just the merged snippet portion\n        return [x[2] for x in merged]\n\n    def merge_all_snippets(\n        all_files_snips: List[List[List[Tuple[int, str]]]], gap: int = 0\n    ) -> List[List[List[Tuple[int, str]]]]:\n        \"\"\"\n        Merge snippet blocks within each file.\n        all_files_snips is a list-of-lists:\n          [\n            [ snippetA, snippetB, ... ],  # file 1\n            [ snippetC, snippetD, ... ],  # file 2\n          ]\n        \"\"\"\n        merged: List[List[List[Tuple[int, str]]]] = []\n        for snips in all_files_snips:\n            merged.append(merge_file_snippets(snips, gap=gap))\n        return merged\n\n    # ---------------------------------------------------------\n    # 4. RUN LOGIC: generate files, search, merge, and BUILD A STRING\n    # ---------------------------------------------------------\n\n    has_any_matches: bool = False\n\n    # 1) Gather snippets around each match\n    context_snippets: List[List[List[Tuple[int, str]]]] = (\n        find_lines_in_files_with_context(files_to_search, context_lines=context_lines)\n    )\n\n    # 2) Merge overlapping snippets\n    merged_snips: List[List[List[Tuple[int, str]]]] = merge_all_snippets(\n        context_snippets, gap=max_gap\n    )\n\n    # 3) Build a string (instead of printing)\n    output = StringIO()\n\n    # Header\n    output.write(\"Sample files created successfully.\\n\\n\")\n    output.write(\"Search Results (by file, merging any overlapping context):\\n\\n\")\n\n    # For each file\n    for (filepath, terms), snippet_list in zip(files_to_search.items(), merged_snips):\n\n        if not snippet_list:\n            pass\n            # output.write(\"  No matches found.\\n\")\n        else:\n            # output.write(f\"[file name]: {filepath[len(REPO_PATH) + 1:]}\\n\")\n            output.write(f\"### {filepath[len(rp_path) + 1:]}\\n\")\n            terms_searched_as_str = \"\\n\".join(terms)\n            # output.write(f\"[terms searched]:\\n{terms_searched_as_str}\\n\")\n            # output.write(\"[file content begin]\\n\")\n            has_any_matches = True\n            for snippet_idx, snippet in enumerate(snippet_list, start=1):\n                snippet_start: int = snippet[0][0]\n                snippet_end: int = snippet[-1][0]\n                output.write(\n                    f\"\\nMatch #{snippet_idx}, lines {snippet_start} to {snippet_end}:\\n\"\n                )\n                for line_no, text in snippet:\n                    # output.write(f\"  {line_no:3d} | {text}\\n\")\n                    output.write(f\"{text}\\n\")\n                output.write(\"\\n\")\n            output.write(\"[file content end]\\n\\n\")\n\n    file_content_string: str = output.getvalue()\n\n    if has_any_matches:\n        return file_content_string\n    return \"\"","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:55.981366Z","iopub.execute_input":"2025-08-05T20:00:55.981675Z","iopub.status.idle":"2025-08-05T20:00:55.996589Z","shell.execute_reply.started":"2025-08-05T20:00:55.981661Z","shell.execute_reply":"2025-08-05T20:00:55.996139Z"},"papermill":{"duration":0.036805,"end_time":"2025-03-08T18:06:38.75842","exception":false,"start_time":"2025-03-08T18:06:38.721615","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Generate patch function","metadata":{}},{"cell_type":"markdown","source":"## Predict patch","metadata":{}},{"cell_type":"code","source":"def extract_patch_string(text: str) -> Optional[str]:\n    pattern: str = r\"\\n```diff\\n(.*?)\\n```\"\n    matches: List[str] = re.findall(pattern, text, re.DOTALL)\n    if not matches:\n        return None\n    return matches[-1] + \"\\n\"\n    \npatching_prompt: str = (\n\"\"\"\nYou will be implementing a git diff patch to solve an issue with the code repository.\nThis is the problem statement.\n\n{problem_statement}\n\nThese are the files that is thought to be relevant\n\n{file_content_string}\n\nWrite a git diff within ```diff and ``` that fully fixes the problem.\nThe git diff should not cause other tests to fail.\n\nExample:\n\n```diff\n--- a/first.txt\n+++ b/first.txt\n@@ -1,3 +1,3 @@\n start\n-first change\n+new first change\n middle\n@@ -7,4 +7,4 @@\n some content\n-second change\n+new second change\n more content\n--- a/second.txt\n+++ b/second.txt\n@@ -1,3 +1,3 @@\n beginning\n-old line\n+new line\n end\n```\n\nReminder\n- Put your diff within ```diff and ``` and make sure the diff is valid.\n- Only the last diff printed will be considered.\n\"\"\".strip()\n)\n\ngcm_prompt = \"\"\"We are currently solving the following issue within our repository. Here is the issue text:\n--- BEGIN ISSUE ---\n{problem_statement}\n--- END ISSUE ---\n\nBelow are some code segments, each from a relevant file. One or more of these files may contain bugs.\n\n--- BEGIN FILE ---\n```\n{file_content_string}\n```\n--- END FILE ---\n\nPlease first localize the bug based on the issue statement, and then generate *SEARCH/REPLACE* edits to fix the issue.\n\nEvery *SEARCH/REPLACE* edit must use this format:\n1. The file path\n2. The start of search block: <<<<<<< SEARCH\n3. A contiguous chunk of lines to search for in the existing source code\n4. The dividing line: =======\n5. The lines to replace into the source code\n6. The end of the replace block: >>>>>>> REPLACE\n\nHere is an example:\n\n```python\n### mathweb/flask/app.py\n<<<<<<< SEARCH\nfrom flask import Flask\n=======\nimport math\nfrom flask import Flask\n>>>>>>> REPLACE\n```\n\nPlease note that the *SEARCH/REPLACE* edit REQUIRES PROPER INDENTATION. If you would like to add the line '        print(x)', you must fully write that out, with all those spaces before the code!\nWrap each *SEARCH/REPLACE* edit in a code block as shown in the example above. If you have multiple *SEARCH/REPLACE* edits, use a separate code block for each one.\"\"\"\n\nimport re\n\n\ndef get_patch_string(\n    problem_statement: str, file_content_strings: List[str], use_gcm=False\n) -> Tuple[List[str], List[Optional[str]]]:\n    sampling_params: SamplingParams = SamplingParams(\n        temperature=0.6,  # randomness of the sampling\n        min_p=0.01,\n        skip_special_tokens=True,  # Whether to skip special tokens in the output\n        max_tokens=MAX_TOKENS,\n    )\n\n    inference_idx_to_input_idx: list[int] = [\n        input_idx\n        for input_idx, file_content_string in enumerate(file_content_strings)\n        if file_content_string != \"\"\n    ]\n    x_prompt = patching_prompt\n    if use_gcm:\n        x_prompt = gcm_prompt\n\n    list_of_messages: List[List[Dict[str, str]]] = [\n        [\n            {\n                \"role\": \"user\",\n                \"content\": x_prompt.format(\n                    problem_statement=problem_statement[:20_000],\n                    file_content_string=file_content_strings[input_idx][:30_000],\n                ),\n            },\n        ]\n        for input_idx in inference_idx_to_input_idx\n    ]\n\n    prompt_texts: List[str] = [\n            tokenizer.apply_chat_template(\n                conversation=messages, tokenize=False, add_generation_prompt=True\n            )  # type: ignore\n        for messages in list_of_messages\n    ]\n    # print(prompt_texts)\n\n    print(\"get_patch_string input\", [count_tokens(text) for text in prompt_texts])\n    request_outputs: list[RequestOutput] = llm.generate(\n        prompt_texts,\n        sampling_params=[gen_param(i) for i in range(len(prompt_texts))]\n    )\n    response_texts_from_inference: List[str] = [\n        request_output.outputs[0].text for request_output in request_outputs\n    ]\n    print(\n        \"get_patch_string output\",\n        [count_tokens(text) for text in response_texts_from_inference],\n    )\n\n    return response_texts_from_inference","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:55.997099Z","iopub.execute_input":"2025-08-05T20:00:55.997257Z","iopub.status.idle":"2025-08-05T20:00:56.009195Z","shell.execute_reply.started":"2025-08-05T20:00:55.997244Z","shell.execute_reply":"2025-08-05T20:00:56.008696Z"},"papermill":{"duration":0.029219,"end_time":"2025-03-08T18:06:39.086797","exception":false,"start_time":"2025-03-08T18:06:39.057578","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## GCM helper","metadata":{}},{"cell_type":"code","source":"from collections import OrderedDict\nimport uuid\n\ndef extract_python_blocks(text):\n    # Regular expression pattern to match ```python\\n{text}\\n```\n    pattern = r\"```python\\n(.*?)\\n```\"\n\n    # Use re.findall to find all matches\n    matches = re.findall(pattern, text, re.DOTALL)\n\n    return matches\n\ndef split_edit_multifile_commands(commands: list[str]) -> dict[str, list[str]]:\n    \"\"\"Split commands based on edited files.\"\"\"\n    file_to_commands = OrderedDict()  # type: ignore\n    for command in commands:\n        file_name = None\n        for subcommand in command.split(\">>>>>>> REPLACE\")[:-1]:\n            subcommand = subcommand.strip()\n            if \"<<<<<<< SEARCH\" in subcommand:\n                fn = subcommand.split(\"<<<<<<< SEARCH\")[0].lstrip(\"#\").strip()\n                if fn:\n                    file_name = fn\n\n            if len(subcommand.split(\"<<<<<<< SEARCH\")) != 2:\n                continue\n            converted_command = (\n                \"<<<<<<< SEARCH\"\n                + subcommand.split(\"<<<<<<< SEARCH\")[1]\n                + \"\\n\"\n                + \">>>>>>> REPLACE\"\n            )\n            # deduplicate\n            if file_name is not None and (\n                file_name not in file_to_commands\n                or converted_command not in file_to_commands[file_name]\n            ):\n                file_to_commands.setdefault(file_name, []).append(converted_command)\n    return file_to_commands\n\ndef parse_diff_edit_commands(commands: list[str], content: str) -> str:\n    replaced = False\n    # apply the edits from the end of file to the beginning of file\n    # this is to make sure context is correct\n    # since we want to replace the original context, let's first check for all edits.\n    can_apply = []\n    for subcommand in commands:\n        if not subcommand.startswith(\"<<<<<<< SEARCH\") and subcommand.endswith(\n            \">>>>>>> REPLACE\"\n        ):\n            continue\n\n        subcommand = \"\\n\".join(subcommand.splitlines()[1:-1])\n        if len(subcommand.split(\"\\n=======\\n\")) != 2:\n            continue\n\n        original, replace = subcommand.split(\"\\n=======\\n\")\n\n        if original in content:\n            can_apply.append(subcommand)\n\n    # apply edits backwards\n    # for subcommand in can_apply[::-1]:\n    # NOTE(yuxiang): 02/16, not needed; just apply them forwards\n    for subcommand in can_apply:\n        original, replace = subcommand.split(\"\\n=======\\n\")\n        content = content.replace(original, replace)\n        # print('original\\n', original)\n        # print('replace\\n', replace)\n        replaced = True\n\n    if not replaced:\n        print(\"not replaced\")\n\n    return content\n\n\ndef fake_git_repo(repo_playground, file_pathes, old_contents, new_contents) -> str:\n    \"\"\"create a fake git repo to obtain git diff format\"\"\"\n\n    if not isinstance(file_pathes, list):\n        # for backwards compatibility\n        file_pathes = [file_pathes]\n        old_contents = [old_contents]\n        new_contents = [new_contents]\n\n    # Generate a temperary folder and add uuid to avoid collision\n    repo_playground = os.path.join(repo_playground, str(uuid.uuid4()))\n\n    # assert playground doesn't exist\n    assert not os.path.exists(repo_playground), f\"{repo_playground} already exists\"\n\n    # create playground\n    os.makedirs(repo_playground)\n\n    # create a fake git repo\n    subprocess.run(f\"cd {repo_playground} && git init\", shell=True)\n\n    for file_path, old_content, new_content in zip(\n        file_pathes, old_contents, new_contents\n    ):\n        # create a file\n        subprocess.run(\n            f\"mkdir -p {repo_playground}/{os.path.dirname(file_path)}\", shell=True\n        )\n\n        with open(f\"{repo_playground}/{file_path}\", \"w\") as f:\n            f.write(old_content)\n\n        # add file to git\n        # same message is okay\n        subprocess.run(\n            f\"cd {repo_playground} && git add {file_path} && git commit -m 'initial commit'\",\n            shell=True,\n        )\n\n    for file_path, old_content, new_content in zip(\n        file_pathes, old_contents, new_contents\n    ):\n        # edit file\n        with open(f\"{repo_playground}/{file_path}\", \"w\") as f:\n            f.write(new_content)\n\n    # get git diff\n    o = subprocess.run(\n        f\"cd {repo_playground} && git diff .\", shell=True, capture_output=True\n    )\n\n    s = o.stdout.decode(\"utf-8\")\n\n    # remove playground\n    subprocess.run(f\"rm -rf {repo_playground}\", shell=True)\n\n    return s\n\ndef check_syntax(code):\n    if not isinstance(code, list):\n        code = [code]\n\n    for c in code:\n        if (\n            not c.strip()\n        ):  # Check for cases where the model didn't return a python block\n            return False\n        try:\n            ast.parse(c)\n        except SyntaxError as e:\n            return False\n    return True\n\ndef remove_empty_lines(code: str) -> str:\n    # Split the code into lines\n    lines = code.splitlines()\n    # Remove empty lines\n    filtered_lines = [line for line in lines if line.strip() != \"\"]\n    return \"\\n\".join(filtered_lines)\n\ndef check_code_differ_by_just_empty_lines(codes, prev_codes) -> bool:\n\n    if not isinstance(codes, list):\n        codes = [codes]\n        prev_codes = [prev_codes]\n\n    normalized_code1 = \"\"\n    normalized_code2 = \"\"\n\n    for code, prev_code in zip(codes, prev_codes):\n        # Normalize both code snippets\n        normalized_code1 += remove_empty_lines(code)\n        normalized_code2 += remove_empty_lines(prev_code)\n\n    return normalized_code1 == normalized_code2\n\ndef _post_process_multifile_repair(\n    raw_output: str, file_contents: dict[str, str]\n) -> tuple[list[str], list[str]]:\n    edit_multifile_commands = extract_python_blocks(raw_output)\n    edited_files = list[str]()\n    new_contents = list[str]()\n    file_to_commands = split_edit_multifile_commands(edit_multifile_commands)\n    # print('file_to_commands', file_to_commands)\n    for edited_file_key in file_to_commands:\n        edited_file = \"\"\n        new_content = \"\"\n        edit_commands = file_to_commands[edited_file_key]\n        edited_file = edited_file_key\n        if edited_file not in file_contents:\n            continue\n\n        content = file_contents[edited_file]\n        new_content = parse_diff_edit_commands(edit_commands, content)\n\n        if edited_file == \"\" or new_content == \"\":\n            continue\n        edited_files.append(edited_file)\n        new_contents.append(new_content)\n\n    return edited_files, new_contents\n\ndef post_process_raw_output(raw_output_text: str, file_contents: dict[str, str]):\n    git_diffs = \"\"\n    raw_git_diffs = \"\"\n    edited_files = list[str]()\n    new_contents = list[str]()\n    contents = list[str]()\n    edited_files, new_contents = _post_process_multifile_repair(\n        raw_output_text, file_contents\n    )\n    print('edited_files', edited_files)\n    # print('new_contents', new_contents)\n    contents = [file_contents[edited_file] for edited_file in edited_files]\n\n    git_diff = fake_git_repo(\n        'playground', edited_files, contents, new_contents\n    )\n\n    raw_git_diffs += \"\\n\" + git_diff.replace(\"\\\\ No newline at end of file\\n\", \"\")\n\n    syntax_success = check_syntax(new_contents)\n\n    differ_by_empty_lines = check_code_differ_by_just_empty_lines(\n        new_contents, contents\n    )\n    print('syntax_success', syntax_success)\n    if syntax_success and not differ_by_empty_lines:\n        git_diffs = raw_git_diffs\n    else:\n        if differ_by_empty_lines:\n            print('differ_by_empty_lines_err')\n        else:\n            print('WRONG SYNTAX')\n        git_diffs = \"\"  # no need to evaluate\n\n    return git_diffs, raw_git_diffs, contents, edited_files, new_contents\n\ndef path2content(file_paths, split_by:str) -> dict:\n    file_contents = {}\n    for file_path in file_paths:\n        if os.path.isfile(file_path):\n            with open(file_path, 'r') as f:\n                file_content = f.read()\n            file_contents[file_path.split(split_by+'/', maxsplit=1)[-1]] = file_content\n    return file_contents","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:56.009793Z","iopub.execute_input":"2025-08-05T20:00:56.009957Z","iopub.status.idle":"2025-08-05T20:00:56.027857Z","shell.execute_reply.started":"2025-08-05T20:00:56.009945Z","shell.execute_reply":"2025-08-05T20:00:56.027394Z"},"papermill":{"duration":0.040537,"end_time":"2025-03-08T18:06:39.288969","exception":false,"start_time":"2025-03-08T18:06:39.248432","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Predict funtion","metadata":{}},{"cell_type":"code","source":"def predict_inner(problem_statement: str, directory: str, return_all=False, use_gcm=False) -> Optional[str]:\n    try:\n        r, err_line = parse_error_locations(problem_statement)\n        if not r:\n            return [None], [None]\n    except:\n        pass\n    directory_string = stringify_directory(directory)\n\n    try:\n        with HiddenPrints():\n            file_querie = get_selection_query_x(directory, problem_statement)\n        print('file_querie', file_querie)\n        file_queries = [file_querie for _ in range(BATCH_SIZE)]\n        if not file_querie:\n            selection_completion_texts, file_queries = get_selection_query(\n        directory_string, problem_statement\n    )\n\n    except:\n        print('ERROR XX')\n        selection_completion_texts, file_queries = get_selection_query(\n        directory_string, problem_statement\n    )\n\n    try:\n        file_content_strings: List[str] = [\n            fetch_file_contents(file_query, rp_path=directory) for file_query in file_queries\n        ]\n        # print('file_content_strings\\n', file_content_strings)\n        patch_response_texts = get_patch_string(\n            problem_statement, file_content_strings, use_gcm=use_gcm\n        )\n    except:\n        return [None], [None]\n    if use_gcm:\n        try:\n            search_maps = []\n            tmp_maps = [list(file_querie.keys()) for file_querie in file_queries]\n            for tmp_map in tmp_maps:\n                search_maps.extend(tmp_map)\n            search_maps = list(set(search_maps))\n            print('search_maps', search_maps)\n            file_contents = path2content(search_maps, split_by=directory)\n            patch_strings = []\n            for i, output in enumerate(patch_response_texts):\n                # print('test new gcm\\n', output)\n                git_diffs, raw_git_diffs, contents, edited_files, new_contents = post_process_raw_output(\n                    raw_output_text=output, file_contents=file_contents\n                )\n                patch_strings.append(git_diffs)\n                print('git_diffs\\n', git_diffs)\n        except:\n            return [None], [None]\n    else:\n        patch_strings = [extract_patch_string(response_text) for response_text in patch_response_texts]\n\n    # try:\n    file_content_strings = [x for i,x in enumerate(file_content_strings) if file_content_strings[i]!='']\n    file_content_strings = [x for i,x in enumerate(file_content_strings) if patch_strings[i]]\n    patch_response_texts = [x for i,x in enumerate(patch_response_texts) if patch_strings[i]]\n    if not use_gcm:\n        patch_strings = [fix_num_patch(x) for x in patch_strings if x]\n\n    for i in patch_strings:\n        print('patch_strings')\n        print(i)\n        print('xxxxxx')\n    patch_strings = [i for i in patch_strings if i]\n    # except:\n    #     return [None], [None]\n    if patch_strings:\n        if return_all:\n            return patch_strings, file_content_strings\n        return patch_strings[0]\n    return [None for _ in range(len(file_content_strings))], file_content_strings\n","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:56.029718Z","iopub.execute_input":"2025-08-05T20:00:56.030053Z","iopub.status.idle":"2025-08-05T20:00:56.040696Z","shell.execute_reply.started":"2025-08-05T20:00:56.030039Z","shell.execute_reply":"2025-08-05T20:00:56.040229Z"},"papermill":{"duration":0.030181,"end_time":"2025-03-08T18:06:39.426402","exception":false,"start_time":"2025-03-08T18:06:39.396221","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%writefile make_copy_env.py\nimport shutil\nimport glob\nimport os\nBATCH_SIZE = 8\nx = glob.glob('/kaggle/working/q*')\nx = [int(i.split('/kaggle/working/q')[-1]) for i in x]\nx.sort()\nfor i in range(BATCH_SIZE):\n    shutil.copytree(f'/kaggle/working/q{x[-1]}/repo', f'/kaggle/working/q{x[-1]}/repo_patch_{i}', dirs_exist_ok=True)\n","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:56.041143Z","iopub.execute_input":"2025-08-05T20:00:56.041308Z","iopub.status.idle":"2025-08-05T20:00:56.049285Z","shell.execute_reply.started":"2025-08-05T20:00:56.041294Z","shell.execute_reply":"2025-08-05T20:00:56.048811Z"},"papermill":{"duration":0.027089,"end_time":"2025-03-08T18:06:39.519545","exception":false,"start_time":"2025-03-08T18:06:39.492456","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def setup_and_get_initial_error(repo_archive: io.BytesIO, pip_packages_archive: io.BytesIO, env_setup_cmds_templates: list[str], repo_path: str, skip_write=False) -> str:\n    \"\"\" Replace this function with your inference code.\n    Args:\n        problem_statement: The text of the git issue.\n        repo_path: A BytesIO buffer path with a .tar containing the codebase that must be patched. The gateway will make this directory available immediately before this function runs.\n        pip_packages_archive: A BytesIO buffer path with a .tar containing the wheel files necessary for running unit tests.\n        env_setup_cmds_templates: Commands necessary for installing the pip_packages_archive.\n    \"\"\"\n\n    # Unpack the codebase to be patched into a directory that won't be exported when\n    # the notebook is saved.\n    archive_path = '/tmp/repo_archive.tar'\n    if not skip_write:\n        with open(archive_path, 'wb') as f:\n            f.write(repo_archive.read())\n    if os.path.exists(repo_path):\n        shutil.rmtree(repo_path)\n    shutil.unpack_archive(archive_path, extract_dir=repo_path)\n    os.remove(archive_path)\n\n    # return \"\"  # TODO: continue running tests when ok\n\n    \"\"\"\n    Unpack pip_packages if you want to run unit tests on your patch.\n    Note that editing unit tests with your patch -- even to add valid tests -- can cause your submission to be flagged as a failure.\n    Most of the relevant repos use pytest for running tests. You will almost certainly need to run only a subset of the unit tests to avoid running out of inference time.\n    \"\"\"\n    pip_archive_dir = '/tmp/pip_packages_archive.tar'\n    if not skip_write:\n        with open(pip_archive_dir, 'wb') as f:\n            f.write(pip_packages_archive.read())\n    pip_packages_path = '/path/to/pip_packages'\n    if os.path.exists(pip_packages_path):\n        shutil.rmtree(pip_packages_path)\n    shutil.unpack_archive(pip_archive_dir, extract_dir=pip_packages_path)\n    os.remove(pip_archive_dir)\n\n    # Get env setup cmds by setting the pip_packages_path\n    # ['uv venv --python python3.11', 'source .venv/bin/activate', 'uv pip install --no-index --find-links=/path/to/pip_packages --link-mode=symlink -e . && uv pip install --no-index --find-links=/path/to/pip_packages --link-mode=symlink pytest', 'uv pip install --no-index --find-links=/path/to/pip_packages --link-mode=symlink pytest-xdist']\n    env_setup_cmds = [cmd.format(pip_packages_path=pip_packages_path) for cmd in env_setup_cmds_templates]\n    if env_setup_cmds[0] == 'uv venv --python python3.11':\n        env_setup_cmds[0] = 'uv venv --python python3.11 --relocatable'\n    # Install pytest-xdist to run parallel tests\n    for cmd in env_setup_cmds:\n        if 'pytest-xdist' in cmd:\n            break\n    else:\n        pass\n\n    env_setup_cmds.append('python -u /kaggle/working/make_copy_env.py')\n    print('repo_path', repo_path)\n    print('repo_archive', repo_archive)\n    print('pip_packages_path', pip_packages_path)\n    print('pip_packages_archive', pip_packages_archive)\n    print('env_setup_cmds', env_setup_cmds)\n\n    log = open(f'{str(len(env_setup_cmds[-1]))}.txt', 'a')\n    print(len(env_setup_cmds[-1]))\n    log.write(\" && \".join(env_setup_cmds))\n    log.flush()  # <-- here's something not to forget!\n    xx = subprocess.Popen(\n    # xx = subprocess.run(\n            \"\\n\".join(env_setup_cmds),\n            shell=True,\n            cwd=repo_path,\n            executable=\"/bin/bash\",\n            stdout=log,\n            stderr=log,\n            text=True\n\n        )\n    return env_setup_cmds","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:56.049826Z","iopub.execute_input":"2025-08-05T20:00:56.04999Z","iopub.status.idle":"2025-08-05T20:00:56.05847Z","shell.execute_reply.started":"2025-08-05T20:00:56.049977Z","shell.execute_reply":"2025-08-05T20:00:56.057951Z"},"papermill":{"duration":0.029189,"end_time":"2025-03-08T18:06:39.568482","exception":false,"start_time":"2025-03-08T18:06:39.539293","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def apply_patch(repo_path: str, patch_string: str, rm=False, dry_run=False) -> bool:\n    \"\"\"Apply the patch using patch command.\n    Returns True if patch was applied successfully.\"\"\"\n    patch_file = os.path.join(repo_path, \"fix.diff\")\n    print('start def apply_patch at', repo_path)\n    try:\n        # Write patch to file\n        with open(patch_file, \"w\") as f:\n            f.write(patch_string)\n\n        # Apply patch\n        result = subprocess.run(\n            ' '.join([\"patch\", \"-p1\", '-i', './fix.diff']) if not dry_run else ' '.join([\"patch\", \"--dry-run\", \"-p1\", '-i', './fix.diff']),\n            # input=patch_string,\n            text=True,\n            shell=True,\n            cwd=repo_path,\n            executable=\"/bin/bash\",\n            stdout=subprocess.PIPE,\n            stderr=subprocess.STDOUT\n        )\n        print(result.stdout)\n        print(result.stderr)\n        return result.returncode == 0\n    except Exception as e:\n        print(f\"Error applying patch: {e}\")\n        return False\n    finally:\n        if rm:\n            # Clean up patch file\n            if os.path.exists(patch_file):\n                os.remove(patch_file)","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:56.059046Z","iopub.execute_input":"2025-08-05T20:00:56.059219Z","iopub.status.idle":"2025-08-05T20:00:56.070108Z","shell.execute_reply.started":"2025-08-05T20:00:56.059206Z","shell.execute_reply":"2025-08-05T20:00:56.069567Z"},"papermill":{"duration":0.026611,"end_time":"2025-03-08T18:06:39.614758","exception":false,"start_time":"2025-03-08T18:06:39.588147","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import io\nfrom typing import Optional, List\n\nskip_prediction: bool = False\n\nprob_count = 0\ndef predict(\n    problem_statement: str,\n    repo_archive: io.BytesIO,\n    pip_packages_archive: io.BytesIO,\n    env_setup_cmds_templates: List[str],\n) -> Optional[str]:\n\n    global prob_count\n    # global df\n    USE_GCM = True\n    prob_count+=1\n    # if prob_count!=1:\n    #     return None\n\n    for i in range(300):\n        if os.path.exists(f'/kaggle/working/q{i}'):\n            shutil.rmtree(f'/kaggle/working/q{i}')\n\n    global skip_prediction\n\n    print('PROBLEM STATEMENT\\n', problem_statement)\n\n    repo_path: str = f\"/kaggle/working/q{prob_count}/repo\"\n    if not os.path.exists(repo_path):\n        os.makedirs(repo_path)\n\n    env_setup_cmds = setup_and_get_initial_error(repo_archive, pip_packages_archive, env_setup_cmds_templates, repo_path)\n\n    patch_strings, file_content_strings = predict_inner(\n        problem_statement=problem_statement, directory=repo_path, return_all=True, use_gcm=USE_GCM\n    )\n\n    try:\n        file_content_strings = [x for i,x in enumerate(file_content_strings) if patch_strings[i]]\n\n        patch_strings = [i for i in patch_strings if i]\n        if not USE_GCM:\n            patch_strings = [fix_num_patch(i) for i in patch_strings]\n\n        for patch in patch_strings:\n            print('xxxxxx')\n            print(patch)\n\n        # Check if the patch can be applied\n        success_patchs = []\n        success_file_content_strings = []\n        for i, patch in enumerate(patch_strings):\n            if apply_patch(repo_path, patch_string=patch, rm=True, dry_run=True):\n                success_patchs.append(patch)\n                success_file_content_strings.append(file_content_strings[i])\n        patch_strings = success_patchs\n        file_content_strings = success_file_content_strings\n        print('DONE CHECK PATCH', len(patch_strings))\n        if not patch_strings:\n            # if initial_test_process:\n            #     initial_test_process.kill()\n            shutil.rmtree(f\"/kaggle/working/q{prob_count}\")\n            print(\"No patches generated\")\n            return None\n        import time\n        return patch_strings[0]\n    except:\n        if os.path.exists(repo_path):\n            shutil.rmtree(repo_path)\n        return None","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:56.070679Z","iopub.execute_input":"2025-08-05T20:00:56.070862Z","iopub.status.idle":"2025-08-05T20:00:56.081437Z","shell.execute_reply.started":"2025-08-05T20:00:56.070848Z","shell.execute_reply":"2025-08-05T20:00:56.080942Z"},"papermill":{"duration":0.036757,"end_time":"2025-03-08T18:06:39.715031","exception":false,"start_time":"2025-03-08T18:06:39.678274","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nimport zipfile\n\n# !mkdir -p /kaggle/tmp/konwinski-prize-alt\nos.makedirs(\"/kaggle/tmp/konwinski-prize-alt\", exist_ok=True)\n\ntry:\n    with zipfile.ZipFile(\"/kaggle/input/konwinski-prize/data.a_zip\", \"r\") as zip_ref:\n        zip_ref.extractall(\"/kaggle/tmp/konwinski-prize-alt/\")\nexcept:\n    pass","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:00:56.081921Z","iopub.execute_input":"2025-08-05T20:00:56.082083Z","iopub.status.idle":"2025-08-05T20:01:00.320752Z","shell.execute_reply.started":"2025-08-05T20:00:56.082066Z","shell.execute_reply":"2025-08-05T20:01:00.320181Z"},"papermill":{"duration":4.900793,"end_time":"2025-03-08T18:06:44.719531","exception":false,"start_time":"2025-03-08T18:06:39.818738","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndef get_problem(problem_index: int) -> Tuple[str, str, io.BytesIO]:\n    df = pd.read_parquet(\"/kaggle/tmp/konwinski-prize-alt/data/data.parquet\")\n\n    problem_statement: str = df[\"problem_statement\"][problem_index]\n    repo_path: str = (\n        f\"/kaggle/tmp/konwinski-prize-alt/data/repos/repo__{df['instance_id'][problem_index]}\"\n    )\n\n    import shutil\n    import tempfile\n\n    with tempfile.TemporaryDirectory() as tmpdir:\n        shutil.make_archive(os.path.join(tmpdir, \"a_repo\"), \"tar\", repo_path)\n        with open(os.path.join(tmpdir, \"a_repo.tar\"), \"rb\") as f:\n            repo_archive = io.BytesIO(f.read())\n\n    return problem_statement, repo_path, repo_archive","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:01:00.321393Z","iopub.execute_input":"2025-08-05T20:01:00.321585Z","iopub.status.idle":"2025-08-05T20:01:00.326211Z","shell.execute_reply.started":"2025-08-05T20:01:00.32157Z","shell.execute_reply":"2025-08-05T20:01:00.325688Z"},"papermill":{"duration":0.026195,"end_time":"2025-03-08T18:06:44.766228","exception":false,"start_time":"2025-03-08T18:06:44.740033","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"demo_problem_index: int = 0\n\nif os.getenv(\"KAGGLE_KERNEL_RUN_TYPE\") == \"Interactive\" and not os.getenv(\n    \"KAGGLE_IS_COMPETITION_RERUN\"\n):\n    problem_statement, repo_path, repo_archive = get_problem(\n        problem_index=demo_problem_index\n    )\n\n    print(repo_path)\n    print(problem_statement)\n    print(len(list(repo_archive)))\n    print(len(list(repo_archive)))","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:01:00.326718Z","iopub.execute_input":"2025-08-05T20:01:00.326889Z","iopub.status.idle":"2025-08-05T20:01:00.805208Z","shell.execute_reply.started":"2025-08-05T20:01:00.326875Z","shell.execute_reply":"2025-08-05T20:01:00.804658Z"},"papermill":{"duration":0.024879,"end_time":"2025-03-08T18:06:44.810885","exception":false,"start_time":"2025-03-08T18:06:44.786006","status":"completed"},"tags":[],"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"skip_prediction = False\nprob_count = 0\ninference_server = (\n    kaggle_evaluation.konwinski_prize_inference_server.KPrizeInferenceServer(\n        get_number_of_instances, predict\n    )\n)\n\nif os.getenv(\"KAGGLE_IS_COMPETITION_RERUN\"):\n    inference_server.serve()\nelse:\n    inference_server.run_local_gateway(\n        data_paths=(\n            \"/kaggle/input/konwinski-prize/\",  # Path to the entire competition dataset\n            \"/kaggle/tmp/konwinski-prize/\",  # Path to a scratch directory for unpacking data.a_zip.\n        ),  # type: ignore\n        use_concurrency=True,  # This can safely be disabled for purposes of local testing if necessary.\n    )","metadata":{"execution":{"iopub.status.busy":"2025-08-05T20:01:00.805819Z","iopub.execute_input":"2025-08-05T20:01:00.805995Z","iopub.status.idle":"2025-08-05T20:09:10.873431Z","shell.execute_reply.started":"2025-08-05T20:01:00.805981Z","shell.execute_reply":"2025-08-05T20:09:10.872672Z"},"papermill":{"duration":682.324085,"end_time":"2025-03-08T18:18:07.154787","exception":false,"start_time":"2025-03-08T18:06:44.830702","status":"completed"},"scrolled":true,"tags":[],"trusted":true},"outputs":[],"execution_count":null}]}