Download tests/test_session.py from tailot/codex-python: direct link, hf CLI and curl.
- Browser
- Download file 6.79 kB
-
https://huggingface.co/datasets/tailot/codex-python/resolve/1ed2d7f095db52138ba410054960ec4133926f85/tests/test_session.py
- Command line
-
hf download hf://datasets/tailot/codex-python@1ed2d7f095db52138ba410054960ec4133926f85/tests/test_session.py
-
curl -L -o test_session.py https://huggingface.co/datasets/tailot/codex-python/resolve/1ed2d7f095db52138ba410054960ec4133926f85/tests/test_session.py
6.79 kB
| """Tests for PythonWorkspaceSession transactional editing, verification, and rollback.""" | |
| from __future__ import annotations | |
| from pathlib import Path | |
| from typing import Any, Dict, Tuple | |
| from lingologos_python.session import PythonWorkspaceSession, SessionResult, _verify_probe | |
| def test_verify_clean_workspace_reaches_verified_assurance_3(sample_pkg: Path) -> None: | |
| session = PythonWorkspaceSession(sample_pkg) | |
| res = session.verify("src/mathops.py") | |
| assert res.verdict == "VERIFIED" | |
| assert res.assurance == 3 | |
| assert res.committed is False | |
| def test_edit_file_commits_only_when_verified(sample_pkg: Path) -> None: | |
| session = PythonWorkspaceSession(sample_pkg) | |
| res = session.edit_file( | |
| "src/mathops.py", | |
| "return left + right", | |
| "return int(left) + int(right)", | |
| commit=True, | |
| ) | |
| assert res.verdict == "VERIFIED" | |
| assert res.assurance == 3 | |
| assert res.committed is True | |
| assert "return int(left) + int(right)" in (sample_pkg / "src" / "mathops.py").read_text( | |
| encoding="utf-8" | |
| ) | |
| def test_edit_file_does_not_touch_disk_when_tests_fail(sample_pkg: Path) -> None: | |
| session = PythonWorkspaceSession(sample_pkg) | |
| before = (sample_pkg / "src" / "mathops.py").read_text(encoding="utf-8") | |
| res = session.edit_file( | |
| "src/mathops.py", | |
| "return left + right", | |
| "return left - right", | |
| commit=True, | |
| ) | |
| after = (sample_pkg / "src" / "mathops.py").read_text(encoding="utf-8") | |
| assert res.verdict == "UNKNOWN" | |
| assert res.reason == "GATE_REJECTED" | |
| assert res.committed is False | |
| assert before == after | |
| def test_edit_file_refuses_protected_and_traversal_paths(sample_pkg: Path) -> None: | |
| (sample_pkg / ".env").write_text("KEY=1\n", encoding="utf-8") | |
| session = PythonWorkspaceSession(sample_pkg) | |
| env_res = session.edit_file(".env", "KEY=1", "KEY=2", commit=True) | |
| assert env_res.verdict == "UNKNOWN" | |
| assert env_res.reason == "PROTECTED_PATH" | |
| assert env_res.committed is False | |
| assert (sample_pkg / ".env").read_text(encoding="utf-8") == "KEY=1\n" | |
| esc_res = session.edit_file("../outside.py", "a = 1", "a = 2", commit=True) | |
| assert esc_res.verdict == "UNKNOWN" | |
| assert esc_res.reason in ("PROTECTED_PATH", "NOT_APPLIED") | |
| assert esc_res.committed is False | |
| def test_create_and_delete_file_transactions(sample_pkg: Path) -> None: | |
| session = PythonWorkspaceSession(sample_pkg) | |
| created = session.create_file( | |
| "src/helper.py", | |
| '"""Extra module."""\n\n\ndef square(x: int) -> int:\n return x * x\n', | |
| commit=True, | |
| ) | |
| assert created.verdict == "VERIFIED" | |
| assert created.committed is True | |
| assert (sample_pkg / "src" / "helper.py").is_file() | |
| deleted = session.delete_file("src/helper.py", commit=True) | |
| assert deleted.verdict == "VERIFIED" | |
| assert deleted.committed is True | |
| assert not (sample_pkg / "src" / "helper.py").exists() | |
| def test_retry_loop_stops_on_identical_failure_stall(sample_pkg: Path) -> None: | |
| session = PythonWorkspaceSession(sample_pkg) | |
| def repeated_bad_edit(attempt: int, last_result: object) -> tuple[str, str]: | |
| return ("return left + right", "return left - right") | |
| final, attempts = session.run_retry_loop("src/mathops.py", repeated_bad_edit, max_attempts=5) | |
| assert final.verdict == "UNKNOWN" | |
| assert len(attempts) == 2 | |
| assert any(c.get("role") == "adversary_loop" for c in final.dossier.get("checkers", [])) | |
| def test_verify_probe_annotates_a_line_that_keeps_the_file_parsable() -> None: | |
| text = '"""Module doc.\n\nSpread over several lines.\n"""\n\n\ndef go() -> int:\n return 1\n' | |
| search, replace, original = _verify_probe(text) | |
| assert search == "def go() -> int:" | |
| assert replace == search + " # codex-verify-probe\n" | |
| assert original is text | |
| compile(original.replace(search, replace, 1), "module.py", "exec") | |
| empty_search, _empty_replace, empty_text = _verify_probe("") | |
| assert empty_text == "# empty\n" | |
| assert empty_search in empty_text | |
| def test_edit_file_refuses_when_the_file_moved_during_the_run(sample_pkg: Path, monkeypatch) -> None: | |
| session = PythonWorkspaceSession(sample_pkg) | |
| real_run = session._run_six_nodes | |
| def run_with_a_moving_workspace(case: Dict[str, Any]) -> Tuple[Any, ...]: | |
| path = sample_pkg / "src" / "mathops.py" | |
| path.write_text( | |
| path.read_text(encoding="utf-8", newline="").replace( | |
| "return left + right", "return left + right # moved" | |
| ), | |
| encoding="utf-8", | |
| newline="", | |
| ) | |
| return real_run(case) | |
| monkeypatch.setattr(session, "_run_six_nodes", run_with_a_moving_workspace) | |
| res = session.edit_file( | |
| "src/mathops.py", | |
| "return left + right", | |
| "return int(left) + int(right)", | |
| commit=True, | |
| ) | |
| assert res.verdict == "REFUSED" | |
| assert res.reason == "PATCH_NOT_APPLIED" | |
| assert res.committed is False | |
| on_disk = (sample_pkg / "src" / "mathops.py").read_text(encoding="utf-8") | |
| assert "# moved" in on_disk | |
| assert "int(left)" not in on_disk | |
| def test_create_file_refuses_when_a_file_landed_at_its_path_during_the_run( | |
| sample_pkg: Path, monkeypatch | |
| ) -> None: | |
| session = PythonWorkspaceSession(sample_pkg) | |
| real_run = session._run_six_nodes | |
| def run_that_lands_a_file(case: Dict[str, Any]) -> Tuple[Any, ...]: | |
| (sample_pkg / "src" / "landed.py").write_text("VALUE = 1\n", encoding="utf-8", newline="\n") | |
| return real_run(case) | |
| monkeypatch.setattr(session, "_run_six_nodes", run_that_lands_a_file) | |
| res = session.create_file("src/landed.py", "VALUE = 2\n", commit=True) | |
| assert res.verdict == "REFUSED" | |
| assert res.reason == "PATCH_NOT_APPLIED" | |
| assert res.committed is False | |
| assert (sample_pkg / "src" / "landed.py").read_text(encoding="utf-8") == "VALUE = 1\n" | |
| def test_retry_loop_spots_repetition_even_without_a_patch_signature( | |
| sample_pkg: Path, monkeypatch | |
| ) -> None: | |
| session = PythonWorkspaceSession(sample_pkg) | |
| def always_unknown(rel_path: str, search: str, replace: str, **kwargs: Any) -> SessionResult: | |
| return SessionResult( | |
| case_id=str(kwargs.get("case_id", "python-edit")), | |
| verdict="UNKNOWN", | |
| assurance=0, | |
| reason="GATE_REJECTED", | |
| detail="", | |
| committed=False, | |
| ) | |
| monkeypatch.setattr(session, "edit_file", always_unknown) | |
| def same_proposal(attempt: int, last_result: object) -> tuple: | |
| return ("return left + right", "return left - right") | |
| final, attempts = session.run_retry_loop("src/mathops.py", same_proposal, max_attempts=5) | |
| assert final.verdict == "UNKNOWN" | |
| assert len(attempts) == 2 | |