Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions skillopt/envs/spreadsheetbench/codegen_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import json
import os
import random
import re
import signal
import time

Expand Down Expand Up @@ -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)
Expand Down
44 changes: 44 additions & 0 deletions tests/test_codegen_extract_python_block.py
Original file line number Diff line number Diff line change
@@ -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)"