diff --git a/CHANGELOG.md b/CHANGELOG.md index 7542320d..489a097a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -154,6 +154,11 @@ All notable changes to SkillOpt are documented here. This project adheres to value is rejected with a clear error. ### Fixed +- SpreadsheetBench code extraction picks the Python block when a shell block + comes first. `extract_code` returned the first fenced block in the reply, so a + shell example such as `pip install openpyxl` shown before the script was run as + Python and failed with a SyntaxError. A block labelled `python`, `py` or with no + label is now preferred, and the old behaviour is kept when there is none (thanks @Shizoqua). - `scripts/train.py` accepts `--slow_update_gate_with_selection`. The config key `optimizer.slow_update_gate_with_selection` exists, but the flag was never added, so `--slow_update_gate_with_selection true` failed with an diff --git a/skillopt/envs/spreadsheetbench/codegen_agent.py b/skillopt/envs/spreadsheetbench/codegen_agent.py index e3152891..1ad7691b 100644 --- a/skillopt/envs/spreadsheetbench/codegen_agent.py +++ b/skillopt/envs/spreadsheetbench/codegen_agent.py @@ -13,6 +13,7 @@ import json import os import random +import re import signal import time @@ -123,10 +124,18 @@ def _preview_workbook(path: str, max_rows: int = 5, max_cols: int = 20) -> str: # ── Code extraction (same as official prompt.py) ──────────────────────────── +_PYTHON_FENCE_RE = re.compile(r"```([^\n`]*)\n(.*?)```", re.DOTALL) + + def extract_code(text: str) -> str: """Extract the first ```python``` fenced code block from LLM output.""" if "```" not in text: return text.strip() + # Prefer a block labelled as Python (or unlabelled). A shell example shown + # before the script must not be run as Python. + for lang, body in _PYTHON_FENCE_RE.findall(text): + if lang.strip().lower() in {"python", "py", ""}: + return body.strip() start = text.find("```") nl = text.find("\n", start) end = text.find("```", nl + 1) diff --git a/tests/test_codegen_extract_python_block.py b/tests/test_codegen_extract_python_block.py new file mode 100644 index 00000000..9bfb0c6e --- /dev/null +++ b/tests/test_codegen_extract_python_block.py @@ -0,0 +1,44 @@ +"""extract_code returns the Python block when a shell block comes first. + +A reply that shows a shell command before the script, such as +```bash ... ``` followed by a ```python``` block, used to return the shell +text, which the executor ran as Python and failed with a SyntaxError. +""" + +import pytest + +from skillopt.envs.spreadsheetbench.codegen_agent import extract_code + + +@pytest.mark.parametrize( + "reply", + [ + "First install:\n```bash\npip install openpyxl\n```\nThen run:\n```python\nimport openpyxl\nprint(1)\n```", + "```sh\npip install openpyxl\n```\n```py\nimport openpyxl\nprint(1)\n```", + "Shell first:\n```console\n$ pip install openpyxl\n```\n```Python\nimport openpyxl\nprint(1)\n```", + ], +) +def test_python_block_wins_over_an_earlier_shell_block(reply): + assert extract_code(reply) == "import openpyxl\nprint(1)" + + +def test_python_block_first_is_returned_unchanged(): + reply = "```python\nimport openpyxl\nprint(1)\n```\n```bash\npip install openpyxl\n```" + + assert extract_code(reply) == "import openpyxl\nprint(1)" + + +def test_unlabelled_block_is_still_accepted(): + reply = "Here:\n```\nprint(1)\n```" + + assert extract_code(reply) == "print(1)" + + +def test_reply_with_only_a_shell_block_keeps_the_previous_behaviour(): + reply = "```bash\npip install openpyxl\n```" + + assert extract_code(reply) == "pip install openpyxl" + + +def test_reply_without_a_fence_is_returned_stripped(): + assert extract_code(" print(1)\n") == "print(1)"