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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ All notable changes to SkillOpt are documented here. This project adheres to
value is rejected with a clear error.

### Fixed
- The SpreadsheetBench ReAct auto-verify finds the output workbook named by
`solution.py`. A relative `OUTPUT_PATH` was checked against the process
working directory instead of the work directory where the script runs, and
a trailing comment on the line (`OUTPUT_PATH = "out.xlsx" # ...`) became part
of the path. Either case reported "Output file not found" for a correct
solution, which could make the model rewrite a working script. Both are now
handled (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
14 changes: 12 additions & 2 deletions skillopt/envs/spreadsheetbench/react_agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,21 @@ def _auto_verify(work_dir: str) -> str:
stripped = line.strip()
if stripped.startswith("OUTPUT_PATH"):
try:
val = stripped.split("=", 1)[1].strip()
output_path = val.strip("'\"").strip()
value = stripped.split("=", 1)[1].strip()
if value[:1] in ("'", '"'):
# Quoted path: keep only what is between the quotes, so a
# trailing comment is not read as part of the path.
end = value.find(value[0], 1)
output_path = value[1:end] if end != -1 else None
else:
output_path = value.split("#", 1)[0].strip() or None
except Exception: # noqa: BLE001
pass
break
# solution.py runs with work_dir as its working directory, so a relative
# OUTPUT_PATH is relative to work_dir, not to the process running this check.
if output_path and not os.path.isabs(output_path):
output_path = os.path.join(work_dir, output_path)

if not output_path or not os.path.exists(output_path):
xlsx_files = [
Expand Down
78 changes: 78 additions & 0 deletions tests/test_spreadsheetbench_auto_verify.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
"""ReAct auto-verify finds the output workbook named by solution.py.

``solution.py`` runs with its work directory as the working directory, so a
relative OUTPUT_PATH is relative to that directory. Before the fix,
``_auto_verify`` also kept the text after a closing quote (a trailing comment)
as part of the path, and it resolved a relative path against the process
working directory. Either case made a correct solution report
"Output file not found".
"""

from __future__ import annotations

import pytest

openpyxl = pytest.importorskip("openpyxl")

from skillopt.envs.spreadsheetbench.react_agent import _auto_verify # noqa: E402

FOUND = "[AUTO-VERIFY] Output file exists"
NOT_FOUND = "Output file not found"


@pytest.fixture
def work_dir(tmp_path, monkeypatch):
"""A work directory with out/result.xlsx, and a process cwd elsewhere."""
work = tmp_path / "work"
(work / "out").mkdir(parents=True)
wb = openpyxl.Workbook()
wb.active["A1"] = 1
wb.save(work / "out" / "result.xlsx")

elsewhere = tmp_path / "elsewhere"
elsewhere.mkdir()
monkeypatch.chdir(elsewhere)
return work


def _write_solution(work, line):
(work / "solution.py").write_text(line + "\nprint('done')\n", encoding="utf-8")


@pytest.mark.parametrize(
"line",
[
'OUTPUT_PATH = "out/result.xlsx"',
"OUTPUT_PATH = 'out/result.xlsx'",
'OUTPUT_PATH = "out/result.xlsx" # saved here',
"OUTPUT_PATH = 'out/result.xlsx' # the answer workbook",
"OUTPUT_PATH = out/result.xlsx",
"OUTPUT_PATH = out/result.xlsx # unquoted with comment",
],
)
def test_relative_output_path_is_found(work_dir, line):
_write_solution(work_dir, line)

report = _auto_verify(str(work_dir))

assert FOUND in report
assert NOT_FOUND not in report


def test_absolute_output_path_with_comment_is_found(work_dir):
absolute = work_dir / "out" / "result.xlsx"
_write_solution(work_dir, f"OUTPUT_PATH = '{absolute}' # saved here")

report = _auto_verify(str(work_dir))

assert FOUND in report
assert NOT_FOUND not in report


def test_missing_output_still_reports_a_warning(work_dir):
_write_solution(work_dir, 'OUTPUT_PATH = "out/missing.xlsx" # not written')

report = _auto_verify(str(work_dir))

assert NOT_FOUND in report
assert FOUND not in report