Datasets:
Download artifacts/runtime-source/bash_timeout.py from suunny/terminal-agent-rl: direct link, hf CLI and curl.
- Browser
- Download file 4.29 kB
-
https://huggingface.co/datasets/suunny/terminal-agent-rl/resolve/main/artifacts/runtime-source/bash_timeout.py
- Command line
-
hf download hf://datasets/suunny/terminal-agent-rl/artifacts/runtime-source/bash_timeout.py
-
curl -L -o bash_timeout.py https://huggingface.co/datasets/suunny/terminal-agent-rl/resolve/main/artifacts/runtime-source/bash_timeout.py
4.29 kB
| """Narrow Bash executor patch: 60s deadline and explicit invalid-command feedback.""" | |
| import ast | |
| import hashlib | |
| import importlib.metadata | |
| import importlib.util | |
| import py_compile | |
| from pathlib import Path | |
| ORIGINAL_SHA256 = "0fb1503bad84d4f26b6430f216af6be1daf94a4a767804a73625c055b26e4886" | |
| PREVIOUS_PATCH_SHA256 = "059d4734686bad1ea69b9b09a12728f41b77120c911ddbcd6d9263762790508a" | |
| def run_bash(command: str, timeout: float = 60) -> str: | |
| """Stop a timed-out shell's process group and return its partial output.""" | |
| import os | |
| import signal | |
| import subprocess | |
| if not isinstance(command, str) or not command.strip(): | |
| return ('error: bash requires a non-empty string parameter named command. ' | |
| 'No command was executed. Resend as <function=bash>\n' | |
| '<parameter=command>\nyour shell command\n</parameter>\n</function>. ' | |
| 'The opening parameter tag must end with >.') | |
| process = None | |
| try: | |
| process = subprocess.Popen( | |
| ["bash", "-c", command], stdin=subprocess.DEVNULL, | |
| stdout=subprocess.PIPE, stderr=subprocess.PIPE, start_new_session=True, | |
| ) | |
| try: | |
| stdout, stderr = process.communicate(timeout=timeout) | |
| except subprocess.TimeoutExpired as expired: | |
| try: | |
| os.killpg(process.pid, signal.SIGKILL) | |
| except ProcessLookupError: | |
| pass | |
| try: | |
| stdout, stderr = process.communicate(timeout=1) | |
| except subprocess.TimeoutExpired as drain: | |
| # A descendant can escape the process group and retain a pipe. | |
| # Never wait indefinitely for its EOF; sandbox teardown owns it. | |
| stdout, stderr = drain.output or expired.output or b"", drain.stderr or expired.stderr or b"" | |
| process.stdout.close() | |
| process.stderr.close() | |
| process.wait(timeout=1) | |
| output = (stdout + stderr).decode("utf-8", errors="replace") | |
| return output + f"\n[Command timed out after {timeout:g}s; process group killed. Partial file changes remain.]" | |
| return (stdout + stderr).decode("utf-8", errors="replace") | |
| except Exception as error: | |
| return f"error: {error}" | |
| finally: | |
| if process is not None and process.returncode is None: | |
| try: | |
| os.killpg(process.pid, signal.SIGKILL) | |
| except ProcessLookupError: | |
| pass | |
| process.wait(timeout=1) | |
| def function_span(source: str): | |
| function = next(node for node in ast.parse(source).body | |
| if isinstance(node, ast.FunctionDef) and node.name == "run_bash") | |
| return function.lineno - 1, function.end_lineno | |
| def install(): | |
| """Replace only the known upstream function, before the harness bundles it.""" | |
| if importlib.metadata.version("verifiers") not in ("0.4.0", "0.4.1.dev12"): | |
| raise RuntimeError("Timeout patch requires reviewed Verifiers 0.4.0 or 0.4.1.dev12") | |
| package = importlib.util.find_spec("verifiers") | |
| path = Path(package.origin).parent / "v1/harnesses/utils/core.py" | |
| source = path.read_text() | |
| lines = source.splitlines(keepends=True) | |
| start, end = function_span(source) | |
| own_lines = Path(__file__).read_text().splitlines(keepends=True) | |
| first, last = function_span("".join(own_lines)) | |
| replacement = "".join(own_lines[first:last]) | |
| current = "".join(lines[start:end]) | |
| if current == replacement: | |
| print("Bash 60s patch already installed") | |
| else: | |
| if hashlib.sha256(current.encode()).hexdigest() not in (ORIGINAL_SHA256, PREVIOUS_PATCH_SHA256): | |
| raise RuntimeError("Upstream Bash executor changed; refusing an unreviewed patch") | |
| updated = "".join(lines[:start]) + replacement + "".join(lines[end:]) | |
| ast.parse(updated) | |
| path.write_text(updated) | |
| print("Installed Bash 60s patch (one executor function)") | |
| # uv images can ship unchecked-hash bytecode that ignores changed source. | |
| for cached in (path.parent / "__pycache__").glob("core.*.pyc"): | |
| cached.unlink() | |
| py_compile.compile(str(path), doraise=True, invalidation_mode=py_compile.PycInvalidationMode.CHECKED_HASH) | |
| if __name__ == "__main__": | |
| install() | |